Changeset 6276

Show
Ignore:
Timestamp:
10/19/07 17:45:08 (1 year ago)
Author:
ryan
Message:

phpdoc for plugin API. Props darkdragon. fixes #5225

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/wp-includes/plugin.php

    r6200 r6276  
    11<?php 
    2  
    3 /** 
    4  * Hooks a function to a specific filter action. 
     2/** 
     3 * The plugin API is located in this file, which allows for creating actions 
     4 * and filters and hooking functions, and methods. The functions or methods will 
     5 * then be run when the action or filter is called. 
     6 * 
     7 * The API callback examples reference functions, but can be methods of classes. 
     8 * To hook methods, you'll need to pass an array one of two ways. 
     9 * 
     10 * For static methods (you won't have access to the <tt>$this</tt> variable in the 
     11 * method): 
     12 * <code>array('class_name', 'method_name');</code> 
     13 * 
     14 * The second method will need the reference to the object to have access to the 
     15 * method. 
     16 * <code>array(&$this, 'method_name');</code> 
     17 * <code> 
     18 * $obj = new myObject(); 
     19 * array(&$obj, 'method_name'); 
     20 * </code> 
     21 * Any of the syntaxes explained in the PHP documentation for the 
     22 * {@link http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback' type} are valid. 
     23 * 
     24 * Also see the {@link http://codex.wordpress.org/Plugin_API Plugin API} for more information 
     25 * and examples on how to use a lot of these functions. 
     26 * 
     27 * @package WordPress 
     28 * @subpackage Plugin 
     29 * @since 1.5 
     30 */ 
     31 
     32/** 
     33 * Hooks a function or method to a specific filter action. 
    534 * 
    635 * Filters are the hooks that WordPress launches to modify text of various types 
     
    837 * can specify that one or more of its PHP functions is executed to 
    938 * modify specific types of text at these times, using the Filter API. 
    10  * See the [Plugin API] for a list of filter hooks. 
     39 * 
     40 * To use the API, the following code should be used to bind a callback to the filter 
     41 * <code> 
     42 * function example_hook($example) { echo $example; } 
     43 * 
     44 * add_filter('example_filter', 'example_hook'); 
     45 * </code> 
     46 * 
     47 * In WordPress 1.5.1+, hooked functions can take extra arguments that are set when 
     48 * the matching do_action() or apply_filters() call is run. The <tt>$accepted_args 
     49 * allow for calling functions only when the number of args match. Hooked functions 
     50 * can take extra arguments that are set when the matching <tt>do_action()</tt> or 
     51 * <tt>apply_filters()</tt> call is run. For example, the action <tt>comment_id_not_found</tt> 
     52 * will pass any functions that hook onto it the ID of the requested comment. 
     53 * 
     54 * <strong>Note:</strong> the function will return true no matter if the function was hooked 
     55 * fails or not. There are no checks for whether the function exists beforehand and no checks 
     56 * to whether the <tt>$function_to_add is even a string. It is up to you to take care and 
     57 * this is done for optimization purposes, so everything is as quick as possible. 
     58 * 
     59 * @package WordPress 
     60 * @subpackage Plugin 
     61 * @since 1.5 
     62 * @global array $wp_filter Stores all of the filters added in the form of 
     63 *  wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]'] 
     64 * @global array $merged_filters Tracks the tags that need to be merged for later. If the hook is added, it doesn't need to run through that process. 
    1165 * 
    1266 * @param string $tag The name of the filter to hook the <tt>$function_to_add</tt> to. 
    1367 * @param callback $function_to_add The name of the function to be called when the filter is applied. 
    1468 * @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. 
    15  * @param int $accepted_args optional. The number of arguments the function accept (default 1). In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. 
    16  * @return boolean true if the <tt>$function_to_add</tt> is added succesfully to filter <tt>$tag</tt>. How many arguments your function takes. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching <tt>do_action()</tt> or <tt>apply_filters()</tt> call is run. For example, the action <tt>comment_id_not_found</tt> will pass any functions that hook onto it the ID of the requested comment. 
     69 * @param int $accepted_args optional. The number of arguments the function accept (default 1). 
     70 * @return boolean true 
    1771 */ 
    1872function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 
    1973    global $wp_filter, $merged_filters; 
    2074 
    21     // So the format is wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]'] 
    2275    $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority); 
    23     $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 
    24     //$wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 
     76    $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args); 
    2577    unset( $merged_filters[ $tag ] ); 
    2678    return true; 
     
    3486 * by simply calling this function with the name of the new hook specified using 
    3587 * the <tt>$tag</a> parameter. 
    36  * @uses merge_filters Merges the filter hooks using this function. 
     88 * 
     89 * The function allows for additional arguments to be added and passed to hooks. 
     90 * <code> 
     91 * function example_hook($string, $arg1, $arg2) 
     92 * { 
     93 *      //Do stuff 
     94 * } 
     95 * $value = apply_filters('example_filter', 'filter me', 'arg1', 'arg2'); 
     96 * </code> 
     97 * 
     98 * @package WordPress 
     99 * @subpackage Plugin 
     100 * @since 1.5 
     101 * @global array $wp_filter Stores all of the filters 
     102 * @global array $merge_filters Merges the filter hooks using this function. 
     103 * 
    37104 * @param string $tag The name of the filter hook. 
    38  * @param string $string The text on which the filters hooked to <tt>$tag</tt> are applied on. 
     105 * @param string $value The value on which the filters hooked to <tt>$tag</tt> are applied on. 
    39106 * @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>. 
    40107 * @return string The text in <tt>$string</tt> after all hooked functions are applied to it. 
    41108 */ 
    42 function apply_filters($tag, $string) { 
     109function apply_filters($tag, $value) { 
    43110    global $wp_filter, $merged_filters; 
    44111 
     
    47114 
    48115    if ( !isset($wp_filter[$tag]) ) 
    49         return $string
     116        return $value
    50117 
    51118    reset( $wp_filter[ $tag ] ); 
     
    56123        foreach( (array) current($wp_filter[$tag]) as $the_ ) 
    57124            if ( !is_null($the_['function']) ){ 
    58                 $args[1] = $string
    59                 $string = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); 
     125                $args[1] = $value
     126                $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args'])); 
    60127            } 
    61128 
    62129    } while ( next($wp_filter[$tag]) !== false ); 
    63130 
    64     return $string
     131    return $value
    65132} 
    66133 
     
    71138 * <em>all</e>. These functions are called for every filter tag. This function 
    72139 * merges the functions attached to the <em>all</em> hook with the functions 
    73  * of a specific hoook defined by <tt>$tag</tt>. 
     140 * of a specific hook defined by <tt>$tag</tt>. 
     141 * 
     142 * Bugged if you hook into 'all' tag, then you <strong>will</strong> lose all priority 
     143 * information. {@link http://trac.wordpress.org/ticket/4715 Bug #4715} for more information. 
     144 * 
     145 * @package WordPress 
     146 * @subpackage Plugin 
     147 * @since 1.5 
     148 * @global array $wp_filter Stores all of the filters 
     149 * @global array $merge_filters Merges the filter hooks using this function. 
     150 * 
    74151 * @param string $tag The filter hook of which the functions should be merged. 
    75152 */ 
     
    93170 * method can be used to remove default functions attached to a specific filter 
    94171 * hook and possibly replace them with a substitute. 
     172 * 
     173 * To remove a hook, the <tt>$function_to_remove</tt> and <tt>$priority</tt> arguments 
     174 * must match when the hook was added. This goes for both filters and actions. No warning 
     175 * will be given on removal failure. 
     176 * 
     177 * @package WordPress 
     178 * @subpackage Plugin 
     179 * @since 1.5 
     180 * 
    95181 * @param string $tag The filter hook to which the function to be removed is hooked. 
    96182 * @param callback $function_to_remove The name of the function which should be removed. 
    97183 * @param int $priority optional. The priority of the function (default: 10). 
    98184 * @param int $accepted_args optional. The number of arguments the function accpets (default: 1). 
    99  * @return boolean Whether the function is removed. 
     185 * @return boolean Whether the function existed before it was removed. 
    100186 */ 
    101187function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 
     
    118204 * Action API. 
    119205 * 
     206 * @uses add_filter() Adds an action. Parameter list and functionality are the same. 
     207 * 
     208 * @package WordPress 
     209 * @subpackage Plugin 
     210 * @since 1.5 
     211 * 
    120212 * @param string $tag The name of the action to which the <tt>$function_to-add</tt> is hooked. 
    121  * @param callback $function_to_add The name of the function you wish to be called. Note: any of the syntaxes explained in the PHP documentation for the 'callback' type (http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback) are valid. 
     213 * @param callback $function_to_add The name of the function you wish to be called. 
    122214 * @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. 
    123  * @param int $accepted_args optional. The number of arguments the function accept (default 1). In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. 
    124  * @return boolean Always true. 
     215 * @param int $accepted_args optional. The number of arguments the function accept (default 1). 
    125216 */ 
    126217function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 
     
    134225 * It is possible to create new action hooks by simply calling this function, 
    135226 * specifying the name of the new hook using the <tt>$tag</tt> parameter. 
    136  * @uses merge_filters 
     227 * 
     228 * You can pass extra arguments to the hooks, much like you can with apply_filters(). 
     229 * 
     230 * @see apply_filters() This function works similar with the exception that nothing is 
     231 * returned and only the functions or methods are called. 
     232 * 
     233 * @package WordPress 
     234 * @subpackage Plugin 
     235 * @since 1.5 
     236 * @global array $wp_filter Stores all of the filters 
     237 * @global array $wp_actions Increments the amount of times action was triggered. 
     238 * 
    137239 * @param string $tag The name of the action to be executed. 
    138240 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action. 
     241 * @return null Will return null if $tag does not exist in $wp_filter array 
    139242 */ 
    140243function do_action($tag, $arg = '') { 
     
    170273/** 
    171274 * Return the number times an action is fired. 
     275 * 
     276 * @package WordPress 
     277 * @subpackage Plugin 
     278 * @since 2.1 
     279 * @global array $wp_actions Increments the amount of times action was triggered. 
     280 * 
    172281 * @param string $tag The name of the action hook. 
    173282 * @return int The number of times action hook <tt>$tag</tt> is fired 
     
    183292 
    184293/** 
    185  * Execute functions hooked on a specific action hook, specifying arguments in a array. 
    186  * 
    187  * This function is identical to {@link do_action}, but the argumetns passe to 
     294 * Execute functions hooked on a specific action hook, specifying arguments in an array. 
     295 * 
     296 * @see do_action() This function is identical, but the arguments passed to 
    188297 * the functions hooked to <tt>$tag</tt> are supplied using an array. 
     298 * 
     299 * @uses merge_filters() 
     300 * 
     301 * @package WordPress 
     302 * @subpackage Plugin 
     303 * @since 2.1 
     304 * @global array $wp_filter Stores all of the filters 
     305 * @global array $wp_actions Increments the amount of times action was triggered. 
     306 * 
    189307 * @param string $tag The name of the action to be executed. 
    190308 * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt> 
     309 * @return null Will return null if $tag does not exist in $wp_filter array 
    191310 */ 
    192311function do_action_ref_array($tag, $args) { 
     
    218337 * method can be used to remove default functions attached to a specific filter 
    219338 * hook and possibly replace them with a substitute. 
     339 * 
     340 * @uses remove_filter() Uses remove_filter to remove actions added. 
     341 * 
     342 * @package WordPress 
     343 * @subpackage Plugin 
     344 * @since 1.5 
     345 * 
    220346 * @param string $tag The action hook to which the function to be removed is hooked. 
    221347 * @param callback $function_to_remove The name of the function which should be removed. 
     
    236362 * 
    237363 * This method extract the name of a plugin from its filename. 
     364 * 
     365 * @package WordPress 
     366 * @subpackage Plugin 
     367 * @since 1.5 
     368 * 
     369 * @access private 
     370 * 
    238371 * @param string $file The filename of plugin. 
    239372 * @return string The name of a plugin. 
     
    253386 * the plugin, including the optional subdirectory. For example, when the plugin 
    254387 * is located in <tt>wp-content/plugin/sampleplugin/sample.php</tt>, then the 
    255  * name of this hook will become 'activate_sampleplugin/sample.php'. 
     388 * name of this hook will become 'activate_sampleplugin/sample.php' 
    256389 * When the plugin consists of only one file and is (as by default) located at 
    257390 * <tt>wp-content/plugin/sample.php</tt> the name of this hook will be 
    258391 * 'activate_sample.php'. 
     392 * 
     393 * @package WordPress 
     394 * @subpackage Plugin 
     395 * @since 1.5 
     396 * 
     397 * @access private 
     398 * 
    259399 * @param string $file The filename of the plugin including the path. 
    260400 * @param string $function the function hooked to the 'activate_PLUGIN' action. 
     
    276416 * <tt>wp-content/plugin/sample.php</tt> the name of this hook will be 
    277417 * 'activate_sample.php'. 
     418 * 
     419 * @package WordPress 
     420 * @subpackage Plugin 
     421 * @since 2.0 
     422 * 
     423 * @access private 
     424 * 
    278425 * @param string $file The filename of the plugin including the path. 
    279426 * @param string $function the function hooked to the 'activate_PLUGIN' action. 
     
    285432 
    286433/** 
     434 * Build Unique ID for storage and retrieval 
     435 * 
     436 * The old way to serialize the callback caused issues and this function is the 
     437 * solution. It works by checking for objects and creating an a new property in 
     438 * the class to keep track of the object and new objects of the same class that 
     439 * need to be added. 
     440 * 
     441 * It also allows for the removal of actions and filters for objects after they 
     442 * change class properties. It is possible to include the property $wp_filter_id 
     443 * in your class and set it to "null" or a number to bypass the workaround. However 
     444 * this will prevent you from adding new classes and any new classes will overwrite 
     445 * the previous hook by the same class. 
     446 * 
     447 * Functions and static method callbacks are just returned as strings and shouldn't 
     448 * have any speed penalty. 
     449 * 
     450 * @package WordPress 
     451 * @subpackage Plugin 
     452 * @since 2.2.3 
     453 * 
     454 * @link http://trac.wordpress.org/ticket/3875 
     455 * 
    287456 * @access private 
    288457 * 
    289  * _wp_filter_build_unique_id() - Build Unique ID for storage and retrieval 
    290  * 
    291  * This function is used to fix the issue where serialized, when used with  
    292  * classes that updated their properties, wouldn't be able to remove actions. 
    293  * 
    294  * How it works is that it first checks if the $function parameter is a string 
    295  * and passes it through, untouched. Functions won't need to be changed, since 
    296  * there can only be one declared. 
    297  *  
    298  * The second type that will be passed through untouched, is for static methods 
    299  * in classes. They don't need to be changed since they are much like functions 
    300  * in that you can only call one of them. 
    301  * 
    302  * The main purpose of this function is for classes, which can have more than 
    303  * one declared and you want to add more than one hook for each one that is  
    304  * declared, or want to change properties internal of the class that you declared 
    305  * the hook. 
    306  * 
    307  * @global $wp_filter 
     458 * @global array $wp_filter Storage for all of the filters and actions 
    308459 * @param string $tag Used in counting how many hooks were applied 
    309460 * @param string|array $function Used for creating unique id 
     
    316467 
    317468    // If function then just skip all of the tests and not overwrite the following. 
    318     // Static Calling 
    319469    if( is_string($function) ) 
    320470        return $function; 
     
    323473    { 
    324474        $obj_idx = get_class($function[0]).$function[1]; 
    325         if( is_null($function[0]->wp_filter_id) ) { 
     475        if( is_null($function[0]->wp_filter_id) ) { // This should be instead of is_null() change to !isset() to fix notice 
    326476            $count = count((array)$wp_filter[$tag][$priority]); 
    327477            $function[0]->wp_filter_id = $count; 
     
    332482        return $obj_idx; 
    333483    } 
     484    // Static Calling 
    334485    else if( is_string($function[0]) ) 
    335486        return $function[0].$function[1];