3
* Widget API: WP_Widget base class
11
* Core base class extended to register widgets.
13
* This class must be extended for each widget and WP_Widget::widget(), WP_Widget::update()
14
* and WP_Widget::form() need to be overridden.
17
* @since 4.4.0 Moved to its own file from wp-includes/widgets.php
22
* Root ID for all widgets of this type.
31
* Name for this widget type.
40
* Option array passed to {@see wp_register_sidebar_widget()}.
46
public $widget_options;
49
* Option array passed to {@see wp_register_widget_control()}.
55
public $control_options;
58
* Unique ID number of the current instance.
64
public $number = false;
67
* Unique ID string of the current instance (id_base-number).
76
* Whether the widget data has been updated.
78
* Set to true when the data is updated after a POST submit - ensures it does
85
public $updated = false;
87
// Member functions that you must over-ride.
90
* Echoes the widget content.
92
* Sub-classes should over-ride this function to generate their widget code.
97
* @param array $args Display arguments including 'before_title', 'after_title',
98
* 'before_widget', and 'after_widget'.
99
* @param array $instance The settings for the particular instance of the widget.
101
public function widget( $args, $instance ) {
102
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
106
* Updates a particular instance of a widget.
108
* This function should check that `$new_instance` is set correctly. The newly-calculated
109
* value of `$instance` should be returned. If false is returned, the instance won't be
115
* @param array $new_instance New settings for this instance as input by the user via
117
* @param array $old_instance Old settings for this instance.
118
* @return array Settings to save or bool false to cancel saving.
120
public function update( $new_instance, $old_instance ) {
121
return $new_instance;
125
* Outputs the settings update form.
130
* @param array $instance Current settings.
131
* @return string Default return is 'noform'.
133
public function form( $instance ) {
134
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
138
// Functions you'll need to call.
146
* @param string $id_base Optional Base ID for the widget, lowercase and unique. If left empty,
147
* a portion of the widget's class name will be used Has to be unique.
148
* @param string $name Name for the widget displayed on the configuration page.
149
* @param array $widget_options Optional. Widget options. See wp_register_sidebar_widget() for information
150
* on accepted arguments. Default empty array.
151
* @param array $control_options Optional. Widget control options. See wp_register_widget_control() for
152
* information on accepted arguments. Default empty array.
154
public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
155
$this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
157
$this->option_name = 'widget_' . $this->id_base;
158
$this->widget_options = wp_parse_args( $widget_options, array('classname' => $this->option_name) );
159
$this->control_options = wp_parse_args( $control_options, array('id_base' => $this->id_base) );
165
* @param string $id_base
166
* @param string $name
167
* @param array $widget_options
168
* @param array $control_options
170
public function WP_Widget( $id_base, $name, $widget_options = array(), $control_options = array() ) {
171
_deprecated_constructor( 'WP_Widget', '4.3.0' );
172
WP_Widget::__construct( $id_base, $name, $widget_options, $control_options );
176
* Constructs name attributes for use in form() fields
178
* This function should be used in form() methods to create name attributes for fields to be saved by update()
181
* @since 4.4.0 Array format field names are now accepted.
183
* @param string $field_name Field name
184
* @return string Name attribute for $field_name
186
public function get_field_name($field_name) {
187
if ( false === $pos = strpos( $field_name, '[' ) ) {
188
return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
190
return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
195
* Constructs id attributes for use in {@see WP_Widget::form()} fields.
197
* This function should be used in form() methods to create id attributes
198
* for fields to be saved by {@see WP_Widget::update()}.
201
* @since 4.4.0 Array format field IDs are now accepted.
204
* @param string $field_name Field name.
205
* @return string ID attribute for `$field_name`.
207
public function get_field_id( $field_name ) {
208
return 'widget-' . $this->id_base . '-' . $this->number . '-' . trim( str_replace( array( '[]', '[', ']' ), array( '', '-', '' ), $field_name ), '-' );
212
* Register all widget instances of this widget class.
217
public function _register() {
218
$settings = $this->get_settings();
221
// When $settings is an array-like object, get an intrinsic array for use with array_keys().
222
if ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) {
223
$settings = $settings->getArrayCopy();
226
if ( is_array( $settings ) ) {
227
foreach ( array_keys( $settings ) as $number ) {
228
if ( is_numeric( $number ) ) {
229
$this->_set( $number );
230
$this->_register_one( $number );
237
// If there are none, we register the widget's existence with a generic template.
239
$this->_register_one();
244
* Set the internal order number for the widget instance.
249
* @param int $number The unique order number of this widget instance compared to other
250
* instances of the same class.
252
public function _set($number) {
253
$this->number = $number;
254
$this->id = $this->id_base . '-' . $number;
260
public function _get_display_callback() {
261
return array($this, 'display_callback');
266
public function _get_update_callback() {
267
return array($this, 'update_callback');
272
public function _get_form_callback() {
273
return array($this, 'form_callback');
277
* Determine whether the current request is inside the Customizer preview.
279
* If true -- the current request is inside the Customizer preview, then
280
* the object cache gets suspended and widgets should check this to decide
281
* whether they should store anything persistently to the object cache,
282
* to transients, or anywhere else.
287
* @global WP_Customize_Manager $wp_customize
289
* @return bool True if within the Customizer preview, false if not.
291
public function is_preview() {
292
global $wp_customize;
293
return ( isset( $wp_customize ) && $wp_customize->is_preview() ) ;
297
* Generate the actual widget content (Do NOT override).
299
* Finds the instance and calls {@see WP_Widget::widget()}.
304
* @param array $args Display arguments. See {@see WP_Widget::widget()} for information
305
* on accepted arguments.
306
* @param int|array $widget_args {
307
* Optional. Internal order number of the widget instance, or array of multi-widget arguments.
310
* @type int $number Number increment used for multiples of the same widget.
313
public function display_callback( $args, $widget_args = 1 ) {
314
if ( is_numeric( $widget_args ) ) {
315
$widget_args = array( 'number' => $widget_args );
318
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
319
$this->_set( $widget_args['number'] );
320
$instances = $this->get_settings();
322
if ( array_key_exists( $this->number, $instances ) ) {
323
$instance = $instances[ $this->number ];
326
* Filter the settings for a particular widget instance.
328
* Returning false will effectively short-circuit display of the widget.
332
* @param array $instance The current widget instance's settings.
333
* @param WP_Widget $this The current widget instance.
334
* @param array $args An array of default widget arguments.
336
$instance = apply_filters( 'widget_display_callback', $instance, $this, $args );
338
if ( false === $instance ) {
342
$was_cache_addition_suspended = wp_suspend_cache_addition();
343
if ( $this->is_preview() && ! $was_cache_addition_suspended ) {
344
wp_suspend_cache_addition( true );
347
$this->widget( $args, $instance );
349
if ( $this->is_preview() ) {
350
wp_suspend_cache_addition( $was_cache_addition_suspended );
356
* Deal with changed settings (Do NOT override).
361
* @global array $wp_registered_widgets
363
* @param int $deprecated Not used.
365
public function update_callback( $deprecated = 1 ) {
366
global $wp_registered_widgets;
368
$all_instances = $this->get_settings();
370
// We need to update the data
371
if ( $this->updated )
374
if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
375
// Delete the settings for this instance of the widget
376
if ( isset($_POST['the-widget-id']) )
377
$del_id = $_POST['the-widget-id'];
381
if ( isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
382
$number = $wp_registered_widgets[$del_id]['params'][0]['number'];
384
if ( $this->id_base . '-' . $number == $del_id )
385
unset($all_instances[$number]);
388
if ( isset($_POST['widget-' . $this->id_base]) && is_array($_POST['widget-' . $this->id_base]) ) {
389
$settings = $_POST['widget-' . $this->id_base];
390
} elseif ( isset($_POST['id_base']) && $_POST['id_base'] == $this->id_base ) {
391
$num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
392
$settings = array( $num => array() );
397
foreach ( $settings as $number => $new_instance ) {
398
$new_instance = stripslashes_deep($new_instance);
399
$this->_set($number);
401
$old_instance = isset($all_instances[$number]) ? $all_instances[$number] : array();
403
$was_cache_addition_suspended = wp_suspend_cache_addition();
404
if ( $this->is_preview() && ! $was_cache_addition_suspended ) {
405
wp_suspend_cache_addition( true );
408
$instance = $this->update( $new_instance, $old_instance );
410
if ( $this->is_preview() ) {
411
wp_suspend_cache_addition( $was_cache_addition_suspended );
415
* Filter a widget's settings before saving.
417
* Returning false will effectively short-circuit the widget's ability
418
* to update settings.
422
* @param array $instance The current widget instance's settings.
423
* @param array $new_instance Array of new widget settings.
424
* @param array $old_instance Array of old widget settings.
425
* @param WP_Widget $this The current widget instance.
427
$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
428
if ( false !== $instance ) {
429
$all_instances[$number] = $instance;
432
break; // run only once
436
$this->save_settings($all_instances);
437
$this->updated = true;
441
* Generate the widget control form (Do NOT override).
446
* @param int|array $widget_args Widget instance number or array of widget arguments.
447
* @return string|null
449
public function form_callback( $widget_args = 1 ) {
450
if ( is_numeric($widget_args) )
451
$widget_args = array( 'number' => $widget_args );
453
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
454
$all_instances = $this->get_settings();
456
if ( -1 == $widget_args['number'] ) {
457
// We echo out a form where 'number' can be set later
458
$this->_set('__i__');
461
$this->_set($widget_args['number']);
462
$instance = $all_instances[ $widget_args['number'] ];
466
* Filter the widget instance's settings before displaying the control form.
468
* Returning false effectively short-circuits display of the control form.
472
* @param array $instance The current widget instance's settings.
473
* @param WP_Widget $this The current widget instance.
475
$instance = apply_filters( 'widget_form_callback', $instance, $this );
478
if ( false !== $instance ) {
479
$return = $this->form($instance);
482
* Fires at the end of the widget control form.
484
* Use this hook to add extra fields to the widget form. The hook
485
* is only fired if the value passed to the 'widget_form_callback'
488
* Note: If the widget has no form, the text echoed from the default
489
* form method can be hidden using CSS.
493
* @param WP_Widget $this The widget instance, passed by reference.
494
* @param null $return Return null if new fields are added.
495
* @param array $instance An array of the widget's settings.
497
do_action_ref_array( 'in_widget_form', array( &$this, &$return, $instance ) );
503
* Register an instance of the widget class.
508
* @param integer $number Optional. The unique order number of this widget instance
509
* compared to other instances of the same class. Default -1.
511
public function _register_one( $number = -1 ) {
512
wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
513
_register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
514
_register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
518
* Save the settings for all instances of the widget class.
523
* @param array $settings Multi-dimensional array of widget instance settings.
525
public function save_settings( $settings ) {
526
$settings['_multiwidget'] = 1;
527
update_option( $this->option_name, $settings );
531
* Get the settings for all instances of the widget class.
536
* @return array Multi-dimensional array of widget instance settings.
538
public function get_settings() {
540
$settings = get_option( $this->option_name );
542
if ( false === $settings ) {
543
if ( isset( $this->alt_option_name ) ) {
544
$settings = get_option( $this->alt_option_name );
546
// Save an option so it can be autoloaded next time.
547
$this->save_settings( array() );
551
if ( ! is_array( $settings ) && ! ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) ) {
555
if ( ! empty( $settings ) && ! isset( $settings['_multiwidget'] ) ) {
556
// Old format, convert if single widget.
557
$settings = wp_convert_widget_settings( $this->id_base, $this->option_name, $settings );
560
unset( $settings['_multiwidget'], $settings['__i__'] );