Completed
Pull Request — dev/5.7.0 (#195)
by
unknown
08:32 queued 04:45
created

BD_Util::is_posts_box_hidden()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
if ( ! function_exists( ' bd_array_get' ) ) {
229
	function bd_array_get( $array, $key, $default = null ) {
230
		return isset( $array[ $key ] ) ? $array[ $key ] : $default;
231
	}
232
}
233
234
/**
235
 * Get a value from an array based on key and convert it into bool.
236
 *
237
 * @since 5.6.0 added `bd` prefix.
238
 *
239
 * @param array  $array   Array from which value has to be retrieved.
240
 * @param string $key     Key, whose value to be retrieved.
241
 * @param bool   $default (Optional) Default value to be returned, if the key is not found.
242
 *
243
 * @return bool Boolean converted Value if key is present, else the default value.
244
 */
245
function bd_array_get_bool( $array, $key, $default = false ) {
246
	return bd_to_bool(  bd_array_get( $array, $key, $default ) );
247
}
248
249
/**
250
 * Convert a string value into boolean, based on whether the value "True" or "False" is present.
251
 *
252
 * @since 5.5
253
 *
254
 * @param string $string String value to compare.
255
 *
256
 * @return bool True if string is "True", False otherwise.
257
 */
258
function bd_to_bool( $string ) {
259
	return filter_var( $string, FILTER_VALIDATE_BOOLEAN );
260
}
261
262
/**
263
 * Get the formatted list of allowed mime types.
264
 * This function was originally defined in the Bulk Delete Attachment addon.
265
 *
266
 * @since 5.5
267
 *
268
 * @return array List of allowed mime types after formatting
269
 */
270
function bd_get_allowed_mime_types() {
271
	$mime_types = get_allowed_mime_types();
272
	sort( $mime_types );
273
274
	$processed_mime_types        = array();
275
	$processed_mime_types['all'] = __( 'All mime types', 'bulk-delete' );
276
277
	$last_value = '';
278
	foreach ( $mime_types as $key => $value ) {
279
		$splitted = explode( '/', $value, 2 );
280
		$prefix   = $splitted[0];
281
282
		if ( '' == $last_value || $prefix != $last_value ) {
283
			$processed_mime_types[ $prefix ] = __( 'All', 'bulk-delete' ) . ' ' . $prefix;
284
			$last_value                      = $prefix;
285
		}
286
287
		$processed_mime_types[ $value ] = $value;
288
	}
289
290
	return $processed_mime_types;
291
}
292
293
/**
294
 * Get current theme name.
295
 *
296
 * @since 5.5.4
297
 *
298
 * @return string Current theme name.
299
 */
300
function bd_get_current_theme_name() {
301
	if ( get_bloginfo( 'version' ) < '3.4' ) {
302
		$theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' );
303
304
		return $theme_data['Name'] . ' ' . $theme_data['Version'];
305
	} else {
306
		$theme_data = wp_get_theme();
307
308
		return $theme_data->Name . ' ' . $theme_data->Version;
309
	}
310
}
311
312
/**
313
 * Try to identity the hosting provider.
314
 *
315
 * @since 5.5.4
316
 *
317
 * @return string Web host name if identified, empty string otherwise.
318
 */
319
function bd_identify_host() {
320
	$host = '';
321
	if ( defined( 'WPE_APIKEY' ) ) {
322
		$host = 'WP Engine';
323
	} elseif ( defined( 'PAGELYBIN' ) ) {
324
		$host = 'Pagely';
325
	}
326
327
	return $host;
328
}
329
330
/**
331
 * Print plugins that are currently active.
332
 *
333
 * @since 5.5.4
334
 */
335
function bd_print_current_plugins() {
336
	$plugins        = get_plugins();
337
	$active_plugins = get_option( 'active_plugins', array() );
338
339
	foreach ( $plugins as $plugin_path => $plugin ) {
340
		// If the plugin isn't active, don't show it.
341
		if ( ! in_array( $plugin_path, $active_plugins ) ) {
342
			continue;
343
		}
344
345
		echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
346
	}
347
}
348
349
/**
350
 * Print network active plugins.
351
 *
352
 * @since 5.5.4
353
 */
354
function bd_print_network_active_plugins() {
355
	$plugins        = wp_get_active_network_plugins();
356
	$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
357
358
	foreach ( $plugins as $plugin_path ) {
359
		$plugin_base = plugin_basename( $plugin_path );
360
361
		// If the plugin isn't active, don't show it.
362
		if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
363
			continue;
364
		}
365
366
		$plugin = get_plugin_data( $plugin_path );
367
368
		echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
369
	}
370
}
371