Skip to:
Content

bbPress.org

Changeset 6054


Ignore:
Timestamp:
06/02/2016 10:33:05 AM (6 years ago)
Author:
netweb
Message:

Cache: Allow a second parameter to be passed to bbp_clean_post_cache()

Following [5774] WP's clean_post_cache hook can pass both post ID and the post object, this changeset allows that second parmeter to be passed to bbp_clean_post_cache() and updates our add_action call to send both parameters.

Props thebrandonallen.
Fixes #2813.

Location:
trunk/src/includes/core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/includes/core/actions.php

    r6036 r6054  
    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/**
  • trunk/src/includes/core/cache.php

    r5954 r6054  
    131131 *
    132132 * @since 2.1.0 bbPress (r4040)
    133  *
     133 * @since 2.6.0 bbPress (r6053) Introduced the `$post_id` parameter.
     134 *
     135 * @param int     $post_id The post id.
     136 * @param WP_Post $post    The WP_Post object.
     137 *
     138 * @uses get_post() To get the post object.
     139 * @uses bbp_get_forum_post_type() To get the forum post type.
     140 * @uses bbp_get_topic_post_type() To get the topic post type.
     141 * @uses bbp_get_reply_post_type() To get the reply post type.
     142 * @uses wp_cache_delete() To delete the cache item.
     143 * @uses clean_object_term_cache() To clean the term cache.
     144 * @uses bbp_clean_post_cache() Recursion.
    134145 * @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
     146 *
     147 * @return void
    136148 */
    137 function bbp_clean_post_cache( $_post = '' ) {
    138 
    139     // Bail if no post
    140     $_post = get_post( $_post );
    141     if ( empty( $_post ) ) {
     149function bbp_clean_post_cache( $post_id = null, $post = null ) {
     150
     151    // Get the post object.
     152    if ( null !== $post ) {
     153        $post = get_post( $post );
     154    } else {
     155        $post = get_post( $post_id );
     156    }
     157
     158    // Bail if no post.
     159    if ( empty( $post ) ) {
    142160        return;
    143161    }
    144162
    145     // Child query types to clean
     163    // Child query types to clean.
    146164    $post_types = array(
    147165        bbp_get_forum_post_type(),
    148166        bbp_get_topic_post_type(),
    149         bbp_get_reply_post_type()
     167        bbp_get_reply_post_type(),
    150168    );
    151169
    152     // Bail if not a bbPress post type
    153     if ( ! in_array( $_post->post_type, $post_types, true ) ) {
     170    // Bail if not a bbPress post type.
     171    if ( ! in_array( $post->post_type, $post_types, true ) ) {
    154172        return;
    155173    }
    156174
    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 );
    161 
    162     do_action( 'bbp_clean_post_cache', $_post->ID, $_post );
    163 
    164     // Loop through query types and clean caches
     175    // Be sure we haven't recached the post data.
     176    wp_cache_delete( $post->ID, 'posts'    );
     177    wp_cache_delete( $post->ID, 'post_meta' );
     178
     179    // Clean the term cache for the given post.
     180    clean_object_term_cache( $post->ID, $post->post_type );
     181
     182    // Loop through query types and clean caches.
    165183    foreach ( $post_types as $post_type ) {
    166         wp_cache_delete( 'bbp_parent_all_'    . $_post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
    167     }
    168 
    169     // Invalidate parent caches
    170     if ( ! empty( $_post->post_parent ) ) {
    171         bbp_clean_post_cache( $_post->post_parent );
     184        wp_cache_delete( 'bbp_parent_all_'    . $post->ID . '_type_' . $post_type . '_child_ids',     'bbpress_posts' );
     185    }
     186
     187    /**
     188     * Fires immediately after the given post's cache is cleaned.
     189     *
     190     * @since 2.1.0
     191     *
     192     * @param int     $post_id Post ID.
     193     * @param WP_Post $post    Post object.
     194     */
     195    do_action( 'bbp_clean_post_cache', $post->ID, $post );
     196
     197    // Invalidate parent caches.
     198    if ( ! empty( $post->post_parent ) ) {
     199        bbp_clean_post_cache( $post->post_parent );
    172200    }
    173201}
Note: See TracChangeset for help on using the changeset viewer.