/includes/core/cache.php

  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. if ( !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 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 bbPress (r4011) 
  51. */ 
  52. public function __construct() { 
  53. add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); 
  54.  
  55. /** 
  56. * Only clean post caches for main bbPress posts. 
  57. * 
  58. * Check that the post being updated is a bbPress post type, saves the 
  59. * post ID to be used later, and adds an action to 'clean_post_cache' that 
  60. * prevents child post caches from being cleared. 
  61. * 
  62. * @since bbPress (r4011) 
  63. * 
  64. * @param int $post_id The post ID being updated 
  65. * @return If invalid post data 
  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. // Store the $post_id 
  74. $this->updating_post = $post_id; 
  75.  
  76. // Skip related post cache invalidation. This prevents invalidating the 
  77. // caches of the child posts when there is no reason to do so. 
  78. add_action( 'clean_post_cache', array( $this, 'skip_related_posts' ) ); 
  79.  
  80. /** 
  81. * Skip cache invalidation of related posts if the post ID being invalidated 
  82. * is not the one that was just updated. 
  83. * 
  84. * @since bbPress (r4011) 
  85. * 
  86. * @param int $post_id The post ID of the cache being invalidated 
  87. * @return If invalid post data 
  88. */ 
  89. public function skip_related_posts( $post_id = 0 ) { 
  90.  
  91. // Bail if this post is not the current bbPress post 
  92. if ( empty( $post_id ) || ( $this->updating_post !== $post_id ) ) 
  93. return; 
  94.  
  95. // Stash the current cache invalidation value in a variable, so we can 
  96. // restore back to it nicely in the future. 
  97. global $_wp_suspend_cache_invalidation; 
  98.  
  99. $this->original_cache_invalidation = $_wp_suspend_cache_invalidation; 
  100.  
  101. // Turn off cache invalidation 
  102. wp_suspend_cache_invalidation( true ); 
  103.  
  104. // Restore cache invalidation 
  105. add_action( 'wp_insert_post', array( $this, 'restore_cache_invalidation' ) ); 
  106.  
  107. /** 
  108. * Restore the cache invalidation to its previous value. 
  109. * 
  110. * @since bbPress (r4011) 
  111. * @uses wp_suspend_cache_invalidation() 
  112. */ 
  113. public function restore_cache_invalidation() { 
  114. wp_suspend_cache_invalidation( $this->original_cache_invalidation ); 
  115. new BBP_Skip_Children(); 
  116.  
  117. /** General *******************************************************************/ 
  118.  
  119. /** 
  120. * Will clean a post in the cache. 
  121. * 
  122. * Will call to clean the term object cache associated with the post ID. 
  123. * 
  124. * @since bbPress (r4040) 
  125. * 
  126. * @uses do_action() Calls 'bbp_clean_post_cache' on $id 
  127. * @param object|int $_post The post object or ID to remove from the cache 
  128. */ 
  129. function bbp_clean_post_cache( $_post = '' ) { 
  130.  
  131. // Bail if no post 
  132. $_post = get_post( $_post ); 
  133. if ( empty( $_post ) ) 
  134. return; 
  135.  
  136. wp_cache_delete( $_post->ID, 'posts' ); 
  137. wp_cache_delete( $_post->ID, 'post_meta' ); 
  138.  
  139. clean_object_term_cache( $_post->ID, $_post->post_type ); 
  140.  
  141. do_action( 'bbp_clean_post_cache', $_post->ID, $_post ); 
  142.  
  143. // Child query types to clean 
  144. $post_types = array( 
  145. bbp_get_topic_post_type(),  
  146. bbp_get_forum_post_type(),  
  147. bbp_get_reply_post_type() 
  148. ); 
  149.  
  150. // Loop through query types and clean caches 
  151. foreach ( $post_types as $post_type ) { 
  152. wp_cache_delete( 'bbp_get_forum_' . $_post->ID . '_reply_id', 'bbpress_posts' ); 
  153. wp_cache_delete( 'bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_last_id', 'bbpress_posts' ); 
  154. wp_cache_delete( 'bbp_parent_' . $_post->ID . '_type_' . $post_type . '_child_count', 'bbpress_posts' ); 
  155. wp_cache_delete( 'bbp_parent_public_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress_posts' ); 
  156. wp_cache_delete( 'bbp_parent_all_' . $_post->ID . '_type_' . $post_type . '_child_ids', 'bbpress_posts' ); 
  157.  
  158. // Invalidate parent caches 
  159. if ( ! empty( $_post->post_parent ) ) { 
  160. bbp_clean_post_cache( $_post->post_parent ); 
.