Completed
Push — dev/6.0.0 ( 66aafb...0eb546 )
by Sudar
43:54 queued 28:47
created

bd_get_short_class_name()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
nc 4
nop 1
dl 0
loc 13
ccs 1
cts 1
cp 1
crap 3
rs 10
c 1
b 0
f 0
1
<?php
2
/**
3
 * Contains the helper functions.
4
 *
5
 * Some of the functions where created before dropping support for PHP 5.2 and that's the reason why they are not namespaced.
6
 *
7
 * @since 6.0.0 File created.
8
 */
9
defined( 'ABSPATH' ) || exit; // Exit if accessed directly.
10
11
/**
12
 * Get a value from an array based on key.
13
 *
14
 * If key is present returns the value, else returns the default value.
15
 *
16
 * @since 5.6.0 added `bd` prefix.
17
 *
18
 * @param array  $array   Array from which value has to be retrieved.
19
 * @param string $key     Key, whose value to be retrieved.
20
 * @param mixed  $default Optional. Default value to be returned, if the key is not found.
21
 *
22
 * @return mixed Value if key is present, else the default value.
23
 */
24
function bd_array_get( $array, $key, $default = null ) {
25 127
	return isset( $array[ $key ] ) ? $array[ $key ] : $default;
26
}
27
28
/**
29
 * Get a value from an array based on key and convert it into bool.
30
 *
31
 * @since 5.6.0 added `bd` prefix.
32
 *
33
 * @param array  $array   Array from which value has to be retrieved.
34
 * @param string $key     Key, whose value to be retrieved.
35
 * @param bool   $default (Optional) Default value to be returned, if the key is not found.
36
 *
37
 * @return bool Boolean converted Value if key is present, else the default value.
38
 */
39
function bd_array_get_bool( $array, $key, $default = false ) {
40 31
	return bd_to_bool( bd_array_get( $array, $key, $default ) );
41
}
42
43
/**
44
 * Convert a string value into boolean, based on whether the value "True" or "False" is present.
45
 *
46
 * @since 5.5
47
 *
48
 * @param string $string String value to compare.
49
 *
50
 * @return bool True if string is "True", False otherwise.
51
 */
52
function bd_to_bool( $string ) {
53 201
	return filter_var( $string, FILTER_VALIDATE_BOOLEAN );
54
}
55
56
/**
57
 * Check if a string starts with a sub string.
58
 *
59
 * Copied from StackOverFlow.
60
 *
61
 * @see https://stackoverflow.com/a/834355/24949.
62
 * @since 6.0.0
63
 *
64
 * @param string $haystack Haystack.
65
 * @param string $needle   Needle.
66
 *
67
 * @return bool True if Haystack starts with Needle, False otherwise.
68
 */
69
function bd_starts_with( $haystack, $needle ) {
70 4
	return ( substr( $haystack, 0, strlen( $needle ) ) === $needle );
71
}
72
73
/**
74
 * Check if a string ends with a sub string.
75
 *
76
 * Copied from StackOverFlow.
77
 *
78
 * @see https://stackoverflow.com/a/51491517/24949
79
 * @since 6.0.0
80
 *
81
 * @param string $haystack Haystack.
82
 * @param string $needle   Needle.
83
 *
84
 * @return bool True if Haystack ends with Needle, False otherwise.
85
 */
86
function bd_ends_with( $haystack, $needle ) {
87 4
	return substr( $haystack, - strlen( $needle ) ) === $needle;
88
}
89
90
/**
91
 * Check if a string contains another sub string.
92
 *
93
 * Copied from StackOverFlow.
94
 *
95
 * @see https://stackoverflow.com/a/4366748/24949
96
 * @since 6.0.0
97
 *
98
 * @param string $haystack Haystack.
99
 * @param string $needle   Needle.
100
 *
101
 * @return bool True if Haystack ends with Needle, False otherwise.
102
 */
103
function bd_contains( $haystack, $needle ) {
104 8
	return strpos( $haystack, $needle ) !== false;
105
}
106
107
/**
108
 * Get the short class name of an object.
109
 *
110
 * Short class name is the name of the class without namespace.
111
 *
112
 * @since 6.0.0
113
 *
114
 * @param object|string $class_name_or_object Object or Class name.
115
 *
116
 * @return string Short class name.
117 28
 */
118
function bd_get_short_class_name( $class_name_or_object ) {
119 28
	$class_name = $class_name_or_object;
120
121
	if ( is_object( $class_name_or_object ) ) {
122
		$class_name = get_class( $class_name_or_object );
123
	}
124
125
	$pos = strrpos( $class_name, '\\' );
0 ignored issues
show
Bug introduced by
It seems like $class_name can also be of type object; however, parameter $haystack of strrpos() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

125
	$pos = strrpos( /** @scrutinizer ignore-type */ $class_name, '\\' );
Loading history...
126
	if ( false === $pos ) {
127
		return $class_name;
128
	}
129
130
	return substr( $class_name, $pos + 1 );
0 ignored issues
show
Bug introduced by
It seems like $class_name can also be of type object; however, parameter $string of substr() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

130
	return substr( /** @scrutinizer ignore-type */ $class_name, $pos + 1 );
Loading history...
131
}
132
133
/**
134
 * Get GMT Offseted time in Unix Timestamp format.
135
 *
136
 * @since 6.0.0
137
 *
138
 * @param string $time_string Time string.
139
 *
140
 * @return int GMT Offseted time.in Unix Timestamp.
141
 */
142
function bd_get_gmt_offseted_time( $time_string ) {
143
	$gmt_offset = sanitize_text_field( get_option( 'gmt_offset' ) );
0 ignored issues
show
Bug introduced by
It seems like get_option('gmt_offset') can also be of type false; however, parameter $str of sanitize_text_field() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

143
	$gmt_offset = sanitize_text_field( /** @scrutinizer ignore-type */ get_option( 'gmt_offset' ) );
Loading history...
144
145
	return strtotime( $time_string ) - ( $gmt_offset * HOUR_IN_SECONDS );
146
}
147
148
/**
149
 * Get the formatted list of allowed mime types.
150
 * This function was originally defined in the Bulk Delete Attachment addon.
151
 *
152
 * @since 5.5
153
 *
154
 * @return array List of allowed mime types after formatting
155
 */
156
function bd_get_allowed_mime_types() {
157
	$mime_types = get_allowed_mime_types();
158
	sort( $mime_types );
159
160
	$processed_mime_types        = array();
161
	$processed_mime_types['all'] = __( 'All mime types', 'bulk-delete' );
162
163
	$last_value = '';
164
	foreach ( $mime_types as $key => $value ) {
165
		$splitted = explode( '/', $value, 2 );
166
		$prefix   = $splitted[0];
167
168
		if ( '' == $last_value || $prefix != $last_value ) {
169
			$processed_mime_types[ $prefix ] = __( 'All', 'bulk-delete' ) . ' ' . $prefix;
170
			$last_value                      = $prefix;
171
		}
172
173
		$processed_mime_types[ $value ] = $value;
174
	}
175
176
	return $processed_mime_types;
177
}
178
179
/**
180
 * Get current theme name.
181
 *
182
 * @since 5.5.4
183
 *
184
 * @return string Current theme name.
185
 */
186
function bd_get_current_theme_name() {
187
	if ( get_bloginfo( 'version' ) < '3.4' ) {
188
		$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

188
		$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...
189
190
		return $theme_data['Name'] . ' ' . $theme_data['Version'];
191
	} else {
192
		$theme_data = wp_get_theme();
193
194
		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

194
		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

194
		return /** @scrutinizer ignore-type */ $theme_data->Name . ' ' . $theme_data->Version;
Loading history...
195
	}
196
}
197
198
/**
199
 * Try to identity the hosting provider.
200
 *
201
 * @since 5.5.4
202
 *
203
 * @return string Web host name if identified, empty string otherwise.
204
 */
205
function bd_identify_host() {
206
	$host = '';
207
	if ( defined( 'WPE_APIKEY' ) ) {
208
		$host = 'WP Engine';
209
	} elseif ( defined( 'PAGELYBIN' ) ) {
210
		$host = 'Pagely';
211
	}
212
213
	return $host;
214
}
215
216
/**
217
 * Print plugins that are currently active.
218
 *
219
 * @since 5.5.4
220
 */
221
function bd_print_current_plugins() {
222
	$plugins        = get_plugins();
223
	$active_plugins = get_option( 'active_plugins', array() );
224
225
	foreach ( $plugins as $plugin_path => $plugin ) {
226
		// If the plugin isn't active, don't show it.
227
		if ( ! in_array( $plugin_path, $active_plugins ) ) {
0 ignored issues
show
Bug introduced by
It seems like $active_plugins can also be of type false; however, parameter $haystack of in_array() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

227
		if ( ! in_array( $plugin_path, /** @scrutinizer ignore-type */ $active_plugins ) ) {
Loading history...
228
			continue;
229
		}
230
231
		echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
232
	}
233
}
234
235
/**
236
 * Print network active plugins.
237
 *
238
 * @since 5.5.4
239
 */
240
function bd_print_network_active_plugins() {
241
	$plugins        = wp_get_active_network_plugins();
242
	$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
243
244
	foreach ( $plugins as $plugin_path ) {
245
		$plugin_base = plugin_basename( $plugin_path );
246
247
		// If the plugin isn't active, don't show it.
248
		if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
249
			continue;
250
		}
251
252
		$plugin = get_plugin_data( $plugin_path );
253
254
		echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
255
	}
256
}
257
258
/**
259
 * Print scheduled jobs.
260
 *
261
 * @since 6.0
262
 */
263
function bd_print_scheduled_jobs() {
264
	$cron        = _get_cron_array();
265
	$date_format = _x( 'M j, Y @ G:i', 'Cron table date format', 'bulk-delete' );
266
267
	foreach ( $cron as $timestamp => $cronhooks ) {
268
		foreach ( (array) $cronhooks as $hook => $events ) {
269
			if ( 'do-bulk-delete-' === substr( $hook, 0, 15 ) ) {
270
				foreach ( (array) $events as $key => $event ) {
271
					echo date_i18n( $date_format, $timestamp + ( get_option( 'gmt_offset' ) * 60 * 60 ) ) . ' (' . $timestamp . ')';
272
					echo ' | ';
273
					echo $event['schedule'];
274
					echo ' | ';
275
					echo $hook;
276
					echo "\n";
277
				}
278
			}
279
		}
280
	}
281
}
282
283
/**
284
 * Print License Info.
285
 *
286
 * @since 6.0
287
 *
288
 * @param mixed $keys
289
 */
290
function bd_print_license_info( $keys ) {
291
	foreach ( $keys as $key ) {
292
		echo $key['addon-name'];
293
		echo ' | ';
294
		echo $key['license'];
295
		echo ' | ';
296
		echo $key['expires'];
297
		echo ' | ';
298
		echo $key['validity'];
299
		echo ' | ';
300
		echo $key['addon-code'];
301
	}
302
}
303