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

Block Theme Previews does not work if we call the get_template or get_stylesheet functions early #53284

Closed
okmttdhr opened this issue Aug 3, 2023 · 5 comments · Fixed by Automattic/wp-calypso#81337
Assignees
Labels
[Type] Bug An existing feature does not function as intended

Comments

@okmttdhr
Copy link
Contributor

okmttdhr commented Aug 3, 2023

Description

Block Theme Previews does not work if we call the get_template or get_stylesheet functions before the pluggable functions are not ready. So, if a 3rd party plugin (e.g. CoBlocks, Elementor, Classic Editor, and WooCommerce) needs to access the template or stylesheet before the pluggable functions are ready, then the site with such a plugin will break (see the reproduction instruction below).

The reason this error happens is;

A possible solution would be one of the following;

  • Load the pluggable functions early
  • Load theme-previews.php after the pluggable functions
  • Load the pluggable functions on demand in theme-previews.php
  • Tell developers for the 3rd party plugins not to access the template or stylesheet directly when loading plugins (a developer guide should be covered, maybe)
  • Something else

Step-by-step reproduction instructions

  1. Add a file in wp-content/plugins/sample-plugin/sample-plugin.php on your site.
<?php
/*
Plugin Name: Sample plugin
*/

$wp_theme = wp_get_theme();
$wp_theme->get_stylesheet();
  1. Go to /wp-admin/themes.php and click Live Preview button on your installed Block theme.
  2. See the error: Uncaught Error: Call to undefined function wp_get_current_user()

Screenshots, screen recording, code snippet

Screen Shot 2023-08-03 at 16 25 19

Environment info

  • WordPress: 6.3-RC3-56344
  • Gutenberg: 16.3.0

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
Copy link
Contributor

I had an idea for how to solve this here: WordPress/wordpress-develop#4959

@okmttdhr
Copy link
Contributor Author

okmttdhr commented Aug 15, 2023

So, if a 3rd party plugin needs to access the template or stylesheet before the pluggable functions are ready, then the site with such a plugin will break

For example, it breaks if a user has the CoBlocks plugin activated.
https://github.com/godaddy-wordpress/coblocks/blob/1bf29ec/class-coblocks.php#L225

Update: This issue happens on the site with a plugin like Elementor, and WooCommerce.

@okmttdhr
Copy link
Contributor Author

I had an idea for how to solve this here: WordPress/wordpress-develop#4959

Thanks for trying this out, @scruffian!
However, I've noticed some comments on https://core.trac.wordpress.org/ticket/59000 expressing concerns about its reliability.

Load theme-previews.php after the pluggable functions

This alternative seems more fitting to me, as I feel that performing capability checks before defining pluggable functions itself might not be the optimal approach. What are your thoughts?

@fushar
Copy link

fushar commented Oct 13, 2023

@okmttdhr @scruffian, this has been shipped and backported to WP 6.3.2, should we close this issue?

@okmttdhr
Copy link
Contributor Author

Closing, as this issue has been solved at https://core.trac.wordpress.org/ticket/59000!

@okmttdhr okmttdhr self-assigned this Oct 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
3 participants