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

PHP Warnings when featured image blocks have a sizeSlug but not classname #49615

Open
tomjn opened this issue Apr 5, 2023 · 17 comments
Open
Labels
[Block] Post Featured Image Affects the Post Featured Image Block [Type] Enhancement A suggestion for improvement.

Comments

@tomjn
Copy link
Contributor

tomjn commented Apr 5, 2023

Description

If i use the following block I get PHP warnings in core:

<!-- wp:post-featured-image {isLink":true,"sizeSlug":"medium"} /-->

The addition of sizeSlug is the instigator, and it's related to the className attribute.

I can resolve this by using the render_block_data to filter the block data and ensure that if className is unset it has an empty string value:

function render_block_data( array $parsed_block ) : array {
	if ( $parsed_block['blockName'] === 'core/post-featured-image' ) {
		if ( ! isset( $parsed_block['attrs']['className'] ) ) {
			$parsed_block['attrs']['className'] = '';
		}
	}
}

This is the warning:

array_key_exists() expects parameter 2 to be array, null given

And it appears in 2 locations in core every time the block is rendered:

wp-includes/block-supports/align.php:48
wp_apply_alignment_support()
wp-includes/class-wp-block-supports.php:119
WP_Block_Supports->apply_block_supports()
wp-includes/class-wp-block-supports.php:176
get_block_wrapper_attributes()
wp-includes/blocks/post-featured-image.php:77
render_block_core_post_featured_image()
wp-includes/class-wp-block.php:258
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/blocks.php:1051
render_block()
wp-includes/blocks.php:1089
do_blocks()
wp-includes/blocks/template-part.php:146
render_block_core_template_part()
wp-includes/class-wp-block.php:258
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/blocks/post-template.php:98
render_block_core_post_template()
wp-includes/class-wp-block.php:258
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/blocks.php:1051
render_block()
wp-includes/blocks.php:1089
do_blocks()
wp-includes/block-template.php:240
get_the_block_template_html()
wp-includes/template-canvas.php:12

and:

wp-includes/block-supports/custom-classname.php:48
wp_apply_custom_classname_support()
wp-includes/class-wp-block-supports.php:119
WP_Block_Supports->apply_block_supports()
wp-includes/class-wp-block-supports.php:176
get_block_wrapper_attributes()
wp-includes/blocks/post-featured-image.php:77
render_block_core_post_featured_image()
wp-includes/class-wp-block.php:258
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/blocks.php:1051
render_block()
wp-includes/blocks.php:1089
do_blocks()
wp-includes/blocks/template-part.php:146
render_block_core_template_part()
wp-includes/class-wp-block.php:258
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/blocks/post-template.php:98
render_block_core_post_template()
wp-includes/class-wp-block.php:258
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/class-wp-block.php:244
WP_Block->render()
wp-includes/blocks.php:1051
render_block()
wp-includes/blocks.php:1089
do_blocks()
wp-includes/block-template.php:240
get_the_block_template_html()
wp-includes/template-canvas.php:12

Step-by-step reproduction instructions

Try to use a sizeSlug attribute on a featured image block in a block theme

Screenshots, screen recording, code snippet

No response

Environment info

WP 6.2 with a block theme

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

@kathrynwp kathrynwp added [Block] Post Featured Image Affects the Post Featured Image Block [Type] Enhancement A suggestion for improvement. labels Apr 5, 2023
@carolinan
Copy link
Contributor

carolinan commented Apr 6, 2023

Is this markup copied from an actual block that was inserted in the editor?

What I mean is, if this attribute is added manually in the markup, for example, in an HTML template, the attribute is incorrect, because the markup is invalid and the warning would be accurate.

@tomjn
Copy link
Contributor Author

tomjn commented Apr 6, 2023 via email

@carolinan
Copy link
Contributor

I understand now.

I am not sure if that could be handled in the deprecation of the featured image block 🤔 It probably assumes the class name should be preserved.

@tomjn
Copy link
Contributor Author

tomjn commented Apr 6, 2023

Even still I'd assume there'd be a logic issue rather than a warning, this is the place it occurs in one of the stack traces:

function wp_apply_custom_classname_support( $block_type, $block_attributes ) {
	$has_custom_classname_support = block_has_support( $block_type, array( 'customClassName' ), true );
	$attributes                   = array();
	if ( $has_custom_classname_support ) {
		$has_custom_classnames = array_key_exists( 'className', $block_attributes );

I'm unsure why $block_attributes would be null in this specific scenario. I've been unable to reproduce the issue using other attributes and blocks and my workaround to ensure a null className attribute is set to '' is still working strong.

Noting I've yet to test this with the GB plugin

@t-hamano
Copy link
Contributor

t-hamano commented Apr 9, 2023

I am attempting to reproduce this problem but have not yet been able to output a warning error. Is this problem related to the size of the images, or the media settings?

@tomjn
Copy link
Contributor Author

tomjn commented Apr 9, 2023

It's using the specific markup I posted in the opening issue, the sizeSlug reliably reproduces this issue for me. Removing it removes the issue.

Somewhere there's an assumption that a value is always a string or array when it is not and it's being passed down and causing a PHP warning. In this case, checking if className is null and replacing it with an empty string in a filter avoids the PHP warnings

Note that I have not tested this with the gutenberg plugin, only stock WP 6.2 and a block theme

@carolinan
Copy link
Contributor

Are you saying the markup is not produced by placing a block in the editor and then upgrading WordPress?

@carolinan
Copy link
Contributor

If a block expects both an attribute and a class name, then removing one makes the markup invalid and the error is correct. The solution is to correct the markup, not the expectation...

@tomjn
Copy link
Contributor Author

tomjn commented Apr 12, 2023

The featured image is displayed on the frontend despite the PHP warnings, the HTML is not the problem.

As I mentioned, this is enough to reproduce the problem reliably at my end in WordPress 6.2:

<!-- wp:post-featured-image {isLink":true,"sizeSlug":"medium"} /-->

sizeSlug is an attribute declared and supported by that block.

But even if sizeSlug was arbitrary and unknown to WordPress I would not expect a PHP warning

I'm also unsure where upgrading WordPress is coming from, I did not mention WordPress upgrades. Just that the above block in WordPress 6.2 generates PHP warnings for me, and that I had to write a filter to give className a non-empty value to resolve this

@tomjn
Copy link
Contributor Author

tomjn commented Apr 12, 2023

If you are suggesting that the className should have been added also, then this is unclear, but also reinforces that the code handling this is brittle and prone to PHP warnings, and should be handling the false assumption. className may be missing for other reasons, e.g. faulty filters. I would much prefer a doing it wrong style message in this situation than a cryptic PHP warning resulting from unhandled bad values

@carolinan
Copy link
Contributor

carolinan commented Apr 12, 2023

I asked how the markup was generated, and you replied:

it’s based on attributes declared in the block that don’t have GUI anymore.

I assumed that if the UI is gone, there has been a change in the block code, and the error, like a missing class name, should have been resolved by a deprecation when the block code was changed.

So yes, because it did not sound like the user had manually added incomplete markup, I thought there was a block placed in a previous version of WordPress or Gutenberg and that updating either caused the error.

@carolinan
Copy link
Contributor

If you are suggesting that the className should have been added also, then this is unclear, but also reinforces that the code handling this is brittle and prone to PHP warnings, and should be handling the false assumption. className may be missing for other reasons, e.g. faulty filters. I would much prefer a doing it wrong style message in this situation than a cryptic PHP warning resulting from unhandled bad values

I agree

@carolinan
Copy link
Contributor

It is very unclear because not all attributes have a class name, and some class names replace attributes that have been removed or renamed to keep backward compatibility.

@carolinan
Copy link
Contributor

carolinan commented Apr 14, 2023

I am not able to reproduce the warning by pasting <!-- wp:post-featured-image {isLink":true,"sizeSlug":"medium"} /--> into the block editor.
As soon as I exit the code editor mode or save the post, the markup is changed into
<!-- wp:post-featured-image /-->

If I add <!-- wp:post-featured-image {isLink":true,"sizeSlug":"medium"} /--> directly in a single.html template file, using my standard code editor, not WordPress, I get this fatal error on the front when I select to view the single post:

Fatal error: Uncaught Error: array_key_exists(): Argument #2 ($array) must be of type array, null given
in wp-includes\block-supports\align.php on line 48

Call stack:

wp_apply_alignment_support()
wp-includes/class-wp-block-supports.php:119

WP_Block_Supports::apply_block_supports()
wp-includes/class-wp-block-supports.php:176

get_block_wrapper_attributes()
wp-includes/blocks/post-featured-image.php:77

render_block_core_post_featured_image()
wp-includes/class-wp-block.php:258

WP_Block::render()
wp-includes/blocks.php:1051

render_block()
wp-includes/blocks.php:1089

do_blocks()
wp-includes/block-template.php:240

get_the_block_template_html()
wp-includes/template-canvas.php:12

include()
wp-includes/template-loader.php:106

require_once()
wp-blog-header.php:19

require()
index.php:17

The block editor can not "heal" the block and remove the problems without the user opening the template in the site editor.

@carolinan
Copy link
Contributor

carolinan commented Apr 14, 2023

Oh wow, I can't believe it took me this long to see it.
The issue is the missing starting quote before isLink:
<!-- wp:post-featured-image {isLink":true,"sizeSlug":"medium"} /-->
needs to be
<!-- wp:post-featured-image {"isLink":true,"sizeSlug":"medium"} /-->

@carolinan
Copy link
Contributor

I still agree that it is brittle. I often prefer to add the code in the HTML file without having to copy-paste everything from the editor.

We could:

  1. Try to improve error handling
  2. Improve documentation about which block settings need what JSON data in the block comment.
@carolinan
Copy link
Contributor

It is also unclear why the attributes were removed completely instead of displaying as a block validation error in the editor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Block] Post Featured Image Affects the Post Featured Image Block [Type] Enhancement A suggestion for improvement.
4 participants