PluginProbe
Gutenberg / 23.7.2
Gutenberg v23.7.2
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 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / icon.php

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

147 lines 4.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 // 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 $processor->set_attribute( 'style', $styles['css'] );
100 }
101
102 // Apply flip classes to the SVG.
103 $flip_horizontal = $attributes['flipHorizontal'] ?? false;
104 $flip_vertical = $attributes['flipVertical'] ?? false;
105
106 if ( $flip_horizontal ) {
107 $processor->add_class( 'is-flip-horizontal' );
108 }
109 if ( $flip_vertical ) {
110 $processor->add_class( 'is-flip-vertical' );
111 }
112
113 $rotation = isset( $attributes['rotation'] ) ? (int) $attributes['rotation'] : 0;
114
115 if ( $rotation ) {
116 $current_style = $processor->get_attribute( 'style' ) ?? '';
117 $rotation_css = 'rotate: ' . $rotation . 'deg;';
118 if ( $current_style ) {
119 $processor->set_attribute( 'style', $current_style . ' ' . $rotation_css );
120 } else {
121 $processor->set_attribute( 'style', $rotation_css );
122 }
123 }
124
125 $svg = $processor->get_updated_html();
126 }
127
128 $wrapper_attributes = get_block_wrapper_attributes();
129 return sprintf( '<div %s>%s</div>', $wrapper_attributes, $svg );
130 }
131
132
133 /**
134 * Registers the `core/icon` block on server.
135 *
136 * @since 7.0.0
137 */
138 function gutenberg_register_block_core_icon() {
139 register_block_type_from_metadata(
140 __DIR__ . '/icon',
141 array(
142 'render_callback' => 'gutenberg_render_block_core_icon',
143 )
144 );
145 }
146 add_action( 'init', 'gutenberg_register_block_core_icon', 20 );
147