[ 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   *
  15   * @return string Returns the Icon.
  16   */
  17  function render_block_core_icon( $attributes ) {
  18      if ( empty( $attributes['icon'] ) || ! is_string( $attributes['icon'] ) ) {
  19          return;
  20      }
  21  
  22      // Icons in non-public collections are not available in the editor, so do not render them either.
  23      $registered_icon = WP_Icons_Registry::get_instance()->get_registered_icon( $attributes['icon'] );
  24      if ( null !== $registered_icon ) {
  25          $icon_collection = WP_Icon_Collections_Registry::get_instance()->get_registered( $registered_icon['collection'] );
  26          if ( null !== $icon_collection && ! $icon_collection['public'] ) {
  27              return;
  28          }
  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      // Background image, size and gradient.
  43      $background_styles = array();
  44      if ( isset( $attributes['style']['background'] ) && is_array( $attributes['style']['background'] ) ) {
  45          $background_styles = array_intersect_key(
  46              $attributes['style']['background'],
  47              array_flip( array( 'backgroundImage', 'backgroundSize', 'backgroundPosition', 'backgroundRepeat', 'backgroundAttachment', 'gradient' ) )
  48          );
  49  
  50          // Mirror of the defaults in the background block support.
  51          if ( ! empty( $background_styles['backgroundImage'] ) ) {
  52              $background_styles['backgroundSize'] = $background_styles['backgroundSize'] ?? 'cover';
  53              if ( 'contain' === $background_styles['backgroundSize'] && empty( $background_styles['backgroundPosition'] ) ) {
  54                  $background_styles['backgroundPosition'] = '50% 50%';
  55              }
  56          }
  57      }
  58  
  59      // Border.
  60      $border_styles = array();
  61      $sides         = array( 'top', 'right', 'bottom', 'left' );
  62  
  63      if ( isset( $attributes['style']['border']['radius'] ) ) {
  64          $border_styles['radius'] = $attributes['style']['border']['radius'];
  65      }
  66      if ( isset( $attributes['style']['border']['style'] ) ) {
  67          $border_styles['style'] = $attributes['style']['border']['style'];
  68      }
  69      if ( isset( $attributes['style']['border']['width'] ) ) {
  70          $border_styles['width'] = $attributes['style']['border']['width'];
  71      }
  72  
  73      $preset_color           = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
  74      $custom_color           = $attributes['style']['border']['color'] ?? null;
  75      $border_styles['color'] = $preset_color ? $preset_color : $custom_color;
  76  
  77      foreach ( $sides as $side ) {
  78          $border                 = $attributes['style']['border'][ $side ] ?? null;
  79          $border_styles[ $side ] = array(
  80              'color' => $border['color'] ?? null,
  81              'style' => $border['style'] ?? null,
  82              'width' => $border['width'] ?? null,
  83          );
  84      }
  85  
  86      // Shadow.
  87      $shadow_styles = $attributes['style']['shadow'] ?? null;
  88  
  89      // Spacing (Padding).
  90      $spacing_styles = array();
  91      if ( isset( $attributes['style']['spacing']['padding'] ) ) {
  92          $spacing_styles['padding'] = $attributes['style']['spacing']['padding'];
  93      }
  94  
  95      // Dimensions (Width, Min width, Min height).
  96      $dimensions_styles = array();
  97      foreach ( array( 'width', 'minWidth', 'minHeight' ) as $dimension ) {
  98          if ( isset( $attributes['style']['dimensions'][ $dimension ] ) ) {
  99              $dimensions_styles[ $dimension ] = $attributes['style']['dimensions'][ $dimension ];
 100          }
 101      }
 102  
 103      // Generate styles and classes.
 104      $styles = wp_style_engine_get_styles(
 105          array(
 106              'color'      => $color_styles,
 107              'background' => $background_styles,
 108              'border'     => $border_styles,
 109              'shadow'     => $shadow_styles,
 110              'spacing'    => $spacing_styles,
 111              'dimensions' => $dimensions_styles,
 112          ),
 113      );
 114  
 115      $svg = wp_get_icon(
 116          $attributes['icon'],
 117          array(
 118              // Width is applied via the dimensions block support styles below.
 119              'size'  => null,
 120              'class' => $styles['classnames'] ?? '',
 121              'label' => $attributes['ariaLabel'] ?? '',
 122          )
 123      );
 124  
 125      if ( '' === $svg ) {
 126          return;
 127      }
 128  
 129      $processor = new WP_HTML_Tag_Processor( $svg );
 130      if ( $processor->next_tag( 'svg' ) ) {
 131          if ( ! empty( $styles['css'] ) ) {
 132              // Merge with the SVG's intrinsic style (e.g. `fill: none` on
 133              // stroke-based icons) so it is preserved. The block styles come last
 134              // so they win on any conflicting property.
 135              $existing_style = $processor->get_attribute( 'style' );
 136              $trimmed_style  = is_string( $existing_style )
 137                  ? rtrim( trim( $existing_style ), ';' )
 138                  : '';
 139              $merged_style   = '' !== $trimmed_style
 140                  ? $trimmed_style . '; ' . $styles['css']
 141                  : $styles['css'];
 142              $processor->set_attribute( 'style', $merged_style );
 143          }
 144  
 145          // The style engine only adds `has-background` for a gradient.
 146          if ( ! empty( $background_styles['backgroundImage'] ) ) {
 147              $processor->add_class( 'has-background' );
 148          }
 149  
 150          // Apply flip classes to the SVG.
 151          $flip_horizontal = $attributes['flipHorizontal'] ?? false;
 152          $flip_vertical   = $attributes['flipVertical'] ?? false;
 153  
 154          if ( $flip_horizontal ) {
 155              $processor->add_class( 'is-flip-horizontal' );
 156          }
 157          if ( $flip_vertical ) {
 158              $processor->add_class( 'is-flip-vertical' );
 159          }
 160  
 161          $rotation = isset( $attributes['rotation'] ) ? (int) $attributes['rotation'] : 0;
 162  
 163          if ( $rotation ) {
 164              $current_style = $processor->get_attribute( 'style' ) ?? '';
 165              $rotation_css  = 'rotate: ' . $rotation . 'deg;';
 166              $trimmed_style = is_string( $current_style )
 167                  ? rtrim( trim( $current_style ), ';' )
 168                  : '';
 169              $merged_style  = '' !== $trimmed_style
 170                  ? $trimmed_style . '; ' . $rotation_css
 171                  : $rotation_css;
 172              $processor->set_attribute( 'style', $merged_style );
 173          }
 174  
 175          $svg = $processor->get_updated_html();
 176      }
 177  
 178      $wrapper_attributes = get_block_wrapper_attributes();
 179      return sprintf( '<div %s>%s</div>', $wrapper_attributes, $svg );
 180  }
 181  
 182  
 183  /**
 184   * Registers the `core/icon` block on server.
 185   *
 186   * @since 7.0.0
 187   */
 188  function register_block_core_icon() {
 189      register_block_type_from_metadata(
 190          __DIR__ . '/icon',
 191          array(
 192              'render_callback' => 'render_block_core_icon',
 193          )
 194      );
 195  }
 196  add_action( 'init', 'register_block_core_icon' );


Generated : Tue Nov 3 08:20:36 2026 Cross-referenced by PHPXref