| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\XML_Sitemaps |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class WPSEO_Sitemaps. |
| 10 |
* |
| 11 |
* @todo This class could use a general description with some explanation on sitemaps. OR. |
| 12 |
*/ |
| 13 |
class WPSEO_Sitemaps { |
| 14 |
|
| 15 |
/** |
| 16 |
* Sitemap index identifier. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
const SITEMAP_INDEX_TYPE = '1'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Content of the sitemap to output. |
| 24 |
* |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
protected $sitemap = ''; |
| 28 |
|
| 29 |
/** |
| 30 |
* Flag to indicate if this is an invalid or empty sitemap. |
| 31 |
* |
| 32 |
* @var bool |
| 33 |
*/ |
| 34 |
public $bad_sitemap = false; |
| 35 |
|
| 36 |
/** |
| 37 |
* Whether or not the XML sitemap was served from a transient or not. |
| 38 |
* |
| 39 |
* @var bool |
| 40 |
*/ |
| 41 |
private $transient = false; |
| 42 |
|
| 43 |
/** |
| 44 |
* HTTP protocol to use in headers. |
| 45 |
* |
| 46 |
* @since 3.2 |
| 47 |
* |
| 48 |
* @var string |
| 49 |
*/ |
| 50 |
protected $http_protocol = 'HTTP/1.1'; |
| 51 |
|
| 52 |
/** |
| 53 |
* Holds the n variable. |
| 54 |
* |
| 55 |
* @var int |
| 56 |
*/ |
| 57 |
private $current_page = 1; |
| 58 |
|
| 59 |
/** |
| 60 |
* The sitemaps router. |
| 61 |
* |
| 62 |
* @since 3.2 |
| 63 |
* |
| 64 |
* @var WPSEO_Sitemaps_Router |
| 65 |
*/ |
| 66 |
public $router; |
| 67 |
|
| 68 |
/** |
| 69 |
* The sitemap renderer. |
| 70 |
* |
| 71 |
* @since 3.2 |
| 72 |
* |
| 73 |
* @var WPSEO_Sitemaps_Renderer |
| 74 |
*/ |
| 75 |
public $renderer; |
| 76 |
|
| 77 |
/** |
| 78 |
* The sitemap cache. |
| 79 |
* |
| 80 |
* @since 3.2 |
| 81 |
* |
| 82 |
* @var WPSEO_Sitemaps_Cache |
| 83 |
*/ |
| 84 |
public $cache; |
| 85 |
|
| 86 |
/** |
| 87 |
* The sitemap providers. |
| 88 |
* |
| 89 |
* @since 3.2 |
| 90 |
* |
| 91 |
* @var WPSEO_Sitemap_Provider[] |
| 92 |
*/ |
| 93 |
public $providers; |
| 94 |
|
| 95 |
/** |
| 96 |
* Class constructor. |
| 97 |
*/ |
| 98 |
public function __construct() { |
| 99 |
|
| 100 |
add_action( 'after_setup_theme', [ $this, 'init_sitemaps_providers' ] ); |
| 101 |
add_action( 'after_setup_theme', [ $this, 'reduce_query_load' ], 99 ); |
| 102 |
add_action( 'pre_get_posts', [ $this, 'redirect' ], 1 ); |
| 103 |
add_action( 'wpseo_hit_sitemap_index', [ $this, 'hit_sitemap_index' ] ); |
| 104 |
add_action( 'wpseo_ping_search_engines', [ __CLASS__, 'ping_search_engines' ] ); |
| 105 |
|
| 106 |
$this->router = new WPSEO_Sitemaps_Router(); |
| 107 |
$this->renderer = new WPSEO_Sitemaps_Renderer(); |
| 108 |
$this->cache = new WPSEO_Sitemaps_Cache(); |
| 109 |
|
| 110 |
if ( ! empty( $_SERVER['SERVER_PROTOCOL'] ) ) { |
| 111 |
$this->http_protocol = sanitize_text_field( wp_unslash( $_SERVER['SERVER_PROTOCOL'] ) ); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Initialize sitemap providers classes. |
| 117 |
* |
| 118 |
* @since 5.3 |
| 119 |
*/ |
| 120 |
public function init_sitemaps_providers() { |
| 121 |
|
| 122 |
$this->providers = [ |
| 123 |
new WPSEO_Post_Type_Sitemap_Provider(), |
| 124 |
new WPSEO_Taxonomy_Sitemap_Provider(), |
| 125 |
new WPSEO_Author_Sitemap_Provider(), |
| 126 |
]; |
| 127 |
|
| 128 |
$external_providers = apply_filters( 'wpseo_sitemaps_providers', [] ); |
| 129 |
|
| 130 |
foreach ( $external_providers as $provider ) { |
| 131 |
if ( is_object( $provider ) && $provider instanceof WPSEO_Sitemap_Provider ) { |
| 132 |
$this->providers[] = $provider; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Check the current request URI, if we can determine it's probably an XML sitemap, kill loading the widgets. |
| 139 |
*/ |
| 140 |
public function reduce_query_load() { |
| 141 |
if ( ! isset( $_SERVER['REQUEST_URI'] ) ) { |
| 142 |
return; |
| 143 |
} |
| 144 |
$request_uri = sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ); |
| 145 |
$extension = substr( $request_uri, -4 ); |
| 146 |
if ( stripos( $request_uri, 'sitemap' ) !== false && in_array( $extension, [ '.xml', '.xsl' ], true ) ) { |
| 147 |
remove_all_actions( 'widgets_init' ); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Register your own sitemap. Call this during 'init'. |
| 153 |
* |
| 154 |
* @param string $name The name of the sitemap. |
| 155 |
* @param callback $building_function Function to build your sitemap. |
| 156 |
* @param string $rewrite Optional. Regular expression to match your sitemap with. |
| 157 |
*/ |
| 158 |
public function register_sitemap( $name, $building_function, $rewrite = '' ) { |
| 159 |
add_action( 'wpseo_do_sitemap_' . $name, $building_function ); |
| 160 |
if ( ! empty( $rewrite ) ) { |
| 161 |
add_rewrite_rule( $rewrite, 'index.php?sitemap=' . $name, 'top' ); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Register your own XSL file. Call this during 'init'. |
| 167 |
* |
| 168 |
* @since 1.4.23 |
| 169 |
* |
| 170 |
* @param string $name The name of the XSL file. |
| 171 |
* @param callback $building_function Function to build your XSL file. |
| 172 |
* @param string $rewrite Optional. Regular expression to match your sitemap with. |
| 173 |
*/ |
| 174 |
public function register_xsl( $name, $building_function, $rewrite = '' ) { |
| 175 |
add_action( 'wpseo_xsl_' . $name, $building_function ); |
| 176 |
if ( ! empty( $rewrite ) ) { |
| 177 |
add_rewrite_rule( $rewrite, 'index.php?yoast-sitemap-xsl=' . $name, 'top' ); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Set the sitemap current page to allow creating partial sitemaps with WP-CLI |
| 183 |
* in a one-off process. |
| 184 |
* |
| 185 |
* @param int $current_page The part that should be generated. |
| 186 |
*/ |
| 187 |
public function set_n( $current_page ) { |
| 188 |
if ( is_scalar( $current_page ) && intval( $current_page ) > 0 ) { |
| 189 |
$this->current_page = intval( $current_page ); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Set the sitemap content to display after you have generated it. |
| 195 |
* |
| 196 |
* @param string $sitemap The generated sitemap to output. |
| 197 |
*/ |
| 198 |
public function set_sitemap( $sitemap ) { |
| 199 |
$this->sitemap = $sitemap; |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* Set as true to make the request 404. Used stop the display of empty sitemaps or invalid requests. |
| 204 |
* |
| 205 |
* @param bool $is_bad Is this a bad request. True or false. |
| 206 |
*/ |
| 207 |
public function set_bad_sitemap( $is_bad ) { |
| 208 |
$this->bad_sitemap = (bool) $is_bad; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Prevent stupid plugins from running shutdown scripts when we're obviously not outputting HTML. |
| 213 |
* |
| 214 |
* @since 1.4.16 |
| 215 |
*/ |
| 216 |
public function sitemap_close() { |
| 217 |
remove_all_actions( 'wp_footer' ); |
| 218 |
die(); |
| 219 |
} |
| 220 |
|
| 221 |
/** |
| 222 |
* Hijack requests for potential sitemaps and XSL files. |
| 223 |
* |
| 224 |
* @param \WP_Query $query Main query instance. |
| 225 |
*/ |
| 226 |
public function redirect( $query ) { |
| 227 |
|
| 228 |
if ( ! $query->is_main_query() ) { |
| 229 |
return; |
| 230 |
} |
| 231 |
|
| 232 |
$yoast_sitemap_xsl = get_query_var( 'yoast-sitemap-xsl' ); |
| 233 |
|
| 234 |
if ( ! empty( $yoast_sitemap_xsl ) ) { |
| 235 |
/* |
| 236 |
* This is a method to provide the XSL via the home_url. |
| 237 |
* Needed when the site_url and home_url are not the same. |
| 238 |
* Loading the XSL needs to come from the same domain, protocol and port as the XML. |
| 239 |
* |
| 240 |
* Whenever home_url and site_url are the same, the file can be loaded directly. |
| 241 |
*/ |
| 242 |
$this->xsl_output( $yoast_sitemap_xsl ); |
| 243 |
$this->sitemap_close(); |
| 244 |
|
| 245 |
return; |
| 246 |
} |
| 247 |
|
| 248 |
$type = get_query_var( 'sitemap' ); |
| 249 |
|
| 250 |
if ( empty( $type ) ) { |
| 251 |
return; |
| 252 |
} |
| 253 |
|
| 254 |
$this->set_n( get_query_var( 'sitemap_n' ) ); |
| 255 |
|
| 256 |
if ( ! $this->get_sitemap_from_cache( $type, $this->current_page ) ) { |
| 257 |
$this->build_sitemap( $type ); |
| 258 |
} |
| 259 |
|
| 260 |
if ( $this->bad_sitemap ) { |
| 261 |
$query->set_404(); |
| 262 |
status_header( 404 ); |
| 263 |
|
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
$this->output(); |
| 268 |
$this->sitemap_close(); |
| 269 |
} |
| 270 |
|
| 271 |
/** |
| 272 |
* Try to get the sitemap from cache. |
| 273 |
* |
| 274 |
* @param string $type Sitemap type. |
| 275 |
* @param int $page_number The page number to retrieve. |
| 276 |
* |
| 277 |
* @return bool If the sitemap has been retrieved from cache. |
| 278 |
*/ |
| 279 |
private function get_sitemap_from_cache( $type, $page_number ) { |
| 280 |
|
| 281 |
$this->transient = false; |
| 282 |
|
| 283 |
if ( $this->cache->is_enabled() !== true ) { |
| 284 |
return false; |
| 285 |
} |
| 286 |
|
| 287 |
/** |
| 288 |
* Fires before the attempt to retrieve XML sitemap from the transient cache. |
| 289 |
* |
| 290 |
* @param WPSEO_Sitemaps $sitemaps Sitemaps object. |
| 291 |
*/ |
| 292 |
do_action( 'wpseo_sitemap_stylesheet_cache_' . $type, $this ); |
| 293 |
|
| 294 |
$sitemap_cache_data = $this->cache->get_sitemap_data( $type, $page_number ); |
| 295 |
|
| 296 |
// No cache was found, refresh it because cache is enabled. |
| 297 |
if ( empty( $sitemap_cache_data ) ) { |
| 298 |
return $this->refresh_sitemap_cache( $type, $page_number ); |
| 299 |
} |
| 300 |
|
| 301 |
// Cache object was found, parse information. |
| 302 |
$this->transient = true; |
| 303 |
|
| 304 |
$this->sitemap = $sitemap_cache_data->get_sitemap(); |
| 305 |
$this->bad_sitemap = ! $sitemap_cache_data->is_usable(); |
| 306 |
|
| 307 |
return true; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Build and save sitemap to cache. |
| 312 |
* |
| 313 |
* @param string $type Sitemap type. |
| 314 |
* @param int $page_number The page number to save to. |
| 315 |
* |
| 316 |
* @return bool |
| 317 |
*/ |
| 318 |
private function refresh_sitemap_cache( $type, $page_number ) { |
| 319 |
$this->set_n( $page_number ); |
| 320 |
$this->build_sitemap( $type ); |
| 321 |
|
| 322 |
return $this->cache->store_sitemap( $type, $page_number, $this->sitemap, ! $this->bad_sitemap ); |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Attempts to build the requested sitemap. |
| 327 |
* |
| 328 |
* Sets $bad_sitemap if this isn't for the root sitemap, a post type or taxonomy. |
| 329 |
* |
| 330 |
* @param string $type The requested sitemap's identifier. |
| 331 |
*/ |
| 332 |
public function build_sitemap( $type ) { |
| 333 |
|
| 334 |
/** |
| 335 |
* Filter the type of sitemap to build. |
| 336 |
* |
| 337 |
* @param string $type Sitemap type, determined by the request. |
| 338 |
*/ |
| 339 |
$type = apply_filters( 'wpseo_build_sitemap_post_type', $type ); |
| 340 |
|
| 341 |
if ( $type === '1' ) { |
| 342 |
$this->build_root_map(); |
| 343 |
|
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
$entries_per_page = $this->get_entries_per_page(); |
| 348 |
|
| 349 |
foreach ( $this->providers as $provider ) { |
| 350 |
if ( ! $provider->handles_type( $type ) ) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
|
| 354 |
try { |
| 355 |
$links = $provider->get_sitemap_links( $type, $entries_per_page, $this->current_page ); |
| 356 |
} catch ( OutOfBoundsException $exception ) { |
| 357 |
$this->bad_sitemap = true; |
| 358 |
|
| 359 |
return; |
| 360 |
} |
| 361 |
|
| 362 |
$this->sitemap = $this->renderer->get_sitemap( $links, $type, $this->current_page ); |
| 363 |
|
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
if ( has_action( 'wpseo_do_sitemap_' . $type ) ) { |
| 368 |
/** |
| 369 |
* Fires custom handler, if hooked to generate sitemap for the type. |
| 370 |
*/ |
| 371 |
do_action( 'wpseo_do_sitemap_' . $type ); |
| 372 |
|
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
$this->bad_sitemap = true; |
| 377 |
} |
| 378 |
|
| 379 |
/** |
| 380 |
* Build the root sitemap (example.com/sitemap_index.xml) which lists sub-sitemaps for other content types. |
| 381 |
*/ |
| 382 |
public function build_root_map() { |
| 383 |
|
| 384 |
$links = []; |
| 385 |
$entries_per_page = $this->get_entries_per_page(); |
| 386 |
|
| 387 |
foreach ( $this->providers as $provider ) { |
| 388 |
$links = array_merge( $links, $provider->get_index_links( $entries_per_page ) ); |
| 389 |
} |
| 390 |
|
| 391 |
/** |
| 392 |
* Filter the sitemap links array before the index sitemap is built. |
| 393 |
* |
| 394 |
* @param array $links Array of sitemap links |
| 395 |
*/ |
| 396 |
$links = apply_filters( 'wpseo_sitemap_index_links', $links ); |
| 397 |
|
| 398 |
if ( empty( $links ) ) { |
| 399 |
$this->bad_sitemap = true; |
| 400 |
$this->sitemap = ''; |
| 401 |
|
| 402 |
return; |
| 403 |
} |
| 404 |
|
| 405 |
$this->sitemap = $this->renderer->get_index( $links ); |
| 406 |
} |
| 407 |
|
| 408 |
/** |
| 409 |
* Spits out the XSL for the XML sitemap. |
| 410 |
* |
| 411 |
* @since 1.4.13 |
| 412 |
* |
| 413 |
* @param string $type Type to output. |
| 414 |
*/ |
| 415 |
public function xsl_output( $type ) { |
| 416 |
|
| 417 |
if ( $type !== 'main' ) { |
| 418 |
|
| 419 |
/** |
| 420 |
* Fires for the output of XSL for XML sitemaps, other than type "main". |
| 421 |
*/ |
| 422 |
do_action( 'wpseo_xsl_' . $type ); |
| 423 |
|
| 424 |
return; |
| 425 |
} |
| 426 |
|
| 427 |
header( $this->http_protocol . ' 200 OK', true, 200 ); |
| 428 |
// Prevent the search engines from indexing the XML Sitemap. |
| 429 |
header( 'X-Robots-Tag: noindex, follow', true ); |
| 430 |
header( 'Content-Type: text/xml' ); |
| 431 |
|
| 432 |
// Make the browser cache this file properly. |
| 433 |
$expires = YEAR_IN_SECONDS; |
| 434 |
header( 'Pragma: public' ); |
| 435 |
header( 'Cache-Control: max-age=' . $expires ); |
| 436 |
header( 'Expires: ' . YoastSEO()->helpers->date->format_timestamp( ( time() + $expires ), 'D, d M Y H:i:s' ) . ' GMT' ); |
| 437 |
|
| 438 |
// Don't use WP_Filesystem() here because that's not initialized yet. See https://yoast.atlassian.net/browse/QAK-2043. |
| 439 |
readfile( WPSEO_PATH . 'css/main-sitemap.xsl' ); |
| 440 |
} |
| 441 |
|
| 442 |
/** |
| 443 |
* Spit out the generated sitemap. |
| 444 |
*/ |
| 445 |
public function output() { |
| 446 |
$this->send_headers(); |
| 447 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- Escaping sitemap as either xml or html results in empty document. |
| 448 |
echo $this->renderer->get_output( $this->sitemap ); |
| 449 |
} |
| 450 |
|
| 451 |
/** |
| 452 |
* Makes a request to the sitemap index to cache it before the arrival of the search engines. |
| 453 |
* |
| 454 |
* @return void |
| 455 |
*/ |
| 456 |
public function hit_sitemap_index() { |
| 457 |
if ( ! $this->cache->is_enabled() ) { |
| 458 |
return; |
| 459 |
} |
| 460 |
|
| 461 |
wp_remote_get( WPSEO_Sitemaps_Router::get_base_url( 'sitemap_index.xml' ) ); |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Get the GMT modification date for the last modified post in the post type. |
| 466 |
* |
| 467 |
* @since 3.2 |
| 468 |
* |
| 469 |
* @param string|array $post_types Post type or array of types. |
| 470 |
* @param bool $return_all Flag to return array of values. |
| 471 |
* |
| 472 |
* @return string|array|false |
| 473 |
*/ |
| 474 |
public static function get_last_modified_gmt( $post_types, $return_all = false ) { |
| 475 |
|
| 476 |
global $wpdb; |
| 477 |
|
| 478 |
static $post_type_dates = null; |
| 479 |
|
| 480 |
if ( ! is_array( $post_types ) ) { |
| 481 |
$post_types = [ $post_types ]; |
| 482 |
} |
| 483 |
|
| 484 |
foreach ( $post_types as $post_type ) { |
| 485 |
if ( ! isset( $post_type_dates[ $post_type ] ) ) { // If we hadn't seen post type before. R. |
| 486 |
$post_type_dates = null; |
| 487 |
break; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
if ( is_null( $post_type_dates ) ) { |
| 492 |
|
| 493 |
$post_type_dates = []; |
| 494 |
$post_type_names = WPSEO_Post_Type::get_accessible_post_types(); |
| 495 |
|
| 496 |
if ( ! empty( $post_type_names ) ) { |
| 497 |
$post_statuses = array_map( 'esc_sql', self::get_post_statuses() ); |
| 498 |
|
| 499 |
$sql = " |
| 500 |
SELECT post_type, MAX(post_modified_gmt) AS date |
| 501 |
FROM $wpdb->posts |
| 502 |
WHERE post_status IN ('" . implode( "','", $post_statuses ) . "') |
| 503 |
AND post_type IN ('" . implode( "','", $post_type_names ) . "') |
| 504 |
GROUP BY post_type |
| 505 |
ORDER BY date DESC |
| 506 |
"; |
| 507 |
|
| 508 |
foreach ( $wpdb->get_results( $sql ) as $obj ) { |
| 509 |
$post_type_dates[ $obj->post_type ] = $obj->date; |
| 510 |
} |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
$dates = array_intersect_key( $post_type_dates, array_flip( $post_types ) ); |
| 515 |
|
| 516 |
if ( count( $dates ) > 0 ) { |
| 517 |
if ( $return_all ) { |
| 518 |
return $dates; |
| 519 |
} |
| 520 |
|
| 521 |
return max( $dates ); |
| 522 |
} |
| 523 |
|
| 524 |
return false; |
| 525 |
} |
| 526 |
|
| 527 |
/** |
| 528 |
* Get the modification date for the last modified post in the post type. |
| 529 |
* |
| 530 |
* @param array $post_types Post types to get the last modification date for. |
| 531 |
* |
| 532 |
* @return string |
| 533 |
*/ |
| 534 |
public function get_last_modified( $post_types ) { |
| 535 |
return YoastSEO()->helpers->date->format( self::get_last_modified_gmt( $post_types ) ); |
| 536 |
} |
| 537 |
|
| 538 |
/** |
| 539 |
* Notify search engines of the updated sitemap. |
| 540 |
* |
| 541 |
* @param string|null $url Optional URL to make the ping for. |
| 542 |
*/ |
| 543 |
public static function ping_search_engines( $url = null ) { |
| 544 |
|
| 545 |
/** |
| 546 |
* Filter: 'wpseo_allow_xml_sitemap_ping' - Check if pinging is not allowed (allowed by default) |
| 547 |
* |
| 548 |
* @api boolean $allow_ping The boolean that is set to true by default. |
| 549 |
*/ |
| 550 |
if ( apply_filters( 'wpseo_allow_xml_sitemap_ping', true ) === false ) { |
| 551 |
return; |
| 552 |
} |
| 553 |
|
| 554 |
if ( get_option( 'blog_public' ) === '0' ) { // Don't ping if blog is not public. |
| 555 |
return; |
| 556 |
} |
| 557 |
|
| 558 |
if ( empty( $url ) ) { |
| 559 |
$url = rawurlencode( WPSEO_Sitemaps_Router::get_base_url( 'sitemap_index.xml' ) ); |
| 560 |
} |
| 561 |
|
| 562 |
// Ping Google and Bing. |
| 563 |
wp_remote_get( 'https://www.google.com/ping?sitemap=' . $url, [ 'blocking' => false ] ); |
| 564 |
wp_remote_get( 'https://www.bing.com/ping?sitemap=' . $url, [ 'blocking' => false ] ); |
| 565 |
} |
| 566 |
|
| 567 |
/** |
| 568 |
* Get the maximum number of entries per XML sitemap. |
| 569 |
* |
| 570 |
* @return int The maximum number of entries. |
| 571 |
*/ |
| 572 |
protected function get_entries_per_page() { |
| 573 |
/** |
| 574 |
* Filter the maximum number of entries per XML sitemap. |
| 575 |
* |
| 576 |
* After changing the output of the filter, make sure that you disable and enable the |
| 577 |
* sitemaps to make sure the value is picked up for the sitemap cache. |
| 578 |
* |
| 579 |
* @param int $entries The maximum number of entries per XML sitemap. |
| 580 |
*/ |
| 581 |
$entries = (int) apply_filters( 'wpseo_sitemap_entries_per_page', 1000 ); |
| 582 |
|
| 583 |
return $entries; |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
* Get post statuses for post_type or the root sitemap. |
| 588 |
* |
| 589 |
* @since 10.2 |
| 590 |
* |
| 591 |
* @param string $type Provide a type for a post_type sitemap, SITEMAP_INDEX_TYPE for the root sitemap. |
| 592 |
* |
| 593 |
* @return array List of post statuses. |
| 594 |
*/ |
| 595 |
public static function get_post_statuses( $type = self::SITEMAP_INDEX_TYPE ) { |
| 596 |
/** |
| 597 |
* Filter post status list for sitemap query for the post type. |
| 598 |
* |
| 599 |
* @param array $post_statuses Post status list, defaults to array( 'publish' ). |
| 600 |
* @param string $type Post type or SITEMAP_INDEX_TYPE. |
| 601 |
*/ |
| 602 |
$post_statuses = apply_filters( 'wpseo_sitemap_post_statuses', [ 'publish' ], $type ); |
| 603 |
|
| 604 |
if ( ! is_array( $post_statuses ) || empty( $post_statuses ) ) { |
| 605 |
$post_statuses = [ 'publish' ]; |
| 606 |
} |
| 607 |
|
| 608 |
if ( ( $type === self::SITEMAP_INDEX_TYPE || $type === 'attachment' ) |
| 609 |
&& ! in_array( 'inherit', $post_statuses, true ) |
| 610 |
) { |
| 611 |
$post_statuses[] = 'inherit'; |
| 612 |
} |
| 613 |
|
| 614 |
return $post_statuses; |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* Sends all the required HTTP Headers. |
| 619 |
*/ |
| 620 |
private function send_headers() { |
| 621 |
if ( headers_sent() ) { |
| 622 |
return; |
| 623 |
} |
| 624 |
|
| 625 |
$headers = [ |
| 626 |
$this->http_protocol . ' 200 OK' => 200, |
| 627 |
// Prevent the search engines from indexing the XML Sitemap. |
| 628 |
'X-Robots-Tag: noindex, follow' => '', |
| 629 |
'Content-Type: text/xml; charset=' . esc_attr( $this->renderer->get_output_charset() ) => '', |
| 630 |
]; |
| 631 |
|
| 632 |
/** |
| 633 |
* Filter the HTTP headers we send before an XML sitemap. |
| 634 |
* |
| 635 |
* @param array $headers The HTTP headers we're going to send out. |
| 636 |
*/ |
| 637 |
$headers = apply_filters( 'wpseo_sitemap_http_headers', $headers ); |
| 638 |
|
| 639 |
foreach ( $headers as $header => $status ) { |
| 640 |
if ( is_numeric( $status ) ) { |
| 641 |
header( $header, true, $status ); |
| 642 |
continue; |
| 643 |
} |
| 644 |
header( $header, true ); |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
|