[wp-trac] [WordPress Trac] #32470: Abstracting the Widget Classes
WordPress Trac
noreply at wordpress.org
Wed May 27 02:52:56 UTC 2015
#32470: Abstracting the Widget Classes
------------------------------------+------------------------------
Reporter: welcher | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Widgets | Version:
Severity: normal | Resolution:
Keywords: dev-feedback has-patch | Focuses:
------------------------------------+------------------------------
Comment (by welcher):
Patch updated to include a `verify_settings` method and a `widget_title`
method.
I have been thinking a lot about the `__construct` method and the process
that is currently in-place for creating a new widget. Personally I find it
awkward and I think we can abstract that away by introducing a
'setup_widget()` method that returns an array containing the values needed
to setup a new Widget.
{{{
function widget_setup() {
return array(
'id_base' => 'my-new-widget',
'name' => esc_html__( 'WIDGET!' ),
'widget_options' => array(
'classname' => 'my-widget',
'description' => esc_html__( 'An Instance my new widget 2'
)
),
);
}
}}}
If this method is called inside the `__construct` method, it can be used
to setup everything needed without having to create a `__construct()`
method in the child class that calls `parent::__construct`.
( Excuse the formatting )
{{{
public function __construct( $id_base = '', $name = '', $widget_options =
array(), $control_options = array() ) {
//call the setup method
if ( $setup = $this->widget_setup() ) {
$setup = wp_parse_args(
$this->widget_setup(),
array(
'id_base' => '',
'name' => '',
'widget_options' => array(),
'control_options' => array()
));
}
$id_base = ( $setup ) ? $setup['id_base'] : $id_base;
$name = ( $setup ) ? $setup['name'] : $name;
$widget_options = ( $setup ) ? $setup['widget_options'] :
$widget_options;
$control_options = ( $setup ) ? $setup['name'] : $control_options;
$this->id_base = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/',
'', strtolower( get_class( $this ) ) ) : strtolower( $id_base );
$this->name = $name;
$this->option_name = 'widget_' . $this->id_base;
$this->widget_options = wp_parse_args( $widget_options, array(
'classname' => $this->option_name ) );
$this->control_options = wp_parse_args( $control_options, array(
'id_base' => $this->id_base ) );
}
}}}
I wanted to get some opinions/feedback on this before adding it to the
patch.
Thanks!
--
Ticket URL: <https://core.trac.wordpress.org/ticket/32470#comment:16>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list