Example #1
0
 /**
  * Tests where an occurrence's start time.
  */
 public function testChangeDateTime()
 {
     $_event_data = array('start' => new DateTime('2014-08-11 18:48:00', eo_get_blog_timezone()), 'end' => new DateTime('2014-08-11 19:48:00', eo_get_blog_timezone()), 'schedule' => 'weekly', 'until' => new DateTime('2014-09-01 18:48:00', eo_get_blog_timezone()), 'include' => array(new DateTime('2014-08-25 22:48:00', eo_get_blog_timezone())), 'exclude' => array(new DateTime('2014-08-25 18:48:00', eo_get_blog_timezone())));
     $event_data = _eventorganiser_generate_occurrences($_event_data);
     //echo print_r( $event_data );
     $expected = array(new DateTime('2014-08-11 18:48:00', eo_get_blog_timezone()), new DateTime('2014-08-18 18:48:00', eo_get_blog_timezone()), new DateTime('2014-08-25 22:48:00', eo_get_blog_timezone()), new DateTime('2014-09-01 18:48:00', eo_get_blog_timezone()));
     $this->assertEquals($expected, $event_data['occurrences']);
 }
Example #2
0
/**
* This functions inserts a post of event type, with data given in the $post_data
* and event data given in $event_data. Returns the post ID.
*
* Triggers {@see `eventorganiser_save_event`} passing event (post) ID
*
* The event data array can contain
*
* * `schedule` => (custom | once | daily | weekly | monthly | yearly)  -- specifies the reoccurrence pattern
* * `schedule_meta` =>
*   * For monthly schedules,
*      * (string) BYMONTHDAY=XX to repeat on XXth day of month, e.g. BYMONTHDAY=01 to repeat on the first of every month.
*      * (string) BYDAY=ND. N= 1|2|3|4|-1 (first, second, third, fourth, last). D is day of week SU|MO|TU|WE|TH|FR|SA. E.g. BYDAY=2TU (repeat on second tuesday)
*   * For weekly schedules,
*      * (array) Days to repeat on: (SU,MO,TU,WE,TH,FR,SA). e.g. set to array('SU','TU') to repeat on Tuesdays & Sundays. 
*      * Can be left blank to repeat weekly from the start date.
* * `frequency` => (int) positive integer, sets frequency of reoccurrence (every 2 days, or every 3 days etc)
* * `all_day` => 1 if its an all day event, 0 if not
* * `start` =>  start date (of first occurrence)  as a datetime object
* * `end` => end date (of first occurrence)  as a datetime object
* * `schedule_last` =>  **START** date of last occurrence (or upper-bound thereof) as a datetime object
* * `number_occurrences` => Instead of specifying `schedule_last` you can specify the number of occurrence a recurring event should have. 
* This is only used if `schedule_last` is not, and for daily, weekly, monthly or yearly recurring events.
* * `include` => array of datetime objects to include in the schedule
* * `exclude` => array of datetime objects to exclude in the schedule
*
* ### Example
* The following example creates an event which starts on the 3rd December 2012 15:00 and ends on the 4th December 15:00 and repeats every 4 days until the 25th December (So the last occurrence actually ends on the 23rd).
* <code>
*     $event_data = array(
*	     'start'=> new DateTime('2012-12-03 15:00', eo_get_blog_timezone() ),
*	     'end'=> new DateTime('2012-12-04 15:00', eo_get_blog_timezone() ),
*	     'schedule_last'=> new DateTime('2012-12-25 15:00', eo_get_blog_timezone() ),
*	     'frequency' => 4,
*	     'all_day' => 0,
*	     'schedule'=>'daily',
*    );
*     $post_data = array(
*	     'post_title'=>'The Event Title',
*	     'post_content'=>'My event content',
*    );
*
*    $e = eo_insert_event($post_data,$event_data);
* </code>
* 
* ### Tutorial
* See this <a href="http://www.stephenharris.info/2012/front-end-event-posting/">tutorial</a> or <a href="https://gist.github.com/3867194">this Gist</a> on front-end event posting.
*
* @since 1.5
* @link http://www.stephenharris.info/2012/front-end-event-posting/ Tutorial on front-end event posting
* @uses wp_insert_post() 
*
* @param array $post_data array of data to be used by wp_insert_post.
* @param array $event_data array of event data
* @return int the post ID of the updated event
*/
function eo_insert_event($post_data = array(), $event_data = array())
{
    global $wpdb;
    $input = array_merge($post_data, $event_data);
    //Backwards compat:
    if (!empty($input['venue'])) {
        $input['tax_input']['event-venue'] = $input['venue'];
    }
    if (!empty($input['category'])) {
        $input['tax_input']['event-category'] = $input['category'];
    }
    $event_keys = array_flip(array('start', 'end', 'schedule', 'schedule_meta', 'frequency', 'all_day', 'schedule_last', 'include', 'exclude', 'occurs_by', 'number_occurrences'));
    $post_keys = array_flip(array('post_title', 'post_content', 'post_status', 'post_type', 'post_author', 'ping_status', 'post_parent', 'menu_order', 'to_ping', 'pinged', 'post_password', 'guid', 'post_content_filtered', 'post_excerpt', 'import_id', 'tax_input', 'comment_status', 'context', 'post_date', 'post_date_gmt'));
    $event_data = array_intersect_key($input, $event_keys) + $event_data;
    $post_data = array_intersect_key($input, $post_keys);
    //If schedule is 'once' and dates are included - set to 'custom':
    if ((empty($event_data['schedule']) || 'once' == $event_data['schedule']) && !empty($event_data['include'])) {
        $event_data['schedule'] = 'custom';
    }
    $event_data = _eventorganiser_generate_occurrences($event_data);
    if (is_wp_error($event_data)) {
        return $event_data;
    }
    /**
     *@ignore
     */
    $event_data = apply_filters('eventorganiser_insert_event_event_data', $event_data, $post_data, $event_data);
    /**
     *@ignore
     */
    $post_data = apply_filters('eventorganiser_insert_event_post_data', $post_data, $post_data, $event_data);
    //Finally we create event (first create the post in WP)
    $post_input = array_merge(array('post_title' => 'untitled event'), $post_data, array('post_type' => 'event'));
    $post_id = wp_insert_post($post_input, true);
    //Did the event insert correctly?
    if (is_wp_error($post_id)) {
        return $post_id;
    }
    _eventorganiser_insert_occurrences($post_id, $event_data);
    //Action used to break cache & trigger Pro actions (& by other plug-ins?)
    /**
     * Triggered after an event has been updated.
     * 
     * @param int $post_id The ID of the event 
     */
    do_action('eventorganiser_save_event', $post_id);
    return $post_id;
}