From f9a12d09e79cd27800e4e7396c008c9c74a398f9 Mon Sep 17 00:00:00 2001 From: Eru Penkman Date: Mon, 19 Dec 2022 09:21:11 +1000 Subject: [PATCH] trim to periods if they exist in the title --- packages/block-library/src/rss/index.php | 28 +++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/rss/index.php b/packages/block-library/src/rss/index.php index e860a09480aae..342e95563f976 100644 --- a/packages/block-library/src/rss/index.php +++ b/packages/block-library/src/rss/index.php @@ -20,8 +20,30 @@ function trim_to_word_boundary( $to_trim, $character_count, $more = ' […] return $to_trim; } - preg_match( '/^(.{0,' . $character_count . '})\s/', $to_trim, $parts ); - return $parts[1] . $more; + preg_match( '/^.{0,' . $character_count . '}\s/', $to_trim, $parts ); + return $parts[0] . $more; +} + +/** + * Helper funtion to trim text up to the first period (.) followed by a space. + * If no period is found, the text is trimmed to the nearest word boundary. + * If multiple periods are found, the last one within $title_length is used. + * + * @param string $to_trim The text to trim. + * @param int $title_length The maximum number of characters to return. + * @param string $more The string to append to the trimmed text. + */ +function trim_title_from_excerpt( $to_trim, $title_length, $more = ' […]' ) { + $to_trim = trim_to_word_boundary($to_trim, $title_length, $more ); + + // Match to the first period followed by a space. + preg_match( '/^.{0,' . $title_length . '}\.\s/', $to_trim, $parts ); + + if ( empty( $parts[0] ) ) { + return $to_trim; + } + + return $parts[0] . $more; } /** @@ -57,7 +79,7 @@ function render_block_core_rss( $attributes ) { if ( empty( $title ) ) { // If the title is empty, use the begining of the excerpt instead. - $title = trim_to_word_boundary( $excerpt_trimmed, $attributes['titleLength'], '' ); + $title = trim_title_from_excerpt( $excerpt_trimmed, $attributes['titleLength'], '' ); if ( $attributes['displayExcerpt'] ) { // Trim out the words that were used for the title. $excerpt_trimmed = trim( substr( $excerpt_trimmed, strlen( $title ) ) );