PHP Classes

File: src/Functions.php

Recommend this page to a friend!
  Classes of Miraz Mac  >  Whooks  >  src/Functions.php  >  Download  
File: src/Functions.php
Role: Auxiliary script
Content type: text/plain
Description: Auxiliary script
Class: Whooks
Implement hooks and filters like in WordPress
Author: By
Last change:
Date: 3 years ago
Size: 8,668 bytes
 

Contents

Class file image Download
<?php
/**
 * Polyfill for Wordpress Hooking Functions
 *
 * @package MirazMac\Whooks
 * @since 0.1 Initial Release
 * @author Miraz Mac <mirazmac@gmail.com>
 * @link https://mirazmac.info Author Homepage
 * @license LICENSE The MIT License
 */
use MirazMac\Whooks\Whooks;

/**
* Hooks a function or method to a specific filter action.
*
* @param string $tag The name of the filter to hook the $function_to_add to.
* @param callback $function_to_add The name of the function to be called when the filter is applied.
* @param int $priority optional. Used to specify the order in which the functions associated with a particular
* action are executed (default: 10). Lower numbers correspond with earlier execution,
* and functions with the same priority are executed in the order in which they were
* added to the action.
* @param int $accepted_args optional. The number of arguments the function accept (default 1).
* @return boolean true
*/
function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1)
{
    return
Whooks::addFilter($tag, $function_to_add, $priority, $accepted_args);
}

/**
* Removes a function from a specified filter hook.
*
* @param string $tag The filter hook to which the function to be removed is hooked.
* @param callback $function_to_remove The name of the function which should be removed.
* @param int $priority optional. The priority of the function (default: 10).
* @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
* @return boolean Whether the function existed before it was removed.
*/
function remove_filter($tag, $function_to_remove, $priority = 10)
{
    return
Whooks::removeFilter($tag, $function_to_remove, $priority);
}

/**
* Remove all of the hooks from a filter.
*
* @param string $tag The filter to remove hooks from.
* @param int $priority The priority number to remove.
* @return bool true when finished.
*/
function remove_all_filters($tag, $priority = false)
{
    return
Whooks::removeAllFilters($tag, $priority);
}

/**
* Check if any filter has been registered for a hook.
*
* @param string $tag The name of the filter hook.
* @param callback $function_to_check optional.
* @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything
* registered. When checking a specific function, the priority of that hook is returned,
* or false if the function is not attached. When using the $function_to_check argument, this
* function may return a non-boolean value that evaluates to false (e.g.) 0, so use
* the === operator for testing the return value.
*/
function has_filter($tag, $function_to_check = false)
{
    return
Whooks::hasFilter($tag, $function_to_check);
}

/**
* Call the functions added to a filter hook.
*
* @param string $tag The name of the filter hook.
* @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
* @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
* @return mixed The filtered value after all hooked functions are applied to it.
*/
function apply_filters($tag, $value)
{
    return
Whooks::applyFilters($tag, $value);
}

/**
* Execute functions hooked on a specific filter hook, specifying arguments in an array.
*
* @param string $tag The name of the filter hook.
* @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
* @return mixed The filtered value after all hooked functions are applied to it.
*/
function apply_filters_ref_array($tag, $args)
{
    return
Whooks::applyFiltersRefArray($tag, $args);
}

/**
* Hooks a function on to a specific action.
*
* @param string $tag The name of the action to which the $function_to_add is hooked.
* @param callback $function_to_add The name of the function you wish to be called.
* @param int $priority optional. Used to specify the order in which the functions associated with a
* particular action are executed (default: 10). Lower numbers correspond with
* earlier execution, and functions with the same priority are executed in the
* order in which they were added to the action.
* @param int $accepted_args optional. The number of arguments the function accept (default 1).
* @return mixed
*/
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1)
{
    return
Whooks::addAction($tag, $function_to_add, $priority, $accepted_args);
}


/**
* Check if any action has been registered for a hook.
*
* @param string $tag The name of the action hook.
* @param callback $function_to_check optional.
* @return mixed If $function_to_check is omitted, returns boolean for whether the hook has anything
* registered. When checking a specific function, the priority of that hook is returned,
* or false if the function is not attached. When using the $function_to_check argument,
* this function may return a non-boolean value that evaluates to false. (e.g.) 0, so use
* the === operator for testing the return value.
*/
function has_action($tag, $function_to_check = false)
{
    return
Whooks::hasAction($tag, $function_to_check);
}

/**
* Removes a function from a specified action hook.
*
* @param string $tag The action hook to which the function to be removed is hooked.
* @param callback $function_to_remove The name of the function which should be removed.
* @param int $priority optional The priority of the function (default: 10).
* @return boolean Whether the function is removed.
*/
function remove_action($tag, $function_to_remove, $priority = 10)
{
    return
Whooks::removeAction($tag, $function_to_remove, $priority);
}

/**
* Remove all of the hooks from an action.
*
* @param string $tag The action to remove hooks from.
* @param int $priority The priority number to remove them from.
* @return bool True when finished.
*/
function remove_all_actions($tag, $priority = false)
{
    return
Whooks::removeAllActions($tag, $priority);
}

/**
* Execute functions hooked on a specific action hook.
*
* @param string $tag The name of the action to be executed.
* @param mixed $arg,... Optional additional arguments which are passed on to the
* functions hooked to the action.
* @return null Will return null if $tag does not exist in $filter array
*/
function do_action($tag, $arg = '')
{
    return
Whooks::doAction($tag, $arg);
}

/**
* Execute functions hooked on a specific action hook, specifying arguments in an array.
*
* @param string $tag The name of the action to be executed.
* @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
* @return null Will return null if $tag does not exist in $filter array
*/
function do_action_ref_array($tag, $args)
{
    return
Whooks::doActionRefArray($tag, $args);
}

/**
* Retrieve the number of times an action is fired.
*
* @param string $tag The name of the action hook.
* @return int The number of times action hook <tt>$tag</tt> is fired
*/
function did_action($tag)
{
    return
Whooks::didAction($tag);
}

/**
* Retrieve the name of the current filter or action.
*
* @return string Hook name of the current filter or action.
*/
function current_filter()
{
    return
Whooks::currentFilter();
}

/**
* Retrieve the name of the current action.
*
* @return string Hook name of the current action.
*/
function current_action()
{
    return
Whooks::currentAction();
}

/**
* Retrieve the name of a filter currently being processed.
*
* current_filter() only returns the most recent filter or action
* being executed. did_action() returns true once the action is initially
* processed. This function allows detection for any filter currently being
* executed (despite not being the most recent filter to fire, in the case of
* hooks called from hook callbacks) to be verified.
*
* @param null|string $filter Optional. Filter to check. Defaults to null, which
* checks if any filter is currently being run.
* @return bool Whether the filter is currently in the stack
*/
function doing_filter($filter = null)
{
    return
Whooks::doingFilter($filter);
}

/**
* Retrieve the name of an action currently being processed.
*
* @param string|null $action Optional. Action to check. Defaults to null, which checks
* if any action is currently being run.
* @return bool Whether the action is currently in the stack.
*/
function doing_action($action = null)
{
     return
Whooks::doingAction($action);
}

For more information send a message to info at phpclasses dot org.