From 1d3511ba8c8dd6fc71ee8f15de1b0e8d9e453dc3 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 25 May 2016 18:29:01 +0000 Subject: [PATCH] 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 --- src/wp-includes/bookmark.php | 9 +++++--- tests/phpunit/tests/bookmark/getBookmarks.php | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/bookmark.php b/src/wp-includes/bookmark.php index d2b98d9065..85142b2630 100644 --- a/src/wp-includes/bookmark.php +++ b/src/wp-includes/bookmark.php @@ -128,7 +128,8 @@ function get_bookmarks( $args = '' ) { $r = wp_parse_args( $args, $defaults ); $key = md5( serialize( $r ) ); - if ( $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { + $cache = false; + if ( 'rand' !== $r['orderby'] && $cache = wp_cache_get( 'get_bookmarks', 'bookmark' ) ) { if ( is_array( $cache ) && isset( $cache[ $key ] ) ) { $bookmarks = $cache[ $key ]; /** @@ -285,8 +286,10 @@ function get_bookmarks( $args = '' ) { $results = $wpdb->get_results( $query ); - $cache[ $key ] = $results; - wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); + if ( 'rand()' !== $orderby ) { + $cache[ $key ] = $results; + wp_cache_set( 'get_bookmarks', $cache, 'bookmark' ); + } /** This filter is documented in wp-includes/bookmark.php */ return apply_filters( 'get_bookmarks', $results, $r ); diff --git a/tests/phpunit/tests/bookmark/getBookmarks.php b/tests/phpunit/tests/bookmark/getBookmarks.php index accc3af72e..14407d69fd 100644 --- a/tests/phpunit/tests/bookmark/getBookmarks.php +++ b/tests/phpunit/tests/bookmark/getBookmarks.php @@ -47,4 +47,27 @@ class Tests_Bookmark_GetBookmarks extends WP_UnitTestCase { $this->assertEqualSets( $bookmarks, wp_list_pluck( $found2, 'link_id' ) ); $this->assertTrue( $num_queries < $wpdb->num_queries ); } + + /** + * @ticket 18356 + */ + public function test_orderby_rand_should_not_be_cached() { + global $wpdb; + + $bookmarks = self::factory()->bookmark->create_many( 2 ); + + $found1 = get_bookmarks( array( + 'orderby' => 'rand', + ) ); + + $num_queries = $wpdb->num_queries; + + $found2 = get_bookmarks( array( + 'orderby' => 'rand', + ) ); + + // equal sets != same order + $this->assertEqualSets( $found1, $found2 ); + $this->assertTrue( $num_queries < $wpdb->num_queries ); + } }