These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Add a new Debug Bar Panel. |
||
| 5 | */ |
||
| 6 | class ZT_Debug_Bar_Cron extends Debug_Bar_Panel { |
||
| 7 | |||
| 8 | /** |
||
| 9 | * Holds all of the cron events. |
||
| 10 | * |
||
| 11 | * @var array |
||
| 12 | */ |
||
| 13 | private $_crons; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * Holds only the cron events initiated by WP core. |
||
| 17 | * |
||
| 18 | * @var array |
||
| 19 | */ |
||
| 20 | private $_core_crons; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Holds the cron events created by plugins or themes. |
||
| 24 | * |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | private $_user_crons; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Total number of cron events |
||
| 31 | * |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | private $_total_crons = 0; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Whether cron is being executed or not. |
||
| 38 | * |
||
| 39 | * @var string |
||
| 40 | */ |
||
| 41 | private $_doing_cron = 'No'; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Give the panel a title and set the enqueues. |
||
| 45 | * |
||
| 46 | * @return void |
||
| 47 | */ |
||
| 48 | public function init() { |
||
| 49 | $this->title( __( 'Cron', 'debug-bar' ) ); |
||
| 50 | add_action( 'wp_print_styles', array( $this, 'print_styles' ) ); |
||
| 51 | add_action( 'admin_print_styles', array( $this, 'print_styles' ) ); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Enqueue styles. |
||
| 56 | * |
||
| 57 | * @return void |
||
| 58 | */ |
||
| 59 | public function print_styles() { |
||
| 60 | $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : ''; |
||
| 61 | wp_enqueue_style( 'zt-debug-bar-cron', plugins_url( "css/debug-bar-cron$suffix.css", __FILE__ ), array(), '20131228' ); |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Show the menu item in Debug Bar. |
||
| 66 | * |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | public function prerender() { |
||
| 70 | $this->set_visible( true ); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Show the contents of the page. |
||
| 75 | |||
| 76 | * @return void |
||
| 77 | */ |
||
| 78 | public function render() { |
||
| 79 | $this->get_crons(); |
||
| 80 | |||
| 81 | $this->_doing_cron = get_transient( 'doing_cron' ) ? __( 'Yes', 'zt-debug-bar-cron' ) : __( 'No', 'zt-debug-bar-cron' ); |
||
| 82 | |||
| 83 | // Get the time of the next event |
||
| 84 | $cron_times = array_keys( $this->_crons ); |
||
| 85 | $unix_time_next_cron = $cron_times[0]; |
||
| 86 | $time_next_cron = date( 'Y-m-d H:i:s', $unix_time_next_cron ); |
||
| 87 | |||
| 88 | $human_time_next_cron = human_time_diff( $unix_time_next_cron ); |
||
| 89 | |||
| 90 | // Add a class if past current time and doing cron is not running |
||
| 91 | $times_class = time() > $unix_time_next_cron && 'No' == $this->_doing_cron ? ' past' : ''; |
||
| 92 | |||
| 93 | echo '<div class="debug-bar-cron">'; |
||
| 94 | echo '<h2><span>' . __( 'Total Events', 'zt-debug-bar-cron' ) . ':</span>' . (int) $this->_total_crons . '</h2>'; |
||
| 95 | echo '<h2><span>' . __( 'Doing Cron', 'zt-debug-bar-cron' ) . ':</span>' . $this->_doing_cron . '</h2>'; |
||
| 96 | echo '<h2 class="times' . esc_attr( $times_class ) . '"><span>' . __( 'Next Event', 'zt-debug-bar-cron' ) . ':</span>' . $time_next_cron . '<br />' . $unix_time_next_cron . '<br />' . $human_time_next_cron . $this->display_past_time( $unix_time_next_cron ) . '</h2>'; |
||
| 97 | echo '<h2><span>' . __( 'Current Time', 'zt-debug-bar-cron' ) . ':</span>' . date( 'H:i:s' ) . '</h2>'; |
||
| 98 | echo '<div class="clear"></div>'; |
||
| 99 | |||
| 100 | echo '<h3>' . __( 'Custom Events', 'zt-debug-bar-cron' ) . '</h3>'; |
||
| 101 | |||
| 102 | View Code Duplication | if ( ! is_null( $this->_user_crons ) ) |
|
| 103 | $this->display_events( $this->_user_crons ); |
||
| 104 | else |
||
| 105 | echo '<p>' . __( 'No Custom Events scheduled.', 'zt-debug-bar-cron' ) . '</p>'; |
||
| 106 | |||
| 107 | echo '<h3>' . __( 'Schedules', 'zt-debug-bar-cron' ) . '</h3>'; |
||
| 108 | |||
| 109 | $this->display_schedules(); |
||
| 110 | |||
| 111 | echo '<h3>' . __( 'Core Events', 'zt-debug-bar-cron' ) . '</h3>'; |
||
| 112 | |||
| 113 | View Code Duplication | if ( ! is_null( $this->_core_crons ) ) |
|
| 114 | $this->display_events( $this->_core_crons ); |
||
| 115 | else |
||
| 116 | echo '<p>' . __( 'No Core Events scheduled.', 'zt-debug-bar-cron' ) . '</p>'; |
||
| 117 | |||
| 118 | echo '</div>'; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Gets all of the cron jobs. |
||
| 123 | * |
||
| 124 | * This function sorts the cron jobs into core crons, and custom crons. It also tallies |
||
| 125 | * a total count for the crons as this number is otherwise tough to get. |
||
| 126 | * |
||
| 127 | * @return array Array of crons. |
||
| 128 | */ |
||
| 129 | private function get_crons() { |
||
| 130 | if ( ! is_null( $this->_crons ) ) |
||
| 131 | return $this->_crons; |
||
| 132 | |||
| 133 | if ( ! $crons = _get_cron_array() ) |
||
| 134 | return $this->_crons; |
||
| 135 | |||
| 136 | $this->_crons = $crons; |
||
| 137 | |||
| 138 | // Lists all crons that are defined in WP Core |
||
| 139 | $core_cron_hooks = array( |
||
| 140 | 'wp_scheduled_delete', |
||
| 141 | 'upgrader_scheduled_cleanup', |
||
| 142 | 'importer_scheduled_cleanup', |
||
| 143 | 'publish_future_post', |
||
| 144 | 'akismet_schedule_cron_recheck', |
||
| 145 | 'akismet_scheduled_delete', |
||
| 146 | 'do_pings', |
||
| 147 | 'wp_version_check', |
||
| 148 | 'wp_update_plugins', |
||
| 149 | 'wp_update_themes' |
||
| 150 | ); |
||
| 151 | |||
| 152 | // Sort and count crons |
||
| 153 | foreach ( $this->_crons as $time => $time_cron_array ) { |
||
| 154 | foreach ( $time_cron_array as $hook => $data ) { |
||
| 155 | $this->_total_crons++; |
||
| 156 | |||
| 157 | if ( in_array( $hook, $core_cron_hooks ) ) |
||
| 158 | $this->_core_crons[ $time ][ $hook ] = $data; |
||
| 159 | else |
||
| 160 | $this->_user_crons[ $time ][ $hook ] = $data; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | |||
| 164 | return $this->_crons; |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Displays the events in an easy to read table. |
||
| 169 | * |
||
| 170 | * @param array $events Array of events. |
||
| 171 | * @return void|string Void on failure; table display of events on success. |
||
| 172 | */ |
||
| 173 | private function display_events( $events ) { |
||
| 174 | if ( is_null( $events ) || empty( $events ) ) |
||
| 175 | return; |
||
| 176 | |||
| 177 | echo '<table class="zt-debug-bar-cron-event-table" cellspacing="0">'; |
||
| 178 | echo '<thead><tr>'; |
||
| 179 | echo '<th class="col1">' . __( 'Next Execution', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 180 | echo '<th class="col2">' . __( 'Hook', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 181 | echo '<th class="col3">' . __( 'Interval Hook', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 182 | echo '<th class="col4">' . __( 'Interval Value', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 183 | echo '<th class="col5">' . __( 'Args', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 184 | echo '</tr></thead>'; |
||
| 185 | echo '<tbody>'; |
||
| 186 | |||
| 187 | foreach ( $events as $time => $time_cron_array ) { |
||
| 188 | foreach ( $time_cron_array as $hook => $data ) { |
||
| 189 | // Add a class if past current time |
||
| 190 | $times_class = time() > $time && 'No' == $this->_doing_cron ? ' class="past"' : ''; |
||
| 191 | |||
| 192 | echo '<tr>'; |
||
| 193 | echo '<td' . $times_class . '>' . date( 'Y-m-d H:i:s', $time ) . '<br />' . $time . '<br />' . human_time_diff( $time ) . $this->display_past_time( $time ) . '</td>'; |
||
| 194 | echo '<td>' . wp_strip_all_tags( $hook ) . '</td>'; |
||
| 195 | |||
| 196 | foreach ( $data as $hash => $info ) { |
||
| 197 | // Report the schedule |
||
| 198 | echo '<td>'; |
||
| 199 | if ( $info['schedule'] ) |
||
| 200 | echo wp_strip_all_tags( $info['schedule'] ); |
||
| 201 | else |
||
| 202 | echo 'Single Event'; |
||
| 203 | echo '</td>'; |
||
| 204 | |||
| 205 | // Report the interval |
||
| 206 | echo '<td>'; |
||
| 207 | if ( isset( $info['interval'] ) ) { |
||
| 208 | echo wp_strip_all_tags( $info['interval'] ) . 's<br />'; |
||
| 209 | echo $info['interval'] / 60 . 'm<br />'; |
||
| 210 | echo $info['interval'] / ( 60 * 60 ). 'h'; |
||
| 211 | } else { |
||
| 212 | echo 'Single Event'; |
||
| 213 | } |
||
| 214 | echo '</td>'; |
||
| 215 | |||
| 216 | // Report the args |
||
| 217 | echo '<td>'; |
||
| 218 | if ( is_array( $info['args'] ) && ! empty( $info['args'] ) ) { |
||
| 219 | foreach ( $info['args'] as $key => $value ) { |
||
| 220 | $this->display_cron_arguments( $key, $value ); |
||
| 221 | } |
||
| 222 | } else if ( is_string( $info['args'] ) && $info['args'] !== '' ) { |
||
| 223 | echo esc_html( $info['args'] ); |
||
| 224 | } else { |
||
| 225 | echo 'No Args'; |
||
| 226 | } |
||
| 227 | echo '</td>'; |
||
| 228 | } |
||
| 229 | |||
| 230 | echo '</tr>'; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | |||
| 234 | echo '</tbody>'; |
||
| 235 | echo '</table>'; |
||
| 236 | } |
||
| 237 | |||
| 238 | |||
| 239 | /** |
||
| 240 | * Displays the cron arguments in a readable format. |
||
| 241 | * |
||
| 242 | * @param int|string $key Key of the array element. |
||
| 243 | * @param mixed $value Cron argument(s). |
||
| 244 | * @param int $depth Current recursion depth. |
||
| 245 | * @return void |
||
| 246 | */ |
||
| 247 | function display_cron_arguments( $key, $value, $depth = 0 ) { |
||
|
0 ignored issues
–
show
|
|||
| 248 | if ( is_string( $value ) || is_int( $value ) ) { |
||
| 249 | echo str_repeat( ' ', ( $depth * 2 ) ) . wp_strip_all_tags( $key ) . ' => ' . esc_html( $value ) . '<br />'; |
||
| 250 | } else if ( is_array( $value ) ) { |
||
| 251 | if ( count( $value ) > 0 ) { |
||
| 252 | echo str_repeat( ' ', ( $depth * 2 ) ) . wp_strip_all_tags( $key ) . ' => array(<br />'; |
||
| 253 | $depth++; |
||
| 254 | foreach ( $value as $k => $v ) { |
||
| 255 | $this->display_cron_arguments( $k, $v, $depth ); |
||
| 256 | } |
||
| 257 | echo str_repeat( ' ', ( ( $depth - 1 ) * 2 ) ) . ')'; |
||
| 258 | } else { |
||
| 259 | echo 'Empty Array'; |
||
| 260 | } |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Displays all of the schedules defined. |
||
| 266 | * |
||
| 267 | * @return void |
||
| 268 | */ |
||
| 269 | private function display_schedules() { |
||
| 270 | echo '<table class="zt-debug-bar-cron-event-table" cellspacing="0">'; |
||
| 271 | echo '<thead><tr>'; |
||
| 272 | echo '<th class="col1">' . __( 'Interval Hook', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 273 | echo '<th class="col2">' . __( 'Interval (S)', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 274 | echo '<th class="col3">' . __( 'Interval (M)', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 275 | echo '<th class="col4">' . __( 'Interval (H)', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 276 | echo '<th class="col5">' . __( 'Display Name', 'zt-debug-bar-cron' ) . '</th>'; |
||
| 277 | echo '</tr></thead>'; |
||
| 278 | echo '<tbody>'; |
||
| 279 | |||
| 280 | foreach ( wp_get_schedules() as $interval_hook => $data ) { |
||
| 281 | echo '<tr>'; |
||
| 282 | echo '<td>' . esc_html( $interval_hook ) . '</td>'; |
||
| 283 | echo '<td>' . wp_strip_all_tags( $data['interval'] ) . '</td>'; |
||
| 284 | echo '<td>' . wp_strip_all_tags( $data['interval'] ) / 60 . '</td>'; |
||
| 285 | echo '<td>' . wp_strip_all_tags( $data['interval'] ) / ( 60 * 60 ). '</td>'; |
||
| 286 | echo '<td>' . esc_html( $data['display'] ) . '</td>'; |
||
| 287 | echo '</tr>'; |
||
| 288 | } |
||
| 289 | |||
| 290 | echo '</tbody>'; |
||
| 291 | echo '</table>'; |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Compares time with current time and outputs 'ago' if current time is greater that even time. |
||
| 296 | * |
||
| 297 | * @param int $time Unix time of event. |
||
| 298 | * @return string |
||
| 299 | */ |
||
| 300 | private function display_past_time( $time ) { |
||
| 301 | return time() > $time ? ' ' . __( 'ago', 'zt-debug-bar-cron' ) : ''; |
||
| 302 | } |
||
| 303 | } |
||
| 304 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.