Example #1
0
 public function test_hooks()
 {
     add_action('LP_TEST_HOOK', function ($data) {
         return strtoupper($data);
     });
     $this->assertTrue(has_hook('LP_TEST_HOOK'));
     $this->assertEqual(do_action('LP_TEST_HOOK', 'abc'), 'ABC');
     remove_hook('LP_TEST_HOOK');
     $this->assertFalse(has_hook('LP_TEST_HOOK'));
 }
Example #2
0
/**
 * Adds a procedure to a hook for later execution.
 *
 * @param hook_name Name of the hook.
 * @param hook_function	Name of the php function called upon hook trigger.
 * @param priority Numeric priority. Lowest means procedure will be executed before.
 * @param rollback_function	Name of the php rollback function called upon failure.
 */
function add_hook($hook_name, $hook_function, $priority = 0, $rollback_function = null)
{
    if (defined('DEBUG_ENABLED') && DEBUG_ENABLED && (($fargs = func_get_args()) || ($fargs = 'NOARGS'))) {
        debug_log('Entered (%%)', 257, 0, __FILE__, __LINE__, __METHOD__, $fargs);
    }
    # First, see if the hook function exists.
    if (!function_exists($hook_function)) {
        system_message(array('title' => _('Hook function does not exist'), 'body' => sprintf('Hook name: %s<br/>Hook function: %s', $hook_name, $hook_function), 'type' => 'warn'));
        return;
    }
    if (!array_key_exists($hook_name, $_SESSION[APPCONFIG]->hooks)) {
        $_SESSION[APPCONFIG]->hooks[$hook_name] = array();
    }
    remove_hook($hook_name, $hook_function, -1, null);
    array_push($_SESSION[APPCONFIG]->hooks[$hook_name], array('priority' => $priority, 'hook_function' => $hook_function, 'rollback_function' => $rollback_function));
    uasort($_SESSION[APPCONFIG]->hooks[$hook_name], 'sort_array_by_priority');
}
/**
 * remove a security filter
 * @since 3.4
 * @param string $filter_name id of action
 * @param string $hook_function function to remove
 */
function remove_secfilter($filter_name, $hook_function)
{
    global $secFilters;
    return remove_hook($secFilters, $filter_name, $hook_function);
}