From e5f3dc0fde5ea75b26d8763f1199f7daed6acf08 Mon Sep 17 00:00:00 2001
From: Paul Biron <paul@sparrowhawkcomputing.com>
Date: Wed, 8 Jul 2020 09:39:40 -0600
Subject: [PATCH] Add l10n helpers for esc_xml() and replace all occurrences of
 esc_xml( __() ) in core with esc_xml__().

---
 src/wp-includes/l10n.php                      | 53 +++++++++++++++++++
 .../sitemaps/class-wp-sitemaps-renderer.php   |  4 +-
 .../sitemaps/class-wp-sitemaps-stylesheet.php | 28 +++++-----
 3 files changed, 69 insertions(+), 16 deletions(-)

diff --git a/src/wp-includes/l10n.php b/src/wp-includes/l10n.php
index 226cc2d8fd..61fd92dcea 100644
--- a/src/wp-includes/l10n.php
+++ b/src/wp-includes/l10n.php
@@ -371,6 +371,59 @@ function esc_html_e( $text, $domain = 'default' ) {
 	echo esc_html( translate( $text, $domain ) );
 }
 
+/**
+ * Retrieve the translation of $text and escapes it for safe use in XML output.
+ *
+ * If there is no translation, or the text domain isn't loaded, the original text
+ * is escaped and returned.
+ *
+ * @since 5.5.0
+ *
+ * @param string $text   Text to translate.
+ * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
+ *                       Default 'default'.
+ * @return string Translated text.
+ */
+function esc_xml__( $text, $domain = 'default' ) {
+	return esc_xml( translate( $text, $domain ) );
+}
+
+/**
+ * Display translated text that has been escaped for safe use in XML output.
+ *
+ * If there is no translation, or the text domain isn't loaded, the original text
+ * is escaped and displayed.
+ *
+ * If you need the value for use in PHP, use esc_xml__().
+ *
+ * @since 5.5.0
+ *
+ * @param string $text   Text to translate.
+ * @param string $domain Optional. Text domain. Unique identifier for retrieving translated strings.
+ *                       Default 'default'.
+ */
+function esc_xml_e( $text, $domain = 'default' ) {
+	echo esc_xml( translate( $text, $domain ) );
+}
+
+/**
+ * Translate string with gettext context, and escapes it for safe use in XML output.
+ *
+ * If there is no translation, or the text domain isn't loaded, the original text
+ * is escaped and returned.
+ *
+ * @since 5.5.0
+ *
+ * @param string $text    Text to translate.
+ * @param string $context Context information for the translators.
+ * @param string $domain  Optional. Text domain. Unique identifier for retrieving translated strings.
+ *                        Default 'default'.
+ * @return string Translated text.
+ */
+function esc_xml_x( $text, $context, $domain = 'default' ) {
+	return esc_xml( translate_with_gettext_context( $text, $context, $domain ) );
+}
+
 /**
  * Retrieve translated string with gettext context.
  *
diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php
index c313585d22..c49f5a5962 100644
--- a/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php
+++ b/src/wp-includes/sitemaps/class-wp-sitemaps-renderer.php
@@ -261,10 +261,10 @@ class WP_Sitemaps_Renderer {
 			wp_die(
 				sprintf(
 					/* translators: %s: SimpleXML */
-					esc_xml( __( 'Could not generate XML sitemap due to missing %s extension' ) ),
+					esc_xml__( 'Could not generate XML sitemap due to missing %s extension' ),
 					'SimpleXML'
 				),
-				esc_xml( __( 'WordPress &rsaquo; Error' ) ),
+				esc_xml__( 'WordPress &rsaquo; Error' ),
 				array(
 					'response' => 501, // "Not implemented".
 				)
diff --git a/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php b/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php
index 490541ab28..57f53c8fdf 100644
--- a/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php
+++ b/src/wp-includes/sitemaps/class-wp-sitemaps-stylesheet.php
@@ -43,25 +43,25 @@ class WP_Sitemaps_Stylesheet {
 	 */
 	public function get_sitemap_stylesheet() {
 		$css         = $this->get_stylesheet_css();
-		$title       = esc_xml( __( 'XML Sitemap' ) );
-		$description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) );
+		$title       = esc_xml__( 'XML Sitemap' );
+		$description = esc_xml__( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' );
 		$learn_more  = sprintf(
 			'<a href="%s">%s</a>',
 			esc_url( __( 'https://www.sitemaps.org/' ) ),
-			esc_xml( __( 'Learn more about XML sitemaps.' ) )
+			esc_xml__( 'Learn more about XML sitemaps.' )
 		);
 
 		$text = sprintf(
 			/* translators: %s: Number of URLs. */
-			esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ),
+			esc_xml__( 'Number of URLs in this XML Sitemap: %s.' ),
 			'<xsl:value-of select="count( sitemap:urlset/sitemap:url )" />'
 		);
 
 		$lang       = get_language_attributes( 'html' );
-		$url        = esc_xml( __( 'URL' ) );
-		$lastmod    = esc_xml( __( 'Last Modified' ) );
-		$changefreq = esc_xml( __( 'Change Frequency' ) );
-		$priority   = esc_xml( __( 'Priority' ) );
+		$url        = esc_xml__( 'URL' );
+		$lastmod    = esc_xml__( 'Last Modified' );
+		$changefreq = esc_xml__( 'Change Frequency' );
+		$priority   = esc_xml__( 'Priority' );
 
 		$xsl_content = <<<XSL
 <?xml version="1.0" encoding="UTF-8"?>
@@ -153,23 +153,23 @@ XSL;
 	 */
 	public function get_sitemap_index_stylesheet() {
 		$css         = $this->get_stylesheet_css();
-		$title       = esc_xml( __( 'XML Sitemap' ) );
-		$description = esc_xml( __( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' ) );
+		$title       = esc_xml__( 'XML Sitemap' );
+		$description = esc_xml__( 'This XML Sitemap is generated by WordPress to make your content more visible for search engines.' );
 		$learn_more  = sprintf(
 			'<a href="%s">%s</a>',
 			esc_url( __( 'https://www.sitemaps.org/' ) ),
-			esc_xml( __( 'Learn more about XML sitemaps.' ) )
+			esc_xml__( 'Learn more about XML sitemaps.' )
 		);
 
 		$text = sprintf(
 			/* translators: %s: Number of URLs. */
-			esc_xml( __( 'Number of URLs in this XML Sitemap: %s.' ) ),
+			esc_xml__( 'Number of URLs in this XML Sitemap: %s.' ),
 			'<xsl:value-of select="count( sitemap:sitemapindex/sitemap:sitemap )" />'
 		);
 
 		$lang    = get_language_attributes( 'html' );
-		$url     = esc_xml( __( 'URL' ) );
-		$lastmod = esc_xml( __( 'Last Modified' ) );
+		$url     = esc_xml__( 'URL' );
+		$lastmod = esc_xml__( 'Last Modified' );
 
 		$xsl_content = <<<XSL
 <?xml version="1.0" encoding="UTF-8"?>
-- 
2.26.2.windows.1

