diff --git src/wp-includes/query.php src/wp-includes/query.php
index 053f4e9..b8c245f 100644
|
|
|
class WP_Query { |
| 2332 | 2332 | $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); |
| 2333 | 2333 | } |
| 2334 | 2334 | |
| | 2335 | // Sanitize RAND() with a seed value and add to allowed keys. |
| | 2336 | $rand_with_seed = false; |
| | 2337 | if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { |
| | 2338 | $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) ); |
| | 2339 | $allowed_keys[] = $orderby; |
| | 2340 | $rand_with_seed = true; |
| | 2341 | } |
| | 2342 | |
| 2335 | 2343 | if ( ! in_array( $orderby, $allowed_keys, true ) ) { |
| 2336 | 2344 | return false; |
| 2337 | 2345 | } |
| … |
… |
class WP_Query { |
| 2368 | 2376 | // $orderby corresponds to a meta_query clause. |
| 2369 | 2377 | $meta_clause = $meta_clauses[ $orderby ]; |
| 2370 | 2378 | $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})"; |
| | 2379 | } elseif ( $rand_with_seed ) { |
| | 2380 | $orderby_clause = $orderby; |
| 2371 | 2381 | } else { |
| 2372 | 2382 | // Default: order by post field. |
| 2373 | 2383 | $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby ); |
diff --git tests/phpunit/tests/post/query.php tests/phpunit/tests/post/query.php
index bc0e865..3571538 100644
|
|
|
class Tests_Post_Query extends WP_UnitTestCase { |
| 304 | 304 | } |
| 305 | 305 | |
| 306 | 306 | /** |
| | 307 | * @ticket 35692 |
| | 308 | */ |
| | 309 | public function test_orderby_rand_with_seed() { |
| | 310 | $q = new WP_Query( array( |
| | 311 | 'orderby' => 'RAND(5)', |
| | 312 | ) ); |
| | 313 | |
| | 314 | $this->assertContains( 'ORDER BY RAND(5)', $q->request ); |
| | 315 | } |
| | 316 | |
| | 317 | /** |
| | 318 | * @ticket 35692 |
| | 319 | */ |
| | 320 | public function test_orderby_rand_should_ignore_invalid_seed() { |
| | 321 | $q = new WP_Query( array( |
| | 322 | 'orderby' => 'RAND(foo)', |
| | 323 | ) ); |
| | 324 | |
| | 325 | $this->assertNotContains( 'ORDER BY RAND', $q->request ); |
| | 326 | } |
| | 327 | |
| | 328 | /** |
| | 329 | * @ticket 35692 |
| | 330 | */ |
| | 331 | public function test_orderby_rand_with_seed_should_be_case_insensitive() { |
| | 332 | $q = new WP_Query( array( |
| | 333 | 'orderby' => 'rand(5)', |
| | 334 | ) ); |
| | 335 | |
| | 336 | $this->assertContains( 'ORDER BY RAND(5)', $q->request ); |
| | 337 | } |
| | 338 | |
| | 339 | /** |
| 307 | 340 | * Tests the post_name__in attribute of WP_Query. |
| 308 | 341 | * |
| 309 | 342 | * @ticket 33065 |