diff --git a/src/includes/event-utilities.php b/src/includes/event-utilities.php new file mode 100644 index 00000000..a6a0747c --- /dev/null +++ b/src/includes/event-utilities.php @@ -0,0 +1,57 @@ + $types, + 'tax_query' => array( + array( + 'taxonomy' => $taxonomy, + 'field' => 'slug', + 'terms' => $term, + ), + ), + 'fields' => 'ids', + 'posts_per_page' => -1, + ); + + $posts = get_posts( $args ); + + return $posts; +} + +/** + * Fetch a group of events using a My Calendar group ID. Argument is an event ID, gets all events in the same group as that event. + * + * @param string $event_id A My Calendar event ID. + * + * @return array + */ +function mt_get_events_by_group_id( $event_id ) { + $events = array(); + if ( function_exists( 'mc_get_data' ) ) { + $group_id = mc_get_data( 'event_group_id', $event_id ); + $events = mc_get_grouped_events( $group_id ); + } + + return $events; +} \ No newline at end of file diff --git a/src/mt-add-to-cart.php b/src/mt-add-to-cart.php index d387f120..120be094 100644 --- a/src/mt-add-to-cart.php +++ b/src/mt-add-to-cart.php @@ -104,15 +104,16 @@ function mt_check_early_returns( $event_id, $override ) { /** * Generates event registration form. * - * @param string $content Page content. - * @param mixed/bool/int/object $event If boolean, exit. - * @param string $view Type of view for context. - * @param string $time Time view being displayed. - * @param boolean $override Don't display. - * + * @param string $content Page content. + * @param bool|int|object $event If boolean, exit. + * @param string $view Type of view for context. + * @param string $time Time view being displayed. + * @param bool $override Don't display. + * @param bool|array $group If grouped display, array with first and last IDs. + * @return string */ -function mt_registration_form( $content, $event = false, $view = 'calendar', $time = 'month', $override = false ) { +function mt_registration_form( $content, $event = false, $view = 'calendar', $time = 'month', $override = false, $group = false ) { $options = array_merge( mt_default_settings(), get_option( 'mt_settings', array() ) ); $continue = mt_check_early_returns( $event, $override ); if ( ! $continue ) { @@ -238,7 +239,8 @@ function mt_registration_form( $content, $event = false, $view = 'calendar', $ti $in_cart $remaining_notice -
$form
" . $fields . "+ $form + $fields" . "
diff --git a/src/mt-cpt.php b/src/mt-cpt.php index ed4d121b..76cfb122 100644 --- a/src/mt-cpt.php +++ b/src/mt-cpt.php @@ -755,6 +755,41 @@ function mt_add() { } } +/** + * Add a grouping taxonomy to any post type enabled for ticketing. + */ +function mt_add_taxonomy() { + $options = array_merge( mt_default_settings(), get_option( 'mt_settings', array() ) ); + + // Labels for locations. + $group_labels = array( + 'name' => __( 'Ticket Groups', 'my-tickets' ), + 'singular_name' => __( 'Ticket Group', 'my-tickets' ), + 'search_items' => __( 'Search Ticket Groups', 'my-tickets' ), + 'popular_items' => __( 'Popular Ticket Groups', 'my-tickets' ), + 'all_items' => __( 'All Ticket Groups', 'my-tickets' ), + 'edit_item' => __( 'Edit Ticket Group', 'my-tickets' ), + 'update_item' => __( 'Update Ticket Group', 'my-tickets' ), + 'add_new_item' => __( 'Add Ticket Group', 'my-tickets' ), + 'new_item_name' => __( 'New Ticket Group', 'my-tickets' ), + 'not_found' => __( 'No Ticket Groups found', 'my-tickets' ), + ); + register_taxonomy( + 'mt-event-group', + // Internal name = machine-readable taxonomy name. + $options['mt_post_types'], + array( + 'hierarchical' => true, + 'label' => __( 'Ticket Groups', 'my-tickets' ), + 'labels' => $group_labels, + 'query_var' => true, + 'rewrite' => array( 'slug' => 'ticket-group' ), + 'show_in_rest' => true, + ) + ); +} +add_action( 'wp_loaded', 'mt_add_taxonomy' ); + /** * Add column to show whether a post has event characteristics to post manager. * diff --git a/src/mt-shortcodes.php b/src/mt-shortcodes.php index 47614290..678f08f6 100644 --- a/src/mt-shortcodes.php +++ b/src/mt-shortcodes.php @@ -50,7 +50,6 @@ function my_tickets_short_cart() { "; } -add_shortcode( 'ticket', 'mt_registration_form_shortcode' ); /** * Shortcode to generate ticketing form. Required attribute: event="event_id" * @@ -74,7 +73,7 @@ function mt_registration_form_shortcode( $atts, $content = '' ) { return ''; } -add_shortcode( 'tickets', 'mt_featured_tickets' ); +add_shortcode( 'ticket', 'mt_registration_form_shortcode' ); /** * Produce a list of featured tickets with a custom template and registration forms. @@ -85,9 +84,12 @@ function mt_registration_form_shortcode( $atts, $content = '' ) { * @return string */ function mt_featured_tickets( $atts, $content = '' ) { - $atts = shortcode_atts( + $grouped = false; + $atts = shortcode_atts( array( 'events' => false, + 'group' => false, + 'taxonomy' => 'mt-event-group', 'view' => 'calendar', 'time' => 'month', 'template' => '
{post_excerpt}
', @@ -96,6 +98,16 @@ function mt_featured_tickets( $atts, $content = '' ) { ); if ( $atts['events'] ) { $events = explode( ',', $atts['events'] ); + } elseif ( $atts['group'] ) { + if ( is_string( $atts['group'] ) ) { + // Gets a group of events by taxonomy term. + $events = mt_get_events_by_term( $atts['group'], $atts['taxonomy'] ); + } + if ( is_numeric( $atts['group'] ) ) { + // Gets a group of events by My Calendar event group ID. + $events = mt_get_events_by_group_id( $atts['group'] ); + } + $grouped = true; } else { /** * Set an array of default event IDs to show in the [tickets] shortcode. Only runs if the 'events' shortcode attribute is false. @@ -111,7 +123,17 @@ function mt_featured_tickets( $atts, $content = '' ) { $events = apply_filters( 'mt_default_ticketed_events', array(), $atts, $content ); } $content = ''; - if ( is_array( $events ) ) { + if ( is_array( $events ) && ! empty( $events ) ) { + $group = false; + if ( $grouped ) { + $count = count( $events ); + $last = $events[ $count - 1 ]; + $first = $events[0]; + $group = array( + 'first' => $first, + 'last' => $last, + ); + } foreach ( $events as $event ) { $event_data = get_post_meta( $event, '_mc_event_data', true ); $post = get_post( $event, ARRAY_A ); @@ -127,14 +149,14 @@ function mt_featured_tickets( $atts, $content = '' ) { */ $data = apply_filters( 'mt_ticket_template_array', array_merge( $event_data, $post ) ); $event_data = "