2006-06-19 22:03:24 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
//
|
|
|
|
// Filter functions, the core of the WP plugin architecture.
|
|
|
|
//
|
|
|
|
|
|
|
|
function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
|
|
|
|
global $wp_filter;
|
|
|
|
|
2007-02-27 20:09:20 -05:00
|
|
|
// So the format is wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]']
|
|
|
|
$wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
|
2006-06-19 22:03:24 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
function apply_filters($tag, $string) {
|
|
|
|
global $wp_filter;
|
|
|
|
|
|
|
|
merge_filters($tag);
|
|
|
|
|
2006-10-13 10:01:53 -04:00
|
|
|
if ( !isset($wp_filter[$tag]) )
|
2006-06-19 22:03:24 -04:00
|
|
|
return $string;
|
|
|
|
|
2007-02-27 20:09:20 -05:00
|
|
|
$args = func_get_args();
|
|
|
|
|
|
|
|
do{
|
|
|
|
foreach( (array) current($wp_filter[$tag]) as $the_ )
|
|
|
|
if ( !is_null($the_['function']) ){
|
|
|
|
$args[1] = $string;
|
|
|
|
$string = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
|
2006-06-19 22:03:24 -04:00
|
|
|
}
|
2007-02-27 20:09:20 -05:00
|
|
|
|
|
|
|
} while ( next($wp_filter[$tag]) );
|
|
|
|
|
2006-06-19 22:03:24 -04:00
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
|
|
|
|
function merge_filters($tag) {
|
|
|
|
global $wp_filter;
|
|
|
|
|
2007-02-27 20:09:20 -05:00
|
|
|
if ( isset($wp_filter['all']) )
|
|
|
|
$wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]);
|
|
|
|
|
|
|
|
if ( isset($wp_filter[$tag]) ){
|
|
|
|
reset($wp_filter[$tag]);
|
|
|
|
uksort($wp_filter[$tag], "strnatcasecmp");
|
|
|
|
}
|
2006-06-19 22:03:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
|
|
|
|
global $wp_filter;
|
|
|
|
|
2007-02-27 20:09:20 -05:00
|
|
|
unset($GLOBALS['wp_filter'][$tag][$priority][serialize($function_to_remove)]);
|
|
|
|
|
2006-06-19 22:03:24 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Action functions
|
|
|
|
//
|
|
|
|
|
|
|
|
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
|
|
|
|
add_filter($tag, $function_to_add, $priority, $accepted_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
function do_action($tag, $arg = '') {
|
2006-12-07 22:45:34 -05:00
|
|
|
global $wp_filter, $wp_actions;
|
|
|
|
|
2006-09-08 19:19:29 -04:00
|
|
|
$args = array();
|
|
|
|
if ( is_array($arg) && 1 == count($arg) && is_object($arg[0]) ) // array(&$this)
|
|
|
|
$args[] =& $arg[0];
|
|
|
|
else
|
|
|
|
$args[] = $arg;
|
2006-09-08 19:18:03 -04:00
|
|
|
for ( $a = 2; $a < func_num_args(); $a++ )
|
2006-09-08 19:19:29 -04:00
|
|
|
$args[] = func_get_arg($a);
|
2006-06-19 22:03:24 -04:00
|
|
|
|
|
|
|
merge_filters($tag);
|
|
|
|
|
2006-09-12 13:45:23 -04:00
|
|
|
if ( !isset($wp_filter[$tag]) )
|
2006-06-19 22:03:24 -04:00
|
|
|
return;
|
2006-09-12 13:45:23 -04:00
|
|
|
|
2007-02-27 20:09:20 -05:00
|
|
|
do{
|
|
|
|
foreach( (array) current($wp_filter[$tag]) as $the_ )
|
|
|
|
if ( !is_null($the_['function']) )
|
|
|
|
call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
|
2006-09-12 13:45:23 -04:00
|
|
|
|
2007-02-27 20:09:20 -05:00
|
|
|
} while ( next($wp_filter[$tag]) );
|
2006-12-07 22:45:34 -05:00
|
|
|
|
|
|
|
if ( is_array($wp_actions) )
|
|
|
|
$wp_actions[] = $tag;
|
|
|
|
else
|
|
|
|
$wp_actions = array($tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the number of times an action has been done
|
|
|
|
function did_action($tag) {
|
|
|
|
global $wp_actions;
|
|
|
|
|
|
|
|
return count(array_keys($wp_actions, $tag));
|
2006-09-12 13:45:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function do_action_ref_array($tag, $args) {
|
2006-12-07 22:45:34 -05:00
|
|
|
global $wp_filter, $wp_actions;
|
|
|
|
|
|
|
|
if ( !is_array($wp_actions) )
|
|
|
|
$wp_actions = array($tag);
|
|
|
|
else
|
|
|
|
$wp_actions[] = $tag;
|
2006-09-12 13:45:23 -04:00
|
|
|
|
|
|
|
merge_filters($tag);
|
|
|
|
|
|
|
|
if ( !isset($wp_filter[$tag]) )
|
|
|
|
return;
|
|
|
|
|
2007-02-27 20:09:20 -05:00
|
|
|
do{
|
|
|
|
foreach( (array) current($wp_filter[$tag]) as $the_ )
|
|
|
|
if ( !is_null($the_['function']) )
|
|
|
|
call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
|
|
|
|
|
|
|
|
} while ( next($wp_filter[$tag]) );
|
|
|
|
|
2006-06-19 22:03:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
|
|
|
|
remove_filter($tag, $function_to_remove, $priority, $accepted_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Functions for handling plugins.
|
|
|
|
//
|
|
|
|
|
|
|
|
function plugin_basename($file) {
|
|
|
|
$file = preg_replace('|\\\\+|', '\\\\', $file);
|
|
|
|
$file = preg_replace('/^.*wp-content[\\\\\/]plugins[\\\\\/]/', '', $file);
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
|
|
|
function register_activation_hook($file, $function) {
|
|
|
|
$file = plugin_basename($file);
|
|
|
|
add_action('activate_' . $file, $function);
|
|
|
|
}
|
|
|
|
|
|
|
|
function register_deactivation_hook($file, $function) {
|
|
|
|
$file = plugin_basename($file);
|
|
|
|
add_action('deactivate_' . $file, $function);
|
|
|
|
}
|
|
|
|
|
2006-12-06 22:57:23 -05:00
|
|
|
?>
|