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