& John Blackbourn * @copyright Copyright 2008 Edward Dale, 2012-2017 John Blackbourn * @license http://www.gnu.org/licenses/gpl.txt GPL 2.0 * @link https://wordpress.org/plugins/wp-crontrol/ * @since 0.2 */ defined( 'ABSPATH' ) or die(); class Crontrol { /** * Hook onto all of the actions and filters needed by the plugin. */ protected function __construct() { $plugin_file = plugin_basename( __FILE__ ); add_action( 'init', array( $this, 'action_init' ) ); add_action( 'init', array( $this, 'action_handle_posts' ) ); add_action( 'admin_menu', array( $this, 'action_admin_menu' ) ); add_filter( "plugin_action_links_{$plugin_file}", array( $this, 'plugin_action_links' ), 10, 4 ); add_filter( 'removable_query_args', array( $this, 'filter_removable_query_args' ) ); add_action( 'load-tools_page_crontrol_admin_manage_page', array( $this, 'enqueue_code_editor' ) ); register_activation_hook( __FILE__, array( $this, 'action_activate' ) ); add_filter( 'cron_schedules', array( $this, 'filter_cron_schedules' ) ); add_action( 'crontrol_cron_job', array( $this, 'action_php_cron_event' ) ); } /** * Evaluates the provided code using eval. */ public function action_php_cron_event( $code ) { eval( $code ); // @codingStandardsIgnoreLine } /** * Run using the 'init' action. */ public function action_init() { load_plugin_textdomain( 'wp-crontrol', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); } /** * Handles any POSTs made by the plugin. Run using the 'init' action. */ public function action_handle_posts() { if ( isset( $_POST['new_cron'] ) ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to add new cron events.', 'wp-crontrol' ) ); } check_admin_referer( 'new-cron' ); extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' ); if ( 'crontrol_cron_job' === $in_hookname && ! current_user_can( 'edit_files' ) ) { wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ) ); } $in_args = json_decode( $in_args, true ); $next_run = $in_next_run_date . ' ' . $in_next_run_time; $this->add_cron( $next_run, $in_schedule, $in_hookname, $in_args ); $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '5', 'crontrol_name' => rawurlencode( $in_hookname ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } elseif ( isset( $_POST['new_php_cron'] ) ) { if ( ! current_user_can( 'edit_files' ) ) { wp_die( esc_html__( 'You are not allowed to add new PHP cron events.', 'wp-crontrol' ) ); } check_admin_referer( 'new-cron' ); extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' ); $args = array( 'code' => $in_hookcode, 'name' => $in_eventname, ); $next_run = $in_next_run_date . ' ' . $in_next_run_time; $this->add_cron( $next_run, $in_schedule, 'crontrol_cron_job', $args ); $hookname = ( ! empty( $in_eventname ) ) ? $in_eventname : __( 'PHP Cron', 'wp-crontrol' ); $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '5', 'crontrol_name' => rawurlencode( $hookname ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } elseif ( isset( $_POST['edit_cron'] ) ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to edit cron events.', 'wp-crontrol' ) ); } extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' ); check_admin_referer( "edit-cron_{$in_original_hookname}_{$in_original_sig}_{$in_original_next_run}" ); if ( 'crontrol_cron_job' === $in_hookname && ! current_user_can( 'edit_files' ) ) { wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ) ); } $in_args = json_decode( $in_args, true ); $i = $this->delete_cron( $in_original_hookname, $in_original_sig, $in_original_next_run ); $next_run = $in_next_run_date . ' ' . $in_next_run_time; $i = $this->add_cron( $next_run, $in_schedule, $in_hookname, $in_args ); $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '4', 'crontrol_name' => rawurlencode( $in_hookname ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } elseif ( isset( $_POST['edit_php_cron'] ) ) { if ( ! current_user_can( 'edit_files' ) ) { wp_die( esc_html__( 'You are not allowed to edit PHP cron events.', 'wp-crontrol' ) ); } extract( wp_unslash( $_POST ), EXTR_PREFIX_ALL, 'in' ); check_admin_referer( "edit-cron_{$in_original_hookname}_{$in_original_sig}_{$in_original_next_run}" ); $args['code'] = $in_hookcode; $args['name'] = $in_eventname; $args = array( 'code' => $in_hookcode, 'name' => $in_eventname, ); $i = $this->delete_cron( $in_original_hookname, $in_original_sig, $in_original_next_run ); $next_run = $in_next_run_date . ' ' . $in_next_run_time; $i = $this->add_cron( $next_run, $in_schedule, 'crontrol_cron_job', $args ); $hookname = ( ! empty( $in_eventname ) ) ? $in_eventname : __( 'PHP Cron', 'wp-crontrol' ); $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '4', 'crontrol_name' => rawurlencode( $hookname ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } elseif ( isset( $_POST['new_schedule'] ) ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to add new cron schedules.', 'wp-crontrol' ) ); } check_admin_referer( 'new-sched' ); $name = wp_unslash( $_POST['internal_name'] ); $interval = wp_unslash( $_POST['interval'] ); $display = wp_unslash( $_POST['display_name'] ); // The user entered something that wasn't a number. // Try to convert it with strtotime if ( ! is_numeric( $interval ) ) { $now = time(); $future = strtotime( $interval, $now ); if ( false === $future || -1 == $future || $now > $future ) { $redirect = array( 'page' => 'crontrol_admin_options_page', 'crontrol_message' => '7', 'crontrol_name' => rawurlencode( $interval ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) ); exit; } $interval = $future - $now; } elseif ( $interval <= 0 ) { $redirect = array( 'page' => 'crontrol_admin_options_page', 'crontrol_message' => '7', 'crontrol_name' => rawurlencode( $interval ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) ); exit; } $this->add_schedule( $name, $interval, $display ); $redirect = array( 'page' => 'crontrol_admin_options_page', 'crontrol_message' => '3', 'crontrol_name' => rawurlencode( $name ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) ); exit; } elseif ( isset( $_GET['action'] ) && 'delete-sched' == $_GET['action'] ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to delete cron schedules.', 'wp-crontrol' ) ); } $id = wp_unslash( $_GET['id'] ); check_admin_referer( "delete-sched_{$id}" ); $this->delete_schedule( $id ); $redirect = array( 'page' => 'crontrol_admin_options_page', 'crontrol_message' => '2', 'crontrol_name' => rawurlencode( $id ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'options-general.php' ) ) ); exit; } elseif ( isset( $_POST['delete_crons'] ) ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ) ); } check_admin_referer( 'bulk-delete-crons' ); if ( empty( $_POST['delete'] ) ) { return; } $delete = wp_unslash( $_POST['delete'] ); $deleted = 0; foreach ( $delete as $next_run => $events ) { foreach ( $events as $id => $sig ) { if ( 'crontrol_cron_job' === $id && ! current_user_can( 'edit_files' ) ) { continue; } if ( $this->delete_cron( urldecode( $id ), $sig, $next_run ) ) { $deleted++; } } } $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_name' => $deleted, 'crontrol_message' => '9', ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } elseif ( isset( $_GET['action'] ) && 'delete-cron' == $_GET['action'] ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to delete cron events.', 'wp-crontrol' ) ); } $id = wp_unslash( $_GET['id'] ); $sig = wp_unslash( $_GET['sig'] ); $next_run = intval( $_GET['next_run'] ); check_admin_referer( "delete-cron_{$id}_{$sig}_{$next_run}" ); if ( 'crontrol_cron_job' === $id && ! current_user_can( 'edit_files' ) ) { wp_die( esc_html__( 'You are not allowed to delete PHP cron events.', 'wp-crontrol' ) ); } if ( $this->delete_cron( $id, $sig, $next_run ) ) { $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '6', 'crontrol_name' => rawurlencode( $id ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } else { $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '7', 'crontrol_name' => rawurlencode( $id ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; }; } elseif ( isset( $_GET['action'] ) && 'run-cron' == $_GET['action'] ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to run cron events.', 'wp-crontrol' ) ); } $id = wp_unslash( $_GET['id'] ); $sig = wp_unslash( $_GET['sig'] ); check_admin_referer( "run-cron_{$id}_{$sig}" ); if ( $this->run_cron( $id, $sig ) ) { $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '1', 'crontrol_name' => rawurlencode( $id ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } else { $redirect = array( 'page' => 'crontrol_admin_manage_page', 'crontrol_message' => '8', 'crontrol_name' => rawurlencode( $id ), ); wp_safe_redirect( add_query_arg( $redirect, admin_url( 'tools.php' ) ) ); exit; } } } /** * Executes a cron event immediately. * * Executes an event by scheduling a new single event with the same arguments. * * @param string $hookname The hookname of the cron event to run */ public function run_cron( $hookname, $sig ) { $crons = _get_cron_array(); foreach ( $crons as $time => $cron ) { if ( isset( $cron[ $hookname ][ $sig ] ) ) { $args = $cron[ $hookname ][ $sig ]['args']; delete_transient( 'doing_cron' ); wp_schedule_single_event( time() - 1, $hookname, $args ); spawn_cron(); return true; } } return false; } /** * Adds a new cron event. * * @param string $next_run A human-readable (strtotime) time that the event should be run at, in the local timezone * @param string $schedule The recurrence of the cron event * @param string $hookname The name of the hook to execute * @param array $args Arguments to add to the cron event */ public function add_cron( $next_run, $schedule, $hookname, $args ) { $next_run = strtotime( $next_run ); if ( false === $next_run ) { $next_run = time(); } else { $next_run = get_gmt_from_date( date( 'Y-m-d H:i:s', $next_run ), 'U' ); } if ( ! is_array( $args ) ) { $args = array(); } if ( '_oneoff' === $schedule ) { return wp_schedule_single_event( $next_run, $hookname, $args ) === null; } else { return wp_schedule_event( $next_run, $schedule, $hookname, $args ) === null; } } /** * Deletes a cron event. * * @param string $name The hookname of the event to delete. */ public function delete_cron( $to_delete, $sig, $next_run ) { $crons = _get_cron_array(); if ( isset( $crons[ $next_run ][ $to_delete ][ $sig ] ) ) { $args = $crons[ $next_run ][ $to_delete ][ $sig ]['args']; wp_unschedule_event( $next_run, $to_delete, $args ); return true; } return false; } /** * Adds a new custom cron schedule. * * @param string $name The internal name of the schedule * @param int $interval The interval between executions of the new schedule * @param string $display The display name of the schedule */ public function add_schedule( $name, $interval, $display ) { $old_scheds = get_option( 'crontrol_schedules', array() ); $old_scheds[ $name ] = array( 'interval' => $interval, 'display' => $display, ); update_option( 'crontrol_schedules', $old_scheds ); } /** * Deletes a custom cron schedule. * * @param string $name The internal_name of the schedule to delete. */ public function delete_schedule( $name ) { $scheds = get_option( 'crontrol_schedules', array() ); unset( $scheds[ $name ] ); update_option( 'crontrol_schedules', $scheds ); } /** * Sets up the plugin environment upon first activation. * * Run using the 'activate_' action. */ public function action_activate() { add_option( 'crontrol_schedules', array() ); // if there's never been a cron event, _get_cron_array will return false if ( _get_cron_array() === false ) { _set_cron_array( array() ); } } /** * Adds options & management pages to the admin menu. * * Run using the 'admin_menu' action. */ public function action_admin_menu() { add_options_page( esc_html__( 'Cron Schedules', 'wp-crontrol' ), esc_html__( 'Cron Schedules', 'wp-crontrol' ), 'manage_options', 'crontrol_admin_options_page', array( $this, 'admin_options_page' ) ); add_management_page( esc_html__( 'Cron Events', 'wp-crontrol' ), esc_html__( 'Cron Events', 'wp-crontrol' ), 'manage_options', 'crontrol_admin_manage_page', array( $this, 'admin_manage_page' ) ); } public function plugin_action_links( $actions, $plugin_file, $plugin_data, $context ) { $actions['crontrol-events'] = sprintf( '%s', esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ), esc_html__( 'Cron Events', 'wp-crontrol' ) ); $actions['crontrol-schedules'] = sprintf( '%s', esc_url( admin_url( 'options-general.php?page=crontrol_admin_options_page' ) ), esc_html__( 'Cron Schedules', 'wp-crontrol' ) ); return $actions; } /** * Gives WordPress the plugin's set of cron schedules. * * Called by the 'cron_schedules' filter. * * @param array $scheds The current cron schedules. Usually an empty array. * @return array The existing cron schedules along with the plugin's schedules. */ public function filter_cron_schedules( $scheds ) { $new_scheds = get_option( 'crontrol_schedules', array() ); return array_merge( $new_scheds, $scheds ); } /** * Displays the options page for the plugin. */ public function admin_options_page() { $schedules = $this->get_schedules(); $events = $this->get_cron_events(); $custom_schedules = get_option( 'crontrol_schedules', array() ); $custom_keys = array_keys( $custom_schedules ); if ( is_wp_error( $events ) ) { $events = array(); } $used_schedules = array_unique( wp_list_pluck( $events, 'schedule' ) ); $messages = array( /* translators: 1: The name of the cron schedule. */ '2' => __( 'Successfully deleted the cron schedule %s.', 'wp-crontrol' ), /* translators: 1: The name of the cron schedule. */ '3' => __( 'Successfully added the cron schedule %s.', 'wp-crontrol' ), /* translators: 1: The name of the cron schedule. */ '7' => __( 'Cron schedule not added because there was a problem parsing %s.', 'wp-crontrol' ), ); if ( isset( $_GET['crontrol_message'] ) && isset( $_GET['crontrol_name'] ) && isset( $messages[ $_GET['crontrol_message'] ] ) ) { $hook = wp_unslash( $_GET['crontrol_name'] ); $message = wp_unslash( $_GET['crontrol_message'] ); $msg = sprintf( esc_html( $messages[ $message ] ), '' . esc_html( $hook ) . '' ); printf( '

%s

', $msg ); // WPCS:: XSS ok. } ?>

$data ) { printf( '', esc_attr( $name ) ); printf( '', esc_html( $name ) ); printf( '', esc_html( $data['interval'] ), esc_html( $this->interval( $data['interval'] ) ) ); printf( '', esc_html( $data['display'] ) ); echo ''; echo ''; } } ?>
%s%s (%s)%s'; if ( in_array( $name, $custom_keys, true ) ) { if ( in_array( $name, $used_schedules, true ) ) { esc_html_e( 'This custom schedule is in use and cannot be deleted', 'wp-crontrol' ); } else { $url = add_query_arg( array( 'page' => 'crontrol_admin_options_page', 'action' => 'delete-sched', 'id' => rawurlencode( $name ), ), admin_url( 'options-general.php' ) ); $url = wp_nonce_url( $url, 'delete-sched_' . $name ); printf( '%s', esc_url( $url ), esc_html__( 'Delete', 'wp-crontrol' ) ); } } else { echo ' '; } echo '

%s', esc_url( admin_url( 'tools.php?page=crontrol_admin_manage_page' ) ), esc_html__( 'Manage Cron Events', 'wp-crontrol' ) ); ?>

get_schedules(); ?> site_url( 'wp-cron.php?doing_wp_cron=' . $doing_wp_cron ), 'key' => $doing_wp_cron, 'args' => array( 'timeout' => 3, 'blocking' => true, 'sslverify' => apply_filters( 'https_local_ssl_verify', $sslverify ), ), ) ); $cron_request['args']['blocking'] = true; $result = wp_remote_post( $cron_request['url'], $cron_request['args'] ); if ( is_wp_error( $result ) ) { return $result; } elseif ( wp_remote_retrieve_response_code( $result ) >= 300 ) { return new WP_Error( 'unexpected_http_response_code', sprintf( /* translators: 1: The HTTP response code. */ __( 'Unexpected HTTP response code: %s', 'wp-crontrol' ), intval( wp_remote_retrieve_response_code( $result ) ) ) ); } else { set_transient( 'wp-cron-test-ok', 1, 3600 ); return true; } } /** * Shows the status of WP-Cron functionality on the site. Only displays a message when there's a problem. * */ public function show_cron_status() { $status = $this->test_cron_spawn(); if ( is_wp_error( $status ) ) { if ( 'crontrol_info' === $status->get_error_code() ) { ?>

get_error_message() ); ?>

' . esc_html( $status->get_error_message() ) . '' ); ?>

= 0 ) { $gmt_offset = '+' . $gmt_offset; } if ( '' === $timezone_string ) { $name = sprintf( 'UTC%s', $gmt_offset ); } else { $name = sprintf( '%s (UTC%s)', str_replace( '_', ' ', $timezone_string ), $gmt_offset ); } return $name; } /** * Shows the form used to add/edit cron events. * * @param boolean $is_php Whether this is a PHP cron event * @param mixed $existing An array of existing values for the cron event, or null */ public function show_cron_form( $is_php, $existing ) { $new_tabs = array( 'cron' => __( 'Add Cron Event', 'wp-crontrol' ), 'php-cron' => __( 'Add PHP Cron Event', 'wp-crontrol' ), ); $modify_tabs = array( 'cron' => __( 'Modify Cron Event', 'wp-crontrol' ), 'php-cron' => __( 'Modify PHP Cron Event', 'wp-crontrol' ), ); $new_links = array( 'cron' => admin_url( 'tools.php?page=crontrol_admin_manage_page&action=new-cron' ) . '#crontrol_form', 'php-cron' => admin_url( 'tools.php?page=crontrol_admin_manage_page&action=new-php-cron' ) . '#crontrol_form', ); $display_args = ''; if ( $is_php ) { $helper_text = esc_html__( 'Cron events trigger actions in your code. Enter the schedule of the event, as well as the PHP code to execute when the action is triggered.', 'wp-crontrol' ); } else { $helper_text = sprintf( /* translators: %s: A file name */ esc_html__( 'Cron events trigger actions in your code. A cron event needs a corresponding action hook somewhere in code, e.g. the %1$s file in your theme.', 'wp-crontrol' ), 'functions.php' ); } if ( is_array( $existing ) ) { $other_fields = wp_nonce_field( "edit-cron_{$existing['hookname']}_{$existing['sig']}_{$existing['next_run']}", '_wpnonce', true, false ); $other_fields .= sprintf( '', esc_attr( $existing['hookname'] ) ); $other_fields .= sprintf( '', esc_attr( $existing['sig'] ) ); $other_fields .= sprintf( '', esc_attr( $existing['next_run'] ) ); if ( ! empty( $existing['args'] ) ) { $display_args = wp_json_encode( $existing['args'] ); } $action = $is_php ? 'edit_php_cron' : 'edit_cron'; $button = $is_php ? $modify_tabs['php-cron'] : $modify_tabs['cron']; $show_edit_tab = true; list( $next_run_date, $next_run_time ) = explode( ' ', get_date_from_gmt( date( 'Y-m-d H:i:s', $existing['next_run'] ), 'Y-m-d H:i:s' ) ); } else { $other_fields = wp_nonce_field( 'new-cron', '_wpnonce', true, false ); $existing = array( 'hookname' => '', 'args' => array(), 'next_run' => 'now', 'schedule' => false, ); $action = $is_php ? 'new_php_cron' : 'new_cron'; $button = $is_php ? $new_tabs['php-cron'] : $new_tabs['cron']; $show_edit_tab = false; $next_run_time = ''; $next_run_date = ''; } if ( $is_php ) { if ( ! isset( $existing['args']['code'] ) ) { $existing['args']['code'] = ''; } if ( ! isset( $existing['args']['name'] ) ) { $existing['args']['name'] = ''; } } $allowed = ( ! $is_php || current_user_can( 'edit_files' ) ); ?>

<?php' ); ?>

[25]', '["asdf"]', '["i","want",25,"cakes"]' ); ?>

' . esc_html( $this->get_timezone_name() ) . '' ); ?>

schedules_dropdown( $existing['schedule'] ); ?>

%s', esc_url( admin_url( 'options-general.php?page=crontrol_admin_options_page' ) ), esc_html__( 'Manage Cron Schedules', 'wp-crontrol' ) ); ?>

$cron ) { foreach ( $cron as $hook => $dings ) { foreach ( $dings as $sig => $data ) { # This is a prime candidate for a Crontrol_Event class but I'm not bothering currently. $events[ "$hook-$sig-$time" ] = (object) array( 'hook' => $hook, 'time' => $time, 'sig' => $sig, 'args' => $data['args'], 'schedule' => $data['schedule'], 'interval' => isset( $data['interval'] ) ? $data['interval'] : null, ); } } } return $events; } /** * Displays the manage page for the plugin. */ public function admin_manage_page() { $messages = array( /* translators: 1: The name of the cron event. */ '1' => __( 'Successfully executed the cron event %s.', 'wp-crontrol' ), /* translators: 1: The name of the cron event. */ '4' => __( 'Successfully edited the cron event %s.', 'wp-crontrol' ), /* translators: 1: The name of the cron event. */ '5' => __( 'Successfully created the cron event %s.', 'wp-crontrol' ), /* translators: 1: The name of the cron event. */ '6' => __( 'Successfully deleted the cron event %s.', 'wp-crontrol' ), /* translators: 1: The name of the cron event. */ '7' => __( 'Failed to the delete the cron event %s.', 'wp-crontrol' ), /* translators: 1: The name of the cron event. */ '8' => __( 'Failed to the execute the cron event %s.', 'wp-crontrol' ), '9' => __( 'Successfully deleted the selected cron events.', 'wp-crontrol' ), ); if ( isset( $_GET['crontrol_name'] ) && isset( $_GET['crontrol_message'] ) && isset( $messages[ $_GET['crontrol_message'] ] ) ) { $hook = wp_unslash( $_GET['crontrol_name'] ); $message = wp_unslash( $_GET['crontrol_message'] ); $msg = sprintf( esc_html( $messages[ $message ] ), '' . esc_html( $hook ) . '' ); printf( '

%s

', $msg ); // WPCS:: XSS ok. } $events = $this->get_cron_events(); $doing_edit = ( isset( $_GET['action'] ) && 'edit-cron' === $_GET['action'] ) ? wp_unslash( $_GET['id'] ) : false ; $time_format = 'Y-m-d H:i:s'; $can_edit_files = current_user_can( 'edit_files' ); $core_hooks = array( 'wp_version_check', 'wp_update_plugins', 'wp_update_themes', 'wp_scheduled_delete', 'wp_scheduled_auto_draft_delete', 'update_network_counts', 'delete_expired_transients', ); $this->show_cron_status(); ?>

$event ) { if ( $doing_edit && $doing_edit == $event->hook && $event->time == $_GET['next_run'] && $event->sig == $_GET['sig'] ) { $doing_edit = array( 'hookname' => $event->hook, 'next_run' => $event->time, 'schedule' => ( $event->schedule ? $event->schedule : '_oneoff' ), 'sig' => $event->sig, 'args' => $event->args, ); } if ( empty( $event->args ) ) { $args = '' . esc_html__( 'None', 'wp-crontrol' ) . ''; } else { $json_options = 0; if ( defined( 'JSON_UNESCAPED_SLASHES' ) ) { $json_options |= JSON_UNESCAPED_SLASHES; } if ( defined( 'JSON_PRETTY_PRINT' ) ) { $json_options |= JSON_PRETTY_PRINT; } $args = '
' . wp_json_encode( $event->args, $json_options ) . '
'; } echo ''; echo ''; if ( 'crontrol_cron_job' === $event->hook ) { if ( ! empty( $event->args['name'] ) ) { /* translators: 1: The name of the PHP cron event. */ echo ''; } else { echo ''; } echo ''; echo ''; } else { echo ''; echo ''; // WPCS:: XSS ok. echo ''; } echo ''; echo ''; $links = array(); echo ''; echo ''; } } ?>
get_error_message() ); ?>
'; if ( ! in_array( $event->hook, $core_hooks, true ) ) { printf( '', esc_attr( $event->time ), esc_attr( rawurlencode( $event->hook ) ), esc_attr( $event->sig ) ); } echo '' . esc_html( sprintf( __( 'PHP Cron (%s)', 'wp-crontrol' ), $event->args['name'] ) ) . '' . esc_html__( 'PHP Cron', 'wp-crontrol' ) . '' . esc_html__( 'PHP Code', 'wp-crontrol' ) . '' . esc_html__( 'WP Crontrol', 'wp-crontrol' ) . '' . esc_html( $event->hook ) . '' . $args . ''; $callbacks = array(); foreach ( $this->get_action_callbacks( $event->hook ) as $callback ) { $callbacks[] = '
' . self::output_callback( $callback ) . '
'; } echo implode( '', $callbacks ); // WPCS:: XSS ok. echo '
'; printf( '%s (%s)', esc_html( get_date_from_gmt( date( 'Y-m-d H:i:s', $event->time ), $time_format ) ), esc_html( $this->time_since( time(), $event->time ) ) ); echo ''; if ( $event->schedule ) { echo esc_html( $this->get_schedule_name( $event->interval ) ); } else { esc_html_e( 'Non-repeating', 'wp-crontrol' ); } echo ''; if ( ( 'crontrol_cron_job' !== $event->hook ) || $can_edit_files ) { $link = array( 'page' => 'crontrol_admin_manage_page', 'action' => 'edit-cron', 'id' => rawurlencode( $event->hook ), 'sig' => rawurlencode( $event->sig ), 'next_run' => rawurlencode( $event->time ), ); $link = add_query_arg( $link, admin_url( 'tools.php' ) ) . '#crontrol_form'; $links[] = "" . esc_html__( 'Edit', 'wp-crontrol' ) . ''; } $link = array( 'page' => 'crontrol_admin_manage_page', 'action' => 'run-cron', 'id' => rawurlencode( $event->hook ), 'sig' => rawurlencode( $event->sig ), 'next_run' => rawurlencode( $event->time ), ); $link = add_query_arg( $link, admin_url( 'tools.php' ) ); $link = wp_nonce_url( $link, "run-cron_{$event->hook}_{$event->sig}" ); $links[] = "" . esc_html__( 'Run Now', 'wp-crontrol' ) . ''; if ( ! in_array( $event->hook, $core_hooks, true ) && ( ( 'crontrol_cron_job' !== $event->hook ) || $can_edit_files ) ) { $link = array( 'page' => 'crontrol_admin_manage_page', 'action' => 'delete-cron', 'id' => rawurlencode( $event->hook ), 'sig' => rawurlencode( $event->sig ), 'next_run' => rawurlencode( $event->time ), ); $link = add_query_arg( $link, admin_url( 'tools.php' ) ); $link = wp_nonce_url( $link, "delete-cron_{$event->hook}_{$event->sig}_{$event->time}" ); $links[] = "" . esc_html__( 'Delete', 'wp-crontrol' ) . ''; } echo implode( ' | ', $links ); // WPCS:: XSS ok. echo '
show_cron_form( 'crontrol_cron_job' == $doing_edit['hookname'], $doing_edit ); } else { $this->show_cron_form( ( isset( $_GET['action'] ) and 'new-php-cron' === $_GET['action'] ), false ); } } protected function get_action_callbacks( $name ) { global $wp_filter; $actions = array(); if ( isset( $wp_filter[ $name ] ) ) { # http://core.trac.wordpress.org/ticket/17817 $action = $wp_filter[ $name ]; foreach ( $action as $priority => $callbacks ) { foreach ( $callbacks as $callback ) { $callback = self::populate_callback( $callback ); $actions[] = array( 'priority' => $priority, 'callback' => $callback, ); } } } return $actions; } public static function populate_callback( array $callback ) { // If Query Monitor is installed, use its rich callback analysis: if ( method_exists( 'QM_Util', 'populate_callback' ) ) { return QM_Util::populate_callback( $callback ); } if ( is_string( $callback['function'] ) && ( false !== strpos( $callback['function'], '::' ) ) ) { $callback['function'] = explode( '::', $callback['function'] ); } if ( is_array( $callback['function'] ) ) { if ( is_object( $callback['function'][0] ) ) { $class = get_class( $callback['function'][0] ); $access = '->'; } else { $class = $callback['function'][0]; $access = '::'; } $callback['name'] = $class . $access . $callback['function'][1] . '()'; } elseif ( is_object( $callback['function'] ) ) { if ( is_a( $callback['function'], 'Closure' ) ) { $callback['name'] = 'Closure'; } else { $class = get_class( $callback['function'] ); $callback['name'] = $class . '->__invoke()'; } } else { $callback['name'] = $callback['function'] . '()'; } return $callback; } public static function output_callback( array $callback ) { $qm = WP_PLUGIN_DIR . '/query-monitor/query-monitor.php'; $html = plugin_dir_path( $qm ) . 'output/Html.php'; // If Query Monitor is installed, use its rich callback output: if ( class_exists( 'QueryMonitor' ) && file_exists( $html ) ) { require_once $html; if ( class_exists( 'QM_Output_Html' ) ) { return QM_Output_Html::output_filename( $callback['callback']['name'], $callback['callback']['file'], $callback['callback']['line'] ); } } return $callback['callback']['name']; } /** * Pretty-prints the difference in two times. * * @param int $older_date Unix timestamp. * @param int $newer_date Unix timestamp. * @return string The pretty time_since value * @link http://binarybonsai.com/code/timesince.txt */ public function time_since( $older_date, $newer_date ) { return $this->interval( $newer_date - $older_date ); } /** * Converts a period of time in seconds into a human-readable format representing the interval. * * Example: * * echo self::interval( 90 ); * // 1 minute 30 seconds * * @param int $since A period of time in seconds. * @return string An interval represented as a string. */ public function interval( $since ) { // array of time period chunks $chunks = array( /* translators: 1: The number of years in an interval of time. */ array( 60 * 60 * 24 * 365, _n_noop( '%s year', '%s years', 'wp-crontrol' ) ), /* translators: 1: The number of months in an interval of time. */ array( 60 * 60 * 24 * 30, _n_noop( '%s month', '%s months', 'wp-crontrol' ) ), /* translators: 1: The number of weeks in an interval of time. */ array( 60 * 60 * 24 * 7, _n_noop( '%s week', '%s weeks', 'wp-crontrol' ) ), /* translators: 1: The number of days in an interval of time. */ array( 60 * 60 * 24, _n_noop( '%s day', '%s days', 'wp-crontrol' ) ), /* translators: 1: The number of hours in an interval of time. */ array( 60 * 60, _n_noop( '%s hour', '%s hours', 'wp-crontrol' ) ), /* translators: 1: The number of minutes in an interval of time. */ array( 60, _n_noop( '%s minute', '%s minutes', 'wp-crontrol' ) ), /* translators: 1: The number of seconds in an interval of time. */ array( 1, _n_noop( '%s second', '%s seconds', 'wp-crontrol' ) ), ); if ( $since <= 0 ) { return __( 'now', 'wp-crontrol' ); } // we only want to output two chunks of time here, eg: // x years, xx months // x days, xx hours // so there's only two bits of calculation below: $j = count( $chunks ); // step one: the first chunk for ( $i = 0; $i < $j; $i++ ) { $seconds = $chunks[ $i ][0]; $name = $chunks[ $i ][1]; // finding the biggest chunk (if the chunk fits, break) $count = floor( $since / $seconds ); if ( $count ) { break; } } // set output var $output = sprintf( translate_nooped_plural( $name, $count, 'wp-crontrol' ), $count ); // step two: the second chunk if ( $i + 1 < $j ) { $seconds2 = $chunks[ $i + 1 ][0]; $name2 = $chunks[ $i + 1 ][1]; $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ); if ( $count2 ) { // add to output var $output .= ' ' . sprintf( translate_nooped_plural( $name2, $count2, 'wp-crontrol' ), $count2 ); } } return $output; } /** * Returns the schedule display name for a given interval. * * Falls back to the time interval if no corresponding schedule exists. * * @param int $interval An interval of time. * @return string The interval display name. */ protected function get_schedule_name( $interval ) { $schedules = $this->get_schedules(); foreach ( $schedules as $schedule ) { if ( $interval === $schedule['interval'] ) { return $schedule['display']; } } return $this->interval( $interval ); } /** * Enqueues the editor UI that's used for the PHP cron event code editor. */ public function enqueue_code_editor() { if ( ! function_exists( 'wp_enqueue_code_editor' ) ) { return; } if ( ! current_user_can( 'edit_files' ) ) { return; } $settings = wp_enqueue_code_editor( array( 'type' => 'text/x-php', ) ); if ( false === $settings ) { return; } wp_add_inline_script( 'code-editor', sprintf( 'jQuery( function( $ ) { if ( $( "#hookcode" ).length ) { wp.codeEditor.initialize( "hookcode", %s ); } } );', wp_json_encode( $settings ) ) ); } /** * Filters the list of query arguments which get removed from admin area URLs in WordPress. * * @link https://core.trac.wordpress.org/ticket/23367 * * @param string[] $args List of removable query arguments. * @return string[] Updated list of removable query arguments. */ public function filter_removable_query_args( array $args ) { return array_merge( $args, array( 'crontrol_message', 'crontrol_name', ) ); } public static function init() { static $instance = null; if ( ! $instance ) { $instance = new Crontrol; } return $instance; } } // Get this show on the road Crontrol::init();