class-wp-widget-factory.php 3.3 KB

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