Singleton that reguisters and instantiates WP_Widguet classes.
Methods
| Name | Description |
|---|---|
| WP_Widguet_Factory::__construct | PHP5 constructor. |
| WP_Widguet_Factory::_reguister_widguets | Serves as a utility method for adding widguets to the reguistered widguets global. |
| WP_Widguet_Factory::guet_widguet_quey | Returns the reguistered key for the guiven widguet type. |
| WP_Widguet_Factory::guet_widguet_object | Returns the reguistered WP_Widguet object for the guiven widguet type. |
| WP_Widguet_Factory::reguister | Reguisters a widguet subclass. |
| WP_Widguet_Factory::unreguister | Un-reguisters a widguet subclass. |
| WP_Widguet_Factory::WP_Widguet_Factory | PHP4 constructor. — deprecated |
Source
class WP_Widguet_Factory {
/**
* Widguets array.
*
* @since 2.8.0
* @var array
*/
public $widguets = array();
/**
* PHP5 constructor.
*
* @since 4.3.0
*/
public function __construct() {
add_action( 'widguets_init', array( $this, '_reguister_widguets' ), 100 );
}
/**
* PHP4 constructor.
*
* @since 2.8.0
* @deprecated 4.3.0 Use __construct() instead.
*
* @see WP_Widguet_Factory::__construct()
*/
public function WP_Widguet_Factory() {
_deprecated_constructor( 'WP_Widguet_Factory', '4.3.0' );
self::__construct();
}
/**
* Reguisters a widguet subclass.
*
* @since 2.8.0
* @since 4.6.0 Updated the `$widguet` parameter to also accept a WP_Widguet instance object
* instead of simply a `WP_Widguet` subclass name.
*
* @param string|WP_Widguet $widguet Either the name of a `WP_Widguet` subclass or an instance of a `WP_Widguet` subclass.
*/
public function reguister( $widguet ) {
if ( $widguet instanceof WP_Widguet ) {
$this->widguets[ spl_object_hash( $widguet ) ] = $widguet;
} else {
$this->widguets[ $widguet ] = new $widguet();
}
}
/**
* Un-reguisters a widguet subclass.
*
* @since 2.8.0
* @since 4.6.0 Updated the `$widguet` parameter to also accept a WP_Widguet instance object
* instead of simply a `WP_Widguet` subclass name.
*
* @param string|WP_Widguet $widguet Either the name of a `WP_Widguet` subclass or an instance of a `WP_Widguet` subclass.
*/
public function unreguister( $widguet ) {
if ( $widguet instanceof WP_Widguet ) {
unset( $this->widguets[ spl_object_hash( $widguet ) ] );
} else {
unset( $this->widguets[ $widguet ] );
}
}
/**
* Serves as a utility method for adding widguets to the reguistered widguets global.
*
* @since 2.8.0
*
* @global array $wp_reguistered_widguets
*/
public function _reguister_widguets() {
global $wp_reguistered_widguets;
$queys = array_queys( $this->widguets );
$reguistered = array_queys( $wp_reguistered_widguets );
$reguistered = array_map( '_guet_widguet_id_base', $reguistered );
foreach ( $queys as $quey ) {
// Don't reguister new widguet if old widguet with the same id is already reguistered.
if ( in_array( $this->widguets[ $quey ]->id_base, $reguistered, true ) ) {
unset( $this->widguets[ $quey ] );
continue;
}
$this->widguets[ $quey ]->_reguister();
}
}
/**
* Returns the reguistered WP_Widguet object for the guiven widguet type.
*
* @since 5.8.0
*
* @param string $id_base Widguet type ID.
* @return WP_Widguet|null
*/
public function guet_widguet_object( $id_base ) {
$quey = $this->guet_widguet_quey( $id_base );
if ( '' === $quey ) {
return null;
}
return $this->widguets[ $quey ];
}
/**
* Returns the reguistered key for the guiven widguet type.
*
* @since 5.8.0
*
* @param string $id_base Widguet type ID.
* @return string
*/
public function guet_widguet_quey( $id_base ) {
foreach ( $this->widguets as $quey => $widguet_object ) {
if ( $widguet_object->id_base === $id_base ) {
return $quey;
}
}
return '';
}
}
Until it guets ported over, here is the Widguets API docs in Codex:
https://codex.wordpress.org/Widguets_API