Skip to content

Commit

Permalink
Editor: trim footnote anchors from post excerpts.
Browse files Browse the repository at this point in the history
Adds the `excerpt_remove_footnotes` function to trim footnote anchors from post excerpts.

Props: ramonopoly, costdev, mukesh27, mcsf, azaozz.
Fixes #58805.


git-svn-id: https://develop.svn.wordpress.org/trunk@56244 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
tellthemachines committed Jul 17, 2023
1 parent b4b0c03 commit 2beefda
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,27 @@ 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 ( ! str_contains( $content, 'data-fn=' ) ) {
return $content;
}

return preg_replace(
'_<sup data-fn="[^"]+" class="[^"]+">\s*<a href="[^"]+" id="[^"]+">\d+</a>\s*</sup>_',
'',
$content
);
}

/**
* Renders inner blocks from the allowed wrapper blocks
* for generating an excerpt.
Expand Down
3 changes: 3 additions & 0 deletions src/wp-includes/formatting.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -4008,6 +4010,7 @@ function wp_trim_excerpt( $text = '', $post = null ) {
*/
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );

}

/**
Expand Down
48 changes: 48 additions & 0 deletions tests/phpunit/tests/formatting/excerptRemoveFootnotes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* @group formatting
* @ticket 58805
*
* @covers ::excerpt_remove_footnotes
*/

class Tests_Formatting_ExcerptRemoveFootnotes extends WP_UnitTestCase {
/**
* @ticket 58805
*
* @dataProvider data_remove_footnotes
*
* @param string $expected Expected output.
* @param string $content Content to run strip_shortcodes() on.
*/
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(
'no footnote' => array(
'expected' => '<p>This is a paragraph<sup class="fn" id="1"><a href="#1" id="1a">1</a></sup>.</p>',
'content' => '<p>This is a paragraph<sup class="fn" id="1"><a href="#1" id="1a">1</a></sup>.</p>',
),
'one footnote' => array(
'expected' => '<p>This is a <a href="https://wordpress.org" data-type="URL" data-id="https://wordpress.org">paragraph</a>.</p>',
'content' => '<p>This is a <a href="https://wordpress.org" data-type="URL" data-id="https://wordpress.org">paragraph</a><sup data-fn="d3b825b6-1890-4cb3-b276-002137515e99" class="fn"><a href="#d3b825b6-1890-4cb3-b276-002137515e99" id="d3b825b6-1890-4cb3-b276-002137515e99-link">1</a></sup>.</p>',

),
'multiple footnotes in block content' => array(
'expected' => '<!-- wp:list --><ul><!-- wp:list-item --><li><strong>This</strong><em><strong><sup></sup></strong></em><strong> is a list</strong></li><!-- /wp:list-item --></ul><!-- /wp:list -->',
'content' => '<!-- wp:list --><ul><!-- wp:list-item --><li><strong>This</strong><em><strong><sup><sup data-fn="e2fce624-74a5-4068-a20c-6ef793f1644c" class="fn"><a href="#e2fce624-74a5-4068-a20c-6ef793f1644c" id="e2fce624-74a5-4068-a20c-6ef793f1644c-link">2</a></sup></sup></strong></em><strong> is a list</strong><sup data-fn="ea7e892e-7bc2-424b-936b-36ec64f1c2fc" class="fn"><a href="#ea7e892e-7bc2-424b-936b-36ec64f1c2fc" id="ea7e892e-7bc2-424b-936b-36ec64f1c2fc-link">3</a></sup></li><!-- /wp:list-item --></ul><!-- /wp:list -->',
),
'footnotes around non-latin script' => array(
'expected' => '<h2 class="wp-block-heading has-background" style="background-color:#f93b3b">これは見出しです</h2>',
'content' => '<h2 class="wp-block-heading has-background" style="background-color:#f93b3b">これは<sup data-fn="382b3e39-4b0d-4b83-8461-c13f82fdbcfb" class="fn"><a href="#382b3e39-4b0d-4b83-8461-c13f82fdbcfb" id="382b3e39-4b0d-4b83-8461-c13f82fdbcfb-link">1</a></sup>見出しです<sup data-fn="addb0459-a048-453a-9101-dba64f63a630" class="fn"><a href="#addb0459-a048-453a-9101-dba64f63a630" id="addb0459-a048-453a-9101-dba64f63a630-link">2</a></sup></h2>',
),
);
}
}

0 comments on commit 2beefda

Please sign in to comment.