moehrenzahn/wp-toolkit

一个围绕常见WordPress功能的面向对象框架,用于快速启动主题和插件开发。

1.1.0 2022-11-16 12:53 UTC

This package is auto-updated.

Last update: 2024-09-11 15:44:17 UTC


README

以现代、面向对象的方式与常见的WordPress插件和主题API一起工作。

Max Melzer 提供

WordPress Toolkit 是一个 Composer 模块,它提供了对常见WordPress功能的轻松访问,灵感来自像 Symfony 这样的现代PHP框架。

它的目标是提供一致的面向对象方法来开发WordPress插件和主题。

安装

要使用此工具包,请通过 Composer 将其添加到您的主题或插件中。

cd wp-content/themes/your-awesome-theme
composer init
composer require moehrenzahn/wp-toolkit

然后,您可以在PHP脚本中初始化Client类。这是工具包所有功能的入口点。

<?php
$client = new \Moehrenzahn\Toolkit\Api\Client();

要求

  • Composer
  • PHP >= 7.1
  • WordPress >= 5.0(旧版本可能工作,但未经测试)

特性

  • 围绕WordPress操作和过滤器管理的方便API
  • 具有简单、自动依赖注入的对象管理器
  • 使用模型-视图-控制器架构的模板渲染
  • 添加和管理
    • JavaScript文件
    • CSS文件
    • 图像大小
    • 短代码
    • 虚拟用户帐户
    • Transients
    • 帖子类型
    • 帖子术语和元数据
    • 帖子元框
    • 帖子过滤器
    • 评论元数据
    • 评论元框
    • 设置页面
    • 管理页面
    • 管理通知
    • AJAX操作
    • POST操作

使用示例

向您的主题添加一个 CSS文件

<?php
// Entry point for all actions is the Client object
$client = new \Moehrenzahn\Toolkit\Api\Client();
$stylesheets = $client->getStylesheetManager();
$stylesheets->add('eule-stylesheet', 'src/css/style.css', '1.0.0');

添加一个具有自定义模板的 短代码

<?php
$client = new \Moehrenzahn\Toolkit\Api\Client();
$shortcodes = $client->getShortcodeManager();
$shortcodes->add(
    'my-shortcode',
    $client->getViewFactory()->create('shortcode-template.phtml')
);

使用预构建的输入类型模板创建一个 自定义设置页面

<?php
$client = new \Moehrenzahn\Toolkit\Api\Client();
$client->getAdminPageManager()->addSettingsPage(
    'Sample settings page',
    'sample-settings-page',
    getSections($client)
);

/**
 * @param \Moehrenzahn\Toolkit\Api\Client $client
 * @return Moehrenzahn\Toolkit\View\Settings\Section[]
 */
function getSections(\Moehrenzahn\Toolkit\Api\Client $client): array
{
    $sectionBuilder = $client->getSettingsSectionBuilder();
    $sectionBuilder->addSetting(
        'my-sample-setting',
        'A sample setting title',
        \Moehrenzahn\Toolkit\AdminPage\SettingsSectionBuilder::SETTING_TYPE_BOOLEAN,
        'A sample setting description.'
    );

    $sectionBuilder->addSetting(
        'my-sample-select',
        'Select your thing',
        \Moehrenzahn\Toolkit\AdminPage\SettingsSectionBuilder::SETTING_TYPE_SELECT,
        'You can also do select inputs!',
       [
           'sample-option-value' => 'Sample option label',
           'another-option-value' => 'Another option!',
       ]
    );
    
    return [$sectionBuilder->create('sample-section', 'A sample settings section')];
}

使用内置的 View 类渲染您的模板,并访问诸如 懒加载图像和部分 等功能

<?php /** @var \Moehrenzahn\Toolkit\View $view */ ?>
<h2>Here comes a lazy-loaded partial that is only loaded when it's scrolled into view:</h2>
<?php $view->renderLazyPartial(
    'your-template-to-load.phtml',
    'your-static-placeholder-template.phtml'
) ?>

使用 对象管理器 通过自动依赖解析初始化对象

<?php 
$client = new \Moehrenzahn\Toolkit\Api\Client();
$objectManager = $client->getObjectManager();
/**
 * The object manager will try to automatically and recursively
 * resolve all dependencies of the given class.
 * Don't use this for very large projects since it can impact performance.
 */
$customObject = $objectManager->getSingleton(YourCustomClass::class);

发布历史

  • 1.0.0
    • 初始版本

元数据

Max Melzer – @_maxmelzerhi@moehrenzahn.de

根据GNU通用公共许可证分发。有关更多信息,请参阅 LICENSE.md

https://github.com/moehrenzahn

贡献

  1. 将它分支(https://github.com/moehrenzahn/wp-toolkit/fork
  2. 创建您的功能分支(git checkout -b feature/fooBar
  3. 提交您的更改(git commit -am 'Add some fooBar'
  4. 推送到分支(git push origin feature/fooBar
  5. 创建一个新的拉取请求