title( __( 'Cron', 'debug-bar' ) ); add_action( 'wp_print_styles', array( $this, 'print_styles' ) ); add_action( 'admin_print_styles', array( $this, 'print_styles' ) ); } /** * Enqueue styles. * * @return void */ public function print_styles() { $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '.dev' : ''; wp_enqueue_style( 'zt-debug-bar-cron', plugins_url( "css/debug-bar-cron$suffix.css", __FILE__ ), array(), '20131228' ); } /** * Show the menu item in Debug Bar. * * @return void */ public function prerender() { $this->set_visible( true ); } /** * Show the contents of the page. * @return void */ public function render() { $this->get_crons(); $this->_doing_cron = get_transient( 'doing_cron' ) ? __( 'Yes', 'zt-debug-bar-cron' ) : __( 'No', 'zt-debug-bar-cron' ); // Get the time of the next event $cron_times = array_keys( $this->_crons ); $unix_time_next_cron = $cron_times[0]; $time_next_cron = date( 'Y-m-d H:i:s', $unix_time_next_cron ); $human_time_next_cron = human_time_diff( $unix_time_next_cron ); // Add a class if past current time and doing cron is not running $times_class = time() > $unix_time_next_cron && 'No' == $this->_doing_cron ? ' past' : ''; echo '
'; echo '

' . __( 'Total Events', 'zt-debug-bar-cron' ) . ':' . (int) $this->_total_crons . '

'; echo '

' . __( 'Doing Cron', 'zt-debug-bar-cron' ) . ':' . $this->_doing_cron . '

'; echo '

' . __( 'Next Event', 'zt-debug-bar-cron' ) . ':' . $time_next_cron . '
' . $unix_time_next_cron . '
' . $human_time_next_cron . $this->display_past_time( $unix_time_next_cron ) . '

'; echo '

' . __( 'Current Time', 'zt-debug-bar-cron' ) . ':' . date( 'H:i:s' ) . '

'; echo '
'; echo '

' . __( 'Custom Events', 'zt-debug-bar-cron' ) . '

'; if ( ! is_null( $this->_user_crons ) ) $this->display_events( $this->_user_crons ); else echo '

' . __( 'No Custom Events scheduled.', 'zt-debug-bar-cron' ) . '

'; echo '

' . __( 'Schedules', 'zt-debug-bar-cron' ) . '

'; $this->display_schedules(); echo '

' . __( 'Core Events', 'zt-debug-bar-cron' ) . '

'; if ( ! is_null( $this->_core_crons ) ) $this->display_events( $this->_core_crons ); else echo '

' . __( 'No Core Events scheduled.', 'zt-debug-bar-cron' ) . '

'; echo '
'; } /** * Gets all of the cron jobs. * * This function sorts the cron jobs into core crons, and custom crons. It also tallies * a total count for the crons as this number is otherwise tough to get. * * @return array Array of crons. */ private function get_crons() { if ( ! is_null( $this->_crons ) ) return $this->_crons; if ( ! $crons = _get_cron_array() ) return $this->_crons; $this->_crons = $crons; // Lists all crons that are defined in WP Core $core_cron_hooks = array( 'wp_scheduled_delete', 'upgrader_scheduled_cleanup', 'importer_scheduled_cleanup', 'publish_future_post', 'akismet_schedule_cron_recheck', 'akismet_scheduled_delete', 'do_pings', 'wp_version_check', 'wp_update_plugins', 'wp_update_themes' ); // Sort and count crons foreach ( $this->_crons as $time => $time_cron_array ) { foreach ( $time_cron_array as $hook => $data ) { $this->_total_crons++; if ( in_array( $hook, $core_cron_hooks ) ) $this->_core_crons[ $time ][ $hook ] = $data; else $this->_user_crons[ $time ][ $hook ] = $data; } } return $this->_crons; } /** * Displays the events in an easy to read table. * * @param array $events Array of events. * @return void|string Void on failure; table display of events on success. */ private function display_events( $events ) { if ( is_null( $events ) || empty( $events ) ) return; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ( $events as $time => $time_cron_array ) { foreach ( $time_cron_array as $hook => $data ) { // Add a class if past current time $times_class = time() > $time && 'No' == $this->_doing_cron ? ' class="past"' : ''; echo ''; echo '' . date( 'Y-m-d H:i:s', $time ) . '
' . $time . '
' . human_time_diff( $time ) . $this->display_past_time( $time ) . ''; echo '
'; foreach ( $data as $hash => $info ) { // Report the schedule echo ''; // Report the interval echo ''; // Report the args echo ''; } echo ''; } } echo ''; echo '
' . __( 'Next Execution', 'zt-debug-bar-cron' ) . '' . __( 'Hook', 'zt-debug-bar-cron' ) . '' . __( 'Interval Hook', 'zt-debug-bar-cron' ) . '' . __( 'Interval Value', 'zt-debug-bar-cron' ) . '' . __( 'Args', 'zt-debug-bar-cron' ) . '
' . wp_strip_all_tags( $hook ) . ''; if ( $info['schedule'] ) echo wp_strip_all_tags( $info['schedule'] ); else echo 'Single Event'; echo ''; if ( isset( $info['interval'] ) ) { echo wp_strip_all_tags( $info['interval'] ) . 's
'; echo $info['interval'] / 60 . 'm
'; echo $info['interval'] / ( 60 * 60 ). 'h'; } else { echo 'Single Event'; } echo '
'; if ( is_array( $info['args'] ) && ! empty( $info['args'] ) ) { foreach ( $info['args'] as $key => $value ) { $this->display_cron_arguments( $key, $value ); } } else if ( is_string( $info['args'] ) && $info['args'] !== '' ) { echo esc_html( $info['args'] ); } else { echo 'No Args'; } echo '
'; } /** * Displays the cron arguments in a readable format. * * @param int|string $key Key of the array element. * @param mixed $value Cron argument(s). * @param int $depth Current recursion depth. * @return void */ function display_cron_arguments( $key, $value, $depth = 0 ) { if ( is_string( $value ) ) { echo str_repeat( ' ', ( $depth * 2 ) ) . wp_strip_all_tags( $key ) . ' => ' . esc_html( $value ) . '
'; } else if ( is_array( $value ) ) { if ( count( $value ) > 0 ) { echo str_repeat( ' ', ( $depth * 2 ) ) . wp_strip_all_tags( $key ) . ' => array(
'; $depth++; foreach ( $value as $k => $v ) { $this->display_cron_arguments( $k, $v, $depth ); } echo str_repeat( ' ', ( ( $depth - 1 ) * 2 ) ) . ')'; } else { echo 'Empty Array'; } } } /** * Displays all of the schedules defined. * * @return void */ private function display_schedules() { echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; foreach ( wp_get_schedules() as $interval_hook => $data ) { echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; echo ''; } echo ''; echo '
' . __( 'Interval Hook', 'zt-debug-bar-cron' ) . '' . __( 'Interval (S)', 'zt-debug-bar-cron' ) . '' . __( 'Interval (M)', 'zt-debug-bar-cron' ) . '' . __( 'Interval (H)', 'zt-debug-bar-cron' ) . '' . __( 'Display Name', 'zt-debug-bar-cron' ) . '
' . esc_html( $interval_hook ) . '' . wp_strip_all_tags( $data['interval'] ) . '' . wp_strip_all_tags( $data['interval'] ) / 60 . '' . wp_strip_all_tags( $data['interval'] ) / ( 60 * 60 ). '' . esc_html( $data['display'] ) . '
'; } /** * Compares time with current time and outputs 'ago' if current time is greater that even time. * * @param int $time Unix time of event. * @return string */ private function display_past_time( $time ) { return time() > $time ? ' ' . __( 'ago', 'zt-debug-bar-cron' ) : ''; } }