110
110
* @param array $instance The settings for the particular instance of the widget.
112
112
public function widget( $args, $instance ) {
113
die('function WP_Widget::widget() must be over-ridden in a sub-class.');
113
die( 'function WP_Widget::widget() must be over-ridden in a sub-class.' );
140
140
* @return string Default return is 'noform'.
142
142
public function form( $instance ) {
143
echo '<p class="no-options-widget">' . __('There are no options for this widget.') . '</p>';
143
echo '<p class="no-options-widget">' . __( 'There are no options for this widget.' ) . '</p>';
160
160
* information on accepted arguments. Default empty array.
162
162
public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
163
$this->id_base = empty($id_base) ? preg_replace( '/(wp_)?widget_/', '', strtolower(get_class($this)) ) : strtolower($id_base);
165
$this->option_name = 'widget_' . $this->id_base;
166
$this->widget_options = wp_parse_args( $widget_options, array( 'classname' => $this->option_name, 'customize_selective_refresh' => false ) );
163
$this->id_base = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ) : strtolower( $id_base );
165
$this->option_name = 'widget_' . $this->id_base;
166
$this->widget_options = wp_parse_args(
169
'classname' => $this->option_name,
170
'customize_selective_refresh' => false,
167
173
$this->control_options = wp_parse_args( $control_options, array( 'id_base' => $this->id_base ) );
199
205
* @param string $field_name Field name
200
206
* @return string Name attribute for $field_name
202
public function get_field_name($field_name) {
208
public function get_field_name( $field_name ) {
203
209
if ( false === $pos = strpos( $field_name, '[' ) ) {
204
210
return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
231
237
public function _register() {
232
238
$settings = $this->get_settings();
235
241
// When $settings is an array-like object, get an intrinsic array for use with array_keys().
236
242
if ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) {
262
268
* @param int $number The unique order number of this widget instance compared to other
263
269
* instances of the same class.
265
public function _set($number) {
271
public function _set( $number ) {
266
272
$this->number = $number;
267
$this->id = $this->id_base . '-' . $number;
273
$this->id = $this->id_base . '-' . $number;
275
281
* @return callable Display callback.
277
283
public function _get_display_callback() {
278
return array($this, 'display_callback');
284
return array( $this, 'display_callback' );
286
292
* @return callable Update callback.
288
294
public function _get_update_callback() {
289
return array($this, 'update_callback');
295
return array( $this, 'update_callback' );
297
303
* @return callable Form callback.
299
305
public function _get_form_callback() {
300
return array($this, 'form_callback');
306
return array( $this, 'form_callback' );
392
398
$all_instances = $this->get_settings();
394
400
// We need to update the data
395
if ( $this->updated )
401
if ( $this->updated ) {
398
if ( isset($_POST['delete_widget']) && $_POST['delete_widget'] ) {
405
if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {
399
406
// Delete the settings for this instance of the widget
400
if ( isset($_POST['the-widget-id']) )
407
if ( isset( $_POST['the-widget-id'] ) ) {
401
408
$del_id = $_POST['the-widget-id'];
405
if ( isset($wp_registered_widgets[$del_id]['params'][0]['number']) ) {
406
$number = $wp_registered_widgets[$del_id]['params'][0]['number'];
408
if ( $this->id_base . '-' . $number == $del_id )
409
unset($all_instances[$number]);
413
if ( isset( $wp_registered_widgets[ $del_id ]['params'][0]['number'] ) ) {
414
$number = $wp_registered_widgets[ $del_id ]['params'][0]['number'];
416
if ( $this->id_base . '-' . $number == $del_id ) {
417
unset( $all_instances[ $number ] );
412
if ( isset($_POST['widget-' . $this->id_base]) && is_array($_POST['widget-' . $this->id_base]) ) {
413
$settings = $_POST['widget-' . $this->id_base];
414
} elseif ( isset($_POST['id_base']) && $_POST['id_base'] == $this->id_base ) {
415
$num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
421
if ( isset( $_POST[ 'widget-' . $this->id_base ] ) && is_array( $_POST[ 'widget-' . $this->id_base ] ) ) {
422
$settings = $_POST[ 'widget-' . $this->id_base ];
423
} elseif ( isset( $_POST['id_base'] ) && $_POST['id_base'] == $this->id_base ) {
424
$num = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
416
425
$settings = array( $num => array() );
421
430
foreach ( $settings as $number => $new_instance ) {
422
$new_instance = stripslashes_deep($new_instance);
423
$this->_set($number);
431
$new_instance = stripslashes_deep( $new_instance );
432
$this->_set( $number );
425
$old_instance = isset($all_instances[$number]) ? $all_instances[$number] : array();
434
$old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array();
427
436
$was_cache_addition_suspended = wp_suspend_cache_addition();
428
437
if ( $this->is_preview() && ! $was_cache_addition_suspended ) {
451
460
$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
452
461
if ( false !== $instance ) {
453
$all_instances[$number] = $instance;
462
$all_instances[ $number ] = $instance;
456
465
break; // run only once
460
$this->save_settings($all_instances);
469
$this->save_settings( $all_instances );
461
470
$this->updated = true;
475
484
* @return string|null
477
486
public function form_callback( $widget_args = 1 ) {
478
if ( is_numeric($widget_args) )
487
if ( is_numeric( $widget_args ) ) {
479
488
$widget_args = array( 'number' => $widget_args );
481
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
491
$widget_args = wp_parse_args( $widget_args, array( 'number' => -1 ) );
482
492
$all_instances = $this->get_settings();
484
494
if ( -1 == $widget_args['number'] ) {
485
495
// We echo out a form where 'number' can be set later
486
$this->_set('__i__');
496
$this->_set( '__i__' );
487
497
$instance = array();
489
$this->_set($widget_args['number']);
499
$this->_set( $widget_args['number'] );
490
500
$instance = $all_instances[ $widget_args['number'] ];
536
546
* compared to other instances of the same class. Default -1.
538
548
public function _register_one( $number = -1 ) {
539
wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
549
wp_register_sidebar_widget( $this->id, $this->name, $this->_get_display_callback(), $this->widget_options, array( 'number' => $number ) );
540
550
_register_widget_update_callback( $this->id_base, $this->_get_update_callback(), $this->control_options, array( 'number' => -1 ) );
541
_register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );
551
_register_widget_form_callback( $this->id, $this->name, $this->_get_form_callback(), $this->control_options, array( 'number' => $number ) );