Ticket #5225: plugin.phpdoc.2.diff
File plugin.phpdoc.2.diff, 18.2 KB (added by , 17 years ago) |
---|
-
plugin.php
1 1 <?php 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 */ 2 31 3 32 /** 4 * Hooks a function to a specific filter action.33 * Hooks a function or method to a specific filter action. 5 34 * 6 35 * Filters are the hooks that WordPress launches to modify text of various types 7 36 * before adding it to the database or sending it to the browser screen. Plugins 8 37 * can specify that one or more of its PHP functions is executed to 9 38 * modify specific types of text at these times, using the Filter API. 10 * See the [Plugin API] for a list of filter hooks.11 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. 65 * 12 66 * @param string $tag The name of the filter to hook the <tt>$function_to_add</tt> to. 13 67 * @param callback $function_to_add The name of the function to be called when the filter is applied. 14 68 * @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 17 71 */ 18 72 function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 19 73 global $wp_filter, $merged_filters; 20 74 21 // So the format is wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]']22 75 $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); 25 77 unset( $merged_filters[ $tag ] ); 26 78 return true; 27 79 } … … 33 85 * calling this function. This function can be used to create a new filter hook 34 86 * by simply calling this function with the name of the new hook specified using 35 87 * 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 * 37 104 * @param string $tag The name of the filter hook. 38 * @param string $ string The texton 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. 39 106 * @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>. 40 107 * @return string The text in <tt>$string</tt> after all hooked functions are applied to it. 41 108 */ 42 function apply_filters($tag, $ string) {109 function apply_filters($tag, $value) { 43 110 global $wp_filter, $merged_filters; 44 111 45 112 if ( !isset( $merged_filters[ $tag ] ) ) 46 113 merge_filters($tag); 47 114 48 115 if ( !isset($wp_filter[$tag]) ) 49 return $ string;116 return $value; 50 117 51 118 reset( $wp_filter[ $tag ] ); 52 119 … … 55 122 do{ 56 123 foreach( (array) current($wp_filter[$tag]) as $the_ ) 57 124 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'])); 60 127 } 61 128 62 129 } while ( next($wp_filter[$tag]) !== false ); 63 130 64 return $ string;131 return $value; 65 132 } 66 133 67 134 /** … … 70 137 * It is possible to defined generic filter functions using the filter hook 71 138 * <em>all</e>. These functions are called for every filter tag. This function 72 139 * 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 * 74 151 * @param string $tag The filter hook of which the functions should be merged. 75 152 */ 76 153 function merge_filters($tag) { … … 92 169 * This function removes a function attached to a specified filter hook. This 93 170 * method can be used to remove default functions attached to a specific filter 94 171 * 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 * 95 181 * @param string $tag The filter hook to which the function to be removed is hooked. 96 182 * @param callback $function_to_remove The name of the function which should be removed. 97 183 * @param int $priority optional. The priority of the function (default: 10). 98 184 * @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. 100 186 */ 101 187 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) { 102 188 $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority); … … 117 203 * one or more of its PHP functions are executed at these points, using the 118 204 * Action API. 119 205 * 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 * 120 212 * @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. 122 214 * @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). 125 216 */ 126 217 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) { 127 218 add_filter($tag, $function_to_add, $priority, $accepted_args); … … 133 224 * This function invokes all functions attached to action hook <tt>$tag</tt>. 134 225 * It is possible to create new action hooks by simply calling this function, 135 226 * 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 * 137 239 * @param string $tag The name of the action to be executed. 138 240 * @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 139 242 */ 140 243 function do_action($tag, $arg = '') { 141 244 global $wp_filter, $wp_actions; … … 169 272 170 273 /** 171 274 * 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 * 172 281 * @param string $tag The name of the action hook. 173 282 * @return int The number of times action hook <tt>$tag</tt> is fired 174 283 */ … … 182 291 } 183 292 184 293 /** 185 * Execute functions hooked on a specific action hook, specifying arguments in a array.294 * Execute functions hooked on a specific action hook, specifying arguments in an array. 186 295 * 187 * This function is identical to {@link do_action}, but the argumetns passeto296 * @see do_action() This function is identical, but the arguments passed to 188 297 * 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 * 189 307 * @param string $tag The name of the action to be executed. 190 308 * @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 191 310 */ 192 311 function do_action_ref_array($tag, $args) { 193 312 global $wp_filter, $wp_actions; … … 217 336 * This function removes a function attached to a specified action hook. This 218 337 * method can be used to remove default functions attached to a specific filter 219 338 * 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 * 220 346 * @param string $tag The action hook to which the function to be removed is hooked. 221 347 * @param callback $function_to_remove The name of the function which should be removed. 222 348 * @param int $priority optional The priority of the function (default: 10). … … 235 361 * Gets the basename of a plugin. 236 362 * 237 363 * 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 * 238 371 * @param string $file The filename of plugin. 239 372 * @return string The name of a plugin. 240 373 */ … … 252 385 * activated. In the name of this hook, PLUGINNAME is replaced with the name of 253 386 * the plugin, including the optional subdirectory. For example, when the plugin 254 387 * 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' 256 389 * When the plugin consists of only one file and is (as by default) located at 257 390 * <tt>wp-content/plugin/sample.php</tt> the name of this hook will be 258 391 * 'activate_sample.php'. 392 * 393 * @package WordPress 394 * @subpackage Plugin 395 * @since 1.5 396 * 397 * @access private 398 * 259 399 * @param string $file The filename of the plugin including the path. 260 400 * @param string $function the function hooked to the 'activate_PLUGIN' action. 261 401 */ … … 275 415 * When the plugin consists of only one file and is (as by default) located at 276 416 * <tt>wp-content/plugin/sample.php</tt> the name of this hook will be 277 417 * 'activate_sample.php'. 418 * 419 * @package WordPress 420 * @subpackage Plugin 421 * @since 2.0 422 * 423 * @access private 424 * 278 425 * @param string $file The filename of the plugin including the path. 279 426 * @param string $function the function hooked to the 'activate_PLUGIN' action. 280 427 */ … … 284 431 } 285 432 286 433 /** 287 * @access private434 * Build Unique ID for storage and retrieval 288 435 * 289 * _wp_filter_build_unique_id() - Build Unique ID for storage and retrieval 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. 290 440 * 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. 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. 293 446 * 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. 447 * Functions and static method callbacks are just returned as strings and shouldn't 448 * have any speed penalty. 301 449 * 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. 450 * @package WordPress 451 * @subpackage Plugin 452 * @since 2.2.3 306 453 * 307 * @global $wp_filter 454 * @link http://trac.wordpress.org/ticket/3875 455 * 456 * @access private 457 * 458 * @global array $wp_filter Storage for all of the filters and actions 308 459 * @param string $tag Used in counting how many hooks were applied 309 460 * @param string|array $function Used for creating unique id 310 461 * @param int $priority Used in counting how many hooks were applied … … 315 466 global $wp_filter; 316 467 317 468 // If function then just skip all of the tests and not overwrite the following. 318 // Static Calling319 469 if( is_string($function) ) 320 470 return $function; 321 471 // Object Class Calling 322 472 else if(is_object($function[0]) ) 323 473 { 324 474 $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 326 476 $count = count((array)$wp_filter[$tag][$priority]); 327 477 $function[0]->wp_filter_id = $count; 328 478 $obj_idx .= $count; … … 331 481 $obj_idx .= $function[0]->wp_filter_id; 332 482 return $obj_idx; 333 483 } 484 // Static Calling 334 485 else if( is_string($function[0]) ) 335 486 return $function[0].$function[1]; 336 487 } 337 488 338 ?> 489 ?> 490 No newline at end of file