| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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'] ) ) { 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 // 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 $svg = wp_get_icon( 92 $attributes['icon'], 93 array( 94 // Width is applied via the dimensions block support styles below. 95 'size' => null, 96 'class' => $styles['classnames'] ?? '', 97 'label' => $attributes['ariaLabel'] ?? '', 98 ) 99 ); 100 101 if ( '' === $svg ) { 102 return; 103 } 104 105 $processor = new WP_HTML_Tag_Processor( $svg ); 106 if ( $processor->next_tag( 'svg' ) ) { 107 if ( ! empty( $styles['css'] ) ) { 108 // Merge with the SVG's intrinsic style (e.g. `fill: none` on 109 // stroke-based icons) so it is preserved. The block styles come last 110 // so they win on any conflicting property. 111 $existing_style = $processor->get_attribute( 'style' ); 112 $trimmed_style = is_string( $existing_style ) 113 ? rtrim( trim( $existing_style ), ';' ) 114 : ''; 115 $merged_style = '' !== $trimmed_style 116 ? $trimmed_style . '; ' . $styles['css'] 117 : $styles['css']; 118 $processor->set_attribute( 'style', $merged_style ); 119 } 120 121 // Apply flip classes to the SVG. 122 $flip_horizontal = $attributes['flipHorizontal'] ?? false; 123 $flip_vertical = $attributes['flipVertical'] ?? false; 124 125 if ( $flip_horizontal ) { 126 $processor->add_class( 'is-flip-horizontal' ); 127 } 128 if ( $flip_vertical ) { 129 $processor->add_class( 'is-flip-vertical' ); 130 } 131 132 $rotation = isset( $attributes['rotation'] ) ? (int) $attributes['rotation'] : 0; 133 134 if ( $rotation ) { 135 $current_style = $processor->get_attribute( 'style' ) ?? ''; 136 $rotation_css = 'rotate: ' . $rotation . 'deg;'; 137 $trimmed_style = is_string( $current_style ) 138 ? rtrim( trim( $current_style ), ';' ) 139 : ''; 140 $merged_style = '' !== $trimmed_style 141 ? $trimmed_style . '; ' . $rotation_css 142 : $rotation_css; 143 $processor->set_attribute( 'style', $merged_style ); 144 } 145 146 $svg = $processor->get_updated_html(); 147 } 148 149 $wrapper_attributes = get_block_wrapper_attributes(); 150 return sprintf( '<div %s>%s</div>', $wrapper_attributes, $svg ); 151 } 152 153 154 /** 155 * Registers the `core/icon` block on server. 156 * 157 * @since 7.0.0 158 */ 159 function register_block_core_icon() { 160 register_block_type_from_metadata( 161 __DIR__ . '/icon', 162 array( 163 'render_callback' => 'render_block_core_icon', 164 ) 165 ); 166 } 167 add_action( 'init', 'register_block_core_icon' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Oct 13 08:20:33 2026 | Cross-referenced by PHPXref |