Skip to content

Commit aff30fb

Browse files
ntsekourasmcsf
authored andcommitted
[Block Library - Post Excerpt]: Fix excerpt_more filter conflict and remove wordCount attribute (#33366)
* [Block Library - Post Excerpt]: Fix `excerpt_more` filter conflict * use Disabled component * add comments * Remove `wordCount` attribute * prioritize textContent * Post Excerpt: Don't output empty P tag when $more_text empty * Query Loop Patterns: Remove extinct wordCount attribute Co-authored-by: Miguel Fonseca <[email protected]>
1 parent 237ab0c commit aff30fb

File tree

5 files changed

+38
-66
lines changed

5 files changed

+38
-66
lines changed

lib/block-patterns.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function register_gutenberg_patterns() {
8181
<!-- wp:post-template -->
8282
<!-- wp:group {"style":{"spacing":{"padding":{"top":"30px","right":"30px","bottom":"30px","left":"30px"}}},"layout":{"inherit":false}} -->
8383
<div class="wp-block-group" style="padding-top:30px;padding-right:30px;padding-bottom:30px;padding-left:30px"><!-- wp:post-title {"isLink":true} /-->
84-
<!-- wp:post-excerpt {"wordCount":20} /-->
84+
<!-- wp:post-excerpt /-->
8585
<!-- wp:post-date /--></div>
8686
<!-- /wp:group -->
8787
<!-- /wp:post-template -->

packages/block-library/src/post-excerpt/block.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
"textAlign": {
1010
"type": "string"
1111
},
12-
"wordCount": {
13-
"type": "number",
14-
"default": 55
15-
},
1612
"moreText": {
1713
"type": "string"
1814
},

packages/block-library/src/post-excerpt/edit.js

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,37 +16,16 @@ import {
1616
Warning,
1717
useBlockProps,
1818
} from '@wordpress/block-editor';
19-
import { PanelBody, RangeControl, ToggleControl } from '@wordpress/components';
19+
import { PanelBody, ToggleControl, Disabled } from '@wordpress/components';
2020
import { __ } from '@wordpress/i18n';
2121

2222
/**
2323
* Internal dependencies
2424
*/
2525
import { useCanEditEntity } from '../utils/hooks';
2626

27-
function usePostContentExcerpt( wordCount, postId, postType ) {
28-
// Don't destrcuture items from content here, it can be undefined.
29-
const [ , , content ] = useEntityProp(
30-
'postType',
31-
postType,
32-
'content',
33-
postId
34-
);
35-
const renderedPostContent = content?.rendered;
36-
return useMemo( () => {
37-
if ( ! renderedPostContent ) {
38-
return '';
39-
}
40-
const excerptElement = document.createElement( 'div' );
41-
excerptElement.innerHTML = renderedPostContent;
42-
const excerpt =
43-
excerptElement.textContent || excerptElement.innerText || '';
44-
return excerpt.trim().split( ' ', wordCount ).join( ' ' );
45-
}, [ renderedPostContent, wordCount ] );
46-
}
47-
4827
export default function PostExcerptEditor( {
49-
attributes: { textAlign, wordCount, moreText, showMoreOnNewLine },
28+
attributes: { textAlign, moreText, showMoreOnNewLine },
5029
setAttributes,
5130
isSelected,
5231
context: { postId, postType, queryId },
@@ -59,16 +38,24 @@ export default function PostExcerptEditor( {
5938
setExcerpt,
6039
{ rendered: renderedExcerpt, protected: isProtected } = {},
6140
] = useEntityProp( 'postType', postType, 'excerpt', postId );
62-
const postContentExcerpt = usePostContentExcerpt(
63-
wordCount,
64-
postId,
65-
postType
66-
);
6741
const blockProps = useBlockProps( {
6842
className: classnames( {
6943
[ `has-text-align-${ textAlign }` ]: textAlign,
7044
} ),
7145
} );
46+
/**
47+
* When excerpt is editable, strip the html tags from
48+
* rendered excerpt. This will be used if the entity's
49+
* excerpt has been produced from the content.
50+
*/
51+
const strippedRenderedExcerpt = useMemo( () => {
52+
if ( ! renderedExcerpt ) return '';
53+
const document = new window.DOMParser().parseFromString(
54+
renderedExcerpt,
55+
'text/html'
56+
);
57+
return document.body.textContent || document.body.innerText || '';
58+
}, [ renderedExcerpt ] );
7259
if ( ! postType || ! postId ) {
7360
return (
7461
<div { ...blockProps }>
@@ -110,16 +97,17 @@ export default function PostExcerptEditor( {
11097
aria-label={ __( 'Post excerpt text' ) }
11198
value={
11299
rawExcerpt ||
113-
postContentExcerpt ||
100+
strippedRenderedExcerpt ||
114101
( isSelected ? '' : __( 'No post excerpt found' ) )
115102
}
116103
onChange={ setExcerpt }
117104
/>
118105
) : (
119106
( renderedExcerpt && (
120-
<RawHTML key="html">{ renderedExcerpt }</RawHTML>
107+
<Disabled>
108+
<RawHTML key="html">{ renderedExcerpt }</RawHTML>
109+
</Disabled>
121110
) ) ||
122-
postContentExcerpt ||
123111
__( 'No post excerpt found' )
124112
);
125113
return (
@@ -134,17 +122,6 @@ export default function PostExcerptEditor( {
134122
</BlockControls>
135123
<InspectorControls>
136124
<PanelBody title={ __( 'Post Excerpt Settings' ) }>
137-
{ ! renderedExcerpt && (
138-
<RangeControl
139-
label={ __( 'Max words' ) }
140-
value={ wordCount }
141-
onChange={ ( newExcerptLength ) =>
142-
setAttributes( { wordCount: newExcerptLength } )
143-
}
144-
min={ 10 }
145-
max={ 100 }
146-
/>
147-
) }
148125
<ToggleControl
149126
label={ __( 'Show link on new line' ) }
150127
checked={ showMoreOnNewLine }

packages/block-library/src/post-excerpt/index.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,34 +18,34 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
1818
return '';
1919
}
2020

21-
$more_text = isset( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . $attributes['moreText'] . '</a>' : '';
22-
23-
$filter_excerpt_length = function() use ( $attributes ) {
24-
return isset( $attributes['wordCount'] ) ? $attributes['wordCount'] : 55;
21+
$more_text = ! empty( $attributes['moreText'] ) ? '<a class="wp-block-post-excerpt__more-link" href="' . esc_url( get_the_permalink( $block->context['postId'] ) ) . '">' . $attributes['moreText'] . '</a>' : '';
22+
$filter_excerpt_more = function( $more ) use ( $more_text ) {
23+
return empty( $more_text ) ? $more : '';
2524
};
26-
add_filter(
27-
'excerpt_length',
28-
$filter_excerpt_length
29-
);
30-
25+
/**
26+
* Some themes might use `excerpt_more` filter to handle the
27+
* `more` link displayed after a trimmed excerpt. Since the
28+
* block has a `more text` attribute we have to check and
29+
* override if needed the return value from this filter.
30+
* So if the block's attribute is not empty override the
31+
* `excerpt_more` filter and return nothing. This will
32+
* result in showing only one `read more` link at a time.
33+
*/
34+
add_filter( 'excerpt_more', $filter_excerpt_more );
3135
$classes = '';
3236
if ( isset( $attributes['textAlign'] ) ) {
33-
$classes .= 'has-text-align-' . $attributes['textAlign'];
37+
$classes .= "has-text-align-{$attributes['textAlign']}";
3438
}
3539
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );
3640

37-
$content = '<p class="wp-block-post-excerpt__excerpt">' . get_the_excerpt( $block->context['postId'] );
38-
if ( ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'] ) {
41+
$content = '<p class="wp-block-post-excerpt__excerpt">' . get_the_excerpt( $block->context['postId'] );
42+
$show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'];
43+
if ( $show_more_on_new_line && ! empty( $more_text ) ) {
3944
$content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
4045
} else {
4146
$content .= " $more_text</p>";
4247
}
43-
44-
remove_filter(
45-
'excerpt_length',
46-
$filter_excerpt_length
47-
);
48-
48+
remove_filter( 'excerpt_more', $filter_excerpt_more );
4949
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
5050
}
5151

packages/e2e-tests/fixtures/blocks/core__post-excerpt.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
"name": "core/post-excerpt",
55
"isValid": true,
66
"attributes": {
7-
"wordCount": 55,
87
"showMoreOnNewLine": true
98
},
109
"innerBlocks": [],

0 commit comments

Comments
 (0)