Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Block Library - Post Excerpt]: Fix excerpt_more filter conflict and remove wordCount attribute #33366

Merged
merged 7 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/block-patterns.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ function register_gutenberg_patterns() {
<!-- wp:post-template -->
<!-- wp:group {"style":{"spacing":{"padding":{"top":"30px","right":"30px","bottom":"30px","left":"30px"}}},"layout":{"inherit":false}} -->
<div class="wp-block-group" style="padding-top:30px;padding-right:30px;padding-bottom:30px;padding-left:30px"><!-- wp:post-title {"isLink":true} /-->
<!-- wp:post-excerpt {"wordCount":20} /-->
<!-- wp:post-excerpt /-->
<!-- wp:post-date /--></div>
<!-- /wp:group -->
<!-- /wp:post-template -->
Expand Down
4 changes: 0 additions & 4 deletions packages/block-library/src/post-excerpt/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
"textAlign": {
"type": "string"
},
"wordCount": {
"type": "number",
"default": 55
},
"moreText": {
"type": "string"
},
Expand Down
61 changes: 19 additions & 42 deletions packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,16 @@ import {
Warning,
useBlockProps,
} from '@wordpress/block-editor';
import { PanelBody, RangeControl, ToggleControl } from '@wordpress/components';
import { PanelBody, ToggleControl, Disabled } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useCanEditEntity } from '../utils/hooks';

function usePostContentExcerpt( wordCount, postId, postType ) {
// Don't destrcuture items from content here, it can be undefined.
const [ , , content ] = useEntityProp(
'postType',
postType,
'content',
postId
);
const renderedPostContent = content?.rendered;
return useMemo( () => {
if ( ! renderedPostContent ) {
return '';
}
const excerptElement = document.createElement( 'div' );
excerptElement.innerHTML = renderedPostContent;
const excerpt =
excerptElement.textContent || excerptElement.innerText || '';
return excerpt.trim().split( ' ', wordCount ).join( ' ' );
}, [ renderedPostContent, wordCount ] );
}

export default function PostExcerptEditor( {
attributes: { textAlign, wordCount, moreText, showMoreOnNewLine },
attributes: { textAlign, moreText, showMoreOnNewLine },
setAttributes,
isSelected,
context: { postId, postType, queryId },
Expand All @@ -59,16 +38,24 @@ export default function PostExcerptEditor( {
setExcerpt,
{ rendered: renderedExcerpt, protected: isProtected } = {},
] = useEntityProp( 'postType', postType, 'excerpt', postId );
const postContentExcerpt = usePostContentExcerpt(
wordCount,
postId,
postType
);
const blockProps = useBlockProps( {
className: classnames( {
[ `has-text-align-${ textAlign }` ]: textAlign,
} ),
} );
/**
* When excerpt is editable, strip the html tags from
* rendered excerpt. This will be used if the entity's
* excerpt has been produced from the content.
*/
const strippedRenderedExcerpt = useMemo( () => {
if ( ! renderedExcerpt ) return '';
const document = new window.DOMParser().parseFromString(
renderedExcerpt,
'text/html'
);
return document.body.textContent || document.body.innerText || '';
}, [ renderedExcerpt ] );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
if ( ! postType || ! postId ) {
return (
<div { ...blockProps }>
Expand Down Expand Up @@ -110,16 +97,17 @@ export default function PostExcerptEditor( {
aria-label={ __( 'Post excerpt text' ) }
value={
rawExcerpt ||
postContentExcerpt ||
strippedRenderedExcerpt ||
( isSelected ? '' : __( 'No post excerpt found' ) )
}
onChange={ setExcerpt }
/>
) : (
( renderedExcerpt && (
<RawHTML key="html">{ renderedExcerpt }</RawHTML>
<Disabled>
<RawHTML key="html">{ renderedExcerpt }</RawHTML>
</Disabled>
) ) ||
postContentExcerpt ||
__( 'No post excerpt found' )
);
return (
Expand All @@ -134,17 +122,6 @@ export default function PostExcerptEditor( {
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Post Excerpt Settings' ) }>
{ ! renderedExcerpt && (
<RangeControl
label={ __( 'Max words' ) }
value={ wordCount }
onChange={ ( newExcerptLength ) =>
setAttributes( { wordCount: newExcerptLength } )
}
min={ 10 }
max={ 100 }
/>
) }
<ToggleControl
label={ __( 'Show link on new line' ) }
checked={ showMoreOnNewLine }
Expand Down
36 changes: 18 additions & 18 deletions packages/block-library/src/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,34 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
return '';
}

$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>' : '';

$filter_excerpt_length = function() use ( $attributes ) {
return isset( $attributes['wordCount'] ) ? $attributes['wordCount'] : 55;
$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>' : '';
$filter_excerpt_more = function( $more ) use ( $more_text ) {
return empty( $more_text ) ? $more : '';
};
add_filter(
'excerpt_length',
$filter_excerpt_length
);

/**
* Some themes might use `excerpt_more` filter to handle the
* `more` link displayed after a trimmed excerpt. Since the
* block has a `more text` attribute we have to check and
* override if needed the return value from this filter.
* So if the block's attribute is not empty override the
* `excerpt_more` filter and return nothing. This will
* result in showing only one `read more` link at a time.
*/
add_filter( 'excerpt_more', $filter_excerpt_more );
$classes = '';
if ( isset( $attributes['textAlign'] ) ) {
$classes .= 'has-text-align-' . $attributes['textAlign'];
$classes .= "has-text-align-{$attributes['textAlign']}";
}
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classes ) );

$content = '<p class="wp-block-post-excerpt__excerpt">' . get_the_excerpt( $block->context['postId'] );
if ( ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'] ) {
$content = '<p class="wp-block-post-excerpt__excerpt">' . get_the_excerpt( $block->context['postId'] );
$show_more_on_new_line = ! isset( $attributes['showMoreOnNewLine'] ) || $attributes['showMoreOnNewLine'];
if ( $show_more_on_new_line && ! empty( $more_text ) ) {
$content .= '</p><p class="wp-block-post-excerpt__more-text">' . $more_text . '</p>';
} else {
$content .= " $more_text</p>";
}

remove_filter(
'excerpt_length',
$filter_excerpt_length
);

remove_filter( 'excerpt_more', $filter_excerpt_more );
return sprintf( '<div %1$s>%2$s</div>', $wrapper_attributes, $content );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"name": "core/post-excerpt",
"isValid": true,
"attributes": {
"wordCount": 55,
"showMoreOnNewLine": true
},
"innerBlocks": [],
Expand Down