WP_Widget::update_callback()publicWP 2.8.0

Handles changed settings (Do NOT override).

Метод класса: WP_Widget{}

Хуки из метода

Возвращает

null. Ничего (null).

Использование

$WP_Widget = new WP_Widget();
$WP_Widget->update_callback( $deprecated );
$deprecated(int)
Not used.
По умолчанию: 1

Заметки

  • Global. Массив. $wp_registered_widgets

Список изменений

С версии 2.8.0 Введена.

Код WP_Widget::update_callback() WP 6.5.2

public function update_callback( $deprecated = 1 ) {
	global $wp_registered_widgets;

	$all_instances = $this->get_settings();

	// We need to update the data.
	if ( $this->updated ) {
		return;
	}

	if ( isset( $_POST['delete_widget'] ) && $_POST['delete_widget'] ) {
		// Delete the settings for this instance of the widget.
		if ( isset( $_POST['the-widget-id'] ) ) {
			$del_id = $_POST['the-widget-id'];
		} else {
			return;
		}

		if ( isset( $wp_registered_widgets[ $del_id ]['params'][0]['number'] ) ) {
			$number = $wp_registered_widgets[ $del_id ]['params'][0]['number'];

			if ( $this->id_base . '-' . $number === $del_id ) {
				unset( $all_instances[ $number ] );
			}
		}
	} else {
		if ( isset( $_POST[ 'widget-' . $this->id_base ] ) && is_array( $_POST[ 'widget-' . $this->id_base ] ) ) {
			$settings = $_POST[ 'widget-' . $this->id_base ];
		} elseif ( isset( $_POST['id_base'] ) && $_POST['id_base'] === $this->id_base ) {
			$num      = $_POST['multi_number'] ? (int) $_POST['multi_number'] : (int) $_POST['widget_number'];
			$settings = array( $num => array() );
		} else {
			return;
		}

		foreach ( $settings as $number => $new_instance ) {
			$new_instance = stripslashes_deep( $new_instance );
			$this->_set( $number );

			$old_instance = isset( $all_instances[ $number ] ) ? $all_instances[ $number ] : array();

			$was_cache_addition_suspended = wp_suspend_cache_addition();
			if ( $this->is_preview() && ! $was_cache_addition_suspended ) {
				wp_suspend_cache_addition( true );
			}

			$instance = $this->update( $new_instance, $old_instance );

			if ( $this->is_preview() ) {
				wp_suspend_cache_addition( $was_cache_addition_suspended );
			}

			/**
			 * Filters a widget's settings before saving.
			 *
			 * Returning false will effectively short-circuit the widget's ability
			 * to update settings.
			 *
			 * @since 2.8.0
			 *
			 * @param array     $instance     The current widget instance's settings.
			 * @param array     $new_instance Array of new widget settings.
			 * @param array     $old_instance Array of old widget settings.
			 * @param WP_Widget $widget       The current widget instance.
			 */
			$instance = apply_filters( 'widget_update_callback', $instance, $new_instance, $old_instance, $this );

			if ( false !== $instance ) {
				$all_instances[ $number ] = $instance;
			}

			break; // Run only once.
		}
	}

	$this->save_settings( $all_instances );
	$this->updated = true;
}