-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxwp-helper-fns.php
251 lines (221 loc) · 8.25 KB
/
xwp-helper-fns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* Helper functions definition file
*
* @package eXtended WordPress
* @subpackage Helper\Functions
*/
use XWP\Helper\Functions as f;
if ( ! function_exists( 'xwp_parse_args' ) ) :
/**
* Same as `wp_parse_args` but recursive.
*
* @template TArgs of array
* @template TDefs of array
*
* @param string|TArgs|object $args Arguments to parse.
* @param TDefs $defaults Optional. Default values. Default empty array.
* @return (TArgs is array ? TArgs&TDefs : TDefs)
*/
function xwp_parse_args( string|array|object $args, array $defaults = array() ): array {
match ( true ) {
is_object( $args ) => $parsed = get_object_vars( $args ),
is_array( $args ) => $parsed = &$args,
default => wp_parse_str( $args, $parsed ),
};
$result = $defaults;
foreach ( $parsed as $k => $v ) {
$result[ $k ] = isset( $result[ $k ] ) && is_array( $v ) && is_array( $result[ $k ] )
? xwp_parse_args( $v, $result[ $k ] )
: $v;
}
return $result;
}
endif;
if ( ! function_exists( 'xwp_wpfs' ) ) :
/**
* Loads the WordPress filesystem
*
* @template TFS of \WP_Filesystem_Base
*
* @param class-string<TFS> $method Optional. Filesystem method classname. Default null.
* @param array|false $args Optional. Connection args, These are passed directly to the WP_Filesystem_*() classes. Default false.
* @param string|false $context Optional. Context for get_filesystem_method(). Default false.
* @return TFS|false|null
*/
function xwp_wpfs(
string $method = WP_Filesystem_Direct::class,
array|bool $args = false,
string|bool $context = false,
): WP_Filesystem_Base|bool|null {
//phpcs:ignore Universal.Operators.DisallowShortTernary.Found
$args = array_filter( $args ?: array( 'method' => $method ) );
return f\WPFS::load( $args, $context );
}
endif;
if ( ! function_exists( 'wp_load_filesystem' ) ) :
/**
* Loads the WordPress filesystem
*
* @template TFS of \WP_Filesystem_Base
*
* @param array{method?: class-string<TFS>}|array<string,mixed>|false $args Optional. Connection args, These are passed directly to the WP_Filesystem_*() classes. Default false.
* @param string|false $context Optional. Context for get_filesystem_method(). Default false.
*
* @return \WP_Filesystem_Base|false|null
*
* @deprecated 1.10.0 Use xwp_wpfs instead.
*/
function wp_load_filesystem(
array|bool $args = false,
string|bool $context = false,
): WP_Filesystem_Base|bool|null {
return xwp_wpfs( args: $args, context: $context );
}
endif;
if ( ! function_exists( 'xwp_deregister_blocks' ) ) :
/**
* Deregister all blocks.
*
* @return array<string> The names of the blocks that were deregistered.
*/
function xwp_deregister_blocks(): array {
return f\Block::deregister_all();
}
endif;
if ( ! function_exists( 'xwp_remove_hook_callbacks' ) ) :
/**
* Remove callbacks for a given classname.
*
* @param class-string $classname The name of the class to remove callbacks for.
* @param string|false $target_hook Optional. Hook tag to remove callbacks for.
* @param string|false $method Optional. The name of the method to remove callbacks for.
* @param int|false $priority Optional. The priority of the hook to remove callbacks for.
*
* @return array<string, array> The names of the callbacks that were removed.
*/
function xwp_remove_hook_callbacks(
string $classname,
string|bool $target_hook = false,
string|bool $method = false,
int|bool $priority = false,
): array {
return f\Hook_Remover::remove_callbacks( $classname, $target_hook, $method, $priority );
}
endif;
if ( ! function_exists( 'xwp_clean' ) ) :
/**
* Clean variables using sanitize_text_field. Arrays are cleaned recursively.
* Non-scalar values are ignored.
*
* @param string|array $input Data to sanitize.
* @return string|array
*/
function xwp_clean( $input ) {
return f\Request::clean( $input );
}
endif;
if ( ! function_exists( 'xwp_uclean' ) ) :
/**
* Unslash then clean variables using sanitize_text_field. Arrays are cleaned recursively.
* Non-scalar values are ignored.
*
* @param string|array $input Data to sanitize.
* @return string|array
*/
function xwp_uclean( $input ) {
return f\Request::uclean( $input );
}
endif;
if ( ! function_exists( 'xwp_format_term_name' ) ) :
/**
* Format a term name with term parents.
*
* @param WP_Term|int|string|null|array|\WP_Error $term WP_Term object, Term ID, Term slug, or Term name.
* @param array<string, mixed> $args Formatting arguments. Default empty array.
* - formatter (callable) Custom formatter for the displayed term name. Default null.
* - count (bool) Whether to include the term count in the formatted name. Default false.
* - link_format (string|callable|array|bool) URL Link format for the term link. Default false.
* - link_items (bool) Whether to link the term items. Default false.
* - link_final (bool) Whether to link the final term. Default true.
* - separator (string) Separator between terms. Default ' > '.
* - taxonomy (string) Taxonomy name. Default null. Mandatory if $term is a string. Optional otherwise.
* @return string Formatted term name with ancestors.
*/
function xwp_format_term_name( WP_Term|int|string|null|array|\WP_Error $term, array $args = array() ): string {
return f\Term::format_hierarhical_name( $term, $args );
}
endif;
if ( ! function_exists( 'xwp_str_to_bool' ) ) :
/**
* Convert a string to a boolean.
*
* @param string|bool|null $str The string to convert.
* @return bool
*/
function xwp_str_to_bool( string|bool|null $str = '' ): bool {
if ( is_bool( $str ) ) {
return $str;
}
if ( xwp_is_int_str( $str ) ) {
return intval( $str ) > 0;
}
return match ( strtolower( $str ) ) {
'yes', 'true', 'on' => true,
'no', 'false', 'off' => false,
default => false,
};
}
endif;
if ( ! function_exists( 'xwp_bool_to_str' ) ) :
/**
* Convert a boolean to a string.
*
* @param bool|string $boolean The boolean to convert.
* @return 'yes'|'no'
*/
function xwp_bool_to_str( bool|string $boolean ): string {
return xwp_str_to_bool( $boolean ) ? 'yes' : 'no';
}
endif;
if ( ! function_exists( 'xwp_get_template' ) ) :
/**
* Get a template passing variables and including the file.
*
* @param string $template The template file.
* @param null|array<string,mixed> $params Optional. The variables to pass to the template file.
*
* @since 1.18.0
*/
function xwp_get_template( string $template, ?array $params = null ): void {
if ( ! file_exists( $template ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf( 'The template file %s does not exist.', esc_html( basename( $template ) ), ),
'1.0.0',
);
return;
}
if ( is_array( $params ) && $params ) {
//phpcs:ignore WordPress.PHP.DontExtract.extract_extract
extract( $params );
}
include $template;
}
endif;
if ( ! function_exists( 'xwp_get_template_html' ) ) :
/**
* Like `xwp_get_template` but returns the HTML instead of outputting it.
*
* @param string $template The template file.
* @param null|array<string,mixed> $params Optional. The variables to pass to the template file.
* @return string
*
* @since 1.18.0
*/
function xwp_get_template_html( string $template, ?array $params = null ): string {
ob_start();
xwp_get_template( $template, $params );
return (string) ob_get_clean();
}
endif;