@@ -81,121 +81,6 @@ function wp_load_filesystem(
81
81
}
82
82
endif ;
83
83
84
- if ( ! function_exists ( 'xwp_array_flatmap ' ) ) :
85
- /**
86
- * Flattens and maps an array.
87
- *
88
- * @template T The type of the elements in the input array.
89
- * @template R The type of the elements in the returned array.
90
- *
91
- * @param callable(T): R $callback Function to apply to each element.
92
- * @param array<array-key, T> $input_array Array to flatten and map.
93
- *
94
- * @return array<array-key, R>
95
- */
96
- function xwp_array_flatmap ( callable $ callback , array $ input_array ) {
97
- return f \Array_Extra::flatmap ( $ callback , $ input_array );
98
- }
99
- endif ;
100
-
101
- if ( ! function_exists ( 'wp_array_flatmap ' ) ) :
102
- /**
103
- * Flattens and maps an array.
104
- *
105
- * @template T The type of the elements in the input array.
106
- * @template R The type of the elements in the returned array.
107
- *
108
- * @param array<array-key, T>|callable(T): R $callback Function to apply to each element.
109
- * @param array<array-key, T>|callable(T): R $input_array Array to flatten and map.
110
- *
111
- * @return array<array-key, R>
112
- */
113
- function wp_array_flatmap ( callable |array $ callback , array |callable $ input_array ) {
114
- return is_array ( $ input_array )
115
- ? xwp_array_flatmap ( $ callback , $ input_array )
116
- : xwp_array_flatmap ( $ input_array , $ callback );
117
- }
118
- endif ;
119
-
120
- if ( ! function_exists ( 'wp_array_flatmap_assoc ' ) ) :
121
- /**
122
- * Flatten and map an associative array of arrays.
123
- *
124
- * @template R
125
- * @template T — Applies the callback to the elements of the given arrays
126
- *
127
- * @param callable(T): R $callback Callback function to run for each element in each array.
128
- * @param array<string, <array<T>> $input The input array.
129
- * @param key-of<T> $key Key whose value will be used as the key for the returned array.
130
- * @param bool $unkey Optional. Whether to remove the key from the returned array. Default true.
131
- *
132
- * @return array<value-of<key-of<T>>, R> An array containing all the elements of arr1 after applying the callback function to each one.
133
- */
134
- function wp_array_flatmap_assoc ( callable $ callback , array $ input , string $ key , bool $ unkey = true ) {
135
- return f \Array_Extra::flatmap_assoc ( $ callback , $ input , $ key , $ unkey );
136
- }
137
- endif ;
138
-
139
- if ( ! function_exists ( 'wp_array_diff_assoc ' ) ) :
140
- /**
141
- * Legacy function to extract a slice of an array not including the specified keys.
142
- *
143
- * @param array $input_array Input array.
144
- * @param array $keys Keys to exclude.
145
- */
146
- function wp_array_diff_assoc ( array $ input_array , array $ keys ) {
147
- return xwp_array_diff_assoc ( $ input_array , ...$ keys );
148
- }
149
- endif ;
150
-
151
- if ( ! function_exists ( 'xwp_array_diff_assoc ' ) ) :
152
- /**
153
- * Extracts a slice of array not including the specified keys.
154
- *
155
- * @template T The type of the elements in the input array.
156
- *
157
- * @param array<string, T> $input_array Input array.
158
- * @param array<string>|string ...$keys Keys to exclude.
159
- * @return array<string, T> Array with the keys removed.
160
- */
161
- function xwp_array_diff_assoc ( array $ input_array , string ...$ keys ) {
162
- if ( is_array ( $ keys [0 ] ) ) {
163
- $ keys = $ keys [0 ];
164
- }
165
-
166
- return f \Array_Extra::diff_assoc ( $ input_array , $ keys );
167
- }
168
-
169
- endif ;
170
-
171
- if ( ! function_exists ( 'wp_array_rekey ' ) ) :
172
- /**
173
- * Rekey an array of arrays by a specific key.
174
- *
175
- * @param array<string, array<string, mixed>> $arr The input array.
176
- * @param string $key The key to rekey by.
177
- * @return array<string, array<string, mixed>> The rekeyed array.
178
- */
179
- function wp_array_rekey ( array $ arr , string $ key ): array {
180
- return f \Array_Extra::rekey ( $ arr , $ key );
181
- }
182
- endif ;
183
-
184
- if ( ! function_exists ( 'xwp_array_slice_assoc ' ) ) :
185
- /**
186
- * Extracts a slice of an array.
187
- *
188
- * @template T The type of the elements in the input array.
189
- *
190
- * @param array<string, T> $input_array Input array.
191
- * @param string ...$keys Keys to include.
192
- * @return array<string, T> Array with only the keys specified.
193
- */
194
- function xwp_array_slice_assoc ( array $ input_array , string ...$ keys ) {
195
- return f \Array_Extra::slice_assoc ( $ input_array , $ keys );
196
- }
197
- endif ;
198
-
199
84
if ( ! function_exists ( 'xwp_deregister_blocks ' ) ) :
200
85
/**
201
86
* Deregister all blocks.
@@ -273,3 +158,40 @@ function xwp_format_term_name( WP_Term|int|string|null|array|\WP_Error $term, ar
273
158
return f \Term::format_hierarhical_name ( $ term , $ args );
274
159
}
275
160
endif ;
161
+
162
+
163
+ if ( ! function_exists ( 'xwp_str_to_bool ' ) ) :
164
+ /**
165
+ * Convert a string to a boolean.
166
+ *
167
+ * @param string|bool|null $str The string to convert.
168
+ * @return bool
169
+ */
170
+ function xwp_str_to_bool ( string |bool |null $ str = '' ): bool {
171
+ if ( is_bool ( $ str ) ) {
172
+ return $ str ;
173
+ }
174
+
175
+ if ( xwp_is_int_str ( $ str ) ) {
176
+ return intval ( $ str ) > 0 ;
177
+ }
178
+
179
+ return match ( strtolower ( $ str ) ) {
180
+ 'yes ' , 'true ' , 'on ' => true ,
181
+ 'no ' , 'false ' , 'off ' => false ,
182
+ default => false ,
183
+ };
184
+ }
185
+ endif ;
186
+
187
+ if ( ! function_exists ( 'xwp_bool_to_str ' ) ) :
188
+ /**
189
+ * Convert a boolean to a string.
190
+ *
191
+ * @param bool $boolean The boolean to convert.
192
+ * @return 'yes'|'no'
193
+ */
194
+ function xwp_bool_to_str ( bool |string $ boolean ): string {
195
+ return xwp_str_to_bool ( $ boolean ) ? 'yes ' : 'no ' ;
196
+ }
197
+ endif ;
0 commit comments