[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Anchor block support flag.
   4   *
   5   * @package WordPress
   6   * @since 7.0.0
   7   */
   8  
   9  /**
  10   * Registers the anchor block attribute for block types that support it.
  11   *
  12   * @since 7.0.0
  13   * @access private
  14   *
  15   * @param WP_Block_Type $block_type Block Type.
  16   */
  17  function wp_register_anchor_support( WP_Block_Type $block_type ) {
  18      if ( ! block_has_support( $block_type, array( 'anchor' ) ) ) {
  19          return;
  20      }
  21  
  22      $block_type->attributes ??= array();
  23  
  24      if ( ! array_key_exists( 'anchor', $block_type->attributes ) ) {
  25          $block_type->attributes['anchor'] = array(
  26              'type' => 'string',
  27          );
  28      }
  29  }
  30  
  31  /**
  32   * Add the anchor id to the output.
  33   *
  34   * @since 7.0.0
  35   * @access private
  36   *
  37   * @param WP_Block_Type        $block_type       Block Type.
  38   * @param array<string, mixed> $block_attributes Block attributes.
  39   * @return array<string, string> Attributes with block anchor id.
  40   */
  41  function wp_apply_anchor_support( WP_Block_Type $block_type, array $block_attributes ): array {
  42      if ( empty( $block_attributes ) ) {
  43          return array();
  44      }
  45  
  46      if ( ! block_has_support( $block_type, array( 'anchor' ) ) ) {
  47          return array();
  48      }
  49  
  50      if ( ! isset( $block_attributes['anchor'] ) || ! is_string( $block_attributes['anchor'] ) || '' === $block_attributes['anchor'] ) {
  51          return array();
  52      }
  53  
  54      return array( 'id' => $block_attributes['anchor'] );
  55  }
  56  
  57  // Register the block support.
  58  WP_Block_Supports::get_instance()->register(
  59      'anchor',
  60      array(
  61          'register_attribute' => 'wp_register_anchor_support',
  62          'apply'              => 'wp_apply_anchor_support',
  63      )
  64  );


Generated : Mon Nov 23 08:20:01 2026 Cross-referenced by PHPXref