PluginProbe ʕ •ᴥ•ʔ
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More / 1.10.0.5
WPForms – Easy Form Builder for WordPress – Contact Forms, Payment Forms, Surveys, & More v1.10.0.5
1.10.1.1 1.10.1 1.10.0.5 trunk 1.1.4 1.1.4.2 1.1.5 1.1.5.1 1.1.6 1.1.6.1 1.1.7 1.1.7.1 1.1.7.2 1.1.8 1.1.8.1 1.1.8.2 1.1.8.3 1.1.8.4 1.10.0.1 1.10.0.2 1.10.0.3 1.10.0.4 1.2.0 1.2.0.1 1.2.1 1.2.2 1.2.2.1 1.2.2.2 1.2.3 1.2.3.1 1.2.3.2 1.2.4 1.2.4.1 1.2.5 1.2.5.1 1.2.6 1.2.7 1.2.8 1.2.8.1 1.2.9 1.3.0 1.3.1 1.3.1.1 1.3.1.2 1.3.2 1.3.3 1.3.5 1.3.6 1.3.6.1 1.3.6.2 1.3.7.2 1.3.7.3 1.3.7.4 1.3.8 1.3.9.1 1.4.0.1 1.4.1.1 1.4.2 1.4.2.1 1.4.2.2 1.4.3 1.4.4 1.4.4.1 1.4.5 1.4.5.1 1.4.5.2 1.4.5.3 1.4.6 1.4.7.1 1.4.7.2 1.4.8.1 1.4.9 1.5.0.1 1.5.0.3 1.5.0.4 1.5.1 1.5.1.1 1.5.1.3 1.5.2.1 1.5.2.2 1.5.2.3 1.5.3 1.5.3.1 1.5.4.1 1.5.4.2 1.5.5 1.5.5.1 1.5.6 1.5.6.2 1.5.7 1.5.8.2 1.5.9.1 1.5.9.4 1.5.9.5 1.6.0.1 1.6.0.2 1.6.1 1.6.2.2 1.6.2.3 1.6.3.1 1.6.4 1.6.4.1 1.6.5 1.6.6 1.6.7 1.6.7.1 1.6.7.2 1.6.7.3 1.6.8 1.6.8.1 1.6.9 1.7.0 1.7.1.1 1.7.1.2 1.7.2 1.7.2.1 1.7.3 1.7.4 1.7.4.1 1.7.4.2 1.7.5.1 1.7.5.2 1.7.5.3 1.7.5.5 1.7.6 1.7.7 1.7.7.1 1.7.7.2 1.7.8 1.7.9 1.7.9.1 1.8.0.1 1.8.0.2 1.8.1.1 1.8.1.2 1.8.1.3 1.8.2.1 1.8.2.2 1.8.2.3 1.8.3 1.8.3.1 1.8.4 1.8.4.1 1.8.5.2 1.8.5.3 1.8.5.4 1.8.6.2 1.8.6.3 1.8.6.4 1.8.7.2 1.8.8.2 1.8.8.3 1.8.9.1 1.8.9.2 1.8.9.4 1.8.9.5 1.8.9.6 1.9.0.1 1.9.0.2 1.9.0.3 1.9.0.4 1.9.1.1 1.9.1.2 1.9.1.3 1.9.1.4 1.9.1.5 1.9.1.6 1.9.2.1 1.9.2.2 1.9.2.3 1.9.3.1 1.9.3.2 1.9.4.1 1.9.4.2 1.9.5 1.9.5.1 1.9.5.2 1.9.6 1.9.6.1 1.9.6.2 1.9.7.1 1.9.7.2 1.9.7.3 1.9.8.1 1.9.8.2 1.9.8.4 1.9.8.7 1.9.9.2 1.9.9.3 1.9.9.4
wpforms-lite / includes / functions / list.php
wpforms-lite / includes / functions Last commit date
access.php 8 months ago builder.php 11 months ago checks.php 2 months ago colors.php 2 years ago data-presets.php 6 months ago date-time.php 11 months ago debug.php 9 months ago education.php 11 months ago escape-sanitize.php 6 months ago filesystem-media.php 1 year ago form-fields.php 2 months ago forms.php 2 months ago list.php 1 year ago payments.php 10 months ago plugins.php 11 months ago privacy.php 1 year ago providers.php 11 months ago utilities.php 4 months ago
list.php
325 lines
1 <?php
2 /**
3 * Helper functions to work with multidimensional arrays easier.
4 *
5 * @since 1.5.6
6 */
7
8 /**
9 * Determine whether the given value is array accessible.
10 *
11 * @since 1.5.6
12 *
13 * @param mixed $value Checkin to accessible.
14 *
15 * @return bool
16 */
17 function wpforms_list_accessible( $value ) {
18
19 return is_array( $value ) || $value instanceof ArrayAccess;
20 }
21
22 /**
23 * Set an array item to a given value using "dot" notation.
24 *
25 * If no key is given to the method, the entire array will be replaced.
26 *
27 * @since 1.5.6
28 *
29 * @param array $array Existing array.
30 * @param string $key Path to set.
31 * @param mixed $value Value to set.
32 * @param string $separator Separator.
33 *
34 * @return array New array.
35 */
36 function wpforms_list_set( $array, $key, $value, $separator = '.' ) {
37
38 if ( ! wpforms_list_accessible( $array ) ) {
39 return $value;
40 }
41
42 if ( $key === null ) {
43 $array = $value;
44
45 return $array;
46 }
47
48 $keys = explode( $separator, $key );
49 $count_keys = count( $keys );
50 $values = array_values( $keys );
51 $last_key = $values[ $count_keys - 1 ];
52 $tmp_array = &$array;
53
54 for ( $i = 0; $i < $count_keys - 1; $i ++ ) {
55 $k = $keys[ $i ];
56 $tmp_array = &$tmp_array[ $k ];
57 }
58
59 $tmp_array[ $last_key ] = $value;
60
61 return $array;
62 }
63
64 /**
65 * Determine if the given key exists in the provided array.
66 *
67 * @since 1.5.6
68 *
69 * @param ArrayAccess|array $array Existing array.
70 * @param string|int $key To check.
71 *
72 * @return bool
73 */
74 function wpforms_list_exists( $array, $key ) {
75
76 if ( ! wpforms_list_accessible( $array ) ) {
77 return false;
78 }
79
80 if ( $array instanceof ArrayAccess ) {
81 return $array->offsetExists( $key );
82 }
83
84 return array_key_exists( $key, $array );
85 }
86
87 /**
88 * Get an item from an array using "dot" notation.
89 *
90 * @since 1.5.6
91 *
92 * @param ArrayAccess|array $array Where we want to get.
93 * @param string $key Key with dot's.
94 * @param mixed $default Value.
95 *
96 * @return mixed
97 */
98 function wpforms_list_get( $array, $key, $default = null ) {
99
100 if ( ! wpforms_list_accessible( $array ) ) {
101 return $default;
102 }
103
104 if ( $key === null ) {
105 return $array;
106 }
107
108 if ( ! is_string( $key ) ) {
109 return $default;
110 }
111
112 if ( wpforms_list_exists( $array, $key ) ) {
113 return $array[ $key ];
114 }
115
116 foreach ( explode( '.', $key ) as $segment ) {
117 if ( wpforms_list_accessible( $array ) && wpforms_list_exists( $array, $segment ) ) {
118 $array = $array[ $segment ];
119 } else {
120 return $default;
121 }
122 }
123
124 return $array;
125 }
126
127 /**
128 * Check if an item exists in an array using "dot" notation.
129 *
130 * @since 1.5.6
131 *
132 * @param ArrayAccess|array $array To check.
133 * @param string $key Keys with dot's.
134 *
135 * @return bool
136 */
137 function wpforms_list_has( $array, $key ) {
138
139 if ( ! $array ) {
140 return false;
141 }
142
143 if ( $key === null || ! is_string( $key ) ) {
144 return false;
145 }
146
147 if ( wpforms_list_exists( $array, $key ) ) {
148 return true;
149 }
150
151 foreach ( explode( '.', $key ) as $segment ) {
152 if ( wpforms_list_accessible( $array ) && wpforms_list_exists( $array, $segment ) ) {
153 $array = $array[ $segment ];
154 } else {
155 return false;
156 }
157 }
158
159 return true;
160 }
161
162 /**
163 * Determine if an array is associative.
164 *
165 * An array is "associative" if it doesn't have sequential numerical keys beginning with zero.
166 *
167 * @since 1.5.6
168 *
169 * @param array $array To check.
170 *
171 * @return bool
172 */
173 function wpforms_list_is_assoc( $array ) {
174
175 $keys = array_keys( $array );
176
177 return array_keys( $keys ) !== $keys;
178 }
179
180 /**
181 * Get a subset of the items from the given array.
182 *
183 * @since 1.5.6
184 *
185 * @param array $array To get.
186 * @param array|string $keys To filter.
187 *
188 * @return array
189 */
190 function wpforms_list_only( $array, $keys ) {
191
192 return array_intersect_key( $array, array_flip( (array) $keys ) );
193 }
194
195 /**
196 * Remove one or many array items from a given array using "dot" notation.
197 *
198 * @since 1.5.6
199 *
200 * @param array $array To forget.
201 * @param array|string $keys To exclude.
202 *
203 * @return array
204 */
205 function wpforms_list_forget( $array, $keys ) {
206
207 $tmp_array = &$array;
208 $keys = (array) $keys;
209
210 if ( count( $keys ) === 0 ) {
211 return $array;
212 }
213
214 foreach ( $keys as $key ) {
215 // if the exact key exists in the top-level, remove it.
216 if ( wpforms_list_exists( $array, $key ) ) {
217 unset( $array[ $key ] );
218 continue;
219 }
220
221 $parts = explode( '.', $key );
222 $count_keys = count( $parts );
223 $values = array_values( $parts );
224 $last_key = $values[ $count_keys - 1 ];
225
226 for ( $i = 0; $i < $count_keys - 1; $i ++ ) {
227 $k = $parts[ $i ];
228 $tmp_array = &$tmp_array[ $k ];
229 }
230
231 unset( $tmp_array[ $last_key ] );
232 }
233
234 return $array;
235 }
236
237 /**
238 * Insert a value or key/value pair after a specific key in an array.
239 * If key doesn't exist, value is appended to the end of the array.
240 *
241 * @since 1.5.8
242 *
243 * @param array $target Array where to insert.
244 * @param string $key Insert after key.
245 * @param array $data Array to insert.
246 *
247 * @return array
248 */
249 function wpforms_list_insert_after( array $target, string $key, array $data ): array {
250
251 return wpforms_list_insert( $target, $key, $data, 'after' );
252 }
253
254 /**
255 * Insert a value or key/value pair before a specific key in an array.
256 * If key doesn't exist, value is prepended to the beginning of the array.
257 *
258 * @since 1.8.9
259 *
260 * @param array $target Array where to insert.
261 * @param string $key Insert before key.
262 * @param array $data Array to insert.
263 *
264 * @return array
265 */
266 function wpforms_list_insert_before( array $target, string $key, array $data ): array {
267
268 return wpforms_list_insert( $target, $key, $data, 'before' );
269 }
270
271 /**
272 * Insert a value or key/value pair before or after a specific key in an array.
273 * If key doesn't exist, value is appended to the end of the array.
274 *
275 * @since 1.8.9
276 *
277 * @param array $target Array where to insert.
278 * @param string $key Insert before/after key.
279 * @param array $data Array to insert.
280 * @param string $position Position to insert before/after.
281 *
282 * @return array
283 */
284 function wpforms_list_insert( array $target, string $key, array $data, string $position ): array {
285
286 $position = strtolower( $position );
287
288 $keys = array_keys( $target );
289 $index = array_search( $key, $keys, true );
290 $pos = 0;
291
292 if ( $position === 'before' ) {
293 $pos = $index === false ? 0 : $index;
294 } elseif ( $position === 'after' ) {
295 $pos = $index === false ? count( $target ) : $index + 1;
296 }
297
298 return array_merge( array_slice( $target, 0, $pos ), $data, array_slice( $target, $pos ) );
299 }
300
301 /**
302 * Cleanup $items array recursively removing from it all keys not existing in the $default array.
303 *
304 * @since 1.7.2
305 *
306 * @param array $items Items.
307 * @param array $default Default items.
308 *
309 * @return array
310 */
311 function wpforms_list_intersect_key( $items, $default ) {
312
313 if ( ! is_array( $items ) ) {
314 return $items;
315 }
316
317 $items = array_intersect_key( $items, $default );
318
319 foreach ( $items as $key => &$item ) {
320 $item = wpforms_list_intersect_key( $item, $default[ $key ] );
321 }
322
323 return $items;
324 }
325