wp/wp-includes/query.php
changeset 21 48c4eec2b7e6
parent 18 be944660c56a
--- a/wp/wp-includes/query.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/query.php	Fri Sep 05 18:40:08 2025 +0200
@@ -15,17 +15,18 @@
  * Retrieves the value of a query variable in the WP_Query class.
  *
  * @since 1.5.0
- * @since 3.9.0 The `$default` argument was introduced.
+ * @since 3.9.0 The `$default_value` argument was introduced.
  *
  * @global WP_Query $wp_query WordPress Query object.
  *
- * @param string $var       The variable key to retrieve.
- * @param mixed  $default   Optional. Value to return if the query variable is not set. Default empty.
+ * @param string $query_var     The variable key to retrieve.
+ * @param mixed  $default_value Optional. Value to return if the query variable is not set.
+ *                              Default empty string.
  * @return mixed Contents of the query variable.
  */
-function get_query_var( $var, $default = '' ) {
+function get_query_var( $query_var, $default_value = '' ) {
 	global $wp_query;
-	return $wp_query->get( $var, $default );
+	return $wp_query->get( $query_var, $default_value );
 }
 
 /**
@@ -67,12 +68,12 @@
  *
  * @global WP_Query $wp_query WordPress Query object.
  *
- * @param string $var   Query variable key.
- * @param mixed  $value Query variable value.
+ * @param string $query_var Query variable key.
+ * @param mixed  $value     Query variable value.
  */
-function set_query_var( $var, $value ) {
+function set_query_var( $query_var, $value ) {
 	global $wp_query;
-	$wp_query->set( $var, $value );
+	$wp_query->set( $query_var, $value );
 }
 
 /**
@@ -450,7 +451,7 @@
  * If you set a static page for the front page of your site, this function will return
  * true when viewing that page.
  *
- * Otherwise the same as @see is_home()
+ * Otherwise the same as {@see is_home()}.
  *
  * For more information on this and similar theme functions, check out
  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
@@ -901,6 +902,11 @@
 function is_main_query() {
 	global $wp_query;
 
+	if ( ! isset( $wp_query ) ) {
+		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '6.1.0' );
+		return false;
+	}
+
 	if ( 'pre_get_posts' === current_filter() ) {
 		_doing_it_wrong(
 			__FUNCTION__,
@@ -934,6 +940,11 @@
  */
 function have_posts() {
 	global $wp_query;
+
+	if ( ! isset( $wp_query ) ) {
+		return false;
+	}
+
 	return $wp_query->have_posts();
 }
 
@@ -952,6 +963,11 @@
  */
 function in_the_loop() {
 	global $wp_query;
+
+	if ( ! isset( $wp_query ) ) {
+		return false;
+	}
+
 	return $wp_query->in_the_loop;
 }
 
@@ -964,6 +980,11 @@
  */
 function rewind_posts() {
 	global $wp_query;
+
+	if ( ! isset( $wp_query ) ) {
+		return;
+	}
+
 	$wp_query->rewind_posts();
 }
 
@@ -976,6 +997,11 @@
  */
 function the_post() {
 	global $wp_query;
+
+	if ( ! isset( $wp_query ) ) {
+		return;
+	}
+
 	$wp_query->the_post();
 }
 
@@ -994,6 +1020,11 @@
  */
 function have_comments() {
 	global $wp_query;
+
+	if ( ! isset( $wp_query ) ) {
+		return false;
+	}
+
 	return $wp_query->have_comments();
 }
 
@@ -1003,12 +1034,15 @@
  * @since 2.2.0
  *
  * @global WP_Query $wp_query WordPress Query object.
- *
- * @return null
  */
 function the_comment() {
 	global $wp_query;
-	return $wp_query->the_comment();
+
+	if ( ! isset( $wp_query ) ) {
+		return;
+	}
+
+	$wp_query->the_comment();
 }
 
 /**
@@ -1105,8 +1139,10 @@
 
 	$query = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, get_query_var( 'name' ) );
 
-	// If year, monthnum, or day have been specified, make our query more precise
-	// just in case there are multiple identical _wp_old_slug values.
+	/*
+	 * If year, monthnum, or day have been specified, make our query more precise
+	 * just in case there are multiple identical _wp_old_slug values.
+	 */
 	if ( get_query_var( 'year' ) ) {
 		$query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
 	}
@@ -1117,7 +1153,16 @@
 		$query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
 	}
 
-	$id = (int) $wpdb->get_var( $query );
+	$key          = md5( $query );
+	$last_changed = wp_cache_get_last_changed( 'posts' );
+	$cache_key    = "find_post_by_old_slug:$key:$last_changed";
+	$cache        = wp_cache_get( $cache_key, 'post-queries' );
+	if ( false !== $cache ) {
+		$id = $cache;
+	} else {
+		$id = (int) $wpdb->get_var( $query );
+		wp_cache_set( $cache_key, $id, 'post-queries' );
+	}
 
 	return $id;
 }
@@ -1150,11 +1195,20 @@
 
 	$id = 0;
 	if ( $date_query ) {
-		$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) );
-
-		if ( ! $id ) {
-			// Check to see if an old slug matches the old date.
-			$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
+		$query        = $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) );
+		$key          = md5( $query );
+		$last_changed = wp_cache_get_last_changed( 'posts' );
+		$cache_key    = "find_post_by_old_date:$key:$last_changed";
+		$cache        = wp_cache_get( $cache_key, 'post-queries' );
+		if ( false !== $cache ) {
+			$id = $cache;
+		} else {
+			$id = (int) $wpdb->get_var( $query );
+			if ( ! $id ) {
+				// Check to see if an old slug matches the old date.
+				$id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) );
+			}
+			wp_cache_set( $cache_key, $id, 'post-queries' );
 		}
 	}