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