wp/wp-includes/class-wp-widget.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    15  * If adding widget options, WP_Widget::update() and WP_Widget::form() should also be overridden.
    15  * If adding widget options, WP_Widget::update() and WP_Widget::form() should also be overridden.
    16  *
    16  *
    17  * @since 2.8.0
    17  * @since 2.8.0
    18  * @since 4.4.0 Moved to its own file from wp-includes/widgets.php
    18  * @since 4.4.0 Moved to its own file from wp-includes/widgets.php
    19  */
    19  */
       
    20 #[AllowDynamicProperties]
    20 class WP_Widget {
    21 class WP_Widget {
    21 
    22 
    22 	/**
    23 	/**
    23 	 * Root ID for all widgets of this type.
    24 	 * Root ID for all widgets of this type.
    24 	 *
    25 	 *
   149 	/**
   150 	/**
   150 	 * PHP5 constructor.
   151 	 * PHP5 constructor.
   151 	 *
   152 	 *
   152 	 * @since 2.8.0
   153 	 * @since 2.8.0
   153 	 *
   154 	 *
   154 	 * @param string $id_base         Optional. Base ID for the widget, lowercase and unique. If left empty,
   155 	 * @param string $id_base         Base ID for the widget, lowercase and unique. If left empty,
   155 	 *                                a portion of the widget's PHP class name will be used. Has to be unique.
   156 	 *                                a portion of the widget's PHP class name will be used. Has to be unique.
   156 	 * @param string $name            Name for the widget displayed on the configuration page.
   157 	 * @param string $name            Name for the widget displayed on the configuration page.
   157 	 * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
   158 	 * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
   158 	 *                                information on accepted arguments. Default empty array.
   159 	 *                                information on accepted arguments. Default empty array.
   159 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   160 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   185 	 * @since 2.8.0
   186 	 * @since 2.8.0
   186 	 * @deprecated 4.3.0 Use __construct() instead.
   187 	 * @deprecated 4.3.0 Use __construct() instead.
   187 	 *
   188 	 *
   188 	 * @see WP_Widget::__construct()
   189 	 * @see WP_Widget::__construct()
   189 	 *
   190 	 *
   190 	 * @param string $id_base         Optional. Base ID for the widget, lowercase and unique. If left empty,
   191 	 * @param string $id_base         Base ID for the widget, lowercase and unique. If left empty,
   191 	 *                                a portion of the widget's PHP class name will be used. Has to be unique.
   192 	 *                                a portion of the widget's PHP class name will be used. Has to be unique.
   192 	 * @param string $name            Name for the widget displayed on the configuration page.
   193 	 * @param string $name            Name for the widget displayed on the configuration page.
   193 	 * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
   194 	 * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
   194 	 *                                information on accepted arguments. Default empty array.
   195 	 *                                information on accepted arguments. Default empty array.
   195 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   196 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   426 			}
   427 			}
   427 
   428 
   428 			if ( isset( $wp_registered_widgets[ $del_id ]['params'][0]['number'] ) ) {
   429 			if ( isset( $wp_registered_widgets[ $del_id ]['params'][0]['number'] ) ) {
   429 				$number = $wp_registered_widgets[ $del_id ]['params'][0]['number'];
   430 				$number = $wp_registered_widgets[ $del_id ]['params'][0]['number'];
   430 
   431 
   431 				if ( $this->id_base . '-' . $number == $del_id ) {
   432 				if ( $this->id_base . '-' . $number === $del_id ) {
   432 					unset( $all_instances[ $number ] );
   433 					unset( $all_instances[ $number ] );
   433 				}
   434 				}
   434 			}
   435 			}
   435 		} else {
   436 		} else {
   436 			if ( isset( $_POST[ 'widget-' . $this->id_base ] ) && is_array( $_POST[ 'widget-' . $this->id_base ] ) ) {
   437 			if ( isset( $_POST[ 'widget-' . $this->id_base ] ) && is_array( $_POST[ 'widget-' . $this->id_base ] ) ) {
   437 				$settings = $_POST[ 'widget-' . $this->id_base ];
   438 				$settings = $_POST[ 'widget-' . $this->id_base ];
   438 			} elseif ( isset( $_POST['id_base'] ) && $_POST['id_base'] == $this->id_base ) {
   439 			} elseif ( isset( $_POST['id_base'] ) && $_POST['id_base'] === $this->id_base ) {
   439 				$num      = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
   440 				$num      = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
   440 				$settings = array( $num => array() );
   441 				$settings = array( $num => array() );
   441 			} else {
   442 			} else {
   442 				return;
   443 				return;
   443 			}
   444 			}
   505 		}
   506 		}
   506 
   507 
   507 		$widget_args   = wp_parse_args( $widget_args, array( 'number' => -1 ) );
   508 		$widget_args   = wp_parse_args( $widget_args, array( 'number' => -1 ) );
   508 		$all_instances = $this->get_settings();
   509 		$all_instances = $this->get_settings();
   509 
   510 
   510 		if ( -1 == $widget_args['number'] ) {
   511 		if ( -1 === $widget_args['number'] ) {
   511 			// We echo out a form where 'number' can be set later.
   512 			// We echo out a form where 'number' can be set later.
   512 			$this->_set( '__i__' );
   513 			$this->_set( '__i__' );
   513 			$instance = array();
   514 			$instance = array();
   514 		} else {
   515 		} else {
   515 			$this->_set( $widget_args['number'] );
   516 			$this->_set( $widget_args['number'] );
   610 	public function get_settings() {
   611 	public function get_settings() {
   611 
   612 
   612 		$settings = get_option( $this->option_name );
   613 		$settings = get_option( $this->option_name );
   613 
   614 
   614 		if ( false === $settings ) {
   615 		if ( false === $settings ) {
       
   616 			$settings = array();
   615 			if ( isset( $this->alt_option_name ) ) {
   617 			if ( isset( $this->alt_option_name ) ) {
   616 				$settings = get_option( $this->alt_option_name );
   618 				// Get settings from alternative (legacy) option.
   617 			} else {
   619 				$settings = get_option( $this->alt_option_name, array() );
   618 				// Save an option so it can be autoloaded next time.
   620 
   619 				$this->save_settings( array() );
   621 				// Delete the alternative (legacy) option as the new option will be created using `$this->option_name`.
   620 			}
   622 				delete_option( $this->alt_option_name );
       
   623 			}
       
   624 			// Save an option so it can be autoloaded next time.
       
   625 			$this->save_settings( $settings );
   621 		}
   626 		}
   622 
   627 
   623 		if ( ! is_array( $settings ) && ! ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) ) {
   628 		if ( ! is_array( $settings ) && ! ( $settings instanceof ArrayObject || $settings instanceof ArrayIterator ) ) {
   624 			$settings = array();
   629 			$settings = array();
   625 		}
   630 		}