Changeset 59766 for trunk/src/wp-includes/class-wp-query.php
- Timestamp:
- 02/06/2025 05:02:17 AM (3 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-query.php
r59495 r59766 474 474 475 475 private $compat_methods = array( 'init_query_flags', 'parse_tax_query' ); 476 477 /** 478 * The cache key generated by the query. 479 * 480 * The cache key is generated by the method ::generate_cache_key() after the 481 * query has been normalized. 482 * 483 * @var string 484 */ 485 private $query_cache_key = ''; 476 486 477 487 /** … … 1102 1112 if ( ! empty( $qv['post_type'] ) ) { 1103 1113 if ( is_array( $qv['post_type'] ) ) { 1104 $qv['post_type'] = array_map( 'sanitize_key', $qv['post_type'] ); 1114 $qv['post_type'] = array_map( 'sanitize_key', array_unique( $qv['post_type'] ) ); 1115 sort( $qv['post_type'] ); 1105 1116 } else { 1106 1117 $qv['post_type'] = sanitize_key( $qv['post_type'] ); … … 1110 1121 if ( ! empty( $qv['post_status'] ) ) { 1111 1122 if ( is_array( $qv['post_status'] ) ) { 1112 $qv['post_status'] = array_map( 'sanitize_key', $qv['post_status'] ); 1123 $qv['post_status'] = array_map( 'sanitize_key', array_unique( $qv['post_status'] ) ); 1124 sort( $qv['post_status'] ); 1113 1125 } else { 1114 1126 $qv['post_status'] = preg_replace( '|[^a-z0-9_,-]|', '', $qv['post_status'] ); … … 1183 1195 $term = $q[ $t->query_var ]; 1184 1196 1185 if ( is_array( $term ) ) { 1186 $term = implode( ',', $term ); 1187 } 1197 if ( ! is_array( $term ) ) { 1198 $term = explode( ',', $term ); 1199 $term = array_map( 'trim', $term ); 1200 } 1201 sort( $term ); 1202 $term = implode( ',', $term ); 1188 1203 1189 1204 if ( str_contains( $term, '+' ) ) { … … 1221 1236 $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) ); 1222 1237 $cat_array = array_map( 'intval', $cat_array ); 1223 $q['cat'] = implode( ',', $cat_array ); 1238 sort( $cat_array ); 1239 $q['cat'] = implode( ',', $cat_array ); 1224 1240 1225 1241 foreach ( $cat_array as $cat ) { … … 1263 1279 if ( ! empty( $q['category__in'] ) ) { 1264 1280 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); 1265 $tax_query[] = array( 1281 sort( $q['category__in'] ); 1282 $tax_query[] = array( 1266 1283 'taxonomy' => 'category', 1267 1284 'terms' => $q['category__in'], … … 1273 1290 if ( ! empty( $q['category__not_in'] ) ) { 1274 1291 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) ); 1292 sort( $q['category__not_in'] ); 1275 1293 $tax_query[] = array( 1276 1294 'taxonomy' => 'category', … … 1283 1301 if ( ! empty( $q['category__and'] ) ) { 1284 1302 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) ); 1285 $tax_query[] = array( 1303 sort( $q['category__and'] ); 1304 $tax_query[] = array( 1286 1305 'taxonomy' => 'category', 1287 1306 'terms' => $q['category__and'], … … 1301 1320 if ( '' !== $q['tag'] && ! $this->is_singular && $this->query_vars_changed ) { 1302 1321 if ( str_contains( $q['tag'], ',' ) ) { 1322 // @todo Handle normalizing `tag` query string. 1303 1323 $tags = preg_split( '/[,\r\n\t ]+/', $q['tag'] ); 1304 1324 foreach ( (array) $tags as $tag ) { 1305 1325 $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); 1306 1326 $q['tag_slug__in'][] = $tag; 1327 sort( $q['tag_slug__in'] ); 1307 1328 } 1308 1329 } elseif ( preg_match( '/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) { … … 1315 1336 $q['tag'] = sanitize_term_field( 'slug', $q['tag'], 0, 'post_tag', 'db' ); 1316 1337 $q['tag_slug__in'][] = $q['tag']; 1338 sort( $q['tag_slug__in'] ); 1317 1339 } 1318 1340 } … … 1328 1350 if ( ! empty( $q['tag__in'] ) ) { 1329 1351 $q['tag__in'] = array_map( 'absint', array_unique( (array) $q['tag__in'] ) ); 1330 $tax_query[] = array( 1352 sort( $q['tag__in'] ); 1353 $tax_query[] = array( 1331 1354 'taxonomy' => 'post_tag', 1332 1355 'terms' => $q['tag__in'], … … 1336 1359 if ( ! empty( $q['tag__not_in'] ) ) { 1337 1360 $q['tag__not_in'] = array_map( 'absint', array_unique( (array) $q['tag__not_in'] ) ); 1361 sort( $q['tag__not_in'] ); 1338 1362 $tax_query[] = array( 1339 1363 'taxonomy' => 'post_tag', … … 1345 1369 if ( ! empty( $q['tag__and'] ) ) { 1346 1370 $q['tag__and'] = array_map( 'absint', array_unique( (array) $q['tag__and'] ) ); 1347 $tax_query[] = array( 1371 sort( $q['tag__and'] ); 1372 $tax_query[] = array( 1348 1373 'taxonomy' => 'post_tag', 1349 1374 'terms' => $q['tag__and'], … … 1354 1379 if ( ! empty( $q['tag_slug__in'] ) ) { 1355 1380 $q['tag_slug__in'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) ); 1356 $tax_query[] = array( 1381 sort( $q['tag_slug__in'] ); 1382 $tax_query[] = array( 1357 1383 'taxonomy' => 'post_tag', 1358 1384 'terms' => $q['tag_slug__in'], … … 1363 1389 if ( ! empty( $q['tag_slug__and'] ) ) { 1364 1390 $q['tag_slug__and'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) ); 1365 $tax_query[] = array( 1391 sort( $q['tag_slug__and'] ); 1392 $tax_query[] = array( 1366 1393 'taxonomy' => 'post_tag', 1367 1394 'terms' => $q['tag_slug__and'], … … 2187 2214 } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { 2188 2215 $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); 2189 $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; 2190 $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; 2216 // Duplicate array before sorting to allow for the orderby clause. 2217 $post_name__in_for_where = array_unique( $q['post_name__in'] ); 2218 sort( $post_name__in_for_where ); 2219 $post_name__in = "'" . implode( "','", $post_name__in_for_where ) . "'"; 2220 $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; 2191 2221 } 2192 2222 … … 2200 2230 $where .= " AND {$wpdb->posts}.ID = " . $q['p']; 2201 2231 } elseif ( $q['post__in'] ) { 2202 $post__in = implode( ',', array_map( 'absint', $q['post__in'] ) ); 2232 // Duplicate array before sorting to allow for the orderby clause. 2233 $post__in_for_where = $q['post__in']; 2234 $post__in_for_where = array_unique( array_map( 'absint', $post__in_for_where ) ); 2235 sort( $post__in_for_where ); 2236 $post__in = implode( ',', array_map( 'absint', $post__in_for_where ) ); 2203 2237 $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; 2204 2238 } elseif ( $q['post__not_in'] ) { 2239 sort( $q['post__not_in'] ); 2205 2240 $post__not_in = implode( ',', array_map( 'absint', $q['post__not_in'] ) ); 2206 2241 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; … … 2210 2245 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); 2211 2246 } elseif ( $q['post_parent__in'] ) { 2212 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); 2247 // Duplicate array before sorting to allow for the orderby clause. 2248 $post_parent__in_for_where = $q['post_parent__in']; 2249 $post_parent__in_for_where = array_unique( array_map( 'absint', $post_parent__in_for_where ) ); 2250 sort( $post_parent__in_for_where ); 2251 $post_parent__in = implode( ',', array_map( 'absint', $post_parent__in_for_where ) ); 2213 2252 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; 2214 2253 } elseif ( $q['post_parent__not_in'] ) { 2254 sort( $q['post_parent__not_in'] ); 2215 2255 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) ); 2216 2256 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; … … 2342 2382 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); 2343 2383 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) ); 2384 sort( $authors ); 2344 2385 foreach ( $authors as $author ) { 2345 2386 $key = $author > 0 ? 'author__in' : 'author__not_in'; … … 2350 2391 2351 2392 if ( ! empty( $q['author__not_in'] ) ) { 2352 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); 2393 if ( is_array( $q['author__not_in'] ) ) { 2394 $q['author__not_in'] = array_unique( array_map( 'absint', $q['author__not_in'] ) ); 2395 sort( $q['author__not_in'] ); 2396 } 2397 $author__not_in = implode( ',', (array) $q['author__not_in'] ); 2353 2398 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; 2354 2399 } elseif ( ! empty( $q['author__in'] ) ) { 2400 if ( is_array( $q['author__in'] ) ) { 2401 $q['author__in'] = array_unique( array_map( 'absint', $q['author__in'] ) ); 2402 sort( $q['author__in'] ); 2403 } 2355 2404 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); 2356 2405 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; … … 2589 2638 $q_status = explode( ',', $q_status ); 2590 2639 } 2640 sort( $q_status ); 2591 2641 $r_status = array(); 2592 2642 $p_status = array(); … … 4903 4953 sort( $args['post_type'] ); 4904 4954 4955 /* 4956 * Sort arrays that can be used for ordering prior to cache key generation. 4957 * 4958 * These arrays are sorted in the query generator for the purposes of the 4959 * WHERE clause but the arguments are not modified as they can be used for 4960 * the orderby clase. 4961 * 4962 * Their use in the orderby clause will generate a different SQL query so 4963 * they can be sorted for the cache key generation. 4964 */ 4965 $sortable_arrays_with_int_values = array( 4966 'post__in', 4967 'post_parent__in', 4968 ); 4969 foreach ( $sortable_arrays_with_int_values as $key ) { 4970 if ( isset( $args[ $key ] ) && is_array( $args[ $key ] ) ) { 4971 $args[ $key ] = array_unique( array_map( 'absint', $args[ $key ] ) ); 4972 sort( $args[ $key ] ); 4973 } 4974 } 4975 4976 // Sort and unique the 'post_name__in' for cache key generation. 4977 if ( isset( $args['post_name__in'] ) && is_array( $args['post_name__in'] ) ) { 4978 $args['post_name__in'] = array_unique( $args['post_name__in'] ); 4979 sort( $args['post_name__in'] ); 4980 } 4981 4905 4982 if ( isset( $args['post_status'] ) ) { 4906 4983 $args['post_status'] = (array) $args['post_status']; … … 4943 5020 } 4944 5021 4945 return "wp_query:$key:$last_changed"; 5022 $this->query_cache_key = "wp_query:$key:$last_changed"; 5023 return $this->query_cache_key; 4946 5024 } 4947 5025
Note: See TracChangeset
for help on using the changeset viewer.