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

Fatal error when user CPT for Global Styles is deleted #45240

Closed
scruffian opened this issue Oct 24, 2022 · 6 comments
Closed

Fatal error when user CPT for Global Styles is deleted #45240

scruffian opened this issue Oct 24, 2022 · 6 comments
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended

Comments

@scruffian
Copy link
Contributor

Description

If the user CPT for Global Styles is deleted then this line throws a fatal error:

if ( array_key_exists( 'post_content', $user_cpt ) ) {

in wordpress-6.1/class-wp-theme-json-resolver-6-1.php.

We should check that $user_cpt is an array before we check if a key exists in it.

Step-by-step reproduction instructions

Delete the user CPT for Global Styles and reload your site.

Screenshots, screen recording, code snippet

Error:

Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in /wordpress/plugins/gutenberg/14.3.1/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php:84
Stack trace:
#0 /wordpress/plugins/gutenberg/14.3.1/lib/experimental/class-wp-theme-json-resolver-gutenberg.php(203): WP_Theme_JSON_Resolver_6_1::get_user_data()
#1 /wordpress/plugins/gutenberg/14.3.1/lib/experimental/register-webfonts-from-theme-json.php(13): WP_Theme_JSON_Resolver_Gutenberg::get_merged_data()
#2 /wordpress/core/6.0.3/wp-includes/class-wp-hook.php(307): gutenberg_register_webfonts_from_theme_json('')
#3 /wordpress/core/6.0.3/wp-includes/class-wp-hook.php(331): WP_Hook->apply_filters(NULL, Array)
#4 /wordpress/core/6.0.3/wp-includes/plugin.php(476): WP_Hook->do_action(Array)
#5 /wordpress/core/6.0.3/wp-settings.php(598): do_action('init')
#6 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1317): require('/wordpress/core...')
#7 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Runner.php(1235): WP_CLI\Runner->load_wordpress()
#8 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/WP_CLI/Bootstrap/LaunchRunner.php(28): WP_CLI\Runner->start()
#9 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/bootstrap.php(78): WP_CLI\Bootstrap\LaunchRunner->process(Object(WP_CLI\Bootstrap\BootstrapState))
#10 phar:///usr/local/bin/wp-cli/vendor/wp-cli/wp-cli/php/wp-cli.php(27): WP_CLI\bootstrap()
#11 phar:///usr/local/bin/wp-cli/php/boot-phar.php(11): include('phar:///usr/loc...')
#12 /usr/local/bin/wp-cli(4): include('phar:///usr/loc...')
#13 {main}
  thrown in /wordpress/plugins/gutenberg/14.3.1/lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php on line 84

Environment info

No response

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

@scruffian scruffian added the Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json label Oct 24, 2022
@scruffian
Copy link
Contributor Author

I have added this to the 6.1 tasks as I wonder if we want to get if fixed in 6.1?

@ndiego
Copy link
Member

ndiego commented Oct 24, 2022

This would be good to fix for 6.1 if a fix can be prepared asap. Otherwise, this is a good candidate for 6.1.1.

@cbravobernal
Copy link
Contributor

Taking a look at it 😄

@github-actions github-actions bot added the [Status] In Progress Tracking issues with work in progress label Oct 25, 2022
@ockham
Copy link
Contributor

ockham commented Oct 25, 2022

TBH I'm having a bit of trouble reproducing. Can someone help me out there?

What I've tried:

  • Using TT3:
  • Go to the Site Editor.
  • Make some Global Style changes, e.g.:
    • Change the Theme style variation to "Sherbet"
    • Change block styling globally, e.g. change Headings to have an orange background color.
  • In the console, list all Global Style CPTs: npm run wp-env run cli "wp post list --post_type=wp_global_styles" (Shows only one for me; locate its ID)
  • Delete that CPT: npm run wp-env run cli "wp post delete --force <ID>"
  • In a browser tab with the frontend open, reload. Note that styles are gone.
  • In the browser tab with the Site Editor open, open the "Network" tab and reload the page.
  • Check REST API requests. I didn't see any that would report an error 🤔
  • Run npm run wp-env logs. I didn't see any fatals 🤔

@Mamaduka
Copy link
Member

Mamaduka commented Nov 6, 2022

Was anyone able to reproduce this issue with WP 6.1 without the Gutenberg plugin? Because I believe the issue only exists in the plugin.

First, the get_user_data_from_wp_global_styles methods in Core and Gutenberg are different. The method got some optimization during the backport, but we forgot to port changes back to the plugin.

The get_user_data_from_wp_global_styles in the plugin can return null under certain conditions because it uses a custom caching layer.

Conditions:

  • The site uses persistent object caching.
  • Global Styles post record gets deleted.
  • The method returns a cached ID for the deleted Global Styles post.

The cache isn't purged on post deletion but relies on expiration, so the above condition results in get_post returning null.

A simple example to fake this condition:

add_action( 'init', function() {
	$theme = wp_get_theme();

	$args             = array(
		'numberposts' => 1,
		'orderby'     => 'date',
		'order'       => 'desc',
		'post_type'   => 'wp_global_styles',
		'post_status' => array( 'publish' ),
		'tax_query'   => array(
			array(
				'taxonomy' => 'wp_theme',
				'field'    => 'name',
				'terms'    => $theme->get_stylesheet(),
			),
		),
	);

	$cache_key = sprintf( 'wp_global_styles_%s', md5( serialize( $args ) ) );

	// Fake cache with non-existing post ID.
	wp_cache_set( $cache_key, 999999, '', HOUR_IN_SECONDS );

	$data = WP_Theme_JSON_Resolver_6_1::get_user_data_from_wp_global_styles( $theme );

	// The $data will be null.
	var_dump( $data );
} );

Cc @hellofromtonya, @desrosj

@Mamaduka
Copy link
Member

This was fixed in the plugin via #45634.

@priethor priethor removed the [Status] In Progress Tracking issues with work in progress label Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
No open projects
Development

Successfully merging a pull request may close this issue.

6 participants