From 294922a0a21b375cf7681af1caa3d3c5ee890439 Mon Sep 17 00:00:00 2001 From: Pio Concepcion Date: Mon, 25 Apr 2022 18:37:13 +0800 Subject: [PATCH 1/8] Fixes excerpt if there are cjk chars. --- src/block/posts/index.php | 22 +++++++++++++++++++++- src/block/posts/util.js | 13 +++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/block/posts/index.php b/src/block/posts/index.php index dffd2d997b..0371c2d795 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -115,7 +115,8 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t } else { $excerpt = implode( ' ', $excerpt ); } - + // Trim the excerpt differently if there are chinese characters. + $excerpt = filter_cjk_characters( $excerpt, $trim_to_length ); $excerpt = wp_kses_post( $excerpt ); $new_template = str_replace( '!#excerpt!#', $excerpt, $new_template ); } else { @@ -131,6 +132,25 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t return $new_template; } + + /** + * Checks if there are cjk characters in the string + * and cuts it based on character count. + * + * @since 3.3.2 + * @param string | string $excerpt + * @param int | int $trim_to_length + * + * @return string trimmed excerpt if it contains a cjk character, original excerpt if not. + */ + function filter_cjk_characters( $excerpt, $trim_to_length ) { + $chinese_excerpt = preg_match_all("/\p{Han}+/u", $excerpt, $matches); + if($chinese_excerpt) { + $sample = mb_substr( $excerpt, 0, $trim_to_length ) . '...'; + return $sample; + } + return $excerpt; + } } if ( ! function_exists( 'generate_post_query_from_stackable_posts_block' ) ) { diff --git a/src/block/posts/util.js b/src/block/posts/util.js index 4b7e954329..541cbb178b 100644 --- a/src/block/posts/util.js +++ b/src/block/posts/util.js @@ -26,6 +26,16 @@ import { applyFilters } from '@wordpress/hooks' */ import variations from './variations' +// Check if there are east asian characters. Trim with regards to each char length rather than word count. +const trimCJKCharacters = ( excerpt, excerptLength ) => { + const regexCJK = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/ + if ( excerpt.match( regexCJK ) ) { + const outputExcerpt = excerpt.substring( 0, excerptLength ) + '...' + return outputExcerpt + } + return excerpt +} + export const META_SEPARATORS = { dot: '·', space: ' ', @@ -205,6 +215,9 @@ export const generateRenderPostItem = attributes => { excerptString = post.post_excerpt_stackable } + // Trim the excerpt differently if there are cjk characters. + excerptString = trimCJKCharacters( excerptString, excerptLength ) + const excerpt = excerptString && (
Date: Fri, 6 May 2022 22:49:18 +0800 Subject: [PATCH 2/8] Changed implementation. --- src/block/posts/index.php | 11 ++++++++--- src/block/posts/util.js | 30 ++++++++++++++++-------------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/block/posts/index.php b/src/block/posts/index.php index 0371c2d795..f255061dd0 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -108,15 +108,20 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t // Trim the excerpt. if ( ! empty( $excerpt ) ) { + $untrimmed_excerpt = $excerpt; $excerpt = explode( ' ', $excerpt ); $trim_to_length = (int) $excerpt_length; - if ( count( $excerpt ) > $trim_to_length ) { + // $excerpt = filter_cjk_characters( $excerpt, $trim_to_length ); + if ( preg_match_all("/\p{Han}+/u", $untrimmed_excerpt, $matches) ) { + if( strlen( $untrimmed_excerpt ) > $trim_to_length ) { + $excerpt = mb_substr( $untrimmed_excerpt, 0, $trim_to_length ) . '...'; + } + $excerpt = $untrimmed_excerpt; + } elseif ( count( $excerpt ) > $trim_to_length ) { $excerpt = implode( ' ', array_slice( $excerpt, 0, $trim_to_length ) ) . '...'; } else { $excerpt = implode( ' ', $excerpt ); } - // Trim the excerpt differently if there are chinese characters. - $excerpt = filter_cjk_characters( $excerpt, $trim_to_length ); $excerpt = wp_kses_post( $excerpt ); $new_template = str_replace( '!#excerpt!#', $excerpt, $new_template ); } else { diff --git a/src/block/posts/util.js b/src/block/posts/util.js index 541cbb178b..fdae5bb8d0 100644 --- a/src/block/posts/util.js +++ b/src/block/posts/util.js @@ -26,16 +26,6 @@ import { applyFilters } from '@wordpress/hooks' */ import variations from './variations' -// Check if there are east asian characters. Trim with regards to each char length rather than word count. -const trimCJKCharacters = ( excerpt, excerptLength ) => { - const regexCJK = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/ - if ( excerpt.match( regexCJK ) ) { - const outputExcerpt = excerpt.substring( 0, excerptLength ) + '...' - return outputExcerpt - } - return excerpt -} - export const META_SEPARATORS = { dot: '·', space: ' ', @@ -209,15 +199,27 @@ export const generateRenderPostItem = attributes => { // Trim the excerpt. let excerptString = postExcerptStackable.split( ' ' ) - if ( excerptString.length > ( excerptLength || 55 ) ) { + + //Checks if there are CJK characters present + const regexCJK = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/ + if ( postExcerptStackable.match( regexCJK ) ) { + if ( postExcerptStackable.length > excerptLength || 55 ) { + if ( excerptLength === 1 ) { + excerptString = postExcerptStackable.substring( 3, 4 ) + '...' + } else { + excerptString = postExcerptStackable.substring( 3, excerptLength === '' ? 55 : excerptLength + 3 ) + '...' + } + // Outerconditional is always true even though postExcerptStackable.length < 55. + if ( postExcerptStackable.length < 55 && excerptLength === '' ) { + excerptString = postExcerptStackable + } + } + } else if ( excerptString.length > ( excerptLength || 55 ) ) { excerptString = excerptString.slice( 0, excerptLength || 55 ).join( ' ' ) + '...' } else { excerptString = post.post_excerpt_stackable } - // Trim the excerpt differently if there are cjk characters. - excerptString = trimCJKCharacters( excerptString, excerptLength ) - const excerpt = excerptString && (
Date: Fri, 6 May 2022 22:50:09 +0800 Subject: [PATCH 3/8] Added comments. --- src/block/posts/index.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/block/posts/index.php b/src/block/posts/index.php index f255061dd0..275f16fcfb 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -111,9 +111,11 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t $untrimmed_excerpt = $excerpt; $excerpt = explode( ' ', $excerpt ); $trim_to_length = (int) $excerpt_length; - // $excerpt = filter_cjk_characters( $excerpt, $trim_to_length ); + + // Check if there are CJK characters. if ( preg_match_all("/\p{Han}+/u", $untrimmed_excerpt, $matches) ) { if( strlen( $untrimmed_excerpt ) > $trim_to_length ) { + // Trim according to string length. $excerpt = mb_substr( $untrimmed_excerpt, 0, $trim_to_length ) . '...'; } $excerpt = $untrimmed_excerpt; From 1c12f913973111a444ffa20b76eb9a4442828d17 Mon Sep 17 00:00:00 2001 From: Pio Concepcion Date: Fri, 6 May 2022 22:51:03 +0800 Subject: [PATCH 4/8] Removed function. --- src/block/posts/index.php | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/block/posts/index.php b/src/block/posts/index.php index 275f16fcfb..d54c042839 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -139,25 +139,6 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t return $new_template; } - - /** - * Checks if there are cjk characters in the string - * and cuts it based on character count. - * - * @since 3.3.2 - * @param string | string $excerpt - * @param int | int $trim_to_length - * - * @return string trimmed excerpt if it contains a cjk character, original excerpt if not. - */ - function filter_cjk_characters( $excerpt, $trim_to_length ) { - $chinese_excerpt = preg_match_all("/\p{Han}+/u", $excerpt, $matches); - if($chinese_excerpt) { - $sample = mb_substr( $excerpt, 0, $trim_to_length ) . '...'; - return $sample; - } - return $excerpt; - } } if ( ! function_exists( 'generate_post_query_from_stackable_posts_block' ) ) { From 4b5171fed8b8573f6e98dcc9f020a5ec8e4ea359 Mon Sep 17 00:00:00 2001 From: Pio Concepcion Date: Fri, 6 May 2022 23:04:09 +0800 Subject: [PATCH 5/8] Changed the conditional. --- src/block/posts/index.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/block/posts/index.php b/src/block/posts/index.php index d54c042839..5f73fbdce8 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -116,9 +116,10 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t if ( preg_match_all("/\p{Han}+/u", $untrimmed_excerpt, $matches) ) { if( strlen( $untrimmed_excerpt ) > $trim_to_length ) { // Trim according to string length. - $excerpt = mb_substr( $untrimmed_excerpt, 0, $trim_to_length ) . '...'; + $excerpt = mb_substr( $untrimmed_excerpt, 3, $trim_to_length ) . '...'; + } else { + $excerpt = $untrimmed_excerpt; } - $excerpt = $untrimmed_excerpt; } elseif ( count( $excerpt ) > $trim_to_length ) { $excerpt = implode( ' ', array_slice( $excerpt, 0, $trim_to_length ) ) . '...'; } else { From 31d2f020e593db843d342df0c30a75ce1a2a216f Mon Sep 17 00:00:00 2001 From: Pio Concepcion Date: Tue, 10 May 2022 20:22:40 +0800 Subject: [PATCH 6/8] Changed spacing. --- src/block/posts/index.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/block/posts/index.php b/src/block/posts/index.php index 5f73fbdce8..df585ee673 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -113,8 +113,8 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t $trim_to_length = (int) $excerpt_length; // Check if there are CJK characters. - if ( preg_match_all("/\p{Han}+/u", $untrimmed_excerpt, $matches) ) { - if( strlen( $untrimmed_excerpt ) > $trim_to_length ) { + if ( function_exists( 'mb_substr' ) && preg_match_all( "/\p{Han}+/u", $untrimmed_excerpt, $matches ) ) { + if ( strlen( $untrimmed_excerpt ) > $trim_to_length ) { // Trim according to string length. $excerpt = mb_substr( $untrimmed_excerpt, 3, $trim_to_length ) . '...'; } else { @@ -123,7 +123,7 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t } elseif ( count( $excerpt ) > $trim_to_length ) { $excerpt = implode( ' ', array_slice( $excerpt, 0, $trim_to_length ) ) . '...'; } else { - $excerpt = implode( ' ', $excerpt ); + $excerpt = $untrimmed_excerpt; } $excerpt = wp_kses_post( $excerpt ); $new_template = str_replace( '!#excerpt!#', $excerpt, $new_template ); From 9f552526fb4ec28314e3af8be32c88cda4bc5d48 Mon Sep 17 00:00:00 2001 From: Pio Concepcion Date: Tue, 10 May 2022 20:22:58 +0800 Subject: [PATCH 7/8] Changed implementation. --- src/block/posts/util.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/block/posts/util.js b/src/block/posts/util.js index fdae5bb8d0..75a96b910d 100644 --- a/src/block/posts/util.js +++ b/src/block/posts/util.js @@ -203,16 +203,13 @@ export const generateRenderPostItem = attributes => { //Checks if there are CJK characters present const regexCJK = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/ if ( postExcerptStackable.match( regexCJK ) ) { - if ( postExcerptStackable.length > excerptLength || 55 ) { - if ( excerptLength === 1 ) { - excerptString = postExcerptStackable.substring( 3, 4 ) + '...' - } else { - excerptString = postExcerptStackable.substring( 3, excerptLength === '' ? 55 : excerptLength + 3 ) + '...' - } - // Outerconditional is always true even though postExcerptStackable.length < 55. - if ( postExcerptStackable.length < 55 && excerptLength === '' ) { - excerptString = postExcerptStackable - } + // Extract the string inbetween the tags and newline. + const tempExcerptString = postExcerptStackable.match( '

(.*)<\/p>\\n' ) + + if ( tempExcerptString[ 1 ].length > ( excerptLength || 55 ) ) { + excerptString = tempExcerptString[ 1 ].substring( 0, excerptLength === '' ? 55 : excerptLength ) + '...' + } else { + excerptString = tempExcerptString[ 1 ] } } else if ( excerptString.length > ( excerptLength || 55 ) ) { excerptString = excerptString.slice( 0, excerptLength || 55 ).join( ' ' ) + '...' From 4b51d48b5453200b9a0fbafc87ab81e38bc14e0a Mon Sep 17 00:00:00 2001 From: Pio Concepcion Date: Thu, 12 May 2022 06:41:52 +0800 Subject: [PATCH 8/8] Changed implementation. --- src/block/posts/index.php | 6 ++++-- src/block/posts/util.js | 13 +++++++------ 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/block/posts/index.php b/src/block/posts/index.php index df585ee673..a2a4aebbeb 100644 --- a/src/block/posts/index.php +++ b/src/block/posts/index.php @@ -111,12 +111,14 @@ function generate_render_item_from_stackable_posts_block( $post, $attributes, $t $untrimmed_excerpt = $excerpt; $excerpt = explode( ' ', $excerpt ); $trim_to_length = (int) $excerpt_length; - // Check if there are CJK characters. if ( function_exists( 'mb_substr' ) && preg_match_all( "/\p{Han}+/u", $untrimmed_excerpt, $matches ) ) { + // Remove the

tags. + $untrimmed_excerpt = substr( $untrimmed_excerpt, 3, strlen( $untrimmed_excerpt ) - 8 ); + if ( strlen( $untrimmed_excerpt ) > $trim_to_length ) { // Trim according to string length. - $excerpt = mb_substr( $untrimmed_excerpt, 3, $trim_to_length ) . '...'; + $excerpt = mb_substr( $untrimmed_excerpt, 0, $trim_to_length ) . '...'; } else { $excerpt = $untrimmed_excerpt; } diff --git a/src/block/posts/util.js b/src/block/posts/util.js index 75a96b910d..ea4fd76513 100644 --- a/src/block/posts/util.js +++ b/src/block/posts/util.js @@ -200,16 +200,17 @@ export const generateRenderPostItem = attributes => { // Trim the excerpt. let excerptString = postExcerptStackable.split( ' ' ) - //Checks if there are CJK characters present + //Checks if there are CJK characters present. const regexCJK = /[\u3040-\u30ff\u3400-\u4dbf\u4e00-\u9fff\uf900-\ufaff\uff66-\uff9f]/ + if ( postExcerptStackable.match( regexCJK ) ) { - // Extract the string inbetween the tags and newline. - const tempExcerptString = postExcerptStackable.match( '

(.*)<\/p>\\n' ) + // Remove the

tags. + const tempExcerptString = postExcerptStackable.substring( 3, postExcerptStackable.length - 5 ) - if ( tempExcerptString[ 1 ].length > ( excerptLength || 55 ) ) { - excerptString = tempExcerptString[ 1 ].substring( 0, excerptLength === '' ? 55 : excerptLength ) + '...' + if ( tempExcerptString.length > ( excerptLength || 55 ) ) { + excerptString = tempExcerptString.substring( 0, excerptLength === '' ? 55 : excerptLength ) + '...' } else { - excerptString = tempExcerptString[ 1 ] + excerptString = tempExcerptString } } else if ( excerptString.length > ( excerptLength || 55 ) ) { excerptString = excerptString.slice( 0, excerptLength || 55 ).join( ' ' ) + '...'