PluginProbe
Gutenberg / 24.1.0
Gutenberg v24.1.0
24.1.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 All 404 releases
gutenberg / build / scripts / block-library / icon.php

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

168 lines 5.3 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 // 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 = gutenberg_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 gutenberg_register_block_core_icon() {
160 register_block_type_from_metadata(
161 __DIR__ . '/icon',
162 array(
163 'render_callback' => 'gutenberg_render_block_core_icon',
164 )
165 );
166 }
167 add_action( 'init', 'gutenberg_register_block_core_icon', 20 );
168