Commit 41d8c6d771 for wordpress.org
commit 41d8c6d77110fb05130414063649c1723c724e78
Author: SergeyBiryukov <sergeybiryukov@git.wordpress.org>
Date: Sat Sep 12 23:55:47 2026 +0000
Code Modernization: Use the null coalescing assignment operator for query variable defaults.
Replaces the “assign a default only if not already set” guard blocks in `WP_Query`, `WP_Tax_Query`, `WP_Date_Query` and `WP_Metadata_Lazyloader` with the null coalescing assignment operator (`??=`).
Follow-up to r63443, r63501, r63509, r63541.
Props Soean, mukesh27.
See #65823.
Built from https://develop.svn.wordpress.org/trunk@63607
git-svn-id: http://core.svn.wordpress.org/trunk@62783 1a063a9b-81f0-0310-95a4-ce76da25c4cd
diff --git a/wp-includes/class-wp-date-query.php b/wp-includes/class-wp-date-query.php
index 4a6e6ca2ed..abfacce6cd 100644
--- a/wp-includes/class-wp-date-query.php
+++ b/wp-includes/class-wp-date-query.php
@@ -784,9 +784,7 @@ class WP_Date_Query {
if ( isset( $query['hour'] ) || isset( $query['minute'] ) || isset( $query['second'] ) ) {
// Avoid notices.
foreach ( array( 'hour', 'minute', 'second' ) as $unit ) {
- if ( ! isset( $query[ $unit ] ) ) {
- $query[ $unit ] = null;
- }
+ $query[ $unit ] ??= null;
}
$time_query = $this->build_time_query( $column, $compare, $query['hour'], $query['minute'], $query['second'] );
diff --git a/wp-includes/class-wp-metadata-lazyloader.php b/wp-includes/class-wp-metadata-lazyloader.php
index f9c02391d2..2725cbfee3 100644
--- a/wp-includes/class-wp-metadata-lazyloader.php
+++ b/wp-includes/class-wp-metadata-lazyloader.php
@@ -84,15 +84,11 @@ class WP_Metadata_Lazyloader {
$type_settings = $this->settings[ $object_type ];
- if ( ! isset( $this->pending_objects[ $object_type ] ) ) {
- $this->pending_objects[ $object_type ] = array();
- }
+ $this->pending_objects[ $object_type ] ??= array();
foreach ( $object_ids as $object_id ) {
// Keyed by ID for faster lookup.
- if ( ! isset( $this->pending_objects[ $object_type ][ $object_id ] ) ) {
- $this->pending_objects[ $object_type ][ $object_id ] = 1;
- }
+ $this->pending_objects[ $object_type ][ $object_id ] ??= 1;
}
add_filter( $type_settings['filter'], $type_settings['callback'], 10, 5 );
diff --git a/wp-includes/class-wp-query.php b/wp-includes/class-wp-query.php
index 1d6b652c5a..2835da604b 100644
--- a/wp-includes/class-wp-query.php
+++ b/wp-includes/class-wp-query.php
@@ -618,9 +618,7 @@ class WP_Query {
);
foreach ( $keys as $key ) {
- if ( ! isset( $query_vars[ $key ] ) ) {
- $query_vars[ $key ] = '';
- }
+ $query_vars[ $key ] ??= '';
}
$array_keys = array(
@@ -643,9 +641,7 @@ class WP_Query {
);
foreach ( $array_keys as $key ) {
- if ( ! isset( $query_vars[ $key ] ) ) {
- $query_vars[ $key ] = array();
- }
+ $query_vars[ $key ] ??= array();
}
return $query_vars;
@@ -1279,10 +1275,8 @@ class WP_Query {
}
if ( ! empty( $query_vars['category__and'] ) && 1 === count( (array) $query_vars['category__and'] ) ) {
- $query_vars['category__and'] = (array) $query_vars['category__and'];
- if ( ! isset( $query_vars['category__in'] ) ) {
- $query_vars['category__in'] = array();
- }
+ $query_vars['category__and'] = (array) $query_vars['category__and'];
+ $query_vars['category__in'] ??= array();
$query_vars['category__in'][] = absint( reset( $query_vars['category__and'] ) );
unset( $query_vars['category__and'] );
}
@@ -1967,30 +1961,14 @@ class WP_Query {
)
);
- if ( ! isset( $query_vars['ignore_sticky_posts'] ) ) {
- $query_vars['ignore_sticky_posts'] = $query_vars['caller_get_posts'];
- }
+ $query_vars['ignore_sticky_posts'] ??= $query_vars['caller_get_posts'];
}
- if ( ! isset( $query_vars['ignore_sticky_posts'] ) ) {
- $query_vars['ignore_sticky_posts'] = false;
- }
-
- if ( ! isset( $query_vars['suppress_filters'] ) ) {
- $query_vars['suppress_filters'] = false;
- }
-
- if ( ! isset( $query_vars['cache_results'] ) ) {
- $query_vars['cache_results'] = true;
- }
-
- if ( ! isset( $query_vars['update_post_term_cache'] ) ) {
- $query_vars['update_post_term_cache'] = true;
- }
-
- if ( ! isset( $query_vars['update_menu_item_cache'] ) ) {
- $query_vars['update_menu_item_cache'] = false;
- }
+ $query_vars['ignore_sticky_posts'] ??= false;
+ $query_vars['suppress_filters'] ??= false;
+ $query_vars['cache_results'] ??= true;
+ $query_vars['update_post_term_cache'] ??= true;
+ $query_vars['update_menu_item_cache'] ??= false;
if ( ! isset( $query_vars['lazy_load_term_meta'] ) ) {
$query_vars['lazy_load_term_meta'] = $query_vars['update_post_term_cache'];
@@ -1998,9 +1976,7 @@ class WP_Query {
$query_vars['update_post_term_cache'] = true;
}
- if ( ! isset( $query_vars['update_post_meta_cache'] ) ) {
- $query_vars['update_post_meta_cache'] = true;
- }
+ $query_vars['update_post_meta_cache'] ??= true;
if ( ! isset( $query_vars['post_type'] ) ) {
if ( $this->is_search ) {
@@ -3322,9 +3298,7 @@ class WP_Query {
}
if ( 'ids' === $query_vars['fields'] ) {
- if ( null === $this->posts ) {
- $this->posts = $wpdb->get_col( $this->request );
- }
+ $this->posts ??= $wpdb->get_col( $this->request );
/** @var int[] */
$this->posts = array_map( 'intval', $this->posts );
@@ -3345,9 +3319,7 @@ class WP_Query {
}
if ( 'id=>parent' === $query_vars['fields'] ) {
- if ( null === $this->posts ) {
- $this->posts = $wpdb->get_results( $this->request );
- }
+ $this->posts ??= $wpdb->get_results( $this->request );
$this->post_count = count( $this->posts );
$this->set_found_posts( $query_vars, $limits );
@@ -5081,9 +5053,7 @@ class WP_Query {
}
// Add a default orderby value of date to ensure same cache key generation.
- if ( ! isset( $args['orderby'] ) ) {
- $args['orderby'] = 'date';
- }
+ $args['orderby'] ??= 'date';
$placeholder = $wpdb->placeholder_escape();
array_walk_recursive(
diff --git a/wp-includes/class-wp-tax-query.php b/wp-includes/class-wp-tax-query.php
index c6ec3258cc..7e254b1275 100644
--- a/wp-includes/class-wp-tax-query.php
+++ b/wp-includes/class-wp-tax-query.php
@@ -161,9 +161,8 @@ class WP_Tax_Query {
*/
if ( ! empty( $cleaned_clause['taxonomy'] ) && 'NOT IN' !== $cleaned_clause['operator'] ) {
$taxonomy = $cleaned_clause['taxonomy'];
- if ( ! isset( $this->queried_terms[ $taxonomy ] ) ) {
- $this->queried_terms[ $taxonomy ] = array();
- }
+
+ $this->queried_terms[ $taxonomy ] ??= array();
/*
* Backward compatibility: Only store the first
@@ -184,9 +183,7 @@ class WP_Tax_Query {
if ( ! empty( $cleaned_subquery ) ) {
// All queries with children must have a relation.
- if ( ! isset( $cleaned_subquery['relation'] ) ) {
- $cleaned_subquery['relation'] = 'AND';
- }
+ $cleaned_subquery['relation'] ??= 'AND';
$cleaned_query[] = $cleaned_subquery;
}
diff --git a/wp-includes/version.php b/wp-includes/version.php
index 6e5a533db5..bc0a23dd52 100644
--- a/wp-includes/version.php
+++ b/wp-includes/version.php
@@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
-$wp_version = '7.2-alpha-63606';
+$wp_version = '7.2-alpha-63607';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.