wp/wp-includes/class-wp-block-styles-registry.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    15 final class WP_Block_Styles_Registry {
    15 final class WP_Block_Styles_Registry {
    16 	/**
    16 	/**
    17 	 * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays.
    17 	 * Registered block styles, as `$block_name => $block_style_name => $block_style_properties` multidimensional arrays.
    18 	 *
    18 	 *
    19 	 * @since 5.3.0
    19 	 * @since 5.3.0
    20 	 * @var array
    20 	 *
       
    21 	 * @var array[]
    21 	 */
    22 	 */
    22 	private $registered_block_styles = array();
    23 	private $registered_block_styles = array();
    23 
    24 
    24 	/**
    25 	/**
    25 	 * Container for the main instance of the class.
    26 	 * Container for the main instance of the class.
    26 	 *
    27 	 *
    27 	 * @since 5.3.0
    28 	 * @since 5.3.0
       
    29 	 *
    28 	 * @var WP_Block_Styles_Registry|null
    30 	 * @var WP_Block_Styles_Registry|null
    29 	 */
    31 	 */
    30 	private static $instance = null;
    32 	private static $instance = null;
    31 
    33 
    32 	/**
    34 	/**
    33 	 * Registers a block style.
    35 	 * Registers a block style for the given block type.
    34 	 *
    36 	 *
    35 	 * @since 5.3.0
    37 	 * @since 5.3.0
    36 	 *
    38 	 *
    37 	 * @param string $block_name       Block type name including namespace.
    39 	 * @param string $block_name       Block type name including namespace.
    38 	 * @param array  $style_properties Array containing the properties of the style name, label,
    40 	 * @param array  $style_properties Array containing the properties of the style name, label,
    58 				'5.3.0'
    60 				'5.3.0'
    59 			);
    61 			);
    60 			return false;
    62 			return false;
    61 		}
    63 		}
    62 
    64 
       
    65 		if ( str_contains( $style_properties['name'], ' ' ) ) {
       
    66 			_doing_it_wrong(
       
    67 				__METHOD__,
       
    68 				__( 'Block style name must not contain any spaces.' ),
       
    69 				'5.9.0'
       
    70 			);
       
    71 			return false;
       
    72 		}
       
    73 
    63 		$block_style_name = $style_properties['name'];
    74 		$block_style_name = $style_properties['name'];
    64 
    75 
    65 		if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) {
    76 		if ( ! isset( $this->registered_block_styles[ $block_name ] ) ) {
    66 			$this->registered_block_styles[ $block_name ] = array();
    77 			$this->registered_block_styles[ $block_name ] = array();
    67 		}
    78 		}
    69 
    80 
    70 		return true;
    81 		return true;
    71 	}
    82 	}
    72 
    83 
    73 	/**
    84 	/**
    74 	 * Unregisters a block style.
    85 	 * Unregisters a block style of the given block type.
       
    86 	 *
       
    87 	 * @since 5.3.0
    75 	 *
    88 	 *
    76 	 * @param string $block_name       Block type name including namespace.
    89 	 * @param string $block_name       Block type name including namespace.
    77 	 * @param string $block_style_name Block style name.
    90 	 * @param string $block_style_name Block style name.
    78 	 * @return bool True if the block style was unregistered with success and false otherwise.
    91 	 * @return bool True if the block style was unregistered with success and false otherwise.
    79 	 */
    92 	 */
    92 
   105 
    93 		return true;
   106 		return true;
    94 	}
   107 	}
    95 
   108 
    96 	/**
   109 	/**
    97 	 * Retrieves an array containing the properties of a registered block style.
   110 	 * Retrieves the properties of a registered block style for the given block type.
    98 	 *
   111 	 *
    99 	 * @since 5.3.0
   112 	 * @since 5.3.0
   100 	 *
   113 	 *
   101 	 * @param string $block_name       Block type name including namespace.
   114 	 * @param string $block_name       Block type name including namespace.
   102 	 * @param string $block_style_name Block style name.
   115 	 * @param string $block_style_name Block style name.
   113 	/**
   126 	/**
   114 	 * Retrieves all registered block styles.
   127 	 * Retrieves all registered block styles.
   115 	 *
   128 	 *
   116 	 * @since 5.3.0
   129 	 * @since 5.3.0
   117 	 *
   130 	 *
   118 	 * @return array Array of arrays containing the registered block styles properties grouped per block,
   131 	 * @return array[] Array of arrays containing the registered block styles properties grouped by block type.
   119 	 *               and per style.
       
   120 	 */
   132 	 */
   121 	public function get_all_registered() {
   133 	public function get_all_registered() {
   122 		return $this->registered_block_styles;
   134 		return $this->registered_block_styles;
   123 	}
   135 	}
   124 
   136 
   125 	/**
   137 	/**
   126 	 * Retrieves registered block styles for a specific block.
   138 	 * Retrieves registered block styles for a specific block type.
   127 	 *
   139 	 *
   128 	 * @since 5.3.0
   140 	 * @since 5.3.0
   129 	 *
   141 	 *
   130 	 * @param string $block_name Block type name including namespace.
   142 	 * @param string $block_name Block type name including namespace.
   131 	 * @return array Array whose keys are block style names and whose value are block style properties.
   143 	 * @return array[] Array whose keys are block style names and whose values are block style properties.
   132 	 */
   144 	 */
   133 	public function get_registered_styles_for_block( $block_name ) {
   145 	public function get_registered_styles_for_block( $block_name ) {
   134 		if ( isset( $this->registered_block_styles[ $block_name ] ) ) {
   146 		if ( isset( $this->registered_block_styles[ $block_name ] ) ) {
   135 			return $this->registered_block_styles[ $block_name ];
   147 			return $this->registered_block_styles[ $block_name ];
   136 		}
   148 		}
   137 		return array();
   149 		return array();
   138 	}
   150 	}
   139 
   151 
   140 	/**
   152 	/**
   141 	 * Checks if a block style is registered.
   153 	 * Checks if a block style is registered for the given block type.
   142 	 *
   154 	 *
   143 	 * @since 5.3.0
   155 	 * @since 5.3.0
   144 	 *
   156 	 *
   145 	 * @param string $block_name       Block type name including namespace.
   157 	 * @param string $block_name       Block type name including namespace.
   146 	 * @param string $block_style_name Block style name.
   158 	 * @param string $block_style_name Block style name.