Skip to:
Content

bbPress.org

Ticket #2813: 2813.02.patch

File 2813.02.patch, 5.0 KB (added by thebrandonallen, 6 years ago)
  • src/includes/core/actions.php

    diff --git src/includes/core/actions.php src/includes/core/actions.php
    index c9efadb..04ca341 100644
    add_action( 'bbp_new_reply_pre_extras', 'bbp_clean_post_cache' ); 
    340340add_action( 'bbp_new_reply_post_extras', 'bbp_clean_post_cache' );
    341341
    342342// Clean bbPress post caches when WordPress's is cleaned
    343 add_action( 'clean_post_cache', 'bbp_clean_post_cache' );
     343add_action( 'clean_post_cache', 'bbp_clean_post_cache', 10, 2 );
    344344
    345345/**
    346346 * bbPress needs to redirect the user around in a few different circumstances:
  • src/includes/core/cache.php

    diff --git src/includes/core/cache.php src/includes/core/cache.php
    index db1ba6c..300bc66 100644
    new BBP_Skip_Children(); 
    131131 *
    132132 * @since 2.1.0 bbPress (r4040)
    133133 *
     134 * @param int     $post_id The post id.
     135 * @param WP_Post $post    The WP_Post object.
     136 *
     137 * @uses get_post() To get the post object.
     138 * @uses bbp_get_forum_post_type() To get the forum post type.
     139 * @uses bbp_get_topic_post_type() To get the topic post type.
     140 * @uses bbp_get_reply_post_type() To get the reply post type.
     141 * @uses wp_cache_delete() To delete the cache item.
     142 * @uses clean_object_term_cache() To clean the term cache.
     143 * @uses bbp_clean_post_cache() Recursion.
    134144 * @uses do_action() Calls 'bbp_clean_post_cache' on $id
    135  * @param object|int $_post The post object or ID to remove from the cache
     145 *
     146 * @return void
    136147 */
    137 function bbp_clean_post_cache( $_post = '' ) {
     148function bbp_clean_post_cache( $post_id = null, $post = null ) {
    138149
    139         // Bail if no post
    140         $_post = get_post( $_post );
    141         if ( empty( $_post ) ) {
     150        // Get the post object.
     151        if ( null !== $post ) {
     152                $post = get_post( $post );
     153        } else {
     154                $post = get_post( $post_id );
     155        }
     156
     157        // Bail if no post.
     158        if ( empty( $post ) ) {
    142159                return;
    143160        }
    144161
    145         // Child query types to clean
     162        // Child query types to clean.
    146163        $post_types = array(
    147164                bbp_get_forum_post_type(),
    148165                bbp_get_topic_post_type(),
    149                 bbp_get_reply_post_type()
     166                bbp_get_reply_post_type(),
    150167        );
    151168
    152         // Bail if not a bbPress post type
    153         if ( ! in_array( $_post->post_type, $post_types, true ) ) {
     169        // Bail if not a bbPress post type.
     170        if ( ! in_array( $post->post_type, $post_types, true ) ) {
    154171                return;
    155172        }
    156173
    157         wp_cache_delete( $_post->ID, 'posts'     );
    158         wp_cache_delete( $_post->ID, 'post_meta' );
    159 
    160         clean_object_term_cache( $_post->ID, $_post->post_type );
     174        // Be sure we haven't recached the post data.
     175        wp_cache_delete( $post->ID, 'posts'     );
     176        wp_cache_delete( $post->ID, 'post_meta' );
    161177
    162         do_action( 'bbp_clean_post_cache', $_post->ID, $_post );
     178        // Clean the term cache for the given post.
     179        clean_object_term_cache( $post->ID, $post->post_type );
    163180
    164         // Loop through query types and clean caches
     181        // Loop through query types and clean caches.
    165182        foreach ( $post_types as $post_type ) {
    166                 wp_cache_delete( 'bbp_parent_all_'    . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
     183                wp_cache_delete( 'bbp_parent_all_'    . $post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
    167184        }
    168185
    169         // Invalidate parent caches
    170         if ( ! empty( $_post->post_parent ) ) {
    171                 bbp_clean_post_cache( $_post->post_parent );
     186        /**
     187         * Fires immediately after the given post's cache is cleaned.
     188         *
     189         * @since 2.1.0
     190         *
     191         * @param int     $post_id Post ID.
     192         * @param WP_Post $post    Post object.
     193         */
     194        do_action( 'bbp_clean_post_cache', $post->ID, $post );
     195
     196        // Invalidate parent caches.
     197        if ( ! empty( $post->post_parent ) ) {
     198                bbp_clean_post_cache( $post->post_parent );
    172199        }
    173200}
  • new file tests/phpunit/testcases/core/cache.php

    diff --git tests/phpunit/testcases/core/cache.php tests/phpunit/testcases/core/cache.php
    new file mode 100644
    index 0000000..5d7eefa
    - +  
     1<?php
     2
     3/**
     4 * @group cache
     5 */
     6class BBP_Core_Cache_Tests extends BBP_UnitTestCase {
     7
     8        /**
     9         * @covers ::bbp_clean_post_cache
     10         */
     11        public function test_bbp_clean_post_cache() {
     12
     13                // Get the topic post type.
     14                $tpt = bbp_get_topic_post_type();
     15
     16                // Set up a forum with 1 topic and 1 reply to that topic.
     17                $f = $this->factory->forum->create();
     18                $t = $this->factory->topic->create( array(
     19                        'post_parent' => $f,
     20                        'topic_meta' => array(
     21                                'forum_id' => $f,
     22                        ),
     23                ) );
     24                $r = $this->factory->topic->create( array(
     25                        'post_parent' => $t,
     26                        'reply_meta' => array(
     27                                'forum_id' => $f,
     28                                'topic_id' => $t,
     29                        ),
     30                ) );
     31
     32                // Make sure we've cached some data.
     33                bbp_get_all_child_ids( $f, $tpt );
     34                bbp_get_all_child_ids( $t, $tpt );
     35
     36                $this->assertEquals( array( $t ), wp_cache_get( "bbp_parent_all_{$f}_type_{$tpt}_child_ids", 'bbpress_posts' ) );
     37                $this->assertEquals( array( $r ), wp_cache_get( "bbp_parent_all_{$t}_type_{$tpt}_child_ids", 'bbpress_posts' ) );
     38
     39                // Clean the cache.
     40                bbp_clean_post_cache( $r );
     41
     42                $this->assertEquals( false, wp_cache_get( "bbp_parent_all_{$f}_type_{$tpt}_child_ids", 'bbpress_posts' ) );
     43                $this->assertEquals( false, wp_cache_get( "bbp_parent_all_{$t}_type_{$tpt}_child_ids", 'bbpress_posts' ) );
     44        }
     45}