tgeorgel/objectpress

是时候将WordPress视为一个对象了。

v2.6.0 2024-05-15 10:08 UTC

README

一个用于以面向对象的方式增强WordPress的PHP库。使用PSR-4类和现代开发工具管理PostTypes、分类法、角色、Eloquent模型、WP-CLI命令等。

文档

您可以在ObjectPress网站上找到详细文档。

一些您可能喜欢的功能

创建WordPress元素

使用PSR-4类轻松定义帖子类型。标签会自动生成和翻译。

<?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';
}

分类法也是如此。标签也会自动生成和翻译。

<?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',
    ];
}

高效查询数据库

您的帖子类型分类法现在是Eloquent模型。您可以使用与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();

查看此页面了解ObjectPress中的模型。在官方文档上了解更多关于Eloquent的信息。

CLI命令 & 事件调度

您可以使用PSR-4类轻松创建WP-CLI命令

<?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.
    }
}

在简洁的配置文件中定义计划

    /*
    |--------------------------------------------------------------------------
    | 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,
            ],
        ],
    ],

更多详情请参阅文档。

许可证

贡献