WP_Sitemaps::render_sitemaps │ public │ WP 5.5.0

Renders sitemap templates based on rewrite rules.

Method of the class: WP_Sitemaps{}

No Hooks.

Returns

null. Nothing (null).

Usage

$WP_Sitemaps = new WP_Sitemaps();
$WP_Sitemaps->render_sitemaps();

Changelog

Since 5.5.0 Introduced.

WP_Sitemaps::render_sitemaps() code WP 7.1.3

public function render_sitemaps() {
	/*
	 * Bail early if this isn't a sitemap or stylesheet route.
	 *
	 * This runs on every front-end request, so it comes before any
	 * sanitizing. The raw query vars are tested here, matching
	 * WP::handle_404(), which exempts sitemap requests from its own 404 on
	 * the same basis. Testing the sanitized values instead would let a
	 * request that handle_404() exempted fall through both, leaving it a 200.
	 */
	if ( ! get_query_var( 'sitemap' ) && ! get_query_var( 'sitemap-stylesheet' ) ) {
		return;
	}

	$sitemap         = sanitize_text_field( get_query_var( 'sitemap' ) );
	$object_subtype  = sanitize_text_field( get_query_var( 'sitemap-subtype' ) );
	$stylesheet_type = sanitize_text_field( get_query_var( 'sitemap-stylesheet' ) );
	$paged           = absint( get_query_var( 'paged' ) );

	// Force a 404 and bail early if the route did not survive sanitizing.
	if ( ! ( $sitemap || $stylesheet_type ) ) {
		$this->send_404();
		return;
	}

	if ( ! $this->sitemaps_enabled() ) {
		$this->send_404();
		return;
	}

	// Render stylesheet if this is stylesheet route.
	if ( $stylesheet_type ) {
		// Force a 404 and bail early if the stylesheet type is not recognized.
		if ( ! in_array( $stylesheet_type, array( 'sitemap', 'index' ), true ) ) {
			$this->send_404();
			return;
		}

		$stylesheet = new WP_Sitemaps_Stylesheet();

		$stylesheet->render_stylesheet( $stylesheet_type );
		exit;
	}

	// Render the index.
	if ( 'index' === $sitemap ) {
		$sitemap_list = $this->index->get_sitemap_list();

		$this->renderer->render_index( $sitemap_list );
		exit;
	}

	$provider = $this->registry->get_provider( $sitemap );

	// Force a 404 and bail early if the requested provider is not registered.
	if ( ! $provider ) {
		$this->send_404();
		return;
	}

	if ( empty( $paged ) ) {
		$paged = 1;
	}

	$url_list = $provider->get_url_list( $paged, $object_subtype );

	// Force a 404 and bail early if no URLs are present.
	if ( empty( $url_list ) ) {
		$this->send_404();
		return;
	}

	$this->renderer->render_sitemap( $url_list );
	exit;
}