WPSEO_Sitemaps_Cache

Handles sitemaps caching and invalidation.

Defined (1)

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

/inc/sitemaps/class-sitemaps-cache.php  
  1. class WPSEO_Sitemaps_Cache { 
  2.  
  3. /** @var array $cache_clear Holds the options that, when updated, should cause the cache to clear. */ 
  4. protected static $cache_clear = array(); 
  5.  
  6. /** @var bool $is_enabled Mirror of enabled status for static calls. */ 
  7. protected static $is_enabled = true; 
  8.  
  9. /** @var bool $clear_all Holds the flag to clear all cache. */ 
  10. protected static $clear_all = false; 
  11.  
  12. /** @var array $clear_types Holds the array of types to clear. */ 
  13. protected static $clear_types = array(); 
  14.  
  15. /** 
  16. * Hook methods for invalidation on necessary events. 
  17. */ 
  18. public function __construct() { 
  19.  
  20. add_action( 'init', array( $this, 'init' ) ); 
  21.  
  22. add_action( 'deleted_term_relationships', array( __CLASS__, 'invalidate' ) ); 
  23.  
  24. add_action( 'update_option', array( __CLASS__, 'clear_on_option_update' ) ); 
  25.  
  26. add_action( 'edited_terms', array( __CLASS__, 'invalidate_helper' ), 10, 2 ); 
  27. add_action( 'clean_term_cache', array( __CLASS__, 'invalidate_helper' ), 10, 2 ); 
  28. add_action( 'clean_object_term_cache', array( __CLASS__, 'invalidate_helper' ), 10, 2 ); 
  29.  
  30. add_action( 'user_register', array( __CLASS__, 'invalidate_author' ) ); 
  31. add_action( 'delete_user', array( __CLASS__, 'invalidate_author' ) ); 
  32.  
  33. add_action( 'shutdown', array( __CLASS__, 'clear_queued' ) ); 
  34.  
  35. /** 
  36. * Setup context for static calls. 
  37. */ 
  38. public function init() { 
  39.  
  40. self::$is_enabled = $this->is_enabled(); 
  41.  
  42. /** 
  43. * If cache is enabled. 
  44. * @since 3.2 
  45. * @return boolean 
  46. */ 
  47. public function is_enabled() { 
  48.  
  49. /** 
  50. * Filter if XML sitemap transient cache is enabled. 
  51. * @param bool $unsigned Enable cache or not, defaults to true 
  52. */ 
  53. return apply_filters( 'wpseo_enable_xml_sitemap_transient_caching', true ); 
  54.  
  55.  
  56. /** 
  57. * Retrieve the sitemap page from cache. 
  58. * @since 3.2 
  59. * @param string $type Sitemap type. 
  60. * @param int $page Page number to retrieve. 
  61. * @return string|boolean 
  62. */ 
  63. public function get_sitemap( $type, $page ) { 
  64.  
  65. $transient_key = WPSEO_Sitemaps_Cache_Validator::get_storage_key( $type, $page ); 
  66. if ( false === $transient_key ) { 
  67. return false; 
  68.  
  69. return get_transient( $transient_key ); 
  70.  
  71. /** 
  72. * Get the sitemap that is cached 
  73. * @param string $type Sitemap type. 
  74. * @param int $page Page number to retrieve. 
  75. * @return null|WPSEO_Sitemap_Cache_Data Null on no cache found otherwise object containing sitemap and meta data. 
  76. */ 
  77. public function get_sitemap_data( $type, $page ) { 
  78.  
  79. $sitemap = $this->get_sitemap( $type, $page ); 
  80.  
  81. if ( empty( $sitemap ) ) { 
  82. return null; 
  83.  
  84. // Unserialize Cache Data object (is_serialized doesn't recognize classes). 
  85. if ( is_string( $sitemap ) && 0 === strpos( $sitemap, 'C:24:"WPSEO_Sitemap_Cache_Data"' ) ) { 
  86.  
  87. $sitemap = unserialize( $sitemap ); 
  88.  
  89. // What we expect it to be if it is set. 
  90. if ( $sitemap instanceof WPSEO_Sitemap_Cache_Data_Interface ) { 
  91. return $sitemap; 
  92.  
  93. return null; 
  94.  
  95. /** 
  96. * Store the sitemap page from cache. 
  97. * @since 3.2 
  98. * @param string $type Sitemap type. 
  99. * @param int $page Page number to store. 
  100. * @param string $sitemap Sitemap body to store. 
  101. * @param bool $usable Is this a valid sitemap or a cache of an invalid sitemap. 
  102. * @return bool 
  103. */ 
  104. public function store_sitemap( $type, $page, $sitemap, $usable = true ) { 
  105.  
  106. $transient_key = WPSEO_Sitemaps_Cache_Validator::get_storage_key( $type, $page ); 
  107.  
  108. if ( false === $transient_key ) { 
  109. return false; 
  110.  
  111. $status = ( $usable ) ? WPSEO_Sitemap_Cache_Data::OK : WPSEO_Sitemap_Cache_Data::ERROR; 
  112.  
  113. $sitemap_data = new WPSEO_Sitemap_Cache_Data(); 
  114. $sitemap_data->set_sitemap( $sitemap ); 
  115. $sitemap_data->set_status( $status ); 
  116.  
  117. return set_transient( $transient_key, $sitemap_data, DAY_IN_SECONDS ); 
  118.  
  119. /** 
  120. * Delete cache transients for index and specific type. 
  121. * Always deletes the main index sitemaps cache, as that's always invalidated by any other change. 
  122. * @since 1.5.4 
  123. * @since 3.2 Changed from function wpseo_invalidate_sitemap_cache() to method in this class. 
  124. * @param string $type Sitemap type to invalidate. 
  125. * @return void 
  126. */ 
  127. public static function invalidate( $type ) { 
  128.  
  129. self::clear( array( $type ) ); 
  130.  
  131. /** 
  132. * Helper to invalidate in hooks where type is passed as second argument. 
  133. * @since 3.2 
  134. * @param int $unused Unused term ID value. 
  135. * @param string $type Taxonomy to invalidate. 
  136. * @return void 
  137. */ 
  138. public static function invalidate_helper( $unused, $type ) { 
  139.  
  140. $sitemap_options = WPSEO_Options::get_option( 'wpseo_xml' ); 
  141.  
  142. $taxonomy_not_in_sitemap = 'taxonomies-' . $type . '-not_in_sitemap'; 
  143. if ( isset( $sitemap_options[ $taxonomy_not_in_sitemap ] ) && $sitemap_options[ $taxonomy_not_in_sitemap ] === false ) { 
  144. self::invalidate( $type ); 
  145.  
  146. /** 
  147. * Invalidate sitemap cache for authors. 
  148. * @param int $user_id User ID. 
  149. */ 
  150. public static function invalidate_author( $user_id ) { 
  151.  
  152. $user = get_user_by( 'id', $user_id ); 
  153.  
  154. if ( 'user_register' === current_action() ) { 
  155. update_user_meta( $user_id, '_yoast_wpseo_profile_updated', time() ); 
  156.  
  157. if ( ! in_array( 'subscriber', $user->roles ) ) { 
  158. self::invalidate( 'author' ); 
  159.  
  160. /** 
  161. * Invalidate sitemap cache for the post type of a post. 
  162. * Don't invalidate for revisions. 
  163. * @since 1.5.4 
  164. * @since 3.2 Changed from function wpseo_invalidate_sitemap_cache_on_save_post() to method in this class. 
  165. * @param int $post_id Post ID to invalidate type for. 
  166. * @return void 
  167. */ 
  168. public static function invalidate_post( $post_id ) { 
  169.  
  170. if ( wp_is_post_revision( $post_id ) ) { 
  171. return; 
  172.  
  173. self::invalidate( get_post_type( $post_id ) ); 
  174.  
  175. /** 
  176. * Delete cache transients for given sitemaps types or all by default. 
  177. * @since 1.8.0 
  178. * @since 3.2 Moved from WPSEO_Utils to this class. 
  179. * @param array $types Set of sitemap types to delete cache transients for. 
  180. * @return void 
  181. */ 
  182. public static function clear( $types = array() ) { 
  183.  
  184. if ( ! self::$is_enabled ) { 
  185. return; 
  186.  
  187. // No types provided, clear all. 
  188. if ( empty( $types ) ) { 
  189. self::$clear_all = true; 
  190.  
  191. return; 
  192.  
  193. // Always invalidate the index sitemap as well. 
  194. if ( ! in_array( WPSEO_Sitemaps::SITEMAP_INDEX_TYPE, $types ) ) { 
  195. array_unshift( $types, WPSEO_Sitemaps::SITEMAP_INDEX_TYPE ); 
  196.  
  197. foreach ( $types as $type ) { 
  198. if ( ! in_array( $type, self::$clear_types ) ) { 
  199. self::$clear_types[] = $type; 
  200.  
  201. /** 
  202. * Invalidate storage for cache types queued to clear. 
  203. */ 
  204. public static function clear_queued() { 
  205.  
  206. if ( self::$clear_all ) { 
  207.  
  208. WPSEO_Sitemaps_Cache_Validator::invalidate_storage(); 
  209. self::$clear_all = false; 
  210. self::$clear_types = array(); 
  211.  
  212. return; 
  213.  
  214. foreach ( self::$clear_types as $type ) { 
  215. WPSEO_Sitemaps_Cache_Validator::invalidate_storage( $type ); 
  216.  
  217. self::$clear_types = array(); 
  218.  
  219. /** 
  220. * Adds a hook that when given option is updated, the cache is cleared 
  221. * @since 3.2 
  222. * @param string $option Option name. 
  223. * @param string $type Sitemap type. 
  224. */ 
  225. public static function register_clear_on_option_update( $option, $type = '' ) { 
  226.  
  227. self::$cache_clear[ $option ] = $type; 
  228.  
  229. /** 
  230. * Clears the transient cache when a given option is updated, if that option has been registered before 
  231. * @since 3.2 
  232. * @param string $option The option name that's being updated. 
  233. * @return void 
  234. */ 
  235. public static function clear_on_option_update( $option ) { 
  236.  
  237. if ( array_key_exists( $option, self::$cache_clear ) ) { 
  238.  
  239. if ( empty( self::$cache_clear[ $option ] ) ) { 
  240. // Clear all caches. 
  241. self::clear(); 
  242. else { 
  243. // Clear specific provided type(s). 
  244. $types = (array) self::$cache_clear[ $option ]; 
  245. self::clear( $types );