Completed
Push — 112-fix/scheduled-jobs-in-syst... ( 52fa2d...c6aa40 )
by Sudar
08:47 queued 08:44
created

bd_print_scheduled_jobs()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 16
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 13
c 2
b 1
f 0
nc 5
nop 0
dl 0
loc 16
ccs 0
cts 13
cp 0
crap 30
rs 8.8571
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 string $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 63
	return filter_var( $string, FILTER_VALIDATE_BOOLEAN );
54
}
55
56
/**
57
 * Get GMT Offseted time in Unix Timestamp format.
58
 *
59
 * @since 6.0.0
60
 *
61
 * @param string $time_string Time string.
62
 *
63
 * @return int GMT Offseted time.in Unix Timestamp.
64
 */
65
function bd_get_gmt_offseted_time( $time_string ) {
66 28
	$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

66
	$gmt_offset = sanitize_text_field( /** @scrutinizer ignore-type */ get_option( 'gmt_offset' ) );
Loading history...
67
68 28
	return strtotime( $time_string ) - ( $gmt_offset * HOUR_IN_SECONDS );
69
}
70
71
/**
72
 * Get the formatted list of allowed mime types.
73
 * This function was originally defined in the Bulk Delete Attachment addon.
74
 *
75
 * @since 5.5
76
 *
77
 * @return array List of allowed mime types after formatting
78
 */
79
function bd_get_allowed_mime_types() {
80
	$mime_types = get_allowed_mime_types();
81
	sort( $mime_types );
82
83
	$processed_mime_types        = array();
84
	$processed_mime_types['all'] = __( 'All mime types', 'bulk-delete' );
85
86
	$last_value = '';
87
	foreach ( $mime_types as $key => $value ) {
88
		$splitted = explode( '/', $value, 2 );
89
		$prefix   = $splitted[0];
90
91
		if ( '' == $last_value || $prefix != $last_value ) {
92
			$processed_mime_types[ $prefix ] = __( 'All', 'bulk-delete' ) . ' ' . $prefix;
93
			$last_value                      = $prefix;
94
		}
95
96
		$processed_mime_types[ $value ] = $value;
97
	}
98
99
	return $processed_mime_types;
100
}
101
102
/**
103
 * Get current theme name.
104
 *
105
 * @since 5.5.4
106
 *
107
 * @return string Current theme name.
108
 */
109
function bd_get_current_theme_name() {
110
	if ( get_bloginfo( 'version' ) < '3.4' ) {
111
		$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

111
		$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...
112
113
		return $theme_data['Name'] . ' ' . $theme_data['Version'];
114
	} else {
115
		$theme_data = wp_get_theme();
116
117
		return $theme_data->Name . ' ' . $theme_data->Version;
0 ignored issues
show
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

117
		return /** @scrutinizer ignore-type */ $theme_data->Name . ' ' . $theme_data->Version;
Loading history...
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

117
		return $theme_data->Name . ' ' . /** @scrutinizer ignore-type */ $theme_data->Version;
Loading history...
118
	}
119
}
120
121
/**
122
 * Try to identity the hosting provider.
123
 *
124
 * @since 5.5.4
125
 *
126
 * @return string Web host name if identified, empty string otherwise.
127
 */
128
function bd_identify_host() {
129
	$host = '';
130
	if ( defined( 'WPE_APIKEY' ) ) {
131
		$host = 'WP Engine';
132
	} elseif ( defined( 'PAGELYBIN' ) ) {
133
		$host = 'Pagely';
134
	}
135
136
	return $host;
137
}
138
139
/**
140
 * Print plugins that are currently active.
141
 *
142
 * @since 5.5.4
143
 */
144
function bd_print_current_plugins() {
145
	$plugins        = get_plugins();
146
	$active_plugins = get_option( 'active_plugins', array() );
147
148
	foreach ( $plugins as $plugin_path => $plugin ) {
149
		// If the plugin isn't active, don't show it.
150
		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

150
		if ( ! in_array( $plugin_path, /** @scrutinizer ignore-type */ $active_plugins ) ) {
Loading history...
151
			continue;
152
		}
153
154
		echo $plugin['Name'] . ': ' . $plugin['Version'] . "\n";
155
	}
156
}
157
158
/**
159
 * Print network active plugins.
160
 *
161
 * @since 5.5.4
162
 */
163
function bd_print_network_active_plugins() {
164
	$plugins        = wp_get_active_network_plugins();
165
	$active_plugins = get_site_option( 'active_sitewide_plugins', array() );
166
167
	foreach ( $plugins as $plugin_path ) {
168
		$plugin_base = plugin_basename( $plugin_path );
169
170
		// If the plugin isn't active, don't show it.
171
		if ( ! array_key_exists( $plugin_base, $active_plugins ) ) {
172
			continue;
173
		}
174
175
		$plugin = get_plugin_data( $plugin_path );
176
177
		echo $plugin['Name'] . ' :' . $plugin['Version'] . "\n";
178
	}
179
}
180
181
/**
182
 * Print scheduled jobs.
183
 *
184
 * @since 6.0
185
 */
186
function bd_print_scheduled_jobs() {
187
	$cron        = _get_cron_array();
188
	$date_format = _x( 'M j, Y @ G:i', 'Cron table date format', 'bulk-delete' );
189
190
	foreach ( $cron as $timestamp => $cronhooks ) {
191
		foreach ( (array) $cronhooks as $hook => $events ) {
192
			if ( 'do-bulk-delete-' === substr( $hook, 0, 15 ) ) {
193
				$cron_item = array();
0 ignored issues
show
Unused Code introduced by
The assignment to $cron_item is dead and can be removed.
Loading history...
194
195
				foreach ( (array) $events as $key => $event ) {
196
					echo date_i18n( $date_format, $timestamp + ( get_option( 'gmt_offset' ) * 60 * 60 ) ) . ' (' . $timestamp . ')';
197
					echo ' | ';
198
					echo $event['schedule'];
199
					echo ' | ';
200
					echo $hook;
201
					echo "\n";
202
				}
203
			}
204
		}
205
	}
206
}
207