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

Conversation

CoderAbhinav
Copy link

@CoderAbhinav CoderAbhinav commented Dec 10, 2024

What?

This PR adds missing the_excerpt filter. Allowing much more consistent usage.

Why?

The post excerpt block does not call the the_excerpt filter before rendering the block. In contrast, the post content block does call the the_content filter before rendering. (Ref. #28214). In order to add consistency this filter has been added.

How?

This PR adds filter in frontend (index.js) as well as in the edit.js of the block.
https://github.com/CoderAbhinav/gutenberg/blob/351c8fe74313d3f2f8eabbbcca1ac91224751347/packages/block-library/src/post-excerpt/index.php#L31-L38

https://github.com/CoderAbhinav/gutenberg/blob/756b5970d155feae8a45f23fd62473dacd6a89b6/packages/block-library/src/post-excerpt/edit.js#L149-L155

The implementation is done with reference to https://developer.wordpress.org/reference/functions/the_excerpt/#source this code.

Testing Instructions

Add a filter like this first.

function my_excerpt_filter( $excerpt ) {
	return "Filtered: $excerpt";
  }
add_filter( 'the_excerpt', 'my_excerpt_filter' );
  1. Then create a new post.
  2. Add excerpt to the post.
  3. Add post excerpt block in the editor. Check if the content is updated.
  4. Save the post and check the frontend, for same changes.

Description:
This allows us to have consistency between frontend and backend.
@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Dec 10, 2024
Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @CoderAbhinav! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@@ -27,7 +27,15 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
* 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.

@Mamaduka
Copy link
Member

Thanks for contributing, @CoderAbhinav! What is PR is trying to solve?

The PHP rendered has the correct filters, and data on the client side is consumed via REST API, which also has excerpt filters.

There's no need to mimic PHP filters in client-side code (JS), as they serve a different purpose, and filters in JS can negatively affect performance.

@Mamaduka Mamaduka added [Block] Post Excerpt Affects the Post Excerpt Block [Type] Bug An existing feature does not function as intended labels Dec 10, 2024
@@ -27,7 +27,8 @@ function render_block_core_post_excerpt( $attributes, $content, $block ) {
* wp_trim_words is used instead.
*/
$excerpt_length = $attributes['excerptLength'];
$excerpt = get_the_excerpt( $block->context['postId'] );

$excerpt = apply_filters( 'the_excerpt', get_the_excerpt( $block->context['postId'] ) );
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 ) );

@talldan talldan added the Needs Dev Note Requires a developer note for a major WordPress release cycle label Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Excerpt Affects the Post Excerpt Block First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository Needs Dev Note Requires a developer note for a major WordPress release cycle [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants