WPSEO_Option::validate_url()publicYoast 1.0

Validates an option as a valid URL. Prints out a WordPress settings error notice if the URL is invalid.

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

Хуков нет.

Возвращает

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

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

$WPSEO_Option = new WPSEO_Option();
$WPSEO_Option->validate_url( $key, $dirty, $old, $clean );
$key(строка) (обязательный)
Key to check, by type of URL setting.
$dirty(массив) (обязательный)
Dirty data with the new values.
$old(массив) (обязательный)
Old data.
$clean(массив) (обязательный) (передается по ссылке — &)
Clean data by reference, normally the default values.

Код WPSEO_Option::validate_url() Yoast 24.9

public function validate_url( $key, $dirty, $old, &$clean ) {
	if ( isset( $dirty[ $key ] ) && $dirty[ $key ] !== '' ) {

		$submitted_url = trim( $dirty[ $key ] );
		$validated_url = filter_var( WPSEO_Utils::sanitize_url( $submitted_url ), FILTER_VALIDATE_URL );

		if ( $validated_url === false ) {
			// Restore the previous URL value, if any.
			if ( isset( $old[ $key ] ) && $old[ $key ] !== '' ) {
				$url = WPSEO_Utils::sanitize_url( $old[ $key ] );
				if ( $url !== '' ) {
					$clean[ $key ] = $url;
				}
			}

			Yoast_Input_Validation::add_dirty_value_to_settings_errors( $key, $submitted_url );

			return;
		}

		// The URL format is valid, let's sanitize it.
		$url = WPSEO_Utils::sanitize_url( $validated_url );

		if ( $url !== '' ) {
			$clean[ $key ] = $url;
		}
	}
}