WordPress.org

Make WordPress Core

Ticket #3875: wp-includes.plugin.php.diff

File wp-includes.plugin.php.diff, 3.8 KB (added by santosj, 13 years ago)

Patch for review, hasn't been tested, but might provide fixes.

  • plugin.php

     
    1515 * @param int $accepted_args optional. The number of arguments the function accept (default 1). In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run.
    1616 * @return boolean true if the <tt>$function_to_add</tt> is added succesfully to filter <tt>$tag</tt>. How many arguments your function takes. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching <tt>do_action()</tt> or <tt>apply_filters()</tt> call is run. For example, the action <tt>comment_id_not_found</tt> will pass any functions that hook onto it the ID of the requested comment.
    1717 */
    18 function add_filter($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
     18function add_filter($tag, &$function_to_add, $priority = 10, $accepted_args = 1) {
    1919        global $wp_filter, $merged_filters;
    2020
    2121        // So the format is wp_filter['tag']['array of priorities']['array of functions serialized']['array of ['array (functions, accepted_args)]']
    22         $wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
     22        $fn_idx = _wp_filter_build_unique_id($tag, $function_to_add, $priority);
     23       
     24        $wp_filter[$tag][$priority][$fn_idx] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
     25        //$wp_filter[$tag][$priority][serialize($function_to_add)] = array('function' => $function_to_add, 'accepted_args' => $accepted_args);
    2326        unset( $merged_filters[ $tag ] );
    2427        return true;
    2528}
     
    97100 * @return boolean Whether the function is removed.
    98101 */
    99102function remove_filter($tag, $function_to_remove, $priority = 10, $accepted_args = 1) {
    100         $function_to_remove = serialize($function_to_remove);
     103        $function_to_remove = _wp_filter_build_unique_id($tag, &$function_to_remove, $priority);
    101104
    102105        $r = isset($GLOBALS['wp_filter'][$tag][$priority][$function_to_remove]);
    103106
     
    280283        add_action('deactivate_' . $file, $function);
    281284}
    282285
     286function _wp_filter_build_unique_id($tag, &$function_to_add, $priority = 10)
     287{
     288        global $wp_filter;
     289        // If function then just skip all of the tests and not overwrite the following.
     290        $fn_idx = $function_to_add;
     291       
     292        // Only test if using an array to call a class and method.
     293        // Else bypass all of the following checks.
     294        if(is_array($function_to_add))
     295        {
     296                list($obj,$fn) = $function_to_add;
     297               
     298                // Expect most use cases for $obj to be objects and not strings
     299                if(is_object($obj))
     300                {
     301                        // We are going to use $classname more than once, store it
     302                        $classname = get_class($obj);
     303                        $fn_idx = $classname.$fn;
     304                       
     305                        if(!is_null($function_to_add[0]->wp_filter_id))
     306                        {
     307                                $fn_idx .= $function_to_add[0]->wp_filter_id;
     308                        }
     309                       
     310                        // Compare the two object references to see if they are the same object.
     311                        // This is slow however as it will check each property and method for similar, much
     312                        // like an array comparsion. Should work similar to PHP 4 and PHP 5.
     313                        if($function_to_add[0] == $wp_filter[$tag][$priority][$fn_idx]['function'][0]) {
     314                                return $fn_idx; // Do not add
     315                        } else {
     316                                $i = count($wp_filter[$tag][$priority]);
     317                                $function_to_add[0]->wp_filter_id = $i;
     318                                $fn_idx .= $function_to_add[0]->wp_filter_id;
     319                        }
     320                }
     321                // Must mean that it is using static calling.
     322                // We can also assume that if they are using static calling
     323                // it doesn't matter if they have two or more variables that they are trying
     324                // to add to the same hooks. The bug would be theirs, we can only assume
     325                // that they know enough when doing static method call binding to know what
     326                // to expect.
     327                else
     328                        $fn_idx = $obj.fn;
     329        }
     330        return $fn_idx;
     331}
     332
    283333?>