Jetpack_Sitemap_State

This class provides an interface for storing and retrieving the state of a sitemap generation phase.

Defined (1)

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

/modules/sitemaps/sitemap-state.php  
  1. class Jetpack_Sitemap_State { 
  2.  
  3. /** 
  4. * Initial state for the sitemap generator. 
  5. * @access public 
  6. * @since 4.8.0 
  7. * @param string $type The initial sitemap type. 
  8. * @return array $args { 
  9. * @type string sitemap-type The type of sitemap to be generated. 
  10. * @type int last-added The largest index to be added to a generated sitemap page. 
  11. * @type int number The index of the last sitemap to be generated. 
  12. * @type string last-modified The latest timestamp seen. 
  13. * @type array max The latest index of each sitemap type seen. 
  14. * } 
  15. */ 
  16. private static function initial( $type = '' ) { 
  17. return array( 
  18. 'sitemap-type' => $type,  
  19. 'last-added' => 0,  
  20. 'number' => 0,  
  21. 'last-modified' => '1970-01-01 00:00:00',  
  22. 'max' => array(),  
  23. ); 
  24.  
  25. /** 
  26. * Reset the sitemap state. 
  27. * @param string $type The initial sitemap type. 
  28. * @access public 
  29. * @since 4.8.0 
  30. */ 
  31. public static function reset( $type ) { 
  32. delete_transient( 'jetpack-sitemap-state-lock' ); 
  33. update_option( 
  34. 'jetpack-sitemap-state',  
  35. self::initial( $type ) 
  36. ); 
  37.  
  38. /** 
  39. * Store a sitemap state, and unlock it. 
  40. * @access public 
  41. * @since 4.8.0 
  42. * @param array $state { 
  43. * @type string sitemap-type The type of sitemap to be generated. 
  44. * @type int last-added The largest index to be added to a generated sitemap page. 
  45. * @type int number The index of the last sitemap to be generated. 
  46. * @type string last-modified The latest timestamp seen. 
  47. * } 
  48. */ 
  49. public static function check_in( $state ) { 
  50. // Get the old max value. 
  51. $sitemap_old = get_option( 'jetpack-sitemap-state', self::initial() ); 
  52. $state['max'] = $sitemap_old['max']; 
  53.  
  54. // Update the max value of the current type. 
  55. $state['max'][ $state['sitemap-type'] ]['number'] = $state['number']; 
  56. $state['max'][ $state['sitemap-type'] ]['lastmod'] = $state['last-modified']; 
  57.  
  58. update_option( 'jetpack-sitemap-state', $state ); 
  59.  
  60. /** 
  61. * Unlock the sitemap state. 
  62. * @access public 
  63. * @since 4.8.0 
  64. */ 
  65. public static function unlock() { 
  66. delete_transient( 'jetpack-sitemap-state-lock' ); 
  67.  
  68. /** 
  69. * Read the stored sitemap state. Returns false if the state is locked. 
  70. * @access public 
  71. * @since 4.8.0 
  72. * @return bool|array $args { 
  73. * @type string sitemap-type The type of sitemap to be generated. 
  74. * @type int last-added The largest index to be added to a generated sitemap page. 
  75. * @type int number The index of the last sitemap to be generated. 
  76. * @type string last-modified The latest timestamp seen. 
  77. * @type array max The latest index of each sitemap type seen. 
  78. * } 
  79. */ 
  80. public static function check_out() { 
  81. // See if the state is locked. 
  82. if ( true === get_transient( 'jetpack-sitemap-state-lock' ) ) { 
  83. // If it is, return false. 
  84. return false; 
  85. } else { 
  86. // Otherwise, lock the state for 15 minutes and then return it. 
  87. set_transient( 'jetpack-sitemap-state-lock', true, JP_SITEMAP_LOCK_INTERVAL ); 
  88. return get_option( 'jetpack-sitemap-state', self::initial() ); 
  89.  
  90. /** 
  91. * Delete the stored state and lock. 
  92. * @access public 
  93. * @since 4.8.0 
  94. */ 
  95. public static function delete() { 
  96. delete_transient( 'jetpack-sitemap-state-lock' ); 
  97. delete_option( 'jetpack-sitemap-state' ); 
  98.