sitemap-buffer-fallback.php
3 years ago
sitemap-buffer-image-fallback.php
4 years ago
sitemap-buffer-image.php
4 years ago
sitemap-buffer-master-fallback.php
4 years ago
sitemap-buffer-master.php
4 years ago
sitemap-buffer-news-fallback.php
4 years ago
sitemap-buffer-news.php
4 years ago
sitemap-buffer-page-fallback.php
4 years ago
sitemap-buffer-page.php
4 years ago
sitemap-buffer-video-fallback.php
4 years ago
sitemap-buffer-video.php
4 years ago
sitemap-buffer.php
4 years ago
sitemap-builder.php
3 years ago
sitemap-constants.php
4 years ago
sitemap-finder.php
4 years ago
sitemap-librarian.php
3 years ago
sitemap-logger.php
4 years ago
sitemap-state.php
4 years ago
sitemap-stylist.php
3 years ago
sitemaps.php
3 years ago
sitemaps.php
603 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Generate sitemap files in base XML as well as some namespace extensions. |
| 4 | * |
| 5 | * This module generates two different base sitemaps. |
| 6 | * |
| 7 | * 1. sitemap.xml |
| 8 | * The basic sitemap is updated regularly by wp-cron. It is stored in the |
| 9 | * database and retrieved when requested. This sitemap aims to include canonical |
| 10 | * URLs for all published content and abide by the sitemap spec. This is the root |
| 11 | * of a tree of sitemap and sitemap index xml files, depending on the number of URLs. |
| 12 | * |
| 13 | * By default the sitemap contains published posts of type 'post' and 'page', as |
| 14 | * well as the home url. To include other post types use the 'jetpack_sitemap_post_types' |
| 15 | * filter. |
| 16 | * |
| 17 | * @link https://www.sitemaps.org/protocol.html Base sitemaps protocol. |
| 18 | * @link https://support.google.com/webmasters/answer/178636 Image sitemap extension. |
| 19 | * @link https://developers.google.com/webmasters/videosearch/sitemaps Video sitemap extension. |
| 20 | * |
| 21 | * 2. news-sitemap.xml |
| 22 | * The news sitemap is generated on the fly when requested. It does not aim for |
| 23 | * completeness, instead including at most 1000 of the most recent published posts |
| 24 | * from the previous 2 days, per the news-sitemap spec. |
| 25 | * |
| 26 | * @link https://support.google.com/webmasters/answer/74288 News sitemap extension. |
| 27 | * |
| 28 | * @package automattic/jetpack |
| 29 | * @since 3.9.0 |
| 30 | * @since 4.8.0 Remove 1000 post limit. |
| 31 | * @author Automattic |
| 32 | */ |
| 33 | |
| 34 | /* Include all of the sitemap subclasses. */ |
| 35 | require_once __DIR__ . '/sitemap-constants.php'; |
| 36 | require_once __DIR__ . '/sitemap-buffer.php'; |
| 37 | require_once __DIR__ . '/sitemap-stylist.php'; |
| 38 | require_once __DIR__ . '/sitemap-librarian.php'; |
| 39 | require_once __DIR__ . '/sitemap-finder.php'; |
| 40 | require_once __DIR__ . '/sitemap-builder.php'; |
| 41 | |
| 42 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) { |
| 43 | require_once __DIR__ . '/sitemap-logger.php'; |
| 44 | } |
| 45 | |
| 46 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. |
| 47 | |
| 48 | /** |
| 49 | * Governs the generation, storage, and serving of sitemaps. |
| 50 | * |
| 51 | * @since 4.8.0 |
| 52 | */ |
| 53 | class Jetpack_Sitemap_Manager { |
| 54 | |
| 55 | /** |
| 56 | * Librarian object for storing and retrieving sitemap data. |
| 57 | * |
| 58 | * @see Jetpack_Sitemap_Librarian |
| 59 | * @since 4.8.0 |
| 60 | * @var Jetpack_Sitemap_Librarian $librarian Librarian object for storing and retrieving sitemap data. |
| 61 | */ |
| 62 | private $librarian; |
| 63 | |
| 64 | /** |
| 65 | * Logger object for reporting debug messages. |
| 66 | * |
| 67 | * @see Jetpack_Sitemap_Logger |
| 68 | * @since 4.8.0 |
| 69 | * @var Jetpack_Sitemap_Logger $logger Logger object for reporting debug messages. |
| 70 | */ |
| 71 | private $logger; |
| 72 | |
| 73 | /** |
| 74 | * Finder object for handling sitemap URIs. |
| 75 | * |
| 76 | * @see Jetpack_Sitemap_Finder |
| 77 | * @since 4.8.0 |
| 78 | * @var Jetpack_Sitemap_Finder $finder Finder object for handling with sitemap URIs. |
| 79 | */ |
| 80 | private $finder; |
| 81 | |
| 82 | /** |
| 83 | * Construct a new Jetpack_Sitemap_Manager. |
| 84 | * |
| 85 | * @access public |
| 86 | * @since 4.8.0 |
| 87 | */ |
| 88 | public function __construct() { |
| 89 | $this->librarian = new Jetpack_Sitemap_Librarian(); |
| 90 | $this->finder = new Jetpack_Sitemap_Finder(); |
| 91 | |
| 92 | if ( defined( 'WP_DEBUG' ) && ( true === WP_DEBUG ) ) { |
| 93 | $this->logger = new Jetpack_Sitemap_Logger(); |
| 94 | } |
| 95 | |
| 96 | // Add callback for sitemap URL handler. |
| 97 | add_action( |
| 98 | 'init', |
| 99 | array( $this, 'callback_action_catch_sitemap_urls' ), |
| 100 | defined( 'IS_WPCOM' ) && IS_WPCOM ? 100 : 10 |
| 101 | ); |
| 102 | |
| 103 | // Add generator to wp_cron task list. |
| 104 | $this->schedule_sitemap_generation(); |
| 105 | |
| 106 | // Add sitemap to robots.txt. |
| 107 | add_action( |
| 108 | 'do_robotstxt', |
| 109 | array( $this, 'callback_action_do_robotstxt' ), |
| 110 | 20 |
| 111 | ); |
| 112 | |
| 113 | // The news sitemap is cached; here we add a callback to |
| 114 | // flush the cached news sitemap when a post is published. |
| 115 | add_action( |
| 116 | 'publish_post', |
| 117 | array( $this, 'callback_action_flush_news_sitemap_cache' ), |
| 118 | 10 |
| 119 | ); |
| 120 | |
| 121 | // In case we need to purge all sitemaps, we do this. |
| 122 | add_action( |
| 123 | 'jetpack_sitemaps_purge_data', |
| 124 | array( $this, 'callback_action_purge_data' ) |
| 125 | ); |
| 126 | |
| 127 | /* |
| 128 | * Module parameters are stored as options in the database. |
| 129 | * This allows us to avoid having to process all of init |
| 130 | * before serving the sitemap data. The following actions |
| 131 | * process and store these filters. |
| 132 | */ |
| 133 | |
| 134 | // Process filters and store location string for sitemap. |
| 135 | add_action( |
| 136 | 'init', |
| 137 | array( $this, 'callback_action_filter_sitemap_location' ), |
| 138 | 999 |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Echo a raw string of given content-type. |
| 144 | * |
| 145 | * @access private |
| 146 | * @since 4.8.0 |
| 147 | * |
| 148 | * @param string $the_content_type The content type to be served. |
| 149 | * @param string $the_content The string to be echoed. |
| 150 | */ |
| 151 | private function serve_raw_and_die( $the_content_type, $the_content ) { |
| 152 | header( 'Content-Type: ' . $the_content_type . '; charset=UTF-8' ); |
| 153 | |
| 154 | global $wp_query; |
| 155 | $wp_query->is_feed = true; |
| 156 | set_query_var( 'feed', 'sitemap' ); |
| 157 | |
| 158 | if ( '' === $the_content ) { |
| 159 | $error = __( 'No sitemap found. Please try again later.', 'jetpack' ); |
| 160 | if ( current_user_can( 'manage_options' ) ) { |
| 161 | $next = human_time_diff( wp_next_scheduled( 'jp_sitemap_cron_hook' ) ); |
| 162 | /* translators: %s is a human_time_diff until next sitemap generation. */ |
| 163 | $error = sprintf( __( 'No sitemap found. The system will try to build it again in %s.', 'jetpack' ), $next ); |
| 164 | } |
| 165 | |
| 166 | wp_die( |
| 167 | esc_html( $error ), |
| 168 | esc_html__( 'Sitemaps', 'jetpack' ), |
| 169 | array( |
| 170 | 'response' => 404, |
| 171 | ) |
| 172 | ); |
| 173 | } |
| 174 | |
| 175 | echo $the_content; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All content created by Jetpack. |
| 176 | |
| 177 | die(); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Callback to intercept sitemap url requests and serve sitemap files. |
| 182 | * |
| 183 | * @access public |
| 184 | * @since 4.8.0 |
| 185 | */ |
| 186 | public function callback_action_catch_sitemap_urls() { |
| 187 | // Regular expressions for sitemap URL routing. |
| 188 | $regex = array( |
| 189 | 'master' => '/^sitemap\.xml$/', |
| 190 | 'sitemap' => '/^sitemap-[1-9][0-9]*\.xml$/', |
| 191 | 'index' => '/^sitemap-index-[1-9][0-9]*\.xml$/', |
| 192 | 'sitemap-style' => '/^sitemap\.xsl$/', |
| 193 | 'index-style' => '/^sitemap-index\.xsl$/', |
| 194 | 'image' => '/^image-sitemap-[1-9][0-9]*\.xml$/', |
| 195 | 'image-index' => '/^image-sitemap-index-[1-9][0-9]*\.xml$/', |
| 196 | 'image-style' => '/^image-sitemap\.xsl$/', |
| 197 | 'video' => '/^video-sitemap-[1-9][0-9]*\.xml$/', |
| 198 | 'video-index' => '/^video-sitemap-index-[1-9][0-9]*\.xml$/', |
| 199 | 'video-style' => '/^video-sitemap\.xsl$/', |
| 200 | 'news' => '/^news-sitemap\.xml$/', |
| 201 | 'news-style' => '/^news-sitemap\.xsl$/', |
| 202 | ); |
| 203 | |
| 204 | // The raw path(+query) of the requested URI. |
| 205 | if ( isset( $_SERVER['REQUEST_URI'] ) ) { // WPCS: Input var okay. |
| 206 | $raw_uri = sanitize_text_field( |
| 207 | wp_unslash( $_SERVER['REQUEST_URI'] ) // WPCS: Input var okay. |
| 208 | ); |
| 209 | } else { |
| 210 | $raw_uri = ''; |
| 211 | } |
| 212 | |
| 213 | $request = $this->finder->recognize_sitemap_uri( $raw_uri ); |
| 214 | |
| 215 | if ( isset( $request['sitemap_name'] ) ) { |
| 216 | |
| 217 | /** |
| 218 | * Filter the content type used to serve the sitemap XML files. |
| 219 | * |
| 220 | * @module sitemaps |
| 221 | * |
| 222 | * @since 3.9.0 |
| 223 | * |
| 224 | * @param string $xml_content_type By default, it's 'text/xml'. |
| 225 | */ |
| 226 | $xml_content_type = apply_filters( 'jetpack_sitemap_content_type', 'text/xml' ); |
| 227 | |
| 228 | // Catch master sitemap xml. |
| 229 | if ( preg_match( $regex['master'], $request['sitemap_name'] ) ) { |
| 230 | $sitemap_content = $this->librarian->get_sitemap_text( |
| 231 | jp_sitemap_filename( JP_MASTER_SITEMAP_TYPE, 0 ), |
| 232 | JP_MASTER_SITEMAP_TYPE |
| 233 | ); |
| 234 | |
| 235 | // if there is no master sitemap yet, let's just return an empty sitemap with a short TTL instead of a 404. |
| 236 | if ( empty( $sitemap_content ) ) { |
| 237 | $builder = new Jetpack_Sitemap_Builder(); |
| 238 | $sitemap_content = $builder->empty_sitemap_xml(); |
| 239 | } |
| 240 | |
| 241 | $this->serve_raw_and_die( |
| 242 | $xml_content_type, |
| 243 | $sitemap_content |
| 244 | ); |
| 245 | } |
| 246 | |
| 247 | // Catch sitemap xml. |
| 248 | if ( preg_match( $regex['sitemap'], $request['sitemap_name'] ) ) { |
| 249 | $this->serve_raw_and_die( |
| 250 | $xml_content_type, |
| 251 | $this->librarian->get_sitemap_text( |
| 252 | $request['sitemap_name'], |
| 253 | JP_PAGE_SITEMAP_TYPE |
| 254 | ) |
| 255 | ); |
| 256 | } |
| 257 | |
| 258 | // Catch sitemap index xml. |
| 259 | if ( preg_match( $regex['index'], $request['sitemap_name'] ) ) { |
| 260 | $this->serve_raw_and_die( |
| 261 | $xml_content_type, |
| 262 | $this->librarian->get_sitemap_text( |
| 263 | $request['sitemap_name'], |
| 264 | JP_PAGE_SITEMAP_INDEX_TYPE |
| 265 | ) |
| 266 | ); |
| 267 | } |
| 268 | |
| 269 | // Catch sitemap xsl. |
| 270 | if ( preg_match( $regex['sitemap-style'], $request['sitemap_name'] ) ) { |
| 271 | $this->serve_raw_and_die( |
| 272 | 'application/xml', |
| 273 | Jetpack_Sitemap_Stylist::sitemap_xsl() |
| 274 | ); |
| 275 | } |
| 276 | |
| 277 | // Catch sitemap index xsl. |
| 278 | if ( preg_match( $regex['index-style'], $request['sitemap_name'] ) ) { |
| 279 | $this->serve_raw_and_die( |
| 280 | 'application/xml', |
| 281 | Jetpack_Sitemap_Stylist::sitemap_index_xsl() |
| 282 | ); |
| 283 | } |
| 284 | |
| 285 | // Catch image sitemap xml. |
| 286 | if ( preg_match( $regex['image'], $request['sitemap_name'] ) ) { |
| 287 | $this->serve_raw_and_die( |
| 288 | $xml_content_type, |
| 289 | $this->librarian->get_sitemap_text( |
| 290 | $request['sitemap_name'], |
| 291 | JP_IMAGE_SITEMAP_TYPE |
| 292 | ) |
| 293 | ); |
| 294 | } |
| 295 | |
| 296 | // Catch image sitemap index xml. |
| 297 | if ( preg_match( $regex['image-index'], $request['sitemap_name'] ) ) { |
| 298 | $this->serve_raw_and_die( |
| 299 | $xml_content_type, |
| 300 | $this->librarian->get_sitemap_text( |
| 301 | $request['sitemap_name'], |
| 302 | JP_IMAGE_SITEMAP_INDEX_TYPE |
| 303 | ) |
| 304 | ); |
| 305 | } |
| 306 | |
| 307 | // Catch image sitemap xsl. |
| 308 | if ( preg_match( $regex['image-style'], $request['sitemap_name'] ) ) { |
| 309 | $this->serve_raw_and_die( |
| 310 | 'application/xml', |
| 311 | Jetpack_Sitemap_Stylist::image_sitemap_xsl() |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | // Catch video sitemap xml. |
| 316 | if ( preg_match( $regex['video'], $request['sitemap_name'] ) ) { |
| 317 | $this->serve_raw_and_die( |
| 318 | $xml_content_type, |
| 319 | $this->librarian->get_sitemap_text( |
| 320 | $request['sitemap_name'], |
| 321 | JP_VIDEO_SITEMAP_TYPE |
| 322 | ) |
| 323 | ); |
| 324 | } |
| 325 | |
| 326 | // Catch video sitemap index xml. |
| 327 | if ( preg_match( $regex['video-index'], $request['sitemap_name'] ) ) { |
| 328 | $this->serve_raw_and_die( |
| 329 | $xml_content_type, |
| 330 | $this->librarian->get_sitemap_text( |
| 331 | $request['sitemap_name'], |
| 332 | JP_VIDEO_SITEMAP_INDEX_TYPE |
| 333 | ) |
| 334 | ); |
| 335 | } |
| 336 | |
| 337 | // Catch video sitemap xsl. |
| 338 | if ( preg_match( $regex['video-style'], $request['sitemap_name'] ) ) { |
| 339 | $this->serve_raw_and_die( |
| 340 | 'application/xml', |
| 341 | Jetpack_Sitemap_Stylist::video_sitemap_xsl() |
| 342 | ); |
| 343 | } |
| 344 | |
| 345 | // Catch news sitemap xml. |
| 346 | if ( preg_match( $regex['news'], $request['sitemap_name'] ) ) { |
| 347 | $sitemap_builder = new Jetpack_Sitemap_Builder(); |
| 348 | $this->serve_raw_and_die( |
| 349 | $xml_content_type, |
| 350 | $sitemap_builder->news_sitemap_xml() |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | // Catch news sitemap xsl. |
| 355 | if ( preg_match( $regex['news-style'], $request['sitemap_name'] ) ) { |
| 356 | $this->serve_raw_and_die( |
| 357 | 'application/xml', |
| 358 | Jetpack_Sitemap_Stylist::news_sitemap_xsl() |
| 359 | ); |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * Callback for adding sitemap-interval to the list of schedules. |
| 366 | * |
| 367 | * @access public |
| 368 | * @since 4.8.0 |
| 369 | * |
| 370 | * @param array $schedules The array of WP_Cron schedules. |
| 371 | * |
| 372 | * @return array The updated array of WP_Cron schedules. |
| 373 | */ |
| 374 | public function callback_add_sitemap_schedule( $schedules ) { |
| 375 | $schedules['sitemap-interval'] = array( |
| 376 | 'interval' => JP_SITEMAP_INTERVAL, |
| 377 | 'display' => __( 'Sitemap Interval', 'jetpack' ), |
| 378 | ); |
| 379 | return $schedules; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Callback handler for sitemap cron hook |
| 384 | * |
| 385 | * @access public |
| 386 | */ |
| 387 | public function callback_sitemap_cron_hook() { |
| 388 | $sitemap_builder = new Jetpack_Sitemap_Builder(); |
| 389 | $sitemap_builder->update_sitemap(); |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * Add actions to schedule sitemap generation. |
| 394 | * Should only be called once, in the constructor. |
| 395 | * |
| 396 | * @access private |
| 397 | * @since 4.8.0 |
| 398 | */ |
| 399 | private function schedule_sitemap_generation() { |
| 400 | // Add cron schedule. |
| 401 | add_filter( 'cron_schedules', array( $this, 'callback_add_sitemap_schedule' ) ); // phpcs:ignore WordPress.WP.CronInterval.ChangeDetected |
| 402 | |
| 403 | add_action( |
| 404 | 'jp_sitemap_cron_hook', |
| 405 | array( $this, 'callback_sitemap_cron_hook' ) |
| 406 | ); |
| 407 | |
| 408 | if ( ! wp_next_scheduled( 'jp_sitemap_cron_hook' ) ) { |
| 409 | /** |
| 410 | * Filter the delay in seconds until sitemap generation cron job is started. |
| 411 | * |
| 412 | * This filter allows a site operator or hosting provider to potentialy spread out sitemap generation for a |
| 413 | * lot of sites over time. By default, it will be randomly done over 15 minutes. |
| 414 | * |
| 415 | * @module sitemaps |
| 416 | * @since 6.6.1 |
| 417 | * |
| 418 | * @param int $delay Time to delay in seconds. |
| 419 | */ |
| 420 | $delay = apply_filters( 'jetpack_sitemap_generation_delay', MINUTE_IN_SECONDS * wp_rand( 1, 15 ) ); // Randomly space it out to start within next fifteen minutes. |
| 421 | wp_schedule_event( |
| 422 | time() + $delay, |
| 423 | 'sitemap-interval', |
| 424 | 'jp_sitemap_cron_hook' |
| 425 | ); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Callback to add sitemap to robots.txt. |
| 431 | * |
| 432 | * @access public |
| 433 | * @since 4.8.0 |
| 434 | */ |
| 435 | public function callback_action_do_robotstxt() { |
| 436 | |
| 437 | /** |
| 438 | * Filter whether to make the default sitemap discoverable to robots or not. Default true. |
| 439 | * |
| 440 | * @module sitemaps |
| 441 | * @since 3.9.0 |
| 442 | * @deprecated 7.4.0 |
| 443 | * |
| 444 | * @param bool $discover_sitemap Make default sitemap discoverable to robots. |
| 445 | */ |
| 446 | $discover_sitemap = apply_filters_deprecated( 'jetpack_sitemap_generate', array( true ), 'jetpack-7.4.0', 'jetpack_sitemap_include_in_robotstxt' ); |
| 447 | |
| 448 | /** |
| 449 | * Filter whether to make the default sitemap discoverable to robots or not. Default true. |
| 450 | * |
| 451 | * @module sitemaps |
| 452 | * @since 7.4.0 |
| 453 | * |
| 454 | * @param bool $discover_sitemap Make default sitemap discoverable to robots. |
| 455 | */ |
| 456 | $discover_sitemap = apply_filters( 'jetpack_sitemap_include_in_robotstxt', $discover_sitemap ); |
| 457 | |
| 458 | if ( true === $discover_sitemap ) { |
| 459 | $sitemap_url = $this->finder->construct_sitemap_url( 'sitemap.xml' ); |
| 460 | echo 'Sitemap: ' . esc_url( $sitemap_url ) . "\n"; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Filter whether to make the news sitemap discoverable to robots or not. Default true. |
| 465 | * |
| 466 | * @module sitemaps |
| 467 | * @since 3.9.0 |
| 468 | * @deprecated 7.4.0 |
| 469 | * |
| 470 | * @param bool $discover_news_sitemap Make default news sitemap discoverable to robots. |
| 471 | */ |
| 472 | $discover_news_sitemap = apply_filters_deprecated( 'jetpack_news_sitemap_generate', array( true ), 'jetpack-7.4.0', 'jetpack_news_sitemap_include_in_robotstxt' ); |
| 473 | |
| 474 | /** |
| 475 | * Filter whether to make the news sitemap discoverable to robots or not. Default true. |
| 476 | * |
| 477 | * @module sitemaps |
| 478 | * @since 7.4.0 |
| 479 | * |
| 480 | * @param bool $discover_news_sitemap Make default news sitemap discoverable to robots. |
| 481 | */ |
| 482 | $discover_news_sitemap = apply_filters( 'jetpack_news_sitemap_include_in_robotstxt', $discover_news_sitemap ); |
| 483 | |
| 484 | if ( true === $discover_news_sitemap ) { |
| 485 | $news_sitemap_url = $this->finder->construct_sitemap_url( 'news-sitemap.xml' ); |
| 486 | echo 'Sitemap: ' . esc_url( $news_sitemap_url ) . "\n"; |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * Callback to delete the news sitemap cache. |
| 492 | * |
| 493 | * @access public |
| 494 | * @since 4.8.0 |
| 495 | */ |
| 496 | public function callback_action_flush_news_sitemap_cache() { |
| 497 | delete_transient( 'jetpack_news_sitemap_xml' ); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * Callback for resetting stored sitemap data. |
| 502 | * |
| 503 | * @access public |
| 504 | * @since 5.3.0 |
| 505 | * @since 6.7.0 Schedules a regeneration. |
| 506 | */ |
| 507 | public function callback_action_purge_data() { |
| 508 | $this->callback_action_flush_news_sitemap_cache(); |
| 509 | $this->librarian->delete_all_stored_sitemap_data(); |
| 510 | /** This filter is documented in modules/sitemaps/sitemaps.php */ |
| 511 | $delay = apply_filters( 'jetpack_sitemap_generation_delay', MINUTE_IN_SECONDS * wp_rand( 1, 15 ) ); // Randomly space it out to start within next fifteen minutes. |
| 512 | wp_schedule_single_event( time() + $delay, 'jp_sitemap_cron_hook' ); |
| 513 | } |
| 514 | |
| 515 | /** |
| 516 | * Callback to set the sitemap location. |
| 517 | * |
| 518 | * @access public |
| 519 | * @since 4.8.0 |
| 520 | */ |
| 521 | public function callback_action_filter_sitemap_location() { |
| 522 | update_option( |
| 523 | 'jetpack_sitemap_location', |
| 524 | /** |
| 525 | * Additional path for sitemap URIs. Default value is empty. |
| 526 | * |
| 527 | * This string is any additional path fragment you want included between |
| 528 | * the home URL and the sitemap filenames. Exactly how this fragment is |
| 529 | * interpreted depends on your permalink settings. For example: |
| 530 | * |
| 531 | * Pretty permalinks: |
| 532 | * home_url() . jetpack_sitemap_location . '/sitemap.xml' |
| 533 | * |
| 534 | * Plain ("ugly") permalinks: |
| 535 | * home_url() . jetpack_sitemap_location . '/?jetpack-sitemap=sitemap.xml' |
| 536 | * |
| 537 | * PATHINFO permalinks: |
| 538 | * home_url() . '/index.php' . jetpack_sitemap_location . '/sitemap.xml' |
| 539 | * |
| 540 | * where 'sitemap.xml' is the name of a specific sitemap file. |
| 541 | * The value of this filter must be a valid path fragment per RFC 3986; |
| 542 | * in particular it must either be empty or begin with a '/'. |
| 543 | * Also take care that any restrictions on sitemap location imposed by |
| 544 | * the sitemap protocol are satisfied. |
| 545 | * |
| 546 | * The result of this filter is stored in an option, 'jetpack_sitemap_location'; |
| 547 | * that option is what gets read when the sitemap location is needed. |
| 548 | * This way we don't have to wait for init to finish before building sitemaps. |
| 549 | * |
| 550 | * @link https://tools.ietf.org/html/rfc3986#section-3.3 RFC 3986 |
| 551 | * @link https://www.sitemaps.org/ The sitemap protocol |
| 552 | * |
| 553 | * @since 4.8.0 |
| 554 | */ |
| 555 | apply_filters( |
| 556 | 'jetpack_sitemap_location', |
| 557 | '' |
| 558 | ) |
| 559 | ); |
| 560 | } |
| 561 | |
| 562 | } // End Jetpack_Sitemap_Manager class. |
| 563 | |
| 564 | new Jetpack_Sitemap_Manager(); |
| 565 | |
| 566 | /** |
| 567 | * Absolute URL of the current blog's sitemap. |
| 568 | * |
| 569 | * @module sitemaps |
| 570 | * |
| 571 | * @since 3.9.0 |
| 572 | * @since 4.8.1 Code uses method found in Jetpack_Sitemap_Finder::construct_sitemap_url in 4.8.0. |
| 573 | * It has been moved here to avoid fatal errors with other plugins that were expecting to find this function. |
| 574 | * |
| 575 | * @param string $filename Sitemap file name. Defaults to 'sitemap.xml', the initial sitemaps page. |
| 576 | * |
| 577 | * @return string Sitemap URL. |
| 578 | */ |
| 579 | function jetpack_sitemap_uri( $filename = 'sitemap.xml' ) { |
| 580 | global $wp_rewrite; |
| 581 | |
| 582 | $location = Jetpack_Options::get_option_and_ensure_autoload( 'jetpack_sitemap_location', '' ); |
| 583 | |
| 584 | if ( $wp_rewrite->using_index_permalinks() ) { |
| 585 | $sitemap_url = home_url( '/index.php' . $location . '/' . $filename ); |
| 586 | } elseif ( $wp_rewrite->using_permalinks() ) { |
| 587 | $sitemap_url = home_url( $location . '/' . $filename ); |
| 588 | } else { |
| 589 | $sitemap_url = home_url( $location . '/?jetpack-sitemap=' . $filename ); |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Filter sitemap URL relative to home URL. |
| 594 | * |
| 595 | * @module sitemaps |
| 596 | * |
| 597 | * @since 3.9.0 |
| 598 | * |
| 599 | * @param string $sitemap_url Sitemap URL. |
| 600 | */ |
| 601 | return apply_filters( 'jetpack_sitemap_location', $sitemap_url ); |
| 602 | } |
| 603 |