jascha030/wp-plugin-lib

使用更现代的 PHP 和面向对象(OOP)方法为 WordPress 插件开发编写的类。

1.5.1 2021-10-10 18:14 UTC

This package is auto-updated.

Last update: 2024-09-11 00:42:09 UTC


README

使用 Wordpress 插件 API 的基础,使用 php@7.4 和面向对象(OOP)。

入门指南

先决条件

  • PHP ^7.4.*
  • Composer ^2
  • WordPress >= 5.5.0

安装

composer require jascha030/wp-plugin-lib

用法

使用示例是解释用法的最简单方法。

包的名称可能具有误导性,插件指的是 WP 插件 API,这意味着它可以用于插件或主题。在这个示例中,我们假设正在构建一个插件,但对于主题,我们只需在主题的 functions.php 文件中编写我们的代码,而不是主插件文件。

main-plugin-file.php

<?php
/**
 * Plugin jascha030/wp-plugin-lib
 *
 * @package   Jascha030
 * @author    Jascha van Aalst
 * @copyright 2021
 * @license   GPL-2.0+
 *
 * @wordpress-plugin
 *
 * Plugin Name: Package Tester
 * Plugin URI: https://github.com/jascha030/wp-plugin-lib
 * Description: Test plugin for package jascha030/wp-plugin-lib
 * Version: 1.0.0
 * Author: Jascha030
 * Author URI: https://github.com/jascha030.
 */

namespace Jascha030;

use Exception;
use Jascha030\PackageTest\Hookable\TestingAfterInitHookable;
use Jascha030\PackageTest\Hookable\TestingHookable;
use Jascha030\PackageTest\PackageTestPlugin;
use Jascha030\PluginLib\Container\Config\ConfigInterface;

use function Jascha030\PluginLib\Functions\buildPlugin;

/**
 * Check if WordPress' ABSPATH const is loaded
 */
if (! defined('ABSPATH')) {
    die('Forbidden');
}

/**
 * Get autoloader
 */
$autoloaderPath = __DIR__.'/vendor/autoload.php';

if (! is_readable($autoloaderPath)) {
    throw new \RuntimeException(sprintf('Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
        $autoloaderPath));
}

include $autoloaderPath;

add_action('plugins_loaded', function () {
    // Set the hookable classes, ServiceProvider and PostTypes.
    $config = (new ConfigInterface('Package test plugin', __FILE__))->setHookables([
        TestingHookable::class,
        TestingAfterInitHookable::class
    ]);

    // The main plugin class extends PluginApiRegistryInterface, which implements FilterManagerInterface.
    $plugin = buildPlugin($config, PackageTestPlugin::class);
    // Injected dependencies will be hooked after calling the run() method.
    $plugin->run();
});

可钩接的类

插件围绕实现 HookableInterface 或其扩展接口的类展开。可钩接的类应只包含公开方法,这些方法被钩接到 WordPress 的过滤器或操作。

我建议使用 LazyHookableInterface,实现此接口的类仅在第一次调用包含其方法之一的钩子时才会被构造。

以下是一个实现 LazyHookableInterface 的类的示例

TestingHookable.php

<?php

namespace Jascha030\PackageTest\Hookable;

use Jascha030\PluginLib\Service\Hookable\LazyHookableInterface;

class TestingHookable implements LazyHookableInterface
{
    public static array $actions = [
        // 'hook' => ['method', priority, numberOfArguments]
        // If the prio and number are default you hook a method with call as `'hook' => 'method',`
        'init' => ['initMethod', 10, 1]
    ];

    public static array $filters = [];

    public static function getActions(): array
    {
        return static::$actions;
    }

    public static function getFilters(): array
    {
        return static::$filters;
    }

    final public function initMethod(): void
    {
        echo 'What\'s up, twitter world?';
        die();
    }
}

提供者

标准容器是来自 pimple/pimple 包的 Psr11 包装器。提供者遵循 pimple 的 ServiceProviderInterface

测试

包含一套单元测试,使用 phpunit/phpunit。为了便于使用,定义了一个 composer 脚本命令来运行测试。

composer run phpunit

代码覆盖率报告以 cov.xml 的形式生成在项目的根目录中。默认情况下,cov.xml 文件不会被 .gitignore 忽略。当部署新功能时,鼓励提交最新的代码覆盖率报告。

代码风格 & 格式化

包含了一个为 friendsofphp/php-cs-fixer 定义的代码风格配置,定义在 .php-cs-fixer.dist.php 中。

为了在没有全局安装 php-cs-fixer 的情况下使用 php-cs-fixer,还包含了一个 composer 脚本命令来使用提供的配置文件和 php-cs-fixer 的供应商二进制文件格式化 PHP 代码。

composer run format

许可证

此 composer 包是一个开源软件,受 MIT 许可证 许可。