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 | | * |
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 ); |
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 | */ |
| 770 | function _wp_build_unique_closure_id( $function ) { |
| 771 | ob_start(); |
| 772 | echo new ReflectionFunction( $function ); |
| 773 | $source = ob_get_clean(); |
| 774 | return md5( $source ); |
| 775 | } |
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 | */ |
| 788 | function 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 | */ |
| 817 | function _dehash_filter_callback_object( $hash ) { |
| 818 | if ( strstr( $hash, '~' ) ) { |
| 819 | $bits = explode( '~', $hash ); |
| 820 | array_shift( $bits ); |
| 821 | return join( '', $bits ); |