Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
require_once( 'wp-includes/class-wp-hook.php' );
require_once( 'wp-includes/class-wp-hook-iterator.php' );
function _wp_filter_build_unique_id($tag, $function, $priority) {
global $wp_filter;
static $filter_id_count = 0;
if ( is_string($function) )
return $function;
if ( is_object($function) ) {
// Closures are currently implemented as objects
$function = array( $function, '' );
} else {
$function = (array) $function;
}
if (is_object($function[0]) ) {
// Object Class Calling
if ( function_exists('spl_object_hash') ) {
return spl_object_hash($function[0]) . $function[1];
} else {
$obj_idx = get_class($function[0]).$function[1];
if ( !isset($function[0]->wp_filter_id) ) {
if ( false === $priority )
return false;
$obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
$function[0]->wp_filter_id = $filter_id_count;
++$filter_id_count;
} else {
$obj_idx .= $function[0]->wp_filter_id;
}
return $obj_idx;
}
} else if ( is_string($function[0]) ) {
// Static Calling
return $function[0] . '::' . $function[1];
}
}
function do_action_old($tag, $arg = '') {
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
if ( ! isset($wp_actions[$tag]) )
$wp_actions[$tag] = 1;
else
++$wp_actions[$tag];
// Do 'all' actions first
if ( isset($wp_filter['all']) ) {
$wp_current_filter[] = $tag;
$all_args = func_get_args();
_wp_call_all_hook($all_args);
}
if ( !isset($wp_filter[$tag]) ) {
if ( isset($wp_filter['all']) )
array_pop($wp_current_filter);
return;
}
if ( !isset($wp_filter['all']) )
$wp_current_filter[] = $tag;
$args = array();
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
$args[] =& $arg[0];
else
$args[] = $arg;
for ( $a = 2; $a < func_num_args(); $a++ )
$args[] = func_get_arg($a);
// Sort
if ( !isset( $merged_filters[ $tag ] ) ) {
ksort($wp_filter[$tag]);
$merged_filters[ $tag ] = true;
}
reset( $wp_filter[ $tag ] );
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]) !== false );
array_pop($wp_current_filter);
}
function add_filter_old( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
global $wp_filter, $merged_filters;
$idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
$wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
unset( $merged_filters[ $tag ] );
return true;
}
function do_action_new($tag, $arg = '') {
global $wp_filter, $wp_actions, $wp_current_filter;
if ( ! isset($wp_actions[$tag]) )
$wp_actions[$tag] = 1;
else
++$wp_actions[$tag];
// Do 'all' actions first
if ( isset($wp_filter['all']) ) {
$wp_current_filter[] = $tag;
$all_args = func_get_args();
_wp_call_all_hook($all_args);
}
if ( !isset($wp_filter[$tag]) ) {
if ( isset($wp_filter['all']) )
array_pop($wp_current_filter);
return;
}
if ( !isset($wp_filter['all']) )
$wp_current_filter[] = $tag;
$args = array();
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
$args[] =& $arg[0];
else
$args[] = $arg;
for ( $a = 2; $a < func_num_args(); $a++ )
$args[] = func_get_arg($a);
$iterator = $wp_filter[$tag]->get_iterator();
foreach ( $iterator as $the_ ) {
call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
}
$iterator->cleanup();
array_pop($wp_current_filter);
}
function add_filter_new( $tag, $function_to_add, $priority = 10, $accepted_args = 1 ) {
global $wp_filter;
if ( !isset($wp_filter[$tag]) ) {
$wp_filter[$tag] = new WP_Hook();
}
$wp_filter[$tag]->add_filter( $function_to_add, $priority, $accepted_args, $tag );
return true;
}
const TEST_ACTION_NAME = '17817';
function ticket_17817_run( $version = 'old', $count = 100 ) {
global $wp_filter;
if ( !isset($wp_filter) ) {
$wp_filter = array();
} elseif ( isset($wp_filter[TEST_ACTION_NAME]) && $wp_filter[TEST_ACTION_NAME] instanceof WP_Hook ) {
$wp_filter[TEST_ACTION_NAME]->remove_all_filters(TEST_ACTION_NAME);
} else {
$wp_filter = array();
}
$add_filter = 'add_filter_'.$version;
$do_action = 'do_action_'.$version;
for ( $i = 0 ; $i < $count ; $i++ ) {
$add_filter( TEST_ACTION_NAME, function(){}, rand(1,1000), 0 );
}
$start = microtime(TRUE);
$do_action(TEST_ACTION_NAME);
$end = microtime(TRUE);
return $end - $start;
}
$loop_count = 10000;
$callback_count = 15;
if ( isset($_SERVER['HTTP_HOST']) ) { echo "<pre>"; }
printf( "Running %d times with %d callbacks\n", $loop_count, $callback_count );
$times = array();
$start = microtime(TRUE);
for ( $i = 0 ; $i < $loop_count ; $i++ ) {
$times[] = ticket_17817_run('old', $callback_count);
}
$end = microtime(TRUE);
$total_old = $end - $start;
$average_old = array_sum($times)/count($times);
unset($GLOBALS['wp_filter']);
$times = array();
$start = microtime(TRUE);
for ( $i = 0 ; $i < $loop_count ; $i++ ) {
$times[] = ticket_17817_run('new', $callback_count);
}
$end = microtime(TRUE);
$total_new = $end - $start;
$average_new = array_sum($times)/count($times);
printf( "Total Time (Old): %02f\n", $total_old );
printf( "Average Time (Old): %02f\n", $average_old );
printf( "Total Time (New): %02f\n", $total_new );
printf( "Average Time (New): %02f\n", $average_new );
printf( "New runs in %02.2f%% of the time of old.\n", ( $total_new / $total_old ) * 100 );
die();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.