[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Auto-register block support.
   4   *
   5   * @package WordPress
   6   * @since 7.0.0
   7   */
   8  
   9  /**
  10   * Marks user-defined attributes for auto-generated inspector controls.
  11   *
  12   * This filter runs during block type registration, before the WP_Block_Type
  13   * is instantiated. Block supports add their attributes AFTER the block type
  14   * is created (via {@see WP_Block_Supports::register_attributes()}), so any attributes
  15   * present at this stage are user-defined.
  16   *
  17   * The marker tells generateFieldsFromAttributes() which attributes should
  18   * get auto-generated inspector controls. Attributes are excluded if they:
  19   * - Have a 'source' (HTML-derived, edited inline not via inspector)
  20   * - Have role 'local' (internal state, not user-configurable)
  21   * - Have an unsupported type (only 'string', 'number', 'integer', 'boolean' are supported)
  22   * - Were added by block supports (added after this filter runs)
  23   *
  24   * @since 7.0.0
  25   * @access private
  26   *
  27   * @param array<string, mixed> $args Array of arguments for registering a block type.
  28   * @return array<string, mixed> Modified block type arguments.
  29   */
  30  function wp_mark_auto_generate_control_attributes( array $args ): array {
  31      if ( empty( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) {
  32          return $args;
  33      }
  34  
  35      $has_auto_register = ! empty( $args['supports']['autoRegister'] );
  36      if ( ! $has_auto_register ) {
  37          return $args;
  38      }
  39  
  40      foreach ( $args['attributes'] as $attr_key => $attr_schema ) {
  41          // Skip HTML-derived attributes (edited inline, not via inspector).
  42          if ( ! empty( $attr_schema['source'] ) ) {
  43              continue;
  44          }
  45          // Skip internal attributes (not user-configurable).
  46          if ( isset( $attr_schema['role'] ) && 'local' === $attr_schema['role'] ) {
  47              continue;
  48          }
  49          // Skip unsupported types (only 'string', 'number', 'integer', 'boolean' are supported).
  50          $type = $attr_schema['type'] ?? null;
  51          if ( ! in_array( $type, array( 'string', 'number', 'integer', 'boolean' ), true ) ) {
  52              continue;
  53          }
  54          $args['attributes'][ $attr_key ]['autoGenerateControl'] = true;
  55      }
  56  
  57      return $args;
  58  }
  59  
  60  // Priority 5 to mark original attributes before other filters (priority 10+) might add their own.
  61  add_filter( 'register_block_type_args', 'wp_mark_auto_generate_control_attributes', 5 );


Generated : Sun Jun 14 08:20:09 2026 Cross-referenced by PHPXref