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

Post Terms: Fix fatal error when 'get_the_term_list' returns 'WP_Error' #65848

Merged
merged 2 commits into from
Oct 3, 2024

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
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 <up1512001@git.wordpress.org>
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: david-binda <davidbinda@git.wordpress.org>

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
61 checks passed
@github-actions github-actions bot added this to the Gutenberg 19.5 milestone Oct 3, 2024
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