WordPress.org

Plugin Directory

Changeset 459746


Ignore:
Timestamp:
11/05/11 20:11:27 (2 years ago)
Author:
tott
Message:

adding more inline comments, fixing class for reset settings button props go to yoast

Location:
wp-cron-control/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • wp-cron-control/trunk/readme.txt

    r424635 r459746  
    3131== ChangeLog == 
    3232 
     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 
    3337= Version 0.3 = 
    3438 
  • wp-cron-control/trunk/wp-cron-control.php

    r424635 r459746  
    55 Description: get control over wp-cron execution. 
    66 Author: Thorsten Ott, Automattic 
    7  Version: 0.3 
     7 Version: 0.4 
    88 Author URI: http://hitchhackerguide.com 
    99 */ 
     
    2929        global $blog_id; 
    3030         
     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. 
    3132        if ( NULL <> $this->define_global_secret && !defined( 'WP_CRON_CONTROL_SECRET' ) ) 
    3233            define( 'WP_CRON_CONTROL_SECRET', $this->define_global_secret ); 
     
    3536        add_action( 'admin_menu', array( &$this, 'register_settings_page' ) ); 
    3637         
     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         */ 
    3746        $this->default_settings = (array) apply_filters( $this->plugin_prefix . 'default_settings', array( 
    3847            'enable'                => 1, 
     
    4150        ) ); 
    4251         
     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         */ 
    4357        $this->settings_texts = (array) apply_filters( $this->plugin_prefix . 'settings_texts', array( 
    4458            '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' ), 
     
    5064        if ( false === $user_settings ) 
    5165            $user_settings = array(); 
    52              
     66         
     67        // after getting default settings make sure to parse the arguments together with the user settings 
    5368        $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         */  
    5474        if ( defined( 'WP_CRON_CONTROL_SECRET' ) ) { 
    5575            $this->settings_texts['secret_string']['type'] = 'echo'; 
     
    6282    public static function init() {  
    6383        if ( 1 == self::instance()->settings['enable'] ) { 
    64  
    65         } 
    66          
     84        }    
    6785        self::instance()->prepare(); 
    6886    } 
     
    7795    } 
    7896 
    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         */ 
    80101        if ( file_exists( dirname( __FILE__ ) . "/css/" . $this->dashed_name . ".css" ) ) 
    81102            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         */ 
    82106        if ( file_exists( dirname( __FILE__ ) . "/js/" . $this->dashed_name . ".js" ) ) 
    83107            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         */ 
    85114        if ( 1 == $this->settings['enable'] ) { 
    86115            remove_action( 'sanitize_comment_cookies', 'wp_cron' ); 
     
    99128     
    100129    public function validate_settings( $settings ) { 
     130        // reset to defaults 
    101131        if ( !empty( $_POST[ $this->dashed_name . '-defaults'] ) ) { 
    102132            $settings = $this->default_settings; 
    103133            $_REQUEST['_wp_http_referer'] = add_query_arg( 'defaults', 'true', $_REQUEST['_wp_http_referer'] ); 
     134        // or do some custom validations 
    104135        } else { 
    105136             
     
    126157                    <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> 
    127158                    <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                        ?> 
    128164                        <?php switch( $this->settings_texts[$setting]['type'] ): 
    129165                            case 'yesno': ?> 
     
    174210                    submit_button( null, 'primary', $this->dashed_name . '-submit', false ); 
    175211                    echo ' '; 
    176                     submit_button( 'Reset to Defaults', 'primary', $this->dashed_name . '-defaults', false ); 
     212                    submit_button( 'Reset to Defaults', '', $this->dashed_name . '-defaults', false ); 
    177213                } else { 
    178214                    echo '<input type="submit" name="' . $this->dashed_name . '-submit" class="button-primary" value="Save Changes" />' . "\n"; 
     
    188224    } 
    189225     
     226    /** 
     227     * Alternative function to the current wp_cron function that would usually executed on sanitize_comment_cookies 
     228     */ 
    190229    public function validate_cron_request() { 
    191         // we're in wp-cron.php 
     230        // make sure we're in wp-cron.php 
    192231        if ( false !== strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) ) { 
     232            // grab the necessary secret string 
    193233            if ( defined( 'WP_CRON_CONTROL_SECRET' ) ) 
    194234                $secret = WP_CRON_CONTROL_SECRET; 
    195235            else  
    196236                $secret = $this->settings['secret_string']; 
     237                 
     238            // make sure a secret string is provided in the ur 
    197239            if ( isset( $_GET[$secret] ) ) { 
    198240                // check if there is already a cron request running 
     
    201243                if ( $flag > $local_time + 10*60 ) 
    202244                    $flag = 0; 
     245                     
    203246                // don't run if another process is currently running it or more than once every 60 sec. 
    204247                if ( $flag + 60 > $local_time ) 
    205248                    die( 'another cron process running or previous not older than 60 secs' ); 
    206249                 
     250                // set a transient to allow locking down parallel requests 
    207251                set_transient( 'doing_cron', $local_time ); 
    208252                 
     253                // if settings allow it validate if there are any scheduled posts without a cron event 
    209254                if ( 1 == self::instance()->settings['enable_scheduled_post_validation'] ) { 
    210255                    $this->validate_scheduled_posts(); 
     
    219264        if ( !defined( 'DOING_CRON' ) ) 
    220265            define( 'DOING_CRON', true ); 
     266             
    221267        // and also disable the wp_cron() call execution 
    222268        if ( !defined( 'DISABLE_WP_CRON' ) ) 
     
    227273    public function validate_scheduled_posts() { 
    228274        global $wpdb; 
     275         
     276        // grab all scheduled posts from posts table 
    229277        $sql = $wpdb->prepare( "SELECT ID, post_date_gmt FROM $wpdb->posts WHERE post_status = 'future' " ); 
    230278        $results = $wpdb->get_results( $sql ); 
    231279        $return = true; 
    232280 
     281        // if none exists just return 
    233282        if ( empty( $results ) ) 
    234283            return true; 
    235284 
     285        // otherwise check each of them 
    236286        foreach ( $results as $r ) { 
    237287 
    238288            $gmt_time  = strtotime( $r->post_date_gmt . ' GMT' ); 
     289             
     290            // grab the scheduled job for this post 
    239291            $timestamp = wp_next_scheduled( 'publish_future_post', array( (int) $r->ID ) ); 
    240  
    241292            if ( $timestamp === false ) { 
     293                // if none exists issue one 
    242294                wp_schedule_single_event( $gmt_time, 'publish_future_post', array( (int) $r->ID ) ); 
    243295                $return = false; 
    244296            } else { 
    245                 //update timestamp to adjust for daylights savings change, when necessary 
     297                // if one exists update timestamp to adjust for daylights savings change, when necessary 
    246298                if ( $timestamp != $gmt_time ) { 
    247299                    wp_clear_scheduled_hook( 'publish_future_post', array( (int) $r->ID ) ); 
     
    273325} 
    274326 
     327// if we loaded wp-config then ABSPATH is defined and we know the script was not called directly to issue a cli call 
    275328if ( defined('ABSPATH') ) { 
    276329    WP_Cron_Control::init(); 
    277330} else { 
    278     // cli usage 
     331    // otherwise parse the arguments and call the cron.  
    279332    if ( !empty( $argv ) && $argv[0] == basename( __FILE__ ) || $argv[0] == __FILE__ ) { 
    280333        if ( isset( $argv[1] ) && isset( $argv[2] ) ) { 
Note: See TracChangeset for help on using the changeset viewer.