diff --git a/wp-cron-control.php b/wp-cron-control.php index 0106bd162a213b838074c908dbaf52c1a8c96305..8b4de060efd28abc7c5009a28cd4c7263c3788ac 100644 --- a/wp-cron-control.php +++ b/wp-cron-control.php @@ -11,30 +11,30 @@ class WP_Cron_Control { private static $__instance = NULL; - + private $settings = array(); private $default_settings = array(); private $settings_texts = array(); - + private $plugin_prefix = 'wpcroncontrol_'; private $plugin_name = 'WP-Cron Control'; private $settings_page_name ='WP-Cron Control Settings'; private $dashed_name = 'wp-cron-control'; private $js_version = '20110801'; private $css_version = '20110801'; - + private $define_global_secret = NULL; // if this is set, it's value will be used as secret instead of the option - + public function __construct() { global $blog_id; - + // this allows overwriting of the default secret with a value set in the code. Useful If you don't want to give control to users. if ( NULL <> $this->define_global_secret && !defined( 'WP_CRON_CONTROL_SECRET' ) ) define( 'WP_CRON_CONTROL_SECRET', $this->define_global_secret ); - + add_action( 'admin_init', array( &$this, 'register_setting' ) ); add_action( 'admin_menu', array( &$this, 'register_settings_page' ) ); - + /** * Default settings that will be used for the setup. You can alter these value with a simple filter such as this * add_filter( 'wpcroncontrol_default_settings', 'mywpcroncontrol_settings' ); @@ -48,7 +48,7 @@ class WP_Cron_Control { 'enable_scheduled_post_validation' => 0, 'secret_string' => md5( __FILE__ . $blog_id ), ) ); - + /** * Define fields that will be used on the options page * the array key is the field_name the array then describes the label, description and type of the field. possible values for field types are 'text' and 'yesno' for a text field or input fields or 'echo' for a simple output @@ -59,18 +59,18 @@ class WP_Cron_Control { 'secret_string' => array( 'label' => 'Secret string', 'desc' => 'The secret parameter that needs to be appended to wp-cron.php requests.', 'type' => 'text' ), 'enable_scheduled_post_validation' => array( 'label' => 'Enable scheduled post validation', 'desc' => 'In some rare cases it can happen that even when running wp-cron via a scheduled system cron job posts miss their schedule. This feature makes sure that there is a scheduled event for each scheduled post.', 'type' => 'yesno' ), ) ); - + $user_settings = get_option( $this->plugin_prefix . 'settings' ); if ( false === $user_settings ) $user_settings = array(); - + // after getting default settings make sure to parse the arguments together with the user settings - $this->settings = wp_parse_args( $user_settings, $this->default_settings ); - + $this->settings = wp_parse_args( $user_settings, $this->default_settings ); + /** * If you define( 'WP_CRON_CONTROL_SECRET', 'my_super_secret_string' ); in your wp-config.php or your theme then * users are not allowed to change the secret, so we output the existing secret string rather than allowing to add a new one - */ + */ if ( defined( 'WP_CRON_CONTROL_SECRET' ) ) { $this->settings_texts['secret_string']['type'] = 'echo'; $this->settings_texts['secret_string']['desc'] = $this->settings_texts['secret_string']['desc'] . " Cannot be changed as it is defined via WP_CRON_CONTROL_SECRET"; @@ -78,18 +78,18 @@ class WP_Cron_Control { } } - - public static function init() { + + public static function init() { if ( 1 == self::instance()->settings['enable'] ) { - } + } self::instance()->prepare(); } - + /* * Use this singleton to address methods */ public static function instance() { - if ( self::$__instance == NULL ) + if ( self::$__instance == NULL ) self::$__instance = new WP_Cron_Control; return self::$__instance; } @@ -105,7 +105,7 @@ class WP_Cron_Control { */ if ( file_exists( dirname( __FILE__ ) . "/js/" . $this->dashed_name . ".js" ) ) wp_enqueue_script( $this->dashed_name, plugins_url( "js/" . $this->dashed_name . ".js", __FILE__ ), array(), $this->js_version, true ); - + /** * When the plugin is enabled make sure remove the default behavior for issueing wp-cron requests and add our own method * see: http://core.trac.wordpress.org/browser/trunk/wp-includes/default-filters.php#L236 @@ -115,7 +115,7 @@ class WP_Cron_Control { remove_action( 'sanitize_comment_cookies', 'wp_cron' ); add_action( 'init', array( &$this, 'validate_cron_request' ) ); } - + } public function register_settings_page() { @@ -125,7 +125,7 @@ class WP_Cron_Control { public function register_setting() { register_setting( $this->plugin_prefix . 'settings', $this->plugin_prefix . 'settings', array( &$this, 'validate_settings') ); } - + public function validate_settings( $settings ) { // reset to defaults if ( !empty( $_POST[ $this->dashed_name . '-defaults'] ) ) { @@ -133,12 +133,12 @@ class WP_Cron_Control { $_REQUEST['_wp_http_referer'] = add_query_arg( 'defaults', 'true', $_REQUEST['_wp_http_referer'] ); // or do some custom validations } else { - + } return $settings; } - - public function settings_page() { + + public function settings_page() { if ( !current_user_can( 'manage_options' ) ) { wp_die( __( 'You do not permission to access this page' ) ); } @@ -146,11 +146,11 @@ class WP_Cron_Control { <div class="wrap"> <?php if ( function_exists('screen_icon') ) screen_icon(); ?> <h2><?php echo $this->settings_page_name; ?></h2> - + <form method="post" action="options.php"> - + <?php settings_fields( $this->plugin_prefix . 'settings' ); ?> - + <table class="form-table"> <?php foreach( $this->settings as $setting => $value): ?> <tr valign="top"> @@ -158,14 +158,14 @@ class WP_Cron_Control { <td> <?php /** - * Implement various handlers for the different types of fields. This could be easily extended to allow for drop-down boxes, textareas and more + * Implement various handlers for the different types of fields. This could be easily extended to allow for drop-down boxes, textareas and more */ ?> <?php switch( $this->settings_texts[$setting]['type'] ): case 'yesno': ?> <select name="<?php echo $this->plugin_prefix; ?>settings[<?php echo $setting; ?>]" id="<?php echo $this->dashed_name . '-' . $setting; ?>" class="postform"> - <?php - $yesno = array( 0 => 'No', 1 => 'Yes' ); + <?php + $yesno = array( 0 => 'No', 1 => 'Yes' ); foreach ( $yesno as $val => $txt ) { echo '<option value="' . esc_attr( $val ) . '"' . selected( $value, $val, false ) . '>' . esc_html( $txt ) . " </option>\n"; } @@ -203,7 +203,7 @@ class WP_Cron_Control { </tr> <?php endif; ?> </table> - + <p class="submit"> <?php if ( function_exists( 'submit_button' ) ) { @@ -216,13 +216,13 @@ class WP_Cron_Control { } ?> </p> - + </form> </div> - + <?php } - + /** * Alternative function to the current wp_cron function that would usually executed on sanitize_comment_cookies */ @@ -232,38 +232,38 @@ class WP_Cron_Control { // grab the necessary secret string if ( defined( 'WP_CRON_CONTROL_SECRET' ) ) $secret = WP_CRON_CONTROL_SECRET; - else + else $secret = $this->settings['secret_string']; - + // make sure a secret string is provided in the ur if ( isset( $_GET[$secret] ) ) { - + // check if there is already a cron request running $local_time = time(); if ( function_exists( '_get_cron_lock' ) ) $flag = _get_cron_lock(); else $flag = get_transient('doing_cron'); - + if ( defined( 'WP_CRON_LOCK_TIMEOUT' ) ) $timeout = WP_CRON_LOCK_TIMEOUT; - else + else $timeout = 60; - + if ( $flag > $local_time + 10 * $timeout ) $flag = 0; - + // don't run if another process is currently running it or more than once every 60 sec. if ( $flag + $timeout > $local_time ) die( 'another cron process running or previous not older than 60 secs' ); - + // set a transient to allow locking down parallel requests set_transient( 'doing_cron', $local_time ); - + // make sure the request also validates in wp-cron.php global $doing_wp_cron; $doing_wp_cron = $local_time; - + // if settings allow it validate if there are any scheduled posts without a cron event if ( 1 == self::instance()->settings['enable_scheduled_post_validation'] ) { $this->validate_scheduled_posts(); @@ -273,20 +273,20 @@ class WP_Cron_Control { // something went wrong die( 'invalid secret string' ); } - + // for all other cases disable wp-cron.php and spawn_cron() by telling the system it's already running if ( !defined( 'DOING_CRON' ) ) define( 'DOING_CRON', true ); - + // and also disable the wp_cron() call execution if ( !defined( 'DISABLE_WP_CRON' ) ) define( 'DISABLE_WP_CRON', true ); return false; } - + public function validate_scheduled_posts() { global $wpdb; - + // grab all scheduled posts from posts table $sql = $wpdb->prepare( "SELECT ID, post_date_gmt FROM $wpdb->posts WHERE post_status = 'future' " ); $results = $wpdb->get_results( $sql ); @@ -300,7 +300,7 @@ class WP_Cron_Control { foreach ( $results as $r ) { $gmt_time = strtotime( $r->post_date_gmt . ' GMT' ); - + // grab the scheduled job for this post $timestamp = wp_next_scheduled( 'publish_future_post', array( (int) $r->ID ) ); if ( $timestamp === false ) { @@ -320,7 +320,7 @@ class WP_Cron_Control { } } } - + return $return; } } @@ -342,7 +342,7 @@ function wp_cron_control_call_cron( $blog_address, $secret ) { if ( defined('ABSPATH') ) { WP_Cron_Control::init(); } else { - // otherwise parse the arguments and call the cron. + // otherwise parse the arguments and call the cron. if ( !empty( $argv ) && $argv[0] == basename( __FILE__ ) || $argv[0] == __FILE__ ) { if ( isset( $argv[1] ) && isset( $argv[2] ) ) { wp_cron_control_call_cron( $argv[1], $argv[2] ); @@ -350,7 +350,7 @@ if ( defined('ABSPATH') ) { echo "Usage: php " . __FILE__ . " <blog_address> <secret_string>\n"; echo "Example: php " . __FILE__ . " http://my.blog.com efe18b0e53498e737da9b91cf4ca3d25\n"; exit; - } + } } }