Geen omschrijving

class-wp-widget-factory.php 3.2KB

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