wp/wp-includes/block-supports/block-style-variations.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
     4  * block style variations.
     4  * block style variations.
     5  *
     5  *
     6  * @package WordPress
     6  * @package WordPress
     7  * @since 6.6.0
     7  * @since 6.6.0
     8  */
     8  */
     9 
       
    10 /**
       
    11  * Generate block style variation instance name.
       
    12  *
       
    13  * @since 6.6.0
       
    14  * @access private
       
    15  *
       
    16  * @param array  $block     Block object.
       
    17  * @param string $variation Slug for the block style variation.
       
    18  *
       
    19  * @return string The unique variation name.
       
    20  */
       
    21 function wp_create_block_style_variation_instance_name( $block, $variation ) {
       
    22 	return $variation . '--' . md5( serialize( $block ) );
       
    23 }
       
    24 
     9 
    25 /**
    10 /**
    26  * Determines the block style variation names within a CSS class string.
    11  * Determines the block style variation names within a CSS class string.
    27  *
    12  *
    28  * @since 6.6.0
    13  * @since 6.6.0
    75 			}
    60 			}
    76 		}
    61 		}
    77 	}
    62 	}
    78 }
    63 }
    79 /**
    64 /**
    80  * Render the block style variation's styles.
    65  * Renders the block style variation's styles.
    81  *
    66  *
    82  * In the case of nested blocks with variations applied, we want the parent
    67  * In the case of nested blocks with variations applied, we want the parent
    83  * variation's styles to be rendered before their descendants. This solves the
    68  * variation's styles to be rendered before their descendants. This solves the
    84  * issue of a block type being styled in both the parent and descendant: we want
    69  * issue of a block type being styled in both the parent and descendant: we want
    85  * the descendant style to take priority, and this is done by loading it after,
    70  * the descendant style to take priority, and this is done by loading it after,
   122 	 * Recursively resolve any ref values with the appropriate value within the
   107 	 * Recursively resolve any ref values with the appropriate value within the
   123 	 * theme_json data.
   108 	 * theme_json data.
   124 	 */
   109 	 */
   125 	wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json );
   110 	wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json );
   126 
   111 
   127 	$variation_instance = wp_create_block_style_variation_instance_name( $parsed_block, $variation );
   112 	$variation_instance = wp_unique_id( $variation . '--' );
   128 	$class_name         = "is-style-$variation_instance";
   113 	$class_name         = "is-style-$variation_instance";
   129 	$updated_class_name = $parsed_block['attrs']['className'] . " $class_name";
   114 	$updated_class_name = $parsed_block['attrs']['className'] . " $class_name";
   130 
   115 
   131 	/*
   116 	/*
   132 	 * Even though block style variations are effectively theme.json partials,
   117 	 * Even though block style variations are effectively theme.json partials,
   207 
   192 
   208 	return $parsed_block;
   193 	return $parsed_block;
   209 }
   194 }
   210 
   195 
   211 /**
   196 /**
   212  * Ensure the variation block support class name generated and added to
   197  * Ensures the variation block support class name generated and added to
   213  * block attributes in the `render_block_data` filter gets applied to the
   198  * block attributes in the `render_block_data` filter gets applied to the
   214  * block's markup.
   199  * block's markup.
   215  *
   200  *
       
   201  * @since 6.6.0
       
   202  * @access private
       
   203  *
   216  * @see wp_render_block_style_variation_support_styles
   204  * @see wp_render_block_style_variation_support_styles
   217  *
       
   218  * @since 6.6.0
       
   219  * @access private
       
   220  *
   205  *
   221  * @param  string $block_content Rendered block content.
   206  * @param  string $block_content Rendered block content.
   222  * @param  array  $block         Block object.
   207  * @param  array  $block         Block object.
   223  *
   208  *
   224  * @return string                Filtered block content.
   209  * @return string                Filtered block content.
   228 		return $block_content;
   213 		return $block_content;
   229 	}
   214 	}
   230 
   215 
   231 	/*
   216 	/*
   232 	 * Matches a class prefixed by `is-style`, followed by the
   217 	 * Matches a class prefixed by `is-style`, followed by the
   233 	 * variation slug, then `--`, and finally a hash.
   218 	 * variation slug, then `--`, and finally an instance number.
   234 	 *
   219 	 */
   235 	 * See `wp_create_block_style_variation_instance_name` for class generation.
   220 	preg_match( '/\bis-style-(\S+?--\d+)\b/', $block['attrs']['className'], $matches );
   236 	 */
       
   237 	preg_match( '/\bis-style-(\S+?--\w+)\b/', $block['attrs']['className'], $matches );
       
   238 
   221 
   239 	if ( empty( $matches ) ) {
   222 	if ( empty( $matches ) ) {
   240 		return $block_content;
   223 		return $block_content;
   241 	}
   224 	}
   242 
   225