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