<?php

/*
 * PHP: 5 >= 5.3.0
 */
global $post;
$event = new Pronamic_WP_Event($post);
$repeat_helper = new Pronamic_Events_RepeatEventHelper($event);
$options = array('0' => __('&mdash; Select Repeat &mdash;', 'pronamic_events'), 'daily' => __('Daily', 'pronamic_events'), 'weekly' => __('Weekly', 'pronamic_events'), 'monthly' => __('Monthly', 'pronamic_events'), 'annually' => __('Annually', 'pronamic_events'));
$options_interval_suffix = array('daily' => __('days', 'pronamic_events'), 'weekly' => __('weeks', 'pronamic_events'), 'monthly' => __('moths', 'pronamic_events'), 'annually' => __('year', 'pronamic_events'));
$repeat = get_post_meta($post->ID, '_pronamic_event_repeat', true);
$frequency = get_post_meta($post->ID, '_pronamic_event_repeat_frequency', true);
$interval = get_post_meta($post->ID, '_pronamic_event_repeat_interval', true);
$ends_on = get_post_meta($post->ID, '_pronamic_event_ends_on', true);
$ends_on_count = get_post_meta($post->ID, '_pronamic_event_ends_on_count', true);
$ends_on_until = get_post_meta($post->ID, '_pronamic_event_ends_on_until', true);
?>

<?php 
if (0 != $post->post_parent) {
    ?>

	<p>
		<em><?php 
    printf(__('This event is part of a <a href="%s">repeated series</a>.', 'pronamic_events'), get_edit_post_link($post->post_parent));
    ?>
</em>
	</p>

<?php 
} else {
 /**
  * Save repeats
  *
  * @param string $post_id
  */
 public function save_repeats($post_id)
 {
     // Create repeated posts
     $post = get_post($post_id);
     $event = new Pronamic_WP_Event($post);
     $repeat_helper = new Pronamic_Events_RepeatEventHelper($event);
     $repeat_events = $repeat_helper->get_repeat_events();
     $data = new ArrayIterator($repeat_helper->get_period_data());
     $data = new LimitIterator($data, 0, Pronamic_Events_RepeatModule::MAX_REPEATS);
     if ($repeat_helper->is_repeat_enabled() && $data) {
         // Remove filters
         remove_filter('save_post', array($this->plugin->admin, 'save_post'));
         remove_filter('save_post', array($this, 'save_post'));
         remove_filter('save_post', array($this, 'save_repeats'));
         foreach ($data as $e) {
             $hash_code = $e->get_event_hash_code();
             $post_data = array('post_title' => $post->post_title, 'post_content' => $post->post_content, 'post_author' => $post->post_author, 'post_parent' => $post->ID, 'post_status' => $post->post_status, 'post_type' => $post->post_type);
             if (isset($repeat_events[$hash_code])) {
             } else {
                 $repeat_post_id = wp_insert_post($post_data);
                 $start_timestamp = $e->get_start()->format('U');
                 $end_timestamp = $e->get_end()->format('U');
                 $meta = array();
                 $meta = pronamic_events_get_start_date_meta($start_timestamp, $meta);
                 $meta = pronamic_events_get_end_date_meta($end_timestamp, $meta);
                 // Save meta data
                 foreach ($meta as $key => $value) {
                     update_post_meta($repeat_post_id, $key, $value);
                 }
             }
         }
         /*
          * Sync posts
          */
         // Post
         $post_data = array('post_title' => $post->post_title, 'post_content' => $post->post_content, 'post_author' => $post->post_author, 'post_parent' => $post->ID, 'post_status' => $post->post_status, 'post_type' => $post->post_type);
         // Meta
         $ignore = array_flip(array('_edit_last', '_pronamic_start_date', '_pronamic_event_start_date', '_pronamic_event_start_date_gmt', '_pronamic_end_date', '_pronamic_event_end_date', '_pronamic_event_end_date_gmt', '_pronamic_event_repeat', '_pronamic_event_repeat_frequency', '_pronamic_event_repeat_interval', '_pronamic_event_ends_on', '_pronamic_event_ends_on_count', '_pronamic_event_ends_on_until', '_edit_lock'));
         $post_custom = get_post_custom($post->ID);
         $post_custom = array_diff_key($post_custom, $ignore);
         // Taxonomies
         $taxonomies = array();
         $taxonomy_names = get_object_taxonomies($post);
         foreach ($taxonomy_names as $taxonomy) {
             $terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids'));
             $taxonomies[$taxonomy] = $terms;
         }
         // Posts
         $args = $repeat_helper->get_repeat_posts_query_args(array('fields' => 'ids'));
         $post_ids = get_posts($args);
         foreach ($post_ids as $post_id) {
             // Post
             $post_data['ID'] = $post_id;
             wp_update_post($post_data);
             // Meta
             foreach ($post_custom as $meta_key => $meta_values) {
                 delete_post_meta($post_id, $meta_key);
                 foreach ($meta_values as $meta_value) {
                     add_post_meta($post_id, $meta_key, $meta_value);
                 }
             }
             // Taxonomies
             foreach ($taxonomies as $taxonomy => $terms) {
                 wp_set_object_terms($post_id, $terms, $taxonomy);
             }
         }
         // Add filters
         add_filter('save_post', array($this->plugin->admin, 'save_post'));
         add_filter('save_post', array($this, 'save_post'));
         add_filter('save_post', array($this, 'save_repeats'));
     }
 }
<?php

/*
 * PHP: 5 >= 5.3.0
 */
global $post;
$event = new Pronamic_WP_Event($post);
$repeat_helper = new Pronamic_Events_RepeatEventHelper($event);
$repeat_events = $repeat_helper->get_repeat_events();
$data = $repeat_helper->get_period_data();
if ($repeat_events) {
    ?>

	<table class="pronamic-event-repeats-table">
		<thead>
			<tr>
				<th scope="col"><?php 
    _e('Title', 'pronamic_events');
    ?>
</th>
				<th scope="col"><?php 
    _e('Start Date', 'pronamic_events');
    ?>
</th>
				<th scope="col"><?php 
    _e('End Date', 'pronamic_events');
    ?>
</th>
				<th scope="col"><?php 
    _e('In Series', 'pronamic_events');
    ?>