Skip to content

Fatal error on PHP 5.6 for using isset() on class constant WP_Theme_JSON_6_1::VALID_ELEMENT_PSEUDO_SELECTORS #42565

@hellofromtonya

Description

@hellofromtonya

Description

Introduced with PR #41786 which added a class constant WP_Theme_JSON_6_1::VALID_ELEMENT_PSEUDO_SELECTORS to define an array of valid pseudo selectors for a list of elements.

When activating (or updating) Gutenberg plugin, a fatal error happens:

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in ../wp-content/plugins/gutenberg/lib/compat/wordpress-6.1/class-wp-theme-json-6-1.php on line 190

Why?

Before PHP 7.0, isset() does not work with constants (see it in action here https://3v4l.org/lrFo3#v5.6.40) and will throw a parsing error (which is fatal). Per the warning in the PHP manual

Warning
isset() only works with variables as passing anything else will result in a parse error. For checking if constants are set use the defined() function.

In PHP 5.6, isset() works with variables, not constants.

How to resolve?

The class is defining the data type for each element as an array of pseudos. It has control over the structure. Therefore, array_key_exists() can be used instead to check if the element is defined in the constant. Why? array_key_exists() is a native PHP function that specifically checks for the existence of keys. It's the appropriate function for this need.

Step-by-step reproduction instructions

  1. Set your environment to use PHP 5.6 (how to do this depends upon the localhost app/tooling you're using).
  2. Log into the admin area.
  3. Go to Plugins > Add New.
  4. Install the Gutenberg plugin.
  5. Active the plugin.

Screenshots, screen recording, code snippet

isset_fatal_error

Environment info

  • WordPress version: 6.0
  • Gutenberg plugin: 13.6.0
  • PHP version: 5.6.39
  • Web server: nginx
  • Plugins: Only Gutenberg
  • Theme: Twenty Twenty-Two (TT2)
  • localhost app: Local and wp-env

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

Metadata

Metadata

Labels

CSS StylingRelated to editor and front end styles, CSS-specific issues.Global StylesAnything related to the broader Global Styles efforts, including Styles Engine and theme.json[Priority] HighUsed to indicate top priority items that need quick attention[Type] BugAn existing feature does not function as intended

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions