[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/icon` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Renders the `core/icon` block on server.
  10   *
  11   * @since 7.0.0
  12   *
  13   * @param array    $attributes The block attributes.
  14   * @param string   $content    The block content.
  15   * @param WP_Block $block      The block instance.
  16   *
  17   * @return string Returns the Icon.
  18   */
  19  function render_block_core_icon( $attributes ) {
  20      if ( empty( $attributes['icon'] ) ) {
  21          return;
  22      }
  23  
  24      $registry = WP_Icons_Registry::get_instance();
  25      $icon     = $registry->get_registered_icon( $attributes['icon'] );
  26  
  27      if ( is_null( $icon ) ) {
  28          return;
  29      }
  30  
  31      // Text color and background color.
  32      $color_styles = array();
  33  
  34      $preset_text_color    = array_key_exists( 'textColor', $attributes ) ? "var:preset|color|{$attributes['textColor']}" : null;
  35      $custom_text_color    = $attributes['style']['color']['text'] ?? null;
  36      $color_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
  37  
  38      $preset_background_color    = array_key_exists( 'backgroundColor', $attributes ) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
  39      $custom_background_color    = $attributes['style']['color']['background'] ?? null;
  40      $color_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
  41  
  42      // Border.
  43      $border_styles = array();
  44      $sides         = array( 'top', 'right', 'bottom', 'left' );
  45  
  46      if ( isset( $attributes['style']['border']['radius'] ) ) {
  47          $border_styles['radius'] = $attributes['style']['border']['radius'];
  48      }
  49      if ( isset( $attributes['style']['border']['style'] ) ) {
  50          $border_styles['style'] = $attributes['style']['border']['style'];
  51      }
  52      if ( isset( $attributes['style']['border']['width'] ) ) {
  53          $border_styles['width'] = $attributes['style']['border']['width'];
  54      }
  55  
  56      $preset_color           = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
  57      $custom_color           = $attributes['style']['border']['color'] ?? null;
  58      $border_styles['color'] = $preset_color ? $preset_color : $custom_color;
  59  
  60      foreach ( $sides as $side ) {
  61          $border                 = $attributes['style']['border'][ $side ] ?? null;
  62          $border_styles[ $side ] = array(
  63              'color' => $border['color'] ?? null,
  64              'style' => $border['style'] ?? null,
  65              'width' => $border['width'] ?? null,
  66          );
  67      }
  68  
  69      // Spacing (Padding).
  70      $spacing_styles = array();
  71      if ( isset( $attributes['style']['spacing']['padding'] ) ) {
  72          $spacing_styles['padding'] = $attributes['style']['spacing']['padding'];
  73      }
  74  
  75      // Dimensions (Width).
  76      $dimensions_styles = array();
  77      if ( isset( $attributes['style']['dimensions']['width'] ) ) {
  78          $dimensions_styles['width'] = $attributes['style']['dimensions']['width'];
  79      }
  80  
  81      // Generate styles and classes.
  82      $styles = wp_style_engine_get_styles(
  83          array(
  84              'color'      => $color_styles,
  85              'border'     => $border_styles,
  86              'spacing'    => $spacing_styles,
  87              'dimensions' => $dimensions_styles,
  88          ),
  89      );
  90  
  91      $processor = new WP_HTML_Tag_Processor( $icon['content'] );
  92      $processor->next_tag( 'svg' );
  93  
  94      if ( ! empty( $styles['css'] ) ) {
  95          $processor->set_attribute( 'style', $styles['css'] );
  96      }
  97      if ( ! empty( $styles['classnames'] ) ) {
  98          $processor->add_class( $styles['classnames'] );
  99      }
 100  
 101      $aria_label = ! empty( $attributes['ariaLabel'] ) ? $attributes['ariaLabel'] : '';
 102  
 103      if ( ! $aria_label ) {
 104          // Icon is decorative, hide it from screen readers.
 105          $processor->set_attribute( 'aria-hidden', 'true' );
 106          $processor->set_attribute( 'focusable', 'false' );
 107      } else {
 108          $processor->set_attribute( 'role', 'img' );
 109          $processor->set_attribute( 'aria-label', $aria_label );
 110      }
 111  
 112      // Return the updated SVG markup.
 113      $svg        = $processor->get_updated_html();
 114      $attributes = get_block_wrapper_attributes();
 115      return sprintf( '<div %s>%s</div>', $attributes, $svg );
 116  }
 117  
 118  
 119  /**
 120   * Registers the `core/icon` block on server.
 121   *
 122   * @since 7.0.0
 123   */
 124  function register_block_core_icon() {
 125      register_block_type_from_metadata(
 126          __DIR__ . '/icon',
 127          array(
 128              'render_callback' => 'render_block_core_icon',
 129          )
 130      );
 131  }
 132  add_action( 'init', 'register_block_core_icon' );


Generated : Sat Jun 13 09:38:55 2026 Cross-referenced by PHPXref