Passed
Push — 217-feature/delete-posts-by-po... ( d7bccc...aea384 )
by Sudar
04:38
created

bd_get_gmt_offseted_time()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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
function bd_array_get( $array, $key, $default = null ) {
229 31
	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 31
	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 48
	return filter_var( $string, FILTER_VALIDATE_BOOLEAN );
258
}
259
260
/**
261
 * Get GMT Offseted time in Unix Timestamp format.
262
 *
263
 * @since 6.0.0
264
 *
265
 * @param string $time_string Time string.
266
 *
267
 * @return int GMT Offseted time.in Unix Timestamp.
268
 */
269
function bd_get_gmt_offseted_time( $time_string ) {
270 28
	$gmt_offset = absint( get_option( 'gmt_offset' ) );
271
272 28
	return strtotime( $time_string ) - ( $gmt_offset * HOUR_IN_SECONDS );
273
}
274
275
/**
276
 * Get the formatted list of allowed mime types.
277
 * This function was originally defined in the Bulk Delete Attachment addon.
278
 *
279
 * @since 5.5
280
 *
281
 * @return array List of allowed mime types after formatting
282
 */
283
function bd_get_allowed_mime_types() {
284
	$mime_types = get_allowed_mime_types();
285
	sort( $mime_types );
286
287
	$processed_mime_types        = array();
288
	$processed_mime_types['all'] = __( 'All mime types', 'bulk-delete' );
289
290
	$last_value = '';
291
	foreach ( $mime_types as $key => $value ) {
292
		$splitted = explode( '/', $value, 2 );
293
		$prefix   = $splitted[0];
294
295
		if ( '' == $last_value || $prefix != $last_value ) {
296
			$processed_mime_types[ $prefix ] = __( 'All', 'bulk-delete' ) . ' ' . $prefix;
297
			$last_value                      = $prefix;
298
		}
299
300
		$processed_mime_types[ $value ] = $value;
301
	}
302
303
	return $processed_mime_types;
304
}
305
306
/**
307
 * Get current theme name.
308
 *
309
 * @since 5.5.4
310
 *
311
 * @return string Current theme name.
312
 */
313
function bd_get_current_theme_name() {
314
	if ( get_bloginfo( 'version' ) < '3.4' ) {
315
		$theme_data = get_theme_data( get_stylesheet_directory() . '/style.css' );
0 ignored issues
show
Deprecated Code introduced by
The function get_theme_data() has been deprecated: 3.4.0 Use wp_get_theme() ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

315
		$theme_data = /** @scrutinizer ignore-deprecated */ get_theme_data( get_stylesheet_directory() . '/style.css' );

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
316
317
		return $theme_data['Name'] . ' ' . $theme_data['Version'];
318
	} else {
319
		$theme_data = wp_get_theme();
320
321
		return $theme_data->Name . ' ' . $theme_data->Version;
0 ignored issues
show
Bug introduced by
Are you sure $theme_data->Version of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

321
		return $theme_data->Name . ' ' . /** @scrutinizer ignore-type */ $theme_data->Version;
Loading history...
Bug introduced by
Are you sure $theme_data->Name of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

321
		return /** @scrutinizer ignore-type */ $theme_data->Name . ' ' . $theme_data->Version;
Loading history...
322
	}
323
}
324
325
/**
326
 * Try to identity the hosting provider.
327
 *
328
 * @since 5.5.4
329
 *
330
 * @return string Web host name if identified, empty string otherwise.
331
 */
332
function bd_identify_host() {
333
	$host = '';
334
	if ( defined( 'WPE_APIKEY' ) ) {
335
		$host = 'WP Engine';
336
	} elseif ( defined( 'PAGELYBIN' ) ) {
337
		$host = 'Pagely';
338
	}
339
340
	return $host;
341
}
342
343
/**
344
 * Print plugins that are currently active.
345
 *
346
 * @since 5.5.4
347
 */
348
function bd_print_current_plugins() {
349
	$plugins        = get_plugins();
350
	$active_plugins = get_option( 'active_plugins', array() );
351
352
	foreach ( $plugins as $plugin_path => $plugin ) {
353
		// If the plugin isn't active, don't show it.
354
		if ( ! in_array( $plugin_path, $active_plugins ) ) {
355
			continue;
356
		}
357
358
		echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
359
	}
360
}
361
362
/**
363
 * Print network active plugins.
364
 *
365
 * @since 5.5.4
366
 */
367
function bd_print_network_active_plugins() {
368
	$plugins        = wp_get_active_network_plugins();
369
	$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
370
371
	foreach ( $plugins as $plugin_path ) {
372
		$plugin_base = plugin_basename( $plugin_path );
373
374
		// If the plugin isn't active, don't show it.
375
		if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
376
			continue;
377
		}
378
379
		$plugin = get_plugin_data( $plugin_path );
380
381
		echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
382
	}
383
}
384