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

Initialize BlockTemplatesController for block themes only #48382

Closed
wants to merge 13 commits into from

Conversation

WunderBart
Copy link
Contributor

@WunderBart WunderBart commented Jun 11, 2024

What?

Closes #46463
Supersedes #48385

  • Move the wc_blocks_use_blockified_product_grid_block_as_template option check from BTC to wc-template-functions.php as it's shared between classic and block themes.
  • Effectively switch the entire BTC class to be only initialized for block themes.

How to test

For the wc_blocks_use_blockified_product_grid_block_as_template option part, ensure:

  • it's switched on when switching from classic to block theme,
  • stays switched on when switching from block to block theme,
  • is switched off when switching to a classic theme,
  • stays switched off when switching from classic to classic theme.

For the BTC initialization switch part:

  • Generally, if the tests pass, it should be good to go. The only risk here is that we missed some functional leaks into classic themes, so we might want to do some manual smoke testing. Therefore, switch a classic theme on (e.g. Storefront) and ensure you can edit and view the content as expected.

@WunderBart WunderBart self-assigned this Jun 11, 2024
@github-actions github-actions bot added the plugin: woocommerce Issues related to the WooCommerce Core plugin. label Jun 11, 2024
Copy link
Contributor

github-actions bot commented Jun 11, 2024

Test using WordPress Playground

The changes in this pull request can be previewed and tested using a WordPress Playground instance.
WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Test this pull request with WordPress Playground.

Note that this URL is valid for 30 days from when this comment was last updated. You can update it by closing/reopening the PR or pushing a new commit.

Copy link
Contributor

github-actions bot commented Jun 12, 2024

Hi @Aljullu, @tjcafferkey, @woocommerce/woo-fse

Apart from reviewing the code changes, please make sure to review the testing instructions as well.

You can follow this guide to find out what good testing instructions should look like:
https://github.com/woocommerce/woocommerce/wiki/Writing-high-quality-testing-instructions

@WunderBart WunderBart marked this pull request as ready for review June 12, 2024 12:40
@woocommercebot woocommercebot requested a review from a team June 12, 2024 12:40
@WunderBart
Copy link
Contributor Author

Asked about the failing blocks E2Es here: #48224 (comment)

Copy link
Contributor

@Aljullu Aljullu left a comment

Choose a reason for hiding this comment

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

It's working great and code changes look good, thanks @WunderBart! Just a few questions:

  1. Did you evaluate placing the wc_check_should_use_blockified_product_grid_templates function somewhere else? I don't have a better suggestion, but thinking that it would be really nice if we could keep it inside /src/Blocks/, given that it's related to block themes. Not sure how, thought, maybe in a new file that is only initialized in the editor? 🤔 (This is not a hard opinion, just thinking out loud to know your thoughts)
  2. I think the testing steps should be separated on what should be tested by GlobalStep and what shouldn't (probably the database part)? Btw, I think the option name is wc_blocks_use_blockified_product_grid_block_as_template

$this->container->get( ClassicTemplatesCompatibility::class );
$this->container->get( ArchiveProductTemplatesCompatibility::class )->init();
$this->container->get( SingleProductTemplateCompatibility::class )->init();
$this->container->get( Notices::class )->init();
$this->container->get( PTKPatternsStore::class );

if ( wc_current_theme_is_fse_theme() ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

A classic theme might support block template parts, if I'm not wrong. So we should account for that:

Suggested change
if ( wc_current_theme_is_fse_theme() ) {
if ( wc_current_theme_is_fse_theme() || current_theme_supports( 'block-template-parts' ) ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great catch, and I'm kinda surprised that all the tests have passed without it. Should we add a basic test to cover this part?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup, good point! We definitely should. Do you want to open an issue and add it to the Project board?

Copy link
Contributor Author

@WunderBart WunderBart Jun 13, 2024

Choose a reason for hiding this comment

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

Sounds good. OK if we add those tests along with this PR?

The issue is here: #48468

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure thing!

plugins/woocommerce/src/Blocks/Domain/Bootstrap.php Outdated Show resolved Hide resolved
@WunderBart
Copy link
Contributor Author

WunderBart commented Jun 13, 2024

Did you evaluate placing the wc_check_should_use_blockified_product_grid_templates function somewhere else? I don't have a better suggestion, but thinking that it would be really nice if we could keep it inside /src/Blocks/, given that it's related to block themes.

Good question, @Aljullu. I figured it should be outside of the src/Blocks because, IIUC, it’s relevant to both block and classic themes in the same way: The check disables that option when we switch from a block theme to a classic theme and enables it when we switch from a block theme to a classic theme. Now I kinda realize that it didn't have to be moved all the way out of the src/Blocks though, only the BTC class. 🤔

Not sure how, thought, maybe in a new file that is only initialized in the editor? 🤔 (This is not a hard opinion, just thinking out loud to know your thoughts)

What if we moved that check to the Options class itself? Depending on the perspective, this could either support or contradict the separation of concerns. If we'd like to separate it, maybe some sort of OptionsManager class could handle the options setup. I'm unsure if we wouldn't be over-optimizing at this point, as just one option needs to be set up this way. Anyway, if the OptionsManager idea sounds good to you, it looks to me like we could initialize it from Bootstrap.php somewhere along these lines?

@Aljullu
Copy link
Contributor

Aljullu commented Jun 14, 2024

What if we moved that check to the Options class itself? Depending on the perspective, this could either support or contradict the separation of concerns. If we'd like to separate it, maybe some sort of OptionsManager class could handle the options setup. I'm unsure if we wouldn't be over-optimizing at this point, as just one option needs to be set up this way. Anyway, if the OptionsManager idea sounds good to you, it looks to me like we could initialize it from Bootstrap.php somewhere along these lines?

That sounds like a good plan. It's not a big deal, but what I would really like is to:

  1. have it inside /src/Blocks because even though it also applies to classic themes, the option itself is only relevant for block templates
  2. ideally, only run the code in the admin site, not in the frontend (I know it's not a big deal because we use the after_switch_theme action, but still I think it reduces the chances of introducing bugs in the future)

So creating an OptionsManager class works for me. 💯

@WunderBart
Copy link
Contributor Author

WunderBart commented Jun 21, 2024

This PR unveiled an issue where the current_theme_supports( 'block-template-parts' ) always returns false when called from the Bootstrap.php class. I spent some time debugging this, and it seems that the Bootstrap class is initialized before the after_setup_theme hook is fired.

For testing purposes, I created a classic theme with the block template parts support and uploaded it to this PR. I've also pushed some debugging logs for easier inspection.

To verify the issue:

  1. cd plugins/woocommerce-blocks.

  2. Start wp-env with npx wp-env start --udpate.

  3. Go to http://localhost:8889/wp-admin/themes.php.

  4. Activate the Storefront Child (with block template parts) theme.

  5. Check the debug.log, it should contain something like this:

    [21-Jun-2024 14:48:06 UTC] -------------------------------
    [21-Jun-2024 14:50:07 UTC] From Bootstrap.php:
    [21-Jun-2024 14:50:07 UTC] Theme: Storefront Child (with block template parts)
    [21-Jun-2024 14:50:07 UTC] Supports BTP: no
    [21-Jun-2024 14:50:07 UTC] -------------------------------
    [21-Jun-2024 14:50:07 UTC] From theme's functions.php:
    [21-Jun-2024 14:50:07 UTC] Theme: Storefront Child (with block template parts)
    [21-Jun-2024 14:50:07 UTC] Supports BTP: yes
    [21-Jun-2024 14:50:07 UTC] -------------------------------
    

Note

If the wp-env debug logs aren't showing, you might need to do the following:

npx wp-env run tests-cli wp config set WP_DEBUG 1
npx wp-env run tests-cli wp config set WP_DEBUG_LOG 1
npx wp-env run tests-wordpress -- touch wp-content/debug.log
npx wp-env run tests-wordpress -- chmod 666 wp-content/debug.log
npx wp-env run tests-wordpress -- tail -f wp-content/debug.log

@Aljullu @gigitux @tjcafferkey, how do you suggest approaching this issue? Should we move the BTC initialization somewhere else? Or maybe the entire Bootstrap.php? 😅

@tjcafferkey
Copy link
Contributor

Hmm, I am trying to think if there is another method in which we can reliably determine if we should initialize the BTC.

Should we move the BTC initialization somewhere else? Or maybe the entire Bootstrap.php? 😅

I think if we can't find another way then it would make more sense/be safer to move the BTC initialization rather than the Bootstrap one. But only if we can't find another way around this limitation.

@tjcafferkey
Copy link
Contributor

Would there be any danger to doing something similar to this and initializing the BTC when the after_setup_theme hook is fired?

@Aljullu
Copy link
Contributor

Aljullu commented Jun 27, 2024

Would there be any danger to doing something similar to this and initializing the BTC when the after_setup_theme hook is fired?

Good idea! I'm trying this approach in #48905 and it seems to work fine, I did even test it with the modified Storefront version @WunderBart created and I couldn't spot anything off. We can either hook into after_setup_theme with a high priority or into init with any priority, I lean towards the second approach, but I don't have a strong opinion.

I will continue working on this tomorrow, as there are some tests failing, but hopefully I'll be able to open the PR for review before EOW. 🤞

@Aljullu
Copy link
Contributor

Aljullu commented Jun 28, 2024

We can either hook into after_setup_theme with a high priority or into init with any priority, I lean towards the second approach, but I don't have a strong opinion.

Well, not really. We need to use after_setup_theme as init seems to be too late and causes some issues with template parts.

In any case, I opened the PR for review in #48905. 🙂

@WunderBart
Copy link
Contributor Author

Closing in favor of #48905. Thanks @Aljullu and @tjcafferkey! 🙇

@WunderBart WunderBart closed this Jul 3, 2024
@WunderBart WunderBart deleted the refactor/btc-initialization branch August 12, 2024 12:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
plugin: woocommerce Issues related to the WooCommerce Core plugin.
Projects
None yet
3 participants