mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-07-08 19:20:03 +00:00
Widgets: Allow WP_Widget subclass instances (objects) to be registered/unregistered in addition to WP_Widget subclass names (strings).
Allows widgets to be registered which rely on dependency injection. Also will allow for new widget types to be created dynamically (e.g. a Recent Posts widget for each registered post type). See #35990. Props mdwheele, PeterRKnight, westonruter. Fixes #28216. git-svn-id: https://develop.svn.wordpress.org/trunk@37329 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -49,24 +49,34 @@ class WP_Widget_Factory {
|
||||
* Registers a widget subclass.
|
||||
*
|
||||
* @since 2.8.0
|
||||
* @since 4.6.0 The `$widget` param can also be an instance object of `WP_Widget` instead of just a `WP_Widget` subclass name.
|
||||
* @access public
|
||||
*
|
||||
* @param string $widget_class The name of a WP_Widget subclass.
|
||||
* @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_class ) {
|
||||
$this->widgets[$widget_class] = new $widget_class();
|
||||
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 The `$widget` param can also be an instance object of `WP_Widget` instead of just a `WP_Widget` subclass name.
|
||||
* @access public
|
||||
*
|
||||
* @param string $widget_class The name of a WP_Widget subclass.
|
||||
* @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_class ) {
|
||||
unset( $this->widgets[ $widget_class ] );
|
||||
public function unregister( $widget ) {
|
||||
if ( $widget instanceof WP_Widget ) {
|
||||
unset( $this->widgets[ spl_object_hash( $widget ) ] );
|
||||
} else {
|
||||
unset( $this->widgets[ $widget ] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user