wpdb::check_safe_collation()protectedWP 4.2.0

Checks if the query is accessing a collation considered safe on the current version of MySQL.

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

Хуков нет.

Возвращает

true|false. True if the collation is safe, false if it isn't.

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

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->check_safe_collation( $query );
$query(строка) (обязательный)
The query to check.

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

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

Код wpdb::check_safe_collation() WP 6.4.3

protected function check_safe_collation( $query ) {
	if ( $this->checking_collation ) {
		return true;
	}

	// We don't need to check the collation for queries that don't read data.
	$query = ltrim( $query, "\r\n\t (" );
	if ( preg_match( '/^(?:SHOW|DESCRIBE|DESC|EXPLAIN|CREATE)\s/i', $query ) ) {
		return true;
	}

	// All-ASCII queries don't need extra checking.
	if ( $this->check_ascii( $query ) ) {
		return true;
	}

	$table = $this->get_table_from_query( $query );
	if ( ! $table ) {
		return false;
	}

	$this->checking_collation = true;
	$collation                = $this->get_table_charset( $table );
	$this->checking_collation = false;

	// Tables with no collation, or latin1 only, don't need extra checking.
	if ( false === $collation || 'latin1' === $collation ) {
		return true;
	}

	$table = strtolower( $table );
	if ( empty( $this->col_meta[ $table ] ) ) {
		return false;
	}

	// If any of the columns don't have one of these collations, it needs more sanity checking.
	$safe_collations = array(
		'utf8_bin',
		'utf8_general_ci',
		'utf8mb3_bin',
		'utf8mb3_general_ci',
		'utf8mb4_bin',
		'utf8mb4_general_ci',
	);

	foreach ( $this->col_meta[ $table ] as $col ) {
		if ( empty( $col->Collation ) ) {
			continue;
		}

		if ( ! in_array( $col->Collation, $safe_collations, true ) ) {
			return false;
		}
	}

	return true;
}