Make WordPress Core

Ticket #21267: filter-callback-keys.diff

File filter-callback-keys.diff, 5.2 KB (added by wonderboymusic, 12 years ago)
  • wp-includes/plugin.php

     
    724724/**
    725725 * Build Unique ID for storage and retrieval.
    726726 *
    727  * The old way to serialize the callback caused issues and this function is the
    728  * solution. It works by checking for objects and creating an a new property in
    729  * the class to keep track of the object and new objects of the same class that
    730  * need to be added.
    731  *
    732  * It also allows for the removal of actions and filters for objects after they
    733  * change class properties. It is possible to include the property $wp_filter_id
    734  * in your class and set it to "null" or a number to bypass the workaround.
    735  * However this will prevent you from adding new classes and any new classes
    736  * will overwrite the previous hook by the same class.
    737  *
    738727 * Functions and static method callbacks are just returned as strings and
    739728 * shouldn't have any speed penalty.
    740729 *
     
    750739 * @param int|bool $priority Used in counting how many hooks were applied. If === false and $function is an object reference, we return the unique id only if it already has one, false otherwise.
    751740 * @return string|bool Unique ID for usage as array key or false if $priority === false and $function is an object reference, and it does not already have a unique id.
    752741 */
    753 function _wp_filter_build_unique_id($tag, $function, $priority) {
    754         global $wp_filter;
    755         static $filter_id_count = 0;
    756 
    757         if ( is_string($function) )
     742function _wp_filter_build_unique_id( $tag, $function, $priority ) {
     743        if ( is_string( $function ) )
    758744                return $function;
    759745
    760         if ( is_object($function) ) {
    761                 // Closures are currently implemented as objects
    762                 $function = array( $function, '' );
    763         } else {
    764                 $function = (array) $function;
     746        if ( is_object( $function ) && 'Closure' === get_class( $function ) )
     747                return _wp_build_unique_closure_id( $function );
     748       
     749        $function = array_filter( (array) $function );
     750       
     751        if ( is_object( $function[0] ) ) {     
     752                return spl_object_hash( $function[0] ) . '~' . get_class( $function[0] ) . '::' . $function[1];
     753        } else if ( is_string( $function[0] ) ) {
     754                // Static Calling
     755                return join( '::', $function );
    765756        }
     757}
    766758
    767         if (is_object($function[0]) ) {
    768                 // Object Class Calling
    769                 if ( function_exists('spl_object_hash') ) {
    770                         return spl_object_hash($function[0]) . $function[1];
    771                 } else {
    772                         $obj_idx = get_class($function[0]).$function[1];
    773                         if ( !isset($function[0]->wp_filter_id) ) {
    774                                 if ( false === $priority )
    775                                         return false;
    776                                 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
    777                                 $function[0]->wp_filter_id = $filter_id_count;
    778                                 ++$filter_id_count;
    779                         } else {
    780                                 $obj_idx .= $function[0]->wp_filter_id;
    781                         }
     759/**
     760 * Build Unique ID for Closure
     761 *
     762 * @package WordPress
     763 * @subpackage Plugin
     764 * @access private
     765 * @since 3.5
     766 *
     767 * @param callback $function Used for creating unique id
     768 * @return string Unique ID for usage as array key
     769 */
     770function _wp_build_unique_closure_id( $function ) {
     771        ob_start();
     772        echo new ReflectionFunction( $function );
     773        $source = ob_get_clean();
     774        return md5( $source );
     775}
    782776
    783                         return $obj_idx;
    784                 }
    785         } else if ( is_string($function[0]) ) {
    786                 // Static Calling
    787                 return $function[0].$function[1];
     777/**
     778 * Build Unique ID for list of callbacks
     779 *
     780 * @package WordPress
     781 * @subpackage Plugin
     782 * @access private
     783 * @since 3.5
     784 *
     785 * @param string $tag Name of filter
     786 * @return string Unique ID representing hash of filter callback array keys (hashes)
     787 */
     788function wp_filter_callbacks_hash( $tag ) {
     789        global $wp_filter;
     790               
     791        if ( ! has_filter( $tag ) )
     792                return '';
     793       
     794        $hashes = array();
     795       
     796        foreach ( $wp_filter[$tag] as $priority )
     797                $hashes = array_merge( $hashes, array_keys( $priority ) );
     798       
     799        $hashes = array_map( '_dehash_filter_callback_object', $hashes );
     800       
     801        return md5( join( '', $hashes ) );
     802}
     803
     804/**
     805 * Remove the spl_object_hash portion of the filter callback key
     806 * This will allow persistence between requests
     807 *
     808 * @package WordPress
     809 * @subpackage Plugin
     810 * @access private
     811 * @since 3.5
     812 *
     813 * @param string $hash
     814 * @return string
     815 *
     816 */
     817function _dehash_filter_callback_object( $hash ) {
     818        if ( strstr( $hash, '~' ) ) {
     819                $bits = explode( '~', $hash );
     820                array_shift( $bits );
     821                return join( '', $bits );
    788822        }
    789 }
     823                       
     824        return $hash;
     825}
     826 No newline at end of file
  • wp-includes/taxonomy.php

     
    12181218        }
    12191219
    12201220        // $args can be whatever, only use the args defined in defaults to compute the key
    1221         $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
    1222         $key = md5( serialize( compact(array_keys($defaults)) ) . serialize( $taxonomies ) . $filter_key );
     1221        $filter_key = wp_filter_callbacks_hash( 'list_terms_exclusions' );
     1222        $key = md5( serialize( compact( array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
    12231223        $last_changed = wp_cache_get('last_changed', 'terms');
    12241224        if ( !$last_changed ) {
    12251225                $last_changed = time();