tgeorgel / objectpress
It's time to treat wordpress like an object.
Installs: 3 396
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: ^8.0
- amphibee/hookable: ^1.0
- amphibee/wordpress-eloquent-models: ^1.2|^2.0
- as247/wp-eloquent: ^2.1
- doctrine/inflector: ^2.0
- guzzlehttp/guzzle: ^7.3
- illuminate/config: ^9.52 || ^10.0
- illuminate/container: ^9.52 || ^10.0
- illuminate/contracts: ^9.52 || ^10.0
- illuminate/database: ^9.52 || ^10.0
- illuminate/events: ^9.52 || ^10.0
- illuminate/filesystem: ^9.52 || ^10.0
- illuminate/http: ^9.52 || ^10.0
- illuminate/pagination: ^9.52 || ^10.0
- illuminate/routing: ^9.52 || ^10.0
- illuminate/support: ^9.52 || ^10.0
- illuminate/translation: ^9.52 || ^10.0
- illuminate/validation: ^9.52 || ^10.0
- illuminate/view: ^9.52 || ^10.0
- laravel/helpers: ^1.0
- league/flysystem: ^3.0
- nesbot/carbon: ^2.45
- phpfastcache/phpfastcache: ^9.0
- symfony/error-handler: ^6.0
- symfony/var-dumper: ^5.0|^6.0
- thunderer/shortcode: ^0.7.3
Suggests
- wp-grogu/acf-manager: A powerful Object-Oriented library helping you create ACF field groups & gutemberg blocks while adding type-cast in returned values from database.
README
A PHP Library to enhance Wordpress in a Object-Oriented way. Manage PostTypes, Taxonomies, Roles, Eloquent Models, WP-CLI Commands and even more, using PSR-4 classes and modern development tools.
Documentation
You can find a detailed documentation on the ObjectPress website.
Some feature you might like
Create Wordpress elements
Define post types easily using PSR-4 classes. Labels are generated and translated automatically.
<?php namespace App\Wordpress\PostTypes; use OP\Framework\Wordpress\PostType; class Event extends PostType { public static string $name = 'event'; public $singular = 'Event'; public $plural = 'Events'; public $menu_icon = 'dashicons-store'; }
Same goes for taxonomies. Labels are also generated and translated automatically.
<?php namespace App\Wordpress\Taxonomies; use OP\Framework\Wordpress\Taxonomy; class EventType extends Taxonomy { public static string $name = 'event-type'; public $singular = 'Event type'; public $plural = 'Event types'; protected $post_types = [ 'Event', ]; }
Query your database efficiently
Your post types and taxonomies are now Eloquent models. You can query your database using the same syntax you would use on Laravel.
<?php namespace App\Models; use OP\Framework\Models\Post; class Event extends Post { /** * Wordpress post type identifier, associated to the current model. */ public static $post_type = 'event'; /** * Get this Event associated locations (taxonomy) names. * * @param int $limit Maximum number of terms to get * @return array */ public function locations(int $limit = 5): array { return $this->taxonomies ->where('taxonomy', 'locations') ->take($limit) ->pluck('term.name') ->toArray(); } } # Get all events $events = Event::all(); # Get all events attached to a specific taxonomy term (by slug) $events = Event::whereHas('taxonomies', function ($query) { $query->where('taxonomy', 'locations') ->where('term.slug', 'paris'); })->get(); # Get all events attached to a specific taxonomy term (by id) and a specific meta value $events = Event::whereHas('taxonomies', function ($query) { $query->where('taxonomy', 'locations') ->where('term_id', 123); })->whereHas('meta', function ($query) { $query->where('meta_key', 'price') ->where('meta_value', '<', 100); })->get(); # Get published events created in the last 30 days $events = Event::published() ->where('post_date', '>', now()->subDays(30)) ->get();
Read this page to learn more about models models in ObjectPress. Learn more about Eloquent on the official documentation.
CLI Commands & Event scheduling
You can easily create WP-CLI commands using PSR-4 classes.
<?php namespace App\Wordpress\Commands; use App\Classes\AlgoliaApi; use OP\Framework\Wordpress\Command; class WelcomeNewUsers extends Command { /** * The command name called with "wp {name}" * * @var string * @access protected */ protected string $name = 'mails:welcome-new-users'; /** * Index coaches into algolia for website search. * * @param array $args The command arguments as returned by WpCLI. * @return void */ public function execute(array $args = []) { // do something great. } }
Define shedules in a digest configuration file :
/* |-------------------------------------------------------------------------- | App Events schedules declaration |-------------------------------------------------------------------------- | | You can define wp schedules interval and events easily. | Events support function or class as event callback. | */ 'schedule' => [ 'events' => [ [ 'interval' => 'everyThirtyMinutes', 'class' => App\Wordpress\Commands\IndexPosts::class, ], [ 'interval' => 'everyFiveMinutes', 'class' => App\Wordpress\Commands\WelcomeNewUsers::class, 'when' => fn() :bool => defined('WP_ENV') && WP_ENV === 'production', ], ], 'intervals' => [ [ 'name' => 'everyFiveMinutes', 'label' => 'Every five minutes', 'interval' => 5 * 60, ], [ 'name' => 'everyThirtyMinutes', 'label' => 'Every thirty minutes', 'interval' => 30 * 60, ], ], ],
See more on the documentation.