Ejemplo n.º 1
0
 function testEventSchedule()
 {
     $tz = eo_get_blog_timezone();
     $start = new DateTime('2014-06-17 14:45:00', $tz);
     $end = new DateTime('2014-06-17 15:45:00', $tz);
     $inc = array(new DateTime('2014-08-16 14:45:00', $tz));
     $exc = array(new DateTime('2014-06-19 14:45:00', $tz), new DateTime('2014-07-03 14:45:00', $tz));
     $event = array('start' => $start, 'end' => $end, 'frequency' => 2, 'schedule' => 'weekly', 'schedule_meta' => array('TU', 'TH'), 'include' => $inc, 'exclude' => $exc, 'schedule_last' => new DateTime('2014-08-15 14:45:00', $tz));
     $event_id = $this->factory->event->create($event);
     $schedule = eo_get_event_schedule($event_id);
     $this->assertEquals($start, $schedule['start']);
     $this->assertEquals($end, $schedule['end']);
     $this->assertEquals(false, $schedule['all_day']);
     $this->assertEquals('weekly', $schedule['schedule']);
     $this->assertEquals(array('TU', 'TH'), $schedule['schedule_meta']);
     $this->assertEquals(2, $schedule['frequency']);
     $duration = eo_date_interval($start, $end, '+%y year +%m month +%d days +%h hours +%i minutes +%s seconds');
     $schedule_last = new DateTime('2014-08-16 14:45:00', $tz);
     $schedule_finish = clone $schedule_last;
     $schedule_finish->modify($duration);
     $this->assertEquals($start, $schedule['schedule_start']);
     $this->assertEquals($schedule_last, $schedule['schedule_last']);
     $this->assertEquals($schedule_finish, $schedule['schedule_finish']);
     $this->assertEquals($inc, $schedule['include']);
     $this->assertEquals($exc, $schedule['exclude']);
     $occurrences = array(new DateTime('2014-06-17 14:45:00', $tz), new DateTime('2014-07-01 14:45:00', $tz), new DateTime('2014-07-15 14:45:00', $tz), new DateTime('2014-07-17 14:45:00', $tz), new DateTime('2014-07-29 14:45:00', $tz), new DateTime('2014-07-31 14:45:00', $tz), new DateTime('2014-08-12 14:45:00', $tz), new DateTime('2014-08-14 14:45:00', $tz), new DateTime('2014-08-16 14:45:00', $tz));
     $this->assertEquals($occurrences, array_values($schedule['_occurrences']));
 }
 function testDateIntervalMonthOverflow()
 {
     $timezone = new DateTimeZone('Europe/Berlin');
     $date1 = new DateTime('2015-01-30 13:00:00', $timezone);
     $date2 = new DateTime('2015-01-30 16:30:10', $timezone);
     //Check mod is correctly calculated
     $mod = eo_date_interval($date1, $date2, '+%d days +%h hours +%i minutes +%s seconds');
     $this->assertEquals("+0 days +3 hours +30 minutes +10 seconds", $mod);
 }
 static function parse_template($matches)
 {
     global $post;
     $replacement = '';
     switch ($matches[1]) {
         case 'event_title':
             $replacement = get_the_title();
             break;
         case 'start':
         case 'end':
         case 'schedule_start':
         case 'schedule_last':
         case 'schedule_end':
             switch (count($matches)) {
                 case 2:
                     $dateFormat = get_option('date_format');
                     $dateTime = get_option('time_format');
                     break;
                 case 3:
                     $dateFormat = self::eo_clean_input($matches[2]);
                     $dateTime = '';
                     break;
                 case 5:
                     $dateFormat = self::eo_clean_input($matches[3]);
                     $dateTime = self::eo_clean_input($matches[4]);
                     break;
             }
             $format = eo_is_all_day(get_the_ID()) ? $dateFormat : $dateFormat . $dateTime;
             switch ($matches[1]) {
                 case 'start':
                     $replacement = eo_get_the_start($format);
                     break;
                 case 'end':
                     $replacement = eo_get_the_end($format);
                     break;
                 case 'schedule_start':
                     $replacement = eo_get_schedule_start($format);
                     break;
                 case 'schedule_last':
                 case 'schedule_end':
                     $replacement = eo_get_schedule_end($format);
                     break;
             }
             break;
         case 'event_duration':
             $start = eo_get_the_start(DATETIMEOBJ);
             $end = clone eo_get_the_end(DATETIMEOBJ);
             if (eo_is_all_day()) {
                 $end->modify('+1 minute');
             }
             if (function_exists('date_diff')) {
                 $duration = date_diff($start, $end);
                 $replacement = $duration->format($matches[2]);
             } else {
                 $replacement = eo_date_interval($start, $end, $matches[2]);
             }
             $replacement = false;
             break;
         case 'event_tags':
             $replacement = get_the_term_list(get_the_ID(), 'event-tag', '', ', ', '');
             break;
         case 'event_cats':
             $replacement = get_the_term_list(get_the_ID(), 'event-category', '', ', ', '');
             break;
         case 'event_venue':
             $replacement = eo_get_venue_name();
             break;
         case 'event_venue_map':
             if (eo_get_venue()) {
                 $class = isset($matches[2]) ? self::eo_clean_input($matches[2]) : '';
                 $class = !empty($class) ? 'class=' . $class : '';
                 $replacement = eo_get_venue_map(eo_get_venue(), compact('class'));
             }
             break;
         case 'event_venue_url':
             $venue_link = eo_get_venue_link();
             $replacement = !is_wp_error($venue_link) ? $venue_link : '';
             break;
         case 'event_venue_address':
             $address = eo_get_venue_address();
             $replacement = $address['address'];
             break;
         case 'event_venue_postcode':
             $address = eo_get_venue_address();
             $replacement = $address['postcode'];
             break;
         case 'event_venue_city':
             $address = eo_get_venue_address();
             $replacement = $address['city'];
             break;
         case 'event_venue_country':
             $address = eo_get_venue_address();
             $replacement = $address['country'];
             break;
         case 'event_venue_state':
             $address = eo_get_venue_address();
             $replacement = $address['state'];
             break;
         case 'event_venue_city':
             $address = eo_get_venue_address();
             $replacement = $address['city'];
             break;
         case 'event_thumbnail':
             $size = isset($matches[2]) ? self::eo_clean_input($matches[2]) : '';
             $size = !empty($size) ? $size : 'thumbnail';
             $attr = isset($matches[3]) ? self::eo_clean_input($matches[3]) : '';
             //Decode HTML entities as shortcode encodes them
             $attr = html_entity_decode($attr);
             $replacement = get_the_post_thumbnail(get_the_ID(), $size, $attr);
             break;
         case 'event_url':
             $replacement = get_permalink();
             break;
         case 'event_custom_field':
             $field = $matches[2];
             $meta = get_post_meta(get_the_ID(), $field);
             $replacement = implode($meta);
             break;
         case 'event_excerpt':
             $length = isset($matches[2]) ? intval($matches[2]) : 55;
             //Using get_the_excerpt adds a link....
             if (post_password_required($post)) {
                 $output = __('There is no excerpt because this is a protected post.');
             } else {
                 $output = $post->post_excerpt;
             }
             $replacement = eventorganiser_trim_excerpt($output, $length);
             break;
         case 'event_content':
             $replacement = get_the_content();
             break;
         case 'cat_color':
             $replacement = eo_get_event_color();
             break;
         case 'event_title_attr':
             $replacement = get_the_title();
             break;
     }
     return $replacement;
 }
Ejemplo n.º 4
0
/**
* This is a private function - handles the insertion of dates into the database. Use eo_insert_event or eo_update_event instead.
* @access private
* @ignore
*
* @param int $post_id The post ID of the event
* @param array $event_data Array of event data, including schedule meta (saved as post meta), duration and occurrences
* @return int $post_id
*/
function _eventorganiser_insert_occurrences($post_id, $event_data)
{
    global $wpdb;
    extract($event_data);
    $tz = eo_get_blog_timezone();
    //Don't use date_diff (requires php 5.3+)
    //Also see https://github.com/stephenharris/Event-Organiser/issues/205
    //And https://github.com/stephenharris/Event-Organiser/issues/224
    $duration_str = eo_date_interval($start, $end, '+%y year +%m month +%d days +%h hours +%i minutes +%s seconds');
    $event_data['duration_str'] = $duration_str;
    $schedule_last_end = clone $schedule_last;
    $schedule_last_end->modify($duration_str);
    //Get dates to be deleted / added
    $current_occurrences = eo_get_the_occurrences($post_id);
    $current_occurrences = $current_occurrences ? $current_occurrences : array();
    $delete = array_udiff($current_occurrences, $occurrences, '_eventorganiser_compare_dates');
    $insert = array_udiff($occurrences, $current_occurrences, '_eventorganiser_compare_dates');
    $update = array_uintersect($occurrences, $current_occurrences, '_eventorganiser_compare_dates');
    $update_2 = array_uintersect($current_occurrences, $update, '_eventorganiser_compare_dates');
    $keys = array_keys($update_2);
    if ($delete) {
        $delete_occurrence_ids = array_keys($delete);
        eo_delete_event_occurrences($post_id, $delete_occurrence_ids);
    }
    $occurrence_cache = array();
    $occurrence_array = array();
    if ($update) {
        $update = array_combine($keys, $update);
        foreach ($update as $occurrence_id => $occurrence) {
            $occurrence_end = clone $occurrence;
            $occurrence_end->modify($duration_str);
            $occurrence_input = array('StartDate' => $occurrence->format('Y-m-d'), 'StartTime' => $occurrence->format('H:i:s'), 'EndDate' => $occurrence_end->format('Y-m-d'), 'FinishTime' => $occurrence_end->format('H:i:s'));
            $wpdb->update($wpdb->eo_events, $occurrence_input, array('event_id' => $occurrence_id));
            //Add to occurrence cache: TODO use post meta
            $occurrence_array[$occurrence_id] = $occurrence->format('Y-m-d H:i:s');
            $occurrence_cache[$occurrence_id] = array('start' => $occurrence, 'end' => new DateTime($occurrence_end->format('Y-m-d') . ' ' . $end->format('H:i:s'), eo_get_blog_timezone()));
        }
    }
    if ($insert) {
        foreach ($insert as $counter => $occurrence) {
            $occurrence_end = clone $occurrence;
            $occurrence_end->modify($duration_str);
            $occurrence_input = array('post_id' => $post_id, 'StartDate' => $occurrence->format('Y-m-d'), 'StartTime' => $occurrence->format('H:i:s'), 'EndDate' => $occurrence_end->format('Y-m-d'), 'FinishTime' => $end->format('H:i:s'), 'event_occurrence' => $counter);
            $wpdb->insert($wpdb->eo_events, $occurrence_input);
            $occurrence_array[$wpdb->insert_id] = $occurrence->format('Y-m-d H:i:s');
            //Add to occurrence cache: TODO use post meta
            $occurrence_cache[$wpdb->insert_id] = array('start' => $occurrence, 'end' => new DateTime($occurrence_end->format('Y-m-d') . ' ' . $end->format('H:i:s'), $tz));
        }
    }
    //Set occurrence cache
    wp_cache_set('eventorganiser_occurrences_' . $post_id, $occurrence_cache);
    unset($event_data['occurrences']);
    $event_data['_occurrences'] = $occurrence_array;
    if (!empty($include)) {
        $event_data['include'] = array_map('eo_format_datetime', $include, array_fill(0, count($include), 'Y-m-d H:i:s'));
    }
    if (!empty($exclude)) {
        $event_data['exclude'] = array_map('eo_format_datetime', $exclude, array_fill(0, count($exclude), 'Y-m-d H:i:s'));
    }
    unset($event_data['start']);
    unset($event_data['end']);
    unset($event_data['schedule_start']);
    unset($event_data['schedule_last']);
    update_post_meta($post_id, '_eventorganiser_event_schedule', $event_data);
    update_post_meta($post_id, '_eventorganiser_schedule_start_start', $start->format('Y-m-d H:i:s'));
    update_post_meta($post_id, '_eventorganiser_schedule_start_finish', $end->format('Y-m-d H:i:s'));
    update_post_meta($post_id, '_eventorganiser_schedule_last_start', $schedule_last->format('Y-m-d H:i:s'));
    update_post_meta($post_id, '_eventorganiser_schedule_last_finish', $schedule_last_end->format('Y-m-d H:i:s'));
    return $post_id;
}
Ejemplo n.º 5
0
 public function testDateIntervalTimezone()
 {
     //These dates are actually the same
     $date_1 = new DateTime("2013-12-05 22:00:00", new DateTimeZone('America/New_York'));
     $date_2 = new DateTime("2013-12-06 03:00:00", new DateTimeZone('UTC'));
     $this->assertEquals('0 0 0 0 0', eo_date_interval($date_1, $date_2, '%d %h %i %s %a'));
 }