WP_Date_Query::sanitize_query( array $queries, array $parent_query = null ): array
- Since
- 4.1.0
- Source
wp-includes/class-wp-date-query.php:188
Description
Ensures that each query-level clause has a 'relation' key, and that each first-order clause contains all the necessary keys from $defaults.
Compatibility
- WordPress
- since 4.1.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$queriesarray$parent_queryarrayoptional- Default:
null
Return value
array- Sanitized queries.
Performance profile
How much work a call to WP_Date_Query::sanitize_query() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 21–27
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 60.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 2 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Date_Query::sanitize_query() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!->is_first_order_clause() | 21–24 | ->is_first_order_clause(), ->sanitize_relation() |
->is_first_order_clause() | 24–27 | ->is_first_order_clause(), ->validate_date_values(), ->sanitize_relation() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 60 | 21–27 | 13 | |
| 8.5 | 60 | 21–27 | 13 | |
| 8.4 | 60 | 21–27 | 13 | 5 fewer instructions than PHP 8.3 |
| 8.3 | 65 | 21–27 | 13 | |
| 8.2 | 65 | 21–27 | 13 | |
| 8.1 | 65 | 21–27 | 13 | |
| 7.4 | 65 | 21–27 | 13 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 4
- WP_Date_Query::is_first_order_clause()Determines whether this is a first-order clause.
- WP_Date_Query::validate_date_values()Validates the given date_query values and triggers errors if something is not valid.
- WP_Date_Query::sanitize_relation()Sanitizes a 'relation' operator.
- WP_Date_Query::sanitize_query()Recursive-friendly query sanitizer.
Used by · 2
- WP_Date_Query::__construct()Constructor.
- WP_Date_Query::sanitize_query()Recursive-friendly query sanitizer.
Source code
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; } $queries[ $dkey ] = $parent_query[ $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; }Changelog
Introduced in 4.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-date-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.