web/wp-includes/plugin.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    78  * @subpackage Plugin
    78  * @subpackage Plugin
    79  * @since 2.5
    79  * @since 2.5
    80  * @global array $wp_filter Stores all of the filters
    80  * @global array $wp_filter Stores all of the filters
    81  *
    81  *
    82  * @param string $tag The name of the filter hook.
    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.
    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.
    84  * @return int|boolean Optionally returns the priority on that hook for the specified function.
    85  */
    85  */
    86 function has_filter($tag, $function_to_check = false) {
    86 function has_filter($tag, $function_to_check = false) {
    87 	global $wp_filter;
    87 	global $wp_filter;
    88 
    88 
   133  */
   133  */
   134 function apply_filters($tag, $value) {
   134 function apply_filters($tag, $value) {
   135 	global $wp_filter, $merged_filters, $wp_current_filter;
   135 	global $wp_filter, $merged_filters, $wp_current_filter;
   136 
   136 
   137 	$args = array();
   137 	$args = array();
   138 	$wp_current_filter[] = $tag;
       
   139 
   138 
   140 	// Do 'all' actions first
   139 	// Do 'all' actions first
   141 	if ( isset($wp_filter['all']) ) {
   140 	if ( isset($wp_filter['all']) ) {
       
   141 		$wp_current_filter[] = $tag;
   142 		$args = func_get_args();
   142 		$args = func_get_args();
   143 		_wp_call_all_hook($args);
   143 		_wp_call_all_hook($args);
   144 	}
   144 	}
   145 
   145 
   146 	if ( !isset($wp_filter[$tag]) ) {
   146 	if ( !isset($wp_filter[$tag]) ) {
   147 		array_pop($wp_current_filter);
   147 		if ( isset($wp_filter['all']) )
       
   148 			array_pop($wp_current_filter);
   148 		return $value;
   149 		return $value;
   149 	}
   150 	}
       
   151 
       
   152 	if ( !isset($wp_filter['all']) )
       
   153 		$wp_current_filter[] = $tag;
   150 
   154 
   151 	// Sort
   155 	// Sort
   152 	if ( !isset( $merged_filters[ $tag ] ) ) {
   156 	if ( !isset( $merged_filters[ $tag ] ) ) {
   153 		ksort($wp_filter[$tag]);
   157 		ksort($wp_filter[$tag]);
   154 		$merged_filters[ $tag ] = true;
   158 		$merged_filters[ $tag ] = true;
   172 
   176 
   173 	return $value;
   177 	return $value;
   174 }
   178 }
   175 
   179 
   176 /**
   180 /**
       
   181  * Execute functions hooked on a specific filter hook, specifying arguments in an array.
       
   182  *
       
   183  * @see apply_filters() This function is identical, but the arguments passed to the
       
   184  * functions hooked to <tt>$tag</tt> are supplied using an array.
       
   185  *
       
   186  * @package WordPress
       
   187  * @subpackage Plugin
       
   188  * @since 3.0.0
       
   189  * @global array $wp_filter Stores all of the filters
       
   190  * @global array $merged_filters Merges the filter hooks using this function.
       
   191  * @global array $wp_current_filter stores the list of current filters with the current one last
       
   192  *
       
   193  * @param string $tag The name of the filter hook.
       
   194  * @param array $args The arguments supplied to the functions hooked to <tt>$tag</tt>
       
   195  * @return mixed The filtered value after all hooked functions are applied to it.
       
   196  */
       
   197 function apply_filters_ref_array($tag, $args) {
       
   198 	global $wp_filter, $merged_filters, $wp_current_filter;
       
   199 
       
   200 	// Do 'all' actions first
       
   201 	if ( isset($wp_filter['all']) ) {
       
   202 		$wp_current_filter[] = $tag;
       
   203 		$all_args = func_get_args();
       
   204 		_wp_call_all_hook($all_args);
       
   205 	}
       
   206 
       
   207 	if ( !isset($wp_filter[$tag]) ) {
       
   208 		if ( isset($wp_filter['all']) )
       
   209 			array_pop($wp_current_filter);
       
   210 		return $args[0];
       
   211 	}
       
   212 
       
   213 	if ( !isset($wp_filter['all']) )
       
   214 		$wp_current_filter[] = $tag;
       
   215 
       
   216 	// Sort
       
   217 	if ( !isset( $merged_filters[ $tag ] ) ) {
       
   218 		ksort($wp_filter[$tag]);
       
   219 		$merged_filters[ $tag ] = true;
       
   220 	}
       
   221 
       
   222 	reset( $wp_filter[ $tag ] );
       
   223 
       
   224 	do {
       
   225 		foreach( (array) current($wp_filter[$tag]) as $the_ )
       
   226 			if ( !is_null($the_['function']) )
       
   227 				$args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
       
   228 
       
   229 	} while ( next($wp_filter[$tag]) !== false );
       
   230 
       
   231 	array_pop( $wp_current_filter );
       
   232 
       
   233 	return $args[0];
       
   234 }
       
   235 
       
   236 /**
   177  * Removes a function from a specified filter hook.
   237  * Removes a function from a specified filter hook.
   178  *
   238  *
   179  * This function removes a function attached to a specified filter hook. This
   239  * 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
   240  * method can be used to remove default functions attached to a specific filter
   181  * hook and possibly replace them with a substitute.
   241  * hook and possibly replace them with a substitute.
   189  * @since 1.2
   249  * @since 1.2
   190  *
   250  *
   191  * @param string $tag The filter hook to which the function to be removed is hooked.
   251  * @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.
   252  * @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).
   253  * @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).
   254  * @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
   195  * @return boolean Whether the function existed before it was removed.
   255  * @return boolean Whether the function existed before it was removed.
   196  */
   256  */
   197 function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
   257 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);
   258 	$function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
   199 
   259 
   220  */
   280  */
   221 function remove_all_filters($tag, $priority = false) {
   281 function remove_all_filters($tag, $priority = false) {
   222 	global $wp_filter, $merged_filters;
   282 	global $wp_filter, $merged_filters;
   223 
   283 
   224 	if( isset($wp_filter[$tag]) ) {
   284 	if( isset($wp_filter[$tag]) ) {
   225 		if( false !== $priority && isset($$wp_filter[$tag][$priority]) )
   285 		if( false !== $priority && isset($wp_filter[$tag][$priority]) )
   226 			unset($wp_filter[$tag][$priority]);
   286 			unset($wp_filter[$tag][$priority]);
   227 		else
   287 		else
   228 			unset($wp_filter[$tag]);
   288 			unset($wp_filter[$tag]);
   229 	}
   289 	}
   230 
   290 
   245  */
   305  */
   246 function current_filter() {
   306 function current_filter() {
   247 	global $wp_current_filter;
   307 	global $wp_current_filter;
   248 	return end( $wp_current_filter );
   308 	return end( $wp_current_filter );
   249 }
   309 }
   250 
       
   251 
   310 
   252 /**
   311 /**
   253  * Hooks a function on to a specific action.
   312  * Hooks a function on to a specific action.
   254  *
   313  *
   255  * Actions are the hooks that the WordPress core launches at specific points
   314  * Actions are the hooks that the WordPress core launches at specific points
   270  */
   329  */
   271 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
   330 function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
   272 	return add_filter($tag, $function_to_add, $priority, $accepted_args);
   331 	return add_filter($tag, $function_to_add, $priority, $accepted_args);
   273 }
   332 }
   274 
   333 
   275 
       
   276 /**
   334 /**
   277  * Execute functions hooked on a specific action hook.
   335  * Execute functions hooked on a specific action hook.
   278  *
   336  *
   279  * This function invokes all functions attached to action hook $tag. It is
   337  * This function invokes all functions attached to action hook $tag. It is
   280  * possible to create new action hooks by simply calling this function,
   338  * possible to create new action hooks by simply calling this function,
   297  * @return null Will return null if $tag does not exist in $wp_filter array
   355  * @return null Will return null if $tag does not exist in $wp_filter array
   298  */
   356  */
   299 function do_action($tag, $arg = '') {
   357 function do_action($tag, $arg = '') {
   300 	global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
   358 	global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
   301 
   359 
   302 	if ( is_array($wp_actions) )
   360 	if ( ! isset($wp_actions) )
   303 		$wp_actions[] = $tag;
   361 		$wp_actions = array();
       
   362 
       
   363 	if ( ! isset($wp_actions[$tag]) )
       
   364 		$wp_actions[$tag] = 1;
   304 	else
   365 	else
   305 		$wp_actions = array($tag);
   366 		++$wp_actions[$tag];
   306 
       
   307 	$wp_current_filter[] = $tag;
       
   308 
   367 
   309 	// Do 'all' actions first
   368 	// Do 'all' actions first
   310 	if ( isset($wp_filter['all']) ) {
   369 	if ( isset($wp_filter['all']) ) {
       
   370 		$wp_current_filter[] = $tag;
   311 		$all_args = func_get_args();
   371 		$all_args = func_get_args();
   312 		_wp_call_all_hook($all_args);
   372 		_wp_call_all_hook($all_args);
   313 	}
   373 	}
   314 
   374 
   315 	if ( !isset($wp_filter[$tag]) ) {
   375 	if ( !isset($wp_filter[$tag]) ) {
   316 		array_pop($wp_current_filter);
   376 		if ( isset($wp_filter['all']) )
       
   377 			array_pop($wp_current_filter);
   317 		return;
   378 		return;
   318 	}
   379 	}
   319 
   380 
       
   381 	if ( !isset($wp_filter['all']) )
       
   382 		$wp_current_filter[] = $tag;
       
   383 
   320 	$args = array();
   384 	$args = array();
   321 	if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
   385 	if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
   322 		$args[] =& $arg[0];
   386 		$args[] =& $arg[0];
   323 	else
   387 	else
   324 		$args[] = $arg;
   388 		$args[] = $arg;
   325 	for ( $a = 2; $a < func_num_args(); $a++ )
   389 	for ( $a = 2; $a < func_num_args(); $a++ )
   326 		$args[] = func_get_arg($a);
   390 		$args[] = func_get_arg($a);
   342 
   406 
   343 	array_pop($wp_current_filter);
   407 	array_pop($wp_current_filter);
   344 }
   408 }
   345 
   409 
   346 /**
   410 /**
   347  * Retrieve the number times an action is fired.
   411  * Retrieve the number of times an action is fired.
   348  *
   412  *
   349  * @package WordPress
   413  * @package WordPress
   350  * @subpackage Plugin
   414  * @subpackage Plugin
   351  * @since 2.1
   415  * @since 2.1
   352  * @global array $wp_actions Increments the amount of times action was triggered.
   416  * @global array $wp_actions Increments the amount of times action was triggered.
   355  * @return int The number of times action hook <tt>$tag</tt> is fired
   419  * @return int The number of times action hook <tt>$tag</tt> is fired
   356  */
   420  */
   357 function did_action($tag) {
   421 function did_action($tag) {
   358 	global $wp_actions;
   422 	global $wp_actions;
   359 
   423 
   360 	if ( empty($wp_actions) )
   424 	if ( ! isset( $wp_actions ) || ! isset( $wp_actions[$tag] ) )
   361 		return 0;
   425 		return 0;
   362 
   426 
   363 	return count(array_keys($wp_actions, $tag));
   427 	return $wp_actions[$tag];
   364 }
   428 }
   365 
   429 
   366 /**
   430 /**
   367  * Execute functions hooked on a specific action hook, specifying arguments in an array.
   431  * Execute functions hooked on a specific action hook, specifying arguments in an array.
   368  *
   432  *
   380  * @return null Will return null if $tag does not exist in $wp_filter array
   444  * @return null Will return null if $tag does not exist in $wp_filter array
   381  */
   445  */
   382 function do_action_ref_array($tag, $args) {
   446 function do_action_ref_array($tag, $args) {
   383 	global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
   447 	global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
   384 
   448 
   385 	if ( !is_array($wp_actions) )
   449 	if ( ! isset($wp_actions) )
   386 		$wp_actions = array($tag);
   450 		$wp_actions = array();
       
   451 
       
   452 	if ( ! isset($wp_actions[$tag]) )
       
   453 		$wp_actions[$tag] = 1;
   387 	else
   454 	else
   388 		$wp_actions[] = $tag;
   455 		++$wp_actions[$tag];
   389 
       
   390 	$wp_current_filter[] = $tag;
       
   391 
   456 
   392 	// Do 'all' actions first
   457 	// Do 'all' actions first
   393 	if ( isset($wp_filter['all']) ) {
   458 	if ( isset($wp_filter['all']) ) {
       
   459 		$wp_current_filter[] = $tag;
   394 		$all_args = func_get_args();
   460 		$all_args = func_get_args();
   395 		_wp_call_all_hook($all_args);
   461 		_wp_call_all_hook($all_args);
   396 	}
   462 	}
   397 
   463 
   398 	if ( !isset($wp_filter[$tag]) ) {
   464 	if ( !isset($wp_filter[$tag]) ) {
   399 		array_pop($wp_current_filter);
   465 		if ( isset($wp_filter['all']) )
       
   466 			array_pop($wp_current_filter);
   400 		return;
   467 		return;
   401 	}
   468 	}
       
   469 
       
   470 	if ( !isset($wp_filter['all']) )
       
   471 		$wp_current_filter[] = $tag;
   402 
   472 
   403 	// Sort
   473 	// Sort
   404 	if ( !isset( $merged_filters[ $tag ] ) ) {
   474 	if ( !isset( $merged_filters[ $tag ] ) ) {
   405 		ksort($wp_filter[$tag]);
   475 		ksort($wp_filter[$tag]);
   406 		$merged_filters[ $tag ] = true;
   476 		$merged_filters[ $tag ] = true;
   425  * @subpackage Plugin
   495  * @subpackage Plugin
   426  * @since 2.5
   496  * @since 2.5
   427  * @see has_filter() has_action() is an alias of has_filter().
   497  * @see has_filter() has_action() is an alias of has_filter().
   428  *
   498  *
   429  * @param string $tag The name of the action hook.
   499  * @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.
   500  * @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.
   501  * @return int|boolean Optionally returns the priority on that hook for the specified function.
   432  */
   502  */
   433 function has_action($tag, $function_to_check = false) {
   503 function has_action($tag, $function_to_check = false) {
   434 	return has_filter($tag, $function_to_check);
   504 	return has_filter($tag, $function_to_check);
   435 }
   505 }
   446  * @since 1.2
   516  * @since 1.2
   447  *
   517  *
   448  * @param string $tag The action hook to which the function to be removed is hooked.
   518  * @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.
   519  * @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).
   520  * @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).
   521  * @param int $accepted_args optional. The number of arguments the function accepts (default: 1).
   452  * @return boolean Whether the function is removed.
   522  * @return boolean Whether the function is removed.
   453  */
   523  */
   454 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
   524 function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
   455 	return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
   525 	return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
   456 }
   526 }
   595  * executing.
   665  * executing.
   596  *
   666  *
   597  * @since 2.7
   667  * @since 2.7
   598  *
   668  *
   599  * @param string $file
   669  * @param string $file
   600  * @param callback $callback The callback to run when the hook is called.
   670  * @param callback $callback The callback to run when the hook is called. Must be a static method or function.
   601  */
   671  */
   602 function register_uninstall_hook($file, $callback) {
   672 function register_uninstall_hook( $file, $callback ) {
       
   673 	if ( is_array( $callback ) && is_object( $callback[0] ) ) {
       
   674 		_doing_it_wrong( __FUNCTION__, __( 'Only a static class method or function can be used in an uninstall hook.' ), '3.1' );
       
   675 		return;
       
   676 	}
       
   677 
   603 	// The option should not be autoloaded, because it is not needed in most
   678 	// 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
   679 	// cases. Emphasis should be put on using the 'uninstall.php' way of
   605 	// uninstalling the plugin.
   680 	// uninstalling the plugin.
   606 	$uninstallable_plugins = (array) get_option('uninstall_plugins');
   681 	$uninstallable_plugins = (array) get_option('uninstall_plugins');
   607 	$uninstallable_plugins[plugin_basename($file)] = $callback;
   682 	$uninstallable_plugins[plugin_basename($file)] = $callback;
   665  * @link http://trac.wordpress.org/ticket/3875
   740  * @link http://trac.wordpress.org/ticket/3875
   666  *
   741  *
   667  * @global array $wp_filter Storage for all of the filters and actions
   742  * @global array $wp_filter Storage for all of the filters and actions
   668  * @param string $tag Used in counting how many hooks were applied
   743  * @param string $tag Used in counting how many hooks were applied
   669  * @param callback $function Used for creating unique id
   744  * @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.
   745  * @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
   746  * @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 unique id.
   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  */
   747  */
   674 function _wp_filter_build_unique_id($tag, $function, $priority) {
   748 function _wp_filter_build_unique_id($tag, $function, $priority) {
   675 	global $wp_filter;
   749 	global $wp_filter;
   676 	static $filter_id_count = 0;
   750 	static $filter_id_count = 0;
   677 
   751 
   678 	if ( is_string($function) ) {
   752 	if ( is_string($function) )
   679 		return $function;
   753 		return $function;
   680 	} else if (is_object($function[0]) ) {
   754 
       
   755 	if ( is_object($function) ) {
       
   756 		// Closures are currently implemented as objects
       
   757 		$function = array( $function, '' );
       
   758 	} else {
       
   759 		$function = (array) $function;
       
   760 	}
       
   761 
       
   762 	if (is_object($function[0]) ) {
   681 		// Object Class Calling
   763 		// Object Class Calling
   682 		if ( function_exists('spl_object_hash') ) {
   764 		if ( function_exists('spl_object_hash') ) {
   683 			return spl_object_hash($function[0]) . $function[1];
   765 			return spl_object_hash($function[0]) . $function[1];
   684 		} else {
   766 		} else {
   685 			$obj_idx = get_class($function[0]).$function[1];
   767 			$obj_idx = get_class($function[0]).$function[1];
   690 				$function[0]->wp_filter_id = $filter_id_count;
   772 				$function[0]->wp_filter_id = $filter_id_count;
   691 				++$filter_id_count;
   773 				++$filter_id_count;
   692 			} else {
   774 			} else {
   693 				$obj_idx .= $function[0]->wp_filter_id;
   775 				$obj_idx .= $function[0]->wp_filter_id;
   694 			}
   776 			}
   695 	
   777 
   696 			return $obj_idx;
   778 			return $obj_idx;
   697 		}
   779 		}
   698 	} else if ( is_string($function[0]) ) {
   780 	} else if ( is_string($function[0]) ) {
   699 		// Static Calling
   781 		// Static Calling
   700 		return $function[0].$function[1];
   782 		return $function[0].$function[1];
   701 	}
   783 	}
   702 }
   784 }
   703 
       
   704 ?>