Skip to content

Commit

Permalink
Theme JSON: Extract util to get valid block style variations (WordPre…
Browse files Browse the repository at this point in the history
…ss#61030)

Co-authored-by: aaronrobertshaw <[email protected]>
Co-authored-by: youknowriad <[email protected]>
  • Loading branch information
3 people authored Apr 25, 2024
1 parent fd50003 commit f071978
Showing 1 changed file with 21 additions and 16 deletions.
37 changes: 21 additions & 16 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -729,15 +729,9 @@ public function __construct( $theme_json = array( 'version' => WP_Theme_JSON_Gut
$registry = WP_Block_Type_Registry::get_instance();
$valid_block_names = array_keys( $registry->get_all_registered() );
$valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = array();
foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
if ( ! isset( $block_meta['styleVariations'] ) ) {
continue;
}
$valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
}
$theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
$this->theme_json = static::maybe_opt_in_into_settings( $theme_json );
$valid_variations = static::get_valid_block_style_variations();
$theme_json = static::sanitize( $this->theme_json, $valid_block_names, $valid_element_names, $valid_variations );
$this->theme_json = static::maybe_opt_in_into_settings( $theme_json );

// Internally, presets are keyed by origin.
$nodes = static::get_setting_nodes( $this->theme_json );
Expand Down Expand Up @@ -3130,13 +3124,7 @@ public static function remove_insecure_properties( $theme_json ) {

$valid_block_names = array_keys( static::get_blocks_metadata() );
$valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = array();
foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
if ( ! isset( $block_meta['styleVariations'] ) ) {
continue;
}
$valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
}
$valid_variations = static::get_valid_block_style_variations();

$theme_json = static::sanitize( $theme_json, $valid_block_names, $valid_element_names, $valid_variations );

Expand Down Expand Up @@ -4043,4 +4031,21 @@ function ( $matches ) use ( $variation_class ) {

return implode( ',', $result );
}

/**
* Collects valid block style variations keyed by block type.
*
* @return array Valid block style variations by block type.
*/
protected static function get_valid_block_style_variations() {
$valid_variations = array();
foreach ( self::get_blocks_metadata() as $block_name => $block_meta ) {
if ( ! isset( $block_meta['styleVariations'] ) ) {
continue;
}
$valid_variations[ $block_name ] = array_keys( $block_meta['styleVariations'] );
}

return $valid_variations;
}
}

0 comments on commit f071978

Please sign in to comment.