General: Add _wp_array_set function.
This adds the _wp_array_set function, which is the counterpart of the existing _wp_array_get. This utility is to be used by the Global Settings work. Props nosolosw, jorgefilipecosta. See #53175. Built from https://develop.svn.wordpress.org/trunk@50958 git-svn-id: http://core.svn.wordpress.org/trunk@50567 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
3989828e0b
commit
6c0578055c
|
@ -4626,6 +4626,65 @@ function _wp_array_get( $array, $path, $default = null ) {
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets an array in depth based on a path of keys.
|
||||||
|
*
|
||||||
|
* It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
|
||||||
|
* retain some symmetry between client and server implementations.
|
||||||
|
*
|
||||||
|
* Example usage:
|
||||||
|
*
|
||||||
|
* $array = array();
|
||||||
|
* _wp_array_set( $array, array( 'a', 'b', 'c', 1 );
|
||||||
|
* $array becomes:
|
||||||
|
* array(
|
||||||
|
* 'a' => array(
|
||||||
|
* 'b' => array(
|
||||||
|
* 'c' => 1,
|
||||||
|
* ),
|
||||||
|
* ),
|
||||||
|
* );
|
||||||
|
*
|
||||||
|
* @param array $array An array that we want to mutate to include a specific value in a path.
|
||||||
|
* @param array $path An array of keys describing the path that we want to mutate.
|
||||||
|
* @param mixed $value The value that will be set.
|
||||||
|
*/
|
||||||
|
function _wp_array_set( &$array, $path, $value = null ) {
|
||||||
|
// Confirm $array is valid.
|
||||||
|
if ( ! is_array( $array ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Confirm $path is valid.
|
||||||
|
if ( ! is_array( $path ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$path_length = count( $path );
|
||||||
|
if ( 0 === $path_length ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach ( $path as $path_element ) {
|
||||||
|
if (
|
||||||
|
! is_string( $path_element ) && ! is_integer( $path_element ) &&
|
||||||
|
! is_null( $path_element )
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for ( $i = 0; $i < $path_length - 1; ++$i ) {
|
||||||
|
$path_element = $path[ $i ];
|
||||||
|
if (
|
||||||
|
! array_key_exists( $path_element, $array ) ||
|
||||||
|
! is_array( $array[ $path_element ] )
|
||||||
|
) {
|
||||||
|
$array[ $path_element ] = array();
|
||||||
|
}
|
||||||
|
$array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
|
||||||
|
}
|
||||||
|
$array[ $path[ $i ] ] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determines if the variable is a numeric-indexed array.
|
* Determines if the variable is a numeric-indexed array.
|
||||||
*
|
*
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.8-alpha-50957';
|
$wp_version = '5.8-alpha-50958';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue