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 22.3

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 ) {
			if ( function_exists( 'add_settings_error' ) ) {
				add_settings_error(
					// Slug title of the setting.
					$this->group_name,
					// Suffix-ID for the error message box. WordPress prepends `setting-error-`.
					$key,
					// The error message.
					sprintf(
						/* translators: %s expands to an invalid URL. */
						__( '%s does not seem to be a valid url. Please correct.', 'wordpress-seo' ),
						'<strong>' . esc_url( $submitted_url ) . '</strong>'
					),
					// Message type.
					'error'
				);
			}

			// 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;
		}
	}
}