/** * Update recurring event dates * * @since 0.9 */ function ctc_update_recurring_event_dates() { // Get all events with end date in past and have valid recurring value $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' => '<', 'type' => 'DATE'), array('key' => '_ctc_event_recurrence', 'value' => array('weekly', 'monthly', 'yearly'), 'compare' => 'IN')))); // Loop events if (!empty($events_query->posts)) { // Instantiate recurrence class $ctc_recurrence = new CT_Recurrence(); // Loop events to modify dates foreach ($events_query->posts as $post) { // Get start and end date $start_date = get_post_meta($post->ID, '_ctc_event_start_date', true); $end_date = get_post_meta($post->ID, '_ctc_event_end_date', true); // 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); // Difference between start and end date in seconds $time_difference = strtotime($end_date) - strtotime($start_date); // Get soonest occurence that is today or later $args = array('start_date' => $start_date, 'frequency' => $recurrence); $args = apply_filters('ctc_event_recurrence_args', $args, $post); // Custom Recurring Events add-on uses this $new_start_date = $ctc_recurrence->calc_next_future_date($args); // If no new start date gotten, set it to current start date // This could be because recurrence ended, arguments are invalid, etc. if (!$new_start_date) { $new_start_date = $start_date; } // Add difference between original start/end date to new start date to get new end date $new_end_date = date('Y-m-d', strtotime($new_start_date) + $time_difference); // 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 start and end 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); // Update the hidden datetime fields for ordering ctc_update_event_date_time($post->ID); } } } }
/** * Set Events Defaults (All Events) * * This will ensure defaults are filled for new fields. * This can be safely run by the database upgrader for any version. * * See includes/admin/upgrade.php for how it is used. * * NOTE: This does not set defaults for fields that have always existed (not necessary). * NOTE: This can be modified in future to accommodate other new fields. * * @since 1.2 */ function ctc_set_events_defaults() { // Select all events to check/update $posts = get_posts(array('post_type' => 'ctc_event', 'post_status' => 'publish,pending,draft,auto-draft,future,private,inherit,trash', 'numberposts' => -1)); // Loop each post to update fields foreach ($posts as $post) { // Get current values // Example: $field_name = get_post_meta( $post->ID, '_ctc_event_field_name', true ); // Set defaults for new fields // Example: if ( ! $field_name ) update_post_meta( $post->ID, '_ctc_event_field_name', '1' ); // Date and Time fields are combined into one field for easier ordering (simpler queries) // This hidden field was introduced in 1.2 // If no date, value will be 0000-00-00 00:00:00 // If no time, value will be 2014-10-28 00:00:00 ctc_update_event_date_time($post->ID); } }