[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-block-supports.php (source)

   1  <?php
   2  /**
   3   * Block support flags.
   4   *
   5   * @package WordPress
   6   *
   7   * @since 5.6.0
   8   */
   9  
  10  /**
  11   * Class encapsulating and implementing Block Supports.
  12   *
  13   * @since 5.6.0
  14   *
  15   * @access private
  16   */
  17  class WP_Block_Supports {
  18  
  19      /**
  20       * Config.
  21       *
  22       * @since 5.6.0
  23       * @var array
  24       */
  25      private $block_supports = array();
  26  
  27      /**
  28       * Tracks the current block to be rendered.
  29       *
  30       * @since 5.6.0
  31       * @var array
  32       */
  33      public static $block_to_render = null;
  34  
  35      /**
  36       * Container for the main instance of the class.
  37       *
  38       * @since 5.6.0
  39       * @var WP_Block_Supports|null
  40       */
  41      private static $instance = null;
  42  
  43      /**
  44       * Utility method to retrieve the main instance of the class.
  45       *
  46       * The instance will be created if it does not exist yet.
  47       *
  48       * @since 5.6.0
  49       *
  50       * @return WP_Block_Supports The main instance.
  51       */
  52  	public static function get_instance() {
  53          if ( null === self::$instance ) {
  54              self::$instance = new self();
  55          }
  56  
  57          return self::$instance;
  58      }
  59  
  60      /**
  61       * Initializes the block supports. It registers the block supports block attributes.
  62       *
  63       * @since 5.6.0
  64       */
  65  	public static function init() {
  66          $instance = self::get_instance();
  67          $instance->register_attributes();
  68      }
  69  
  70      /**
  71       * Registers a block support.
  72       *
  73       * @since 5.6.0
  74       *
  75       * @param string $block_support_name   Block support name.
  76       * @param array  $block_support_config Array containing the properties of the block support.
  77       */
  78  	public function register( $block_support_name, $block_support_config ) {
  79          $this->block_supports[ $block_support_name ] = array_merge(
  80              $block_support_config,
  81              array( 'name' => $block_support_name )
  82          );
  83      }
  84  
  85      /**
  86       * Generates an array of HTML attributes, such as classes, by applying to
  87       * the given block all of the features that the block supports.
  88       *
  89       * @since 5.6.0
  90       *
  91       * @return string[] Array of HTML attributes.
  92       */
  93  	public function apply_block_supports() {
  94          $block_attributes = self::$block_to_render['attrs'];
  95          $block_type       = WP_Block_Type_Registry::get_instance()->get_registered(
  96              self::$block_to_render['blockName']
  97          );
  98  
  99          // If no render_callback, assume styles have been previously handled.
 100          if ( ! $block_type || empty( $block_type ) ) {
 101              return array();
 102          }
 103  
 104          $output = array();
 105          foreach ( $this->block_supports as $block_support_config ) {
 106              if ( ! isset( $block_support_config['apply'] ) ) {
 107                  continue;
 108              }
 109  
 110              $new_attributes = call_user_func(
 111                  $block_support_config['apply'],
 112                  $block_type,
 113                  $block_attributes
 114              );
 115  
 116              if ( ! empty( $new_attributes ) ) {
 117                  foreach ( $new_attributes as $attribute_name => $attribute_value ) {
 118                      if ( empty( $output[ $attribute_name ] ) ) {
 119                          $output[ $attribute_name ] = $attribute_value;
 120                      } else {
 121                          $output[ $attribute_name ] .= " $attribute_value";
 122                      }
 123                  }
 124              }
 125          }
 126  
 127          return $output;
 128      }
 129  
 130      /**
 131       * Registers the block attributes required by the different block supports.
 132       *
 133       * @since 5.6.0
 134       */
 135  	private function register_attributes() {
 136          $block_registry         = WP_Block_Type_Registry::get_instance();
 137          $registered_block_types = $block_registry->get_all_registered();
 138          foreach ( $registered_block_types as $block_type ) {
 139              if ( ! property_exists( $block_type, 'supports' ) ) {
 140                  continue;
 141              }
 142              if ( ! $block_type->attributes ) {
 143                  $block_type->attributes = array();
 144              }
 145  
 146              foreach ( $this->block_supports as $block_support_config ) {
 147                  if ( ! isset( $block_support_config['register_attribute'] ) ) {
 148                      continue;
 149                  }
 150  
 151                  call_user_func(
 152                      $block_support_config['register_attribute'],
 153                      $block_type
 154                  );
 155              }
 156          }
 157      }
 158  }
 159  
 160  /**
 161   * Generates a string of attributes by applying to the current block being
 162   * rendered all of the features that the block supports.
 163   *
 164   * @since 5.6.0
 165   *
 166   * @param string[] $extra_attributes Optional. Array of extra attributes to render on the block wrapper.
 167   * @return string String of HTML attributes.
 168   */
 169  function get_block_wrapper_attributes( $extra_attributes = array() ) {
 170      $new_attributes = WP_Block_Supports::get_instance()->apply_block_supports();
 171  
 172      if ( empty( $new_attributes ) && empty( $extra_attributes ) ) {
 173          return '';
 174      }
 175  
 176      // This is hardcoded on purpose.
 177      // We only support a fixed list of attributes.
 178      $attributes_to_merge = array( 'style', 'class' );
 179      $attributes          = array();
 180      foreach ( $attributes_to_merge as $attribute_name ) {
 181          if ( empty( $new_attributes[ $attribute_name ] ) && empty( $extra_attributes[ $attribute_name ] ) ) {
 182              continue;
 183          }
 184  
 185          if ( empty( $new_attributes[ $attribute_name ] ) ) {
 186              $attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ];
 187              continue;
 188          }
 189  
 190          if ( empty( $extra_attributes[ $attribute_name ] ) ) {
 191              $attributes[ $attribute_name ] = $new_attributes[ $attribute_name ];
 192              continue;
 193          }
 194  
 195          $attributes[ $attribute_name ] = $extra_attributes[ $attribute_name ] . ' ' . $new_attributes[ $attribute_name ];
 196      }
 197  
 198      foreach ( $extra_attributes as $attribute_name => $value ) {
 199          if ( ! in_array( $attribute_name, $attributes_to_merge, true ) ) {
 200              $attributes[ $attribute_name ] = $value;
 201          }
 202      }
 203  
 204      if ( empty( $attributes ) ) {
 205          return '';
 206      }
 207  
 208      $normalized_attributes = array();
 209      foreach ( $attributes as $key => $value ) {
 210          $normalized_attributes[] = $key . '="' . esc_attr( $value ) . '"';
 211      }
 212  
 213      return implode( ' ', $normalized_attributes );
 214  }


Generated: Sun Dec 22 01:00:02 2024 Cross-referenced by PHPXref 0.7.1