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

Add registered_block_type action, register clearing Theme JSON cache to fix global styles issue #3392

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/wp-includes/class-wp-block-type-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ public function register( $name, $args = array() ) {

$this->registered_block_types[ $name ] = $block_type;

/**
* Fires after a block type is registered with the WP_Block_Type_Registry class.
*
* @since 6.1.0
*
* @param WP_Block_Type $block_type The registered block type on success.
*/
do_action( 'registered_block_type', $block_type );
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not completely sure of what the best naming convention is, but I see some actions use past tense and others use something like 'after_register_block_type' or 'post_register_block_type'.

It would be interesting to know if there's a preference!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be interesting to know if there's a preference!

Same, I'd be curious to hear what folks think! In this case, I borrowed the naming convention from registered_post_type and registered_taxonomy.


return $block_type;
}

Expand Down Expand Up @@ -126,6 +135,15 @@ public function unregister( $name ) {
$unregistered_block_type = $this->registered_block_types[ $name ];
unset( $this->registered_block_types[ $name ] );

/**
* Fires after a block type is unregistered with the WP_Block_Type_Registry class.
*
* @since 6.1.0
*
* @param WP_Block_Type $block_type The unregistered block type on success.
*/
do_action( 'unregistered_block_type', $unregistered_block_type );

return $unregistered_block_type;
}

Expand Down
9 changes: 9 additions & 0 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,15 @@ protected static function append_to_selector( $selector, $to_append, $position =
return implode( ',', $new_selectors );
}

/**
* Cleans the cached data so it can be recalculated.
*
* @since 6.1.0
*/
public static function clean_cached_data() {
static::$blocks_metadata = null;
}

/**
* Returns the metadata for each block.
*
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
add_action( 'after_switch_theme', '_wp_sidebars_changed' );
add_action( 'wp_print_styles', 'print_emoji_styles' );
add_action( 'plugins_loaded', '_wp_theme_json_webfonts_handler' );
add_filter( 'registered_block_type', 'wp_clean_theme_json_cache' );

if ( isset( $_GET['replytocom'] ) ) {
add_filter( 'wp_robots', 'wp_robots_no_robots' );
Expand Down
12 changes: 12 additions & 0 deletions src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,18 @@ function wp_clean_themes_cache( $clear_update_cache = true ) {
}
}

/**
* Clears the cache held by WP_Theme_JSON and WP_Theme_JSON_Resolver classes.
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be worth adding more documentation if this is a public function. I'm thinking along the lines of a brief detailing of the side effects calling this function will have.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I've added an extra line here to indicate what the function does. I found it hard to capture in a single sentence without falling into the trap of describing the particular use case with registered_block_type. Happy to expand on that if it's suitable, though.

*
* Ensures that subsequent calls to either class generate fresh core, theme, user, and block data.
*
* @since 6.1.0
*/
function wp_clean_theme_json_cache() {
WP_Theme_JSON::clean_cached_data();
WP_Theme_JSON_Resolver::clean_cached_data();
}

/**
* Whether a child theme is in use.
*
Expand Down