From 34d890a93fe5d14b727893cc8cfe4f4a9cd7f764 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Fri, 3 Nov 2023 15:19:25 -0500 Subject: [PATCH 1/3] Sitemaps: preserve linebreaks --- .../plugins/jetpack/changelog/add-line-breaks-sitemap | 4 ++++ .../plugins/jetpack/modules/sitemaps/sitemap-buffer.php | 8 +++++--- 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 projects/plugins/jetpack/changelog/add-line-breaks-sitemap diff --git a/projects/plugins/jetpack/changelog/add-line-breaks-sitemap b/projects/plugins/jetpack/changelog/add-line-breaks-sitemap new file mode 100644 index 0000000000000..6f1e8e76e275e --- /dev/null +++ b/projects/plugins/jetpack/changelog/add-line-breaks-sitemap @@ -0,0 +1,4 @@ +Significance: patch +Type: bugfix + +Sitemaps: include line breaks for easier debugging diff --git a/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php b/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php index b2aac925fd3a6..91d6541d5b1cb 100644 --- a/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php +++ b/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php @@ -270,9 +270,11 @@ protected function array_to_xml_string( $array, $parent = null, $root = null ) { $return_string = false; if ( null === $parent ) { - $return_string = true; - $root = new DOMDocument(); - $parent = $root; + $return_string = true; + $root = new DOMDocument(); + $root->formatOutput = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $root->preserveWhiteSpace = false; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase + $parent = $root; } if ( is_array( $array ) ) { From e214b8c2f3f2dae96b39b53c32bb6054a9190e0b Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Fri, 7 Feb 2025 08:45:02 -0600 Subject: [PATCH 2/3] Set formatting options --- .../jetpack/modules/sitemaps/sitemap-buffer.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php b/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php index 91d6541d5b1cb..f61781955c087 100644 --- a/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php +++ b/projects/plugins/jetpack/modules/sitemaps/sitemap-buffer.php @@ -110,8 +110,10 @@ public function __construct( $item_limit, $byte_limit, $time ) { $this->is_full_flag = false; $this->timestamp = $time; - $this->finder = new Jetpack_Sitemap_Finder(); - $this->doc = new DOMDocument( '1.0', 'UTF-8' ); + $this->finder = new Jetpack_Sitemap_Finder(); + $this->doc = new DOMDocument( '1.0', 'UTF-8' ); + $this->doc->formatOutput = true; + $this->doc->preserveWhiteSpace = false; $this->item_capacity = max( 1, (int) $item_limit ); $this->byte_capacity = max( 1, (int) $byte_limit ) - strlen( $this->doc->saveXML() ); @@ -270,11 +272,9 @@ protected function array_to_xml_string( $array, $parent = null, $root = null ) { $return_string = false; if ( null === $parent ) { - $return_string = true; - $root = new DOMDocument(); - $root->formatOutput = true; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - $root->preserveWhiteSpace = false; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase - $parent = $root; + $return_string = true; + $root = new DOMDocument(); + $parent = $root; } if ( is_array( $array ) ) { From 0b4a8fd83c51f4ec7e5a9bc8678d3e69ed0b6b72 Mon Sep 17 00:00:00 2001 From: Brandon Kraft Date: Fri, 7 Feb 2025 10:20:04 -0600 Subject: [PATCH 3/3] Update tests --- .../sitemaps/test-class.sitemap-buffer.php | 34 +++++++++---------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/projects/plugins/jetpack/tests/php/modules/sitemaps/test-class.sitemap-buffer.php b/projects/plugins/jetpack/tests/php/modules/sitemaps/test-class.sitemap-buffer.php index 27a52aacc6142..75d6b4b30498c 100644 --- a/projects/plugins/jetpack/tests/php/modules/sitemaps/test-class.sitemap-buffer.php +++ b/projects/plugins/jetpack/tests/php/modules/sitemaps/test-class.sitemap-buffer.php @@ -226,21 +226,6 @@ public function test_news_sitemap_item_to_xml() { ), ); - $xml = '' . PHP_EOL - . '' - . 'http://example.com/blog-url-about-stuff' - . "$timestamp" - . '' - . '' - . 'Blog about stuff' - . 'en' - . '' - . 'Stuff with stuff to escape, like less than signs: < and ampersands: &' - . "$timestamp" - . 'Blog with some already escaped stuff: &amp;&#321;' - . '' - . '' . PHP_EOL; - foreach ( array( new Jetpack_Sitemap_Buffer_Dummy( JP_SITEMAP_MAX_ITEMS, JP_SITEMAP_MAX_BYTES, $timestamp ), @@ -249,9 +234,24 @@ public function test_news_sitemap_item_to_xml() { ) { $buffer->append( $array ); + // Normalize the XML by removing whitespace between tags and normalizing newlines + $actual = preg_replace( '/>\s+<', $buffer->contents() ); + $actual = str_replace( "\r\n", "\n", $actual ); + + $expected = 'http://example.com/blog-url-about-stuff' + . "$timestamp" + . '' + . 'Blog about stuff' + . 'en' + . '' + . 'Stuff with stuff to escape, like less than signs: < and ampersands: &' + . "$timestamp" + . 'Blog with some already escaped stuff: &amp;&#321;' + . '' . "\n"; + $this->assertEquals( - $xml, - $buffer->contents(), + $expected, + $actual, get_class( $buffer ) ); }