Skip to content

Commit

Permalink
Mechanisms for fetching groups of events by calendar ID or taxonomy t…
Browse files Browse the repository at this point in the history
…erms
  • Loading branch information
joedolson committed Dec 15, 2023
1 parent 2a7a37c commit 55779b1
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 16 deletions.
57 changes: 57 additions & 0 deletions src/includes/event-utilities.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Utilities for fetching ticketed events.
*
* @category Events
* @package My Tickets
* @author Joe Dolson
* @license GPLv2 or later
* @link https://www.joedolson.com/my-tickets/
*/

/**
* Fetch a group of events on a taxonomy term.
*
* @param string|array $term A term slug or array of term slugs.
* @param string $taxonomy A taxonomy name. Optional; default 'mt-event-group'.
* @param array $types An array of post type names. Optiona; default all enabled types.
*
* @return array
*/
function mt_get_events_by_term( $term, $taxonomy = 'mt-event-group', $types = array() ) {
$options = array_merge( mt_default_settings(), get_option( 'mt_settings', array() ) );
$types = ( empty( $types ) ) ? $options['mt_post_types'] : $types;
$args = array(
'post_type' => $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;
}
18 changes: 10 additions & 8 deletions src/mt-add-to-cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -238,7 +239,8 @@ function mt_registration_form( $content, $event = false, $view = 'calendar', $ti
<legend>$legend</legend>
$in_cart
$remaining_notice
<p>$form</p>" . $fields . "<p>
$form
$fields" . "<p>
<button type='submit' name='mt_add_to_cart'" . $disabled . '>' . apply_filters( 'mt_add_to_cart_text', __( 'Add to Cart', 'my-tickets' ) ) . "<span class='mt-processing'><img src='" . admin_url( 'images/spinner-2x.gif' ) . "' alt='" . __( 'Working', 'my-tickets' ) . "' /></span></button>
<input type='hidden' name='my-tickets' value='true' />
</p>
Expand Down
35 changes: 35 additions & 0 deletions src/mt-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
39 changes: 31 additions & 8 deletions src/mt-shortcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ function my_tickets_short_cart() {
</div>";
}

add_shortcode( 'ticket', 'mt_registration_form_shortcode' );
/**
* Shortcode to generate ticketing form. Required attribute: event="event_id"
*
Expand All @@ -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.
Expand All @@ -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' => '<h3>{post_title}: {event_begin format="l, F d"}</h3><p>{post_excerpt}</p>',
Expand All @@ -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.
Expand All @@ -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 );
Expand All @@ -127,14 +149,14 @@ function mt_featured_tickets( $atts, $content = '' ) {
*/
$data = apply_filters( 'mt_ticket_template_array', array_merge( $event_data, $post ) );
$event_data = "<div class='mt-event-details'>" . mt_draw_template( $data, $atts['template'] ) . '</div>';
$content .= "<div class='mt-event-item'>" . $event_data . mt_registration_form( '', $event, $atts['view'], $atts['time'], true ) . '</div>';
$content .= "<div class='mt-event-item'>" . $event_data . mt_registration_form( '', $event, $atts['view'], $atts['time'], true, $group ) . '</div>';
}
}
}

return "<div class='mt-event-list'>" . $content . '</div>';
}
add_shortcode( 'remaining', 'mt_remaining_tickets' );
add_shortcode( 'tickets', 'mt_featured_tickets' );

/**
* Display the number of tickets remaining for an event.
Expand Down Expand Up @@ -177,9 +199,8 @@ function mt_remaining_tickets( $atts, $content = '' ) {

return $content;
}
add_shortcode( 'remaining', 'mt_remaining_tickets' );

// Add {register} form to My Calendar templating for upcoming events lists, etc.
add_filter( 'mc_filter_shortcodes', 'mt_add_shortcode', 5, 2 );
/**
* Insert {register} quicktag into the My Calendar templating array.
*
Expand All @@ -194,6 +215,8 @@ function mt_add_shortcode( $e, $event ) {

return $e;
}
// Add {register} form to My Calendar templating for upcoming events lists, etc.
add_filter( 'mc_filter_shortcodes', 'mt_add_shortcode', 5, 2 );

/**
* Shortcode to output a user's purchases and tickets.
Expand Down
1 change: 1 addition & 0 deletions src/my-tickets.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ function mt_build_gateways() {

include( plugin_dir_path( __FILE__ ) . 'includes/data-utilities.php' );
include( plugin_dir_path( __FILE__ ) . 'includes/date-utilities.php' );
include( plugin_dir_path( __FILE__ ) . 'includes/event-utilities.php' );
include( plugin_dir_path( __FILE__ ) . 'mt-common.php' );
include( plugin_dir_path( __FILE__ ) . 'mt-cpt.php' );
include( plugin_dir_path( __FILE__ ) . 'mt-fields-api.php' );
Expand Down

0 comments on commit 55779b1

Please sign in to comment.