WP_Widget_Media::update()publicWP 4.8.0

Sanitizes the widget form values as they are saved.

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

Хуков нет.

Возвращает

Массив. Updated safe values to be saved.

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

$WP_Widget_Media = new WP_Widget_Media();
$WP_Widget_Media->update( $new_instance, $old_instance );
$new_instance(массив) (обязательный)
Values just sent to be saved.
$old_instance(массив) (обязательный)
Previously saved values from database.

Заметки

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

С версии 4.8.0 Введена.
С версии 5.9.0 Renamed $instance to $old_instance to match parent class for PHP 8 named parameter support.

Код WP_Widget_Media::update() WP 6.1.1

public function update( $new_instance, $old_instance ) {

	$schema = $this->get_instance_schema();
	foreach ( $schema as $field => $field_schema ) {
		if ( ! array_key_exists( $field, $new_instance ) ) {
			continue;
		}
		$value = $new_instance[ $field ];

		/*
		 * Workaround for rest_validate_value_from_schema() due to the fact that
		 * rest_is_boolean( '' ) === false, while rest_is_boolean( '1' ) is true.
		 */
		if ( 'boolean' === $field_schema['type'] && '' === $value ) {
			$value = false;
		}

		if ( true !== rest_validate_value_from_schema( $value, $field_schema, $field ) ) {
			continue;
		}

		$value = rest_sanitize_value_from_schema( $value, $field_schema );

		// @codeCoverageIgnoreStart
		if ( is_wp_error( $value ) ) {
			continue; // Handle case when rest_sanitize_value_from_schema() ever returns WP_Error as its phpdoc @return tag indicates.
		}

		// @codeCoverageIgnoreEnd
		if ( isset( $field_schema['sanitize_callback'] ) ) {
			$value = call_user_func( $field_schema['sanitize_callback'], $value );
		}
		if ( is_wp_error( $value ) ) {
			continue;
		}
		$old_instance[ $field ] = $value;
	}

	return $old_instance;
}