From 5b369c8e2aa98769b77934bad4586325b3c98419 Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 14 Jul 2023 16:01:03 +1000 Subject: [PATCH 1/2] Adding a function to `wp_trim_excerpt` that removes footnotes from content. Adds tests --- src/wp-includes/blocks.php | 18 +++++++ src/wp-includes/formatting.php | 3 ++ .../formatting/excerptRemoveFootnotes.php | 47 +++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 tests/phpunit/tests/formatting/excerptRemoveFootnotes.php diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index fd287370de9a1..597c17b01706f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1009,6 +1009,24 @@ function excerpt_remove_blocks( $content ) { return $output; } +/** + * Parses footnotes markup out of a content string, + * and renders those appropriate for the excerpt. + * + * @since 6.3.0 + * + * @param string $content The content to parse. + * @return string The parsed and filtered content. + */ +function excerpt_remove_footnotes( $content ) { + if ( false === strpos( $content, 'data-fn=' ) ) { + return $content; + } + + static $footnote_pattern = '_\s*\d+\s*_'; + return preg_replace( $footnote_pattern, '', $content ); +} + /** * Renders inner blocks from the allowed wrapper blocks * for generating an excerpt. diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index a3e35599c0b1e..9ce636edd5b1d 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3952,6 +3952,7 @@ function human_time_diff( $from, $to = 0 ) { * * @since 1.5.0 * @since 5.2.0 Added the `$post` parameter. + * @since 6.3.0 Removes footnotes markup from the excerpt content. * * @param string $text Optional. The excerpt. If set to empty, an excerpt is generated. * @param WP_Post|object|int $post Optional. WP_Post instance or Post ID/object. Default null. @@ -3966,6 +3967,7 @@ function wp_trim_excerpt( $text = '', $post = null ) { $text = strip_shortcodes( $text ); $text = excerpt_remove_blocks( $text ); + $text = excerpt_remove_footnotes( $text ); /* * Temporarily unhook wp_filter_content_tags() since any tags @@ -4008,6 +4010,7 @@ function wp_trim_excerpt( $text = '', $post = null ) { */ $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[…]' ); $text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); + } /** diff --git a/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php b/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php new file mode 100644 index 0000000000000..22fb229b8c10c --- /dev/null +++ b/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php @@ -0,0 +1,47 @@ +assertSame( $expected, excerpt_remove_footnotes( $content ), $message ); + } + + public function data_remove_footnotes() { + return array( + array( + '

This is a paragraph1.

', + '

This is a paragraph1.

', + 'Should not replace if no footnote attribute is found in the content.', + ), + array( + '

This is a paragraph.

', + '

This is a paragraph1.

', + 'Footnote should be removed from HTML content.', + ), + array( + '', + '', + 'Multiple footnotes should be removed from block content.', + ), + array( + '

これは見出しです

', + '

これは1見出しです2

', + 'Footnotes around non latin script should be removed from HTML content.', + ), + ); + } +} From ef262a3f4bbde2e3ebdc3623018d425b92d6476c Mon Sep 17 00:00:00 2001 From: ramon Date: Fri, 14 Jul 2023 18:03:52 +1000 Subject: [PATCH 2/2] Formatting tests Prefer PHP 8's str_contains --- src/wp-includes/blocks.php | 9 ++-- .../formatting/excerptRemoveFootnotes.php | 43 ++++++++++--------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 597c17b01706f..347839f09a1e1 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1019,12 +1019,15 @@ function excerpt_remove_blocks( $content ) { * @return string The parsed and filtered content. */ function excerpt_remove_footnotes( $content ) { - if ( false === strpos( $content, 'data-fn=' ) ) { + if ( ! str_contains( $content, 'data-fn=' ) ) { return $content; } - static $footnote_pattern = '_\s*\d+\s*_'; - return preg_replace( $footnote_pattern, '', $content ); + return preg_replace( + '_\s*\d+\s*_', + '', + $content + ); } /** diff --git a/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php b/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php index 22fb229b8c10c..12ea3acb04cbe 100644 --- a/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php +++ b/tests/phpunit/tests/formatting/excerptRemoveFootnotes.php @@ -12,35 +12,36 @@ class Tests_Formatting_ExcerptRemoveFootnotes extends WP_UnitTestCase { * * @dataProvider data_remove_footnotes * - * @param string $expected Expected output. - * @param string $content Content to run strip_shortcodes() on. - * @param string $message The test message. + * @param string $expected Expected output. + * @param string $content Content to run strip_shortcodes() on. */ - public function test_remove_footnotes( $expected, $content, $message = '' ) { - $this->assertSame( $expected, excerpt_remove_footnotes( $content ), $message ); + public function test_remove_footnotes( $expected, $content ) { + $this->assertSame( $expected, excerpt_remove_footnotes( $content ) ); } + /** + * Data provider. + * + * @return array + */ public function data_remove_footnotes() { return array( - array( - '

This is a paragraph1.

', - '

This is a paragraph1.

', - 'Should not replace if no footnote attribute is found in the content.', + 'no footnote' => array( + 'expected' => '

This is a paragraph1.

', + 'content' => '

This is a paragraph1.

', ), - array( - '

This is a paragraph.

', - '

This is a paragraph1.

', - 'Footnote should be removed from HTML content.', + 'one footnote' => array( + 'expected' => '

This is a paragraph.

', + 'content' => '

This is a paragraph1.

', + ), - array( - '', - '', - 'Multiple footnotes should be removed from block content.', + 'multiple footnotes in block content' => array( + 'expected' => '', + 'content' => '', ), - array( - '

これは見出しです

', - '

これは1見出しです2

', - 'Footnotes around non latin script should be removed from HTML content.', + 'footnotes around non-latin script' => array( + 'expected' => '

これは見出しです

', + 'content' => '

これは1見出しです2

', ), ); }