/**
 * Break a specified occurrence from an event
 * 
 * @param int $post_id The event (post) ID
 * @param int $occurrence_id The occurrence ID
 * @return int|WP_Error The new event (post) ID or a WP_Error on error
 */
function eo_break_occurrence($post_id, $occurrence_id)
{
    global $post;
    $post = get_post($post_id);
    setup_postdata($post_id);
    /**
     * Triggered before an occurrence is broken from an event.
     *
     * @param int $post_id The ID of the original parent event
     * @param int $occurrence_id The ID of the occurrence being broken
     */
    do_action('eventorganiser_pre_break_occurrence', $post_id, $occurrence_id);
    $tax_input = array();
    foreach (array('event-category', 'event-tag', 'event-venue') as $tax) {
        $terms = get_the_terms($post->ID, $tax);
        if ($terms && !is_wp_error($terms)) {
            $tax_input[$tax] = array_map('intval', wp_list_pluck($terms, 'term_id'));
        }
    }
    //Post details
    $post_array = array('post_title' => $post->post_title, 'post_name' => $post->post_name, 'post_author' => $post->post_author, 'post_content' => $post->post_content, 'post_status' => $post->post_status, 'post_date' => $post->post_date, 'post_date_gmt' => $post->post_date_gmt, 'post_excerpt' => $post->post_excerpt, 'post_password' => $post->post_password, 'post_type' => 'event', 'tax_input' => $tax_input, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status);
    //Event details
    $event_array = array('start' => eo_get_the_start(DATETIMEOBJ, $post_id, null, $occurrence_id), 'end' => eo_get_the_end(DATETIMEOBJ, $post_id, null, $occurrence_id), 'all_day' => eo_is_all_day($post_id) ? 1 : 0, 'schedule' => 'once', 'frequency' => 1);
    //Create new event with duplicated details (new event clears cache)
    $new_event_id = eo_insert_event($post_array, $event_array);
    //delete occurrence, and copy post meta
    if ($new_event_id && !is_wp_error($new_event_id)) {
        $response = _eventorganiser_remove_occurrence($post_id, $occurrence_id);
        $post_custom = get_post_custom($post_id);
        foreach ($post_custom as $meta_key => $meta_values) {
            //Don't copy these
            $ignore_meta = array('_eventorganiser_uid', '_eo_tickets', '_edit_last', '_edit_last', '_edit_lock');
            /**
             * Filters an array of keys which should be ignored when breaking an 
             * occurrence.
             * 
             * When breaking an occurrence from an event a new event is made for 
             * that occurrence. Meta data from the original event is copied across, 
             * unless its meta key exists in the filtered array.  
             * 
             * @param array $ignore_meta Array of meta keys to be ignored
             */
            $ignore_meta = apply_filters('eventorganiser_breaking_occurrence_exclude_meta', $ignore_meta);
            if (in_array($meta_key, $ignore_meta)) {
                continue;
            }
            //Don't copy event meta
            if (0 == strncmp($meta_key, '_eventorganiser', 15)) {
                continue;
            }
            foreach ($meta_values as $meta_value) {
                //get_post_meta() without a key doesn't unserialize:
                // @see{https://github.com/WordPress/WordPress/blob/3.5.1/wp-includes/meta.php#L289}
                $meta_value = maybe_unserialize($meta_value);
                add_post_meta($new_event_id, $meta_key, $meta_value);
            }
        }
    }
    _eventorganiser_delete_calendar_cache();
    /**
     * Triggered after an occurrence has been broken from an event.
     *
     * @param int $post_id The ID of the original parent event
     * @param int $occurrence_id The ID of the occurrence being broken
     * @param int $new_event_id The ID of the newly created event
     */
    do_action('eventorganiser_occurrence_broken', $post_id, $occurrence_id, $new_event_id);
    wp_reset_postdata();
    return $new_event_id;
}
Example #2
0
 function page_actions()
 {
     //Add screen option
     $user = wp_get_current_user();
     $is12hour = get_user_meta($user->ID, 'eofc_time_format', true);
     if ('' === $is12hour) {
         $is12hour = eventorganiser_blog_is_24() ? 0 : 1;
     }
     add_screen_option('eofc_time_format', array('value' => $is12hour));
     add_filter('screen_settings', array($this, 'screen_options'), 10, 2);
     //Check action
     if (!empty($_REQUEST['save']) || !empty($_REQUEST['publish'])) {
         //Check nonce
         check_admin_referer('eventorganiser_calendar_save');
         //authentication checks
         if (!current_user_can('edit_events')) {
             wp_die(__('You do not have sufficient permissions to create events. ', 'eventorganiser'));
         }
         $input = $_REQUEST['eo_event'];
         //Retrieve input from posted data
         //Set the status of the new event
         if (!empty($_REQUEST['save'])) {
             $status = 'draft';
         } else {
             $status = current_user_can('publish_events') ? 'publish' : 'pending';
         }
         //Set post and event details
         $venue = (int) $input['venue_id'];
         $post_input = array('post_title' => $input['event_title'], 'post_status' => $status, 'post_content' => $input['event_content'], 'post_type' => 'event', 'tax_input' => array('event-venue' => array($venue)));
         $tz = eo_get_blog_timezone();
         $event_data = array('schedule' => 'once', 'all_day' => $input['allday'], 'start' => new DateTime($input['StartDate'] . ' ' . $input['StartTime'], $tz), 'end' => new DateTime($input['EndDate'] . ' ' . $input['FinishTime'], $tz));
         //Insert event
         $post_id = eo_insert_event($post_input, $event_data);
         if ($post_id) {
             //If event was successfully inserted, redirect and display appropriate message
             $redirect = get_edit_post_link($post_id, '');
             if ($status == 'publish') {
                 $redirect = add_query_arg('message', 6, $redirect);
             } else {
                 $redirect = add_query_arg('message', 7, $redirect);
             }
             //Redirect to event admin page & exit
             wp_redirect(esc_url_raw($redirect));
             exit;
         }
     } elseif (isset($_REQUEST['action']) && ($_REQUEST['action'] == 'delete_occurrence' || $_REQUEST['action'] == 'break_series') && isset($_REQUEST['series']) && isset($_REQUEST['event'])) {
         $post_id = intval($_REQUEST['series']);
         $event_id = intval($_REQUEST['event']);
         $action = $_REQUEST['action'];
         if ($action == 'break_series') {
             //Check nonce
             check_admin_referer('eventorganiser_break_series_' . $event_id);
             //Check permissions
             if (!current_user_can('edit_event', $post_id) || !current_user_can('delete_event', $post_id)) {
                 wp_die(__('You do not have sufficient permissions to edit this event. ', 'eventorganiser'));
             }
             $new_event_id = eo_break_occurrence($post_id, $event_id);
             //Redirect to prevent resubmisson
             $redirect = get_edit_post_link($new_event_id, '');
             $redirect = add_query_arg('message', 20, $redirect);
             wp_redirect(esc_url_raw($redirect));
             exit;
         } elseif ($action == 'delete_occurrence') {
             global $EO_Errors;
             //Check nonce
             check_admin_referer('eventorganiser_delete_occurrence_' . $event_id);
             //Check permissions
             if (!current_user_can('delete_event', $post_id)) {
                 wp_die(__('You do not have sufficient permissions to delete this event. ', 'eventorganiser'));
             }
             $response = _eventorganiser_remove_occurrence($post_id, $event_id);
             //Break Cache!
             _eventorganiser_delete_calendar_cache();
             if (is_wp_error($response)) {
                 $EO_Errors = $response;
             } else {
                 $EO_Errors = new WP_Error('eo_notice', '<strong>' . __('Occurrence deleted.', 'eventorganiser') . '</strong>');
                 /**
                  * Triggered when occurrence is delete via the admin calendar. 
                  * If you use this, send a message here: wp-event-organiser.com/contact. It may be removed.
                  * @ignore
                  */
                 do_action('eventorganiser_admin_calendar_occurrence_deleted', $post_id, $event_id);
             }
         }
     }
 }
/**
 * Break a specified occurrence from an event
 * 
 * @param int $post_id The event (post) ID
 * @param int $occurrence_id The occurrence ID
 * @return int|WP_Error The new event (post) ID or a WP_Error on error
 */
function eo_break_occurrence($post_id, $occurrence_id)
{
    global $post;
    $post = get_post($post_id);
    setup_postdata($post_id);
    do_action('eventorganiser_pre_break_occurrence', $post_id, $occurrence_id);
    $tax_input = array();
    foreach (array('event-category', 'event-tag', 'event-venue') as $tax) {
        $terms = get_the_terms($post->ID, $tax);
        if ($terms && !is_wp_error($terms)) {
            $tax_input[$tax] = array_map('intval', wp_list_pluck($terms, 'term_id'));
        }
    }
    //Post details
    $post_array = array('post_title' => $post->post_title, 'post_name' => $post->post_name, 'post_author' => $post->post_author, 'post_content' => $post->post_content, 'post_status' => $post->post_status, 'post_date' => $post->post_date, 'post_date_gmt' => $post->post_date_gmt, 'post_excerpt' => $post->post_excerpt, 'post_password' => $post->post_password, 'post_type' => 'event', 'tax_input' => $tax_input, 'comment_status' => $post->comment_status, 'ping_status' => $post->ping_status);
    //Event details
    $event_array = array('start' => eo_get_the_start(DATETIMEOBJ, $post_id, null, $occurrence_id), 'end' => eo_get_the_end(DATETIMEOBJ, $post_id, null, $occurrence_id), 'all_day' => eo_is_all_day($post_id) ? 1 : 0, 'schedule' => 'once', 'frequency' => 1);
    //Create new event with duplicated details (new event clears cache)
    $new_event_id = eo_insert_event($post_array, $event_array);
    //delete occurrence, and copy post meta
    if ($new_event_id && !is_wp_error($new_event_id)) {
        $response = _eventorganiser_remove_occurrence($post_id, $occurrence_id);
        $post_custom = get_post_custom($post_id);
        foreach ($post_custom as $meta_key => $meta_values) {
            //Don't copy these
            $ignore_meta = array('_eventorganiser_uid', '_eo_tickets', '_edit_last', '_edit_last', '_edit_lock');
            $ignore_meta = apply_filters('eventorganiser_breaking_occurrence_exclude_meta', $ignore_meta);
            if (in_array($meta_key, $ignore_meta)) {
                continue;
            }
            //Don't copy event meta
            if (0 == strncmp($meta_key, '_eventorganiser', 15)) {
                continue;
            }
            foreach ($meta_values as $meta_value) {
                //get_post_meta() without a key doesn't unserialize:
                // @see{https://github.com/WordPress/WordPress/blob/3.5.1/wp-includes/meta.php#L289}
                $meta_value = maybe_unserialize($meta_value);
                add_post_meta($new_event_id, $meta_key, $meta_value);
            }
        }
    }
    _eventorganiser_delete_calendar_cache();
    do_action('eventorganiser_occurrence_broken', $post_id, $occurrence_id, $new_event_id);
    wp_reset_postdata();
    return $new_event_id;
}