piotrpress / wordpress-hooks
此库使用PHP属性(PHP版本8.0中引入)来自动添加/删除WordPress钩子(过滤器和方法)到/从函数和方法。
v5.3.0
2024-09-16 12:06 UTC
Requires
- php: >=7.4
- piotrpress/cacher: ^1.2
README
此库使用PHP属性(PHP版本8.0
中引入)来自动添加/删除WordPress钩子(过滤器和动作)到/从函数和方法。
注意: 此库支持PHP >= 7.4
版本。
安装
$ composer require piotrpress/wordpress-hooks
加载
require __DIR__ . '/vendor/autoload.php';
用法
属性
#[ Action( string $name, int $priority = 10 ) ] #[ Filter( string $name, int $priority = 10 ) ]
函数
Hooks::add( object $object = null, string $callback = '', PiotrPress\CacherInterface $cache = null ) : void Hooks::remove( object $object = null, string $callback = '', PiotrPress\CacherInterface $cache = null ) : void
示例
Hooks::add/remove( $object )
如果传递了object
参数并且省略了callback
,则添加或删除对象的所有钩子。
use PiotrPress\WordPress\Hooks; use PiotrPress\WordPress\Hooks\Action; use PiotrPress\WordPress\Hooks\Filter; class Example { public function add_hooks() { Hooks::add( $this ); } #[ Action( 'init' ) ] public function example_init() : void { // do something } #[ Filter( 'the_title', 1 ) ] public function example_the_title( string $post_title, int $post_id ) : string { // do something } } $example = new Example(); $example->add_hooks(); Hooks::remove( $example );
这相当于
$example = new Example(); add_action( 'init', [ $example, 'example_init' ] ); add_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 ); remove_action( 'init', [ $example, 'example_init' ] ); remove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );
注意: 可以在方法中或对象外部调用Hooks::add/remove()
方法。
Hooks::add/remove( $object, $callback )
如果传递了object
和callback
参数,则仅添加或删除此方法的钩子。
use PiotrPress\WordPress\Hooks; use PiotrPress\WordPress\Hooks\Action; use PiotrPress\WordPress\Hooks\Filter; class Example { public function add_hooks() { Hooks::add( $this, 'example_init' ); Hooks::add( $this, 'example_the_title' ); } #[ Action( 'init' ) ] public function example_init() : void { // do something } #[ Filter( 'the_title', 1 ) ] public function example_the_title( string $post_title, int $post_id ) : string { // do something } } $example = new Example(); $example->add_hooks(); Hooks::remove( $example, 'example_init' ); Hooks::remove( $example, 'example_the_title' );
这相当于
$example = new Example(); add_action( 'init', [ $example, 'example_init' ] ); add_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 ); remove_action( 'init', [ $example, 'example_init' ] ); remove_filter( 'the_title', [ $example, 'example_the_title' ], 1, 2 );
Hooks::add/remove( callback: $callback )
如果省略了object
参数并传递了callback
,则仅添加或删除此函数的钩子。
use PiotrPress\WordPress\Hooks; use PiotrPress\WordPress\Hooks\Action; use PiotrPress\WordPress\Hooks\Filter; #[ Action( 'init' ) ] public function example_init() : void { // do something } #[ Filter( 'the_title', 1 ) ] public function example_the_title( string $post_title, int $post_id ) : string { // do something } Hooks::add( callback: 'example_init' ); Hooks::add( callback: 'example_the_title' ); Hooks::remove( callback: 'example_init' ); Hooks::remove( callback: 'example_the_title' );
这相当于
add_action( 'init', 'example_init' ); add_filter( 'the_title', 'example_the_title', 1, 2 ); remove_action( 'init', 'example_init' ); remove_filter( 'the_title', 'example_the_title', 1, 2 );
缓存
可选地,您可以传递一个缓存对象,该对象必须实现PiotrPress\CacherInterface接口,作为第三个cache
参数传递给Hooks::add/remove()
方法。
这将缓存Hooks::get()
方法的结果,该方法使用反射API提供给定对象、方法或函数的钩子列表,因此缓存其结果可以显着提高性能。
示例
use PiotrPress\Cacher; use PiotrPress\WordPress\Hooks; use PiotrPress\WordPress\Hooks\Action; use PiotrPress\WordPress\Hooks\Filter; class Example { #[ Action( 'init' ) ] public function example_init() : void { // do something } #[ Filter( 'the_title', 1 ) ] public function example_the_title( string $post_title, int $post_id ) : string { // do something } } $example = new Example(); $cache = new Cacher( '.hooks' ); Hooks::add( object: $example, cache: $cache ); Hooks::remove( object: $example, cache: $cache );
注意: 您可以使用由此库提供的PiotrPress\Cacher库提供的简单文件缓存。
感谢
以下人员的灵感、反馈、想法和功能请求:
故障排除
注意: 命名参数在PHP < 8.0
版本中不起作用。
要求
PHP >= 7.4
版本。