WordPress.org

Make WordPress Core

Ticket #10535: 10535.6.patch

File 10535.6.patch, 4.8 KB (added by hakre, 8 years ago)

Introducing wp_object_hash(), fixed some whitespace issues and a variable typo.

  • wp-includes/plugin.php

     
    703703/**
    704704 * Build Unique ID for storage and retrieval.
    705705 *
    706  * The old way to serialize the callback caused issues and this function is the
    707  * solution. It works by checking for objects and creating an a new property in
    708  * the class to keep track of the object and new objects of the same class that
    709  * need to be added.
    710  *
     706 * Every hook callback needs to have a Unique ID to identify it within the
     707 * hook registry, the global array $wp_filter.
     708 *
    711709 * It also allows for the removal of actions and filters for objects after they
    712  * change class properties. It is possible to include the property $wp_filter_id
    713  * in your class and set it to "null" or a number to bypass the workaround.
    714  * However this will prevent you from adding new classes and any new classes
    715  * will overwrite the previous hook by the same class.
     710 * change class properties.
    716711 *
    717712 * Functions and static method callbacks are just returned as strings and
    718713 * shouldn't have any speed penalty.
    719714 *
     715 * PHP 5.3 anonymous functions are supported as well.
     716 *
    720717 * @package WordPress
    721718 * @subpackage Plugin
    722719 * @access private
     
    724721 * @link http://trac.wordpress.org/ticket/3875
    725722 *
    726723 * @global array $wp_filter Storage for all of the filters and actions
    727  * @param string $tag Used in counting how many hooks were applied
    728  * @param callback $function Used for creating unique id
    729  * @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.
    730  * @param string $type filter or action
    731  * @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 uniqe id.
     724 * @param string $tag Obsolete, kept for backwards compability.
     725 * @param callback $function Callback to create Unique ID of.
     726 * @param int|bool $priority Obsolete, kept for backwards compability.
     727 * @return string|bool Unique ID (string), or false on failure
    732728 */
    733729function _wp_filter_build_unique_id($tag, $function, $priority) {
    734         global $wp_filter;
    735         static $filter_id_count = 0;
    736730
    737         if ( is_string($function) )
     731        // Global function hook or anonymous create_function()-hook.
     732        if ( is_string( $function ) )
    738733                return $function;
    739734
    740         if ( is_object($function) ) {
    741                 // Closures are currently implemented as objects
     735        // Closures are currently implemented as objects, cast everything
     736        // else into an array.
     737        if ( is_object( $function ) ) {
    742738                $function = array( $function, '' );
    743739        } else {
    744740                $function = (array) $function;
    745741        }
     742       
     743        // Shortcut bogus values.
     744        if ( 2 !== count( $function ) || !is_string( $function[1] ) )
     745                return false;
    746746
    747         if (is_object($function[0]) ) {
    748                 // Object Class Calling
    749                 if ( function_exists('spl_object_hash') ) {
    750                         return spl_object_hash($function[0]) . $function[1];
    751                 } else {
    752                         $obj_idx = get_class($function[0]).$function[1];
    753                         if ( !isset($function[0]->wp_filter_id) ) {
    754                                 if ( false === $priority )
    755                                         return false;
    756                                 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
    757                                 $function[0]->wp_filter_id = $filter_id_count;
    758                                 ++$filter_id_count;
    759                         } else {
    760                                 $obj_idx .= $function[0]->wp_filter_id;
    761                         }
     747        // Class and Object Hooks.
     748        if ( is_object( $function[0] ) ) {
     749                // Object Instance Hook.
     750                $object_id = wp_object_hash( $function[0] );
     751                return $object_id   . '->' . $function[1];
     752        } else if ( is_string( $function[0] ) ) {
     753                // Static Class Hook.
     754                return $function[0] . '::' . $function[1];
     755        }
    762756
    763                         return $obj_idx;
    764                 }
    765         } else if ( is_string($function[0]) ) {
    766                 // Static Calling
    767                 return $function[0].$function[1];
     757        return false;
     758}
     759
     760/**
     761 * get object hash
     762 *
     763 * Returns a unique hash per object.
     764 *
     765 * Proxy function for wordpress installments on servers
     766 * with a PHP version < 5.2.0.
     767 *
     768 * @since 3.0.2
     769 * @note Become deprecated with version 3.2.0 (PHP 5.2 requirements)
     770 * @param object $object
     771 * @return string unique object hash
     772 */
     773function wp_object_hash( &$object ) {
     774        static $prefix, $count = 0, $property = '__wphookobjhash__';
     775
     776        if ( !is_object( $object ) ) {
     777                trigger_error( __FUNCTION__ . '() expects parameter 1 to be object', E_USER_WARNING );
     778                return null;
    768779        }
     780
     781        // prefer spl_object_hash if available
     782        if ( function_exists( 'spl_object_hash' ) )
     783                return spl_object_hash( $object );
     784
     785        // create object hash on our own
     786        isset( $prefix ) || ( ( $prefix = uniqid( '' ) ) && $property .= $prefix . '__' );
     787        isset( $object->$property ) || ( $object->$property = sprintf( '%s-%08d', $prefix , ++$count ) );
     788
     789        return $object->$property;
    769790}
    770791
    771792?>