Skip to content

Commit 4eb3b3d

Browse files
author
boonebgorges
committed
In get_bookmarks(), don't cache if 'orderby=rand'.
Props lukecavanagh, prettyboymp, c3mdigital, MikeHansenMe. Fixes #18356. git-svn-id: https://develop.svn.wordpress.org/trunk@37565 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 6d5f877 commit 4eb3b3d

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

src/wp-includes/bookmark.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ function get_bookmarks( $args = '' ) {
128128
$r = wp_parse_args( $args, $defaults );
129129

130130
$key = md5( serialize( $r ) );
131-
if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
131+
$cache = false;
132+
if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) {
132133
if ( is_array( $cache ) && isset( $cache[ $key ] ) ) {
133134
$bookmarks = $cache[ $key ];
134135
/**
@@ -285,8 +286,10 @@ function get_bookmarks( $args = '' ) {
285286

286287
$results = $wpdb->get_results( $query );
287288

288-
$cache[ $key ] = $results;
289-
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
289+
if ( 'rand()' !== $orderby ) {
290+
$cache[ $key ] = $results;
291+
wp_cache_set( 'get_bookmarks', $cache, 'bookmark' );
292+
}
290293

291294
/** This filter is documented in wp-includes/bookmark.php */
292295
return apply_filters( 'get_bookmarks', $results, $r );

tests/phpunit/tests/bookmark/getBookmarks.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,27 @@ public function test_adding_bookmark_should_bust_get_bookmarks_cache() {
4747
$this->assertEqualSets( $bookmarks, wp_list_pluck( $found2, 'link_id' ) );
4848
$this->assertTrue( $num_queries < $wpdb->num_queries );
4949
}
50+
51+
/**
52+
* @ticket 18356
53+
*/
54+
public function test_orderby_rand_should_not_be_cached() {
55+
global $wpdb;
56+
57+
$bookmarks = self::factory()->bookmark->create_many( 2 );
58+
59+
$found1 = get_bookmarks( array(
60+
'orderby' => 'rand',
61+
) );
62+
63+
$num_queries = $wpdb->num_queries;
64+
65+
$found2 = get_bookmarks( array(
66+
'orderby' => 'rand',
67+
) );
68+
69+
// equal sets != same order
70+
$this->assertEqualSets( $found1, $found2 );
71+
$this->assertTrue( $num_queries < $wpdb->num_queries );
72+
}
5073
}

0 commit comments

Comments
 (0)