WordPress.org

Make WordPress Core

Ticket #5231: plugin.#5231.r6312.take2.diff

File plugin.#5231.r6312.take2.diff, 4.0 KB (added by darkdragon, 11 years ago)

Speed enhancement to removing filters.

  • plugin.php

     
    7272function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    7373        global $wp_filter, $merged_filters;
    7474
     75        // If all tag was added by action, this won't match.
     76        if($tag == 'all')
     77                $tag = 'all_filter';
     78
    7579        $idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
    7680        $wp_filter[$tag][$priority][$idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
    7781        unset( $merged_filters[ $tag ] );
     
    110114        global $wp_filter, $merged_filters;
    111115
    112116        if ( !isset( $merged_filters[ $tag ] ) )
    113                 merge_filters($tag);
     117                merge_filters($tag, 'filter');
    114118
    115119        if ( !isset($wp_filter[$tag]) )
    116120                return $value;
     
    149153 * @global array $merge_filters Merges the filter hooks using this function.
    150154 *
    151155 * @param string $tag The filter hook of which the functions should be merged.
     156 * @param string $caller Whether the caller is a 'filter' or 'action'
    152157 */
    153 function merge_filters($tag) {
     158function merge_filters($tag, $caller='filter') {
    154159        global $wp_filter, $merged_filters;
    155160
    156         if ( isset($wp_filter['all']) && is_array($wp_filter['all']) )
    157                 $wp_filter[$tag] = array_merge($wp_filter['all'], (array) $wp_filter[$tag]);
     161        $all = 'all_'.$caller;
     162        if ( isset($wp_filter[$all]) && is_array($wp_filter[$all]) )
     163                $wp_filter[$tag] = $wp_filter[$all] + (array) $wp_filter[$tag];
    158164
    159165        if ( isset($wp_filter[$tag]) ){
    160166                reset($wp_filter[$tag]);
     
    185191 * @return boolean Whether the function existed before it was removed.
    186192 */
    187193function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
     194        if($tag == 'all')
     195                $tag = 'all_filter';
     196
    188197        $function_to_remove = _wp_filter_build_unique_id($tag, $function_to_remove, $priority);
    189198
    190199        $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
    191200
    192         unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
    193         unset($GLOBALS['merged_filters'][$tag]);
     201        if( true === $r) {
     202                unset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
     203                unset($GLOBALS['merged_filters'][$tag]);
     204        }
    194205
    195206        return $r;
    196207}
     
    215226 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
    216227 */
    217228function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
     229        if($tag == 'all')
     230                $tag = 'all_action';
     231
    218232        add_filter($tag, $function_to_add, $priority, $accepted_args);
    219233}
    220234
     
    256270        for ( $a = 2; $a < func_num_args(); $a++ )
    257271                $args[] = func_get_arg($a);
    258272
    259         merge_filters($tag);
     273        if ( !isset( $merged_filters[ $tag ] ) )
     274                merge_filters($tag, 'action');
    260275
    261276        if ( !isset($wp_filter[$tag]) )
    262277                return;
     
    316331        else
    317332                $wp_actions[] = $tag;
    318333
    319         merge_filters($tag);
     334        if ( !isset( $merged_filters[ $tag ] ) )
     335                merge_filters($tag, 'action');
    320336
    321337        if ( !isset($wp_filter[$tag]) )
    322338                return;
     
    350366 * @return boolean Whether the function is removed.
    351367 */
    352368function remove_action($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
     369        if($tag == 'all')
     370                $tag = 'all_action';
     371
    353372        return remove_filter($tag, $function_to_remove, $priority, $accepted_args);
    354373}
    355374
     
    461480 * @param int $priority Used in counting how many hooks were applied
    462481 * @return string Unique ID for usage as array key
    463482 */
    464 function _wp_filter_build_unique_id($tag, $function, $priority = 10)
    465 {
     483function _wp_filter_build_unique_id($tag, $function, $priority = 10) {
    466484        global $wp_filter;
    467485
    468486        // If function then just skip all of the tests and not overwrite the following.
     
    472490        else if(is_object($function[0]) )
    473491        {
    474492                $obj_idx = get_class($function[0]).$function[1];
    475                 if( is_null($function[0]->wp_filter_id) ) { // This should be instead of is_null() change to !isset() to fix notice
     493                if( !isset($function[0]->wp_filter_id) ) {
    476494                        $count = count((array)$wp_filter[$tag][$priority]);
    477495                        $function[0]->wp_filter_id = $count;
    478496                        $obj_idx .= $count;