/wp-includes/class-wp-widget-factory.php

  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. * @access public 
  23. * @var array 
  24. */ 
  25. public $widgets = array(); 
  26.  
  27. /** 
  28. * PHP5 constructor. 
  29. * 
  30. * @since 4.3.0 
  31. * @access public 
  32. */ 
  33. public function __construct() { 
  34. add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 ); 
  35.  
  36. /** 
  37. * PHP4 constructor. 
  38. * 
  39. * @since 2.8.0 
  40. * @access public 
  41. */ 
  42. public function WP_Widget_Factory() { 
  43. _deprecated_constructor( 'WP_Widget_Factory', '4.2.0' ); 
  44. self::__construct(); 
  45.  
  46. /** 
  47. * Memory for the number of times unique class instances have been hashed. 
  48. * 
  49. * This can be eliminated in favor of straight spl_object_hash() when 5.3 
  50. * is the minimum requirement for PHP. 
  51. * 
  52. * @since 4.6.0 
  53. * @access private 
  54. * @var array 
  55. * 
  56. * @see WP_Widget_Factory::hash_object() 
  57. */ 
  58. private $hashed_class_counts = array(); 
  59.  
  60. /** 
  61. * Hashes an object, doing fallback of `spl_object_hash()` if not available. 
  62. * 
  63. * This can be eliminated in favor of straight spl_object_hash() when 5.3 
  64. * is the minimum requirement for PHP. 
  65. * 
  66. * @since 4.6.0 
  67. * @access private 
  68. * 
  69. * @param WP_Widget $widget Widget. 
  70. * @return string Object hash. 
  71. */ 
  72. private function hash_object( $widget ) { 
  73. if ( function_exists( 'spl_object_hash' ) ) { 
  74. return spl_object_hash( $widget ); 
  75. } else { 
  76. $class_name = get_class( $widget ); 
  77. $hash = $class_name; 
  78. if ( ! isset( $widget->_wp_widget_factory_hash_id ) ) { 
  79. if ( ! isset( $this->hashed_class_counts[ $class_name ] ) ) { 
  80. $this->hashed_class_counts[ $class_name ] = 0; 
  81. $this->hashed_class_counts[ $class_name ] += 1; 
  82. $widget->_wp_widget_factory_hash_id = $this->hashed_class_counts[ $class_name ]; 
  83. $hash .= ':' . $widget->_wp_widget_factory_hash_id; 
  84. return $hash; 
  85.  
  86. /** 
  87. * Registers a widget subclass. 
  88. * 
  89. * @since 2.8.0 
  90. * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object 
  91. * instead of simply a `WP_Widget` subclass name. 
  92. * @access public 
  93. * 
  94. * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. 
  95. */ 
  96. public function register( $widget ) { 
  97. if ( $widget instanceof WP_Widget ) { 
  98. $this->widgets[ $this->hash_object( $widget ) ] = $widget; 
  99. } else { 
  100. $this->widgets[ $widget ] = new $widget(); 
  101.  
  102. /** 
  103. * Un-registers a widget subclass. 
  104. * 
  105. * @since 2.8.0 
  106. * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object 
  107. * instead of simply a `WP_Widget` subclass name. 
  108. * @access public 
  109. * 
  110. * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass. 
  111. */ 
  112. public function unregister( $widget ) { 
  113. if ( $widget instanceof WP_Widget ) { 
  114. unset( $this->widgets[ $this->hash_object( $widget ) ] ); 
  115. } else { 
  116. unset( $this->widgets[ $widget ] ); 
  117.  
  118. /** 
  119. * Serves as a utility method for adding widgets to the registered widgets global. 
  120. * 
  121. * @since 2.8.0 
  122. * @access public 
  123. * 
  124. * @global array $wp_registered_widgets 
  125. */ 
  126. public function _register_widgets() { 
  127. global $wp_registered_widgets; 
  128. $keys = array_keys($this->widgets); 
  129. $registered = array_keys($wp_registered_widgets); 
  130. $registered = array_map('_get_widget_id_base', $registered); 
  131.  
  132. foreach ( $keys as $key ) { 
  133. // don't register new widget if old widget with the same id is already registered 
  134. if ( in_array($this->widgets[$key]->id_base, $registered, true) ) { 
  135. unset($this->widgets[$key]); 
  136. continue; 
  137.  
  138. $this->widgets[$key]->_register(); 
.