Make WordPress Core

Ticket #10535: 10535.9.patch

File 10535.9.patch, 4.8 KB (added by hakre, 14 years ago)

Updated for PHP 5 and against current trunk, coding standard fixes

  • wp-includes/plugin.php

     
    708708/**
    709709 * Build Unique ID for storage and retrieval.
    710710 *
    711  * The old way to serialize the callback caused issues and this function is the
    712  * solution. It works by checking for objects and creating an a new property in
    713  * the class to keep track of the object and new objects of the same class that
    714  * need to be added.
    715  *
     711 * Every hook callback needs to have a Unique ID to identify it within the
     712 * hook registry, the global array $wp_filter.
     713 *
    716714 * It also allows for the removal of actions and filters for objects after they
    717  * change class properties. It is possible to include the property $wp_filter_id
    718  * in your class and set it to "null" or a number to bypass the workaround.
    719  * However this will prevent you from adding new classes and any new classes
    720  * will overwrite the previous hook by the same class.
     715 * change class properties.
    721716 *
    722717 * Functions and static method callbacks are just returned as strings and
    723718 * shouldn't have any speed penalty.
    724719 *
     720 * PHP 5.3 anonymous functions are supported as well.
     721 *
    725722 * @package WordPress
    726723 * @subpackage Plugin
    727724 * @access private
     
    729726 * @link http://trac.wordpress.org/ticket/3875
    730727 *
    731728 * @global array $wp_filter Storage for all of the filters and actions
    732  * @param string $tag Used in counting how many hooks were applied
    733  * @param callback $function Used for creating unique id
    734  * @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.
    735  * @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.
     729 * @param string $tag Obsolete, kept for backwards compability.
     730 * @param callback $function Callback to create Unique ID of.
     731 * @param int|bool $priority Obsolete, kept for backwards compability.
     732 * @return string|bool Unique ID (string), or false on failure
    736733 */
    737734function _wp_filter_build_unique_id($tag, $function, $priority) {
    738         global $wp_filter;
    739         static $filter_id_count = 0;
    740735
    741         if ( is_string($function) )
     736        // Ensure that this is in a callable syntax
     737        if ( false === is_callable( $function, true, $callable_name ) )
     738                return false;
     739
     740        // Global function hook or anonymous create_function()-hook.
     741        if ( is_string( $function ) )
    742742                return $function;
    743743
    744         if ( is_object($function) ) {
    745                 // Closures are currently implemented as objects
     744        // Closures are currently implemented as objects
     745        if ( is_object( $function ) )
    746746                $function = array( $function, '' );
    747         } else {
    748                 $function = (array) $function;
     747       
     748        // Object Instance / Closure Hook
     749        if ( is_object( $function[0] ) ) {
     750                $object_id = wp_object_hash( $function[0] );
     751                return $object_id . '::' . $function[1];
    749752        }
    750753
    751         if (is_object($function[0]) ) {
    752                 // Object Class Calling
    753                 if ( function_exists('spl_object_hash') ) {
    754                         return spl_object_hash($function[0]) . $function[1];
    755                 } else {
    756                         $obj_idx = get_class($function[0]).$function[1];
    757                         if ( !isset($function[0]->wp_filter_id) ) {
    758                                 if ( false === $priority )
    759                                         return false;
    760                                 $obj_idx .= isset($wp_filter[$tag][$priority]) ? count((array)$wp_filter[$tag][$priority]) : $filter_id_count;
    761                                 $function[0]->wp_filter_id = $filter_id_count;
    762                                 ++$filter_id_count;
    763                         } else {
    764                                 $obj_idx .= $function[0]->wp_filter_id;
    765                         }
     754        // Static Class Hook
     755        return $callable_name;
     756}
    766757
    767                         return $obj_idx;
    768                 }
    769         } else if ( is_string($function[0]) ) {
    770                 // Static Calling
    771                 return $function[0].$function[1];
     758/**
     759 * get object hash
     760 *
     761 * Returns a unique hash per object.
     762 *
     763 * Proxy function for wordpress installments on servers
     764 * with a PHP version < 5.3.0.
     765 *
     766 * @since 3.2
     767 * @param object $object
     768 * @return string unique object hash
     769 */
     770function wp_object_hash( $object ) {
     771        static $prefix, $count = 0, $property = '__wphookobjhash__', $spl_function_exists;
     772
     773        if ( ! isset( $spl_function_exists ) )
     774                $spl_function_exists = function_exists( 'spl_object_hash' );
     775
     776        // prefer spl_object_hash if available
     777        if ( $spl_function_exists )
     778                return spl_object_hash( $object );
     779
     780        // validate input
     781        if ( !is_object( $object ) ) {
     782                trigger_error( __FUNCTION__ . '() expects parameter 1 to be object', E_USER_WARNING );
     783                return null;
    772784        }
     785
     786        // setup prefix and counter to generate object hash, set it to object if not set
     787        if ( ! isset( $prefix ) ) {
     788                $prefix = uniqid();
     789                $property .= $prefix . '__';
     790        }
     791       
     792        if ( ! isset( $object->$property ) )
     793                $object->$property = sprintf( '%s-%08d', $prefix , ++$count );
     794
     795        return $object->$property;
    773796}
    774797
    775798?>