WP_Date_Query::sanitize_query()publicWP 4.1.0

Recursive-friendly query sanitizer.

Ensures that each query-level clause has a 'relation' key, and that each first-order clause contains all the necessary keys from $defaults.

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

Хуков нет.

Возвращает

Массив. Sanitized queries.

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

$WP_Date_Query = new WP_Date_Query();
$WP_Date_Query->sanitize_query( $queries, $parent_query );
$queries(массив) (обязательный)
-
$parent_query(массив)
-
По умолчанию: null

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

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

Код WP_Date_Query::sanitize_query() WP 6.5.2

public function sanitize_query( $queries, $parent_query = null ) {
	$cleaned_query = array();

	$defaults = array(
		'column'   => 'post_date',
		'compare'  => '=',
		'relation' => 'AND',
	);

	// Numeric keys should always have array values.
	foreach ( $queries as $qkey => $qvalue ) {
		if ( is_numeric( $qkey ) && ! is_array( $qvalue ) ) {
			unset( $queries[ $qkey ] );
		}
	}

	// Each query should have a value for each default key. Inherit from the parent when possible.
	foreach ( $defaults as $dkey => $dvalue ) {
		if ( isset( $queries[ $dkey ] ) ) {
			continue;
		}

		if ( isset( $parent_query[ $dkey ] ) ) {
			$queries[ $dkey ] = $parent_query[ $dkey ];
		} else {
			$queries[ $dkey ] = $dvalue;
		}
	}

	// Validate the dates passed in the query.
	if ( $this->is_first_order_clause( $queries ) ) {
		$this->validate_date_values( $queries );
	}

	// Sanitize the relation parameter.
	$queries['relation'] = $this->sanitize_relation( $queries['relation'] );

	foreach ( $queries as $key => $q ) {
		if ( ! is_array( $q ) || in_array( $key, $this->time_keys, true ) ) {
			// This is a first-order query. Trust the values and sanitize when building SQL.
			$cleaned_query[ $key ] = $q;
		} else {
			// Any array without a time key is another query, so we recurse.
			$cleaned_query[] = $this->sanitize_query( $q, $queries );
		}
	}

	return $cleaned_query;
}