Make WordPress Core

Changeset 41849


Ignore:
Timestamp:
10/12/2017 03:19:30 PM (7 years ago)
Author:
boonebgorges
Message:

Bump 'posts' query cache incrementor when modifying postmeta.

This ensures that the get_pages() query cache doesn't go stale when
postmeta is modified.

Props spacedmonkey.
Fixes #40669.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/post.php

    r41688 r41849  
    17291729        $post_id = $the_post;
    17301730
    1731     return add_metadata('post', $post_id, $meta_key, $meta_value, $unique);
     1731    $added = add_metadata( 'post', $post_id, $meta_key, $meta_value, $unique );
     1732    if ( $added ) {
     1733        wp_cache_set( 'last_changed', microtime(), 'posts' );
     1734    }
     1735    return $added;
    17321736}
    17331737
     
    17521756        $post_id = $the_post;
    17531757
    1754     return delete_metadata('post', $post_id, $meta_key, $meta_value);
     1758    $deleted = delete_metadata( 'post', $post_id, $meta_key, $meta_value );
     1759    if ( $deleted ) {
     1760        wp_cache_set( 'last_changed', microtime(), 'posts' );
     1761    }
     1762    return $deleted;
    17551763}
    17561764
     
    17941802        $post_id = $the_post;
    17951803
    1796     return update_metadata('post', $post_id, $meta_key, $meta_value, $prev_value);
     1804    $updated = update_metadata( 'post', $post_id, $meta_key, $meta_value, $prev_value );
     1805    if ( $updated ) {
     1806        wp_cache_set( 'last_changed', microtime(), 'posts' );
     1807    }
     1808    return $updated;
    17971809}
    17981810
     
    18061818 */
    18071819function delete_post_meta_by_key( $post_meta_key ) {
    1808     return delete_metadata( 'post', null, $post_meta_key, '', true );
     1820    $deleted = delete_metadata( 'post', null, $post_meta_key, '', true );
     1821    if ( $deleted ) {
     1822        wp_cache_set( 'last_changed', microtime(), 'posts' );
     1823    }
     1824    return $deleted;
    18091825}
    18101826
  • trunk/tests/phpunit/tests/post/getPages.php

    r35246 r41849  
    9797
    9898    /**
     99     * @ticket 40669
     100     */
     101    public function test_cache_should_be_invalidated_by_add_post_meta() {
     102        $posts = self::factory()->post->create_many( 2, array(
     103            'post_type' => 'page',
     104        ) );
     105
     106        add_post_meta( $posts[0], 'foo', 'bar' );
     107
     108        $cached = get_pages( array(
     109            'meta_key' => 'foo',
     110            'meta_value' => 'bar',
     111        ) );
     112
     113        $cached_ids = wp_list_pluck( $cached, 'ID' );
     114        $this->assertEqualSets( array( $posts[0] ), $cached_ids );
     115
     116        add_post_meta( $posts[1], 'foo', 'bar' );
     117
     118        $found = get_pages( array(
     119            'meta_key' => 'foo',
     120            'meta_value' => 'bar',
     121        ) );
     122
     123        $found_ids = wp_list_pluck( $found, 'ID' );
     124        $this->assertEqualSets( $posts, $found_ids );
     125    }
     126
     127    /**
     128     * @ticket 40669
     129     */
     130    public function test_cache_should_be_invalidated_by_update_post_meta() {
     131        $posts = self::factory()->post->create_many( 2, array(
     132            'post_type' => 'page',
     133        ) );
     134
     135        add_post_meta( $posts[0], 'foo', 'bar' );
     136        add_post_meta( $posts[1], 'foo', 'bar' );
     137
     138        $cached = get_pages( array(
     139            'meta_key' => 'foo',
     140            'meta_value' => 'bar',
     141        ) );
     142
     143        $cached_ids = wp_list_pluck( $cached, 'ID' );
     144        $this->assertEqualSets( $posts, $cached_ids );
     145
     146        update_post_meta( $posts[1], 'foo', 'baz' );
     147
     148        $found = get_pages( array(
     149            'meta_key' => 'foo',
     150            'meta_value' => 'bar',
     151        ) );
     152
     153        $found_ids = wp_list_pluck( $found, 'ID' );
     154        $this->assertEqualSets( array( $posts[0] ), $found_ids );
     155    }
     156
     157    /**
     158     * @ticket 40669
     159     */
     160    public function test_cache_should_be_invalidated_by_delete_post_meta() {
     161        $posts = self::factory()->post->create_many( 2, array(
     162            'post_type' => 'page',
     163        ) );
     164
     165        add_post_meta( $posts[0], 'foo', 'bar' );
     166        add_post_meta( $posts[1], 'foo', 'bar' );
     167
     168        $cached = get_pages( array(
     169            'meta_key' => 'foo',
     170            'meta_value' => 'bar',
     171        ) );
     172
     173        $cached_ids = wp_list_pluck( $cached, 'ID' );
     174        $this->assertEqualSets( $posts, $cached_ids );
     175
     176        delete_post_meta( $posts[1], 'foo' );
     177
     178        $found = get_pages( array(
     179            'meta_key' => 'foo',
     180            'meta_value' => 'bar',
     181        ) );
     182
     183        $found_ids = wp_list_pluck( $found, 'ID' );
     184        $this->assertEqualSets( array( $posts[0] ), $found_ids );
     185    }
     186
     187    /**
     188     * @ticket 40669
     189     */
     190    public function test_cache_should_be_invalidated_by_delete_post_meta_by_key() {
     191        $posts = self::factory()->post->create_many( 2, array(
     192            'post_type' => 'page',
     193        ) );
     194
     195        add_post_meta( $posts[0], 'foo', 'bar' );
     196        add_post_meta( $posts[1], 'foo', 'bar' );
     197
     198        $cached = get_pages( array(
     199            'meta_key' => 'foo',
     200            'meta_value' => 'bar',
     201        ) );
     202
     203        $cached_ids = wp_list_pluck( $cached, 'ID' );
     204        $this->assertEqualSets( $posts, $cached_ids );
     205
     206        delete_post_meta_by_key( 'foo' );
     207
     208        $found = get_pages( array(
     209            'meta_key' => 'foo',
     210            'meta_value' => 'bar',
     211        ) );
     212
     213        $found_ids = wp_list_pluck( $found, 'ID' );
     214        $this->assertEqualSets( array(), $found_ids );
     215    }
     216
     217    /**
    99218     * @ticket 20376
    100219     */
Note: See TracChangeset for help on using the changeset viewer.