[44] | 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 | * Any of the syntaxes explained in the PHP documentation for the
|
---|
| 11 | * {@link http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback 'callback'}
|
---|
| 12 | * type are valid.
|
---|
| 13 | *
|
---|
| 14 | * Also see the {@link http://codex.wordpress.org/Plugin_API Plugin API} for
|
---|
| 15 | * more information and examples on how to use a lot of these functions.
|
---|
| 16 | *
|
---|
| 17 | * @package WordPress
|
---|
| 18 | * @subpackage Plugin
|
---|
| 19 | * @since 1.5
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Hooks a function or method to a specific filter action.
|
---|
| 24 | *
|
---|
| 25 | * Filters are the hooks that WordPress launches to modify text of various types
|
---|
| 26 | * before adding it to the database or sending it to the browser screen. Plugins
|
---|
| 27 | * can specify that one or more of its PHP functions is executed to
|
---|
| 28 | * modify specific types of text at these times, using the Filter API.
|
---|
| 29 | *
|
---|
| 30 | * To use the API, the following code should be used to bind a callback to the
|
---|
| 31 | * filter.
|
---|
| 32 | *
|
---|
| 33 | * <code>
|
---|
| 34 | * function example_hook($example) { echo $example; }
|
---|
| 35 | * add_filter('example_filter', 'example_hook');
|
---|
| 36 | * </code>
|
---|
| 37 | *
|
---|
| 38 | * In WordPress 1.5.1+, hooked functions can take extra arguments that are set
|
---|
| 39 | * when the matching do_action() or apply_filters() call is run. The
|
---|
| 40 | * $accepted_args allow for calling functions only when the number of args
|
---|
| 41 | * match. Hooked functions can take extra arguments that are set when the
|
---|
| 42 | * matching do_action() or apply_filters() call is run. For example, the action
|
---|
| 43 | * comment_id_not_found will pass any functions that hook onto it the ID of the
|
---|
| 44 | * requested comment.
|
---|
| 45 | *
|
---|
| 46 | * <strong>Note:</strong> the function will return true no matter if the
|
---|
| 47 | * function was hooked fails or not. There are no checks for whether the
|
---|
| 48 | * function exists beforehand and no checks to whether the <tt>$function_to_add
|
---|
| 49 | * is even a string. It is up to you to take care and this is done for
|
---|
| 50 | * optimization purposes, so everything is as quick as possible.
|
---|
| 51 | *
|
---|
| 52 | * @package WordPress
|
---|
| 53 | * @subpackage Plugin
|
---|
| 54 | * @since 0.71
|
---|
| 55 | * @global array $wp_filter Stores all of the filters added in the form of
|
---|
| 56 | * wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)']']
|
---|
| 57 | * @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.
|
---|
| 58 | *
|
---|
| 59 | * @param string $tag The name of the filter to hook the $function_to_add to.
|
---|
| 60 | * @param callback $function_to_add The name of the function to be called when the filter is applied.
|
---|
| 61 | * @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.
|
---|
| 62 | * @param int $accepted_args optional. The number of arguments the function accept (default 1).
|
---|
| 63 | * @return boolean true
|
---|
| 64 | */
|
---|
| 65 | function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
|
---|
| 66 | global $wp_filter, $merged_filters;
|
---|
| 67 |
|
---|
| 68 | $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
|
---|
| 69 | $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
|
---|
| 70 | unset( $merged_filters[ $tag ] );
|
---|
| 71 | return true;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /**
|
---|
| 75 | * Check if any filter has been registered for a hook.
|
---|
| 76 | *
|
---|
| 77 | * @package WordPress
|
---|
| 78 | * @subpackage Plugin
|
---|
| 79 | * @since 2.5
|
---|
| 80 | * @global array $wp_filter Stores all of the filters
|
---|
| 81 | *
|
---|
| 82 | * @param string $tag The name of the filter hook.
|
---|
| 83 | * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
|
---|
| 84 | * @return int|boolean Optionally returns the priority on that hook for the specified function.
|
---|
| 85 | */
|
---|
| 86 | function has_filter($tag, $function_to_check = false) {
|
---|
| 87 | global $wp_filter;
|
---|
| 88 |
|
---|
| 89 | $has = !empty($wp_filter[$tag]);
|
---|
| 90 | if ( false === $function_to_check || false == $has )
|
---|
| 91 | return $has;
|
---|
| 92 |
|
---|
| 93 | if ( !$idx = _wp_filter_build_unique_id($tag, $function_to_check, false) )
|
---|
| 94 | return false;
|
---|
| 95 |
|
---|
| 96 | foreach ( (array) array_keys($wp_filter[$tag]) as $priority ) {
|
---|
| 97 | if ( isset($wp_filter[$tag][$priority][$idx]) )
|
---|
| 98 | return $priority;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | return false;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * Call the functions added to a filter hook.
|
---|
| 106 | *
|
---|
| 107 | * The callback functions attached to filter hook $tag are invoked by calling
|
---|
| 108 | * this function. This function can be used to create a new filter hook by
|
---|
| 109 | * simply calling this function with the name of the new hook specified using
|
---|
| 110 | * the $tag parameter.
|
---|
| 111 | *
|
---|
| 112 | * The function allows for additional arguments to be added and passed to hooks.
|
---|
| 113 | * <code>
|
---|
| 114 | * function example_hook($string, $arg1, $arg2)
|
---|
| 115 | * {
|
---|
| 116 | * //Do stuff
|
---|
| 117 | * return $string;
|
---|
| 118 | * }
|
---|
| 119 | * $value = apply_filters('example_filter', 'filter me', 'arg1', 'arg2');
|
---|
| 120 | * </code>
|
---|
| 121 | *
|
---|
| 122 | * @package WordPress
|
---|
| 123 | * @subpackage Plugin
|
---|
| 124 | * @since 0.71
|
---|
| 125 | * @global array $wp_filter Stores all of the filters
|
---|
| 126 | * @global array $merged_filters Merges the filter hooks using this function.
|
---|
| 127 | * @global array $wp_current_filter stores the list of current filters with the current one last
|
---|
| 128 | *
|
---|
| 129 | * @param string $tag The name of the filter hook.
|
---|
| 130 | * @param mixed $value The value on which the filters hooked to <tt>$tag</tt> are applied on.
|
---|
| 131 | * @param mixed $var,... Additional variables passed to the functions hooked to <tt>$tag</tt>.
|
---|
| 132 | * @return mixed The filtered value after all hooked functions are applied to it.
|
---|
| 133 | */
|
---|
| 134 | function apply_filters($tag, $value) {
|
---|
| 135 | global $wp_filter, $merged_filters, $wp_current_filter;
|
---|
| 136 |
|
---|
| 137 | $args = array();
|
---|
| 138 | $wp_current_filter[] = $tag;
|
---|
| 139 |
|
---|
| 140 | // Do 'all' actions first
|
---|
| 141 | if ( isset($wp_filter['all']) ) {
|
---|
| 142 | $args = func_get_args();
|
---|
| 143 | _wp_call_all_hook($args);
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | if ( !isset($wp_filter[$tag]) ) {
|
---|
| 147 | array_pop($wp_current_filter);
|
---|
| 148 | return $value;
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | // Sort
|
---|
| 152 | if ( !isset( $merged_filters[ $tag ] ) ) {
|
---|
| 153 | ksort($wp_filter[$tag]);
|
---|
| 154 | $merged_filters[ $tag ] = true;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | reset( $wp_filter[ $tag ] );
|
---|
| 158 |
|
---|
| 159 | if ( empty($args) )
|
---|
| 160 | $args = func_get_args();
|
---|
| 161 |
|
---|
| 162 | do {
|
---|
| 163 | foreach( (array) current($wp_filter[$tag]) as $the_ )
|
---|
| 164 | if ( !is_null($the_['function']) ){
|
---|
| 165 | $args[1] = $value;
|
---|
| 166 | $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | } while ( next($wp_filter[$tag]) !== false );
|
---|
| 170 |
|
---|
| 171 | array_pop( $wp_current_filter );
|
---|
| 172 |
|
---|
| 173 | return $value;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | /**
|
---|
| 177 | * Removes a function from a specified filter hook.
|
---|
| 178 | *
|
---|
| 179 | * This function removes a function attached to a specified filter hook. This
|
---|
| 180 | * method can be used to remove default functions attached to a specific filter
|
---|
| 181 | * hook and possibly replace them with a substitute.
|
---|
| 182 | *
|
---|
| 183 | * To remove a hook, the $function_to_remove and $priority arguments must match
|
---|
| 184 | * when the hook was added. This goes for both filters and actions. No warning
|
---|
| 185 | * will be given on removal failure.
|
---|
| 186 | *
|
---|
| 187 | * @package WordPress
|
---|
| 188 | * @subpackage Plugin
|
---|
| 189 | * @since 1.2
|
---|
| 190 | *
|
---|
| 191 | * @param string $tag The filter hook to which the function to be removed is hooked.
|
---|
| 192 | * @param callback $function_to_remove The name of the function which should be removed.
|
---|
| 193 | * @param int $priority optional. The priority of the function (default: 10).
|
---|
| 194 | * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
|
---|
| 195 | * @return boolean Whether the function existed before it was removed.
|
---|
| 196 | */
|
---|
| 197 | function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
|
---|
| 198 | $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
|
---|
| 199 |
|
---|
| 200 | $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
|
---|
| 201 |
|
---|
| 202 | if ( true === $r) {
|
---|
| 203 | unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
|
---|
| 204 | if ( empty($GLOBALS['wp_filter'][$tag][$priority]) )
|
---|
| 205 | unset($GLOBALS['wp_filter'][$tag][$priority]);
|
---|
| 206 | unset($GLOBALS['merged_filters'][$tag]);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | return $r;
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | /**
|
---|
| 213 | * Remove all of the hooks from a filter.
|
---|
| 214 | *
|
---|
| 215 | * @since 2.7
|
---|
| 216 | *
|
---|
| 217 | * @param string $tag The filter to remove hooks from.
|
---|
| 218 | * @param int $priority The priority number to remove.
|
---|
| 219 | * @return bool True when finished.
|
---|
| 220 | */
|
---|
| 221 | function remove_all_filters($tag, $priority = false) {
|
---|
| 222 | global $wp_filter, $merged_filters;
|
---|
| 223 |
|
---|
| 224 | if( isset($wp_filter[$tag]) ) {
|
---|
| 225 | if( false !== $priority && isset($$wp_filter[$tag][$priority]) )
|
---|
| 226 | unset($wp_filter[$tag][$priority]);
|
---|
| 227 | else
|
---|
| 228 | unset($wp_filter[$tag]);
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | if( isset($merged_filters[$tag]) )
|
---|
| 232 | unset($merged_filters[$tag]);
|
---|
| 233 |
|
---|
| 234 | return true;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
| 237 | /**
|
---|
| 238 | * Retrieve the name of the current filter or action.
|
---|
| 239 | *
|
---|
| 240 | * @package WordPress
|
---|
| 241 | * @subpackage Plugin
|
---|
| 242 | * @since 2.5
|
---|
| 243 | *
|
---|
| 244 | * @return string Hook name of the current filter or action.
|
---|
| 245 | */
|
---|
| 246 | function current_filter() {
|
---|
| 247 | global $wp_current_filter;
|
---|
| 248 | return end( $wp_current_filter );
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 | /**
|
---|
| 253 | * Hooks a function on to a specific action.
|
---|
| 254 | *
|
---|
| 255 | * Actions are the hooks that the WordPress core launches at specific points
|
---|
| 256 | * during execution, or when specific events occur. Plugins can specify that
|
---|
| 257 | * one or more of its PHP functions are executed at these points, using the
|
---|
| 258 | * Action API.
|
---|
| 259 | *
|
---|
| 260 | * @uses add_filter() Adds an action. Parameter list and functionality are the same.
|
---|
| 261 | *
|
---|
| 262 | * @package WordPress
|
---|
| 263 | * @subpackage Plugin
|
---|
| 264 | * @since 1.2
|
---|
| 265 | *
|
---|
| 266 | * @param string $tag The name of the action to which the $function_to_add is hooked.
|
---|
| 267 | * @param callback $function_to_add The name of the function you wish to be called.
|
---|
| 268 | * @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.
|
---|
| 269 | * @param int $accepted_args optional. The number of arguments the function accept (default 1).
|
---|
| 270 | */
|
---|
| 271 | function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
|
---|
| 272 | return add_filter($tag, $function_to_add, $priority, $accepted_args);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 |
|
---|
| 276 | /**
|
---|
| 277 | * Execute functions hooked on a specific action hook.
|
---|
| 278 | *
|
---|
| 279 | * This function invokes all functions attached to action hook $tag. It is
|
---|
| 280 | * possible to create new action hooks by simply calling this function,
|
---|
| 281 | * specifying the name of the new hook using the <tt>$tag</tt> parameter.
|
---|
| 282 | *
|
---|
| 283 | * You can pass extra arguments to the hooks, much like you can with
|
---|
| 284 | * apply_filters().
|
---|
| 285 | *
|
---|
| 286 | * @see apply_filters() This function works similar with the exception that
|
---|
| 287 | * nothing is returned and only the functions or methods are called.
|
---|
| 288 | *
|
---|
| 289 | * @package WordPress
|
---|
| 290 | * @subpackage Plugin
|
---|
| 291 | * @since 1.2
|
---|
| 292 | * @global array $wp_filter Stores all of the filters
|
---|
| 293 | * @global array $wp_actions Increments the amount of times action was triggered.
|
---|
| 294 | *
|
---|
| 295 | * @param string $tag The name of the action to be executed.
|
---|
| 296 | * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
|
---|
| 297 | * @return null Will return null if $tag does not exist in $wp_filter array
|
---|
| 298 | */
|
---|
| 299 | function do_action($tag, $arg = '') {
|
---|
| 300 | global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
|
---|
| 301 |
|
---|
| 302 | if ( is_array($wp_actions) )
|
---|
| 303 | $wp_actions[] = $tag;
|
---|
| 304 | else
|
---|
| 305 | $wp_actions = array($tag);
|
---|
| 306 |
|
---|
| 307 | $wp_current_filter[] = $tag;
|
---|
| 308 |
|
---|
| 309 | // Do 'all' actions first
|
---|
| 310 | if ( isset($wp_filter['all']) ) {
|
---|
| 311 | $all_args = func_get_args();
|
---|
| 312 | _wp_call_all_hook($all_args);
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | if ( !isset($wp_filter[$tag]) ) {
|
---|
| 316 | array_pop($wp_current_filter);
|
---|
| 317 | return;
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | $args = array();
|
---|
| 321 | if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
|
---|
| 322 | $args[] =& $arg[0];
|
---|
| 323 | else
|
---|
| 324 | $args[] = $arg;
|
---|
| 325 | for ( $a = 2; $a < func_num_args(); $a++ )
|
---|
| 326 | $args[] = func_get_arg($a);
|
---|
| 327 |
|
---|
| 328 | // Sort
|
---|
| 329 | if ( !isset( $merged_filters[ $tag ] ) ) {
|
---|
| 330 | ksort($wp_filter[$tag]);
|
---|
| 331 | $merged_filters[ $tag ] = true;
|
---|
| 332 | }
|
---|
| 333 |
|
---|
| 334 | reset( $wp_filter[ $tag ] );
|
---|
| 335 |
|
---|
| 336 | do {
|
---|
| 337 | foreach ( (array) current($wp_filter[$tag]) as $the_ )
|
---|
| 338 | if ( !is_null($the_['function']) )
|
---|
| 339 | call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
|
---|
| 340 |
|
---|
| 341 | } while ( next($wp_filter[$tag]) !== false );
|
---|
| 342 |
|
---|
| 343 | array_pop($wp_current_filter);
|
---|
| 344 | }
|
---|
| 345 |
|
---|
| 346 | /**
|
---|
| 347 | * Retrieve the number times an action is fired.
|
---|
| 348 | *
|
---|
| 349 | * @package WordPress
|
---|
| 350 | * @subpackage Plugin
|
---|
| 351 | * @since 2.1
|
---|
| 352 | * @global array $wp_actions Increments the amount of times action was triggered.
|
---|
| 353 | *
|
---|
| 354 | * @param string $tag The name of the action hook.
|
---|
| 355 | * @return int The number of times action hook <tt>$tag</tt> is fired
|
---|
| 356 | */
|
---|
| 357 | function did_action($tag) {
|
---|
| 358 | global $wp_actions;
|
---|
| 359 |
|
---|
| 360 | if ( empty($wp_actions) )
|
---|
| 361 | return 0;
|
---|
| 362 |
|
---|
| 363 | return count(array_keys($wp_actions, $tag));
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | /**
|
---|
| 367 | * Execute functions hooked on a specific action hook, specifying arguments in an array.
|
---|
| 368 | *
|
---|
| 369 | * @see do_action() This function is identical, but the arguments passed to the
|
---|
| 370 | * functions hooked to <tt>$tag</tt> are supplied using an array.
|
---|
| 371 | *
|
---|
| 372 | * @package WordPress
|
---|
| 373 | * @subpackage Plugin
|
---|
| 374 | * @since 2.1
|
---|
| 375 | * @global array $wp_filter Stores all of the filters
|
---|
| 376 | * @global array $wp_actions Increments the amount of times action was triggered.
|
---|
| 377 | *
|
---|
| 378 | * @param string $tag The name of the action to be executed.
|
---|
| 379 | * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
|
---|
| 380 | * @return null Will return null if $tag does not exist in $wp_filter array
|
---|
| 381 | */
|
---|
| 382 | function do_action_ref_array($tag, $args) {
|
---|
| 383 | global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
|
---|
| 384 |
|
---|
| 385 | if ( !is_array($wp_actions) )
|
---|
| 386 | $wp_actions = array($tag);
|
---|
| 387 | else
|
---|
| 388 | $wp_actions[] = $tag;
|
---|
| 389 |
|
---|
| 390 | $wp_current_filter[] = $tag;
|
---|
| 391 |
|
---|
| 392 | // Do 'all' actions first
|
---|
| 393 | if ( isset($wp_filter['all']) ) {
|
---|
| 394 | $all_args = func_get_args();
|
---|
| 395 | _wp_call_all_hook($all_args);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | if ( !isset($wp_filter[$tag]) ) {
|
---|
| 399 | array_pop($wp_current_filter);
|
---|
| 400 | return;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
| 403 | // Sort
|
---|
| 404 | if ( !isset( $merged_filters[ $tag ] ) ) {
|
---|
| 405 | ksort($wp_filter[$tag]);
|
---|
| 406 | $merged_filters[ $tag ] = true;
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | reset( $wp_filter[ $tag ] );
|
---|
| 410 |
|
---|
| 411 | do {
|
---|
| 412 | foreach( (array) current($wp_filter[$tag]) as $the_ )
|
---|
| 413 | if ( !is_null($the_['function']) )
|
---|
| 414 | call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
|
---|
| 415 |
|
---|
| 416 | } while ( next($wp_filter[$tag]) !== false );
|
---|
| 417 |
|
---|
| 418 | array_pop($wp_current_filter);
|
---|
| 419 | }
|
---|
| 420 |
|
---|
| 421 | /**
|
---|
| 422 | * Check if any action has been registered for a hook.
|
---|
| 423 | *
|
---|
| 424 | * @package WordPress
|
---|
| 425 | * @subpackage Plugin
|
---|
| 426 | * @since 2.5
|
---|
| 427 | * @see has_filter() has_action() is an alias of has_filter().
|
---|
| 428 | *
|
---|
| 429 | * @param string $tag The name of the action hook.
|
---|
| 430 | * @param callback $function_to_check optional. If specified, return the priority of that function on this hook or false if not attached.
|
---|
| 431 | * @return int|boolean Optionally returns the priority on that hook for the specified function.
|
---|
| 432 | */
|
---|
| 433 | function has_action($tag, $function_to_check = false) {
|
---|
| 434 | return has_filter($tag, $function_to_check);
|
---|
| 435 | }
|
---|
| 436 |
|
---|
| 437 | /**
|
---|
| 438 | * Removes a function from a specified action hook.
|
---|
| 439 | *
|
---|
| 440 | * This function removes a function attached to a specified action hook. This
|
---|
| 441 | * method can be used to remove default functions attached to a specific filter
|
---|
| 442 | * hook and possibly replace them with a substitute.
|
---|
| 443 | *
|
---|
| 444 | * @package WordPress
|
---|
| 445 | * @subpackage Plugin
|
---|
| 446 | * @since 1.2
|
---|
| 447 | *
|
---|
| 448 | * @param string $tag The action hook to which the function to be removed is hooked.
|
---|
| 449 | * @param callback $function_to_remove The name of the function which should be removed.
|
---|
| 450 | * @param int $priority optional The priority of the function (default: 10).
|
---|
| 451 | * @param int $accepted_args optional. The number of arguments the function accpets (default: 1).
|
---|
| 452 | * @return boolean Whether the function is removed.
|
---|
| 453 | */
|
---|
| 454 | function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
|
---|
| 455 | return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
|
---|
| 456 | }
|
---|
| 457 |
|
---|
| 458 | /**
|
---|
| 459 | * Remove all of the hooks from an action.
|
---|
| 460 | *
|
---|
| 461 | * @since 2.7
|
---|
| 462 | *
|
---|
| 463 | * @param string $tag The action to remove hooks from.
|
---|
| 464 | * @param int $priority The priority number to remove them from.
|
---|
| 465 | * @return bool True when finished.
|
---|
| 466 | */
|
---|
| 467 | function remove_all_actions($tag, $priority = false) {
|
---|
| 468 | return remove_all_filters($tag, $priority);
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | //
|
---|
| 472 | // Functions for handling plugins.
|
---|
| 473 | //
|
---|
| 474 |
|
---|
| 475 | /**
|
---|
| 476 | * Gets the basename of a plugin.
|
---|
| 477 | *
|
---|
| 478 | * This method extracts the name of a plugin from its filename.
|
---|
| 479 | *
|
---|
| 480 | * @package WordPress
|
---|
| 481 | * @subpackage Plugin
|
---|
| 482 | * @since 1.5
|
---|
| 483 | *
|
---|
| 484 | * @access private
|
---|
| 485 | *
|
---|
| 486 | * @param string $file The filename of plugin.
|
---|
| 487 | * @return string The name of a plugin.
|
---|
| 488 | * @uses WP_PLUGIN_DIR
|
---|
| 489 | */
|
---|
| 490 | function plugin_basename($file) {
|
---|
| 491 | $file = str_replace('\\','/',$file); // sanitize for Win32 installs
|
---|
| 492 | $file = preg_replace('|/+|','/', $file); // remove any duplicate slash
|
---|
| 493 | $plugin_dir = str_replace('\\','/',WP_PLUGIN_DIR); // sanitize for Win32 installs
|
---|
| 494 | $plugin_dir = preg_replace('|/+|','/', $plugin_dir); // remove any duplicate slash
|
---|
| 495 | $mu_plugin_dir = str_replace('\\','/',WPMU_PLUGIN_DIR); // sanitize for Win32 installs
|
---|
| 496 | $mu_plugin_dir = preg_replace('|/+|','/', $mu_plugin_dir); // remove any duplicate slash
|
---|
| 497 | $file = preg_replace('#^' . preg_quote($plugin_dir, '#') . '/|^' . preg_quote($mu_plugin_dir, '#') . '/#','',$file); // get relative path from plugins dir
|
---|
| 498 | $file = trim($file, '/');
|
---|
| 499 | return $file;
|
---|
| 500 | }
|
---|
| 501 |
|
---|
| 502 | /**
|
---|
| 503 | * Gets the filesystem directory path (with trailing slash) for the plugin __FILE__ passed in
|
---|
| 504 | * @package WordPress
|
---|
| 505 | * @subpackage Plugin
|
---|
| 506 | * @since 2.8
|
---|
| 507 | *
|
---|
| 508 | * @param string $file The filename of the plugin (__FILE__)
|
---|
| 509 | * @return string the filesystem path of the directory that contains the plugin
|
---|
| 510 | */
|
---|
| 511 | function plugin_dir_path( $file ) {
|
---|
| 512 | return trailingslashit( dirname( $file ) );
|
---|
| 513 | }
|
---|
| 514 |
|
---|
| 515 | /**
|
---|
| 516 | * Gets the URL directory path (with trailing slash) for the plugin __FILE__ passed in
|
---|
| 517 | * @package WordPress
|
---|
| 518 | * @subpackage Plugin
|
---|
| 519 | * @since 2.8
|
---|
| 520 | *
|
---|
| 521 | * @param string $file The filename of the plugin (__FILE__)
|
---|
| 522 | * @return string the URL path of the directory that contains the plugin
|
---|
| 523 | */
|
---|
| 524 | function plugin_dir_url( $file ) {
|
---|
| 525 | return trailingslashit( plugins_url( '', $file ) );
|
---|
| 526 | }
|
---|
| 527 |
|
---|
| 528 | /**
|
---|
| 529 | * Set the activation hook for a plugin.
|
---|
| 530 | *
|
---|
| 531 | * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
|
---|
| 532 | * activated. In the name of this hook, PLUGINNAME is replaced with the name of
|
---|
| 533 | * the plugin, including the optional subdirectory. For example, when the plugin
|
---|
| 534 | * is located in wp-content/plugin/sampleplugin/sample.php, then the name of
|
---|
| 535 | * this hook will become 'activate_sampleplugin/sample.php'. When the plugin
|
---|
| 536 | * consists of only one file and is (as by default) located at
|
---|
| 537 | * wp-content/plugin/sample.php the name of this hook will be
|
---|
| 538 | * 'activate_sample.php'.
|
---|
| 539 | *
|
---|
| 540 | * @package WordPress
|
---|
| 541 | * @subpackage Plugin
|
---|
| 542 | * @since 2.0
|
---|
| 543 | *
|
---|
| 544 | * @param string $file The filename of the plugin including the path.
|
---|
| 545 | * @param callback $function the function hooked to the 'activate_PLUGIN' action.
|
---|
| 546 | */
|
---|
| 547 | function register_activation_hook($file, $function) {
|
---|
| 548 | $file = plugin_basename($file);
|
---|
| 549 | add_action('activate_' . $file, $function);
|
---|
| 550 | }
|
---|
| 551 |
|
---|
| 552 | /**
|
---|
| 553 | * Set the deactivation hook for a plugin.
|
---|
| 554 | *
|
---|
| 555 | * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
|
---|
| 556 | * deactivated. In the name of this hook, PLUGINNAME is replaced with the name
|
---|
| 557 | * of the plugin, including the optional subdirectory. For example, when the
|
---|
| 558 | * plugin is located in wp-content/plugin/sampleplugin/sample.php, then
|
---|
| 559 | * the name of this hook will become 'activate_sampleplugin/sample.php'.
|
---|
| 560 | *
|
---|
| 561 | * When the plugin consists of only one file and is (as by default) located at
|
---|
| 562 | * wp-content/plugin/sample.php the name of this hook will be
|
---|
| 563 | * 'activate_sample.php'.
|
---|
| 564 | *
|
---|
| 565 | * @package WordPress
|
---|
| 566 | * @subpackage Plugin
|
---|
| 567 | * @since 2.0
|
---|
| 568 | *
|
---|
| 569 | * @param string $file The filename of the plugin including the path.
|
---|
| 570 | * @param callback $function the function hooked to the 'activate_PLUGIN' action.
|
---|
| 571 | */
|
---|
| 572 | function register_deactivation_hook($file, $function) {
|
---|
| 573 | $file = plugin_basename($file);
|
---|
| 574 | add_action('deactivate_' . $file, $function);
|
---|
| 575 | }
|
---|
| 576 |
|
---|
| 577 | /**
|
---|
| 578 | * Set the uninstallation hook for a plugin.
|
---|
| 579 | *
|
---|
| 580 | * Registers the uninstall hook that will be called when the user clicks on the
|
---|
| 581 | * uninstall link that calls for the plugin to uninstall itself. The link won't
|
---|
| 582 | * be active unless the plugin hooks into the action.
|
---|
| 583 | *
|
---|
| 584 | * The plugin should not run arbitrary code outside of functions, when
|
---|
| 585 | * registering the uninstall hook. In order to run using the hook, the plugin
|
---|
| 586 | * will have to be included, which means that any code laying outside of a
|
---|
| 587 | * function will be run during the uninstall process. The plugin should not
|
---|
| 588 | * hinder the uninstall process.
|
---|
| 589 | *
|
---|
| 590 | * If the plugin can not be written without running code within the plugin, then
|
---|
| 591 | * the plugin should create a file named 'uninstall.php' in the base plugin
|
---|
| 592 | * folder. This file will be called, if it exists, during the uninstall process
|
---|
| 593 | * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
|
---|
| 594 | * should always check for the 'WP_UNINSTALL_PLUGIN' constant, before
|
---|
| 595 | * executing.
|
---|
| 596 | *
|
---|
| 597 | * @since 2.7
|
---|
| 598 | *
|
---|
| 599 | * @param string $file
|
---|
| 600 | * @param callback $callback The callback to run when the hook is called.
|
---|
| 601 | */
|
---|
| 602 | function register_uninstall_hook($file, $callback) {
|
---|
| 603 | // The option should not be autoloaded, because it is not needed in most
|
---|
| 604 | // cases. Emphasis should be put on using the 'uninstall.php' way of
|
---|
| 605 | // uninstalling the plugin.
|
---|
| 606 | $uninstallable_plugins = (array) get_option('uninstall_plugins');
|
---|
| 607 | $uninstallable_plugins[plugin_basename($file)] = $callback;
|
---|
| 608 | update_option('uninstall_plugins', $uninstallable_plugins);
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | /**
|
---|
| 612 | * Calls the 'all' hook, which will process the functions hooked into it.
|
---|
| 613 | *
|
---|
| 614 | * The 'all' hook passes all of the arguments or parameters that were used for
|
---|
| 615 | * the hook, which this function was called for.
|
---|
| 616 | *
|
---|
| 617 | * This function is used internally for apply_filters(), do_action(), and
|
---|
| 618 | * do_action_ref_array() and is not meant to be used from outside those
|
---|
| 619 | * functions. This function does not check for the existence of the all hook, so
|
---|
| 620 | * it will fail unless the all hook exists prior to this function call.
|
---|
| 621 | *
|
---|
| 622 | * @package WordPress
|
---|
| 623 | * @subpackage Plugin
|
---|
| 624 | * @since 2.5
|
---|
| 625 | * @access private
|
---|
| 626 | *
|
---|
| 627 | * @uses $wp_filter Used to process all of the functions in the 'all' hook
|
---|
| 628 | *
|
---|
| 629 | * @param array $args The collected parameters from the hook that was called.
|
---|
| 630 | * @param string $hook Optional. The hook name that was used to call the 'all' hook.
|
---|
| 631 | */
|
---|
| 632 | function _wp_call_all_hook($args) {
|
---|
| 633 | global $wp_filter;
|
---|
| 634 |
|
---|
| 635 | reset( $wp_filter['all'] );
|
---|
| 636 | do {
|
---|
| 637 | foreach( (array) current($wp_filter['all']) as $the_ )
|
---|
| 638 | if ( !is_null($the_['function']) )
|
---|
| 639 | call_user_func_array($the_['function'], $args);
|
---|
| 640 |
|
---|
| 641 | } while ( next($wp_filter['all']) !== false );
|
---|
| 642 | }
|
---|
| 643 |
|
---|
| 644 | /**
|
---|
| 645 | * Build Unique ID for storage and retrieval.
|
---|
| 646 | *
|
---|
| 647 | * The old way to serialize the callback caused issues and this function is the
|
---|
| 648 | * solution. It works by checking for objects and creating an a new property in
|
---|
| 649 | * the class to keep track of the object and new objects of the same class that
|
---|
| 650 | * need to be added.
|
---|
| 651 | *
|
---|
| 652 | * It also allows for the removal of actions and filters for objects after they
|
---|
| 653 | * change class properties. It is possible to include the property $wp_filter_id
|
---|
| 654 | * in your class and set it to "null" or a number to bypass the workaround.
|
---|
| 655 | * However this will prevent you from adding new classes and any new classes
|
---|
| 656 | * will overwrite the previous hook by the same class.
|
---|
| 657 | *
|
---|
| 658 | * Functions and static method callbacks are just returned as strings and
|
---|
| 659 | * shouldn't have any speed penalty.
|
---|
| 660 | *
|
---|
| 661 | * @package WordPress
|
---|
| 662 | * @subpackage Plugin
|
---|
| 663 | * @access private
|
---|
| 664 | * @since 2.2.3
|
---|
| 665 | * @link http://trac.wordpress.org/ticket/3875
|
---|
| 666 | *
|
---|
| 667 | * @global array $wp_filter Storage for all of the filters and actions
|
---|
| 668 | * @param string $tag Used in counting how many hooks were applied
|
---|
| 669 | * @param callback $function Used for creating unique id
|
---|
| 670 | * @param int|bool $priority Used in counting how many hooks were applied. If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.
|
---|
| 671 | * @param string $type filter or action
|
---|
| 672 | * @return string|bool Unique ID for usage as array key or false if $priority === false and $function is an object reference, and it does not already have a uniqe id.
|
---|
| 673 | */
|
---|
| 674 | function _wp_filter_build_unique_id($tag, $function, $priority) {
|
---|
| 675 | global $wp_filter;
|
---|
| 676 | static $filter_id_count = 0;
|
---|
| 677 |
|
---|
| 678 | // If function then just skip all of the tests and not overwrite the following.
|
---|
| 679 | if ( is_string($function) )
|
---|
| 680 | return $function;
|
---|
| 681 | // Object Class Calling
|
---|
| 682 | else if (is_object($function[0]) ) {
|
---|
| 683 | $obj_idx = get_class($function[0]).$function[1];
|
---|
| 684 | if ( !isset($function[0]->wp_filter_id) ) {
|
---|
| 685 | if ( false === $priority )
|
---|
| 686 | return false;
|
---|
| 687 | $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : 0;
|
---|
| 688 | $function[0]->wp_filter_id = $filter_id_count++;
|
---|
| 689 | } else
|
---|
| 690 | $obj_idx .= $function[0]->wp_filter_id;
|
---|
| 691 | return $obj_idx;
|
---|
| 692 | }
|
---|
| 693 | // Static Calling
|
---|
| 694 | else if ( is_string($function[0]) )
|
---|
| 695 | return $function[0].$function[1];
|
---|
| 696 | }
|
---|
| 697 |
|
---|
| 698 | ?>
|
---|