wp/wp-includes/class-wp-widget-factory.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    34 
    34 
    35 	/**
    35 	/**
    36 	 * PHP4 constructor.
    36 	 * PHP4 constructor.
    37 	 *
    37 	 *
    38 	 * @since 2.8.0
    38 	 * @since 2.8.0
       
    39 	 * @deprecated 4.3.0 Use __construct() instead.
       
    40 	 *
       
    41 	 * @see WP_Widget_Factory::__construct()
    39 	 */
    42 	 */
    40 	public function WP_Widget_Factory() {
    43 	public function WP_Widget_Factory() {
    41 		_deprecated_constructor( 'WP_Widget_Factory', '4.2.0' );
    44 		_deprecated_constructor( 'WP_Widget_Factory', '4.3.0' );
    42 		self::__construct();
    45 		self::__construct();
    43 	}
       
    44 
       
    45 	/**
       
    46 	 * Memory for the number of times unique class instances have been hashed.
       
    47 	 *
       
    48 	 * This can be eliminated in favor of straight spl_object_hash() when 5.3
       
    49 	 * is the minimum requirement for PHP.
       
    50 	 *
       
    51 	 * @since 4.6.0
       
    52 	 * @var array
       
    53 	 *
       
    54 	 * @see WP_Widget_Factory::hash_object()
       
    55 	 */
       
    56 	private $hashed_class_counts = array();
       
    57 
       
    58 	/**
       
    59 	 * Hashes an object, doing fallback of `spl_object_hash()` if not available.
       
    60 	 *
       
    61 	 * This can be eliminated in favor of straight spl_object_hash() when 5.3
       
    62 	 * is the minimum requirement for PHP.
       
    63 	 *
       
    64 	 * @since 4.6.0
       
    65 	 *
       
    66 	 * @param WP_Widget $widget Widget.
       
    67 	 * @return string Object hash.
       
    68 	 */
       
    69 	private function hash_object( $widget ) {
       
    70 		if ( function_exists( 'spl_object_hash' ) ) {
       
    71 			return spl_object_hash( $widget );
       
    72 		} else {
       
    73 			$class_name = get_class( $widget );
       
    74 			$hash       = $class_name;
       
    75 			if ( ! isset( $widget->_wp_widget_factory_hash_id ) ) {
       
    76 				if ( ! isset( $this->hashed_class_counts[ $class_name ] ) ) {
       
    77 					$this->hashed_class_counts[ $class_name ] = 0;
       
    78 				}
       
    79 				$this->hashed_class_counts[ $class_name ] += 1;
       
    80 				$widget->_wp_widget_factory_hash_id        = $this->hashed_class_counts[ $class_name ];
       
    81 			}
       
    82 			$hash .= ':' . $widget->_wp_widget_factory_hash_id;
       
    83 			return $hash;
       
    84 		}
       
    85 	}
    46 	}
    86 
    47 
    87 	/**
    48 	/**
    88 	 * Registers a widget subclass.
    49 	 * Registers a widget subclass.
    89 	 *
    50 	 *
    93 	 *
    54 	 *
    94 	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    55 	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    95 	 */
    56 	 */
    96 	public function register( $widget ) {
    57 	public function register( $widget ) {
    97 		if ( $widget instanceof WP_Widget ) {
    58 		if ( $widget instanceof WP_Widget ) {
    98 			$this->widgets[ $this->hash_object( $widget ) ] = $widget;
    59 			$this->widgets[ spl_object_hash( $widget ) ] = $widget;
    99 		} else {
    60 		} else {
   100 			$this->widgets[ $widget ] = new $widget();
    61 			$this->widgets[ $widget ] = new $widget();
   101 		}
    62 		}
   102 	}
    63 	}
   103 
    64 
   110 	 *
    71 	 *
   111 	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
    72 	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
   112 	 */
    73 	 */
   113 	public function unregister( $widget ) {
    74 	public function unregister( $widget ) {
   114 		if ( $widget instanceof WP_Widget ) {
    75 		if ( $widget instanceof WP_Widget ) {
   115 			unset( $this->widgets[ $this->hash_object( $widget ) ] );
    76 			unset( $this->widgets[ spl_object_hash( $widget ) ] );
   116 		} else {
    77 		} else {
   117 			unset( $this->widgets[ $widget ] );
    78 			unset( $this->widgets[ $widget ] );
   118 		}
    79 		}
   119 	}
    80 	}
   120 
    81 
   130 		$keys       = array_keys( $this->widgets );
    91 		$keys       = array_keys( $this->widgets );
   131 		$registered = array_keys( $wp_registered_widgets );
    92 		$registered = array_keys( $wp_registered_widgets );
   132 		$registered = array_map( '_get_widget_id_base', $registered );
    93 		$registered = array_map( '_get_widget_id_base', $registered );
   133 
    94 
   134 		foreach ( $keys as $key ) {
    95 		foreach ( $keys as $key ) {
   135 			// don't register new widget if old widget with the same id is already registered
    96 			// Don't register new widget if old widget with the same id is already registered.
   136 			if ( in_array( $this->widgets[ $key ]->id_base, $registered, true ) ) {
    97 			if ( in_array( $this->widgets[ $key ]->id_base, $registered, true ) ) {
   137 				unset( $this->widgets[ $key ] );
    98 				unset( $this->widgets[ $key ] );
   138 				continue;
    99 				continue;
   139 			}
   100 			}
   140 
   101