1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Utility classes and functions. |
4
|
|
|
* |
5
|
|
|
* @author Sudar |
6
|
|
|
* |
7
|
|
|
* @package BulkDelete\Util |
8
|
|
|
*/ |
9
|
|
|
defined( 'ABSPATH' ) || exit; // Exit if accessed directly |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Utility class. |
13
|
|
|
* |
14
|
|
|
* Ideally most of the functions should be inside the `BulkDelete\Util` and not as static functions. |
15
|
|
|
*/ |
16
|
|
|
class BD_Util { |
17
|
|
|
// Meta boxes |
18
|
|
|
const VISIBLE_POST_BOXES = 'metaboxhidden_toplevel_page_bulk-delete-posts'; |
19
|
|
|
const VISIBLE_PAGE_BOXES = 'metaboxhidden_bulk-delete_page_bulk-delete-pages'; |
20
|
|
|
const VISIBLE_USER_BOXES = 'metaboxhidden_bulk-delete_page_bulk-delete-users'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Check whether the meta box in posts page is hidden or not. |
24
|
|
|
* |
25
|
|
|
* @static |
26
|
|
|
* @access public |
27
|
|
|
* |
28
|
|
|
* @param string $box The name of the box |
29
|
|
|
* |
30
|
|
|
* @return bool True if the box is hidden, False otherwise |
31
|
|
|
*/ |
32
|
|
|
public static function is_posts_box_hidden( $box ) { |
33
|
|
|
$hidden_boxes = self::get_posts_hidden_boxes(); |
34
|
|
|
|
35
|
|
|
return is_array( $hidden_boxes ) && in_array( $box, $hidden_boxes ); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Get the list of hidden boxes in posts page. |
40
|
|
|
* |
41
|
|
|
* @static |
42
|
|
|
* @access public |
43
|
|
|
* |
44
|
|
|
* @return array The list of hidden meta boxes |
45
|
|
|
*/ |
46
|
|
|
public static function get_posts_hidden_boxes() { |
47
|
|
|
$current_user = wp_get_current_user(); |
48
|
|
|
|
49
|
|
|
return get_user_meta( $current_user->ID, self::VISIBLE_POST_BOXES, true ); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Check whether the meta box in pages page is hidden or not. |
54
|
|
|
* |
55
|
|
|
* @since 5.0 |
56
|
|
|
* @static |
57
|
|
|
* @access public |
58
|
|
|
* |
59
|
|
|
* @param string $box The name of the box to check |
60
|
|
|
* |
61
|
|
|
* @return bool True if the box is hidden, False otherwise |
62
|
|
|
*/ |
63
|
|
|
public static function is_pages_box_hidden( $box ) { |
64
|
|
|
$hidden_boxes = self::get_pages_hidden_boxes(); |
65
|
|
|
|
66
|
|
|
return is_array( $hidden_boxes ) && in_array( $box, $hidden_boxes ); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get the list of hidden boxes in posts page. |
71
|
|
|
* |
72
|
|
|
* @since 5.0 |
73
|
|
|
* @static |
74
|
|
|
* @access public |
75
|
|
|
* |
76
|
|
|
* @return the array of hidden meta boxes |
77
|
|
|
*/ |
78
|
|
|
public static function get_pages_hidden_boxes() { |
79
|
|
|
$current_user = wp_get_current_user(); |
80
|
|
|
|
81
|
|
|
return get_user_meta( $current_user->ID, self::VISIBLE_PAGE_BOXES, true ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Check whether the meta box in users page is hidden or not. |
86
|
|
|
* |
87
|
|
|
* @static |
88
|
|
|
* @access public |
89
|
|
|
* |
90
|
|
|
* @param string $box The name of the box to check |
91
|
|
|
* |
92
|
|
|
* @return bool True if the box is hidden, False otherwise |
93
|
|
|
*/ |
94
|
|
|
public static function is_users_box_hidden( $box ) { |
95
|
|
|
$hidden_boxes = self::get_users_hidden_boxes(); |
96
|
|
|
|
97
|
|
|
return is_array( $hidden_boxes ) && in_array( $box, $hidden_boxes ); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the list of hidden boxes in users page. |
102
|
|
|
* |
103
|
|
|
* @static |
104
|
|
|
* @access public |
105
|
|
|
* |
106
|
|
|
* @return array The array of hidden meta boxes |
107
|
|
|
*/ |
108
|
|
|
public static function get_users_hidden_boxes() { |
109
|
|
|
$current_user = wp_get_current_user(); |
110
|
|
|
|
111
|
|
|
return get_user_meta( $current_user->ID, self::VISIBLE_USER_BOXES, true ); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the list of cron schedules. |
116
|
|
|
* |
117
|
|
|
* @static |
118
|
|
|
* @access public |
119
|
|
|
* |
120
|
|
|
* @return array The list of cron schedules |
121
|
|
|
*/ |
122
|
|
|
public static function get_cron_schedules() { |
123
|
|
|
$cron_items = array(); |
124
|
|
|
$cron = _get_cron_array(); |
125
|
|
|
$date_format = _x( 'M j, Y @ G:i', 'Cron table date format', 'bulk-delete' ); |
126
|
|
|
$i = 0; |
127
|
|
|
|
128
|
|
|
foreach ( $cron as $timestamp => $cronhooks ) { |
129
|
|
|
foreach ( (array) $cronhooks as $hook => $events ) { |
130
|
|
|
if ( 'do-bulk-delete-' == substr( $hook, 0, 15 ) ) { |
131
|
|
|
$cron_item = array(); |
132
|
|
|
|
133
|
|
|
foreach ( (array) $events as $key => $event ) { |
134
|
|
|
$cron_item['timestamp'] = $timestamp; |
135
|
|
|
$cron_item['due'] = date_i18n( $date_format, $timestamp + ( get_option( 'gmt_offset' ) * 60 * 60 ) ); |
136
|
|
|
$cron_item['schedule'] = $event['schedule']; |
137
|
|
|
$cron_item['type'] = $hook; |
138
|
|
|
$cron_item['args'] = $event['args']; |
139
|
|
|
$cron_item['id'] = $i; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
$cron_items[ $i ] = $cron_item; |
143
|
|
|
$i++; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $cron_items; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Generate display name from post type and status. |
153
|
|
|
* |
154
|
|
|
* @static |
155
|
|
|
* |
156
|
|
|
* @param string $str |
157
|
|
|
* |
158
|
|
|
* @return string Label |
159
|
|
|
*/ |
160
|
|
|
public static function display_post_type_status( $str ) { |
161
|
|
|
$type_status = self::split_post_type_status( $str ); |
162
|
|
|
|
163
|
|
|
$status = $type_status['status']; |
164
|
|
|
$type = $type_status['type']; |
165
|
|
|
$label = ''; |
166
|
|
|
|
167
|
|
|
switch ( $status ) { |
168
|
|
|
case 'private': |
169
|
|
|
$label = $type . ' - Private Posts'; |
170
|
|
|
break; |
171
|
|
|
case 'future': |
172
|
|
|
$label = $type . ' - Scheduled Posts'; |
173
|
|
|
break; |
174
|
|
|
case 'draft': |
175
|
|
|
$label = $type . ' - Draft Posts'; |
176
|
|
|
break; |
177
|
|
|
case 'pending': |
178
|
|
|
$label = $type . ' - Pending Posts'; |
179
|
|
|
break; |
180
|
|
|
case 'publish': |
181
|
|
|
$label = $type . ' - Published Posts'; |
182
|
|
|
break; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $label; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Split post type and status. |
190
|
|
|
* |
191
|
|
|
* @static |
192
|
|
|
* @access public |
193
|
|
|
* |
194
|
|
|
* @param string $str |
195
|
|
|
* |
196
|
|
|
* @return array |
197
|
|
|
*/ |
198
|
|
|
public static function split_post_type_status( $str ) { |
199
|
|
|
$type_status = array(); |
200
|
|
|
|
201
|
|
|
$str_arr = explode( '-', $str ); |
202
|
|
|
|
203
|
|
|
if ( count( $str_arr ) > 1 ) { |
204
|
|
|
$type_status['status'] = end( $str_arr ); |
205
|
|
|
$type_status['type'] = implode( '-', array_slice( $str_arr, 0, -1 ) ); |
206
|
|
|
} else { |
207
|
|
|
$type_status['status'] = 'publish'; |
208
|
|
|
$type_status['type'] = $str; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
return $type_status; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* Get a value from an array based on key. |
217
|
|
|
* |
218
|
|
|
* If key is present returns the value, else returns the default value. |
219
|
|
|
* |
220
|
|
|
* @since 5.6.0 added `bd` prefix. |
221
|
|
|
* |
222
|
|
|
* @param array $array Array from which value has to be retrieved. |
223
|
|
|
* @param string $key Key, whose value to be retrieved. |
224
|
|
|
* @param string $default Optional. Default value to be returned, if the key is not found. |
|
|
|
|
225
|
|
|
* |
226
|
|
|
* @return mixed Value if key is present, else the default value. |
227
|
|
|
*/ |
228
|
|
|
function bd_array_get( $array, $key, $default = null ) { |
229
|
|
|
return isset( $array[ $key ] ) ? $array[ $key ] : $default; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Get a value from an array based on key and convert it into bool. |
234
|
|
|
* |
235
|
|
|
* @since 5.6.0 added `bd` prefix. |
236
|
|
|
* |
237
|
|
|
* @param array $array Array from which value has to be retrieved. |
238
|
|
|
* @param string $key Key, whose value to be retrieved. |
239
|
|
|
* @param bool $default (Optional) Default value to be returned, if the key is not found. |
240
|
|
|
* |
241
|
|
|
* @return bool Boolean converted Value if key is present, else the default value. |
242
|
|
|
*/ |
243
|
|
|
function bd_array_get_bool( $array, $key, $default = false ) { |
244
|
|
|
return bd_to_bool( bd_array_get( $array, $key, $default ) ); |
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Convert a string value into boolean, based on whether the value "True" or "False" is present. |
249
|
|
|
* |
250
|
|
|
* @since 5.5 |
251
|
|
|
* |
252
|
|
|
* @param string $string String value to compare. |
253
|
|
|
* |
254
|
|
|
* @return bool True if string is "True", False otherwise. |
255
|
|
|
*/ |
256
|
|
|
function bd_to_bool( $string ) { |
257
|
|
|
return filter_var( $string, FILTER_VALIDATE_BOOLEAN ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Get the formatted list of allowed mime types. |
262
|
|
|
* This function was originally defined in the Bulk Delete Attachment addon. |
263
|
|
|
* |
264
|
|
|
* @since 5.5 |
265
|
|
|
* |
266
|
|
|
* @return array List of allowed mime types after formatting |
267
|
|
|
*/ |
268
|
|
|
function bd_get_allowed_mime_types() { |
269
|
|
|
$mime_types = get_allowed_mime_types(); |
270
|
|
|
sort( $mime_types ); |
271
|
|
|
|
272
|
|
|
$processed_mime_types = array(); |
273
|
|
|
$processed_mime_types['all'] = __( 'All mime types', 'bulk-delete' ); |
274
|
|
|
|
275
|
|
|
$last_value = ''; |
276
|
|
|
foreach ( $mime_types as $key => $value ) { |
277
|
|
|
$splitted = explode( '/', $value, 2 ); |
278
|
|
|
$prefix = $splitted[0]; |
279
|
|
|
|
280
|
|
|
if ( '' == $last_value || $prefix != $last_value ) { |
281
|
|
|
$processed_mime_types[ $prefix ] = __( 'All', 'bulk-delete' ) . ' ' . $prefix; |
282
|
|
|
$last_value = $prefix; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
$processed_mime_types[ $value ] = $value; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return $processed_mime_types; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Get current theme name. |
293
|
|
|
* |
294
|
|
|
* @since 5.5.4 |
295
|
|
|
* |
296
|
|
|
* @return string Current theme name. |
297
|
|
|
*/ |
298
|
|
|
function bd_get_current_theme_name() { |
299
|
|
|
if ( get_bloginfo( 'version' ) < '3.4' ) { |
300
|
|
|
$theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' ); |
301
|
|
|
|
302
|
|
|
return $theme_data['Name'] . ' ' . $theme_data['Version']; |
303
|
|
|
} else { |
304
|
|
|
$theme_data = wp_get_theme(); |
305
|
|
|
|
306
|
|
|
return $theme_data->Name . ' ' . $theme_data->Version; |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
/** |
311
|
|
|
* Try to identity the hosting provider. |
312
|
|
|
* |
313
|
|
|
* @since 5.5.4 |
314
|
|
|
* |
315
|
|
|
* @return string Web host name if identified, empty string otherwise. |
316
|
|
|
*/ |
317
|
|
|
function bd_identify_host() { |
318
|
|
|
$host = ''; |
319
|
|
|
if ( defined( 'WPE_APIKEY' ) ) { |
320
|
|
|
$host = 'WP Engine'; |
321
|
|
|
} elseif ( defined( 'PAGELYBIN' ) ) { |
322
|
|
|
$host = 'Pagely'; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return $host; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Print plugins that are currently active. |
330
|
|
|
* |
331
|
|
|
* @since 5.5.4 |
332
|
|
|
*/ |
333
|
|
|
function bd_print_current_plugins() { |
334
|
|
|
$plugins = get_plugins(); |
335
|
|
|
$active_plugins = get_option( 'active_plugins', array() ); |
336
|
|
|
|
337
|
|
|
foreach ( $plugins as $plugin_path => $plugin ) { |
338
|
|
|
// If the plugin isn't active, don't show it. |
339
|
|
|
if ( ! in_array( $plugin_path, $active_plugins ) ) { |
340
|
|
|
continue; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n"; |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|
347
|
|
|
/** |
348
|
|
|
* Print network active plugins. |
349
|
|
|
* |
350
|
|
|
* @since 5.5.4 |
351
|
|
|
*/ |
352
|
|
|
function bd_print_network_active_plugins() { |
353
|
|
|
$plugins = wp_get_active_network_plugins(); |
354
|
|
|
$active_plugins = get_site_option( 'active_sitewide_plugins', array() ); |
355
|
|
|
|
356
|
|
|
foreach ( $plugins as $plugin_path ) { |
357
|
|
|
$plugin_base = plugin_basename( $plugin_path ); |
358
|
|
|
|
359
|
|
|
// If the plugin isn't active, don't show it. |
360
|
|
|
if ( ! array_key_exists( $plugin_base, $active_plugins ) ) { |
361
|
|
|
continue; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
$plugin = get_plugin_data( $plugin_path ); |
365
|
|
|
|
366
|
|
|
echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n"; |
367
|
|
|
} |
368
|
|
|
} |
369
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.