Changeset 459746
- Timestamp:
- 11/05/2011 08:11:27 PM (14 years ago)
- Location:
- wp-cron-control/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (1 diff)
-
wp-cron-control.php (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
wp-cron-control/trunk/readme.txt
r424635 r459746 31 31 == ChangeLog == 32 32 33 = Version 0.4 = 34 35 * Implementing feedback from Yoast http://yoast.com/wp-plugin-review/wp-cron-control/, fixing button classes, more inline comments 36 33 37 = Version 0.3 = 34 38 -
wp-cron-control/trunk/wp-cron-control.php
r424635 r459746 5 5 Description: get control over wp-cron execution. 6 6 Author: Thorsten Ott, Automattic 7 Version: 0. 37 Version: 0.4 8 8 Author URI: http://hitchhackerguide.com 9 9 */ … … 29 29 global $blog_id; 30 30 31 // 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. 31 32 if ( NULL <> $this->define_global_secret && !defined( 'WP_CRON_CONTROL_SECRET' ) ) 32 33 define( 'WP_CRON_CONTROL_SECRET', $this->define_global_secret ); … … 35 36 add_action( 'admin_menu', array( &$this, 'register_settings_page' ) ); 36 37 38 /** 39 * Default settings that will be used for the setup. You can alter these value with a simple filter such as this 40 * add_filter( 'wpcroncontrol_default_settings', 'mywpcroncontrol_settings' ); 41 * function mywpcroncontrol_settings( $settings ) { 42 * $settings['secret_string'] = 'i am more secret than the default'; 43 * return $settings; 44 * } 45 */ 37 46 $this->default_settings = (array) apply_filters( $this->plugin_prefix . 'default_settings', array( 38 47 'enable' => 1, … … 41 50 ) ); 42 51 52 /** 53 * Define fields that will be used on the options page 54 * 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 55 * a filter similar to the default settings (ie wpcroncontrol_settings_texts) can be used to alter this values 56 */ 43 57 $this->settings_texts = (array) apply_filters( $this->plugin_prefix . 'settings_texts', array( 44 58 'enable' => array( 'label' => 'Enable ' . $this->plugin_name, 'desc' => 'Enable this plugin and allow requests to wp-cron.php only with the appended secret parameter.', 'type' => 'yesno' ), … … 50 64 if ( false === $user_settings ) 51 65 $user_settings = array(); 52 66 67 // after getting default settings make sure to parse the arguments together with the user settings 53 68 $this->settings = wp_parse_args( $user_settings, $this->default_settings ); 69 70 /** 71 * If you define( 'WP_CRON_CONTROL_SECRET', 'my_super_secret_string' ); in your wp-config.php or your theme then 72 * users are not allowed to change the secret, so we output the existing secret string rather than allowing to add a new one 73 */ 54 74 if ( defined( 'WP_CRON_CONTROL_SECRET' ) ) { 55 75 $this->settings_texts['secret_string']['type'] = 'echo'; … … 62 82 public static function init() { 63 83 if ( 1 == self::instance()->settings['enable'] ) { 64 65 } 66 84 } 67 85 self::instance()->prepare(); 68 86 } … … 77 95 } 78 96 79 public function prepare() { 97 public function prepare() { 98 /** 99 * If a css file for this plugin exists in ./css/wp-cron-control.css make sure it's included 100 */ 80 101 if ( file_exists( dirname( __FILE__ ) . "/css/" . $this->dashed_name . ".css" ) ) 81 102 wp_enqueue_style( $this->dashed_name, plugins_url( "css/" . $this->dashed_name . ".css", __FILE__ ), $deps = array(), $this->css_version ); 103 /** 104 * If a js file for this plugin exists in ./js/wp-cron-control.css make sure it's included 105 */ 82 106 if ( file_exists( dirname( __FILE__ ) . "/js/" . $this->dashed_name . ".js" ) ) 83 107 wp_enqueue_script( $this->dashed_name, plugins_url( "js/" . $this->dashed_name . ".js", __FILE__ ), array(), $this->js_version, true ); 84 108 109 /** 110 * When the plugin is enabled make sure remove the default behavior for issueing wp-cron requests and add our own method 111 * see: http://core.trac.wordpress.org/browser/trunk/wp-includes/default-filters.php#L236 112 * and http://core.trac.wordpress.org/browser/trunk/wp-includes/cron.php#L258 113 */ 85 114 if ( 1 == $this->settings['enable'] ) { 86 115 remove_action( 'sanitize_comment_cookies', 'wp_cron' ); … … 99 128 100 129 public function validate_settings( $settings ) { 130 // reset to defaults 101 131 if ( !empty( $_POST[ $this->dashed_name . '-defaults'] ) ) { 102 132 $settings = $this->default_settings; 103 133 $_REQUEST['_wp_http_referer'] = add_query_arg( 'defaults', 'true', $_REQUEST['_wp_http_referer'] ); 134 // or do some custom validations 104 135 } else { 105 136 … … 126 157 <th scope="row"><label for="<?php echo $this->dashed_name . '-' . $setting; ?>"><?php if ( isset( $this->settings_texts[$setting]['label'] ) ) { echo $this->settings_texts[$setting]['label']; } else { echo $setting; } ?></label></th> 127 158 <td> 159 <?php 160 /** 161 * Implement various handlers for the different types of fields. This could be easily extended to allow for drop-down boxes, textareas and more 162 */ 163 ?> 128 164 <?php switch( $this->settings_texts[$setting]['type'] ): 129 165 case 'yesno': ?> … … 174 210 submit_button( null, 'primary', $this->dashed_name . '-submit', false ); 175 211 echo ' '; 176 submit_button( 'Reset to Defaults', ' primary', $this->dashed_name . '-defaults', false );212 submit_button( 'Reset to Defaults', '', $this->dashed_name . '-defaults', false ); 177 213 } else { 178 214 echo '<input type="submit" name="' . $this->dashed_name . '-submit" class="button-primary" value="Save Changes" />' . "\n"; … … 188 224 } 189 225 226 /** 227 * Alternative function to the current wp_cron function that would usually executed on sanitize_comment_cookies 228 */ 190 229 public function validate_cron_request() { 191 // we're in wp-cron.php230 // make sure we're in wp-cron.php 192 231 if ( false !== strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) ) { 232 // grab the necessary secret string 193 233 if ( defined( 'WP_CRON_CONTROL_SECRET' ) ) 194 234 $secret = WP_CRON_CONTROL_SECRET; 195 235 else 196 236 $secret = $this->settings['secret_string']; 237 238 // make sure a secret string is provided in the ur 197 239 if ( isset( $_GET[$secret] ) ) { 198 240 // check if there is already a cron request running … … 201 243 if ( $flag > $local_time + 10*60 ) 202 244 $flag = 0; 245 203 246 // don't run if another process is currently running it or more than once every 60 sec. 204 247 if ( $flag + 60 > $local_time ) 205 248 die( 'another cron process running or previous not older than 60 secs' ); 206 249 250 // set a transient to allow locking down parallel requests 207 251 set_transient( 'doing_cron', $local_time ); 208 252 253 // if settings allow it validate if there are any scheduled posts without a cron event 209 254 if ( 1 == self::instance()->settings['enable_scheduled_post_validation'] ) { 210 255 $this->validate_scheduled_posts(); … … 219 264 if ( !defined( 'DOING_CRON' ) ) 220 265 define( 'DOING_CRON', true ); 266 221 267 // and also disable the wp_cron() call execution 222 268 if ( !defined( 'DISABLE_WP_CRON' ) ) … … 227 273 public function validate_scheduled_posts() { 228 274 global $wpdb; 275 276 // grab all scheduled posts from posts table 229 277 $sql = $wpdb->prepare( "SELECT ID, post_date_gmt FROM $wpdb->posts WHERE post_status = 'future' " ); 230 278 $results = $wpdb->get_results( $sql ); 231 279 $return = true; 232 280 281 // if none exists just return 233 282 if ( empty( $results ) ) 234 283 return true; 235 284 285 // otherwise check each of them 236 286 foreach ( $results as $r ) { 237 287 238 288 $gmt_time = strtotime( $r->post_date_gmt . ' GMT' ); 289 290 // grab the scheduled job for this post 239 291 $timestamp = wp_next_scheduled( 'publish_future_post', array( (int) $r->ID ) ); 240 241 292 if ( $timestamp === false ) { 293 // if none exists issue one 242 294 wp_schedule_single_event( $gmt_time, 'publish_future_post', array( (int) $r->ID ) ); 243 295 $return = false; 244 296 } else { 245 // update timestamp to adjust for daylights savings change, when necessary297 // if one exists update timestamp to adjust for daylights savings change, when necessary 246 298 if ( $timestamp != $gmt_time ) { 247 299 wp_clear_scheduled_hook( 'publish_future_post', array( (int) $r->ID ) ); … … 273 325 } 274 326 327 // if we loaded wp-config then ABSPATH is defined and we know the script was not called directly to issue a cli call 275 328 if ( defined('ABSPATH') ) { 276 329 WP_Cron_Control::init(); 277 330 } else { 278 // cli usage331 // otherwise parse the arguments and call the cron. 279 332 if ( !empty( $argv ) && $argv[0] == basename( __FILE__ ) || $argv[0] == __FILE__ ) { 280 333 if ( isset( $argv[1] ) && isset( $argv[2] ) ) {
Note: See TracChangeset
for help on using the changeset viewer.