[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> button.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/button` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/button` block on the server,
  10   *
  11   * @since 6.6.0
  12   *
  13   * @param array  $attributes The block attributes.
  14   * @param string $content    The block content.
  15   *
  16   * @return string The block content.
  17   */
  18  function render_block_core_button( $attributes, $content ) {
  19      $p = new WP_HTML_Tag_Processor( $content );
  20  
  21      /*
  22       * The button block can render an `<a>` or `<button>` and also has a
  23       * `<div>` wrapper. Find the a or button tag.
  24       */
  25      $tag = null;
  26      while ( $p->next_tag() ) {
  27          $tag = $p->get_tag();
  28          if ( 'A' === $tag || 'BUTTON' === $tag ) {
  29              break;
  30          }
  31      }
  32  
  33      /*
  34       * If this happens, the likelihood is there's no block content,
  35       * or the block has been modified by a plugin.
  36       */
  37      if ( null === $tag ) {
  38          return $content;
  39      }
  40  
  41      // If the next token is the closing tag, the button is empty.
  42      $is_empty = true;
  43      while ( $p->next_token() && $tag !== $p->get_token_name() && $is_empty ) {
  44          if ( '#comment' !== $p->get_token_type() ) {
  45              /**
  46               * Anything else implies this is not empty.
  47               * This might include any text content (including a space),
  48               * inline images or other HTML.
  49               */
  50              $is_empty = false;
  51          }
  52      }
  53  
  54      /*
  55       * When there's no text, render nothing for the block.
  56       * See https://github.com/WordPress/gutenberg/issues/17221 for the
  57       * reasoning behind this.
  58       */
  59      if ( $is_empty ) {
  60          return '';
  61      }
  62  
  63      $width = $attributes['style']['dimensions']['width'] ?? null;
  64  
  65      if ( $width ) {
  66          // Resolve preset references to their actual values.
  67          $resolved_width = $width;
  68          $is_preset      = str_starts_with( $width, 'var:preset|dimension|' );
  69  
  70          if ( $is_preset ) {
  71              $slug              = substr( $width, strlen( 'var:preset|dimension|' ) );
  72              $dimension_presets = wp_get_global_settings(
  73                  array( 'dimensions', 'dimensionSizes' ),
  74                  array( 'block_name' => 'core/button' )
  75              );
  76  
  77              // Search origins in priority order: custom > theme > default.
  78              if ( is_array( $dimension_presets ) ) {
  79                  foreach ( array( 'custom', 'theme', 'default' ) as $origin ) {
  80                      if ( empty( $dimension_presets[ $origin ] ) || ! is_array( $dimension_presets[ $origin ] ) ) {
  81                          continue;
  82                      }
  83                      foreach ( $dimension_presets[ $origin ] as $preset ) {
  84                          if ( isset( $preset['slug'] ) && $preset['slug'] === $slug ) {
  85                              $resolved_width = $preset['size'] ?? $width;
  86                              break 2;
  87                          }
  88                      }
  89                  }
  90              }
  91          }
  92  
  93          $is_percentage = str_ends_with( $resolved_width, '%' );
  94  
  95          $processor = new WP_HTML_Tag_Processor( $content );
  96          // Target the outer wrapper div.
  97          if ( $processor->next_tag( array( 'class_name' => 'wp-block-button' ) ) ) {
  98              $processor->add_class( 'has-custom-width' );
  99              $existing_style = $processor->get_attribute( 'style' );
 100              $existing_style = is_string( $existing_style ) ? $existing_style : '';
 101  
 102              if ( $is_percentage ) {
 103                  $numeric_width = (float) $resolved_width;
 104                  $processor->add_class( 'wp-block-button__width' );
 105  
 106                  // Maintain legacy class for the standard percentage widths.
 107                  $legacy_widths = array(
 108                      '25%'  => 'wp-block-button__width-25',
 109                      '50%'  => 'wp-block-button__width-50',
 110                      '75%'  => 'wp-block-button__width-75',
 111                      '100%' => 'wp-block-button__width-100',
 112                  );
 113                  if ( isset( $legacy_widths[ $resolved_width ] ) ) {
 114                      $processor->add_class( $legacy_widths[ $resolved_width ] );
 115                  }
 116  
 117                  $width_style = "--wp--block-button--width: $numeric_width;";
 118                  $processor->set_attribute( 'style', $width_style . ( $existing_style ? ' ' . $existing_style : '' ) );
 119              } else {
 120                  $css_value   = $is_preset
 121                      ? 'var(--wp--preset--dimension--' . _wp_to_kebab_case( $slug ) . ')'
 122                      : $width;
 123                  $width_style = "width: $css_value;";
 124                  $processor->set_attribute( 'style', $width_style . ( $existing_style ? ' ' . $existing_style : '' ) );
 125              }
 126  
 127              $content = $processor->get_updated_html();
 128          }
 129      }
 130  
 131      return $content;
 132  }
 133  
 134  /**
 135   * Registers the `core/button` block on server.
 136   *
 137   * @since 6.6.0
 138   */
 139  function register_block_core_button() {
 140      register_block_type_from_metadata(
 141          __DIR__ . '/button',
 142          array(
 143              'render_callback' => 'render_block_core_button',
 144          )
 145      );
 146  }
 147  add_action( 'init', 'register_block_core_button' );


Generated : Fri Jul 3 08:20:12 2026 Cross-referenced by PHPXref