| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* bbPress Cache Helpers. |
| 5 |
* |
| 6 |
* Helper functions used to communicate with WordPress's various caches. Many |
| 7 |
* of these functions are used to work around specific WordPress nuances. They |
| 8 |
* are subject to changes, tweaking, and will need iteration as performance |
| 9 |
* improvements are made to WordPress core. |
| 10 |
* |
| 11 |
* @package bbPress |
| 12 |
* @subpackage Cache |
| 13 |
*/ |
| 14 |
|
| 15 |
// Exit if accessed directly |
| 16 |
defined( 'ABSPATH' ) || exit; |
| 17 |
|
| 18 |
/** Helpers *******************************************************************/ |
| 19 |
|
| 20 |
/** |
| 21 |
* Skip invalidation of child post content when editing a parent. |
| 22 |
* |
| 23 |
* This prevents invalidating caches for topics and replies when editing a forum |
| 24 |
* or a topic. Without this in place, WordPress will attempt to invalidate all |
| 25 |
* child posts whenever a parent post is modified. This can cause thousands of |
| 26 |
* cache invalidations to occur on a single edit, which is no good for anyone. |
| 27 |
* |
| 28 |
* @since 2.1.0 bbPress (r4011) |
| 29 |
* |
| 30 |
* @package bbPress |
| 31 |
* @subpackage Cache |
| 32 |
*/ |
| 33 |
class BBP_Skip_Children { |
| 34 |
|
| 35 |
/** |
| 36 |
* @var int Post ID being updated. |
| 37 |
*/ |
| 38 |
private $updating_post = 0; |
| 39 |
|
| 40 |
/** |
| 41 |
* @var bool The original value of $_wp_suspend_cache_invalidation global. |
| 42 |
*/ |
| 43 |
private $original_cache_invalidation = false; |
| 44 |
|
| 45 |
/** Methods ***************************************************************/ |
| 46 |
|
| 47 |
/** |
| 48 |
* Hook into the 'pre_post_update' action. |
| 49 |
* |
| 50 |
* @since 2.1.0 bbPress (r4011) |
| 51 |
*/ |
| 52 |
public function __construct() { |
| 53 |
add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Only clean post caches for main bbPress posts. |
| 58 |
* |
| 59 |
* Check that the post being updated is a bbPress post type, saves the |
| 60 |
* post ID to be used later, and adds an action to 'clean_post_cache' that |
| 61 |
* prevents child post caches from being cleared. |
| 62 |
* |
| 63 |
* @since 2.1.0 bbPress (r4011) |
| 64 |
* |
| 65 |
* @param int $post_id The post ID being updated. |
| 66 |
*/ |
| 67 |
public function pre_post_update( $post_id = 0 ) { |
| 68 |
|
| 69 |
// Bail if post ID is not a bbPress post type |
| 70 |
if ( empty( $post_id ) || ! bbp_is_custom_post_type( $post_id ) ) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
// Store the $post_id |
| 75 |
$this->updating_post = $post_id; |
| 76 |
|
| 77 |
// Skip related post cache invalidation. This prevents invalidating the |
| 78 |
// caches of the child posts when there is no reason to do so. |
| 79 |
add_action( 'clean_post_cache', array( $this, 'skip_related_posts' ) ); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Skip cache invalidation of related posts if the post ID being invalidated |
| 84 |
* is not the one that was just updated. |
| 85 |
* |
| 86 |
* @since 2.1.0 bbPress (r4011) |
| 87 |
* |
| 88 |
* @param int $post_id The post ID of the cache being invalidated. |
| 89 |
*/ |
| 90 |
public function skip_related_posts( $post_id = 0 ) { |
| 91 |
|
| 92 |
// Bail if this post is not the current bbPress post |
| 93 |
if ( empty( $post_id ) || ( $this->updating_post !== $post_id ) ) { |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// Stash the current cache invalidation value in a variable, so we can |
| 98 |
// restore back to it nicely in the future. |
| 99 |
global $_wp_suspend_cache_invalidation; |
| 100 |
|
| 101 |
$this->original_cache_invalidation = $_wp_suspend_cache_invalidation; |
| 102 |
|
| 103 |
// Turn off cache invalidation |
| 104 |
wp_suspend_cache_invalidation( true ); |
| 105 |
|
| 106 |
// Restore cache invalidation |
| 107 |
add_action( 'wp_insert_post', array( $this, 'restore_cache_invalidation' ) ); |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Restore the cache invalidation to its previous value. |
| 112 |
* |
| 113 |
* @since 2.1.0 bbPress (r4011) |
| 114 |
*/ |
| 115 |
public function restore_cache_invalidation() { |
| 116 |
wp_suspend_cache_invalidation( $this->original_cache_invalidation ); |
| 117 |
} |
| 118 |
} |
| 119 |
new BBP_Skip_Children(); |
| 120 |
|
| 121 |
/** General *******************************************************************/ |
| 122 |
|
| 123 |
/** |
| 124 |
* Will clean a post in the cache. |
| 125 |
* |
| 126 |
* Will call to clean the term object cache associated with the post ID. |
| 127 |
* |
| 128 |
* @since 2.1.0 bbPress (r4040) |
| 129 |
* @since 2.6.0 bbPress (r6053) Introduced the `$post_id` parameter. |
| 130 |
* |
| 131 |
* @param int $post_id The post id. |
| 132 |
* @param WP_Post $post The WP_Post object. |
| 133 |
*/ |
| 134 |
function bbp_clean_post_cache( $post_id = null, $post = null ) { |
| 135 |
|
| 136 |
// Child query types to clean |
| 137 |
$post_types = array( |
| 138 |
bbp_get_forum_post_type(), |
| 139 |
bbp_get_topic_post_type(), |
| 140 |
bbp_get_reply_post_type() |
| 141 |
); |
| 142 |
|
| 143 |
// Bail if not a bbPress post type |
| 144 |
if ( ! in_array( $post->post_type, $post_types, true ) ) { |
| 145 |
return; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Fires immediately after the given post cache is cleaned. |
| 150 |
* |
| 151 |
* @since 2.1.0 |
| 152 |
* |
| 153 |
* @param int $post_id Post ID. |
| 154 |
* @param WP_Post $post Post object. |
| 155 |
*/ |
| 156 |
do_action( 'bbp_clean_post_cache', $post->ID, $post ); |
| 157 |
|
| 158 |
// Invalidate parent caches |
| 159 |
if ( ! empty( $post->post_parent ) ) { |
| 160 |
clean_post_cache( $post->post_parent ); |
| 161 |
|
| 162 |
// Only bump `last_changed` when forum-root is reached |
| 163 |
} else { |
| 164 |
wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' ); |
| 165 |
} |
| 166 |
} |
| 167 |
|