BBP_Skip_Children

Skip invalidation of child post content when editing a parent.

Defined (1)

The class is defined in the following location(s).

/includes/core/cache.php  
  1. class BBP_Skip_Children { 
  2.  
  3. /** 
  4. * @var int Post ID being updated 
  5. */ 
  6. private $updating_post = 0; 
  7.  
  8. /** 
  9. * @var bool The original value of $_wp_suspend_cache_invalidation global 
  10. */ 
  11. private $original_cache_invalidation = false; 
  12.  
  13. /** Methods ***************************************************************/ 
  14.  
  15. /** 
  16. * Hook into the 'pre_post_update' action. 
  17. * @since bbPress (r4011) 
  18. */ 
  19. public function __construct() { 
  20. add_action( 'pre_post_update', array( $this, 'pre_post_update' ) ); 
  21.  
  22. /** 
  23. * Only clean post caches for main bbPress posts. 
  24. * Check that the post being updated is a bbPress post type, saves the 
  25. * post ID to be used later, and adds an action to 'clean_post_cache' that 
  26. * prevents child post caches from being cleared. 
  27. * @since bbPress (r4011) 
  28. * @param int $post_id The post ID being updated 
  29. * @return If invalid post data 
  30. */ 
  31. public function pre_post_update( $post_id = 0 ) { 
  32.  
  33. // Bail if post ID is not a bbPress post type 
  34. if ( empty( $post_id ) || ! bbp_is_custom_post_type( $post_id ) ) 
  35. return; 
  36.  
  37. // Store the $post_id 
  38. $this->updating_post = $post_id; 
  39.  
  40. // Skip related post cache invalidation. This prevents invalidating the 
  41. // caches of the child posts when there is no reason to do so. 
  42. add_action( 'clean_post_cache', array( $this, 'skip_related_posts' ) ); 
  43.  
  44. /** 
  45. * Skip cache invalidation of related posts if the post ID being invalidated 
  46. * is not the one that was just updated. 
  47. * @since bbPress (r4011) 
  48. * @param int $post_id The post ID of the cache being invalidated 
  49. * @return If invalid post data 
  50. */ 
  51. public function skip_related_posts( $post_id = 0 ) { 
  52.  
  53. // Bail if this post is not the current bbPress post 
  54. if ( empty( $post_id ) || ( $this->updating_post !== $post_id ) ) 
  55. return; 
  56.  
  57. // Stash the current cache invalidation value in a variable, so we can 
  58. // restore back to it nicely in the future. 
  59. global $_wp_suspend_cache_invalidation; 
  60.  
  61. $this->original_cache_invalidation = $_wp_suspend_cache_invalidation; 
  62.  
  63. // Turn off cache invalidation 
  64. wp_suspend_cache_invalidation( true ); 
  65.  
  66. // Restore cache invalidation 
  67. add_action( 'wp_insert_post', array( $this, 'restore_cache_invalidation' ) ); 
  68.  
  69. /** 
  70. * Restore the cache invalidation to its previous value. 
  71. * @since bbPress (r4011) 
  72. * @uses wp_suspend_cache_invalidation() 
  73. */ 
  74. public function restore_cache_invalidation() { 
  75. wp_suspend_cache_invalidation( $this->original_cache_invalidation );