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

Adds the_excerpt filter in the core/post-excerpt block. #67774

Draft
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Draft
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
4 changes: 3 additions & 1 deletion packages/block-library/src/post-excerpt/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import { PanelBody, ToggleControl, RangeControl } from '@wordpress/components';
import { __, _x } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { applyFilters } from '@wordpress/hooks';

/**
* Internal dependencies
Expand Down Expand Up @@ -150,7 +151,8 @@ export default function PostExcerptEditor( {
* the raw and the rendered excerpt depending on which is being used.
*/
const rawOrRenderedExcerpt = (
rawExcerpt || strippedRenderedExcerpt
applyFilters( 'the_excerpt', undefined, rawExcerpt ) ||
strippedRenderedExcerpt
).trim();

let trimmedExcerpt = '';
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/post-excerpt/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
* wp_trim_words is used instead.
*/
$excerpt_length = $attributes['excerptLength'];
$excerpt = get_the_excerpt( $block->context['postId'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function already has a filter - get_the_excerpt.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @Mamaduka, so in one of the issues #28214 it has been discussed already that, even though get_the_excerpt() function already uses the get_the_excerpt filter, It would be good to have the_excerpt filter for backwards compatibility.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got it. The issue concerns the PHP filter. There's no need to update the JS code. For various reasons, the blocks don't mirror PHP filters in their React components.

P.S. I would recommend using reserved GitHub keywords to reference issues. It makes clear what you're trying to solve.


$excerpt = apply_filters( 'the_excerpt', get_the_excerpt( $block->context['postId'] ) );

Check warning on line 31 in packages/block-library/src/post-excerpt/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Equals sign not aligned correctly; expected 1 space but found 8 spaces

Check failure on line 31 in packages/block-library/src/post-excerpt/index.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Missing @SInCE tag for the "apply_filters()" hook function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CoderAbhinav I think this will result in all the default filters being applied, so it'd be good to make everything still works correctly:
https://github.com/WordPress/wordpress-develop/blob/6106332617128820c0ee5132c34ff1cf6f785d1a/src/wp-includes/default-filters.php#L205-L211

I did some testing personally and couldn't see any issues. I think most of these filters like convert_smilies are already run on the content, so there's no change from trunk to this PR.

The change might also need a dev note, as some plugins might be working around the missing filter and reintroducing it could require plugins updating their code.

As long as everything works, then this change seems fine (apart from Mamaduka's request change). There's precedence in that the post content block also does this and has done for a long time:

$content = apply_filters( 'the_content', str_replace( ']]>', ']]>', $content ) );

if ( isset( $excerpt_length ) ) {
$excerpt = wp_trim_words( $excerpt, $excerpt_length );
}
Expand Down
Loading