Skip to content

Instantly share code, notes, and snippets.

@kurtpayne
Created July 19, 2012 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtpayne/3146810 to your computer and use it in GitHub Desktop.
Save kurtpayne/3146810 to your computer and use it in GitHub Desktop.
Faster filters, maybe
Index: wp-includes/plugin.php
===================================================================
--- wp-includes/plugin.php (revision 21276)
+++ wp-includes/plugin.php (working copy)
@@ -164,12 +164,26 @@
$args = func_get_args();
do {
- foreach( (array) current($wp_filter[$tag]) as $the_ )
- if ( !is_null($the_['function']) ){
- $args[1] = $value;
- $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
- }
+ foreach( (array) current($wp_filter[$tag]) as $the_ ) {
+ if ( null !== ($the_['function']) ) {
+ // Skip call_user_func_array for string() accepting 1 arg
+ if ( 1 == $the_['accepted_args'] && is_string( $the_['function'] ) ) {
+ $args[1] = $value;
+ $value = $the_['function']( $args[1] );
+
+ // Skip call_user_func_array for object->method() accepting 1 arg
+ } elseif ( 1 == $the_['accepted_args'] && is_array( $the_['function']) && is_object( $the_['function'][0] ) ) {
+ $args[1] = $value;
+ $value = $the_['function'][0]->$the_['function'][1]( $args[1] );
+
+ // Default case
+ } else {
+ $args[1] = $value;
+ $value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
+ }
+ }
+ }
} while ( next($wp_filter[$tag]) !== false );
array_pop( $wp_current_filter );
@@ -222,10 +236,23 @@
reset( $wp_filter[ $tag ] );
do {
- foreach( (array) current($wp_filter[$tag]) as $the_ )
- if ( !is_null($the_['function']) )
- $args[0] = call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));
+ foreach( (array) current($wp_filter[$tag]) as $the_ ) {
+ if ( null !== $the_['function'] ) {
+ // Skip call_user_func_array for string() accepting 1 arg
+ if ( 1 == $the_['accepted_args'] && is_string( $the_['function'] ) ) {
+ $args[0] = $the_['function']( $args[0] );
+
+ // Skip call_user_func_array for object->method() accepting 1 arg
+ } elseif ( 1 == $the_['accepted_args'] && is_array( $the_['function']) && is_object( $the_['function'][0] ) ) {
+ $args[0] = $the_['function'][0]->$the_['function'][1]( $args[0] );
+
+ // Default case
+ } else {
+ $args[0] = 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 );
@@ -398,10 +425,23 @@
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']));
+ foreach ( (array) current($wp_filter[$tag]) as $the_ ) {
+ if ( null !== $the_['function']) {
+ // Skip call_user_func_array for string() accepting 1 arg
+ if ( 1 == $the_['accepted_args'] && is_string( $the_['function'] ) ) {
+ $the_['function']( $args[0] );
+
+ // Skip call_user_func_array for object->method() accepting 1 arg
+ } elseif ( 1 == $the_['accepted_args'] && is_array( $the_['function']) && is_object( $the_['function'][0] ) ) {
+ $the_['function'][0]->$the_['function'][1]( $args[0] );
+
+ // Default case
+ } else {
+ 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);
@@ -479,10 +519,23 @@
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']));
+ foreach( (array) current($wp_filter[$tag]) as $the_ ) {
+ if ( null !== $the_['function'] ) {
+
+ // Skip call_user_func_array for string() accepting 1 arg
+ if ( 1 == $the_['accepted_args'] && is_string( $the_['function'] ) ) {
+ $the_['function']( $args[0] );
+
+ // Skip call_user_func_array for object->method() accepting 1 arg
+ } elseif ( 1 == $the_['accepted_args'] && is_array( $the_['function']) && is_object( $the_['function'][0] ) ) {
+ $the_['function'][0]->$the_['function'][1]( $args[0] );
+ // Default case
+ } else {
+ $value = 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);
@@ -710,7 +763,7 @@
reset( $wp_filter['all'] );
do {
foreach( (array) current($wp_filter['all']) as $the_ )
- if ( !is_null($the_['function']) )
+ if ( null !== $the_['function'] )
call_user_func_array($the_['function'], $args);
} while ( next($wp_filter['all']) !== false );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment