Passed
Push — 178-feature/delete-terms--by-p... ( fe64b6...870088 )
by Sudar
11:08
created

bd_ends_with()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
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 33
	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 93
	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
 *
63
 * @since 6.0.0
64
 *
65
 * @param string $haystack Haystack.
66 28
 * @param string $needle   Needle.
67
 *
68 28
 * @return boolean True if Haystack starts with Needle, False otherwise.
69
 */
70
function bd_starts_with( $haystack, $needle ) {
71
	return ( substr( $haystack, 0, strlen( $needle ) ) === $needle );
72
}
73
74
/**
75
 * Check if a string ends with a sub string.
76
 *
77
 * Copied from StackOverFlow.
78
 *
79
 * @see https://stackoverflow.com/a/51491517/24949
80
 *
81
 * @since 6.0.0
82
 *
83
 * @param string $haystack Haystack.
84
 * @param string $needle   Needle.
85
 *
86
 * @return boolean True if Haystack ends with Needle, False otherwise.
87
 */
88
function bd_ends_with( $haystack, $needle ) {
89
	return substr( $haystack, - strlen( $needle ) ) === $needle;
90
}
91
92
/**
93
 * Check if a string contains another sub string.
94
 *
95
 * Copied from StackOverFlow.
96
 *
97
 * @see https://stackoverflow.com/a/4366748/24949
98
 *
99
 * @since 6.0.0
100
 *
101
 * @param string $haystack Haystack.
102
 * @param string $needle   Needle.
103
 *
104
 * @return boolean True if Haystack ends with Needle, False otherwise.
105
 */
106
function bd_contains( $haystack, $needle ) {
107
	return strpos( $haystack, $needle ) !== false;
108
}
109
110
/**
111
 * Get GMT Offseted time in Unix Timestamp format.
112
 *
113
 * @since 6.0.0
114
 *
115
 * @param string $time_string Time string.
116
 *
117
 * @return int GMT Offseted time.in Unix Timestamp.
118
 */
119
function bd_get_gmt_offseted_time( $time_string ) {
120
	$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

120
	$gmt_offset = sanitize_text_field( /** @scrutinizer ignore-type */ get_option( 'gmt_offset' ) );
Loading history...
121
122
	return strtotime( $time_string ) - ( $gmt_offset * HOUR_IN_SECONDS );
123
}
124
125
/**
126
 * Get the formatted list of allowed mime types.
127
 * This function was originally defined in the Bulk Delete Attachment addon.
128
 *
129
 * @since 5.5
130
 *
131
 * @return array List of allowed mime types after formatting
132
 */
133
function bd_get_allowed_mime_types() {
134
	$mime_types = get_allowed_mime_types();
135
	sort( $mime_types );
136
137
	$processed_mime_types        = array();
138
	$processed_mime_types['all'] = __( 'All mime types', 'bulk-delete' );
139
140
	$last_value = '';
141
	foreach ( $mime_types as $key => $value ) {
142
		$splitted = explode( '/', $value, 2 );
143
		$prefix   = $splitted[0];
144
145
		if ( '' == $last_value || $prefix != $last_value ) {
146
			$processed_mime_types[ $prefix ] = __( 'All', 'bulk-delete' ) . ' ' . $prefix;
147
			$last_value                      = $prefix;
148
		}
149
150
		$processed_mime_types[ $value ] = $value;
151
	}
152
153
	return $processed_mime_types;
154
}
155
156
/**
157
 * Get current theme name.
158
 *
159
 * @since 5.5.4
160
 *
161
 * @return string Current theme name.
162
 */
163
function bd_get_current_theme_name() {
164
	if ( get_bloginfo( 'version' ) < '3.4' ) {
165
		$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

165
		$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...
166
167
		return $theme_data['Name'] . ' ' . $theme_data['Version'];
168
	} else {
169
		$theme_data = wp_get_theme();
170
171
		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

171
		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

171
		return /** @scrutinizer ignore-type */ $theme_data->Name . ' ' . $theme_data->Version;
Loading history...
172
	}
173
}
174
175
/**
176
 * Try to identity the hosting provider.
177
 *
178
 * @since 5.5.4
179
 *
180
 * @return string Web host name if identified, empty string otherwise.
181
 */
182
function bd_identify_host() {
183
	$host = '';
184
	if ( defined( 'WPE_APIKEY' ) ) {
185
		$host = 'WP Engine';
186
	} elseif ( defined( 'PAGELYBIN' ) ) {
187
		$host = 'Pagely';
188
	}
189
190
	return $host;
191
}
192
193
/**
194
 * Print plugins that are currently active.
195
 *
196
 * @since 5.5.4
197
 */
198
function bd_print_current_plugins() {
199
	$plugins        = get_plugins();
200
	$active_plugins = get_option( 'active_plugins', array() );
201
202
	foreach ( $plugins as $plugin_path => $plugin ) {
203
		// If the plugin isn't active, don't show it.
204
		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

204
		if ( ! in_array( $plugin_path, /** @scrutinizer ignore-type */ $active_plugins ) ) {
Loading history...
205
			continue;
206
		}
207
208
		echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
209
	}
210
}
211
212
/**
213
 * Print network active plugins.
214
 *
215
 * @since 5.5.4
216
 */
217
function bd_print_network_active_plugins() {
218
	$plugins        = wp_get_active_network_plugins();
219
	$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
220
221
	foreach ( $plugins as $plugin_path ) {
222
		$plugin_base = plugin_basename( $plugin_path );
223
224
		// If the plugin isn't active, don't show it.
225
		if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
226
			continue;
227
		}
228
229
		$plugin = get_plugin_data( $plugin_path );
230
231
		echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
232
	}
233
}
234
235
/**
236
 * Print scheduled jobs.
237
 *
238
 * @since 6.0
239
 */
240
function bd_print_scheduled_jobs() {
241
	$cron        = _get_cron_array();
242
	$date_format = _x( 'M j, Y @ G:i', 'Cron table date format', 'bulk-delete' );
243
244
	foreach ( $cron as $timestamp => $cronhooks ) {
245
		foreach ( (array) $cronhooks as $hook => $events ) {
246
			if ( 'do-bulk-delete-' === substr( $hook, 0, 15 ) ) {
247
				foreach ( (array) $events as $key => $event ) {
248
					echo date_i18n( $date_format, $timestamp + ( get_option( 'gmt_offset' ) * 60 * 60 ) ) . ' (' . $timestamp . ')';
249
					echo ' | ';
250
					echo $event['schedule'];
251
					echo ' | ';
252
					echo $hook;
253
					echo "\n";
254
				}
255
			}
256
		}
257
	}
258
}
259
260
/**
261
 * Print License Info.
262
 *
263
 * @since 6.0
264
 *
265
 * @param mixed $keys
266
 */
267
function bd_print_license_info( $keys ) {
268
	foreach ( $keys as $key ) {
269
		echo $key['addon-name'];
270
		echo ' | ';
271
		echo $key['license'];
272
		echo ' | ';
273
		echo $key['expires'];
274
		echo ' | ';
275
		echo $key['validity'];
276
		echo ' | ';
277
		echo $key['addon-code'];
278
	}
279
}
280