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