|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Add to Calendar button + dropdown for single-event pages. |
| 4 | + * |
| 5 | + * Hooks onto `data_machine_events_action_buttons` (fired inside the |
| 6 | + * EventDetails block render) and emits a button that opens a dropdown |
| 7 | + * with three calendar destinations: Google Calendar, Outlook.com, and |
| 8 | + * a downloadable .ics file (for Apple Calendar / Thunderbird / etc.). |
| 9 | + * |
| 10 | + * Hook priority 7: this puts the button between the existing attendance |
| 11 | + * button (priority 5, from extrachill-events) and the share button |
| 12 | + * (priority 10, from extrachill-events). Order on a page that has all |
| 13 | + * three: Ticket → Attendance → Add to Calendar → Share. |
| 14 | + * |
| 15 | + * @package DataMachineEvents\Blocks\EventDetails |
| 16 | + * @since 0.40.0 |
| 17 | + */ |
| 18 | + |
| 19 | +defined( 'ABSPATH' ) || exit; |
| 20 | + |
| 21 | +if ( ! class_exists( '\\DataMachineEvents\\EventActions\\CalendarUrlBuilder' ) ) { |
| 22 | + require_once DATA_MACHINE_EVENTS_PLUGIN_DIR . 'inc/EventActions/CalendarUrlBuilder.php'; |
| 23 | +} |
| 24 | + |
| 25 | +add_action( |
| 26 | + 'data_machine_events_action_buttons', |
| 27 | + 'data_machine_events_render_add_to_calendar_button', |
| 28 | + 7, |
| 29 | + 2 |
| 30 | +); |
| 31 | + |
| 32 | +if ( ! function_exists( 'data_machine_events_render_add_to_calendar_button' ) ) { |
| 33 | + /** |
| 34 | + * Emit the Add-to-Calendar button + dropdown HTML. |
| 35 | + * |
| 36 | + * @param int $post_id Event post ID. |
| 37 | + * @param string $ticket_url Ticket URL (unused here, present for hook signature parity). |
| 38 | + */ |
| 39 | + function data_machine_events_render_add_to_calendar_button( $post_id, $ticket_url ) { |
| 40 | + unset( $ticket_url ); |
| 41 | + |
| 42 | + $post_id = (int) $post_id; |
| 43 | + if ( $post_id <= 0 ) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + $post = get_post( $post_id ); |
| 48 | + if ( ! $post ) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if ( ! function_exists( 'data_machine_events_parse_event_data' ) ) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $event = \data_machine_events_parse_event_data( $post ); |
| 57 | + if ( ! $event ) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + // Inject post_id so URL builders can resolve title + permalink. |
| 62 | + $event['post_id'] = $post_id; |
| 63 | + |
| 64 | + $google_url = \DataMachineEvents\EventActions\CalendarUrlBuilder::google( $event ); |
| 65 | + $outlook_url = \DataMachineEvents\EventActions\CalendarUrlBuilder::outlook( $event ); |
| 66 | + $ics_url = \DataMachineEvents\EventActions\CalendarUrlBuilder::ics_url( $post_id ); |
| 67 | + |
| 68 | + if ( ! $google_url && ! $outlook_url && ! $ics_url ) { |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + $menu_id = 'dm-atc-menu-' . $post_id; |
| 73 | + ?> |
| 74 | + <div class="dm-events-add-to-calendar" data-dm-add-to-calendar> |
| 75 | + <button |
| 76 | + type="button" |
| 77 | + class="dm-events-action-btn dm-events-add-to-calendar-toggle ticket-button" |
| 78 | + aria-haspopup="true" |
| 79 | + aria-expanded="false" |
| 80 | + aria-controls="<?php echo esc_attr( $menu_id ); ?>" |
| 81 | + > |
| 82 | + <span class="dashicons dashicons-calendar-alt" aria-hidden="true"></span> |
| 83 | + <?php esc_html_e( 'Add to Calendar', 'data-machine-events' ); ?> |
| 84 | + </button> |
| 85 | + <ul |
| 86 | + id="<?php echo esc_attr( $menu_id ); ?>" |
| 87 | + class="dm-events-add-to-calendar-menu" |
| 88 | + role="menu" |
| 89 | + hidden |
| 90 | + > |
| 91 | + <?php if ( $google_url ) : ?> |
| 92 | + <li role="none"> |
| 93 | + <a |
| 94 | + role="menuitem" |
| 95 | + href="<?php echo esc_url( $google_url ); ?>" |
| 96 | + target="_blank" |
| 97 | + rel="noopener noreferrer" |
| 98 | + ><?php esc_html_e( 'Google Calendar', 'data-machine-events' ); ?></a> |
| 99 | + </li> |
| 100 | + <?php endif; ?> |
| 101 | + <?php if ( $outlook_url ) : ?> |
| 102 | + <li role="none"> |
| 103 | + <a |
| 104 | + role="menuitem" |
| 105 | + href="<?php echo esc_url( $outlook_url ); ?>" |
| 106 | + target="_blank" |
| 107 | + rel="noopener noreferrer" |
| 108 | + ><?php esc_html_e( 'Outlook.com', 'data-machine-events' ); ?></a> |
| 109 | + </li> |
| 110 | + <?php endif; ?> |
| 111 | + <?php if ( $ics_url ) : ?> |
| 112 | + <li role="none"> |
| 113 | + <a |
| 114 | + role="menuitem" |
| 115 | + href="<?php echo esc_url( $ics_url ); ?>" |
| 116 | + download |
| 117 | + ><?php esc_html_e( 'Apple Calendar / Download (.ics)', 'data-machine-events' ); ?></a> |
| 118 | + </li> |
| 119 | + <?php endif; ?> |
| 120 | + </ul> |
| 121 | + </div> |
| 122 | + <?php |
| 123 | + } |
| 124 | +} |
0 commit comments