Plugin Directory

Changeset 424635


Ignore:
Timestamp:
08/17/2011 05:12:27 AM (14 years ago)
Author:
tott
Message:

Adding functionality to validate scheduled posts jobs

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

Legend:

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

    r418191 r424635  
    11=== WP-Cron Control ===
    22Contributors: tott, automattic
    3 Tags: wp-cron, cron, cron jobs
     3Tags: wp-cron, cron, cron jobs, post missed schedule, scheduled posts
    44Donate link: http://hitchhackerguide.com
    55Tested up to: 3.1.2
     
    1010== Description ==
    1111
    12 This plugin allows you to take control over the execution of cron jobs. It's mainly useful for sites that either don't get enough comments to ensure a frequent execution of wp-cron or for sites where the execution of cron via regular methods can cause race conditions resulting in multiple execution of wp-cron at the same time.
     12This plugin allows you to take control over the execution of cron jobs. It's mainly useful for sites that either don't get enough comments to ensure a frequent execution of wp-cron or for sites where the execution of cron via regular methods can cause race conditions resulting in multiple execution of wp-cron at the same time. It can also help when you run into posts that missed their schedule.
    1313
    1414This plugin implements a secret parameter and ensures that cron jobs are only executed when this parameter is existing.
     
    3131== ChangeLog ==
    3232
     33= Version 0.3 =
     34
     35* Added option to enable extra check that would search for missing jobs for scheduled posts and add them if necessary.
     36
    3337= Version 0.2 =
    3438
  • wp-cron-control/trunk/wp-cron-control.php

    r418191 r424635  
    55 Description: get control over wp-cron execution.
    66 Author: Thorsten Ott, Automattic
    7  Version: 0.2
     7 Version: 0.3
    88 Author URI: http://hitchhackerguide.com
    99 */
     
    3737        $this->default_settings = (array) apply_filters( $this->plugin_prefix . 'default_settings', array(
    3838            'enable'                => 1,
     39            'enable_scheduled_post_validation' => 0,
    3940            'secret_string'         => md5( __FILE__ . $blog_id ),
    4041        ) );
     
    4344            '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' ),
    4445            'secret_string'         => array( 'label' => 'Secret string', 'desc' => 'The secret parameter that needs to be appended to wp-cron.php requests.', 'type' => 'text' ),
     46            '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' ),
    4547        ) );
    4648                   
     
    204206               
    205207                set_transient( 'doing_cron', $local_time );
     208               
     209                if ( 1 == self::instance()->settings['enable_scheduled_post_validation'] ) {
     210                    $this->validate_scheduled_posts();
     211                }
    206212                return true;
    207213            }
     
    217223            define( 'DISABLE_WP_CRON', true );
    218224        return false;
     225    }
     226   
     227    public function validate_scheduled_posts() {
     228        global $wpdb;
     229        $sql = $wpdb->prepare( "SELECT ID, post_date_gmt FROM $wpdb->posts WHERE post_status = 'future' " );
     230        $results = $wpdb->get_results( $sql );
     231        $return = true;
     232
     233        if ( empty( $results ) )
     234            return true;
     235
     236        foreach ( $results as $r ) {
     237
     238            $gmt_time  = strtotime( $r->post_date_gmt . ' GMT' );
     239            $timestamp = wp_next_scheduled( 'publish_future_post', array( (int) $r->ID ) );
     240
     241            if ( $timestamp === false ) {
     242                wp_schedule_single_event( $gmt_time, 'publish_future_post', array( (int) $r->ID ) );
     243                $return = false;
     244            } else {
     245                //update timestamp to adjust for daylights savings change, when necessary
     246                if ( $timestamp != $gmt_time ) {
     247                    wp_clear_scheduled_hook( 'publish_future_post', array( (int) $r->ID ) );
     248                    wp_schedule_single_event( $gmt_time, 'publish_future_post', array( (int) $r->ID ) );
     249
     250                    $new_date = date( 'Y-m-d H:i:s', $gmt_time );
     251                    $sql_u = $wpdb->prepare( "UPDATE $wpdb->posts SET post_date_gmt=%s WHERE ID=%d", $new_date, $r->ID );
     252                    $wpdb->query( $sql_u );
     253                    $return = false;
     254                }
     255            }
     256        }
     257       
     258        return $return;
    219259    }
    220260}
Note: See TracChangeset for help on using the changeset viewer.