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