PluginProbe
Gutenberg / 23.5.2
Gutenberg v23.5.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 / lib / compat / plugin / style-state-aliases.php

style-state-aliases.php in Gutenberg 23.5.2, at lib/compat/plugin/style-state-aliases.php

77 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin-specific style state alias back compatibility.
4 *
5 * @package Gutenberg
6 */
7
8 /**
9 * Resolves temporary aliases for persisted style state keys to their canonical keys.
10 *
11 * This keeps compatibility for Gutenberg plugin content created before
12 * responsive states used '@' and custom states used '-'. Remove after the
13 * deprecation window ends.
14 *
15 * DO NOT BACKPORT TO CORE.
16 *
17 * @param array $styles Persisted style data.
18 * @param string|null $block_name Current block name, when walking block styles.
19 * @return array Style data with canonical style state keys.
20 */
21 function gutenberg_resolve_style_state_aliases( $styles, $block_name = null ) {
22 if ( ! is_array( $styles ) ) {
23 return $styles;
24 }
25
26 $responsive_breakpoint_aliases = array(
27 '@mobile' => 'mobile',
28 '@tablet' => 'tablet',
29 );
30
31 foreach ( $responsive_breakpoint_aliases as $state => $legacy_state ) {
32 if ( array_key_exists( $legacy_state, $styles ) && ! array_key_exists( $state, $styles ) ) {
33 $styles[ $state ] = $styles[ $legacy_state ];
34 }
35 unset( $styles[ $legacy_state ] );
36 }
37
38 $block_custom_state_aliases = array(
39 'core/navigation-link' => array(
40 '-current' => '@current',
41 ),
42 );
43
44 if ( $block_name && isset( $block_custom_state_aliases[ $block_name ] ) ) {
45 foreach ( $block_custom_state_aliases[ $block_name ] as $state => $legacy_state ) {
46 if ( array_key_exists( $legacy_state, $styles ) && ! array_key_exists( $state, $styles ) ) {
47 $styles[ $state ] = $styles[ $legacy_state ];
48 }
49 unset( $styles[ $legacy_state ] );
50 }
51 }
52
53 foreach ( $styles as $key => $value ) {
54 if ( ! is_array( $value ) ) {
55 continue;
56 }
57
58 if ( 'blocks' === $key ) {
59 foreach ( $value as $child_block_name => $child_value ) {
60 $styles[ $key ][ $child_block_name ] = gutenberg_resolve_style_state_aliases( $child_value, $child_block_name );
61 }
62 continue;
63 }
64
65 if ( 'elements' === $key || 'variations' === $key ) {
66 foreach ( $value as $child_key => $child_value ) {
67 $styles[ $key ][ $child_key ] = gutenberg_resolve_style_state_aliases( $child_value, $block_name );
68 }
69 continue;
70 }
71
72 $styles[ $key ] = gutenberg_resolve_style_state_aliases( $value, $block_name );
73 }
74
75 return $styles;
76 }
77