PluginProbe ʕ •ᴥ•ʔ
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin / 4.9.0
WP Mail SMTP by WPForms – The Most Popular SMTP and Email Log Plugin v4.9.0
4.9.0 0.9.6 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.6.0 1.6.2 1.7.0 1.7.1 1.8.0 1.8.1 1.9.0 2.0.0 2.0.1 2.1.1 2.2.1 2.3.1 2.4.0 2.5.0 2.5.1 2.6.0 2.7.0 2.8.0 2.9.0 3.0.1 3.0.2 3.0.3 3.1.0 3.10.0 3.11.0 3.11.1 3.2.0 3.2.1 3.3.0 3.4.0 3.5.0 3.5.1 3.5.2 3.6.1 3.7.0 3.8.0 3.8.2 3.9.0 4.0.1 4.1.0 4.1.1 4.2.0 4.3.0 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.8.0 trunk 0.10.0 0.10.1 0.11.1 0.11.2 0.3.1 0.3.2 0.4 0.4.1 0.4.2 0.5.0 0.5.1 0.5.2 0.6 0.7 0.8 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.9.0 0.9.1 0.9.2 0.9.3 0.9.4 0.9.5
wp-mail-smtp / src / Helpers / Data.php
wp-mail-smtp / src / Helpers Last commit date
Crypto.php 5 days ago DB.php 5 days ago Data.php 5 days ago Helpers.php 5 days ago PluginImportDataRetriever.php 5 days ago UI.php 5 days ago
Data.php
173 lines
1 <?php
2
3 namespace WPMailSMTP\Helpers;
4
5 use ArrayAccess;
6
7 /**
8 * Class Data.
9 *
10 * Helper class that allows to get/set value in the nested array by string key.
11 *
12 * @since 4.9.0
13 */
14 class Data {
15
16 /**
17 * Keys string separator.
18 *
19 * @since 4.9.0
20 *
21 * @var string
22 */
23 const KEY_SEPARATOR = '.';
24
25 /**
26 * Get nested array value by string key.
27 *
28 * @since 4.9.0
29 *
30 * @param array $array Input array.
31 * @param string $str_key String key. E.g. "level1.level2.level3".
32 * @param mixed $default The default value that should be returned if value by key not found.
33 *
34 * @return mixed
35 */
36 public static function get( $array, $str_key, $default = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound, Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound
37
38 if ( is_array( $array ) && array_key_exists( $str_key, $array ) ) {
39 return $array[ $str_key ];
40 }
41
42 $keys = self::get_keys_array( $str_key );
43
44 foreach ( $keys as $key ) {
45 if ( ! is_array( $array ) && ! $array instanceof ArrayAccess ) {
46 return $default;
47 }
48
49 if (
50 ( $array instanceof ArrayAccess && $array->offsetExists( $key ) ) ||
51 array_key_exists( $key, $array )
52 ) {
53 $array = $array[ $key ];
54 } else {
55 return $default;
56 }
57 }
58
59 return $array;
60 }
61
62 /**
63 * Get keys array from keys string.
64 *
65 * @since 4.9.0
66 *
67 * @param string $keys String key. E.g. "level1.level2.level3".
68 *
69 * @return array
70 */
71 private static function get_keys_array( $keys ) {
72
73 return explode( self::KEY_SEPARATOR, $keys );
74 }
75
76 /**
77 * Set value in the nested array by string key (by reference).
78 *
79 * @since 4.9.0
80 *
81 * @param array &$array Input array (passed by reference).
82 * @param string $str_key String key. E.g. "level1.level2.level3".
83 * @param mixed $value Value that should be added to array.
84 *
85 * @return void
86 */
87 public static function set( array &$array, $str_key, $value ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
88
89 // Fast path for non-nested key.
90 if ( strpos( $str_key, self::KEY_SEPARATOR ) === false ) {
91 $array[ $str_key ] = $value;
92
93 return;
94 }
95
96 $keys = self::get_keys_array( $str_key );
97 $tmp = &$array;
98
99 $keys_count = count( $keys );
100
101 while ( $keys_count > 0 ) {
102 $key = array_shift( $keys );
103
104 --$keys_count;
105
106 if ( ! is_array( $tmp ) ) {
107 $tmp = [];
108 }
109
110 $tmp = &$tmp[ $key ];
111 }
112
113 $tmp = $value;
114 }
115
116 /**
117 * Find a value in an array.
118 *
119 * @since 4.9.0
120 *
121 * @param array $array The array to search in.
122 * @param string $key The key to search for.
123 * @param mixed $value The value to search for.
124 * @param string|false $desired_key The key to return from the found item, or false to return the whole item.
125 * @param mixed $default The default value to return if not found.
126 *
127 * @return mixed
128 */
129 public static function find( $array, $key, $value, $desired_key = false, $default = '' ) { // phpcs:ignore Generic.Metrics.NestingLevel.MaxExceeded, Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound, Universal.NamingConventions.NoReservedKeywordParameterNames.defaultFound
130
131 foreach ( $array as $item ) {
132 if ( isset( $item[ $key ] ) && $item[ $key ] === $value ) {
133 if ( $desired_key !== false ) {
134 if ( isset( $item[ $desired_key ] ) ) {
135 return $item[ $desired_key ];
136 }
137 break;
138 }
139
140 return $item;
141 }
142 }
143
144 return $default;
145 }
146
147 /**
148 * Walk through array recursively.
149 *
150 * @since 4.9.0
151 *
152 * @param array $array Array to walk through.
153 * @param callable $callback Callback function that will be executed for each scalar array item.
154 * @param string $parent_key Parent key. Used for recursive calls.
155 */
156 public static function walk_recursive( &$array, $callback, $parent_key = '' ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
157
158 foreach ( $array as $key => &$value ) {
159 if ( ! is_numeric( $key ) ) {
160 $current_key = $parent_key ? $parent_key . '.' . $key : $key;
161 } else {
162 $current_key = $parent_key;
163 }
164
165 if ( is_array( $value ) ) {
166 self::walk_recursive( $value, $callback, $current_key );
167 } else {
168 call_user_func_array( $callback, [ &$value, $current_key ] );
169 }
170 }
171 }
172 }
173