1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sitemap-related constants. |
4
|
|
|
* |
5
|
|
|
* @package Jetpack |
6
|
|
|
* @since 4.7.0 |
7
|
|
|
* @author Automattic |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Maximum size (in bytes) of a sitemap xml file. |
12
|
|
|
* Per the spec, max value is 10485760 (10MB). |
13
|
|
|
* |
14
|
|
|
* @link http://www.sitemaps.org/ |
15
|
|
|
* @since 4.7.0 |
16
|
|
|
*/ |
17
|
|
|
if ( ! defined( 'JP_SITEMAP_MAX_BYTES' ) ) { |
18
|
|
|
define( 'JP_SITEMAP_MAX_BYTES', 10485760 ); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Maximum size (in url nodes) of a sitemap xml file. |
23
|
|
|
* Per the spec, max value is 50000. |
24
|
|
|
* |
25
|
|
|
* @link http://www.sitemaps.org/ |
26
|
|
|
* @since 4.7.0 |
27
|
|
|
*/ |
28
|
|
|
if ( ! defined( 'JP_SITEMAP_MAX_ITEMS' ) ) { |
29
|
|
|
define( 'JP_SITEMAP_MAX_ITEMS', 2000 ); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Maximum size (in url nodes) of a news sitemap xml file. |
34
|
|
|
* Per the spec, max value is 1000. |
35
|
|
|
* |
36
|
|
|
* @link https://support.google.com/news/publisher/answer/74288?hl=en |
37
|
|
|
* @since 4.7.0 |
38
|
|
|
*/ |
39
|
|
|
if ( ! defined( 'JP_NEWS_SITEMAP_MAX_ITEMS' ) ) { |
40
|
|
|
define( 'JP_NEWS_SITEMAP_MAX_ITEMS', 1000 ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Batch size for database queries. |
45
|
|
|
* |
46
|
|
|
* @since 4.7.0 |
47
|
|
|
*/ |
48
|
|
|
if ( ! defined( 'JP_SITEMAP_BATCH_SIZE' ) ) { |
49
|
|
|
define( 'JP_SITEMAP_BATCH_SIZE', 1000 ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Number of sitemap files to update on each run. |
54
|
|
|
* |
55
|
|
|
* @since 4.7.0 |
56
|
|
|
*/ |
57
|
|
|
if ( ! defined( 'JP_SITEMAP_UPDATE_SIZE' ) ) { |
58
|
|
|
define( 'JP_SITEMAP_UPDATE_SIZE', 100 ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Number of seconds between sitemap updates. |
63
|
|
|
* |
64
|
|
|
* @since 4.7.0 |
65
|
|
|
*/ |
66
|
|
|
if ( ! defined( 'JP_SITEMAP_INTERVAL' ) ) { |
67
|
|
|
define( 'JP_SITEMAP_INTERVAL', 12 * HOUR_IN_SECONDS ); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Number of seconds to lock the sitemap state. |
72
|
|
|
* |
73
|
|
|
* @since 4.7.0 |
74
|
|
|
*/ |
75
|
|
|
if ( ! defined( 'JP_SITEMAP_LOCK_INTERVAL' ) ) { |
76
|
|
|
define( 'JP_SITEMAP_LOCK_INTERVAL', 15 * MINUTE_IN_SECONDS ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Cache lifetime of news sitemap (in seconds). |
81
|
|
|
* |
82
|
|
|
* @since 4.7.0 |
83
|
|
|
*/ |
84
|
|
|
if ( ! defined( 'JP_NEWS_SITEMAP_INTERVAL' ) ) { |
85
|
|
|
define( 'JP_NEWS_SITEMAP_INTERVAL', 12 * HOUR_IN_SECONDS ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/* |
89
|
|
|
* These constants represent the types of various kinds of sitemaps. |
90
|
|
|
* Note: these strings are used as 'post_types' in the database, and |
91
|
|
|
* so must be at most 20 characters long. |
92
|
|
|
*/ |
93
|
|
|
|
94
|
|
|
if ( ! defined( 'JP_MASTER_SITEMAP_TYPE' ) ) { |
95
|
|
|
define( 'JP_MASTER_SITEMAP_TYPE', 'jp_sitemap_master' ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if ( ! defined( 'JP_PAGE_SITEMAP_TYPE' ) ) { |
99
|
|
|
define( 'JP_PAGE_SITEMAP_TYPE', 'jp_sitemap' ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ( ! defined( 'JP_PAGE_SITEMAP_INDEX_TYPE' ) ) { |
103
|
|
|
define( 'JP_PAGE_SITEMAP_INDEX_TYPE', 'jp_sitemap_index' ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if ( ! defined( 'JP_IMAGE_SITEMAP_TYPE' ) ) { |
107
|
|
|
define( 'JP_IMAGE_SITEMAP_TYPE', 'jp_img_sitemap' ); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ( ! defined( 'JP_IMAGE_SITEMAP_INDEX_TYPE' ) ) { |
111
|
|
|
define( 'JP_IMAGE_SITEMAP_INDEX_TYPE', 'jp_img_sitemap_index' ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if ( ! defined( 'JP_VIDEO_SITEMAP_TYPE' ) ) { |
115
|
|
|
define( 'JP_VIDEO_SITEMAP_TYPE', 'jp_vid_sitemap' ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if ( ! defined( 'JP_VIDEO_SITEMAP_INDEX_TYPE' ) ) { |
119
|
|
|
define( 'JP_VIDEO_SITEMAP_INDEX_TYPE', 'jp_vid_sitemap_index' ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* The name (with extension) of a sitemap file of the given |
124
|
|
|
* type and number. |
125
|
|
|
* |
126
|
|
|
* @since 4.7.0 |
127
|
|
|
* |
128
|
|
|
* @param string $type The sitemap type. |
129
|
|
|
* @param string $number The sitemap number. |
130
|
|
|
* |
131
|
|
|
* @return string The filename. |
132
|
|
|
*/ |
133
|
|
|
function jp_sitemap_filename( $type, $number ) { |
134
|
|
|
if ( ! is_int( $number ) ) { |
135
|
|
|
return "error-not-int-$type-$number.xml"; |
136
|
|
|
} elseif ( JP_MASTER_SITEMAP_TYPE === $type ) { |
137
|
|
|
return 'sitemap.xml'; |
138
|
|
|
} elseif ( JP_PAGE_SITEMAP_TYPE === $type ) { |
139
|
|
|
return "sitemap-$number.xml"; |
140
|
|
|
} elseif ( JP_PAGE_SITEMAP_INDEX_TYPE === $type ) { |
141
|
|
|
return "sitemap-index-$number.xml"; |
142
|
|
|
} elseif ( JP_IMAGE_SITEMAP_TYPE === $type ) { |
143
|
|
|
return "image-sitemap-$number.xml"; |
144
|
|
|
} elseif ( JP_IMAGE_SITEMAP_INDEX_TYPE === $type ) { |
145
|
|
|
return "image-sitemap-index-$number.xml"; |
146
|
|
|
} elseif ( JP_VIDEO_SITEMAP_TYPE === $type ) { |
147
|
|
|
return "video-sitemap-$number.xml"; |
148
|
|
|
} elseif ( JP_VIDEO_SITEMAP_INDEX_TYPE === $type ) { |
149
|
|
|
return "video-sitemap-index-$number.xml"; |
150
|
|
|
} else { |
151
|
|
|
return "error-bad-type-$type-$number.xml"; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* The index type corresponding to a sitemap type. |
157
|
|
|
* |
158
|
|
|
* @since 4.7.0 |
159
|
|
|
* |
160
|
|
|
* @param string $type The sitemap type. |
161
|
|
|
* |
162
|
|
|
* @return string The index type. |
163
|
|
|
*/ |
164
|
|
View Code Duplication |
function jp_sitemap_index_type_of( $type ) { |
|
|
|
|
165
|
|
|
if ( JP_PAGE_SITEMAP_TYPE === $type ) { |
166
|
|
|
return JP_PAGE_SITEMAP_INDEX_TYPE; |
167
|
|
|
} elseif ( JP_IMAGE_SITEMAP_TYPE === $type ) { |
168
|
|
|
return JP_IMAGE_SITEMAP_INDEX_TYPE; |
169
|
|
|
} elseif ( JP_VIDEO_SITEMAP_TYPE === $type ) { |
170
|
|
|
return JP_VIDEO_SITEMAP_INDEX_TYPE; |
171
|
|
|
} else { |
172
|
|
|
return "error-bad-type-$type"; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* The sitemap type corresponding to an index type. |
178
|
|
|
* |
179
|
|
|
* @since 4.7.0 |
180
|
|
|
* |
181
|
|
|
* @param string $type The index type. |
182
|
|
|
* |
183
|
|
|
* @return string The sitemap type. |
184
|
|
|
*/ |
185
|
|
View Code Duplication |
function jp_sitemap_child_type_of( $type ) { |
|
|
|
|
186
|
|
|
if ( JP_PAGE_SITEMAP_INDEX_TYPE === $type ) { |
187
|
|
|
return JP_PAGE_SITEMAP_TYPE; |
188
|
|
|
} elseif ( JP_IMAGE_SITEMAP_INDEX_TYPE === $type ) { |
189
|
|
|
return JP_IMAGE_SITEMAP_TYPE; |
190
|
|
|
} elseif ( JP_VIDEO_SITEMAP_INDEX_TYPE === $type ) { |
191
|
|
|
return JP_VIDEO_SITEMAP_TYPE; |
192
|
|
|
} else { |
193
|
|
|
return "error-bad-type-$type"; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Convert '0000-00-00 00:00:00' to '0000-00-00T00:00:00Z'. |
199
|
|
|
* Note that the input is assumed to be in UTC (a.k.a. GMT). |
200
|
|
|
* |
201
|
|
|
* @link https://www.w3.org/TR/NOTE-datetime |
202
|
|
|
* @since 4.7.0 |
203
|
|
|
* |
204
|
|
|
* @param string $datetime The timestamp to convert. |
205
|
|
|
* |
206
|
|
|
* @return string The converted timestamp. |
207
|
|
|
*/ |
208
|
|
|
function jp_sitemap_datetime( $datetime ) { |
209
|
|
|
$regex = '/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/'; |
210
|
|
|
|
211
|
|
|
if ( preg_match( $regex, $datetime ) ) { |
212
|
|
|
return str_replace( ' ', 'T', $datetime ) . 'Z'; |
213
|
|
|
} else { |
214
|
|
|
return $datetime; |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.