PluginProbe
Gutenberg / 17.0.2
Gutenberg v17.0.2
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 / lib / block-supports / duotone.php

duotone.php in Gutenberg 17.0.2, at lib/block-supports/duotone.php

439 lines 12.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Duotone block support flag.
4 *
5 * @package gutenberg
6 */
7
8 // Register the block support. (overrides core one).
9 WP_Block_Supports::get_instance()->register(
10 'duotone',
11 array(
12 'register_attribute' => array( 'WP_Duotone_Gutenberg', 'register_duotone_support' ),
13 )
14 );
15
16 // Set up metadata prior to rendering any blocks.
17 if ( class_exists( 'WP_Duotone' ) ) {
18 remove_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_styles_presets' ) );
19 remove_action( 'wp_loaded', array( 'WP_Duotone', 'set_global_style_block_names' ) );
20 }
21 add_action( 'wp_loaded', array( 'WP_Duotone_Gutenberg', 'set_global_styles_presets' ), 10 );
22 add_action( 'wp_loaded', array( 'WP_Duotone_Gutenberg', 'set_global_style_block_names' ), 10 );
23
24 // Add classnames to blocks using duotone support.
25 if ( function_exists( 'wp_render_duotone_support' ) ) {
26 // Deprecated render function.
27 remove_filter( 'render_block', 'wp_render_duotone_support' );
28 }
29 if ( class_exists( 'WP_Duotone' ) ) {
30 remove_filter( 'render_block', array( 'WP_Duotone', 'render_duotone_support' ) );
31 }
32 add_filter( 'render_block', array( 'WP_Duotone_Gutenberg', 'render_duotone_support' ), 10, 2 );
33
34 // Enqueue styles.
35 // Block styles (core-block-supports-inline-css) before the style engine (gutenberg_enqueue_stored_styles).
36 // Global styles (global-styles-inline-css) after the other global styles (gutenberg_enqueue_global_styles).
37 if ( class_exists( 'WP_Duotone' ) ) {
38 remove_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_block_styles' ) );
39 remove_action( 'wp_enqueue_scripts', array( 'WP_Duotone', 'output_global_styles' ) );
40 }
41 add_action( 'wp_enqueue_scripts', array( 'WP_Duotone_Gutenberg', 'output_block_styles' ), 9 );
42 add_action( 'wp_enqueue_scripts', array( 'WP_Duotone_Gutenberg', 'output_global_styles' ), 11 );
43
44 // Add SVG filters to the footer. Also, for classic themes, output block styles (core-block-supports-inline-css).
45 if ( class_exists( 'WP_Duotone' ) ) {
46 remove_action( 'wp_footer', array( 'WP_Duotone', 'output_footer_assets' ) );
47 }
48 add_action( 'wp_footer', array( 'WP_Duotone_Gutenberg', 'output_footer_assets' ), 10 );
49
50 // Add styles and SVGs for use in the editor via the EditorStyles component.
51 if ( class_exists( 'WP_Duotone' ) ) {
52 remove_filter( 'block_editor_settings_all', array( 'WP_Duotone', 'add_editor_settings' ) );
53 }
54 add_filter( 'block_editor_settings_all', array( 'WP_Duotone_Gutenberg', 'add_editor_settings' ), 10 );
55
56 // Migrate the old experimental duotone support flag.
57 if ( class_exists( 'WP_Duotone' ) ) {
58 remove_filter( 'block_type_metadata_settings', array( 'WP_Duotone', 'migrate_experimental_duotone_support_flag' ) );
59 }
60 add_filter( 'block_type_metadata_settings', array( 'WP_Duotone_Gutenberg', 'migrate_experimental_duotone_support_flag' ), 10, 2 );
61
62 /*
63 * Deprecated functions below. All new functions should be added in class-wp-duotone-gutenberg.php.
64 */
65
66 /**
67 * Direct port of tinycolor's bound01 function, lightly simplified to maintain
68 * consistency with tinycolor.
69 *
70 * @see https://github.com/bgrins/TinyColor
71 *
72 * @deprecated 6.3.0
73 *
74 * @param mixed $n Number of unknown type.
75 * @param int $max Upper value of the range to bound to.
76 * @return float Value in the range [0,1].
77 */
78 function gutenberg_tinycolor_bound01( $n, $max ) {
79 _deprecated_function( __FUNCTION__, '6.3.0' );
80
81 if ( 'string' === gettype( $n ) && str_contains( $n, '.' ) && 1 === (float) $n ) {
82 $n = '100%';
83 }
84
85 $n = min( $max, max( 0, (float) $n ) );
86
87 // Automatically convert percentage into number.
88 if ( 'string' === gettype( $n ) && str_contains( $n, '%' ) ) {
89 $n = (int) ( $n * $max ) / 100;
90 }
91
92 // Handle floating point rounding errors.
93 if ( ( abs( $n - $max ) < 0.000001 ) ) {
94 return 1.0;
95 }
96
97 // Convert into [0, 1] range if it isn't already.
98 return ( $n % $max ) / (float) $max;
99 }
100
101 /**
102 * Direct port of tinycolor's boundAlpha function to maintain consistency with
103 * how tinycolor works.
104 *
105 * @see https://github.com/bgrins/TinyColor
106 *
107 * @deprecated 6.3.0
108 *
109 * @param mixed $n Number of unknown type.
110 * @return float Value in the range [0,1].
111 */
112 function gutenberg_tinycolor_bound_alpha( $n ) {
113 _deprecated_function( __FUNCTION__, '6.3.0' );
114
115 if ( is_numeric( $n ) ) {
116 $n = (float) $n;
117 if ( $n >= 0 && $n <= 1 ) {
118 return $n;
119 }
120 }
121 return 1;
122 }
123
124 /**
125 * Round and convert values of an RGB object.
126 *
127 * @see https://github.com/bgrins/TinyColor
128 *
129 * @deprecated 6.3.0
130 *
131 * @param array $rgb_color RGB object.
132 * @return array Rounded and converted RGB object.
133 */
134 function gutenberg_tinycolor_rgb_to_rgb( $rgb_color ) {
135 _deprecated_function( __FUNCTION__, '6.3.0' );
136
137 return array(
138 'r' => gutenberg_tinycolor_bound01( $rgb_color['r'], 255 ) * 255,
139 'g' => gutenberg_tinycolor_bound01( $rgb_color['g'], 255 ) * 255,
140 'b' => gutenberg_tinycolor_bound01( $rgb_color['b'], 255 ) * 255,
141 );
142 }
143
144 /**
145 * Helper function for hsl to rgb conversion.
146 *
147 * @see https://github.com/bgrins/TinyColor
148 *
149 * @deprecated 6.3.0
150 *
151 * @param float $p first component.
152 * @param float $q second component.
153 * @param float $t third component.
154 * @return float R, G, or B component.
155 */
156 function gutenberg_tinycolor_hue_to_rgb( $p, $q, $t ) {
157 _deprecated_function( __FUNCTION__, '6.3.0' );
158
159 if ( $t < 0 ) {
160 ++$t;
161 }
162 if ( $t > 1 ) {
163 --$t;
164 }
165 if ( $t < 1 / 6 ) {
166 return $p + ( $q - $p ) * 6 * $t;
167 }
168 if ( $t < 1 / 2 ) {
169 return $q;
170 }
171 if ( $t < 2 / 3 ) {
172 return $p + ( $q - $p ) * ( 2 / 3 - $t ) * 6;
173 }
174 return $p;
175 }
176
177 /**
178 * Convert an HSL object to an RGB object with converted and rounded values.
179 *
180 * @see https://github.com/bgrins/TinyColor
181 *
182 * @deprecated 6.3.0
183 *
184 * @param array $hsl_color HSL object.
185 * @return array Rounded and converted RGB object.
186 */
187 function gutenberg_tinycolor_hsl_to_rgb( $hsl_color ) {
188 _deprecated_function( __FUNCTION__, '6.3.0' );
189
190 $h = gutenberg_tinycolor_bound01( $hsl_color['h'], 360 );
191 $s = gutenberg_tinycolor_bound01( $hsl_color['s'], 100 );
192 $l = gutenberg_tinycolor_bound01( $hsl_color['l'], 100 );
193
194 if ( 0 === $s ) {
195 // Achromatic.
196 $r = $l;
197 $g = $l;
198 $b = $l;
199 } else {
200 $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s;
201 $p = 2 * $l - $q;
202 $r = gutenberg_tinycolor_hue_to_rgb( $p, $q, $h + 1 / 3 );
203 $g = gutenberg_tinycolor_hue_to_rgb( $p, $q, $h );
204 $b = gutenberg_tinycolor_hue_to_rgb( $p, $q, $h - 1 / 3 );
205 }
206
207 return array(
208 'r' => $r * 255,
209 'g' => $g * 255,
210 'b' => $b * 255,
211 );
212 }
213
214 /**
215 * Parses hex, hsl, and rgb CSS strings using the same regex as tinycolor v1.4.2
216 * used in the JavaScript. Only colors output from react-color are implemented.
217 *
218 * @see https://github.com/bgrins/TinyColor
219 * @see https://github.com/casesandberg/react-color/
220 *
221 * @deprecated 6.3.0
222 *
223 * @param string $color_str CSS color string.
224 * @return array RGB object.
225 */
226 function gutenberg_tinycolor_string_to_rgb( $color_str ) {
227 _deprecated_function( __FUNCTION__, '6.3.0' );
228
229 $color_str = strtolower( trim( $color_str ) );
230
231 $css_integer = '[-\\+]?\\d+%?';
232 $css_number = '[-\\+]?\\d*\\.\\d+%?';
233
234 $css_unit = '(?:' . $css_number . ')|(?:' . $css_integer . ')';
235
236 $permissive_match3 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?';
237 $permissive_match4 = '[\\s|\\(]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')[,|\\s]+(' . $css_unit . ')\\s*\\)?';
238
239 $rgb_regexp = '/^rgb' . $permissive_match3 . '$/';
240 if ( preg_match( $rgb_regexp, $color_str, $match ) ) {
241 $rgb = gutenberg_tinycolor_rgb_to_rgb(
242 array(
243 'r' => $match[1],
244 'g' => $match[2],
245 'b' => $match[3],
246 )
247 );
248
249 $rgb['a'] = 1;
250
251 return $rgb;
252 }
253
254 $rgba_regexp = '/^rgba' . $permissive_match4 . '$/';
255 if ( preg_match( $rgba_regexp, $color_str, $match ) ) {
256 $rgb = gutenberg_tinycolor_rgb_to_rgb(
257 array(
258 'r' => $match[1],
259 'g' => $match[2],
260 'b' => $match[3],
261 )
262 );
263
264 $rgb['a'] = gutenberg_tinycolor_bound_alpha( $match[4] );
265
266 return $rgb;
267 }
268
269 $hsl_regexp = '/^hsl' . $permissive_match3 . '$/';
270 if ( preg_match( $hsl_regexp, $color_str, $match ) ) {
271 $rgb = gutenberg_tinycolor_hsl_to_rgb(
272 array(
273 'h' => $match[1],
274 's' => $match[2],
275 'l' => $match[3],
276 )
277 );
278
279 $rgb['a'] = 1;
280
281 return $rgb;
282 }
283
284 $hsla_regexp = '/^hsla' . $permissive_match4 . '$/';
285 if ( preg_match( $hsla_regexp, $color_str, $match ) ) {
286 $rgb = gutenberg_tinycolor_hsl_to_rgb(
287 array(
288 'h' => $match[1],
289 's' => $match[2],
290 'l' => $match[3],
291 )
292 );
293
294 $rgb['a'] = gutenberg_tinycolor_bound_alpha( $match[4] );
295
296 return $rgb;
297 }
298
299 $hex8_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
300 if ( preg_match( $hex8_regexp, $color_str, $match ) ) {
301 $rgb = gutenberg_tinycolor_rgb_to_rgb(
302 array(
303 'r' => base_convert( $match[1], 16, 10 ),
304 'g' => base_convert( $match[2], 16, 10 ),
305 'b' => base_convert( $match[3], 16, 10 ),
306 )
307 );
308
309 $rgb['a'] = gutenberg_tinycolor_bound_alpha(
310 base_convert( $match[4], 16, 10 ) / 255
311 );
312
313 return $rgb;
314 }
315
316 $hex6_regexp = '/^#?([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})$/';
317 if ( preg_match( $hex6_regexp, $color_str, $match ) ) {
318 $rgb = gutenberg_tinycolor_rgb_to_rgb(
319 array(
320 'r' => base_convert( $match[1], 16, 10 ),
321 'g' => base_convert( $match[2], 16, 10 ),
322 'b' => base_convert( $match[3], 16, 10 ),
323 )
324 );
325
326 $rgb['a'] = 1;
327
328 return $rgb;
329 }
330
331 $hex4_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
332 if ( preg_match( $hex4_regexp, $color_str, $match ) ) {
333 $rgb = gutenberg_tinycolor_rgb_to_rgb(
334 array(
335 'r' => base_convert( $match[1] . $match[1], 16, 10 ),
336 'g' => base_convert( $match[2] . $match[2], 16, 10 ),
337 'b' => base_convert( $match[3] . $match[3], 16, 10 ),
338 )
339 );
340
341 $rgb['a'] = gutenberg_tinycolor_bound_alpha(
342 base_convert( $match[4] . $match[4], 16, 10 ) / 255
343 );
344
345 return $rgb;
346 }
347
348 $hex3_regexp = '/^#?([0-9a-fA-F]{1})([0-9a-fA-F]{1})([0-9a-fA-F]{1})$/';
349 if ( preg_match( $hex3_regexp, $color_str, $match ) ) {
350 $rgb = gutenberg_tinycolor_rgb_to_rgb(
351 array(
352 'r' => base_convert( $match[1] . $match[1], 16, 10 ),
353 'g' => base_convert( $match[2] . $match[2], 16, 10 ),
354 'b' => base_convert( $match[3] . $match[3], 16, 10 ),
355 )
356 );
357
358 $rgb['a'] = 1;
359
360 return $rgb;
361 }
362
363 // The JS color picker considers the string "transparent" to be a hex value,
364 // so we need to handle it here as a special case.
365 if ( 'transparent' === $color_str ) {
366 return array(
367 'r' => 0,
368 'g' => 0,
369 'b' => 0,
370 'a' => 0,
371 );
372 }
373 }
374
375 /**
376 * Returns the prefixed id for the duotone filter for use as a CSS id.
377 *
378 * @deprecated 6.3.0
379 *
380 * @param array $preset Duotone preset value as seen in theme.json.
381 * @return string Duotone filter CSS id.
382 */
383 function gutenberg_get_duotone_filter_id( $preset ) {
384 _deprecated_function( __FUNCTION__, '6.3.0' );
385 return WP_Duotone_Gutenberg::get_filter_id_from_preset( $preset );
386 }
387
388 /**
389 * Returns the CSS filter property url to reference the rendered SVG.
390 *
391 * @deprecated 6.3.0
392 *
393 * @param array $preset Duotone preset value as seen in theme.json.
394 * @return string Duotone CSS filter property url value.
395 */
396 function gutenberg_get_duotone_filter_property( $preset ) {
397 _deprecated_function( __FUNCTION__, '6.3.0' );
398 return WP_Duotone_Gutenberg::get_filter_css_property_value_from_preset( $preset );
399 }
400
401 /**
402 * Returns the duotone filter SVG string for the preset.
403 *
404 * @deprecated 6.3.0
405 *
406 * @param array $preset Duotone preset value as seen in theme.json.
407 * @return string Duotone SVG filter.
408 */
409 function gutenberg_get_duotone_filter_svg( $preset ) {
410 _deprecated_function( __FUNCTION__, '6.3.0' );
411 return WP_Duotone_Gutenberg::get_filter_svg_from_preset( $preset );
412 }
413
414 /**
415 * Registers the style and colors block attributes for block types that support it.
416 *
417 * @deprecated 6.3.0 Use WP_Duotone_Gutenberg::register_duotone_support() instead.
418 *
419 * @param WP_Block_Type $block_type Block Type.
420 */
421 function gutenberg_register_duotone_support( $block_type ) {
422 _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone_Gutenberg::register_duotone_support' );
423 return WP_Duotone_Gutenberg::register_duotone_support( $block_type );
424 }
425
426 /**
427 * Render out the duotone stylesheet and SVG.
428 *
429 * @deprecated 6.3.0 Use WP_Duotone_Gutenberg::render_duotone_support() instead.
430 *
431 * @param string $block_content Rendered block content.
432 * @param array $block Block object.
433 * @return string Filtered block content.
434 */
435 function gutenberg_render_duotone_support( $block_content, $block ) {
436 _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Duotone_Gutenberg::render_duotone_support' );
437 return WP_Duotone_Gutenberg::render_duotone_support( $block_content, $block );
438 }
439