Skip to content

Commit c0084bc

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@50958 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d28766f commit c0084bc

File tree

2 files changed

+189
-0
lines changed

2 files changed

+189
-0
lines changed

src/wp-includes/functions.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4626,6 +4626,65 @@ function _wp_array_get( $array, $path, $default = null ) {
46264626
return $array;
46274627
}
46284628

4629+
/**
4630+
* Sets an array in depth based on a path of keys.
4631+
*
4632+
* It is the PHP equivalent of JavaScript's `lodash.set()` and mirroring it may help other components
4633+
* retain some symmetry between client and server implementations.
4634+
*
4635+
* Example usage:
4636+
*
4637+
* $array = array();
4638+
* _wp_array_set( $array, array( 'a', 'b', 'c', 1 );
4639+
* $array becomes:
4640+
* array(
4641+
* 'a' => array(
4642+
* 'b' => array(
4643+
* 'c' => 1,
4644+
* ),
4645+
* ),
4646+
* );
4647+
*
4648+
* @param array $array An array that we want to mutate to include a specific value in a path.
4649+
* @param array $path An array of keys describing the path that we want to mutate.
4650+
* @param mixed $value The value that will be set.
4651+
*/
4652+
function _wp_array_set( &$array, $path, $value = null ) {
4653+
// Confirm $array is valid.
4654+
if ( ! is_array( $array ) ) {
4655+
return;
4656+
}
4657+
4658+
// Confirm $path is valid.
4659+
if ( ! is_array( $path ) ) {
4660+
return;
4661+
}
4662+
$path_length = count( $path );
4663+
if ( 0 === $path_length ) {
4664+
return;
4665+
}
4666+
foreach ( $path as $path_element ) {
4667+
if (
4668+
! is_string( $path_element ) && ! is_integer( $path_element ) &&
4669+
! is_null( $path_element )
4670+
) {
4671+
return;
4672+
}
4673+
}
4674+
4675+
for ( $i = 0; $i < $path_length - 1; ++$i ) {
4676+
$path_element = $path[ $i ];
4677+
if (
4678+
! array_key_exists( $path_element, $array ) ||
4679+
! is_array( $array[ $path_element ] )
4680+
) {
4681+
$array[ $path_element ] = array();
4682+
}
4683+
$array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration
4684+
}
4685+
$array[ $path[ $i ] ] = $value;
4686+
}
4687+
46294688
/**
46304689
* Determines if the variable is a numeric-indexed array.
46314690
*
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
/**
3+
* _wp_array_set.
4+
*
5+
* @package WordPress
6+
*/
7+
8+
/**
9+
* Test _wp_array_set function.
10+
*
11+
* @package WordPress
12+
*/
13+
class WP_Array_Set_Test extends WP_UnitTestCase {
14+
/**
15+
* Test _wp_array_set() with simple non subtree path.
16+
*/
17+
public function test_simple_not_subtree_set() {
18+
$test_array = array();
19+
_wp_array_set( $test_array, array( 'a' ), 1 );
20+
$this->assertSame(
21+
$test_array,
22+
array( 'a' => 1 )
23+
);
24+
25+
$test_array = array( 'a' => 2 );
26+
_wp_array_set( $test_array, array( 'a' ), 3 );
27+
$this->assertSame(
28+
$test_array,
29+
array( 'a' => 3 )
30+
);
31+
32+
$test_array = array( 'b' => 1 );
33+
_wp_array_set( $test_array, array( 'a' ), 3 );
34+
$this->assertSame(
35+
$test_array,
36+
array(
37+
'b' => 1,
38+
'a' => 3,
39+
)
40+
);
41+
}
42+
43+
/**
44+
* Test _wp_array_set() with subtree paths.
45+
*/
46+
public function test_subtree_set() {
47+
$test_array = array();
48+
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
49+
$this->assertSame(
50+
$test_array,
51+
array( 'a' => array( 'b' => array( 'c' => 1 ) ) )
52+
);
53+
54+
$test_array = array( 'b' => 3 );
55+
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
56+
$this->assertSame(
57+
$test_array,
58+
array(
59+
'b' => 3,
60+
'a' => array( 'b' => array( 'c' => 1 ) ),
61+
)
62+
);
63+
64+
$test_array = array(
65+
'b' => 3,
66+
'a' => 1,
67+
);
68+
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
69+
$this->assertSame(
70+
$test_array,
71+
array(
72+
'b' => 3,
73+
'a' => array( 'b' => array( 'c' => 1 ) ),
74+
)
75+
);
76+
77+
$test_array = array(
78+
'b' => 3,
79+
'a' => array(),
80+
);
81+
_wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 );
82+
$this->assertSame(
83+
$test_array,
84+
array(
85+
'b' => 3,
86+
'a' => array( 'b' => array( 'c' => 1 ) ),
87+
)
88+
);
89+
}
90+
91+
/**
92+
* Test _wp_array_set() with invalid parameters.
93+
*/
94+
public function test_invalid_parameters_set() {
95+
$test = 3;
96+
_wp_array_set( $test, array( 'a' ), 1 );
97+
$this->assertSame(
98+
$test,
99+
3
100+
);
101+
102+
$test_array = array( 'a' => 2 );
103+
_wp_array_set( $test_array, 'a', 3 );
104+
$this->assertSame(
105+
$test_array,
106+
array( 'a' => 2 )
107+
);
108+
109+
$test_array = array( 'a' => 2 );
110+
_wp_array_set( $test_array, null, 3 );
111+
$this->assertSame(
112+
$test_array,
113+
array( 'a' => 2 )
114+
);
115+
116+
$test_array = array( 'a' => 2 );
117+
_wp_array_set( $test_array, array(), 3 );
118+
$this->assertSame(
119+
$test_array,
120+
array( 'a' => 2 )
121+
);
122+
123+
$test_array = array( 'a' => 2 );
124+
_wp_array_set( $test_array, array( 'a', array() ), 3 );
125+
$this->assertSame(
126+
$test_array,
127+
array( 'a' => 2 )
128+
);
129+
}
130+
}

0 commit comments

Comments
 (0)