wp/wp-includes/class-wp-block-supports.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    12  *
    12  *
    13  * @since 5.6.0
    13  * @since 5.6.0
    14  *
    14  *
    15  * @access private
    15  * @access private
    16  */
    16  */
       
    17 #[AllowDynamicProperties]
    17 class WP_Block_Supports {
    18 class WP_Block_Supports {
    18 
    19 
    19 	/**
    20 	/**
    20 	 * Config.
    21 	 * Config.
    21 	 *
    22 	 *
    69 
    70 
    70 	/**
    71 	/**
    71 	 * Registers a block support.
    72 	 * Registers a block support.
    72 	 *
    73 	 *
    73 	 * @since 5.6.0
    74 	 * @since 5.6.0
       
    75 	 *
       
    76 	 * @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
    74 	 *
    77 	 *
    75 	 * @param string $block_support_name   Block support name.
    78 	 * @param string $block_support_name   Block support name.
    76 	 * @param array  $block_support_config Array containing the properties of the block support.
    79 	 * @param array  $block_support_config Array containing the properties of the block support.
    77 	 */
    80 	 */
    78 	public function register( $block_support_name, $block_support_config ) {
    81 	public function register( $block_support_name, $block_support_config ) {
    86 	 * Generates an array of HTML attributes, such as classes, by applying to
    89 	 * Generates an array of HTML attributes, such as classes, by applying to
    87 	 * the given block all of the features that the block supports.
    90 	 * the given block all of the features that the block supports.
    88 	 *
    91 	 *
    89 	 * @since 5.6.0
    92 	 * @since 5.6.0
    90 	 *
    93 	 *
    91 	 * @return string[] Array of HTML attributes.
    94 	 * @return string[] Array of HTML attribute values keyed by their name.
    92 	 */
    95 	 */
    93 	public function apply_block_supports() {
    96 	public function apply_block_supports() {
    94 		$block_attributes = self::$block_to_render['attrs'];
    97 		$block_type = WP_Block_Type_Registry::get_instance()->get_registered(
    95 		$block_type       = WP_Block_Type_Registry::get_instance()->get_registered(
       
    96 			self::$block_to_render['blockName']
    98 			self::$block_to_render['blockName']
    97 		);
    99 		);
    98 
   100 
    99 		// If no render_callback, assume styles have been previously handled.
   101 		// If no render_callback, assume styles have been previously handled.
   100 		if ( ! $block_type || empty( $block_type ) ) {
   102 		if ( ! $block_type || empty( $block_type ) ) {
   101 			return array();
   103 			return array();
   102 		}
   104 		}
       
   105 
       
   106 		$block_attributes = array_key_exists( 'attrs', self::$block_to_render ) && is_array( self::$block_to_render['attrs'] )
       
   107 			? self::$block_to_render['attrs']
       
   108 			: array();
   103 
   109 
   104 		$output = array();
   110 		$output = array();
   105 		foreach ( $this->block_supports as $block_support_config ) {
   111 		foreach ( $this->block_supports as $block_support_config ) {
   106 			if ( ! isset( $block_support_config['apply'] ) ) {
   112 			if ( ! isset( $block_support_config['apply'] ) ) {
   107 				continue;
   113 				continue;
   134 	 */
   140 	 */
   135 	private function register_attributes() {
   141 	private function register_attributes() {
   136 		$block_registry         = WP_Block_Type_Registry::get_instance();
   142 		$block_registry         = WP_Block_Type_Registry::get_instance();
   137 		$registered_block_types = $block_registry->get_all_registered();
   143 		$registered_block_types = $block_registry->get_all_registered();
   138 		foreach ( $registered_block_types as $block_type ) {
   144 		foreach ( $registered_block_types as $block_type ) {
   139 			if ( ! property_exists( $block_type, 'supports' ) ) {
   145 			if ( ! ( $block_type instanceof WP_Block_Type ) ) {
   140 				continue;
   146 				continue;
   141 			}
   147 			}
   142 			if ( ! $block_type->attributes ) {
   148 			if ( ! $block_type->attributes ) {
   143 				$block_type->attributes = array();
   149 				$block_type->attributes = array();
   144 			}
   150 			}
   173 		return '';
   179 		return '';
   174 	}
   180 	}
   175 
   181 
   176 	// This is hardcoded on purpose.
   182 	// This is hardcoded on purpose.
   177 	// We only support a fixed list of attributes.
   183 	// We only support a fixed list of attributes.
   178 	$attributes_to_merge = array( 'style', 'class' );
   184 	$attributes_to_merge = array( 'style', 'class', 'id' );
   179 	$attributes          = array();
   185 	$attributes          = array();
   180 	foreach ( $attributes_to_merge as $attribute_name ) {
   186 	foreach ( $attributes_to_merge as $attribute_name ) {
   181 		if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) {
   187 		if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) {
   182 			continue;
   188 			continue;
   183 		}
   189 		}