pinkcrab/wp-hook-subscriber

为钩子创建单个订阅者,是 PinkCrab Perique 框架的一部分


README

logo

Perique 钩子订阅者

为钩子创建单个订阅者,是 PinkCrab 插件框架的一部分

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require GitHub contributors GitHub issues

WordPress 5.9 Test Suite [PHP7.2-8.1] WordPress 6.0 Test Suite [PHP7.2-8.1] WordPress 6.1 Test Suite [PHP7.2-8.2] WP6.2 [PHP7.2-8.2] Tests

codecov Scrutinizer Code Quality Maintainability

要求

需要 PinkCrab Perique 框架 V2.0.*

安装

$ composer require pinkcrab/wp-hook-subscriber

此模块允许创建单个钩子订阅,以便与 WordPress 建立接口。底层仍然使用 PinkCrab 框架相同的注册过程,但为单个调用提供了干净的抽象。

每个扩展提供的基类的类,其钩子都将添加到加载器中,无论是在定义的操作还是延迟操作。允许完全使用 DI 容器。

由于加载器注册钩子调用的方式,类是在初始化钩子时实例化的。这可能会对 WooCommerce 和其他可扩展插件造成问题,因为一些全局变量稍后才会填充。Hook_Subscriber 允许延迟构造,因此您的回调将在全局作用域中创建。

非延迟订阅者

class On_Single_Hook extends Abstract_Hook_Subscription {

   /**
    * The hook to register the subscriber
    * @var string
    */
   protected ?string $hook = 'some_hook';

   /**
       * Some service
       * @param My_Service
       */
      protected $my_service;

   public function __construct( My_Service $my_service ) {
      $this->my_service = $my_service;
   }

   /**
    * Callback
    * @param mixed ...$args
    */
   public function execute( ...$args ): void {
      // Args are accessed in the order they are passed.
      // do_action('foo', 'first','second','third',.....);
      //$args[0] = first, $args[1] = second, $args[2] = third, .....

      if ( $args[0] === 'something' ) {
         $this->my_service->do_something( $args[1] );
      }
   }
}

// Would be called by
do_action('some_hook', 'something', ['some','data','to do','something']);

延迟订阅者

class Deferred_Hook extends Abstract_Hook_Subscription {

   /**
    * The hook to register the subscriber
    * @var string
    */
   protected ?string $hook = 'some_hook';

   /**
    * Deferred hook to call
    *
    * @var string|null
    */
   protected ?string $deferred_hook = 'some_global_populated';

   /**
    * Our global data
    * @param Some_Global|null
    */
   protected $some_global;

   public function __construct() {
      global $some_global;
      $this->some_global = $some_global;
   }

   /**
    * Callback
    * @param mixed ...$args
    */
   public function execute( ...$args ): void {
      // Depends on a global which is set later than init.
      if ( $args[0] === 'something' && ! empty( $this->some_global ) ) {
         do_something( $this->some_global->some_property, $args[1] );
      }
   }
}

在另一个插件或 wp-core 中,$some_global 被填充,然后我们可以在创建和实际调用我们的钩子时随时挂钩。

function acme_plugin_function(){
    global $some_global; // Currently empty/null
    $some_global = new Some_Global();

    do_action('some_global_populated', ['some', 'data']);
}  

当 some_global_populated 触发时,会创建一个 Deferred_Hook 的新实例并注册回调。这使我们无论何时都能访问 Some_Global。我们最终创建了两个延迟钩子的实例,一次在初始化时注册第一次调用,然后在实际的延迟钩子中再次创建,用于实际的钩子调用。

以前的版本

  • 对于 Perique V1.0.* 使用版本 1.0.*
  • 对于 Perique V0.4.* 使用版本 0.2.2
  • 对于 Perique V0.3.* 使用版本 0.2.1

变更日志

  • 2.0.1 - 更新开发依赖项
  • 2.0.0 - 停止支持 PHP 7.2 & 7.3 并添加对 Perique V2.0.* 的支持
  • 1.0.1 - 停止支持 PHP 7.1,添加 PHP8 支持,更新所有依赖项并添加第三方质量检查(Scrutinizer & CodeClimate)
  • 1.0.0 - 现在支持 Perique 及其从 Registerable 到 Hookable 接口命名的迁移。
  • ---- 核心从 PinkCrab 插件框架重命名为 Perique ----
  • 0.2.2 更新测试和代码以反映框架 0.4.* 的变化
  • 0.2.1 添加了额外的测试和覆盖率报告。
  • 0.2.0 - 从初始 Event_Hook 命名迁移,并对延迟钩子的添加方式进行了少量更改,使用 DI 重新创建新实例,而不是重置状态。