PluginProbe
Gutenberg / 22.7.0
Gutenberg v22.7.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 / lib / block-supports / duotone.php

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

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