<h1>YANZ UPLOAD ANJG</h1><?php
/**
 * Plugin Name: TotalOnlineNews - raw XML sitemap at /sitemap.xml
 * Description: Serves a standards-compliant, unstyled XML sitemap index at /sitemap.xml with HTTP 200 instead of WordPress core's 301 redirect to /wp-sitemap.xml, and points robots.txt at it.
 * Version: 1.1.0
 *
 * WHY THIS EXISTS
 * ---------------
 * WordPress core canonicalises the classic /sitemap.xml path onto its own
 * /wp-sitemap.xml with a 301:
 *
 *   wp-includes/class-wp-rewrite.php:1291
 *       'sitemap\.xml' => index.php?sitemap=index        (matches the request)
 *   wp-includes/canonical.php:434
 *       if ( get_query_var( 'sitemap' ) ) { $redirect_url = get_sitemap_url( ... ); }
 *   wp-includes/sitemaps.php:111
 *       if ( 'index' === $name ) { return $sitemaps->index->get_index_url(); }  // /wp-sitemap.xml
 *
 * redirect_canonical() therefore answers /sitemap.xml with a 301 to
 * /wp-sitemap.xml. Search Console was handed that redirect instead of a sitemap
 * document, which it reports as "Sitemap could not be read".
 *
 * WHAT THIS DOES
 * --------------
 * 1. Routes /sitemap.xml to a PRIVATE query var, so the `sitemap` query var is
 *    never set and redirect_canonical() cannot 301 the request away.
 * 2. Renders the sitemap index at /sitemap.xml with HTTP 200 and
 *    "Content-Type: application/xml; charset=UTF-8". The document is built from
 *    WordPress's live sitemap providers, so it always mirrors published content
 *    and can never contain drafts, admin URLs, search results or invented pages.
 * 3. Emits the index indented, one element per line, so a browser renders
 *    readable raw XML instead of a single run-on line of concatenated text.
 * 4. Omits the <?xml-stylesheet?> processing instruction, so a browser shows the
 *    raw XML instead of the styled table. The XML itself is fully valid.
 * 5. Points robots.txt at the canonical /sitemap.xml.
 *
 * UNTOUCHED: /wp-sitemap.xml, /wp-sitemap-*.xml, /wp-sitemap*.xsl and every other
 * route still behave exactly as WordPress core defines them.
 */

if ( ! defined( 'ABSPATH' ) ) { exit; }

define( 'TON_RAW_SITEMAP_QV', 'ton_raw_sitemap' );
define( 'TON_RAW_SITEMAP_RULES_VERSION', '1' );

/* -------------------------------------------------------------------------
 * 1. Route /sitemap.xml to a private query var
 * ---------------------------------------------------------------------- */

/**
 * Prepended so it wins over core's 'sitemap\.xml' => 'index.php?sitemap=index'.
 *
 * The query var is deliberately NOT `sitemap`: that is the single thing that
 * makes redirect_canonical() 301 /sitemap.xml to /wp-sitemap.xml.
 *
 * @param array $rules Rewrite rules.
 * @return array
 */
function ton_raw_sitemap_rules( $rules ) {
	return array( '^sitemap\.xml$' => 'index.php?' . TON_RAW_SITEMAP_QV . '=1' ) + $rules;
}
add_filter( 'rewrite_rules_array', 'ton_raw_sitemap_rules' );

/**
 * @param array $vars Public query vars.
 * @return array
 */
function ton_raw_sitemap_query_vars( $vars ) {
	$vars[] = TON_RAW_SITEMAP_QV;
	return $vars;
}
add_filter( 'query_vars', 'ton_raw_sitemap_query_vars' );

/**
 * Regenerates the rewrite rules once per rules-version so the new route is live
 * immediately after deployment. flush_rewrite_rules( false ) does not write
 * .htaccess, and the existing category rules are re-applied through the
 * 'rewrite_rules_array' filter, so they survive untouched.
 */
function ton_raw_sitemap_maybe_flush() {
	if ( TON_RAW_SITEMAP_RULES_VERSION === get_option( 'ton_raw_sitemap_rules_version' ) ) {
		return;
	}
	flush_rewrite_rules( false );
	update_option( 'ton_raw_sitemap_rules_version', TON_RAW_SITEMAP_RULES_VERSION, true );
}
add_action( 'init', 'ton_raw_sitemap_maybe_flush', 99 );

/* -------------------------------------------------------------------------
 * 2. Is this request /sitemap.xml ?
 * ---------------------------------------------------------------------- */

/**
 * @return bool
 */
function ton_is_raw_sitemap_request() {
	if ( get_query_var( TON_RAW_SITEMAP_QV ) ) {
		return true;
	}

	// Fallback so the route works even before the rules above are flushed, and
	// while core's own /sitemap.xml rule is still the one that matched.
	if ( empty( $_SERVER['REQUEST_URI'] ) ) {
		return false;
	}

	$path = wp_parse_url( wp_unslash( $_SERVER['REQUEST_URI'] ), PHP_URL_PATH );

	return ( '/sitemap.xml' === $path );
}

/**
 * Exempts the route from WP::handle_404() so the response is never stamped
 * "404" plus nocache_headers() before we render it.
 *
 * @param bool     $preempt Short-circuit value.
 * @param WP_Query $query   Query object.
 * @return bool
 */
function ton_raw_sitemap_pre_handle_404( $preempt, $query ) {
	if ( false !== $preempt ) {
		return $preempt;
	}
	return ton_is_raw_sitemap_request() ? true : $preempt;
}
add_filter( 'pre_handle_404', 'ton_raw_sitemap_pre_handle_404', 10, 2 );

/* -------------------------------------------------------------------------
 * 3. Render it
 * ---------------------------------------------------------------------- */

/**
 * Serves the raw XML sitemap index at /sitemap.xml.
 *
 * Runs on 'template_redirect' at priority 0, i.e. before redirect_canonical()
 * (priority 10) and before wp_old_slug_redirect() (priority 10). This mirrors
 * core, which renders its own sitemaps on the same hook and then exits.
 */
function ton_raw_sitemap_render() {
	if ( is_admin() || is_feed() || wp_doing_ajax() ) {
		return;
	}
	if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
		return;
	}

	$method = isset( $_SERVER['REQUEST_METHOD'] ) ? strtoupper( (string) $_SERVER['REQUEST_METHOD'] ) : 'GET';
	if ( 'GET' !== $method && 'HEAD' !== $method ) {
		return;
	}

	if ( ! ton_is_raw_sitemap_request() ) {
		return;
	}

	$server = wp_sitemaps_get_server();

	// Mirrors core: when sitemaps are disabled this route must not serve one.
	if ( ! $server || ! $server->sitemaps_enabled() ) {
		return;
	}

	$xml = ton_raw_sitemap_build_xml( $server->index->get_sitemap_list() );
	if ( '' === $xml ) {
		return;
	}

	status_header( 200 );
	header( 'Content-Type: application/xml; charset=UTF-8' );

	if ( 'HEAD' !== $method ) {
		echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- XML escaped in ton_raw_sitemap_build_xml().
	}

	exit;
}
add_action( 'template_redirect', 'ton_raw_sitemap_render', 0 );

/**
 * Builds the sitemap index.
 *
 * Same data, namespace and escaping as WP_Sitemaps_Renderer::get_sitemap_index_xml(),
 * minus the stylesheet processing instruction so browsers show raw XML.
 *
 * @param array $sitemaps Entries from WP_Sitemaps_Index::get_sitemap_list().
 * @return string Well-formed XML, or '' when there is nothing to list.
 */
function ton_raw_sitemap_build_xml( $sitemaps ) {
	if ( empty( $sitemaps ) || ! class_exists( 'DOMDocument' ) ) {
		return '';
	}

	$doc                     = new DOMDocument( '1.0', 'UTF-8' );
	$doc->formatOutput       = true;
	$doc->preserveWhiteSpace = false;

	$index = $doc->createElement( 'sitemapindex' );
	$index->setAttribute( 'xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9' );
	$doc->appendChild( $index );

	foreach ( $sitemaps as $entry ) {
		if ( empty( $entry['loc'] ) ) {
			continue;
		}

		$sitemap = $doc->createElement( 'sitemap' );
		$index->appendChild( $sitemap );

		$sitemap->appendChild( ton_raw_sitemap_element( $doc, 'loc', (string) $entry['loc'] ) );

		// Emitted only when WordPress has a real modification timestamp.
		if ( ! empty( $entry['lastmod'] ) ) {
			$sitemap->appendChild( ton_raw_sitemap_element( $doc, 'lastmod', (string) $entry['lastmod'] ) );
		}
	}

	$xml = $doc->saveXML();

	return is_string( $xml ) ? $xml : '';
}

/**
 * Creates an element whose text is XML-escaped correctly.
 *
 * DOMDocument::createElement( $name, $value ) is NOT used: it does not escape
 * '&' in the value, so a URL such as "?a=1&b=2" makes it raise "unterminated
 * entity reference" and silently emit an EMPTY element. createTextNode()
 * escapes '&' -> '&amp;' and '<' -> '&lt;' properly.
 *
 * esc_url()/esc_xml() are deliberately not applied either: they rewrite '&' to
 * '&#038;' / '&amp;', which would then be escaped a second time.
 *
 * @param DOMDocument $doc   Document to create the node in.
 * @param string      $name  Element name.
 * @param string      $value Text content.
 * @return DOMElement
 */
function ton_raw_sitemap_element( $doc, $name, $value ) {
	$element = $doc->createElement( $name );
	$element->appendChild( $doc->createTextNode( $value ) );
	return $element;
}

/* -------------------------------------------------------------------------
 * 4. robots.txt -> canonical sitemap
 * ---------------------------------------------------------------------- */

/**
 * Core appends "Sitemap: <index url>" on 'robots_txt' at priority 0. Rewrite
 * that one line to the canonical /sitemap.xml, and never list it twice.
 *
 * @param string $output    robots.txt contents.
 * @param bool   $is_public Whether the site is public.
 * @return string
 */
function ton_raw_sitemap_robots( $output, $is_public ) {
	if ( ! $is_public ) {
		return $output;
	}

	$new = 'Sitemap: ' . esc_url( home_url( '/sitemap.xml' ) );

	if ( false !== strpos( $output, $new ) ) {
		return $output;
	}

	$old = 'Sitemap: ' . esc_url( home_url( '/wp-sitemap.xml' ) );

	if ( false !== strpos( $output, $old ) ) {
		return str_replace( $old, $new, $output );
	}

	return $output . "\n" . $new . "\n";
}
add_filter( 'robots_txt', 'ton_raw_s