Classe che incapsula e implementa i supporti per i blocchi.

Fonte

File: wp-includes/class-wp-block-supports.php

classWP_Block_Supports{/**
	 * Config.
	 *
	 * @since 5.6.0
	 * @var array
	 */private$block_supports=array();/**
	 * Tracks the current block to be rendered.
	 *
	 * @since 5.6.0
	 * @var array
	 */publicstatic$block_to_render=null;/**
	 * Container for the main instance of the class.
	 *
	 * @since 5.6.0
	 * @var WP_Block_Supports|null
	 */privatestatic$instance=null;/**
	 * Utility method to retrieve the main instance of the class.
	 *
	 * The instance will be created if it does not exist yet.
	 *
	 * @since 5.6.0
	 *
	 * @return WP_Block_Supports The main instance.
	 */publicstaticfunctionget_instance(){if(null===self::$instance){self::$instance=newself();}returnself::$instance;}/**
	 * Initializes the block supports. It registes the block supports block attributes.
	 *
	 * @since 5.6.0
	 */publicstaticfunctioninit(){$instance=self::get_instance();$instance->register_attributes();}/**
	 * Registers a block support.
	 *
	 * @since 5.6.0
	 *
	 * @param string $block_support_name Block support name.
	 * @param array  $block_support_config Array containing the properties of the block support.
	 */publicfunctionregister($block_support_name,$block_support_config){$this->block_supports[$block_support_name]=array_merge($block_support_config,array('name'=>$block_support_name));}/**
	 * Generates an array of HTML attributes, such as classes, by applying to
	 * the given block all of the features that the block supports.
	 *
	 * @since 5.6.0
	 *
	 * @return array Array of HTML attributes.
	 */publicfunctionapply_block_supports(){$block_attributes=self::$block_to_render['attrs'];$block_type=WP_Block_Type_Registry::get_instance()->get_registered(self::$block_to_render['blockName']);// If no render_callback, assume styles have been previously handled.if(!$block_type||empty($block_type)){returnarray();}$output=array();foreach($this->block_supportsas$block_support_config){if(!isset($block_support_config['apply'])){continue;}$new_attributes=call_user_func($block_support_config['apply'],$block_type,$block_attributes);if(!empty($new_attributes)){foreach($new_attributesas$attribute_name=>$attribute_value){if(empty($output[$attribute_name])){$output[$attribute_name]=$attribute_value;}else{$output[$attribute_name].=" $attribute_value";}}}}return$output;}/**
	 * Registers the block attributes required by the different block supports.
	 *
	 * @since 5.6.0
	 */privatefunctionregister_attributes(){$block_registry=WP_Block_Type_Registry::get_instance();$registered_block_types=$block_registry->get_all_registered();foreach($registered_block_typesas$block_type){if(!property_exists($block_type,'supports')){continue;}if(!$block_type->attributes){$block_type->attributes=array();}foreach($this->block_supportsas$block_support_config){if(!isset($block_support_config['register_attribute'])){continue;}call_user_func($block_support_config['register_attribute'],$block_type);}}}}

Metodi

  • apply_block_supports - Genera un array di attributi HTML, come le classi, applicando al blocco dato tutte le caratteristiche che il blocco supporta.
  • get_instance - Metodo di utilità per recuperare l'istanza principale della classe.
  • init - Inizializza i supporti del blocco. Registra gli attributi del blocco supportato.
  • register - Registra un supporto a blocchi.
  • register_attributes - Registra gli attributi di blocco richiesti dai diversi supporti di blocco.

Changelog

Versione Descrizione
5.6.0 Introdotto.