Source: mt-shortcodes.php

  1. <?php
  2. /**
  3. * Shortcodes
  4. *
  5. * @category Display
  6. * @package My Tickets
  7. * @author Joe Dolson
  8. * @license GPLv3
  9. * @link https://www.joedolson.com/my-tickets/
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. } // Exit if accessed directly.
  14. add_shortcode( 'quick-cart', 'my_tickets_short_cart' );
  15. add_filter( 'universal_top_of_header', 'my_tickets_short_cart', 10, 1 );
  16. add_filter( 'milky_way_top_of_header', 'my_tickets_short_cart', 10, 1 );
  17. /**
  18. * Shortcode to add quick cart to site. Shows current number of tickets and total value plus link to checkout.
  19. *
  20. * @return string
  21. */
  22. function my_tickets_short_cart() {
  23. $options = mt_get_settings();
  24. $cart = mt_get_cart();
  25. $total = mt_total_cart( $cart );
  26. $tickets = mt_count_cart( $cart );
  27. // Translators: Number of tickets.
  28. $ticket_text = apply_filters( 'mt_quick_cart_ticket_text', sprintf( _n( '%s ticket', '%s tickets', $tickets, 'my-tickets' ), "<span class='mt_qc_tickets'>$tickets</span>" ) );
  29. $url = mt_get_cart_url();
  30. $symbol_order = $options['symbol_order'];
  31. if ( 'symbol-first' === $symbol_order ) {
  32. $currency = "<span class='mt_currency'>" . mt_symbols( $options['mt_currency'] ) . "</span><span class='mt_qc_total'>" . number_format( $total, 2 ) . '</span>';
  33. } else {
  34. $currency = "<span class='mt_qc_total'>" . number_format( $total, 2 ) . "</span> <span class='mt_currency'>" . mt_symbols( $options['mt_currency'] ) . '</span>';
  35. }
  36. // Translators: Checkout URL.
  37. $checkout = apply_filters( 'mt_quick_cart_checkout', sprintf( __( '<a href="%s">Checkout</a>', 'my-tickets' ), esc_url( $url ) ) );
  38. if ( $tickets < 1 && 'true' === $options['mt_hide_empty_short_cart'] ) {
  39. return '';
  40. }
  41. return "
  42. <div class='my-tickets mt-quick-cart' aria-live='polite'>" . __( 'In your cart: ', 'my-tickets' ) . "$ticket_text
  43. <span class='divider'>|</span>
  44. $currency
  45. <span class='divider'>|</span>
  46. $checkout
  47. </div>";
  48. }
  49. /**
  50. * Shortcode to generate ticketing form. Required attribute: event="event_id"
  51. *
  52. * @param array $atts Shortcode attributes.
  53. * @param string $content Contained content.
  54. *
  55. * @return string
  56. */
  57. function mt_add_to_cart_form_shortcode( $atts, $content = '' ) {
  58. $atts = shortcode_atts(
  59. array(
  60. 'event' => false,
  61. 'view' => 'calendar',
  62. 'time' => 'month',
  63. 'location' => 'false',
  64. ),
  65. $atts
  66. );
  67. $post_id = mt_get_current_event();
  68. $event_id = ( isset( $atts['event'] ) && ! empty( $atts['event'] ) ) ? absint( $atts['event'] ) : $post_id;
  69. $return = '';
  70. $location = '';
  71. if ( $event_id ) {
  72. $return = mt_add_to_cart_form( $content, $event_id, $atts['view'], $atts['time'], true );
  73. if ( 'false' !== $atts['location'] ) {
  74. $location = mt_get_ticket_venue( false, $event_id );
  75. }
  76. if ( 'before' === $atts['location'] || 'true' === $atts['location'] ) {
  77. $return = $location . $return;
  78. }
  79. if ( 'after' === $atts['location'] ) {
  80. $return = $return . $location;
  81. }
  82. return $return;
  83. }
  84. return $content;
  85. }
  86. add_shortcode( 'ticket', 'mt_add_to_cart_form_shortcode' );
  87. /**
  88. * Shortcode to display event venue.
  89. *
  90. * @param array $atts Shortcode attributes.
  91. * @param string $content Contained content.
  92. *
  93. * @return string
  94. */
  95. function mt_ticket_venue_shortcode( $atts, $content = '' ) {
  96. $atts = shortcode_atts(
  97. array(
  98. 'event' => false,
  99. ),
  100. $atts
  101. );
  102. $post_id = mt_get_current_event();
  103. $event_id = isset( $atts['event'] ) ? absint( $atts['event'] ) : $post_id;
  104. if ( $event_id ) {
  105. $venue = mt_get_ticket_venue( false, $event_id );
  106. } else {
  107. $venue = $content;
  108. }
  109. return $venue;
  110. }
  111. add_shortcode( 'ticket_venue', 'mt_ticket_venue_shortcode' );
  112. /**
  113. * Produce a list of featured tickets with a custom template and registration forms.
  114. *
  115. * @param array $atts Shortcode attributes.
  116. * @param string $content Contained content.
  117. *
  118. * @return string
  119. */
  120. function mt_featured_tickets( $atts, $content = '' ) {
  121. $grouped = false;
  122. $atts = shortcode_atts(
  123. array(
  124. 'events' => false,
  125. 'group' => false,
  126. 'taxonomy' => 'mt-event-group',
  127. 'view' => 'calendar',
  128. 'time' => 'month',
  129. 'template' => '<h3>{post_title}: {event_begin format="l, F d"}</h3><p>{post_excerpt}</p>',
  130. ),
  131. $atts
  132. );
  133. if ( $atts['events'] ) {
  134. $events = explode( ',', $atts['events'] );
  135. } elseif ( $atts['group'] ) {
  136. if ( is_string( $atts['group'] ) ) {
  137. // Gets a group of events by taxonomy term.
  138. $events = mt_get_events_by_term( $atts['group'], $atts['taxonomy'] );
  139. }
  140. if ( is_numeric( $atts['group'] ) ) {
  141. // Gets a group of events by My Calendar event group ID.
  142. $events = mt_get_events_by_group_id( $atts['group'] );
  143. }
  144. $grouped = true;
  145. } else {
  146. /**
  147. * Set an array of default event IDs to show in the [tickets] shortcode. Only runs if the 'events' shortcode attribute is false.
  148. *
  149. * @hook mt_default_ticketed_events
  150. *
  151. * @param {array} $events Array of event IDs.
  152. * @param {array} $atts Shortcode attributes.
  153. * @param {string} $content Shortcode contents.
  154. *
  155. * @return {array}
  156. */
  157. $events = apply_filters( 'mt_default_ticketed_events', array(), $atts, $content );
  158. }
  159. $content = '';
  160. if ( is_array( $events ) && ! empty( $events ) ) {
  161. $group = false;
  162. if ( $grouped ) {
  163. $count = count( $events );
  164. $last = $events[ $count - 1 ];
  165. $first = $events[0];
  166. $group = array(
  167. 'first' => $first,
  168. 'last' => $last,
  169. );
  170. }
  171. foreach ( $events as $event ) {
  172. $event_data = get_post_meta( $event, '_mc_event_data', true );
  173. $post = get_post( $event, ARRAY_A );
  174. if ( is_array( $post ) && is_array( $event_data ) ) {
  175. /**
  176. * Filter the data used to draw event templates in the [tickets] shortcode.
  177. *
  178. * @hook mt_ticket_template_array
  179. *
  180. * @param {array} $data Merged array of stored event data and post object as array.
  181. *
  182. * @return {array}
  183. */
  184. $data = apply_filters( 'mt_ticket_template_array', array_merge( $event_data, $post ) );
  185. $event_data = "<div class='mt-event-details'>" . mt_draw_template( $data, $atts['template'] ) . '</div>';
  186. $content .= "<div class='mt-event-item'>" . $event_data . mt_add_to_cart_form( '', $event, $atts['view'], $atts['time'], true, $group ) . '</div>';
  187. }
  188. }
  189. }
  190. return "<div class='mt-event-list'>" . $content . '</div>';
  191. }
  192. add_shortcode( 'tickets', 'mt_featured_tickets' );
  193. /**
  194. * Display the number of tickets remaining for an event.
  195. *
  196. * @param array $atts Shortcode attributes.
  197. * @param string $content Contained content.
  198. *
  199. * @return string
  200. */
  201. function mt_remaining_tickets( $atts, $content = '' ) {
  202. $atts = shortcode_atts(
  203. array(
  204. 'event' => false,
  205. 'template' => '<p>{remain} tickets left of {total}</p>',
  206. ),
  207. $atts
  208. );
  209. $template = $atts['template'];
  210. if ( ! is_numeric( $atts['event'] ) ) {
  211. global $post;
  212. if ( is_object( $post ) ) {
  213. $event_id = $post->ID;
  214. } else {
  215. return $content;
  216. }
  217. } else {
  218. $event_id = (int) $atts['event'];
  219. }
  220. $registration = get_post_meta( $event_id, '_mt_registration_options', true );
  221. if ( is_array( $registration ) ) {
  222. $pricing = $registration['prices'];
  223. $available = $registration['total'];
  224. $tickets_data = mt_tickets_left( $pricing, $available );
  225. return '<div class="mt-remaining-tickets">' . mt_draw_template( $tickets_data, $template ) . '</div>';
  226. } else {
  227. return $content;
  228. }
  229. return $content;
  230. }
  231. add_shortcode( 'remaining', 'mt_remaining_tickets' );
  232. /**
  233. * Add My Tickets data into the My Calendar templating array.
  234. * Currently: `register` => Add to cart form;
  235. * `ticket_status` => Show if the event is sold out.
  236. * `tickets_available` => Show total number of tickets available.
  237. *
  238. * @param array $e Array of My Calendar template values.
  239. * @param object $event My Calendar event object.
  240. *
  241. * @return array
  242. */
  243. function mt_add_shortcode( $e, $event ) {
  244. $e['register'] = mt_add_to_cart_form( '', $event->event_post );
  245. $e['ticket_status'] = mt_event_status( $event->event_post );
  246. $tickets_available = mt_check_inventory( $event->event_post );
  247. $e['tickets_available'] = ( isset( $tickets_available['available'] ) ) ? '<span class="mc-tickets-available">' . $tickets_available['available'] . '</span>' : '';
  248. return $e;
  249. }
  250. // Add My Tickets tags into My Calendar templating for upcoming events lists, etc.
  251. add_filter( 'mc_filter_shortcodes', 'mt_add_shortcode', 5, 2 );
  252. /**
  253. * Shortcode to output a user's purchases and tickets.
  254. *
  255. * @param array $atts Array of shortcode attributes.
  256. * @param string $content Contained content.
  257. *
  258. * @return string
  259. */
  260. function mt_user_purchases( $atts, $content ) {
  261. $atts = shortcode_atts(
  262. array(
  263. 'user_id' => false,
  264. 'count' => 10,
  265. 'user_email' => '',
  266. ),
  267. $atts
  268. );
  269. $output = mt_display_payments( $atts['user_id'], $atts['count'], $atts['user_email'] );
  270. return $output;
  271. }
  272. add_shortcode( 'my-payments', 'mt_user_purchases' );
  273. /**
  274. * Fetch a user's payment history and output on the front-end.
  275. *
  276. * @param int $user_id User ID.
  277. * @param int $count Number of payments to display by default.
  278. * @param string $user_email User email. If provided, fetch payments by email supplied rather than user ID. Ignores User ID.
  279. *
  280. * @return string
  281. */
  282. function mt_display_payments( $user_id = false, $count = 10, $user_email = '' ) {
  283. $output = '';
  284. if ( is_user_logged_in() ) {
  285. $user = ( ! $user_id ) ? wp_get_current_user()->ID : $user_id;
  286. $count = ( ! $count ) ? 10 : absint( $count );
  287. if ( $user && ! $user_email ) {
  288. $payments = get_posts(
  289. array(
  290. 'post_type' => 'mt-payments',
  291. 'post_status' => array( 'draft, publish' ),
  292. 'author' => $user,
  293. 'numberposts' => $count,
  294. )
  295. );
  296. } elseif ( $user_email && is_email( $user_email ) ) {
  297. $payments = get_posts(
  298. array(
  299. 'post_type' => 'mt-payments',
  300. 'post_status' => array( 'draft', 'publish' ),
  301. 'numberposts' => $count,
  302. 'meta_query' => array(
  303. array(
  304. 'key' => 'email',
  305. 'value' => $user_email,
  306. 'compare' => '=',
  307. ),
  308. ),
  309. )
  310. );
  311. } else {
  312. $payments = array();
  313. }
  314. if ( ! empty( $payments ) ) {
  315. $thead = '<table class="widefat mt-payments striped">
  316. <caption>' . __( 'Your Payments', 'my-tickets' ) . '</caption>
  317. <thead>
  318. <tr>
  319. <th scope="col">' . __( 'ID', 'my-tickets' ) . '</th>
  320. <th scope="col">' . __( 'Name', 'my-tickets' ) . '</th>
  321. <th scope="col">' . __( 'Date', 'my-tickets' ) . '</th>
  322. <th scope="col">' . __( 'Status', 'my-tickets' ) . '</th>
  323. <th scope="col">' . __( 'Details', 'my-tickets' ) . '</th>
  324. </tr>
  325. </thead>
  326. <tbody>';
  327. $tfoot = '</tbody>
  328. </table>';
  329. foreach ( $payments as $payment ) {
  330. $details = '<div class="mt-payment-details">';
  331. $details .= mt_payment_data( $payment->ID, array( 'dispute', 'other', 'purchase', 'ticket' ) );
  332. $details .= '</div>';
  333. $classes = implode( ' ', array( $payment->post_status, sanitize_title( get_post_meta( $payment->ID, '_is_paid', true ) ) ) );
  334. $output .= '<tr class="' . $classes . '">
  335. <td>' . $payment->ID . '</td>
  336. <td>' . esc_html( get_the_title( $payment->ID ) ) . '</td>
  337. <td>' . get_the_date( 'Y-m-d H:i', $payment->ID ) . '</td>
  338. <td>' . mt_get_payment_status( $payment->ID ) . '</td>
  339. <td><button type="button" class="mt-show-payment-details" aria-expanded="false">' . __( 'Payment Details', 'my-tickets' ) . '</button>' . $details . '</td>
  340. </tr>';
  341. }
  342. $output = $thead . $output . $tfoot;
  343. }
  344. }
  345. return $output;
  346. }