PluginProbe
Gutenberg / 12.1.0
Gutenberg v12.1.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 7.4.0 All 402 releases
gutenberg / lib / compat / wordpress-5.8 / utils.php

utils.php in Gutenberg 12.1.0, at lib/compat/wordpress-5.8/utils.php

157 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * General utilities.
4 *
5 * @package gutenberg
6 */
7
8 if ( ! function_exists( '_wp_array_set' ) ) {
9 /**
10 * Sets an array in depth based on a path of keys.
11 *
12 * It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
13 * retain some symmetry between client and server implementations.
14 *
15 * Example usage:
16 *
17 * $array = array();
18 * _wp_array_set( $array, array( 'a', 'b', 'c', 1 ) );
19 *
20 * $array becomes:
21 * array(
22 * 'a' => array(
23 * 'b' => array(
24 * 'c' => 1,
25 * ),
26 * ),
27 * );
28 *
29 * @internal
30 *
31 * @since 5.8.0
32 * @access private
33 *
34 * @param array $array An array that we want to mutate to include a specific value in a path.
35 * @param array $path An array of keys describing the path that we want to mutate.
36 * @param mixed $value The value that will be set.
37 */
38 function _wp_array_set( &$array, $path, $value = null ) {
39 // Confirm $array is valid.
40 if ( ! is_array( $array ) ) {
41 return;
42 }
43
44 // Confirm $path is valid.
45 if ( ! is_array( $path ) ) {
46 return;
47 }
48
49 $path_length = count( $path );
50
51 if ( 0 === $path_length ) {
52 return;
53 }
54
55 foreach ( $path as $path_element ) {
56 if (
57 ! is_string( $path_element ) && ! is_integer( $path_element ) &&
58 ! is_null( $path_element )
59 ) {
60 return;
61 }
62 }
63
64 for ( $i = 0; $i < $path_length - 1; ++$i ) {
65 $path_element = $path[ $i ];
66 if (
67 ! array_key_exists( $path_element, $array ) ||
68 ! is_array( $array[ $path_element ] )
69 ) {
70 $array[ $path_element ] = array();
71 }
72 $array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
73 }
74
75 $array[ $path[ $i ] ] = $value;
76 }
77 }
78
79 if ( ! function_exists( '_wp_to_kebab_case' ) ) {
80 /**
81 * This function is trying to replicate what
82 * lodash's kebabCase (JS library) does in the client.
83 *
84 * The reason we need this function is that we do some processing
85 * in both the client and the server (e.g.: we generate
86 * preset classes from preset slugs) that needs to
87 * create the same output.
88 *
89 * We can't remove or update the client's library due to backward compatibility
90 * (some of the output of lodash's kebabCase is saved in the post content).
91 * We have to make the server behave like the client.
92 *
93 * Changes to this function should follow updates in the client
94 * with the same logic.
95 *
96 * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L14369
97 * @link https://github.com/lodash/lodash/blob/4.17/dist/lodash.js#L278
98 * @link https://github.com/lodash-php/lodash-php/blob/master/src/String/kebabCase.php
99 * @link https://github.com/lodash-php/lodash-php/blob/master/src/internal/unicodeWords.php
100 *
101 * @param string $string The string to kebab-case.
102 *
103 * @return string kebab-cased-string.
104 */
105 function _wp_to_kebab_case( $string ) {
106 //phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
107 // ignore the camelCase names for variables so the names are the same as lodash
108 // so comparing and porting new changes is easier.
109
110 /*
111 * Some notable things we've removed compared to the lodash version are:
112 *
113 * - non-alphanumeric characters: rsAstralRange, rsEmoji, etc
114 * - the groups that processed the apostrophe, as it's removed before passing the string to preg_match: rsApos, rsOptContrLower, and rsOptContrUpper
115 *
116 */
117
118 /** Used to compose unicode character classes. */
119 $rsLowerRange = 'a-z\\xdf-\\xf6\\xf8-\\xff';
120 $rsNonCharRange = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf';
121 $rsPunctuationRange = '\\x{2000}-\\x{206f}';
122 $rsSpaceRange = ' \\t\\x0b\\f\\xa0\\x{feff}\\n\\r\\x{2028}\\x{2029}\\x{1680}\\x{180e}\\x{2000}\\x{2001}\\x{2002}\\x{2003}\\x{2004}\\x{2005}\\x{2006}\\x{2007}\\x{2008}\\x{2009}\\x{200a}\\x{202f}\\x{205f}\\x{3000}';
123 $rsUpperRange = 'A-Z\\xc0-\\xd6\\xd8-\\xde';
124 $rsBreakRange = $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange;
125
126 /** Used to compose unicode capture groups. */
127 $rsBreak = '[' . $rsBreakRange . ']';
128 $rsDigits = '\\d+'; // The last lodash version in GitHub uses a single digit here and expands it when in use.
129 $rsLower = '[' . $rsLowerRange . ']';
130 $rsMisc = '[^' . $rsBreakRange . $rsDigits . $rsLowerRange . $rsUpperRange . ']';
131 $rsUpper = '[' . $rsUpperRange . ']';
132
133 /** Used to compose unicode regexes. */
134 $rsMiscLower = '(?:' . $rsLower . '|' . $rsMisc . ')';
135 $rsMiscUpper = '(?:' . $rsUpper . '|' . $rsMisc . ')';
136 $rsOrdLower = '\\d*(?:1st|2nd|3rd|(?![123])\\dth)(?=\\b|[A-Z_])';
137 $rsOrdUpper = '\\d*(?:1ST|2ND|3RD|(?![123])\\dTH)(?=\\b|[a-z_])';
138
139 $regexp = '/' . implode(
140 '|',
141 array(
142 $rsUpper . '?' . $rsLower . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper, '$' ) ) . ')',
143 $rsMiscUpper . '+' . '(?=' . implode( '|', array( $rsBreak, $rsUpper . $rsMiscLower, '$' ) ) . ')',
144 $rsUpper . '?' . $rsMiscLower . '+',
145 $rsUpper . '+',
146 $rsOrdUpper,
147 $rsOrdLower,
148 $rsDigits,
149 )
150 ) . '/u';
151
152 preg_match_all( $regexp, str_replace( "'", '', $string ), $matches );
153 return strtolower( implode( '-', $matches[0] ) );
154 //phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase
155 }
156 }
157