PluginProbe
Gutenberg / 23.5.1
Gutenberg v23.5.1
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 / lib / block-supports / duotone.php

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

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