WP_Widget_Factory

Singleton that registers and instantiates WP_Widget classes.

Defined (1)

The class is defined in the following location(s).

/wp-includes/class-wp-widget-factory.php  
  1. class WP_Widget_Factory { 
  2.  
  3. /** 
  4. * Widgets array. 
  5. * @since 2.8.0 
  6. * @access public 
  7. * @var array 
  8. */ 
  9. public $widgets = array(); 
  10.  
  11. /** 
  12. * PHP5 constructor. 
  13. * @since 4.3.0 
  14. * @access public 
  15. */ 
  16. public function __construct() { 
  17. add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 ); 
  18.  
  19. /** 
  20. * PHP4 constructor. 
  21. * @since 2.8.0 
  22. * @access public 
  23. */ 
  24. public function WP_Widget_Factory() { 
  25. _deprecated_constructor( 'WP_Widget_Factory', '4.2.0' ); 
  26. self::__construct(); 
  27.  
  28. /** 
  29. * Memory for the number of times unique class instances have been hashed. 
  30. * This can be eliminated in favor of straight spl_object_hash() when 5.3 
  31. * is the minimum requirement for PHP. 
  32. * @since 4.6.0 
  33. * @access private 
  34. * @var array 
  35. * @see WP_Widget_Factory::hash_object() 
  36. */ 
  37. private $hashed_class_counts = array(); 
  38.  
  39. /** 
  40. * Hashes an object, doing fallback of `spl_object_hash()` if not available. 
  41. * This can be eliminated in favor of straight spl_object_hash() when 5.3 
  42. * is the minimum requirement for PHP. 
  43. * @since 4.6.0 
  44. * @access private 
  45. * @param WP_Widget $widget Widget. 
  46. * @return string Object hash. 
  47. */ 
  48. private function hash_object( $widget ) { 
  49. if ( function_exists( 'spl_object_hash' ) ) { 
  50. return spl_object_hash( $widget ); 
  51. } else { 
  52. $class_name = get_class( $widget ); 
  53. $hash = $class_name; 
  54. if ( ! isset( $widget->_wp_widget_factory_hash_id ) ) { 
  55. if ( ! isset( $this->hashed_class_counts[ $class_name ] ) ) { 
  56. $this->hashed_class_counts[ $class_name ] = 0; 
  57. $this->hashed_class_counts[ $class_name ] += 1; 
  58. $widget->_wp_widget_factory_hash_id = $this->hashed_class_counts[ $class_name ]; 
  59. $hash .= ':' . $widget->_wp_widget_factory_hash_id; 
  60. return $hash; 
  61.  
  62. /** 
  63. * Registers a widget subclass. 
  64. * @since 2.8.0 
  65. * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object 
  66. * instead of simply a `WP_Widget` subclass name. 
  67. * @access public 
  68. * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. 
  69. */ 
  70. public function register( $widget ) { 
  71. if ( $widget instanceof WP_Widget ) { 
  72. $this->widgets[ $this->hash_object( $widget ) ] = $widget; 
  73. } else { 
  74. $this->widgets[ $widget ] = new $widget(); 
  75.  
  76. /** 
  77. * Un-registers a widget subclass. 
  78. * @since 2.8.0 
  79. * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object 
  80. * instead of simply a `WP_Widget` subclass name. 
  81. * @access public 
  82. * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. 
  83. */ 
  84. public function unregister( $widget ) { 
  85. if ( $widget instanceof WP_Widget ) { 
  86. unset( $this->widgets[ $this->hash_object( $widget ) ] ); 
  87. } else { 
  88. unset( $this->widgets[ $widget ] ); 
  89.  
  90. /** 
  91. * Serves as a utility method for adding widgets to the registered widgets global. 
  92. * @since 2.8.0 
  93. * @access public 
  94. * @global array $wp_registered_widgets 
  95. */ 
  96. public function _register_widgets() { 
  97. global $wp_registered_widgets; 
  98. $keys = array_keys($this->widgets); 
  99. $registered = array_keys($wp_registered_widgets); 
  100. $registered = array_map('_get_widget_id_base', $registered); 
  101.  
  102. foreach ( $keys as $key ) { 
  103. // don't register new widget if old widget with the same id is already registered 
  104. if ( in_array($this->widgets[$key]->id_base, $registered, true) ) { 
  105. unset($this->widgets[$key]); 
  106. continue; 
  107.  
  108. $this->widgets[$key]->_register();