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