PluginProbe
Gutenberg / 24.0.0
Gutenberg v24.0.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / scripts / block-library / icon.php

icon.php in Gutenberg 24.0.0, at build/scripts/block-library/icon.php

159 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_render_block_core_icon( $attributes ) {
18 if ( empty( $attributes['icon'] ) ) {
19 return;
20 }
21
22 // Text color and background color.
23 $color_styles = array();
24
25 $preset_text_color = array_key_exists( 'textColor', $attributes ) ? "var:preset|color|{$attributes['textColor']}" : null;
26 $custom_text_color = $attributes['style']['color']['text'] ?? null;
27 $color_styles['text'] = $preset_text_color ? $preset_text_color : $custom_text_color;
28
29 $preset_background_color = array_key_exists( 'backgroundColor', $attributes ) ? "var:preset|color|{$attributes['backgroundColor']}" : null;
30 $custom_background_color = $attributes['style']['color']['background'] ?? null;
31 $color_styles['background'] = $preset_background_color ? $preset_background_color : $custom_background_color;
32
33 // Border.
34 $border_styles = array();
35 $sides = array( 'top', 'right', 'bottom', 'left' );
36
37 if ( isset( $attributes['style']['border']['radius'] ) ) {
38 $border_styles['radius'] = $attributes['style']['border']['radius'];
39 }
40 if ( isset( $attributes['style']['border']['style'] ) ) {
41 $border_styles['style'] = $attributes['style']['border']['style'];
42 }
43 if ( isset( $attributes['style']['border']['width'] ) ) {
44 $border_styles['width'] = $attributes['style']['border']['width'];
45 }
46
47 $preset_color = array_key_exists( 'borderColor', $attributes ) ? "var:preset|color|{$attributes['borderColor']}" : null;
48 $custom_color = $attributes['style']['border']['color'] ?? null;
49 $border_styles['color'] = $preset_color ? $preset_color : $custom_color;
50
51 foreach ( $sides as $side ) {
52 $border = $attributes['style']['border'][ $side ] ?? null;
53 $border_styles[ $side ] = array(
54 'color' => $border['color'] ?? null,
55 'style' => $border['style'] ?? null,
56 'width' => $border['width'] ?? null,
57 );
58 }
59
60 // Spacing (Padding).
61 $spacing_styles = array();
62 if ( isset( $attributes['style']['spacing']['padding'] ) ) {
63 $spacing_styles['padding'] = $attributes['style']['spacing']['padding'];
64 }
65
66 // Dimensions (Width).
67 $dimensions_styles = array();
68 if ( isset( $attributes['style']['dimensions']['width'] ) ) {
69 $dimensions_styles['width'] = $attributes['style']['dimensions']['width'];
70 }
71
72 // Generate styles and classes.
73 $styles = gutenberg_style_engine_get_styles(
74 array(
75 'color' => $color_styles,
76 'border' => $border_styles,
77 'spacing' => $spacing_styles,
78 'dimensions' => $dimensions_styles,
79 ),
80 );
81
82 $svg = wp_get_icon(
83 $attributes['icon'],
84 array(
85 // Width is applied via the dimensions block support styles below.
86 'size' => null,
87 'class' => $styles['classnames'] ?? '',
88 'label' => $attributes['ariaLabel'] ?? '',
89 )
90 );
91
92 if ( '' === $svg ) {
93 return;
94 }
95
96 $processor = new WP_HTML_Tag_Processor( $svg );
97 if ( $processor->next_tag( 'svg' ) ) {
98 if ( ! empty( $styles['css'] ) ) {
99 // Merge with the SVG's intrinsic style (e.g. `fill: none` on
100 // stroke-based icons) so it is preserved. The block styles come last
101 // so they win on any conflicting property.
102 $existing_style = $processor->get_attribute( 'style' );
103 $trimmed_style = is_string( $existing_style )
104 ? rtrim( trim( $existing_style ), ';' )
105 : '';
106 $merged_style = '' !== $trimmed_style
107 ? $trimmed_style . '; ' . $styles['css']
108 : $styles['css'];
109 $processor->set_attribute( 'style', $merged_style );
110 }
111
112 // Apply flip classes to the SVG.
113 $flip_horizontal = $attributes['flipHorizontal'] ?? false;
114 $flip_vertical = $attributes['flipVertical'] ?? false;
115
116 if ( $flip_horizontal ) {
117 $processor->add_class( 'is-flip-horizontal' );
118 }
119 if ( $flip_vertical ) {
120 $processor->add_class( 'is-flip-vertical' );
121 }
122
123 $rotation = isset( $attributes['rotation'] ) ? (int) $attributes['rotation'] : 0;
124
125 if ( $rotation ) {
126 $current_style = $processor->get_attribute( 'style' ) ?? '';
127 $rotation_css = 'rotate: ' . $rotation . 'deg;';
128 $trimmed_style = is_string( $current_style )
129 ? rtrim( trim( $current_style ), ';' )
130 : '';
131 $merged_style = '' !== $trimmed_style
132 ? $trimmed_style . '; ' . $rotation_css
133 : $rotation_css;
134 $processor->set_attribute( 'style', $merged_style );
135 }
136
137 $svg = $processor->get_updated_html();
138 }
139
140 $wrapper_attributes = get_block_wrapper_attributes();
141 return sprintf( '<div %s>%s</div>', $wrapper_attributes, $svg );
142 }
143
144
145 /**
146 * Registers the `core/icon` block on server.
147 *
148 * @since 7.0.0
149 */
150 function gutenberg_register_block_core_icon() {
151 register_block_type_from_metadata(
152 __DIR__ . '/icon',
153 array(
154 'render_callback' => 'gutenberg_render_block_core_icon',
155 )
156 );
157 }
158 add_action( 'init', 'gutenberg_register_block_core_icon', 20 );
159