-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Duotone: Action causes premature caching of theme.json block nodes #49324
Comments
While updating Duotone to use the selectors API in #49325. I found that moving the This seems too brittle though so any ideas on perhaps invalidating the theme.json block metadata cache when accessed for the first time in the editor would be welcome 🙏 Quick hack in `WP_Theme_JSON_Gutenberg::get_blocks_metadata`diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php
index 8f4054bf46..a25ecada08 100644
--- a/lib/class-wp-theme-json-gutenberg.php
+++ b/lib/class-wp-theme-json-gutenberg.php
@@ -37,6 +37,17 @@ class WP_Theme_JSON_Gutenberg {
*/
protected static $blocks_metadata = array();
+ /**
+ * Holds the same data as $blocks_metadata except that it is only
+ * populated once blocks metadata has been cached in the editor. This is
+ * useful when there should be editor-only selectors and we need to ensure
+ * their inclusion despite theme.json potential already generating metadata
+ * during the earlier execution of hook actions and filters.
+ *
+ * @var array
+ */
+ protected static $editor_blocks_metadata = array();
+
/**
* The CSS selector for the top-level styles.
*
@@ -846,10 +857,19 @@ class WP_Theme_JSON_Gutenberg {
$registry = WP_Block_Type_Registry::get_instance();
$blocks = $registry->get_all_registered();
+ $in_editor = false;
+ if ( function_exists( 'get_current_screen' ) ) {
+ $in_editor = get_current_screen()->is_block_editor;
+ }
+
// Is there metadata for all currently registered blocks?
- $blocks = array_diff_key( $blocks, static::$blocks_metadata );
+ $blocks = array_diff_key(
+ $blocks,
+ $in_editor ? static::$editor_blocks_metadata : static::$blocks_metadata
+ );
+
if ( empty( $blocks ) ) {
- return static::$blocks_metadata;
+ return $in_editor ? static::$editor_blocks_metadata : static::$blocks_metadata;
}
foreach ( $blocks as $block_name => $block_type ) {
@@ -880,6 +900,10 @@ class WP_Theme_JSON_Gutenberg {
}
}
+ if ( $in_editor ) {
+ static::$editor_blocks_metadata = static::$blocks_metadata;
+ }
+
return static::$blocks_metadata;
}
@oandregal do you have any wisdom to share? |
I would recommend against this. It seems to me that the core issue is that I don't know enough of the new selectors API to be more concrete, sorry, but I can start by offering the following thoughts:
|
Honestly, I looked at where some other blocks had commonly hooked into for their actions. If delaying the action to cc @ajlende |
@aaronrobertshaw I put in a PR at #49343 to update the action to Edit: @scruffian helped me understand how to reproduce it. I was applying the global duotone style to the image in the editor, rather than via the global styles theme editor. |
Glad you were able to sort it out @jeryj 👍
That's the catch. Global styles are global styles and aren't applied to specific individual blocks in the block editor. The application of a duotone filter to a specific block is a "block support". There's so much overlap between the two it gets tricky to maintain the difference and nuance in conversation.
🙇 Thanks @oandregal for the idea. Just the sort of feedback I was hoping to get on the Selectors API PR as I wasn't sure about this approach and it was part of why I was chasing a final review of the API before merging. Unfortunately, I didn't communicate that desire clearly in my PR comments, so it merged prematurely. We have a little time before the next Gutenberg release, so If I can't move the detection of being in the editor, outside the class, the switch to the
I'll try and put a PR together for this. Honestly, though at this point, I don't exactly know where I should move the detection of being in the editor, to be able to pass it as context.
You could argue the old API did need editor selectors but fell short in not offering them. By not offering editor-only selectors, we were outputting useless CSS on the frontend just so we could get global styles to be applied correctly in the editor. The counterpoint to that was the old API didn't need editor-only selectors because it didn't care about the extra unused CSS on the frontend. |
Description
In #49103, SVG filter output was limited to only used filters. Part of this approach involved scraping block names using filters via the
wp_loaded
hook. It appears this is causing the theme.json block node processing to occur before theget_current_screen
global function is available which prevents editor-only selectors from being used in theme.json.Expected Behaviour
Duotone filter processing would occur after we might alter selectors for the editor.
Actual behaviour
Duotone actions cause caching of theme.json block node data prematurely.
Not being able to generate editor-only selectors via theme.json means that #46496 causes a regression with the Image block and the border not being applied to the inline cropping area.
Potential Solutions
wp_enqueue_scripts
fixes the editor-only selectors although I don't know the implications for duotone)Step-by-step reproduction instructions
styles.blocks['core/image'].border
in theme.jsonimage/block.json
Screenshots, screen recording, code snippet
No response
Environment info
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
The text was updated successfully, but these errors were encountered: