PluginProbe
bbPress / 2.6.17
bbPress v2.6.17
2.6.17 trunk 2.0 2.0-beta-1 2.0-beta-2b 2.0-beta-3 2.0-beta-3b 2.0-rc-2 2.0-rc-3 2.0-rc-4 2.0-rc-5 2.0.1 2.0.2 2.0.3 2.1 2.1-beta-1 2.1-rc1 2.1-rc2 2.1-rc3 2.1-rc4 2.1.1 2.1.2 2.1.3 2.2 2.2.1 All 72 releases
bbpress / includes / core / cache.php

cache.php in bbPress 2.6.17, at includes/core/cache.php

169 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Legacy workaround to skip invalidation of child posts when editing a parent.
22 *
23 * Older WordPress versions invalidated child post caches when updating a parent,
24 * which could clear thousands of topic and reply caches during a single edit.
25 * Supported WordPress versions no longer need this workaround.
26 *
27 * @since 2.1.0 bbPress (r4011)
28 * @deprecated 2.7.0 WordPress no longer invalidates child post caches on update.
29 * Retained for compatibility; bbPress no longer instantiates it.
30 *
31 * @package bbPress
32 * @subpackage Cache
33 */
34 class BBP_Skip_Children {
35
36 /**
37 * @var int Post ID being updated
38 */
39 private $updating_post = 0;
40
41 /**
42 * @var bool The original value of $_wp_suspend_cache_invalidation global
43 */
44 private $original_cache_invalidation = false;
45
46 /** Methods ***************************************************************/
47
48 /**
49 * Hook into the 'pre_post_update' action.
50 *
51 * @since 2.1.0 bbPress (r4011)
52 */
53 public function __construct() {
54 add_action( 'pre_post_update', array( $this, 'pre_post_update' ) );
55 }
56
57 /**
58 * Only clean post caches for main bbPress posts.
59 *
60 * Check that the post being updated is a bbPress post type, saves the
61 * post ID to be used later, and adds an action to 'clean_post_cache' that
62 * prevents child post caches from being cleared.
63 *
64 * @since 2.1.0 bbPress (r4011)
65 *
66 * @param int $post_id The post ID being updated
67 * @return If invalid post data
68 */
69 public function pre_post_update( $post_id = 0 ) {
70
71 // Bail if post ID is not a bbPress post type
72 if ( empty( $post_id ) || ! bbp_is_custom_post_type( $post_id ) ) {
73 return;
74 }
75
76 // Store the $post_id
77 $this->updating_post = $post_id;
78
79 // Skip related post cache invalidation. This prevents invalidating the
80 // caches of the child posts when there is no reason to do so.
81 add_action( 'clean_post_cache', array( $this, 'skip_related_posts' ) );
82 }
83
84 /**
85 * Skip cache invalidation of related posts if the post ID being invalidated
86 * is not the one that was just updated.
87 *
88 * @since 2.1.0 bbPress (r4011)
89 *
90 * @param int $post_id The post ID of the cache being invalidated
91 * @return If invalid post data
92 */
93 public function skip_related_posts( $post_id = 0 ) {
94
95 // Bail if this post is not the current bbPress post
96 if ( empty( $post_id ) || ( $this->updating_post !== $post_id ) ) {
97 return;
98 }
99
100 // Stash the current cache invalidation value in a variable, so we can
101 // restore back to it nicely in the future.
102 global $_wp_suspend_cache_invalidation;
103
104 $this->original_cache_invalidation = $_wp_suspend_cache_invalidation;
105
106 // Turn off cache invalidation
107 wp_suspend_cache_invalidation( true );
108
109 // Restore cache invalidation
110 add_action( 'wp_insert_post', array( $this, 'restore_cache_invalidation' ) );
111 }
112
113 /**
114 * Restore the cache invalidation to its previous value.
115 *
116 * @since 2.1.0 bbPress (r4011)
117 */
118 public function restore_cache_invalidation() {
119 wp_suspend_cache_invalidation( $this->original_cache_invalidation );
120 }
121 }
122
123 /** General *******************************************************************/
124
125 /**
126 * Will clean a post in the cache.
127 *
128 * Will call to clean the term object cache associated with the post ID.
129 *
130 * @since 2.1.0 bbPress (r4040)
131 * @since 2.6.0 bbPress (r6053) Introduced the `$post_id` parameter.
132 *
133 * @param int $post_id The post id.
134 * @param WP_Post $post The WP_Post object.
135 */
136 function bbp_clean_post_cache( $post_id = null, $post = null ) {
137
138 // Child query types to clean
139 $post_types = array(
140 bbp_get_forum_post_type(),
141 bbp_get_topic_post_type(),
142 bbp_get_reply_post_type()
143 );
144
145 // Bail if not a bbPress post type
146 if ( ! in_array( $post->post_type, $post_types, true ) ) {
147 return;
148 }
149
150 /**
151 * Fires immediately after the given post cache is cleaned.
152 *
153 * @since 2.1.0
154 *
155 * @param int $post_id Post ID.
156 * @param WP_Post $post Post object.
157 */
158 do_action( 'bbp_clean_post_cache', $post->ID, $post );
159
160 // Invalidate parent caches
161 if ( ! empty( $post->post_parent ) ) {
162 clean_post_cache( $post->post_parent );
163
164 // Only bump `last_changed` when forum-root is reached
165 } else {
166 wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );
167 }
168 }
169