wp/wp-includes/class-wp-widget.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    99 	//
    99 	//
   100 
   100 
   101 	/**
   101 	/**
   102 	 * Echoes the widget content.
   102 	 * Echoes the widget content.
   103 	 *
   103 	 *
   104 	 * Sub-classes should over-ride this function to generate their widget code.
   104 	 * Subclasses should override this function to generate their widget code.
   105 	 *
   105 	 *
   106 	 * @since 2.8.0
   106 	 * @since 2.8.0
   107 	 *
   107 	 *
   108 	 * @param array $args     Display arguments including 'before_title', 'after_title',
   108 	 * @param array $args     Display arguments including 'before_title', 'after_title',
   109 	 *                        'before_widget', and 'after_widget'.
   109 	 *                        'before_widget', and 'after_widget'.
   110 	 * @param array $instance The settings for the particular instance of the widget.
   110 	 * @param array $instance The settings for the particular instance of the widget.
   111 	 */
   111 	 */
   112 	public function widget( $args, $instance ) {
   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 overridden in a subclass.' );
   114 	}
   114 	}
   115 
   115 
   116 	/**
   116 	/**
   117 	 * Updates a particular instance of a widget.
   117 	 * Updates a particular instance of a widget.
   118 	 *
   118 	 *
   149 	/**
   149 	/**
   150 	 * PHP5 constructor.
   150 	 * PHP5 constructor.
   151 	 *
   151 	 *
   152 	 * @since 2.8.0
   152 	 * @since 2.8.0
   153 	 *
   153 	 *
   154 	 * @param string $id_base         Optional Base ID for the widget, lowercase and unique. If left empty,
   154 	 * @param string $id_base         Optional. Base ID for the widget, lowercase and unique. If left empty,
   155 	 *                                a portion of the widget's class name will be used Has to be unique.
   155 	 *                                a portion of the widget's class name will be used. Has to be unique.
   156 	 * @param string $name            Name for the widget displayed on the configuration page.
   156 	 * @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 information
   157 	 * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
   158 	 *                                on accepted arguments. Default empty array.
   158 	 *                                information on accepted arguments. Default empty array.
   159 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   159 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   160 	 *                                information on accepted arguments. Default empty array.
   160 	 *                                information on accepted arguments. Default empty array.
   161 	 */
   161 	 */
   162 	public function __construct( $id_base, $name, $widget_options = array(), $control_options = array() ) {
   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 );
   163 		$this->id_base         = empty( $id_base ) ? preg_replace( '/(wp_)?widget_/', '', strtolower( get_class( $this ) ) ) : strtolower( $id_base );
   175 
   175 
   176 	/**
   176 	/**
   177 	 * PHP4 constructor.
   177 	 * PHP4 constructor.
   178 	 *
   178 	 *
   179 	 * @since 2.8.0
   179 	 * @since 2.8.0
   180 	 *
   180 	 * @deprecated 4.3.0 Use __construct() instead.
   181 	 * @see __construct()
   181 	 *
   182 	 *
   182 	 * @see WP_Widget::__construct()
   183 	 * @param string $id_base         Optional Base ID for the widget, lowercase and unique. If left empty,
   183 	 *
   184 	 *                                a portion of the widget's class name will be used Has to be unique.
   184 	 * @param string $id_base         Optional. Base ID for the widget, lowercase and unique. If left empty,
       
   185 	 *                                a portion of the widget's class name will be used. Has to be unique.
   185 	 * @param string $name            Name for the widget displayed on the configuration page.
   186 	 * @param string $name            Name for the widget displayed on the configuration page.
   186 	 * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for information
   187 	 * @param array  $widget_options  Optional. Widget options. See wp_register_sidebar_widget() for
   187 	 *                                on accepted arguments. Default empty array.
   188 	 *                                information on accepted arguments. Default empty array.
   188 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   189 	 * @param array  $control_options Optional. Widget control options. See wp_register_widget_control() for
   189 	 *                                information on accepted arguments. Default empty array.
   190 	 *                                information on accepted arguments. Default empty array.
   190 	 */
   191 	 */
   191 	public function WP_Widget( $id_base, $name, $widget_options = array(), $control_options = array() ) {
   192 	public function WP_Widget( $id_base, $name, $widget_options = array(), $control_options = array() ) {
   192 		_deprecated_constructor( 'WP_Widget', '4.3.0', get_class( $this ) );
   193 		_deprecated_constructor( 'WP_Widget', '4.3.0', get_class( $this ) );
   204 	 *
   205 	 *
   205 	 * @param string $field_name Field name
   206 	 * @param string $field_name Field name
   206 	 * @return string Name attribute for $field_name
   207 	 * @return string Name attribute for $field_name
   207 	 */
   208 	 */
   208 	public function get_field_name( $field_name ) {
   209 	public function get_field_name( $field_name ) {
   209 		if ( false === $pos = strpos( $field_name, '[' ) ) {
   210 		$pos = strpos( $field_name, '[' );
       
   211 		if ( false === $pos ) {
   210 			return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
   212 			return 'widget-' . $this->id_base . '[' . $this->number . '][' . $field_name . ']';
   211 		} else {
   213 		} else {
   212 			return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
   214 			return 'widget-' . $this->id_base . '[' . $this->number . '][' . substr_replace( $field_name, '][', $pos, strlen( '[' ) );
   213 		}
   215 		}
   214 	}
   216 	}
   395 	public function update_callback( $deprecated = 1 ) {
   397 	public function update_callback( $deprecated = 1 ) {
   396 		global $wp_registered_widgets;
   398 		global $wp_registered_widgets;
   397 
   399 
   398 		$all_instances = $this->get_settings();
   400 		$all_instances = $this->get_settings();
   399 
   401 
   400 		// We need to update the data
   402 		// We need to update the data.
   401 		if ( $this->updated ) {
   403 		if ( $this->updated ) {
   402 			return;
   404 			return;
   403 		}
   405 		}
   404 
   406 
   405 		if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {
   407 		if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {
   406 			// Delete the settings for this instance of the widget
   408 			// Delete the settings for this instance of the widget.
   407 			if ( isset( $_POST['the-widget-id'] ) ) {
   409 			if ( isset( $_POST['the-widget-id'] ) ) {
   408 				$del_id = $_POST['the-widget-id'];
   410 				$del_id = $_POST['the-widget-id'];
   409 			} else {
   411 			} else {
   410 				return;
   412 				return;
   411 			}
   413 			}
   460 				$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
   462 				$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );
   461 				if ( false !== $instance ) {
   463 				if ( false !== $instance ) {
   462 					$all_instances[ $number ] = $instance;
   464 					$all_instances[ $number ] = $instance;
   463 				}
   465 				}
   464 
   466 
   465 				break; // run only once
   467 				break; // Run only once.
   466 			}
   468 			}
   467 		}
   469 		}
   468 
   470 
   469 		$this->save_settings( $all_instances );
   471 		$this->save_settings( $all_instances );
   470 		$this->updated = true;
   472 		$this->updated = true;
   490 
   492 
   491 		$widget_args   = wp_parse_args( $widget_args, array( 'number' => -1 ) );
   493 		$widget_args   = wp_parse_args( $widget_args, array( 'number' => -1 ) );
   492 		$all_instances = $this->get_settings();
   494 		$all_instances = $this->get_settings();
   493 
   495 
   494 		if ( -1 == $widget_args['number'] ) {
   496 		if ( -1 == $widget_args['number'] ) {
   495 			// We echo out a form where 'number' can be set later
   497 			// We echo out a form where 'number' can be set later.
   496 			$this->_set( '__i__' );
   498 			$this->_set( '__i__' );
   497 			$instance = array();
   499 			$instance = array();
   498 		} else {
   500 		} else {
   499 			$this->_set( $widget_args['number'] );
   501 			$this->_set( $widget_args['number'] );
   500 			$instance = $all_instances[ $widget_args['number'] ];
   502 			$instance = $all_instances[ $widget_args['number'] ];