Skip to content

Post excerpt > Ensure the postId from the block context is used to get_the_excerpt - #49495

Merged
gziolo merged 3 commits into
WordPress:trunkfrom
nefeline:fix/post-excerpt-without-post-id
Apr 8, 2023
Merged

gziolo merged 3 commits into
WordPress:trunkfrom
nefeline:fix/post-excerpt-without-post-id

Conversation

@nefeline

@nefeline nefeline commented Mar 30, 2023 •

Copy link
Copy Markdown
Contributor

What?

Ensures the postId from the block context is used for fetching the post excerpt.

Why?

Similar to this change made to the Post Title block, here we can ensure that independent of where the Post Excerpt block is added, the rendered content will be aligned with the context of the block itself rather than the global post.

For context, this limitation surfaced while working on the new Single Product Block on WooCommerce blocks, where the displayed excerpt would match the current post rather than the block context while at the same time, the Post Title block would respect the block context accordingly.

Testing Instructions

  1. Add the Post Excerpt block to any post/page.
  2. Confirm that the excerpt is properly rendered both on the editor and on the front-end sides.

@nefeline
nefeline requested a review from ajitbohra as a code owner March 30, 2023 20:24
@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Mar 30, 2023
@github-actions

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @nefeline! In case you missed it, we'd love to have you join us in our Slack community, where we hold regularly weekly meetings open to anyone to coordinate with each other.

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

@gziolo gziolo added [Type] Bug An existing feature does not function as intended [Block] Post Excerpt Affects the Post Excerpt Block labels Mar 30, 2023
@gziolo

gziolo commented Mar 30, 2023

Copy link
Copy Markdown
Member

Thank you for opening the bug fix. It makes sense and aligns with how it works in edit.

Similar to this change made to the Post Title block

Thank you for linking to that prior work from @spacedmonkey. Would it also he beneficial for performance to use the post object here?

@carolinan

carolinan commented Mar 31, 2023 •

Copy link
Copy Markdown
Contributor

Would it also he beneficial for performance to use the post object here?

There is a related discussion here but not enough testing has been done:
#47997

@gziolo gziolo left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Approving the changes.

The follow-up could cover potential perf improvements. We could also refactor the code so it's easier to follow:

$excerpt = get_the_excerpt( $block->context['postId'] );
if ( isset( $excerpt_length ) ) {
    $excerpt = wp_trim_words( $excerpt, $excerpt_length );
}

@aristath aristath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM 👍

I agree that it would be better if we tweak the code a bit like @gziolo suggested above, but no strong feelings either way.

@nefeline

nefeline commented Mar 31, 2023 •

Copy link
Copy Markdown
Contributor Author

Thanks all 🎉 !

I tweaked the code as @gziolo suggested.

@gziolo
gziolo merged commit 99d837a into WordPress:trunk Apr 8, 2023
@github-actions

github-actions Bot commented Apr 8, 2023

Copy link
Copy Markdown

Congratulations on your first merged pull request, @nefeline! We'd like to credit you for your contribution in the post announcing the next WordPress release, but we can't find a WordPress.org profile associated with your GitHub account. When you have a moment, visit the following URL and click "link your GitHub account" under "GitHub Username" to link your accounts:

https://profiles.wordpress.org/me/profile/edit/

And if you don't have a WordPress.org account, you can create one on this page:

https://login.wordpress.org/register

Kudos!

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 [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants