<?php
class WP_Widget {
public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {}
}
class WP_Widget_Text extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'widget_text',
'description' => 'Arbitrary text.',
'customize_selective_refresh' => true,
'show_instance_in_rest' => true,
);
$control_ops = array(
'width' => 400,
'height' => 350,
);
parent::__construct( 'text', 'Text', $widget_ops, $control_ops );
}
}
class Text_Widget extends WP_Widget {
// Notice it does not have a constructor, which is doing it wrong.
}
class WP_Widget_Factory {
public $widgets = array();
public function register( $widget ) {
if ( $widget instanceof WP_Widget ) {
$this->widgets[ spl_object_hash( $widget ) ] = $widget;
} else {
$this->widgets[ $widget ] = new $widget();
}
}
}
$factory = new WP_Widget_Factory();
$factory->register( 'Text_Widget' );
preferences:
25.2 ms | 404 KiB | 5 Q