WPSEO_Utils::sanitize_text_field()public staticYoast 1.8.0

Emulate the WP native sanitize_text_field function in a %%variable%% safe way.

Sanitize a string from user input or from the db.

  • Check for invalid UTF-8;
  • Convert single < characters to entity;
  • Strip all tags;
  • Remove line breaks, tabs and extra white space;
  • Strip octets - BUT DO NOT REMOVE (part of) VARIABLES WHICH WILL BE REPLACED.

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

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

Возвращает

Строку.

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

$result = WPSEO_Utils::sanitize_text_field( $value );
$value(строка) (обязательный)
String value to sanitize.

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

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

Код WPSEO_Utils::sanitize_text_field() Yoast 22.4

public static function sanitize_text_field( $value ) {
	$filtered = wp_check_invalid_utf8( $value );

	if ( strpos( $filtered, '<' ) !== false ) {
		$filtered = wp_pre_kses_less_than( $filtered );
		// This will strip extra whitespace for us.
		$filtered = wp_strip_all_tags( $filtered, true );
	}
	else {
		$filtered = trim( preg_replace( '`[\r\n\t ]+`', ' ', $filtered ) );
	}

	$found = false;
	while ( preg_match( '`[^%](%[a-f0-9]{2})`i', $filtered, $match ) ) {
		$filtered = str_replace( $match[1], '', $filtered );
		$found    = true;
	}
	unset( $match );

	if ( $found ) {
		// Strip out the whitespace that may now exist after removing the octets.
		$filtered = trim( preg_replace( '` +`', ' ', $filtered ) );
	}

	/**
	 * Filter a sanitized text field string.
	 *
	 * @since WP 2.9.0
	 *
	 * @param string $filtered The sanitized string.
	 * @param string $str      The string prior to being sanitized.
	 */
	return apply_filters( 'sanitize_text_field', $filtered, $value ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals -- Using WP native filter.
}