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); } } } }
/** * Event Organiser generates start dates and uses the duration of the first * event to calculate the other occurrences. So the end date of a recurring event * may vary. * @see https://wordpress.org/support/topic/last-day-missing-because-of-the-leap-year-2016?replies=3 */ public function testOverVarythingMonths() { $event = array('start' => new DateTime('2015-02-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-03 00:00:00', eo_get_blog_timezone()), 'schedule' => 'monthly', 'schedule_meta' => 'BYMONTHDAY=28', 'schedule_last' => new DateTime('2015-04-28 00:00:00', eo_get_blog_timezone())); $event_id = eo_insert_event($event); $actual = array_values(eo_get_the_occurrences_of($event_id)); $expected = array(array('start' => new DateTime('2015-02-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-03 00:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-03-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-31 00:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-04-28 00:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-05-01 00:00:00', eo_get_blog_timezone()))); //$actual and $expected contain the occurrence IDs as keys. $this->assertEquals($expected, $actual); }
/** * Checks that included/excluded dates are converted to the blog timezone * before being inserted into the database * @see https://github.com/stephenharris/Event-Organiser/issues/264 */ function testEventIncludesInForeignTimezone() { //Set the site timezone to America/New_York $original_tz = get_option('timezone_string'); update_option('timezone_string', 'America/New_York'); wp_cache_delete('eventorganiser_timezone'); //Next define an event using a foreign (to the site) timezone. E.g. UTC. $utc = new DateTimeZone('UTC'); $event_id = eo_insert_event(array('start' => new DateTime('2015-03-27 15:00:00', $utc), 'end' => new DateTime('2015-03-27 22:00:00', $utc), 'schedule' => 'daily', 'schedule_last' => new DateTime('2015-03-29 15:00:00', $utc), 'include' => array(new DateTime('2015-03-30 15:00:00', $utc)), 'exclude' => array(new DateTime('2015-03-28 15:00:00', $utc)))); $actual = array_values(eo_get_the_occurrences_of($event_id)); $expected = array(array('start' => new DateTime('2015-03-27 11:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-27 18:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-03-29 11:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-29 18:00:00', eo_get_blog_timezone())), array('start' => new DateTime('2015-03-30 11:00:00', eo_get_blog_timezone()), 'end' => new DateTime('2015-03-30 18:00:00', eo_get_blog_timezone()))); $this->assertEquals($expected, $actual); //Reset update_option('timezone_string', $original_tz); wp_cache_delete('eventorganiser_timezone'); }
function create_object($args) { $args = array_merge(array('start' => new DateTime('2015-02-20 17:54:00'), 'end' => new DateTime('2015-02-20 18:54:00')), $args); return eo_insert_event($args); }
public function testScheduleLastUpdate() { $event_data = array('start' => new DateTime('2013-10-22 19:19:00', eo_get_blog_timezone()), 'end' => new DateTime('2013-10-22 20:19:00', eo_get_blog_timezone()), 'schedule' => 'daily', 'frequency' => 2, 'until' => new DateTime('2013-10-26 19:19:00', eo_get_blog_timezone())); $event = eo_insert_event($event_data); eo_update_event($event, array('include' => array(new DateTime('2013-10-25 19:19:00', eo_get_blog_timezone())))); $schedule = eo_get_event_schedule($event); $this->assertEquals(new DateTime('2013-10-26 19:19:00', eo_get_blog_timezone()), $schedule['until']); }
public function testRRULE_all_day() { global $wpdb; $wpdb->db_connect(); wp_cache_set('eventorganiser_timezone', 'America/New_York'); //Event recurrs every Monday evening in New York but is all day, so day should remain on Monday in UTC $event_id = eo_insert_event(array('start' => new DateTime('2013-12-02 21:00', eo_get_blog_timezone()), 'end' => new DateTime('2013-12-02 23:00', eo_get_blog_timezone()), 'schedule_last' => new DateTime('2013-12-30 21:00', eo_get_blog_timezone()), 'frequency' => 1, 'all_day' => 1, 'schedule' => 'weekly', 'schedule_meta' => array('MO'), 'post_title' => 'The Event Title', 'post_content' => 'My event content', 'post_date' => '2015-02-18 17:30:00')); $this->assertEquals("FREQ=WEEKLY;INTERVAL=1;BYDAY=MO;UNTIL=20131231T020000Z", eventorganiser_generate_ics_rrule($event_id)); wp_cache_delete('eventorganiser_timezone'); }
/** * 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; }
/** * Update and EO event, given a CiviEvent. If no EO event exists * then create one. This will NOT create sequences and is intended for the * initial migration of CiviEvents to WordPress * * @since 0.1 * * @param array $civi_event An array of data for the CiviEvent * @return int $event_id The numeric ID of the event */ public function update_event($civi_event) { /* error_log( print_r( array( 'method' => __METHOD__, 'civi_event' => $civi_event, ), true ) ); */ // define schedule $event_data = array('start' => new DateTime($civi_event['start_date'], eo_get_blog_timezone()), 'end' => new DateTime($civi_event['end_date'], eo_get_blog_timezone()), 'schedule_last' => new DateTime($civi_event['end_date'], eo_get_blog_timezone()), 'frequency' => 1, 'all_day' => 0, 'schedule' => 'once'); // define post $post_data = array('post_title' => $civi_event['title'], 'post_content' => isset($civi_event['description']) ? $civi_event['description'] : '', 'post_excerpt' => isset($civi_event['summary']) ? $civi_event['summary'] : '', 'to_ping' => '', 'pinged' => '', 'post_content_filtered' => ''); // test for created date, which may be absent if (isset($civi_event['created_date']) and !empty($civi_event['created_date'])) { // create DateTime object $datetime = new DateTime($civi_event['created_date'], eo_get_blog_timezone()); // add it, but format it first since Civi seems to send data in the form 20150916135435 $post_data['post_date'] = $datetime->format('Y-m-d H:i:s'); } // assume the CiviEvent is live $post_data['post_status'] = 'publish'; // is the event active? if ($civi_event['is_active'] == 0) { // make the CiviEvent unpublished $post_data['post_status'] = 'draft'; } // init venue as undefined $venue_id = 0; // get location ID if (isset($civi_event['loc_block_id'])) { // we have a location... // get location data $location = $this->plugin->civi->get_location_by_id($civi_event['loc_block_id']); // get corresponding EO venue ID $venue_id = $this->plugin->eo_venue->get_venue_id($location); // did we get one? if ($venue_id === false) { // no, let's create one $venue_id = $this->plugin->eo_venue->create_venue($location); } else { // yes, update it $venue_id = $this->plugin->eo_venue->update_venue($location); } } // init category as undefined $terms = array(); // get location ID if (isset($civi_event['event_type_id'])) { // we have a category... //print_r( $this->plugin->civi->get_event_types() ); die(); // get event type data for this pseudo-ID (actually "value") $type = $this->plugin->civi->get_event_type_by_value($civi_event['event_type_id']); // does this type have an existing term? $term_id = $this->get_term_id($type); // if not... if ($term_id === false) { // create one $term = $this->create_term($type); // assign term ID $term_id = $term['term_id']; } // define as array $terms = array(absint($term_id)); } // add to post data $post_data['tax_input'] = array('event-venue' => array(absint($venue_id)), 'event-category' => $terms); /* error_log( print_r( array( 'method' => __METHOD__, 'post_data' => $post_data, 'type' => isset( $type ) ? $type : 'NOT SET', ) ); die(); */ // do we have a post ID for this event? $eo_post_id = $this->plugin->db->get_eo_event_id_by_civi_event_id($civi_event['id']); // remove hooks remove_action('wp_insert_post', array($this, 'insert_post'), 10); remove_action('eventorganiser_save_event', array($this, 'intercept_save_event'), 10); // did we get a post ID? if ($eo_post_id === false) { // use EO's API to create event $event_id = eo_insert_event($post_data, $event_data); } else { // use EO's API to update event $event_id = eo_update_event($eo_post_id, $post_data, $event_data); } // re-add hooks add_action('wp_insert_post', array($this, 'insert_post'), 10, 2); add_action('eventorganiser_save_event', array($this, 'intercept_save_event'), 10, 1); // --< return $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; }
/** * Reads in an ICAL file into an array, then parses the array and inserts events into database * @since 1.1.0 * @param string $cal_file - the file to import */ function import_file($ical_file) { global $EO_Errors; if (!current_user_can('manage_options') || !current_user_can('edit_events')) { wp_die(__('You do not have sufficient permissions to import events.', 'eventorganiser')); } $ical = new EO_ICAL_Parser(); $ical->parse($ical_file); $import_venues = isset($_POST['eo_import_venue']) ? true : false; $import_cats = isset($_POST['eo_import_cat']) ? true : false; $events_imported = 0; $events_updated = 0; $venues_imported = 0; $categories_imported = 0; if ($import_venues && $ical->venues) { foreach ($ical->venues as $venue) { if (!eo_get_venue_by('name', $venue)) { $args = array(); //If lat/lng meta data is set, include that if (isset($ical->venue_meta[$venue]['latitude']) && isset($ical->venue_meta[$venue]['longtitude'])) { $args['latitude'] = $ical->venue_meta[$venue]['latitude']; $args['longtitude'] = $ical->venue_meta[$venue]['longtitude']; } $new_venue = eo_insert_venue($venue, $args); if (!is_wp_error($new_venue) && $new_venue) { $venues_imported++; } } } } if ($import_cats && $ical->categories) { foreach ($ical->categories as $category) { if (!get_term_by('name', $category, 'event-category')) { $new_cat = wp_insert_term($category, 'event-category', array()); if (!is_wp_error($new_cat) && $new_cat) { $categories_imported++; } } } } foreach ($ical->events as $event) { //TODO refactor eo_insert_event & eo_update_event... $uid = !empty($event['uid']) ? $event['uid'] : false; //TODO Check if event already exists //$found_event = eo_get_event_by_uid( $uid ); //Create event if (!empty($event['event-venue'])) { $venue = eo_get_venue_by('name', $event['event-venue']); if ($venue) { $event['tax_input']['event-venue'][] = intval($venue->term_id); } } if (!empty($event['event-category'])) { $event['tax_input']['event-category'] = array(); foreach ($event['event-category'] as $category) { $cat = get_term_by('name', $category, 'event-category'); if ($cat) { $event['tax_input']['event-category'][] = intval($cat->term_id); } } } $event_id = eo_insert_event($event); if (is_wp_error($event_id)) { $ical->errors[] = $event_id; } else { $events_updated++; } } if ($events_updated == 0) { $EO_Errors->add('eo_error', __("No events were imported.", 'eventorganiser')); } if (count($ical->errors) > 0) { foreach ($ical->errors as $error) { $codes = $error->get_error_codes(); foreach ($codes as $code) { $error_messages = $error->get_error_messages(); foreach ($error_messages as $error_message) { $EO_Errors->add('eo_error', '<strong>' . __('Error:', 'eventorganiser') . '</strong> ' . $error_message); } } } } $message = array(); if ($ical->warnings) { foreach ($ical->warnings as $warning) { $message[] = '<strong>' . __('Warning:', 'eventorganiser') . '</strong> ' . $warning->get_error_message(); } } if (count($ical->events) == 1) { $message[] = __("1 event was successfully imported", 'eventorganiser'); } elseif (count($ical->events) > 1) { $message[] = sprintf(__("%d events were successfully imported", 'eventorganiser'), count($ical->events)) . "."; } if ($venues_imported == 1) { $message[] = __("1 venue was created", 'eventorganiser'); } elseif ($venues_imported > 1) { $message[] = sprintf(__("%d venues were created", 'eventorganiser'), $venues_imported); } if ($categories_imported == 1) { $message[] = __("1 category was created", 'eventorganiser'); } elseif ($categories_imported > 1) { $message[] = sprintf(__("%d categories were created", 'eventorganiser'), $categories_imported); } $EO_Errors->add('eo_notice', implode('<br/>', $message)); }