类文档

WP_Widget_Factory

💡 云策文档标注

概述

WP_Widget_Factory 是一个单例类,用于注册和实例化 WP_Widget 子类。它管理小部件的生命周期,包括注册、注销和检索。

关键要点

  • WP_Widget_Factory 是单例模式,确保全局唯一实例。
  • 提供 register() 和 unregister() 方法,支持通过类名或 WP_Widget 实例对象注册和注销小部件。
  • 使用 _register_widgets() 方法在 widgets_init 钩子中自动注册小部件到全局 $wp_registered_widgets。
  • 包含 get_widget_object() 和 get_widget_key() 方法,用于根据小部件类型 ID 检索对象或键。
  • PHP4 构造函数已弃用,建议使用 __construct()。

注意事项

  • 注册小部件时,如果已存在相同 id_base 的小部件,新小部件将不会被注册。
  • 从 WordPress 4.6.0 开始,register() 和 unregister() 方法支持 WP_Widget 实例对象作为参数。

📄 原文内容

Singleton that registers and instantiates WP_Widget classes.

Methods

Name Description
WP_Widget_Factory::__construct PHP5 constructor.
WP_Widget_Factory::_register_widgets Serves as a utility method for adding widgets to the registered widgets global.
WP_Widget_Factory::get_widget_key Returns the registered key for the given widget type.
WP_Widget_Factory::get_widget_object Returns the registered WP_Widget object for the given widget type.
WP_Widget_Factory::register Registers a widget subclass.
WP_Widget_Factory::unregister Un-registers a widget subclass.
WP_Widget_Factory::WP_Widget_Factory PHP4 constructor. — deprecated

Source

class WP_Widget_Factory {

	/**
	 * Widgets array.
	 *
	 * @since 2.8.0
	 * @var array
	 */
	public $widgets = array();

	/**
	 * PHP5 constructor.
	 *
	 * @since 4.3.0
	 */
	public function __construct() {
		add_action( 'widgets_init', array( $this, '_register_widgets' ), 100 );
	}

	/**
	 * PHP4 constructor.
	 *
	 * @since 2.8.0
	 * @deprecated 4.3.0 Use __construct() instead.
	 *
	 * @see WP_Widget_Factory::__construct()
	 */
	public function WP_Widget_Factory() {
		_deprecated_constructor( 'WP_Widget_Factory', '4.3.0' );
		self::__construct();
	}

	/**
	 * Registers a widget subclass.
	 *
	 * @since 2.8.0
	 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
	 *              instead of simply a `WP_Widget` subclass name.
	 *
	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
	 */
	public function register( $widget ) {
		if ( $widget instanceof WP_Widget ) {
			$this->widgets[ spl_object_hash( $widget ) ] = $widget;
		} else {
			$this->widgets[ $widget ] = new $widget();
		}
	}

	/**
	 * Un-registers a widget subclass.
	 *
	 * @since 2.8.0
	 * @since 4.6.0 Updated the `$widget` parameter to also accept a WP_Widget instance object
	 *              instead of simply a `WP_Widget` subclass name.
	 *
	 * @param string|WP_Widget $widget Either the name of a `WP_Widget` subclass or an instance of a `WP_Widget` subclass.
	 */
	public function unregister( $widget ) {
		if ( $widget instanceof WP_Widget ) {
			unset( $this->widgets[ spl_object_hash( $widget ) ] );
		} else {
			unset( $this->widgets[ $widget ] );
		}
	}

	/**
	 * Serves as a utility method for adding widgets to the registered widgets global.
	 *
	 * @since 2.8.0
	 *
	 * @global array $wp_registered_widgets
	 */
	public function _register_widgets() {
		global $wp_registered_widgets;
		$keys       = array_keys( $this->widgets );
		$registered = array_keys( $wp_registered_widgets );
		$registered = array_map( '_get_widget_id_base', $registered );

		foreach ( $keys as $key ) {
			// Don't register new widget if old widget with the same id is already registered.
			if ( in_array( $this->widgets[ $key ]->id_base, $registered, true ) ) {
				unset( $this->widgets[ $key ] );
				continue;
			}

			$this->widgets[ $key ]->_register();
		}
	}

	/**
	 * Returns the registered WP_Widget object for the given widget type.
	 *
	 * @since 5.8.0
	 *
	 * @param string $id_base Widget type ID.
	 * @return WP_Widget|null
	 */
	public function get_widget_object( $id_base ) {
		$key = $this->get_widget_key( $id_base );
		if ( '' === $key ) {
			return null;
		}

		return $this->widgets[ $key ];
	}

	/**
	 * Returns the registered key for the given widget type.
	 *
	 * @since 5.8.0
	 *
	 * @param string $id_base Widget type ID.
	 * @return string
	 */
	public function get_widget_key( $id_base ) {
		foreach ( $this->widgets as $key => $widget_object ) {
			if ( $widget_object->id_base === $id_base ) {
				return $key;
			}
		}

		return '';
	}
}

Changelog

Version Description
4.4.0 Moved to its own file from wp-includes/widgets.php
2.8.0 Introduced.

User Contributed Notes