From a47c9cd7aeb923298cc9301ec698f725399cd54b Mon Sep 17 00:00:00 2001 From: Kan-Ru Chen Date: Sat, 16 Dec 2023 15:36:45 +0900 Subject: [PATCH] replace `excerpt_more` with custom filter `activitypub_excerpt_more` for better consistency (#610) --- includes/class-shortcodes.php | 3 +-- tests/test-class-activitypub-shortcodes.php | 30 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/includes/class-shortcodes.php b/includes/class-shortcodes.php index 491a6ad..922c10d 100644 --- a/includes/class-shortcodes.php +++ b/includes/class-shortcodes.php @@ -127,8 +127,7 @@ class Shortcodes { // Strip out any remaining tags. $excerpt = \wp_strip_all_tags( $excerpt ); - /** This filter is documented in wp-includes/formatting.php */ - $excerpt_more = \apply_filters( 'excerpt_more', ' [...]' ); + $excerpt_more = \apply_filters( 'activitypub_excerpt_more', ' […]' ); $excerpt_more_len = strlen( $excerpt_more ); // We now have a excerpt, but we need to check it's length, it may be longer than we want for two reasons: diff --git a/tests/test-class-activitypub-shortcodes.php b/tests/test-class-activitypub-shortcodes.php index b1413e8..7b592e0 100644 --- a/tests/test-class-activitypub-shortcodes.php +++ b/tests/test-class-activitypub-shortcodes.php @@ -62,4 +62,34 @@ class Test_Activitypub_Shortcodes extends WP_UnitTestCase { $this->assertEquals( '', $content ); Shortcodes::unregister(); } + + public function test_excerpt() { + Shortcodes::register(); + global $post; + + $post_id = -97; // negative ID, to avoid clash with a valid post + $post = new stdClass(); + $post->ID = $post_id; + $post->post_author = 1; + $post->post_date = current_time( 'mysql' ); + $post->post_date_gmt = current_time( 'mysql', 1 ); + $post->post_title = 'Some title or other'; + $post->post_content = 'Lorem ipsum dolor sit amet, consectetur.'; + $post->post_status = 'publish'; + $post->comment_status = 'closed'; + $post->ping_status = 'closed'; + $post->post_name = 'fake-page-' . rand( 1, 99999 ); // append random number to avoid clash + $post->post_type = 'page'; + $post->filter = 'raw'; // important! + + $content = '[ap_excerpt length="25"]'; + + // Fill in the shortcodes. + setup_postdata( $post ); + $content = do_shortcode( $content ); + wp_reset_postdata(); + + $this->assertEquals( "

Lorem ipsum […]

\n", $content ); + Shortcodes::unregister(); + } }