class-wp-widget-factory.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. */
  36. public function WP_Widget_Factory() {
  37. _deprecated_constructor( 'WP_Widget_Factory', '4.2.0' );
  38. self::__construct();
  39. }
  40. /**
  41. * Memory for the number of times unique class instances have been hashed.
  42. *
  43. * This can be eliminated in favor of straight spl_object_hash() when 5.3
  44. * is the minimum requirement for PHP.
  45. *
  46. * @since 4.6.0
  47. * @var array
  48. *
  49. * @see WP_Widget_Factory::hash_object()
  50. */
  51. private $hashed_class_counts = array();
  52. /**
  53. * Registers a widget subclass.
  54. *
  55. * @since 2.8.0
  56. * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
  57. * instead of simply a `WP_Widget` subclass name.
  58. *
  59. * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
  60. */
  61. public function register( $widget ) {
  62. if ( $widget instanceof WP_Widget ) {
  63. $this->widgets[ spl_object_hash( $widget ) ] = $widget;
  64. } else {
  65. $this->widgets[ $widget ] = new $widget();
  66. }
  67. }
  68. /**
  69. * Un-registers a widget subclass.
  70. *
  71. * @since 2.8.0
  72. * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
  73. * instead of simply a `WP_Widget` subclass name.
  74. *
  75. * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
  76. */
  77. public function unregister( $widget ) {
  78. if ( $widget instanceof WP_Widget ) {
  79. unset( $this->widgets[ spl_object_hash( $widget ) ] );
  80. } else {
  81. unset( $this->widgets[ $widget ] );
  82. }
  83. }
  84. /**
  85. * Serves as a utility method for adding widgets to the registered widgets global.
  86. *
  87. * @since 2.8.0
  88. *
  89. * @global array $wp_registered_widgets
  90. */
  91. public function _register_widgets() {
  92. global $wp_registered_widgets;
  93. $keys = array_keys( $this->widgets );
  94. $registered = array_keys( $wp_registered_widgets );
  95. $registered = array_map( '_get_widget_id_base', $registered );
  96. foreach ( $keys as $key ) {
  97. // don't register new widget if old widget with the same id is already registered
  98. if ( in_array( $this->widgets[ $key ]->id_base, $registered, true ) ) {
  99. unset( $this->widgets[ $key ] );
  100. continue;
  101. }
  102. $this->widgets[ $key ]->_register();
  103. }
  104. }
  105. }