'list', 'upcoming' => 'list', 'summary' => 'list', 'photo' => 'list', 'day' => 'list', 'today' => 'list', 'past' => 'past', 'month' => 'month', 'week' => 'week', 'map' => 'list', ); /** * Hook up. */ public static function init() { add_filter( 'query_vars', array( __CLASS__, 'query_vars' ) ); add_action( 'init', array( __CLASS__, 'rewrite_rules' ), 20 ); add_action( 'init', array( __CLASS__, 'shortcodes' ), 30 ); add_action( 'activated_plugin', array( __CLASS__, 'on_plugin_toggle' ) ); add_action( 'deactivated_plugin', array( __CLASS__, 'on_plugin_toggle' ) ); add_filter( 'eventwright_event_slug', array( __CLASS__, 'avoid_slug_clash' ) ); add_filter( 'eventwright_category_base', array( __CLASS__, 'avoid_category_clash' ) ); } /** * Whether The Events Calendar is running. * * @return bool */ public static function tec_active() { return class_exists( 'Tribe__Events__Main' ); } /** * Our query vars. * * @param array $vars Vars. * @return array */ public static function query_vars( $vars ) { return array_merge( $vars, array( 'evwr_view', 'evwr_month', 'evwr_week', 'evwr_day', 'evwr_date', 'evwr_ics', 'evwr_ical', 'evwr_cat', 'evwr_page' ) ); } /** * While TEC is active both plugins would claim /event/; use a temporary slug until TEC is turned off. * * @param string $slug Slug. * @return string */ public static function avoid_slug_clash( $slug ) { if ( self::tec_active() ) { return 'evw-' . $slug; } return $slug; } /** * Same for category links. * * @param string $base Base. * @return string */ public static function avoid_category_clash( $base ) { if ( self::tec_active() ) { return 'evw-' . $base; } return $base; } /** * Rebuild links after TEC (or anything) is turned on or off. */ public static function on_plugin_toggle() { update_option( 'evwr_flush_rewrites', 1 ); } /** * Path of the events page, like "events" or "whats-on/calendar". * * @return string */ public static function events_path() { $page_id = (int) Settings::get( 'events_page_id' ); if ( $page_id && 'publish' === get_post_status( $page_id ) ) { return get_page_uri( $page_id ); } return ''; } /** * Rewrite rules for our views and TEC's old URL shapes. */ public static function rewrite_rules() { if ( self::tec_active() ) { // TEC still owns these URLs; ours use temporary "evw-" slugs until it is turned off. return; } $page_id = (int) Settings::get( 'events_page_id' ); $path = self::events_path(); $single = apply_filters( 'eventwright_event_slug', Settings::get( 'event_slug' ) ); if ( $path ) { $p = preg_quote( $path, '#' ); // Category pages show the calendar filtered by that category. add_rewrite_rule( '^' . $p . '/category/(.+?)/(?:list|month|upcoming|day|week|photo|map|summary)/?$', 'index.php?page_id=' . $page_id . '&evwr_cat=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/category/(.+?)/?$', 'index.php?page_id=' . $page_id . '&evwr_cat=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/ical/?$', 'index.php?evwr_ical=1', 'top' ); } if ( ! Settings::get( 'tec_urls' ) ) { return; } if ( $path ) { $p = preg_quote( $path, '#' ); $views = implode( '|', array_keys( self::VIEWS ) ); add_rewrite_rule( '^' . $p . '/(' . $views . ')/page/([0-9]+)/?$', 'index.php?page_id=' . $page_id . '&evwr_view=$matches[1]&evwr_page=$matches[2]', 'top' ); add_rewrite_rule( '^' . $p . '/(' . $views . ')/?$', 'index.php?page_id=' . $page_id . '&evwr_view=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/([0-9]{4}-[0-9]{2})/?$', 'index.php?page_id=' . $page_id . '&evwr_month=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/([0-9]{4}-[0-9]{2}-[0-9]{2})/?$', 'index.php?page_id=' . $page_id . '&evwr_view=list&evwr_day=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/month/([0-9]{4}-[0-9]{2})(?:-[0-9]{2})?/?$', 'index.php?page_id=' . $page_id . '&evwr_month=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/week/([0-9]{4}-[0-9]{2}-[0-9]{2})/?$', 'index.php?page_id=' . $page_id . '&evwr_view=week&evwr_week=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/(?:day|list)/([0-9]{4}-[0-9]{2}-[0-9]{2})/?$', 'index.php?page_id=' . $page_id . '&evwr_view=list&evwr_day=$matches[1]', 'top' ); add_rewrite_rule( '^' . $p . '/tag/([^/]+)/?$', 'index.php?tag=$matches[1]&post_type=' . Post_Types::EVENT, 'top' ); add_rewrite_rule( '^' . $p . '/feed/?$', 'index.php?post_type=' . Post_Types::EVENT . '&feed=rss2', 'top' ); } if ( $single ) { $s = preg_quote( $single, '#' ); add_rewrite_rule( '^' . $s . '/([^/]+)/([0-9]{4}-[0-9]{2}-[0-9]{2})/?$', 'index.php?' . Post_Types::EVENT . '=$matches[1]&evwr_date=$matches[2]', 'top' ); add_rewrite_rule( '^' . $s . '/([^/]+)/ical/?$', 'index.php?' . Post_Types::EVENT . '=$matches[1]&evwr_ics=1', 'top' ); add_rewrite_rule( '^' . $s . '/([^/]+)/all/?$', 'index.php?' . Post_Types::EVENT . '=$matches[1]', 'top' ); } } /** * Old TEC shortcodes keep working once TEC is gone. */ public static function shortcodes() { if ( self::tec_active() || ! Settings::get( 'tec_urls' ) ) { return; } if ( ! shortcode_exists( 'tribe_events' ) ) { add_shortcode( 'tribe_events', array( __CLASS__, 'tribe_events_shortcode' ) ); } if ( ! shortcode_exists( 'tribe_events_list' ) ) { add_shortcode( 'tribe_events_list', array( __CLASS__, 'tribe_events_list_shortcode' ) ); } } /** * [tribe_events view="month" category="slug" ...]. * * @param array|string $atts Attributes. * @return string */ public static function tribe_events_shortcode( $atts ) { $atts = shortcode_atts( array( 'view' => 'month', 'category' => '', 'cat' => '', 'tribe-bar' => 'true', ), $atts, 'tribe_events' ); $view = isset( self::VIEWS[ $atts['view'] ] ) ? self::VIEWS[ $atts['view'] ] : 'month'; return Views::calendar( array( 'view' => 'past' === $view ? 'list' : $view, 'categories' => $atts['category'] ? $atts['category'] : $atts['cat'], 'show_filters' => 'false' !== $atts['tribe-bar'], ) ); } /** * [tribe_events_list limit="5" category="slug"]. * * @param array|string $atts Attributes. * @return string */ public static function tribe_events_list_shortcode( $atts ) { $atts = shortcode_atts( array( 'limit' => 5, 'category' => '', ), $atts, 'tribe_events_list' ); return Views::upcoming_list( array( 'limit' => absint( $atts['limit'] ), 'categories' => $atts['category'], ) ); } }