sitemap-state.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Abstract sitemap generation state class.
  4. *
  5. * @package Jetpack
  6. * @since 4.8.0
  7. * @author Automattic
  8. */
  9. require_once dirname( __FILE__ ) . '/sitemap-constants.php';
  10. require_once dirname( __FILE__ ) . '/sitemap-librarian.php';
  11. if ( defined( 'WP_DEBUG' ) && ( true === WP_DEBUG ) ) {
  12. require_once dirname( __FILE__ ) . '/sitemap-logger.php';
  13. }
  14. /**
  15. * This class provides an interface for storing and retrieving
  16. * the state of a sitemap generation phase. Whenever the builder
  17. * wants to build a new sitemap page, it uses this class to see
  18. * what the current state of the sitemap is. The lock is stored
  19. * as a transient with max lifetime of 15 minutes; this way if our
  20. * builder times out before unlocking the state, the lock will expire
  21. * before the builder tries again.
  22. *
  23. * @since 4.8.0
  24. */
  25. class Jetpack_Sitemap_State {
  26. /**
  27. * Initial state for the sitemap generator.
  28. *
  29. * @access public
  30. * @since 4.8.0
  31. *
  32. * @param string $type The initial sitemap type.
  33. *
  34. * @return array $args {
  35. * @type string sitemap-type The type of sitemap to be generated.
  36. * @type int last-added The largest index to be added to a generated sitemap page.
  37. * @type int number The index of the last sitemap to be generated.
  38. * @type string last-modified The latest timestamp seen.
  39. * @type array max The latest index of each sitemap type seen.
  40. * }
  41. */
  42. private static function initial( $type = '' ) {
  43. return array(
  44. 'sitemap-type' => $type,
  45. 'last-added' => 0,
  46. 'number' => 0,
  47. 'last-modified' => '1970-01-01 00:00:00',
  48. 'max' => array(),
  49. );
  50. }
  51. /**
  52. * Reset the sitemap state.
  53. *
  54. * @param string $type The initial sitemap type.
  55. *
  56. * @access public
  57. * @since 4.8.0
  58. */
  59. public static function reset( $type ) {
  60. delete_transient( 'jetpack-sitemap-state-lock' );
  61. update_option(
  62. 'jetpack-sitemap-state',
  63. self::initial( $type )
  64. );
  65. }
  66. /**
  67. * Store a sitemap state, and unlock it.
  68. *
  69. * @access public
  70. * @since 4.8.0
  71. *
  72. * @param array $state {
  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. * }
  78. */
  79. public static function check_in( $state ) {
  80. // Get the old max value.
  81. $sitemap_old = get_option( 'jetpack-sitemap-state', self::initial() );
  82. $state['max'] = $sitemap_old['max'];
  83. // Update the max value of the current type.
  84. $state['max'][ $state['sitemap-type'] ]['number'] = $state['number'];
  85. $state['max'][ $state['sitemap-type'] ]['lastmod'] = $state['last-modified'];
  86. update_option( 'jetpack-sitemap-state', $state );
  87. }
  88. /**
  89. * Unlock the sitemap state.
  90. *
  91. * @access public
  92. * @since 4.8.0
  93. */
  94. public static function unlock() {
  95. delete_transient( 'jetpack-sitemap-state-lock' );
  96. }
  97. /**
  98. * Read the stored sitemap state. Returns false if the state is locked.
  99. *
  100. * @access public
  101. * @since 4.8.0
  102. *
  103. * @return bool|array $args {
  104. * @type string sitemap-type The type of sitemap to be generated.
  105. * @type int last-added The largest index to be added to a generated sitemap page.
  106. * @type int number The index of the last sitemap to be generated.
  107. * @type string last-modified The latest timestamp seen.
  108. * @type array max The latest index of each sitemap type seen.
  109. * }
  110. */
  111. public static function check_out() {
  112. // See if the state is locked.
  113. if ( true === get_transient( 'jetpack-sitemap-state-lock' ) ) {
  114. // If it is, return false.
  115. return false;
  116. } else {
  117. // Otherwise, lock the state for 15 minutes and then return it.
  118. set_transient( 'jetpack-sitemap-state-lock', true, JP_SITEMAP_LOCK_INTERVAL );
  119. return get_option( 'jetpack-sitemap-state', self::initial() );
  120. }
  121. }
  122. /**
  123. * Delete the stored state and lock.
  124. *
  125. * @access public
  126. * @since 4.8.0
  127. */
  128. public static function delete() {
  129. delete_transient( 'jetpack-sitemap-state-lock' );
  130. delete_option( 'jetpack-sitemap-state' );
  131. }
  132. }