symplely/hooks

一个简单的事件分发插件库,WordPress钩子API系统的移植分支。

1.0.0 2019-11-22 02:56 UTC

This package is auto-updated.

Last update: 2024-09-13 12:30:05 UTC


README

Build StatusBuild statuscodecovCodacy BadgeMaintainability

此库允许您通过注册由触发钩子、事件或监听器执行的回调函数,轻松地将基于事件架构添加到您的应用程序中,这些回调函数将在字符串标识符/标签上触发,我们称之为 $hook_point,如果需要,通常使用前缀如 "before" 或 "after" 来表示期望的操作。

如何使用?

简单,将类文件包含在您的应用程序启动文件(设置/加载/配置等)中,然后使用全局 Hooks 函数开始挂钩您的过滤器和动作钩子。例如:

add_action('header_action', 'echo_this_in_header');

function echo_this_in_header() {
   echo 'this came from a hooked function';
}

然后,您只需在应用程序的任何地方调用挂钩函数即可,例如:

echo '<div id="extra_header">';
  do_action('header_action');
echo '</div>';

输出结果将是

<div id="extra_header">this came from a hooked function</div>

安装

要安装此库,请确保已安装 composer,然后运行以下命令

composer require symplely/hooks

用法

此库受 node.js 中的 EventEmitter API 和 Événement 的启发。

因此,它提供了一个熟悉的简单事件发射器接口,该接口将委托给 Hooks API 类的 add_filterapply_filtersadd_actiondo_action 方法。

创建发射器

<?php
require 'vendor/autoload.php';

use Async\Hook\EventEmitter;

$emitter = new EventEmitter();

添加监听器

<?php
$emitter->on('user.created', function (User $user) use ($logger) {
    $logger->log(sprintf("User '%s' was created.", $user->getLogin()));
});

触发事件

<?php
$emitter->emit('user.created', $user);

方法

on()

委托给 Hooks 的 [add_action] 函数。

once()

委托给 Hooks 的 [add_action] 函数,然后 [remove_action] 函数。

off()

委托给 Hooks 的 [remove_action] 函数。

emit()

没有委托,仅执行/触发特定 $hook_point 上的挂钩函数。

dispatch()

委托给 Hooks 的 [do_action] 函数。

add()

委托给 Hooks 的 [add_filter] 函数。

clear()

委托给 Hooks 的 [remove_filter] 函数。

cancel()

委托给 Hooks 的 [remove_all_filters] 函数。

trigger()

委托给 Hooks 的 [apply_filters] 函数。

动作

/**
 * Hooks a function on to a specific action hook.
 */
add_action($hook_point, $function_to_add, $priority, $accepted_args);

/**
 * Execute functions hooked on a specific action hook.
 * Will return null if $hook_point does not exist
 */
do_action($hook_point, ...$arg);

/**
 * Removes a function from a specified action hook.
 * Will return true if the function is removed
 */
remove_action($hook_point, $function_to_remove, $priority);

/**
 * Check if any action has been registered for a hook.
 * Will return boolean if anything registered, or the priority.
 */
has_action($hook_point, $function_to_check);

/**
 * Retrieve the number of times an action is fired.
 */
did_action($hook_point);

过滤器

/**
 * Hooks a function or method to a specific filter hook.
 * Will return boolean true
 */
add_filter($hook_point, $function_to_add, $priority, $accepted_args);

/**
 * Removes a function from a specified filter hook.
 * Will return boolean Whether the function existed before it was removed
 */
remove_filter($hook_point, $function_to_remove, $priority, $accepted_args);

/**
 * Check if any filter has been registered for a hook.
 * Will return mixed
 */
has_filter($hook_point, $function_to_check);

/**
 * Call the functions added to a filter hook.
 * Will return the filtered value after all hooked functions are applied to it.
 */
apply_filters($hook_point, $value, ...$arg);

还有更多方法,但这些都是您将主要使用的方法。