Skip to content

Conversation

@dmsnell
Copy link
Member

@dmsnell dmsnell commented Oct 13, 2023

Trac ticket: Core-59622
Gutenberg backport: WordPress/gutenberg#71594

Block Supports: Avoid throwing warning when checking for class attribute as string.

When encountering HTML tags with boolean or missing tags, the get_attribute()
method in the HTML API returns true and null, respectively. If these returned
values are sent directly into string comparison functions then as of PHP 8.0 they
will throw E_DEPRECATED errors.

In this patch, block supports is enhanced to check that the class value is a
string before it performs string operations on it.


There is a small likelihood of any real damage in production here, as these are only deprecation notices and the logic in these cases isn't affected by the change. That is,
the intention of the code is maintained even when the attribute values aren't strings.

For background support there's a tiny but benign defect when the style attribute is
boolean. It will erroneously inject a ; at the beginning of the wrapper's new style
value. As this doesn't impact the rendering of the wrapper, it's not a true defect.

Further review belongs to block rendering code, but this will occur in the Gutenberg
repo since that PHP comes back into Core through the generated packages.

Testing

There should be no functional or visual changes in this patch. Please consult blocks with layout and background support to verify that this hasn't adversely affected existing rendering. Verify that the test suite continues to pass.

@hellofromtonya
Copy link
Contributor

@dmsnell 10 Test_Block_Supports_Layout tests are failing from this change. Can you take a look please?

@dmsnell dmsnell force-pushed the block-supports/avoid-warning-for-non-string-class-attributes branch 2 times, most recently from 482f394 to bce9b11 Compare October 16, 2023 18:49
@peterwilsoncc
Copy link
Contributor

Could the tests be failing because the markup is missing a closing div? <div><div class="wp-block-group"></div>

To an extent I'm asking the silly question because I'd sure feel silly if I didn't and it turned out to be the case ;)

@dmsnell
Copy link
Member Author

dmsnell commented Oct 17, 2023

@peterwilsoncc no, and I wasn't sure what happened. I tried a few things that I thought should fix it, so I'm curious when my change is at fault or if the implicit behavior this removed is at fault, even though from the unit tests it looks like it should be fine.

@dmsnell dmsnell force-pushed the block-supports/avoid-warning-for-non-string-class-attributes branch from 75092c5 to 0c6e8e7 Compare October 31, 2023 08:00
@dmsnell dmsnell force-pushed the block-supports/avoid-warning-for-non-string-class-attributes branch from 0c6e8e7 to 797c0fa Compare December 19, 2023 20:40
Copy link
Member

@sirreal sirreal left a comment

Choose a reason for hiding this comment

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

This needs to be updated, but generally looks like a good fix for some subtle issues.

$updated_style = '';

if ( ! empty( $existing_style ) ) {
if ( is_string( $existing_style ) && ! empty( $existing_style ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Note for reviewers: the empty on this line would not have any issues, but would do the wrong thing if passed true, ! empty( true ) evaluates to true, so this block would be entered if if $existing_style is true, for example with HTML like <div style>. Inside the block, $existing_style is expected to be a string and is passed to string methods.

@dmsnell dmsnell force-pushed the block-supports/avoid-warning-for-non-string-class-attributes branch from 797c0fa to 7ae9381 Compare September 3, 2025 18:28
@github-actions
Copy link

github-actions bot commented Sep 3, 2025

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props dmsnell, jonsurrell, hellofromtonya, peterwilsoncc.

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

@github-actions
Copy link

github-actions bot commented Sep 3, 2025

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@dmsnell
Copy link
Member Author

dmsnell commented Sep 3, 2025

I have removed the added tests since I had trouble figuring out what the function should be doing, and since it seemed like they didn’t add much value anyway. The existing tests verify the passing behaviors and will catch regressions. This patch only adds a bit of safety to handle some edge cases and therefore need not be distracted by asserting that those edge cases fail.

@dmsnell dmsnell force-pushed the block-supports/avoid-warning-for-non-string-class-attributes branch from 6edbeb1 to 4c1a0f5 Compare September 10, 2025 19:55
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Sep 10, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Sep 10, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.
…ute as string.

When encountering HTML tags with boolean or missing tags, the `get_attribute()`
method in the HTML API returns `true` and `null`, resepectively. If these returned
values are sent directly into string comparison functions then as of PHP 8.0 they
will throw `E_DEPRECATED` errors.

In this patch, block supports is enhanced to check that the `class` value is a
string before it performs string operations on it.
@dmsnell dmsnell force-pushed the block-supports/avoid-warning-for-non-string-class-attributes branch from 4c1a0f5 to 3ba1e75 Compare September 10, 2025 20:06
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Sep 10, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.

Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Jon Surrell <[email protected]>
Github-PR: 71594
Github-PR-URL: #71594
Branch-name: backport/block-supports-background-non-string-attribute-check
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Sep 10, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.

Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Jon Surrell <[email protected]>
Github-PR: 71594
Github-PR-URL: #71594
Branch-name: backport/block-supports-background-non-string-attribute-check
pento pushed a commit that referenced this pull request Sep 10, 2025
…ute as string.

When encountering HTML tags with boolean or missing tags, the get_attribute() method in the HTML API returns true and null, respectively. If these returned values are sent directly into string comparison functions then as of PHP 8.0 they will throw E_DEPRECATED errors.

In this patch, block supports is enhanced to check that the class value is a string before it performs string operations on it.

Also in this patch: using `assertEqualHTML()` in background support test instead of `assertSame()`

Developed in #5486
Discussed in https://core.trac.wordpress.org/ticket/59622

Props dmsnell, jonsurrell, hellofromtonya, peterwilsoncc.
Fixes #59622.


git-svn-id: https://develop.svn.wordpress.org/trunk@60727 602fd350-edb4-49c9-b593-d223f7449a82
@github-actions
Copy link

A commit was made that fixes the Trac ticket referenced in the description of this pull request.

SVN changeset: 60727
GitHub commit: 1ebc4c5

This PR will be closed, but please confirm the accuracy of this and reopen if there is more work to be done.

@github-actions github-actions bot closed this Sep 10, 2025
@dmsnell dmsnell deleted the block-supports/avoid-warning-for-non-string-class-attributes branch September 10, 2025 20:26
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Sep 10, 2025
…ute as string.

When encountering HTML tags with boolean or missing tags, the get_attribute() method in the HTML API returns true and null, respectively. If these returned values are sent directly into string comparison functions then as of PHP 8.0 they will throw E_DEPRECATED errors.

In this patch, block supports is enhanced to check that the class value is a string before it performs string operations on it.

Also in this patch: using `assertEqualHTML()` in background support test instead of `assertSame()`

Developed in WordPress/wordpress-develop#5486
Discussed in https://core.trac.wordpress.org/ticket/59622

Props dmsnell, jonsurrell, hellofromtonya, peterwilsoncc.
Fixes #59622.

Built from https://develop.svn.wordpress.org/trunk@60727


git-svn-id: http://core.svn.wordpress.org/trunk@60063 1a063a9b-81f0-0310-95a4-ce76da25c4cd
github-actions bot pushed a commit to gilzow/wordpress-performance that referenced this pull request Sep 10, 2025
…ute as string.

When encountering HTML tags with boolean or missing tags, the get_attribute() method in the HTML API returns true and null, respectively. If these returned values are sent directly into string comparison functions then as of PHP 8.0 they will throw E_DEPRECATED errors.

In this patch, block supports is enhanced to check that the class value is a string before it performs string operations on it.

Also in this patch: using `assertEqualHTML()` in background support test instead of `assertSame()`

Developed in WordPress/wordpress-develop#5486
Discussed in https://core.trac.wordpress.org/ticket/59622

Props dmsnell, jonsurrell, hellofromtonya, peterwilsoncc.
Fixes #59622.

Built from https://develop.svn.wordpress.org/trunk@60727


git-svn-id: https://core.svn.wordpress.org/trunk@60063 1a063a9b-81f0-0310-95a4-ce76da25c4cd
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Sep 11, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.

Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Jon Surrell <[email protected]>
Github-PR: 71594
Github-PR-URL: #71594
Branch-name: backport/block-supports-background-non-string-attribute-check
dmsnell added a commit to WordPress/gutenberg that referenced this pull request Sep 11, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.

Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Jon Surrell <[email protected]>
Co-authored-by: George Mamadashvili <[email protected]>
Github-PR: 71594
Github-PR-URL: #71594
Branch-name: backport/block-supports-background-non-string-attribute-check
Trac-ticket: 59622
adamsilverstein pushed a commit to adamsilverstein/gutenberg that referenced this pull request Sep 11, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.

Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Jon Surrell <[email protected]>
Co-authored-by: George Mamadashvili <[email protected]>
Github-PR: 71594
Github-PR-URL: WordPress#71594
Branch-name: backport/block-supports-background-non-string-attribute-check
Trac-ticket: 59622
peterwilsoncc pushed a commit to peterwilsoncc/gutenberg-build that referenced this pull request Sep 11, 2025
Trac ticket: Core-59622

This is a backport of WordPress/wordpress-develop#5486, which
introduces an additional check for non-string values of the
`class` attribute when rendering background support for blocks.

This check avoids issuing a warning when a boolean `true` value
of the attribute is passed on to `str_ends_with()`.

Co-authored-by: Tonya Mork <[email protected]>
Co-authored-by: Peter Wilson <[email protected]>
Co-authored-by: Jon Surrell <[email protected]>
Co-authored-by: George Mamadashvili <[email protected]>
Github-PR: 71594
Github-PR-URL: WordPress/gutenberg#71594
Branch-name: backport/block-supports-background-non-string-attribute-check
Trac-ticket: 59622

Source: WordPress/gutenberg@54564ab
jonnynews pushed a commit to spacedmonkey/wordpress-develop that referenced this pull request Sep 24, 2025
…ute as string.

When encountering HTML tags with boolean or missing tags, the get_attribute() method in the HTML API returns true and null, respectively. If these returned values are sent directly into string comparison functions then as of PHP 8.0 they will throw E_DEPRECATED errors.

In this patch, block supports is enhanced to check that the class value is a string before it performs string operations on it.

Also in this patch: using `assertEqualHTML()` in background support test instead of `assertSame()`

Developed in WordPress#5486
Discussed in https://core.trac.wordpress.org/ticket/59622

Props dmsnell, jonsurrell, hellofromtonya, peterwilsoncc.
Fixes #59622.


git-svn-id: https://develop.svn.wordpress.org/trunk@60727 602fd350-edb4-49c9-b593-d223f7449a82
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants