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 4 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
26 changes: 24 additions & 2 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,10 +497,21 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
$metadata
);

return WP_Block_Type_Registry::get_instance()->register(
$block = WP_Block_Type_Registry::get_instance()->register(
andrewserong marked this conversation as resolved.
Show resolved Hide resolved
$metadata['name'],
$settings
);

/**
* Fires after a block type is registered.
*
* @since 6.1.0
*
* @param WP_Block_Type|false $block The registered block type on success, or false on failure.
*/
do_action( 'registered_block_type', $block );

return $block;
}

/**
Expand All @@ -526,7 +537,18 @@ function register_block_type( $block_type, $args = array() ) {
return register_block_type_from_metadata( $block_type, $args );
}

return WP_Block_Type_Registry::get_instance()->register( $block_type, $args );
$block = WP_Block_Type_Registry::get_instance()->register( $block_type, $args );

/**
* Fires after a block type is registered.
*
* @since 6.1.0
*
* @param WP_Block_Type|false $block The registered block type on success, or false on failure.
*/
do_action( 'registered_block_type', $block );

return $block;
}

/**
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
10 changes: 10 additions & 0 deletions src/wp-includes/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ 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.

*
* @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