Ejemplo n.º 1
0
 /**
  * Is hooked by init() filter to parse the WP_Query arguments for main and alt queries.
  *
  * @param object  $query WP_Query object args supplied or default
  * @return object $query (modified)
  */
 public function pre_get_posts($query)
 {
     global $wp_the_query;
     $types = !empty($query->query_vars['post_type']) ? (array) $query->query_vars['post_type'] : array();
     // is the query pulling posts from the past
     $query->tribe_is_past = !empty($query->query_vars['tribe_is_past']) ? $query->query_vars['tribe_is_past'] : false;
     // check if any possiblity of this being an event query
     $query->tribe_is_event = in_array(TribeEvents::POSTTYPE, $types) ? true : false;
     // check if any possiblity of this being an event category
     $query->tribe_is_event_category = isset($query->query_vars[TribeEvents::TAXONOMY]) && $query->query_vars[TribeEvents::TAXONOMY] != '' ? true : false;
     $query->tribe_is_event_venue = in_array(TribeEvents::VENUE_POST_TYPE, $types) ? true : false;
     $query->tribe_is_event_organizer = in_array(TribeEvents::ORGANIZER_POST_TYPE, $types) ? true : false;
     $query->tribe_is_event_query = $query->tribe_is_event || $query->tribe_is_event_category || $query->tribe_is_event_venue || $query->tribe_is_event_organizer ? true : false;
     // move along, this is not the query you are looking for
     // setup static const to preserve query type through hooks
     self::$is_event = $query->tribe_is_event;
     self::$is_event_category = $query->tribe_is_event_category;
     self::$is_event_venue = $query->tribe_is_event_venue;
     self::$is_event_organizer = $query->tribe_is_event_organizer;
     self::$is_event_query = $query->tribe_is_event_query;
     if ($query === $wp_the_query && $query->is_main_query() && tribe_get_option('showEventsInMainLoop', false) && !is_page() && !is_admin() && !is_single() && !is_singular() && (is_home() && !$query->tribe_is_event_query || is_archive() || is_category() || is_tax())) {
         $query->query_vars['post_type'] = isset($query->query_vars['post_type']) ? (array) $query->query_vars['post_type'] : array('post');
         $query->query_vars['post_type'][] = TribeEvents::POSTTYPE;
     }
     if ($query->tribe_is_event || $query->tribe_is_event_category) {
         self::$start_date = null;
         self::$end_date = null;
         add_filter('posts_join', array(__CLASS__, 'posts_join'), 10, 2);
         add_filter('posts_join', array(__CLASS__, 'posts_join_orderby'), 10, 2);
         add_filter('posts_where', array(__CLASS__, 'posts_where'), 10, 2);
         add_filter('posts_fields', array(__CLASS__, 'posts_fields'), 10, 2);
         add_filter('posts_distinct', array(__CLASS__, 'posts_distinct'));
         add_filter('posts_groupby', array(__CLASS__, 'posts_groupby'), 10, 2);
         // if a user selects a date in the event bar we want it to persist as long as possible
         if (!empty($_REQUEST['tribe-bar-date'])) {
             $query->set('eventDate', $_REQUEST['tribe-bar-date']);
         }
         // if a user provides a search term we want to use that in the search params
         if (!empty($_REQUEST['tribe-bar-search'])) {
             $query->query_vars['s'] = $_REQUEST['tribe-bar-search'];
         }
         $query->query_vars['eventDisplay'] = !empty($query->query_vars['eventDisplay']) ? $query->query_vars['eventDisplay'] : TribeEvents::instance()->displaying;
         if (!empty($query->query_vars['eventDisplay'])) {
             switch ($query->query_vars['eventDisplay']) {
                 case 'custom':
                     // if set this allows for a custom query to not be burdened with these settings
                     break;
                 case 'past':
                     // setup past event display query
                     $query->set('end_date', date_i18n(TribeDateUtils::DBDATETIMEFORMAT));
                     $query->set('orderby', self::set_orderby());
                     $query->set('order', self::set_order('DESC'));
                     self::$end_date = $query->get('end_date');
                     $query->tribe_is_past = true;
                     break;
                 case 'all':
                     $query->set('orderby', self::set_orderby());
                     $query->set('order', self::set_order());
                     break;
                 case 'month':
                     $start_date = substr_replace(date_i18n(TribeDateUtils::DBDATEFORMAT), '01', -2);
                     $passed_date = $query->get('eventDate') ? substr_replace(date_i18n(TribeDateUtils::DBDATEFORMAT, strtotime($query->get('eventDate'))), '01', -2) : false;
                     $start_date = $passed_date ? $passed_date : $start_date;
                     $query->set('start_date', $start_date);
                     $query->set('eventDate', $start_date);
                     $query->set('end_date', date('Y-m-d', strtotime(TribeEvents::instance()->nextMonth($start_date)) - 24 * 3600));
                     if ($query->is_main_query()) {
                         $query->set('posts_per_page', 1);
                         // we're going to do this day-by-day later, so limit or order necessary for this
                         $query->set('no_found_rows', TRUE);
                     } else {
                         $query->set('orderby', self::set_orderby());
                         $query->set('order', self::set_order());
                         $query->set('posts_per_page', -1);
                         // get all events for the month
                     }
                     self::$start_date = $query->get('start_date');
                     self::$end_date = $query->get('end_date');
                     break;
                 case 'single-event':
                     if ($query->get('eventDate') != '') {
                         $query->set('start_date', $query->get('eventDate'));
                         $query->set('eventDate', $query->get('eventDate'));
                         self::$start_date = $query->get('start_date');
                     }
                     break;
                 case 'upcoming':
                 default:
                     // default display query
                     $start_date = date_i18n(TribeDateUtils::DBDATETIMEFORMAT);
                     $start_date = $query->get('eventDate') != '' ? $query->get('eventDate') : $start_date;
                     $query->set('hide_upcoming', true);
                     $query->set('start_date', $start_date);
                     $query->set('orderby', self::set_orderby());
                     $query->set('order', self::set_order());
                     self::$start_date = $query->get('start_date');
                     break;
             }
         } else {
             $query->set('hide_upcoming', true);
             $query->set('start_date', date_i18n(TribeDateUtils::DBDATETIMEFORMAT));
             $query->set('orderby', self::set_orderby());
             $query->set('order', self::set_order());
             self::$start_date = $query->get('start_date');
         }
         // eventCat becomes a standard taxonomy query - will need to deprecate and update views eventually
         if (!in_array($query->get(TribeEvents::TAXONOMY), array('', '-1'))) {
             $tax_query[] = array('taxonomy' => TribeEvents::TAXONOMY, 'field' => is_numeric($query->get(TribeEvents::TAXONOMY)) ? 'id' : 'slug', 'terms' => $query->get(TribeEvents::TAXONOMY), 'include_children' => false);
         }
         $meta_query[] = array('key' => '_EventStartDate', 'type' => 'DATETIME');
     }
     // filter by Venue ID
     if ($query->tribe_is_event_query && $query->get('venue') != '') {
         $meta_query[] = array('key' => '_EventVenueID', 'value' => $query->get('venue'));
     }
     // filter by Organizer ID
     if ($query->tribe_is_event_query && $query->get('organizer') != '') {
         $meta_query[] = array('key' => '_EventOrganizerID', 'value' => $query->get('organizer'));
     }
     // proprietary metaKeys go to standard meta
     if ($query->tribe_is_event_query && $query->get('metaKey') != '') {
         $meta_query[] = array('key' => $query->get('metaKey'), 'value' => $query->get('metaValue'));
     }
     // enable pagination setup
     if ($query->tribe_is_event_query && $query->get('numResults') != '') {
         $query->set('posts_per_page', $query->get('numResults'));
     } elseif ($query->tribe_is_event_query && $query->get('posts_per_page') == '') {
         $query->set('posts_per_page', (int) tribe_get_option('postsPerPage', 10));
     }
     // hide upcoming events from query (only not in admin)
     if ($query->tribe_is_event_query && $query->get('hide_upcoming')) {
         $hide_upcoming_ids = self::getHideFromUpcomingEvents();
         if (!empty($hide_upcoming_ids)) {
             $query->set('post__not_in', $hide_upcoming_ids);
         }
     }
     if ($query->tribe_is_event_query && !empty($meta_query)) {
         // setup default relation for meta queries
         $meta_query['relation'] = 'AND';
         $meta_query_combined = array_merge((array) $meta_query, (array) $query->get('meta_query'));
         $query->set('meta_query', $meta_query_combined);
     }
     if ($query->tribe_is_event_query && !empty($tax_query)) {
         // setup default relation for tax queries
         $tax_query_combined = array_merge((array) $tax_query, (array) $query->get('tax_query'));
         $query->set('tax_query', $tax_query_combined);
     }
     if ($query->tribe_is_event_query) {
         add_filter('posts_orderby', array(__CLASS__, 'posts_orderby'), 10, 2);
     }
     // if is in the admin remove the event date & upcoming filters, unless is an ajax call
     global $current_screen;
     if (is_admin() && $query->tribe_is_event_query && !empty($current_screen->id) && $current_screen->id == 'edit-' . TribeEvents::POSTTYPE) {
         if (!defined('DOING_AJAX') || defined('DOING_AJAX') && !DOING_AJAX) {
             // remove_filter( 'posts_join', array( __CLASS__, 'posts_join' ), 10, 2 );
             remove_filter('posts_where', array(__CLASS__, 'posts_where'), 10, 2);
             remove_filter('posts_fields', array(__CLASS__, 'posts_fields'));
             remove_filter('posts_distinct', array(__CLASS__, 'posts_distinct'));
             remove_filter('posts_groupby', array(__CLASS__, 'posts_groupby'));
             $query->set('post__not_in', '');
             // set the default order for posts within admin lists
             if (!isset($query->query['order'])) {
                 $query->set('order', 'DESC');
             } else {
                 // making sure we preserve the order supplied by the query string even if it is overwritten above
                 $query->set('order', $query->query['order']);
             }
         }
     }
     // check if is_event_query === true and hook filter
     if ($query->tribe_is_event_query) {
         // fixing is_home param
         $query->is_home = !empty($query->query_vars['is_home']) ? $query->query_vars['is_home'] : false;
         apply_filters('tribe_events_pre_get_posts', $query);
     }
     return $query;
 }
 /**
  * Is hooked by init() filter to parse the WP_Query arguments for main and alt queries.
  *
  * @param object  $query WP_Query object args supplied or default
  * @return object $query (modified)
  */
 public static function pre_get_posts($query)
 {
     // setup static const to preserve query type through hooks
     self::$is_event = $query->tribe_is_event;
     self::$is_event_category = $query->tribe_is_event_category;
     self::$is_event_venue = $query->tribe_is_event_venue;
     self::$is_event_organizer = $query->tribe_is_event_organizer;
     self::$is_event_query = $query->tribe_is_event_query;
     if ($query->is_main_query() && is_home()) {
         // check option for including events in the main wordpress loop, if true, add events post type
         if (tribe_get_option('showEventsInMainLoop', false)) {
             $query->query_vars['post_type'] = isset($query->query_vars['post_type']) ? (array) $query->query_vars['post_type'] : array('post');
             $query->query_vars['post_type'][] = TribeEvents::POSTTYPE;
             $query->tribe_is_multi_posttype = true;
         }
     }
     if ($query->tribe_is_multi_posttype) {
         do_action('log', 'multi_posttype', 'default', $query->tribe_is_multi_posttype);
         add_filter('posts_fields', array(__CLASS__, 'multi_type_posts_fields'), 10, 2);
         add_filter('posts_join', array(__CLASS__, 'posts_join'), 10, 2);
         add_filter('posts_join', array(__CLASS__, 'posts_join_orderby'), 10, 2);
         add_filter('posts_distinct', array(__CLASS__, 'posts_distinct'));
         add_filter('posts_groupby', array(__CLASS__, 'posts_groupby'), 10, 2);
         add_filter('posts_orderby', array(__CLASS__, 'posts_orderby'), 10, 2);
         do_action('tribe_events_pre_get_posts', $query);
         return;
     }
     if ($query->tribe_is_event || $query->tribe_is_event_category) {
         self::$start_date = null;
         self::$end_date = null;
         if (!($query->is_main_query() && $query->get('eventDisplay') == 'month')) {
             add_filter('posts_fields', array(__CLASS__, 'posts_fields'), 10, 2);
             add_filter('posts_join', array(__CLASS__, 'posts_join'), 10, 2);
             add_filter('posts_join', array(__CLASS__, 'posts_join_orderby'), 10, 2);
             add_filter('posts_where', array(__CLASS__, 'posts_where'), 10, 2);
             add_filter('posts_distinct', array(__CLASS__, 'posts_distinct'));
             add_filter('posts_groupby', array(__CLASS__, 'posts_groupby'), 10, 2);
         } else {
             // reduce number of queries triggered by main WP_Query on month view
             $query->set('posts_per_page', 1);
             $query->set('no_found_rows', true);
             $query->set('cache_results', false);
             $query->set('update_post_meta_cache', false);
             $query->set('update_post_term_cache', false);
             $query->set('meta_query', array(array('key' => '_EventStartDate', 'type' => 'DATETIME')));
             do_action('tribe_events_pre_get_posts', $query);
             return $query;
         }
         // if a user selects a date in the event bar we want it to persist as long as possible
         if (!empty($_REQUEST['tribe-bar-date'])) {
             $query->set('eventDate', $_REQUEST['tribe-bar-date']);
         }
         // if a user provides a search term we want to use that in the search params
         if (!empty($_REQUEST['tribe-bar-search'])) {
             $query->query_vars['s'] = $_REQUEST['tribe-bar-search'];
         }
         $query->query_vars['eventDisplay'] = !empty($query->query_vars['eventDisplay']) ? $query->query_vars['eventDisplay'] : TribeEvents::instance()->displaying;
         if (!empty($query->query_vars['eventDisplay'])) {
             switch ($query->query_vars['eventDisplay']) {
                 case 'custom':
                     // if set this allows for a custom query to not be burdened with these settings
                     break;
                 case 'all':
                     $query->set('orderby', self::set_orderby());
                     $query->set('order', self::set_order());
                     break;
                 case 'single-event':
                     if ($query->get('eventDate') != '') {
                         $query->set('start_date', $query->get('eventDate'));
                         $query->set('eventDate', $query->get('eventDate'));
                         self::$start_date = $query->get('start_date');
                     }
                     break;
                 case 'upcoming':
                 case 'past':
                 default:
                     // default display query
                     $tribe_paged = !empty($_REQUEST['tribe_paged']) ? $_REQUEST['tribe_paged'] : $query->get('paged');
                     $query->set('paged', $tribe_paged);
                     $event_date = $query->get('eventDate') != '' ? $query->get('eventDate') : date_i18n(TribeDateUtils::DBDATETIMEFORMAT);
                     if (!$query->tribe_is_past) {
                         $query->set('start_date', $event_date);
                         $query->set('end_date', '');
                         $query->set('order', self::set_order());
                     } else {
                         $query->set('start_date', '');
                         $query->set('end_date', $event_date);
                         $query->set('order', self::set_order('DESC'));
                     }
                     $query->set('orderby', self::set_orderby());
                     $query->set('hide_upcoming', true);
                     self::$start_date = $query->get('start_date');
                     self::$end_date = $query->get('end_date');
                     break;
                     $query->set('eventDate', '');
             }
         } else {
             $query->set('hide_upcoming', true);
             $query->set('start_date', date_i18n(TribeDateUtils::DBDATETIMEFORMAT));
             $query->set('orderby', self::set_orderby());
             $query->set('order', self::set_order());
             self::$start_date = $query->get('start_date');
         }
         // eventCat becomes a standard taxonomy query - will need to deprecate and update views eventually
         if (!in_array($query->get(TribeEvents::TAXONOMY), array('', '-1'))) {
             $tax_query[] = array('taxonomy' => TribeEvents::TAXONOMY, 'field' => is_numeric($query->get(TribeEvents::TAXONOMY)) ? 'id' : 'slug', 'terms' => $query->get(TribeEvents::TAXONOMY), 'include_children' => false);
         }
         // Only add the postmeta hack if it's not the main admin events list
         // Because this method filters out drafts without EventStartDate.
         // For this screen we're doing the JOIN manually in TribeEventsAdminList
         $screen = !is_admin() || defined('DOING_AJAX') && DOING_AJAX ? null : get_current_screen();
         if (empty($screen) || $screen->id != 'edit-tribe_events') {
             $meta_query[] = array('key' => '_EventStartDate', 'type' => 'DATETIME');
         }
     }
     // filter by Venue ID
     if ($query->tribe_is_event_query && $query->get('venue') != '') {
         $meta_query[] = array('key' => '_EventVenueID', 'value' => $query->get('venue'));
     }
     // filter by Organizer ID
     if ($query->tribe_is_event_query && $query->get('organizer') != '') {
         $meta_query[] = array('key' => '_EventOrganizerID', 'value' => $query->get('organizer'));
     }
     // proprietary metaKeys go to standard meta
     if ($query->tribe_is_event_query && $query->get('metaKey') != '') {
         $meta_query[] = array('key' => $query->get('metaKey'), 'value' => $query->get('metaValue'));
     }
     // enable pagination setup
     if ($query->tribe_is_event_query && $query->get('numResults') != '') {
         $query->set('posts_per_page', $query->get('numResults'));
     } elseif ($query->tribe_is_event_query && $query->get('posts_per_page') == '') {
         $query->set('posts_per_page', (int) tribe_get_option('postsPerPage', 10));
     }
     // hide upcoming events from query (only not in admin)
     if ($query->tribe_is_event_query && $query->get('hide_upcoming')) {
         $hide_upcoming_ids = self::getHideFromUpcomingEvents();
         if (!empty($hide_upcoming_ids)) {
             $query->set('post__not_in', $hide_upcoming_ids);
         }
     }
     if ($query->tribe_is_event_query && !empty($meta_query)) {
         // setup default relation for meta queries
         $meta_query['relation'] = 'AND';
         $meta_query_combined = array_merge((array) $meta_query, (array) $query->get('meta_query'));
         $query->set('meta_query', $meta_query_combined);
     }
     if ($query->tribe_is_event_query && !empty($tax_query)) {
         // setup default relation for tax queries
         $tax_query_combined = array_merge((array) $tax_query, (array) $query->get('tax_query'));
         $query->set('tax_query', $tax_query_combined);
     }
     if ($query->tribe_is_event_query) {
         add_filter('posts_orderby', array(__CLASS__, 'posts_orderby'), 10, 2);
     }
     // if is in the admin remove the event date & upcoming filters, unless is an ajax call
     global $current_screen;
     if (is_admin() && $query->tribe_is_event_query && !empty($current_screen->id) && $current_screen->id == 'edit-' . TribeEvents::POSTTYPE) {
         if (!defined('DOING_AJAX') || defined('DOING_AJAX') && !DOING_AJAX) {
             // remove_filter( 'posts_join', array( __CLASS__, 'posts_join' ), 10, 2 );
             remove_filter('posts_where', array(__CLASS__, 'posts_where'), 10, 2);
             remove_filter('posts_fields', array(__CLASS__, 'posts_fields'));
             remove_filter('posts_distinct', array(__CLASS__, 'posts_distinct'));
             remove_filter('posts_groupby', array(__CLASS__, 'posts_groupby'));
             $query->set('post__not_in', '');
             // set the default order for posts within admin lists
             if (!isset($query->query['order'])) {
                 $query->set('order', 'DESC');
             } else {
                 // making sure we preserve the order supplied by the query string even if it is overwritten above
                 $query->set('order', $query->query['order']);
             }
         }
     }
     if ($query->tribe_is_event_query) {
         do_action('tribe_events_pre_get_posts', $query);
     }
     return $query;
 }