/**
  * Front-end display of widget.
  *
  * @see WP_Widget::widget()
  *
  * @param array $args     Widget arguments.
  * @param array $instance Saved values from database.
  */
 public function widget($args, $instance)
 {
     extract($args);
     global $post;
     /* User-selected settings. */
     $title = apply_filters('widget_title', $instance['title']);
     $venue = $instance['venue'];
     $category = $instance['category'];
     $postcount = $instance['postcount'];
     $template = $instance['template'];
     $before = $instance['before'];
     $after = $instance['after'];
     $emptyevents = $instance['emptyevents'];
     $offset = $instance['offset'];
     /* Before widget (defined by themes). */
     echo $before_widget;
     /* Event category filter args */
     $taxCategory = NULL;
     if ($category !== "all") {
         $taxCategory = array('taxonomy' => 'am_event_categories', 'field' => 'name', 'terms' => $category);
     }
     /* Venue filter args */
     $taxVenue = NULL;
     if ($venue !== "all") {
         $taxVenue = array('taxonomy' => 'am_venues', 'field' => 'name', 'terms' => $venue);
     }
     /* WP_Query args */
     $args = array('post_type' => 'am_event', 'post_status' => 'publish', 'posts_per_page' => $postcount, 'tax_query' => array('relation' => 'AND', $taxCategory, $taxVenue), 'meta_key' => 'am_startdate', 'orderby' => 'meta_value', 'order' => 'ASC', 'meta_query' => array(array('key' => 'am_enddate', 'value' => date(am_get_default_date_format(), time() - intval($offset)), 'compare' => ">")));
     /* Title of widget (before and after defined by themes). */
     if (!empty($title)) {
         echo $before_title . $title . $after_title;
     }
     echo $before;
     $loop = new WP_Query($args);
     if (!$loop->have_posts()) {
         echo $emptyevents;
     } else {
         while ($loop->have_posts()) {
             $loop->the_post();
             $post_id = get_the_ID();
             // Old template system (1.3.1 and older)
             $output = $this->parse_event_old($template);
             // new parsing system, 1.4.0 and newer
             $output = $this->parse_event($output);
             echo $output;
         }
     }
     echo $after;
     /* After widget (defined by themes). */
     echo $after_widget;
 }
Ejemplo n.º 2
0
/**
 * Save event meta and create recurring events.
 * @return type
 */
function am_save_event($post_id)
{
    // Quick edit
    $_POST += array("am_quickedit_nonce" => '');
    if (wp_verify_nonce($_POST["am_quickedit_nonce"], plugin_basename(__FILE__))) {
        // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want
        // to do anything
        if ($_POST['post_type'] !== 'am_event') {
            return;
        }
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
        if (isset($_REQUEST['am_startdate'])) {
            $startdate = strtotime($_REQUEST['am_startdate']);
            if ($startdate) {
                update_post_meta($post_id, 'am_startdate', date(am_get_default_date_format(), $startdate));
            }
        }
        if (isset($_REQUEST['am_enddate'])) {
            $enddate = strtotime($_REQUEST['am_enddate']);
            if ($enddate) {
                update_post_meta($post_id, 'am_enddate', date(am_get_default_date_format(), $enddate));
            }
        }
    } else {
        // Remove save_post action to avoid infinite loop when calling wp_insert_posts
        remove_action('save_post', 'am_save_event');
        if (!isset($_POST['post_ID'])) {
            return;
        }
        $post_id = $_POST['post_ID'];
        $orig_post = get_post($post_id);
        if ($_POST && get_post_type($orig_post) === 'am_event') {
            // Check if 'Recurrent Event' has been checked
            if (isset($_POST['am_recurrent'])) {
                $recurrent = $_POST['am_recurrent'];
                if ($recurrent === 'yes') {
                    // If so, create the events
                    $recurrent_amount = $_POST['am_recurrent_amount'];
                    $recurrence_type = $_POST['am_recurrence_type'];
                    // Limit number of created events between 2 and 99
                    if ($recurrent_amount < 2 || $recurrent_amount > 99) {
                        return;
                    }
                    //change recurrence id
                    $recurrence_id = am_create_recurrence_id($post_id);
                    update_post_meta($post_id, 'am_recurrence_id', $recurrence_id);
                    $startdate = get_post_meta($post_id, 'am_startdate', true);
                    $enddate = get_post_meta($post_id, 'am_enddate', true);
                    $start = DateTime::createFromFormat(am_get_default_date_format(), $startdate);
                    $end = DateTime::createFromFormat(am_get_default_date_format(), $enddate);
                    for ($i = 1; $i < $recurrent_amount; $i++) {
                        $new_post = array('post_title' => $orig_post->post_title, 'post_content' => $orig_post->post_content, 'post_status' => $orig_post->post_status, 'post_date' => $orig_post->post_date, 'post_author' => $orig_post->post_author, 'post_type' => $orig_post->post_type, 'post_category' => $orig_post->post_category, 'post_excerpt' => $orig_post->post_excerpt, 'comment_status' => $orig_post->comment_status, 'ping_status' => $orig_post->ping_status, 'post_password' => $orig_post->post_password);
                        $new_post_id = wp_insert_post($new_post);
                        wp_set_post_tags($new_post_id, wp_get_post_tags($post_id));
                        set_post_thumbnail($new_post_id, get_post_thumbnail_id($post_id));
                        switch ($recurrence_type) {
                            case 'am_weekly':
                                $start->modify('+7 days');
                                $end->modify('+7 days');
                                break;
                            case 'am_biweekly':
                                $start->modify('+14 days');
                                $end->modify('+14 days');
                                break;
                            default:
                                return;
                        }
                        update_post_meta($new_post_id, 'am_startdate', $start->format(am_get_default_date_format()));
                        update_post_meta($new_post_id, 'am_enddate', $end->format(am_get_default_date_format()));
                        update_post_meta($new_post_id, 'am_recurrence_id', $recurrence_id);
                        $eventCategories = wp_get_post_terms($post_id, 'am_event_categories');
                        $venues = wp_get_post_terms($post_id, 'am_venues');
                        foreach ($eventCategories as $c) {
                            wp_set_post_terms($new_post_id, $c->term_id, 'am_event_categories', true);
                        }
                        foreach ($venues as $v) {
                            wp_set_post_terms($new_post_id, $v->term_id, 'am_venues', true);
                        }
                    }
                    // Notify user when recurrent events have been created.
                    am_add_admin_message('<p>' . sprintf(__('Created %d recurrent events.', 'am-events') . '</p>', $recurrent_amount));
                }
            }
            // Determine if the specified post is not a revision or auto-save
            if (!(wp_is_post_revision($post_id) && wp_is_post_autosave($post_id))) {
                // Check if 'Update All' has been clicked
                if (isset($_POST['submit_all']) && $_POST['submit_all'] === 'yes') {
                    // Update all recurring events
                    $recurrence_id = get_post_meta($post_id, 'am_recurrence_id', true);
                    $args = array('post_type' => 'am_event', 'post_status' => 'any', 'post_count' => 99999, 'posts_per_page' => 99999, 'meta_query' => array(array('key' => 'am_recurrence_id', 'value' => $recurrence_id, 'compare' => "=")), 'post__not_in' => array($post_id));
                    $the_query = new WP_Query($args);
                    $post_count = $the_query->post_count;
                    while ($the_query->have_posts()) {
                        $the_query->the_post();
                        $id = get_the_ID();
                        $recurrent_post = array('ID' => $id, 'post_title' => $orig_post->post_title, 'post_content' => $orig_post->post_content, 'post_status' => $orig_post->post_status, 'post_author' => $orig_post->post_author, 'post_excerpt' => $orig_post->post_excerpt, 'comment_status' => $orig_post->comment_status, 'ping_status' => $orig_post->ping_status, 'post_password' => $orig_post->post_password);
                        remove_action('save_post', 'am_save_custom_meta');
                        wp_update_post($recurrent_post);
                        add_action('save_post', 'am_save_custom_meta');
                        wp_set_post_tags($id, wp_get_post_tags($post_id));
                        set_post_thumbnail($id, get_post_thumbnail_id($post_id));
                        // Clear all event categories and venues
                        wp_delete_object_term_relationships($id, 'am_event_categories');
                        wp_delete_object_term_relationships($id, 'am_venues');
                        // Update event categories and venues to match current post
                        $event_categories = wp_get_post_terms($post_id, 'am_event_categories');
                        $venues = wp_get_post_terms($post_id, 'am_venues');
                        foreach ($event_categories as $c) {
                            wp_set_post_terms($id, $c->term_id, 'am_event_categories', true);
                        }
                        foreach ($venues as $v) {
                            wp_set_post_terms($id, $v->term_id, 'am_venues', true);
                        }
                    }
                    // Notify user when recurrent events have been created.
                    am_add_admin_message('<p>' . sprintf(__('%d recurring events updated.', 'am-events') . '</p>', $post_count));
                }
            }
        }
    }
}