balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

class-wp-widget-factory.php (3321B)


      1 <?php
      2 /**
      3  * Widget API: WP_Widget_Factory class
      4  *
      5  * @package WordPress
      6  * @subpackage Widgets
      7  * @since 4.4.0
      8  */
      9 
     10 /**
     11  * Singleton that registers and instantiates WP_Widget classes.
     12  *
     13  * @since 2.8.0
     14  * @since 4.4.0 Moved to its own file from wp-includes/widgets.php
     15  */
     16 class WP_Widget_Factory {
     17 
     18 	/**
     19 	 * Widgets array.
     20 	 *
     21 	 * @since 2.8.0
     22 	 * @var array
     23 	 */
     24 	public $widgets = array();
     25 
     26 	/**
     27 	 * PHP5 constructor.
     28 	 *
     29 	 * @since 4.3.0
     30 	 */
     31 	public function __construct() {
     32 		add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
     33 	}
     34 
     35 	/**
     36 	 * PHP4 constructor.
     37 	 *
     38 	 * @since 2.8.0
     39 	 * @deprecated 4.3.0 Use __construct() instead.
     40 	 *
     41 	 * @see WP_Widget_Factory::__construct()
     42 	 */
     43 	public function WP_Widget_Factory() {
     44 		_deprecated_constructor( 'WP_Widget_Factory', '4.3.0' );
     45 		self::__construct();
     46 	}
     47 
     48 	/**
     49 	 * Registers a widget subclass.
     50 	 *
     51 	 * @since 2.8.0
     52 	 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
     53 	 *              instead of simply a `WP_Widget` subclass name.
     54 	 *
     55 	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
     56 	 */
     57 	public function register( $widget ) {
     58 		if ( $widget instanceof WP_Widget ) {
     59 			$this->widgets[ spl_object_hash( $widget ) ] = $widget;
     60 		} else {
     61 			$this->widgets[ $widget ] = new $widget();
     62 		}
     63 	}
     64 
     65 	/**
     66 	 * Un-registers a widget subclass.
     67 	 *
     68 	 * @since 2.8.0
     69 	 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
     70 	 *              instead of simply a `WP_Widget` subclass name.
     71 	 *
     72 	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
     73 	 */
     74 	public function unregister( $widget ) {
     75 		if ( $widget instanceof WP_Widget ) {
     76 			unset( $this->widgets[ spl_object_hash( $widget ) ] );
     77 		} else {
     78 			unset( $this->widgets[ $widget ] );
     79 		}
     80 	}
     81 
     82 	/**
     83 	 * Serves as a utility method for adding widgets to the registered widgets global.
     84 	 *
     85 	 * @since 2.8.0
     86 	 *
     87 	 * @global array $wp_registered_widgets
     88 	 */
     89 	public function _register_widgets() {
     90 		global $wp_registered_widgets;
     91 		$keys       = array_keys( $this->widgets );
     92 		$registered = array_keys( $wp_registered_widgets );
     93 		$registered = array_map( '_get_widget_id_base', $registered );
     94 
     95 		foreach ( $keys as $key ) {
     96 			// Don't register new widget if old widget with the same id is already registered.
     97 			if ( in_array( $this->widgets[ $key ]->id_base, $registered, true ) ) {
     98 				unset( $this->widgets[ $key ] );
     99 				continue;
    100 			}
    101 
    102 			$this->widgets[ $key ]->_register();
    103 		}
    104 	}
    105 
    106 	/**
    107 	 * Returns the registered WP_Widget object for the given widget type.
    108 	 *
    109 	 * @since 5.8.0
    110 	 *
    111 	 * @param string $id_base Widget type ID.
    112 	 * @return WP_Widget|null
    113 	 */
    114 	public function get_widget_object( $id_base ) {
    115 		$key = $this->get_widget_key( $id_base );
    116 		if ( '' === $key ) {
    117 			return null;
    118 		}
    119 
    120 		return $this->widgets[ $key ];
    121 	}
    122 
    123 	/**
    124 	 * Returns the registered key for the given widget type.
    125 	 *
    126 	 * @since 5.8.0
    127 	 *
    128 	 * @param string $id_base Widget type ID.
    129 	 * @return string
    130 	 */
    131 	public function get_widget_key( $id_base ) {
    132 		foreach ( $this->widgets as $key => $widget_object ) {
    133 			if ( $widget_object->id_base === $id_base ) {
    134 				return $key;
    135 			}
    136 		}
    137 
    138 		return '';
    139 	}
    140 }