示例#1
0
/**
 * Update recurring event dates
 *
 * @since 0.9
 */
function ctc_update_recurring_event_dates()
{
    // Localized dates
    $yesterday = date_i18n('Y-m-d', time() - DAY_IN_SECONDS);
    // Get all events with end date in past and have valid recurring value and
    $events_query = new WP_Query(array('post_type' => 'ctc_event', 'nopaging' => true, 'meta_query' => array('relation' => 'AND', array('key' => '_ctc_event_end_date', 'value' => date_i18n('Y-m-d'), 'compare' => '<'), array('key' => '_ctc_event_recurrence', 'value' => array('weekly', 'monthly', 'yearly'), 'compare' => 'IN'))));
    // Loop events
    if (!empty($events_query->posts)) {
        foreach ($events_query->posts as $post) {
            // Get recurrence
            $recurrence = get_post_meta($post->ID, '_ctc_event_recurrence', true);
            $recurrence_end_date = get_post_meta($post->ID, '_ctc_event_recurrence_end_date', true);
            // Get start and end dates
            $start_date = get_post_meta($post->ID, '_ctc_event_start_date', true);
            $end_date = get_post_meta($post->ID, '_ctc_event_end_date', true);
            // Difference between start and end date in seconds
            $time_difference = strtotime($end_date) - strtotime($start_date);
            // Calculate incremented dates
            $new_start_date = ctc_increment_future_date($start_date, $recurrence);
            // get closest incremented date in future
            $new_end_date = date('Y-m-d', strtotime($new_start_date) + $time_difference);
            // add difference between original start/end date to new start date to get new end date
            // Has recurrence ended?
            // Recurrence end date exists and is earlier than new start date
            if ($recurrence_end_date && strtotime($recurrence_end_date) < strtotime($new_start_date)) {
                // Unset recurrence option to keep dates from being moved forward
                update_post_meta($post->ID, '_ctc_event_recurrence', 'none');
            } else {
                // Update dates
                update_post_meta($post->ID, '_ctc_event_start_date', $new_start_date);
                update_post_meta($post->ID, '_ctc_event_end_date', $new_end_date);
            }
        }
    }
}
示例#2
0
/**
 * Move date forward
 *
 * Move date forward by week, month or year until it is not in past (in case wp cron misses a beat).
 *
 * @since 0.9
 * @param string $date Date to move into the future
 * @param string $increment 'weekly', 'monthly' or 'yearly'
 * @return string Future date
 */
function ctc_increment_future_date($date, $increment)
{
    // In case no change could be made
    $new_date = $date;
    // Get month, day and year, increment if date is valid
    list($y, $m, $d) = explode('-', $date);
    if (checkdate($m, $d, $y)) {
        // Increment
        switch ($increment) {
            // Weekly
            case 'weekly':
                // Add 7 days
                list($y, $m, $d) = explode('-', date('Y-m-d', strtotime($date) + WEEK_IN_SECONDS));
                break;
                // Monthly
            // Monthly
            case 'monthly':
                // Move forward one month
                if ($m < 12) {
                    // same year
                    $m++;
                    // add one month
                } else {
                    // next year (old month is December)
                    $m = 1;
                    // first month of year
                    $y++;
                    // add one year
                }
                break;
                // Yearly
            // Yearly
            case 'yearly':
                // Move forward one year
                $y++;
                break;
        }
        // Day does not exist in month
        // Example: Make "November 31" into 30 or "February 29" into 28 (for non-leap year)
        $days_in_month = date('t', mktime(0, 0, 0, $m, 1, $y));
        if ($d > $days_in_month) {
            $d = $days_in_month;
        }
        // Form the date string
        $new_date = date('Y-m-d', mktime(0, 0, 0, $m, $d, $y));
        // pad day, month with 0
        // Is new date in past? Increment until it is not (automatic correction in case wp-cron misses a beat)
        $today_ts = strtotime(date_i18n('Y-m-d'));
        // localized
        $new_date_ts = strtotime($new_date);
        while ($new_date_ts < $today_ts) {
            $new_date = ctc_increment_future_date($new_date, $increment);
            $new_date_ts = strtotime($new_date);
        }
    }
    // Return filterable
    return apply_filters('ctc_move_date_forward', $new_date, $date, $increment);
}