arnaud-ritti / wp-hook

使用PHP Docblock(注释)来注册WordPress钩子、过滤器和短代码。

0.0.1 2022-06-21 09:23 UTC

README

PHPUnit Latest Unstable Version Total Downloads License

注册WordPress钩子、过滤器和短代码。

  • 使用PHP Docblock(注释)
  • 或使用PHP 8.0属性

要求

  • PHP 7.1或更高版本(已在PHP 7.4、8.0和8.1上测试)

安装

通过Composer

composer require arnaud-ritti/wp-hook

使用方法

要自动连接您的类,只需调用HookRegistry::bootstrap方法,如下所示

<?php

namespace My\CoolNamespace;

use Ari\WpHook\HookRegistry;
use Ari\WpHook\Models\Action;

class MyClass
{
    public function __construct(HookRegistry $hookRegistry) 
    {
        $hookRegistry->bootstrap($this);
    }
    
    /**
     * @Action(tag="init")    
     */
    #[Action(tag: "init")]
    public function doSomething()
    {
        // do something
    }
}

完成!

以下注释可以在PHP 7中使用

/**
 * @Action(tag="the hook name", priority=1, accepted_args=1)
 * @Filter(tag="the filter name", priority=1, accepted_args=1)
 * @Shortcode(tag="the shortcode code")
 */

对于PHP 8,请使用属性

#[Action(tag: "the hook name", priority: 1, accepted_args: 1)]
#[Filter(tag: "the filter name", priority: 1, accepted_args: 1)]
#[Shortcode(tag: "the shortcode code", priority: 1, accepted_args: 1)]

测试

composer test

许可证

WP Hook在MIT许可证下发布。