jascha030 / wp-oop
WP OOP On Roids(工作标题)
Requires
- pimple/pimple: ^3.3
- psr/container: ^1.0
- symfony/uid: ^5.1
Requires (Dev)
- friendsofphp/php-cs-fixer: 3.0.x-dev
- squizlabs/php_codesniffer: 4.0.x-dev
This package is auto-updated.
Last update: 2021-09-06 18:09:52 UTC
README
用于与Wordpress插件通用API(钩子)交互的后台框架。跟踪动作和过滤器,并使挂钩或取消挂钩类方法变得容易。
此包包含一个简单的服务容器(Pimple)包装器,它以懒加载方式挂钩类方法。当WP过滤器/动作首次调用类时,提供类构造。
入门
要求
- Wordpress
- composer
- php >= 7.4
请注意,因为我构建了这个包来提供一种更现代的方式来开发Wordpress插件。因此,这个包不遵循Wordpress编码标准,并且(与Wordpress不同)它不支持PHP 5.3或任何比7.4更旧的PHP版本。
至少支持PHP 7.1的向下兼容性已在待办事项列表中。
安装
composer require jascha030/wp-subscriptions
使用
大多数逻辑将包括需要通过一个或多个 WpHookContainer
实例挂钩的类的方法。
容器实例
以下代码是一个示例,说明了如何使用此包的主插件文件并初始化一个主要的 WpHookContainer
实例。
<?php /** * Plugin Name: WP OOP Test Plugin * Version: 1.0.0 * Description: A test for the wp-oop package */ namespace Jascha030\Test; use Jascha030\WP\OOPOR\Container\Hook\WpHookContainer; use Jascha030\WP\OOPOR\Exception\InvalidClassLiteralArgumentException; /** * Require composer PSR-4 autoloader */ include __DIR__ . '/vendor/autoload.php'; /** * Set plugin path */ if (! defined('WPS_TEST_PLUGIN_DIR')) { define('WPS_TEST_PLUGIN_DIR', plugin_dir_path(__FILE__)); } /** * Adds a global with pre defined Hook service classes. */ if (! defined('WP_OOP_HOOK_SERVICES')) { define('WP_OOP_HOOK_SERVICES', [ WpTest::class ]); } /** * This method calls the main hook container. * This is wrapped in a method to prevent the unnecessary use of the Singleton pattern. * This function allows for other containers to be added if necessary. * * @return WpHookContainer */ function getContainer() { static $container; if (null === $container) { $container = new WpHookContainer(); } return $container; } /** * Register all hooked method for each class. */ function testContainer() { foreach (WP_OOP_HOOK_SERVICES as $hookableClass) { try { // Hook class to the container which will construct the class upon first call by a wordpress hook. (getContainer())->registerHookService($hookableClass); } catch (InvalidClassLiteralArgumentException $e) { var_dump($e->getMessage()); } } } testContainer();
可挂钩服务类
为了确保正确的类被挂钩,将 HookServiceInterface
添加到类的实现中。此实现不需要任何方法,它只用于挂钩容器来断言类的有效性。
挂钩的方法应该始终是公共的,就像常规Wordpress一样。
这些类使用一个静态属性,告诉挂钩容器将特定的方法挂钩到Wordpress。
动作
public static array $actions = []; // ActionProvider interface
过滤器
public static array $filters = []; // FilterProvider interface
以下是一个此类的一个示例。
<?php namespace Jascha030\Test; use Jascha030\WP\OOPOR\Service\Hook\HookServiceInterface; class WpTest implements HookServiceInterface { public static array $actions = [ 'index_test_area' => 'testArea', // most basic implementation: Hook => method 'test_custom_subs' => [ // Example of multiple methods hooked to one action hook ['testMethod', 1, 2], // 1, 2 refer to priority and expected arguments ['secondMethod', 10, 2] ] ]; public static array $filters = []; private string $pluginDir; private string $testOutput; public function __construct() { if (WPS_TEST_PLUGIN_DIR) { $this->pluginDir = WPS_TEST_PLUGIN_DIR; } echo '<h1>' . self::class . '</h1>'; } public function testArea(): void { ob_start(); for ($i = 0; $i < 5; $i++) { do_action('test_custom_subs', 'test1', 'test2'); } $this->testOutput = ob_get_clean(); echo "<pre>{$this->testOutput}</pre>"; } public function testMethod(string $test, string $test2): void { echo "<p><b>Dump:</b> {$test}, {$test2} <br></p>"; } public function secondMethod(string $test, string $test2): void { $test = strrev($test); $test2 = strrev($test2); echo "<p><b>Reverse:</b> {$test}, {$test2} <br> <small>Times called: {$this->called}</small></p>"; } }
信息 & 灵感
这个想法提供了灵活性,因此您不必在OOP WordPress插件中过度使用单例模式。现在您不必为每个需要的其他实例扩展类(例如:当您构建一个帖子类型类时,您可以创建一个包含可以循环遍历的帖子类型的配置,而不是必须为每个帖子类型创建单独的类)。
这个包是我之前的一个实验的延续
https://github.com/jascha030/wp-subscriptions
在完成的过程中,我终于意识到我之前的想法过于复杂。
原始想法基于这篇文章中的想法。
从Laravel中开发API中获得了更多的灵感。