wp/wp-includes/class-wp-block-styles-registry.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    10 /**
    10 /**
    11  * Class used for interacting with block styles.
    11  * Class used for interacting with block styles.
    12  *
    12  *
    13  * @since 5.3.0
    13  * @since 5.3.0
    14  */
    14  */
       
    15 #[AllowDynamicProperties]
    15 final class WP_Block_Styles_Registry {
    16 final class WP_Block_Styles_Registry {
    16 	/**
    17 	/**
    17 	 * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays.
    18 	 * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays.
    18 	 *
    19 	 *
    19 	 * @since 5.3.0
    20 	 * @since 5.3.0
    32 	private static $instance = null;
    33 	private static $instance = null;
    33 
    34 
    34 	/**
    35 	/**
    35 	 * Registers a block style for the given block type.
    36 	 * Registers a block style for the given block type.
    36 	 *
    37 	 *
    37 	 * @since 5.3.0
    38 	 * If the block styles are present in a standalone stylesheet, register it and pass
    38 	 *
    39 	 * its handle as the `style_handle` argument. If the block styles should be inline,
    39 	 * @param string $block_name       Block type name including namespace.
    40 	 * use the `inline_style` argument. Usually, one of them would be used to pass CSS
    40 	 * @param array  $style_properties Array containing the properties of the style name, label,
    41 	 * styles. However, you could also skip them and provide CSS styles in any stylesheet
    41 	 *                                 is_default, style_handle (name of the stylesheet to be enqueued),
    42 	 * or with an inline tag.
    42 	 *                                 inline_style (string containing the CSS to be added).
    43 	 *
       
    44 	 * @since 5.3.0
       
    45 	 * @since 6.6.0 Added ability to register style across multiple block types along with theme.json-like style data.
       
    46 	 *
       
    47 	 * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-styles/
       
    48 	 *
       
    49 	 * @param string|string[] $block_name       Block type name including namespace or array of namespaced block type names.
       
    50 	 * @param array           $style_properties {
       
    51 	 *     Array containing the properties of the style.
       
    52 	 *
       
    53 	 *     @type string $name         The identifier of the style used to compute a CSS class.
       
    54 	 *     @type string $label        A human-readable label for the style.
       
    55 	 *     @type string $inline_style Inline CSS code that registers the CSS class required
       
    56 	 *                                for the style.
       
    57 	 *     @type string $style_handle The handle to an already registered style that should be
       
    58 	 *                                enqueued in places where block styles are needed.
       
    59 	 *     @type bool   $is_default   Whether this is the default style for the block type.
       
    60 	 *     @type array  $style_data   Theme.json-like object to generate CSS from.
       
    61 	 * }
    43 	 * @return bool True if the block style was registered with success and false otherwise.
    62 	 * @return bool True if the block style was registered with success and false otherwise.
    44 	 */
    63 	 */
    45 	public function register( $block_name, $style_properties ) {
    64 	public function register( $block_name, $style_properties ) {
    46 
    65 
    47 		if ( ! isset( $block_name ) || ! is_string( $block_name ) ) {
    66 		if ( ! is_string( $block_name ) && ! is_array( $block_name ) ) {
    48 			_doing_it_wrong(
    67 			_doing_it_wrong(
    49 				__METHOD__,
    68 				__METHOD__,
    50 				__( 'Block name must be a string.' ),
    69 				__( 'Block name must be a string or array.' ),
    51 				'5.3.0'
    70 				'6.6.0'
    52 			);
    71 			);
    53 			return false;
    72 			return false;
    54 		}
    73 		}
    55 
    74 
    56 		if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) {
    75 		if ( ! isset( $style_properties['name'] ) || ! is_string( $style_properties['name'] ) ) {
    70 			);
    89 			);
    71 			return false;
    90 			return false;
    72 		}
    91 		}
    73 
    92 
    74 		$block_style_name = $style_properties['name'];
    93 		$block_style_name = $style_properties['name'];
    75 
    94 		$block_names      = is_string( $block_name ) ? array( $block_name ) : $block_name;
    76 		if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) {
    95 
    77 			$this->registered_block_styles[ $block_name ] = array();
    96 		foreach ( $block_names as $name ) {
    78 		}
    97 			if ( ! isset( $this->registered_block_styles[ $name ] ) ) {
    79 		$this->registered_block_styles[ $block_name ][ $block_style_name ] = $style_properties;
    98 				$this->registered_block_styles[ $name ] = array();
       
    99 			}
       
   100 			$this->registered_block_styles[ $name ][ $block_style_name ] = $style_properties;
       
   101 		}
    80 
   102 
    81 		return true;
   103 		return true;
    82 	}
   104 	}
    83 
   105 
    84 	/**
   106 	/**