Skip to content

Conversation

@up1512001
Copy link
Member

What?

update the core/post-term render_block_core_post_terms function to handle WP_Error and not throw a fatal error.

Why?

fixes #65832

How?

  • store get_the_term_list as a separate variable and check if it's WP_Error then simply return false otherwise return actual output.

Testing Instructions

  • Add the Terms List block to the post.
  • make get_the_term_list to return WP_Error.
  • on the front end, you will not see any fatal error.

Screenshots or screencast

Screen.Recording.2024-10-03.at.11.42.19.mov

@up1512001 up1512001 added [Type] Bug An existing feature does not function as intended [Block] Post Terms Affects the Post Terms Block labels Oct 3, 2024
@up1512001 up1512001 requested a review from talldan October 3, 2024 06:14
@up1512001 up1512001 self-assigned this Oct 3, 2024
@up1512001 up1512001 requested a review from ajitbohra as a code owner October 3, 2024 06:14
@github-actions
Copy link

github-actions bot commented Oct 3, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: up1512001 <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: david-binda <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@Mamaduka
Copy link
Member

Mamaduka commented Oct 3, 2024

Thanks for contributing, @up1512001!

Can you share a code snippet for get_the_term_list to return a WP_Error?

The issue mentions filtering the get_the_terms, in which case an error should be caught early, and code execution shouldn't reach get_the_term_list.

$post_terms = get_the_terms( $block->context['postId'], $attributes['term'] );
if ( is_wp_error( $post_terms ) || empty( $post_terms ) ) {
return '';
}

cc @david-binda

@david-binda
Copy link
Contributor

@Mamaduka sorry for the confusion on my end. The thing is that the get_the_term_list can return a WP_Error even in case the get_the_terms does not.

It would be the case when the get_term_link called from the get_the_term_list would return an instance of WP_Error.

As I have mentioned in the related issue, checking the return value of the get_the_term_list is what WordPress also does.

In order to reproduce the issue I've noticed in production logs, we could filter the get_the_terms in following fashion:

add_filter( 'get_the_terms', function( $terms, $post_id, $taxonomy ) {
    if ( array_key_exists( 0, $terms ) ) {
        $terms[0] = null;
    }
    return $terms;
}, 10, 3 );

Copy link
Member

@Mamaduka Mamaduka left a comment

Choose a reason for hiding this comment

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

Thanks for the additional details, @david-binda!

I can confirm that this fixes the bug ✅

@Mamaduka Mamaduka changed the title fix: core post term render issue when get_the_term_list returns WP_Error Post Terms: Fix fatal error when 'get_the_term_list' returns 'WP_Error' Oct 3, 2024
@Mamaduka Mamaduka merged commit 46fdb67 into WordPress:trunk Oct 3, 2024
@github-actions github-actions bot added this to the Gutenberg 19.5 milestone Oct 3, 2024
karthick-murugan pushed a commit to karthick-murugan/gutenberg that referenced this pull request Nov 13, 2024
…r' (WordPress#65848)


Co-authored-by: up1512001 <[email protected]>
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: david-binda <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Post Terms Affects the Post Terms Block [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fatal error when gutenberg_render_block_core_post_terms is not returning string

3 participants