Excerpt will display properly if there are chinese-japanese-korean characters. #2262
Excerpt will display properly if there are chinese-japanese-korean characters. #2262prconcepcion wants to merge 9 commits intodevelopfrom
Conversation
|
Size Change: +1.1 kB (0%) Total Size: 2.75 MB
ℹ️ View Unchanged
|
| // Trim the excerpt differently if there are chinese characters. | ||
| $excerpt = filter_cjk_characters( $excerpt, $trim_to_length ); |
There was a problem hiding this comment.
While this would technically work, the logic is off. The CJK trimming shouldn't be called after the if/else statement, because this would mean that the strings will always be trimmed and will be trimmed twice.
Our logic above is as follows (in the if statement):
- If the excerpt is too long, trim it
- If not, just use as-is
The logic in the PR is:
- If the excerpt is too long, trim it
- If not, just use as-is
- After 1 & 2, trim CJK characters
The trimming of CJK should make the logic be:
- If the excerpt has CJK characters, trim it if it's too long,
- If the excerpt doesn't have CJK characters but it's too long, trim it
- If not, just use as-is
| } elseif ( count( $excerpt ) > $trim_to_length ) { | ||
| $excerpt = implode( ' ', array_slice( $excerpt, 0, $trim_to_length ) ) . '...'; | ||
| } else { | ||
| $excerpt = implode( ' ', $excerpt ); |
There was a problem hiding this comment.
| $excerpt = implode( ' ', $excerpt ); | |
| $excerpt = $untrimmed_excerpt; |
| if ( count( $excerpt ) > $trim_to_length ) { | ||
|
|
||
| // Check if there are CJK characters. | ||
| if ( preg_match_all("/\p{Han}+/u", $untrimmed_excerpt, $matches) ) { |
There was a problem hiding this comment.
Some recommendations:
- Mind the spaces.
mb_substrisn't always available across all PHP installations, we need to also check for its existence.
| if ( preg_match_all("/\p{Han}+/u", $untrimmed_excerpt, $matches) ) { | |
| if ( function_exists( 'mb_substr' ) && preg_match_all( "/\p{Han}+/u", $untrimmed_excerpt, $matches ) ) { |
|
|
||
| // Check if there are CJK characters. | ||
| if ( preg_match_all("/\p{Han}+/u", $untrimmed_excerpt, $matches) ) { | ||
| if( strlen( $untrimmed_excerpt ) > $trim_to_length ) { |
There was a problem hiding this comment.
| if( strlen( $untrimmed_excerpt ) > $trim_to_length ) { | |
| if ( strlen( $untrimmed_excerpt ) > $trim_to_length ) { |
| 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 | ||
| } |
There was a problem hiding this comment.
-
Why is there a different line for when the
excerptLengthis1, there should be no reason to split this into anif-else -
In this
postExcerptStackable.substring( 3, 4 ), why is the string trimming process started on index3? Shouldn't it start with0? -
What is the last
ifstatement for? Looks unnecessary.
Won't this do? Or is there an edge case where this doesn't work?
| 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 | |
| } | |
| excerptString = postExcerptStackable.substring( 0, excerptLength || 55 ) + '...' |
bfintal
left a comment
There was a problem hiding this comment.
Please see inline comments for some enhancements
| 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 ) . '...'; |
There was a problem hiding this comment.
Why does the index start with 3, shouldn't it be 0?
| $excerpt = mb_substr( $untrimmed_excerpt, 3, $trim_to_length ) . '...'; | |
| $excerpt = mb_substr( $untrimmed_excerpt, 0, $trim_to_length ) . '...'; |
| 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>(.*)<\/p>\\n' ) |
There was a problem hiding this comment.
What scenario is this needed?
There was a problem hiding this comment.
The string that I obtained when I would use the postExcerptStackable would contain the <p> which is why I made it get the string between those tags and this is also the reason why I made the function substing(3,excerptLength+3) start at 3 before and then also added a +3. I also saw this in the $untrimmed_excerpt in the php file that is why I also made the index mb_substr( $untrimmed_excerpt, 3, ... start at 3.
| if ( tempExcerptString[ 1 ].length > ( excerptLength || 55 ) ) { | ||
| excerptString = tempExcerptString[ 1 ].substring( 0, excerptLength === '' ? 55 : excerptLength ) + '...' | ||
| } else { | ||
| excerptString = tempExcerptString[ 1 ] | ||
| } |
There was a problem hiding this comment.
The tempExcerptString[ 1 ] errors out & I'm not sure why we need tempExcerptString, we can just use this directly:
| if ( tempExcerptString[ 1 ].length > ( excerptLength || 55 ) ) { | |
| excerptString = tempExcerptString[ 1 ].substring( 0, excerptLength === '' ? 55 : excerptLength ) + '...' | |
| } else { | |
| excerptString = tempExcerptString[ 1 ] | |
| } | |
| if ( postExcerptStackable.length > ( excerptLength || 55 ) ) { | |
| excerptString = postExcerptStackable.substring( 0, excerptLength === '' ? 55 : excerptLength ) + '...' | |
| } else { | |
| excerptString = postExcerptStackable | |
| } |
There was a problem hiding this comment.
const tempExcerptString = postExcerptStackable.match( '<p>(.*)<\/p>\\n' ) returns an array and the 1st element is not the match, that is why the index is 1. Though I am not sure if this is the best way to do it.
bfintal
left a comment
There was a problem hiding this comment.
Please see comments. The PHP side gives the wrong substr string. The JS produces an error
|
Added this issue to Release Cycle Project since a user raised this issue. See https://www.facebook.com/groups/wpstackable/permalink/1195920727963105/ |


Fixes #2203