Interact with posts, terms & users in a OOP way

Related tags

Laravel wp-models
Overview

Installation

composer require tombroucke/wp-models

Interacting with models

Create a new class for your custom post type

namespace Otomaties\Events\Models;

use Otomaties\WpModels\PostType;

class Event extends PostType {
	/**
     * This method will override the parent method which shows the post date.
     */
    public function date() : DateTime
    {
        $date = $this->meta()->get('date');
        return $date ? DateTime::createFromFormat('Ymd', $date) : null;
    }
	
    public function time() : string
    {
        return substr($this->meta()->get('time'), 0, 5);
    }

	// ... more custom methods

    public static function postType() : string
    {
        return 'event';
    }
}

Client Code

$event = new Otomaties\Events\Models\Event(420);
esc_html_e($event->getId()); // Post ID
esc_html_e($event->title()); // Post title
esc_html_e($event->slug()); // Post slug
esc_html_e($event->meta()->get('meta_key')); // Meta (single)
esc_html_e($event->meta()->get('meta_keys', false)); // Meta (multiple)
esc_html_e($event->date()->format('d-m-Y')); // Custom method
esc_html_e($event->time()); // Custom method

$event->meta()->set('meta_key', 'meta_value');
$event->meta()->add('meta_key', 'meta_value_2');

// See src/PostType.php for all default methods

Insert, update, delete & query models

Inserting

	use Otomaties\WpModels\PostTypeRepository;

	$repository = new PostTypeRepository(Event::class);
	$args = [
		'post_title' => 'Event title',
		'post_status' => 'draft',
		'post_content' => 'Event content',
		'meta_input' => [
			'key' => 'value'
		]
	]
	$event = $repository->insert($args); // returns instance of Event::class

or

	$args = [
		'post_title' => 'Event title',
		'post_status' => 'draft',
		'post_content' => 'Event content',
		'meta_input' => [
			'key' => 'value'
		]
	]
	$event = Event::insert($args);

Updating

	use Otomaties\WpModels\PostTypeRepository;

	$event = new Event(420);
	$repository = new PostTypeRepository(Event::class);
	$args = [
		'post_title' => 'Event title',
		'post_status' => 'draft',
		'post_content' => 'Event content',
		'meta_input' => [
			'key' => 'value'
		]
	]
	$event = $repository->update($event, $args); // returns instance of Event::class

or

	$event = new Event(420);
	$args = [
		'post_title' => 'Event title',
		'post_status' => 'draft',
		'post_content' => 'Event content',
		'meta_input' => [
			'key' => 'value'
		]
	]
	$event = Event::update($event, $args);

Deleting

	use Otomaties\WpModels\PostTypeRepository;

	$event = new Event(420);
	$repository = new PostTypeRepository(Event::class);
	$event = $repository->delete($event);

or

	$event = new Event(420);
	$event = Event::delete($event);

Querying

All posts

	use Otomaties\WpModels\PostTypeRepository;

	$repository = new PostTypeRepository(Event::class);
	$allEvents = $repository->find(); // Returns post type collection
	$tenEventsOffsetTen = $repository->find(null, 10, 10); // Returns post type collection

or

	$allEvents = Event::find(); // Returns post type collection

By id

	use Otomaties\WpModels\PostTypeRepository;

	$repository = new PostTypeRepository(Event::class);
	$event = $repository->find(420)->first(); // Returns PostType object (Event object in this case)

or

	$event = Event::find(420)->first(); // Returns PostType object (Event object in this case)

Custom query

	use Otomaties\WpModels\PostTypeRepository;

	$repository = new PostTypeRepository(Event::class);
	$args = [
		'meta_query' => [
			'relation' => 'OR',
			[
				'key' => 'date',
				'value' => date('Ymd'),
				'compare' => '>='
			],
			[
				'key' => 'date',
				'compare'=>'NOT EXISTS',
			]
		]
	];
	$event = $repository->find($args); // Returns PostTypeCollection

or

	$args = [
		'meta_query' => [
			'relation' => 'OR',
			[
				'key' => 'date',
				'value' => date('Ymd'),
				'compare' => '>='
			],
			[
				'key' => 'date',
				'compare'=>'NOT EXISTS',
			]
		]
	];
	$event = Event::find($args); // Returns PostTypeCollection
You might also like...
Talk is a real-time users messaging and chatting system Laravel.
Talk is a real-time users messaging and chatting system Laravel.

Laravel-Talk Talk is a Laravel 5 based user conversation (inbox) system with realtime messaging. You can easily integrate this package with any Larave

Your users do not always report errors, LaraBug does. LaraBug is a simple to use and implement error tracker built for the Laravel framework.
Your users do not always report errors, LaraBug does. LaraBug is a simple to use and implement error tracker built for the Laravel framework.

Your users do not always report errors, LaraBug does. LaraBug is a simple to use and implement error tracker built for the Laravel framework. This rep

Laravel plugin to track your users logins and alert when a suspicious login occurs
Laravel plugin to track your users logins and alert when a suspicious login occurs

Laravel Suspicious Logins Detect suspicious logins for standard Laravel authentication (base Laravel, Jetstream, etc) and notify a list of administrat

Prevent users from reusing recently used passwords

Laravel Password History Validation Prevent users from reusing recently used passwords. Installation You can install the package via composer: compose

The api help to manage wso2 users from laravel application

Laravel WSO2 Identity API User This is a Laravel library to manage WSO2 IDP users. Installation You can install the package via composer: composer req

Laravel 5 Package to Detect Users Browsers, Devices, Languages and Operating Systems

laravel-identify Laravel 5 Package to identify a User's Browser, Operating System, Language and Device Installation PHP 7.1+ or HHVM 3.3+, and Compose

Laravel package for giving admin-created accounts to users via 'set-password' email.

Invytr When making a website where users are created instead of registering themselves, you are faced with the challenge of safely giving users the ac

Associate users with roles and permissions
Associate users with roles and permissions

Associate users with permissions and roles Sponsor If you want to quickly add authentication and authorization to Laravel projects, feel free to check

Allow your users to login with FaceID/TouchID

Allow your users to login with FaceID/TouchID Allow your users to register physical authentication devices (FaceID or TouchID on iPhones & macs, finge

Owner
null
Validate your input data in a simple way, an easy way and right way. no framework required. For simple or large. project.

wepesi_validation this module will help to do your own input validation from http request POST or GET. INTEGRATION The integration is the simple thing

Boss 4 Dec 17, 2022
27Laracurl Laravel wrapper package for PHP cURL class that provides OOP interface to cURL. [10/27/2015] View Details

Laracurl Laravel cURL Wrapper for Andreas Lutro's OOP cURL Class Installation To install the package, simply add the following to your Laravel install

zjango 8 Sep 9, 2018
ODM with inheritance and OOP composition for Laravel 5+

ODM with inheritance and OOP composition for Laravel 5+ Full Documentation | CHANGELOG LODM module is intended to bring the Spiral ODM component funct

Anton Titov 21 Aug 17, 2022
Laravel Users | A Laravel Users CRUD Management Package

A Users Management Package that includes all necessary routes, views, models, and controllers for a user management dashboard and associated pages for managing Laravels built in user scaffolding. Built for Laravel 5.2, 5.3, 5.4, 5.5, 5.6, 5.7, 5.8, 6.0, 7.0 and 8.0.

Jeremy Kenedy 393 Nov 28, 2022
Laravel API wrapper to interact fluently with your Janus Media Server

Laravel API wrapper to interact fluently with your Janus Media Server. Core server interactions, as well as the video room plugin included.

Richard  Tippin 11 Aug 21, 2022
Interact with TMDB data in your Laravel application.

Laravel TMDB Installation composer require astrotomic/laravel-tmdb php artisan vendor:publish --tag=tmdb-migrations Configuration Add your TMDB API v4

Astrotomic 32 Dec 21, 2022
Easily interact and control your feature flags from Filament

Easily interact and control your feature flags from Filament

Ryan Chandler 32 Nov 29, 2022
🐍 Web application made in PHP with Laravel where you can interact via API with my Snake game which is made in Python

Snake web application Project of the web application where you can interact via API with Snake game which is available to download on it. Application

Maciek Iwaniuk 1 Nov 26, 2022
Rede Social para o mundo fitness que possibilitara as pessoas compartilharem seus treinos,dietas e posts com os amigos.

Myfit Rede Social para o mundo fitness que possibilitara as pessoas compartilharem seus treinos,dietas e posts com os amigos. Sistema de compartilhame

Alan de Medeiros Tavares 1 Dec 1, 2021
Laravel Impersonate is a plugin that allows you to authenticate as your users.

Laravel Impersonate Laravel Impersonate makes it easy to authenticate as your users. Add a simple trait to your user model and impersonate as one of y

404lab 1.6k Dec 30, 2022