From 1aad0bd47121bcaeea6dcc73be23904ae9a8c1e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20W=C3=BCnsch?= Date: Fri, 11 Sep 2026 15:55:55 +0200 Subject: [PATCH] Query: Use the null coalescing assignment operator for query variable defaults. --- src/wp-includes/class-wp-date-query.php | 4 +- .../class-wp-metadata-lazyloader.php | 8 +-- src/wp-includes/class-wp-query.php | 54 +++++-------------- src/wp-includes/class-wp-tax-query.php | 9 ++-- 4 files changed, 20 insertions(+), 55 deletions(-) diff --git a/src/wp-includes/class-wp-date-query.php b/src/wp-includes/class-wp-date-query.php index 4a6e6ca2ed426..abfacce6cdde8 100644 --- a/src/wp-includes/class-wp-date-query.php +++ b/src/wp-includes/class-wp-date-query.php @@ -784,9 +784,7 @@ protected function get_sql_for_clause( $query, $parent_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/src/wp-includes/class-wp-metadata-lazyloader.php b/src/wp-includes/class-wp-metadata-lazyloader.php index f9c02391d2b1d..2725cbfee32c6 100644 --- a/src/wp-includes/class-wp-metadata-lazyloader.php +++ b/src/wp-includes/class-wp-metadata-lazyloader.php @@ -84,15 +84,11 @@ public function queue_objects( $object_type, $object_ids ) { $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/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php index 1d6b652c5a610..c934f5fc26894 100644 --- a/src/wp-includes/class-wp-query.php +++ b/src/wp-includes/class-wp-query.php @@ -618,9 +618,7 @@ public function fill_query_vars( $query_vars ) { ); foreach ( $keys as $key ) { - if ( ! isset( $query_vars[ $key ] ) ) { - $query_vars[ $key ] = ''; - } + $query_vars[ $key ] ??= ''; } $array_keys = array( @@ -643,9 +641,7 @@ public function fill_query_vars( $query_vars ) { ); 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 @@ public function parse_tax_query( &$query_vars ) { } 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,18 @@ public function get_posts() { ) ); - 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; - } + $query_vars['ignore_sticky_posts'] ??= false; - if ( ! isset( $query_vars['suppress_filters'] ) ) { - $query_vars['suppress_filters'] = false; - } + $query_vars['suppress_filters'] ??= false; - if ( ! isset( $query_vars['cache_results'] ) ) { - $query_vars['cache_results'] = true; - } + $query_vars['cache_results'] ??= true; - if ( ! isset( $query_vars['update_post_term_cache'] ) ) { - $query_vars['update_post_term_cache'] = true; - } + $query_vars['update_post_term_cache'] ??= true; - if ( ! isset( $query_vars['update_menu_item_cache'] ) ) { - $query_vars['update_menu_item_cache'] = false; - } + $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 +1980,7 @@ public function get_posts() { $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 +3302,7 @@ public function get_posts() { } 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 +3323,7 @@ public function get_posts() { } 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 +5057,7 @@ protected function generate_cache_key( array $args, $sql ) { } // 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/src/wp-includes/class-wp-tax-query.php b/src/wp-includes/class-wp-tax-query.php index c6ec3258cc201..7e254b1275c9f 100644 --- a/src/wp-includes/class-wp-tax-query.php +++ b/src/wp-includes/class-wp-tax-query.php @@ -161,9 +161,8 @@ public function sanitize_query( $queries ) { */ 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 @@ public function sanitize_query( $queries ) { 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; }