Beispiel #1
0
/**
 * Generates an ics file for a single event 
 */
function em_ical_item()
{
    global $wpdb, $wp_query, $wp_rewrite;
    //check if we're outputting an ical feed
    if (!empty($wp_query) && $wp_query->get('ical')) {
        $execute_ical = false;
        $filename = 'events';
        $args = array();
        //single event
        if ($wp_query->get(EM_POST_TYPE_EVENT)) {
            $event_id = $wpdb->get_var('SELECT event_id FROM ' . EM_EVENTS_TABLE . " WHERE event_slug='" . $wp_query->get(EM_POST_TYPE_EVENT) . "' AND event_status=1 LIMIT 1");
            if (!empty($event_id)) {
                $filename = $wp_query->get(EM_POST_TYPE_EVENT);
                $args['event'] = $event_id;
            }
            //single location
        } elseif ($wp_query->get(EM_POST_TYPE_LOCATION)) {
            $location_id = $wpdb->get_var('SELECT location_id FROM ' . EM_LOCATIONS_TABLE . " WHERE location_slug='" . $wp_query->get(EM_POST_TYPE_LOCATION) . "' AND location_status=1 LIMIT 1");
            if (!empty($location_id)) {
                $filename = $wp_query->get(EM_POST_TYPE_LOCATION);
                $args['location'] = $location_id;
            }
            //taxonomies
        } else {
            $taxonomies = EM_Object::get_taxonomies();
            foreach ($taxonomies as $tax_arg => $taxonomy_info) {
                $taxonomy_term = $wp_query->get($taxonomy_info['query_var']);
                if ($taxonomy_term) {
                    $filename = $taxonomy_term;
                    $args[$tax_arg] = $taxonomy_term;
                }
            }
        }
        //only output the ical if we have a match from above
        if (count($args) > 0) {
            //send headers and output ical
            header('Content-type: text/calendar; charset=utf-8');
            header('Content-Disposition: inline; filename="' . $filename . '.ics"');
            em_locate_template('templates/ical.php', true, array('args' => $args));
            exit;
        } else {
            //no item exists, so redirect to original URL
            $url_to_redirect = preg_replace("/ical\\/\$/", '', esc_url_raw(add_query_arg(array('ical' => null))));
            wp_redirect($url_to_redirect, '302');
            exit;
        }
    }
}
Beispiel #2
0
 public static function rewrite_rules_array($rules)
 {
     global $wpdb;
     //get the slug of the event page
     $events_page_id = get_option('dbem_events_page');
     $events_page = get_post($events_page_id);
     $em_rules = array();
     if (is_object($events_page)) {
         $events_slug = urldecode(preg_replace('/\\/$/', '', str_replace(trailingslashit(home_url()), '', get_permalink($events_page_id))));
         $events_slug = !empty($events_slug) ? trailingslashit($events_slug) : $events_slug;
         $em_rules[$events_slug . '(\\d{4}-\\d{2}-\\d{2})$'] = 'index.php?pagename=' . $events_slug . '&calendar_day=$matches[1]';
         //event calendar date search
         if ($events_page_id != get_option('page_on_front') && EM_POST_TYPE_EVENT_SLUG != $events_slug) {
             //ignore this rule if events page is the home page
             $em_rules[$events_slug . 'rss/?$'] = 'index.php?post_type=' . EM_POST_TYPE_EVENT . '&feed=feed';
             //rss page
             $em_rules[$events_slug . 'feed/?$'] = 'index.php?post_type=' . EM_POST_TYPE_EVENT . '&feed=feed';
             //compatible rss page
         }
         if (EM_POST_TYPE_EVENT_SLUG . '/' == $events_slug) {
             //won't apply on homepage
             //make sure we hard-code rewrites for child pages of events
             $child_posts = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_parent={$events_page->ID} AND post_type='page' AND post_status='publish'");
             foreach ($child_posts as $child_post) {
                 $em_rules[$events_slug . urldecode($child_post->post_name) . '/?$'] = 'index.php?page_id=' . $child_post->ID;
                 //single event booking form with slug    //check if child page has children
                 $grandchildren = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_parent={$child_post->ID} AND post_type='page' AND post_status='publish'");
                 if (count($grandchildren) != 0) {
                     foreach ($grandchildren as $grandchild) {
                         $em_rules[$events_slug . urldecode($child_post->post_name) . '/' . urldecode($grandchild->post_name) . '/?$'] = 'index.php?page_id=' . $grandchild->ID;
                     }
                 }
             }
         } elseif (empty($events_slug)) {
             //hard code homepage child pages
             $child_posts = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_parent={$events_page->ID} AND post_type='page' AND post_status='publish'");
             foreach ($child_posts as $child_post) {
                 $em_rules[$events_page->post_name . '/' . urldecode($child_post->post_name) . '/?$'] = 'index.php?page_id=' . $child_post->ID;
                 //single event booking form with slug    //check if child page has children
                 $grandchildren = $wpdb->get_results("SELECT ID, post_name FROM {$wpdb->posts} WHERE post_parent={$child_post->ID} AND post_type='page' AND post_status='publish'");
                 if (count($grandchildren) != 0) {
                     foreach ($grandchildren as $grandchild) {
                         $em_rules[$events_slug . urldecode($child_post->post_name) . '/' . urldecode($grandchild->post_name) . '/?$'] = 'index.php?page_id=' . $grandchild->ID;
                     }
                 }
             }
         }
         //global links hard-coded
         if (EM_MS_GLOBAL && !get_site_option('dbem_ms_global_events_links', true)) {
             //MS Mode has slug also for global links
             $em_rules[$events_slug . get_site_option('dbem_ms_events_slug', EM_EVENT_SLUG) . '/(.+)$'] = 'index.php?pagename=' . $events_slug . '&em_redirect=1&event_slug=$matches[1]';
             //single event from subsite
         }
         //add redirection for backwards compatability
         $em_rules[$events_slug . EM_EVENT_SLUG . '/(.+)$'] = 'index.php?pagename=' . $events_slug . '&em_redirect=1&event_slug=$matches[1]';
         //single event
         $em_rules[$events_slug . EM_LOCATION_SLUG . '/(.+)$'] = 'index.php?pagename=' . $events_slug . '&em_redirect=1&location_slug=$matches[1]';
         //single location page
         $em_rules[$events_slug . EM_CATEGORY_SLUG . '/(.+)$'] = 'index.php?pagename=' . $events_slug . '&em_redirect=1&category_slug=$matches[1]';
         //single category page slug
         //add a rule that ensures that the events page is found and used over other pages
         $em_rules[trim($events_slug, '/') . '/?$'] = 'index.php?pagename=' . trim($events_slug, '/');
     } else {
         $events_slug = EM_POST_TYPE_EVENT_SLUG;
         $em_rules[$events_slug . '/(\\d{4}-\\d{2}-\\d{2})$'] = 'index.php?post_type=' . EM_POST_TYPE_EVENT . '&calendar_day=$matches[1]';
         //event calendar date search
         if (get_option('dbem_rsvp_enabled')) {
             if (!get_option('dbem_my_bookings_page') || !is_object(get_post(get_option('dbem_my_bookings_page')))) {
                 //only added if bookings page isn't assigned
                 $em_rules[$events_slug . '/my\\-bookings$'] = 'index.php?post_type=' . EM_POST_TYPE_EVENT . '&bookings_page=1';
                 //page for users to manage bookings
             }
         }
         //check for potentially conflicting posts with the same slug as events
         $conflicting_posts = get_posts(array('name' => EM_POST_TYPE_EVENT_SLUG, 'post_type' => 'any', 'numberposts' => 0));
         if (count($conflicting_posts) > 0) {
             //won't apply on homepage
             foreach ($conflicting_posts as $conflicting_post) {
                 //make sure we hard-code rewrites for child pages of events
                 $child_posts = get_posts(array('post_type' => 'any', 'post_parent' => $conflicting_post->ID, 'numberposts' => 0));
                 foreach ($child_posts as $child_post) {
                     $em_rules[EM_POST_TYPE_EVENT_SLUG . '/' . urldecode($child_post->post_name) . '/?$'] = 'index.php?page_id=' . $child_post->ID;
                     //single event booking form with slug
                     //check if child page has children
                     $grandchildren = get_pages('child_of=' . $child_post->ID);
                     if (count($grandchildren) != 0) {
                         foreach ($grandchildren as $grandchild) {
                             $em_rules[$events_slug . urldecode($child_post->post_name) . '/' . urldecode($grandchild->post_name) . '/?$'] = 'index.php?page_id=' . $grandchild->ID;
                         }
                     }
                 }
             }
         }
     }
     $em_rules = apply_filters('em_rewrite_rules_array_events', $em_rules, $events_slug);
     //make sure there's no page with same name as archives, that should take precedence as it can easily be deleted wp admin side
     $em_query = new WP_Query(array('pagename' => EM_POST_TYPE_EVENT_SLUG));
     if ($em_query->have_posts()) {
         $em_rules[trim(EM_POST_TYPE_EVENT_SLUG, '/') . '/?$'] = 'index.php?pagename=' . trim(EM_POST_TYPE_EVENT_SLUG, '/');
         wp_reset_postdata();
     }
     //make sure there's no page with same name as archives, that should take precedence as it can easily be deleted wp admin side
     $em_query = new WP_Query(array('pagename' => EM_POST_TYPE_LOCATION_SLUG));
     if ($em_query->have_posts()) {
         $em_rules[trim(EM_POST_TYPE_LOCATION_SLUG, '/') . '/?$'] = 'index.php?pagename=' . trim(EM_POST_TYPE_LOCATION_SLUG, '/');
         wp_reset_postdata();
     }
     //If in MS global mode and locations are linked on same site
     if (EM_MS_GLOBAL && !get_site_option('dbem_ms_global_locations_links', true)) {
         $locations_page_id = get_option('dbem_locations_page');
         $locations_page = get_post($locations_page_id);
         if (is_object($locations_page)) {
             $locations_slug = preg_replace('/\\/$/', '', str_replace(trailingslashit(home_url()), '', get_permalink($locations_page_id)));
             $locations_slug_slashed = !empty($locations_slug) ? trailingslashit($locations_slug) : $locations_slug;
             $em_rules[$locations_slug . '/' . get_site_option('dbem_ms_locations_slug', EM_LOCATION_SLUG) . '/(.+)$'] = 'index.php?pagename=' . $locations_slug_slashed . '&location_slug=$matches[1]';
             //single event booking form with slug
         }
     }
     //add ical CPT endpoints
     $em_rules[EM_POST_TYPE_EVENT_SLUG . "/([^/]+)/ical/?\$"] = 'index.php?' . EM_POST_TYPE_EVENT . '=$matches[1]&ical=1';
     if (get_option('dbem_locations_enabled')) {
         $em_rules[EM_POST_TYPE_LOCATION_SLUG . "/([^/]+)/ical/?\$"] = 'index.php?' . EM_POST_TYPE_LOCATION . '=$matches[1]&ical=1';
     }
     //add ical taxonomy endpoints
     $taxonomies = EM_Object::get_taxonomies();
     foreach ($taxonomies as $tax_arg => $taxonomy_info) {
         //set the dynamic rule for this taxonomy
         $em_rules[$taxonomy_info['slug'] . "/([^/]+)/ical/?\$"] = 'index.php?' . $taxonomy_info['query_var'] . '=$matches[1]&ical=1';
     }
     //add RSS location CPT endpoint
     if (get_option('dbem_locations_enabled')) {
         $em_rules[EM_POST_TYPE_LOCATION_SLUG . "/([^/]+)/rss/?\$"] = 'index.php?' . EM_POST_TYPE_LOCATION . '=$matches[1]&rss=1';
     }
     return $em_rules + $rules;
 }