Source: mt-templating.php

  1. <?php
  2. /**
  3. * Ticket display template tags.
  4. *
  5. * @category Display
  6. * @package My Tickets
  7. * @author Joe Dolson
  8. * @license GPLv2 or later
  9. * @link https://www.joedolson.com/my-tickets/
  10. */
  11. if ( ! defined( 'ABSPATH' ) ) {
  12. exit;
  13. } // Exit if accessed directly.
  14. require_once 'vendor/autoload.php';
  15. use chillerlan\QRCode\QRCode;
  16. use chillerlan\QRCode\QROptions;
  17. /**
  18. * Get logo for display on receipts and tickets.
  19. *
  20. * @param array $args Custom arguments.
  21. * @param int $post_ID Post ID.
  22. *
  23. * @return string
  24. */
  25. function mt_get_logo( $args = array(), $post_ID = false ) {
  26. $options = mt_get_settings();
  27. $ticket = mt_get_ticket();
  28. if ( isset( $options['mt_ticket_image'] ) && 'event' === $options['mt_ticket_image'] && $ticket ) {
  29. // if event has post thumbnail, use that.
  30. if ( has_post_thumbnail( $ticket->ID ) ) {
  31. return get_the_post_thumbnail( $ticket->ID );
  32. }
  33. }
  34. if ( $post_ID && has_post_thumbnail( $post_ID ) ) {
  35. return get_the_post_thumbnail( $post_ID, 'my-tickets-logo' );
  36. }
  37. $args = array_merge(
  38. array(
  39. 'alt' => 'My Tickets',
  40. 'class' => 'default',
  41. 'width' => '',
  42. 'height' => '',
  43. ),
  44. $args
  45. );
  46. $atts = '';
  47. foreach ( $args as $att => $value ) {
  48. if ( '' !== trim( $value ) ) {
  49. $atts .= ' ' . esc_attr( $att ) . '=' . '"' . esc_attr( $value ) . '"';
  50. }
  51. }
  52. $img = "<img src='" . plugins_url( '/images/logo.png', __FILE__ ) . "' $atts />";
  53. return $img;
  54. }
  55. /**
  56. * Get logo for display on receipts and tickets.
  57. *
  58. * @param array $args Custom arguments.
  59. * @param int $post_ID Post ID.
  60. *
  61. * @return void
  62. */
  63. function mt_logo( $args = array(), $post_ID = false ) {
  64. echo wp_kses_post( mt_get_logo( $args, $post_ID ) );
  65. }
  66. // Template Functions for Receipts.
  67. /**
  68. * Return formatted order data for receipt template.
  69. *
  70. * @return string
  71. */
  72. function mt_get_cart_order() {
  73. $receipt = mt_get_receipt();
  74. if ( $receipt ) {
  75. $purchase = get_post_meta( $receipt->ID, '_purchased' );
  76. $data = mt_format_purchase( $purchase, 'html', $receipt->ID );
  77. return $data;
  78. }
  79. return '';
  80. }
  81. /**
  82. * Return formatted order data for receipt template.
  83. *
  84. * @return void
  85. */
  86. function mt_cart_order() {
  87. echo wp_kses_post( mt_get_cart_order() );
  88. }
  89. /**
  90. * Get ticket information for a given purchase as data.
  91. *
  92. * @return mixed bool/array Ticket IDs.
  93. */
  94. function mt_get_payment_tickets() {
  95. $receipt = mt_get_receipt();
  96. if ( $receipt ) {
  97. $purchase = get_post_meta( $receipt->ID, '_purchased' );
  98. $id = $receipt->ID;
  99. return mt_setup_tickets( $purchase, $id, 'ids' );
  100. }
  101. return false;
  102. }
  103. /**
  104. * Return receipt ID.
  105. *
  106. * @return string
  107. */
  108. function mt_get_receipt_id() {
  109. if ( isset( $_GET['receipt_id'] ) ) {
  110. $receipt_id = sanitize_text_field( $_GET['receipt_id'] );
  111. return $receipt_id;
  112. }
  113. }
  114. /**
  115. * Return receipt ID.
  116. *
  117. * @return void
  118. */
  119. function mt_receipt_id() {
  120. echo esc_html( mt_get_receipt_id() );
  121. }
  122. /**
  123. * Get receipt's purchase ID
  124. *
  125. * @return integer
  126. */
  127. function mt_get_receipt_purchase_id() {
  128. $purchase = mt_get_receipt();
  129. $purchase_id = $purchase->ID;
  130. return $purchase_id;
  131. }
  132. /**
  133. * Get receipt's purchase ID
  134. *
  135. * @return void
  136. */
  137. function mt_receipt_purchase_id() {
  138. echo esc_html( mt_get_receipt_purchase_id() );
  139. }
  140. /**
  141. * Get provided purchaser name from payment.
  142. *
  143. * @return string
  144. */
  145. function mt_get_cart_purchaser() {
  146. $receipt = mt_get_receipt();
  147. if ( $receipt ) {
  148. $purchaser = get_the_title( $receipt->ID );
  149. return $purchaser;
  150. }
  151. return '';
  152. }
  153. /**
  154. * Get provided purchaser name from payment.
  155. *
  156. * @return void
  157. */
  158. function mt_cart_purchaser() {
  159. echo esc_html( mt_get_cart_purchaser() );
  160. }
  161. /**
  162. * Get formatted date/time of purchase.
  163. *
  164. * @return string
  165. */
  166. function mt_get_cart_purchase_date() {
  167. $receipt = mt_get_receipt();
  168. if ( $receipt ) {
  169. /**
  170. * HTML formatting of purchase date on receipts.
  171. *
  172. * @hook mt_cart_purchase_date
  173. *
  174. * @param {string} $format HTML formatted string with date format placeholders.
  175. *
  176. * @return {string}
  177. */
  178. $format = apply_filters( 'mt_cart_purchase_date', '<span class="mt-cart-date">' . get_option( 'date_format' ) . '</span><span class="mt-date-separator"> @ </span><span class="mt-cart-time">' . get_option( 'time_format' ) . '</span>' );
  179. $date = date_i18n( $format, strtotime( $receipt->post_date ) );
  180. return $date;
  181. }
  182. return '';
  183. }
  184. /**
  185. * Get formatted date/time of purchase.
  186. *
  187. * @return void
  188. */
  189. function mt_cart_purchase_date() {
  190. echo wp_kses_post( mt_get_cart_purchase_date() );
  191. }
  192. /**
  193. * Get payment gateway data from payment.
  194. *
  195. * @return string
  196. */
  197. function mt_get_payment_details() {
  198. $receipt = mt_get_receipt();
  199. if ( $receipt ) {
  200. $paid = get_post_meta( $receipt->ID, '_is_paid', true );
  201. if ( 'Completed' === $paid ) {
  202. $gateway = get_post_meta( $receipt->ID, '_gateway', true );
  203. $gateways = mt_setup_gateways();
  204. $gateway_label = isset( $gateways[ $gateway ] ) ? $gateways[ $gateway ]['label'] : $gateway;
  205. $transaction = get_post_meta( $receipt->ID, '_transaction_id', true );
  206. $total = get_post_meta( $receipt->ID, '_total_paid', true );
  207. $hand_total = get_post_meta( $receipt->ID, '_mt_handling', true );
  208. $handling = ( $hand_total ) ? '<li>' . __( 'Handling:', 'my-tickets' ) . ' ' . apply_filters( 'mt_money_format', $hand_total ) . '</li>' : '';
  209. $ship_total = get_post_meta( $receipt->ID, '_mt_shipping', true );
  210. $shipping = ( $ship_total ) ? '<li>' . __( 'Shipping:', 'my-tickets' ) . ' ' . apply_filters( 'mt_money_format', $ship_total ) . '</li>' : '';
  211. $vat = get_post_meta( $receipt->ID, '_vat', true );
  212. // Translators: VAT ID.
  213. $vat = ( $vat ) ? '<li>' . sprintf( __( 'VAT Number: %s', 'my-tickets' ), '<code>' . $vat . '</code>' ) . '</li>' : '';
  214. $return = __( 'This receipt is paid in full.', 'my-tickets' );
  215. $return .= '
  216. <ul>
  217. <li>' . __( 'Payment through:', 'my-tickets' ) . " $gateway_label</li>
  218. <li>" . __( 'Transaction ID:', 'my-tickets' ) . " <code>$transaction</code></li>
  219. $handling
  220. $shipping
  221. <li>" . __( 'Amount Paid:', 'my-tickets' ) . ' ' . apply_filters( 'mt_money_format', $total ) . "</li>
  222. $vat
  223. </ul>";
  224. return $return;
  225. } elseif ( 'Refunded' === $paid ) {
  226. return __( 'This payment has been refunded.', 'my-tickets' );
  227. } elseif ( 'Failed' === $paid ) {
  228. return __( 'Payment on this order failed.', 'my-tickets' );
  229. } elseif ( 'Turned Back' === $paid ) {
  230. return __( 'This purchase was cancelled and the tickets were returned to the seller.', 'my-tickets' );
  231. } else {
  232. $due = get_post_meta( $receipt->ID, '_total_paid', true );
  233. $due = apply_filters( 'mt_money_format', $due );
  234. // Translators: Amount due on this payment.
  235. return __( 'Payment on this purchase is not completed. The receipt will be updated with payment details when payment is completed.', 'my-tickets' ) . ' ' . sprintf( __( 'Amount due: %s', 'my-tickets' ), '<strong>' . $due . '</strong>' );
  236. }
  237. }
  238. return '';
  239. }
  240. /**
  241. * Get payment gateway data from payment.
  242. *
  243. * @return void
  244. */
  245. function mt_payment_details() {
  246. echo wp_kses_post( mt_get_payment_details() );
  247. }
  248. /**
  249. * Get ticket ID (must be used in ticket template.)
  250. *
  251. * @return string
  252. */
  253. function mt_get_ticket_id() {
  254. $ticket_id = ( isset( $_GET['ticket_id'] ) ) ? sanitize_text_field( $_GET['ticket_id'] ) : false;
  255. return $ticket_id;
  256. }
  257. /**
  258. * Get ticket ID (must be used in ticket template.)
  259. *
  260. * @return void
  261. */
  262. function mt_ticket_id() {
  263. echo esc_html( mt_get_ticket_id() );
  264. }
  265. /**
  266. * Get sequential ticket ID for display purposes.
  267. *
  268. * @param string $ticket_id Unique ticket ID.
  269. *
  270. * @return string sequential ID
  271. */
  272. function mt_get_sequential_id( $ticket_id = false ) {
  273. $ticket_id = ( $ticket_id ) ? $ticket_id : mt_get_ticket_id();
  274. $ticket = mt_get_ticket( $ticket_id );
  275. $sequential = get_post_meta( $ticket->ID, '_' . $ticket_id . '_seq_id', true );
  276. if ( ! $sequential ) {
  277. $sequential = mt_generate_sequential_id( $ticket_id, array( 'event_id' => $ticket->ID ) );
  278. }
  279. return zeroise( $sequential, 6 );
  280. }
  281. /**
  282. * Echo sequential ticket ID (must be used in ticket template.)
  283. *
  284. * @return void
  285. */
  286. function mt_sequential_id() {
  287. echo esc_html( mt_get_sequential_id() );
  288. }
  289. /**
  290. * Get ticket method (willcall, postal, eticket, printable)
  291. *
  292. * @param bool|int $ticket_id Ticket ID.
  293. *
  294. * @return mixed|string
  295. */
  296. function mt_get_ticket_method( $ticket_id = false ) {
  297. if ( ! $ticket_id ) {
  298. $ticket_id = mt_get_ticket_id();
  299. }
  300. $purchase = get_post_meta( mt_get_ticket( $ticket_id )->ID, '_' . $ticket_id, true );
  301. $purchase_id = $purchase['purchase_id'];
  302. $ticket_type = get_post_meta( $purchase_id, '_ticketing_method', true );
  303. $ticket_type = ( $ticket_type ) ? $ticket_type : 'willcall';
  304. return $ticket_type;
  305. }
  306. /**
  307. * Get ticket method (willcall, postal, eticket, printable)
  308. *
  309. * @param bool|int $ticket_id Ticket ID.
  310. *
  311. * @return void
  312. */
  313. function mt_ticket_method( $ticket_id = false ) {
  314. echo esc_html( mt_get_ticket_method( $ticket_id ) );
  315. }
  316. /**
  317. * Get ticket's parent purchase ID
  318. *
  319. * @param bool|int $ticket_id Ticket ID.
  320. *
  321. * @return integer
  322. */
  323. function mt_get_ticket_purchase_id( $ticket_id = false ) {
  324. if ( ! $ticket_id ) {
  325. $ticket_id = mt_get_ticket_id();
  326. }
  327. $purchase = get_post_meta( mt_get_ticket( $ticket_id )->ID, '_' . $ticket_id, true );
  328. $purchase_id = $purchase['purchase_id'];
  329. return $purchase_id;
  330. }
  331. /**
  332. * Get ticket's parent purchase ID
  333. *
  334. * @param bool|int $ticket_id Ticket ID.
  335. *
  336. * @return void
  337. */
  338. function mt_ticket_purchase_id( $ticket_id = false ) {
  339. echo esc_html( mt_get_ticket_purchase_id( $ticket_id ) );
  340. }
  341. /**
  342. * Get ticket purchaser name
  343. *
  344. * @param bool|int $ticket_id Ticket ID.
  345. *
  346. * @return mixed|string
  347. */
  348. function mt_get_ticket_purchaser( $ticket_id = false ) {
  349. if ( ! $ticket_id ) {
  350. $ticket_id = mt_get_ticket_id();
  351. }
  352. $purchase = get_post_meta( mt_get_ticket( $ticket_id )->ID, '_' . $ticket_id, true );
  353. $purchase_id = $purchase['purchase_id'];
  354. $purchaser = get_post_field( 'post_title', $purchase_id );
  355. return $purchaser;
  356. }
  357. /**
  358. * Get ticket purchaser name
  359. *
  360. * @param bool|int $ticket_id Ticket ID.
  361. *
  362. * @return void
  363. */
  364. function mt_ticket_purchaser( $ticket_id = false ) {
  365. echo esc_html( mt_get_ticket_purchaser( $ticket_id ) );
  366. }
  367. /**
  368. * Get custom field data; all by default, or only a specific field. Display in tickets.
  369. *
  370. * @param bool|string $custom_field Custom Field Name.
  371. * @param bool|string $ticket_id Ticket ID.
  372. *
  373. * @return string
  374. */
  375. function mt_get_ticket_custom_fields( $custom_field = false, $ticket_id = false ) {
  376. if ( ! $ticket_id ) {
  377. $ticket_id = mt_get_ticket_id();
  378. }
  379. $purchase = get_post_meta( mt_get_ticket( $ticket_id )->ID, '_' . $ticket_id, true );
  380. $purchase_id = $purchase['purchase_id'];
  381. return mt_show_custom_data( $purchase_id, $custom_field );
  382. }
  383. /**
  384. * Get custom field data; all by default, or only a specific field. Display in tickets.
  385. *
  386. * @param bool|string $custom_field Custom Field Name.
  387. * @param bool|string $ticket_id Ticket ID.
  388. *
  389. * @return void
  390. */
  391. function mt_ticket_custom_fields( $custom_field = false, $ticket_id = false ) {
  392. echo wp_kses_post( mt_get_ticket_custom_fields( $custom_field, $ticket_id ) );
  393. }
  394. /**
  395. * Get date & time of event this ticket is for.
  396. *
  397. * @param bool|string $ticket_id Ticket ID.
  398. *
  399. * @return string
  400. */
  401. function mt_get_event_date_time( $ticket_id = false ) {
  402. if ( ! $ticket_id ) {
  403. $ticket = mt_get_ticket();
  404. } else {
  405. $ticket = mt_get_ticket( $ticket_id );
  406. }
  407. if ( $ticket ) {
  408. $event_type = mt_get_event_type( $ticket_id );
  409. if ( 'event' === $event_type ) {
  410. return '';
  411. }
  412. $event = get_post_meta( $ticket->ID, '_mc_event_data', true );
  413. $date = isset( $event['event_begin'] ) ? $event['event_begin'] : '';
  414. $date = date_i18n( get_option( 'date_format' ), strtotime( $date ) );
  415. $time = mt_get_event_time( $ticket_id );
  416. return ( $time ) ? $date . ' @ <span class="time">' . $time . '</span>' : $date;
  417. }
  418. return '';
  419. }
  420. /**
  421. * Get date and time of event this ticket is for.
  422. *
  423. * @param bool|string $ticket_id Ticket ID.
  424. *
  425. * @return void
  426. */
  427. function mt_event_date_time( $ticket_id = false ) {
  428. echo wp_kses_post( mt_get_event_date_time( $ticket_id ) );
  429. }
  430. /**
  431. * Get date of event this ticket is for.
  432. *
  433. * @param bool|string $ticket_id Ticket ID.
  434. *
  435. * @return string
  436. */
  437. function mt_get_event_date( $ticket_id = false ) {
  438. if ( ! $ticket_id ) {
  439. $ticket = mt_get_ticket();
  440. } else {
  441. $ticket = mt_get_ticket( $ticket_id );
  442. }
  443. if ( $ticket ) {
  444. $event_type = mt_get_event_type( $ticket_id );
  445. if ( 'event' === $event_type ) {
  446. return '';
  447. }
  448. $event = get_post_meta( $ticket->ID, '_mc_event_data', true );
  449. $date = isset( $event['event_begin'] ) ? $event['event_begin'] : '';
  450. $date = date_i18n( get_option( 'date_format' ), strtotime( $date ) );
  451. return $date;
  452. }
  453. return '';
  454. }
  455. /**
  456. * Get date of event this ticket is for.
  457. *
  458. * @param bool|string $ticket_id Ticket ID.
  459. *
  460. * @return void
  461. */
  462. function mt_event_date( $ticket_id = false ) {
  463. echo wp_kses_post( mt_get_event_date( $ticket_id ) );
  464. }
  465. /**
  466. * Get event notes.
  467. *
  468. * @param bool|string $ticket_id Ticket ID.
  469. *
  470. * @return string
  471. */
  472. function mt_get_ticket_event_notes( $ticket_id = false ) {
  473. if ( ! $ticket_id ) {
  474. $ticket = mt_get_ticket();
  475. } else {
  476. $ticket = mt_get_ticket( $ticket_id );
  477. }
  478. $notes = wpautop( get_post_meta( $ticket->ID, '_mt_event_notes', true ) );
  479. return $notes;
  480. }
  481. /**
  482. * Echo event notes.
  483. *
  484. * @param bool|string $ticket_id Ticket ID.
  485. */
  486. function mt_ticket_event_notes( $ticket_id = false ) {
  487. echo wp_kses_post( mt_get_ticket_event_notes( $ticket_id ) );
  488. }
  489. /**
  490. * Get title of event this ticket is for.
  491. *
  492. * @param bool|string $ticket_id Ticket ID.
  493. *
  494. * @return string
  495. */
  496. function mt_get_event_title( $ticket_id = false ) {
  497. if ( ! $ticket_id ) {
  498. $ticket = mt_get_ticket();
  499. } else {
  500. $ticket = mt_get_ticket( $ticket_id );
  501. }
  502. if ( $ticket ) {
  503. /**
  504. * Filter the event title as shown on tickets.
  505. *
  506. * @hook mt_the_title
  507. *
  508. * @param {string} $post_title The event title.
  509. * @param {object} $ticket The ticket object.
  510. *
  511. * @return {string}
  512. */
  513. $title = apply_filters( 'the_title', apply_filters( 'mt_the_title', $ticket->post_title, $ticket ), $ticket_id );
  514. return $title;
  515. }
  516. return '';
  517. }
  518. /**
  519. * Get title of event this ticket is for.
  520. *
  521. * @param bool|string $ticket_id Ticket ID.
  522. *
  523. * @return void
  524. */
  525. function mt_event_title( $ticket_id = false ) {
  526. echo wp_kses_post( mt_get_event_title( $ticket_id ) );
  527. }
  528. /**
  529. * Get time of event this ticket is for.
  530. *
  531. * @param bool|string $ticket_id Ticket ID.
  532. *
  533. * @return string
  534. */
  535. function mt_get_event_time( $ticket_id = false ) {
  536. if ( ! $ticket_id ) {
  537. $ticket = mt_get_ticket();
  538. } else {
  539. $ticket = mt_get_ticket( $ticket_id );
  540. }
  541. if ( $ticket ) {
  542. $event_type = mt_get_event_type( $ticket_id );
  543. if ( 'event' === $event_type ) {
  544. return '';
  545. }
  546. $event = get_post_meta( $ticket->ID, '_mc_event_data', true );
  547. $time = isset( $event['event_time'] ) ? $event['event_time'] : '';
  548. $allday = ( isset( $event['event_endtime'] ) && '23:59:59' === $event['event_endtime'] || '23:59:59' === $time ) ? true : false;
  549. $time = ( $allday ) ? false : $time;
  550. if ( $time ) {
  551. $time = date_i18n( get_option( 'time_format' ), strtotime( $time ) );
  552. }
  553. return $time;
  554. }
  555. return '';
  556. }
  557. /**
  558. * Get time of event this ticket is for.
  559. *
  560. * @param bool|string $ticket_id Ticket ID.
  561. *
  562. * @return void
  563. */
  564. function mt_event_time( $ticket_id = false ) {
  565. echo esc_html( mt_get_event_time( $ticket_id ) );
  566. }
  567. /**
  568. * Get type of event tickets. (discrete, continuous, event).
  569. *
  570. * @param bool|string $ticket_id Ticket ID.
  571. *
  572. * @return mixed|string
  573. */
  574. function mt_get_event_type( $ticket_id = false ) {
  575. if ( ! $ticket_id ) {
  576. $ticket = mt_get_ticket();
  577. $ticket_id = mt_get_ticket_id();
  578. } else {
  579. $ticket = mt_get_ticket( $ticket_id );
  580. }
  581. if ( $ticket ) {
  582. $options = get_post_meta( $ticket->ID, '_mt_registration_options', true );
  583. $method = ( ! empty( $options ) && is_array( $options ) ) ? $options['counting_method'] : '';
  584. return $method;
  585. }
  586. return '';
  587. }
  588. /**
  589. * Get type of event tickets. (discrete, continuous, event).
  590. *
  591. * @param bool|string $ticket_id Ticket ID.
  592. *
  593. * @return void
  594. */
  595. function mt_event_type( $ticket_id = false ) {
  596. echo esc_html( mt_get_event_type( $ticket_id ) );
  597. }
  598. /**
  599. * Get type of ticket. (Adult, child, section 1, section 2, etc.)
  600. *
  601. * @param bool|string $ticket_id Ticket ID.
  602. *
  603. * @return mixed|string
  604. */
  605. function mt_get_ticket_type( $ticket_id = false ) {
  606. if ( ! $ticket_id ) {
  607. $ticket = mt_get_ticket();
  608. $ticket_id = mt_get_ticket_id();
  609. } else {
  610. $ticket = mt_get_ticket( $ticket_id );
  611. }
  612. if ( $ticket ) {
  613. $type = get_post_meta( $ticket->ID, '_' . $ticket_id, true );
  614. $type = isset( $type['type'] ) ? $type['type'] : '';
  615. $label = mt_get_label( $type, $ticket->ID );
  616. $event_type = mt_get_event_type( $ticket_id );
  617. if ( 'event' === $event_type ) {
  618. $date = get_option( 'date_format', 'Y-m-d' );
  619. $time = get_option( 'time_format', 'H:i' );
  620. if ( $label === $type ) {
  621. // convert to a proper timestamp.
  622. $label = substr_replace( $type, ' ', 10, 1 );
  623. }
  624. return date_i18n( $date . ' @ ' . $time, strtotime( $label ) );
  625. }
  626. /**
  627. * Filter the ticket type label shown on tickets.
  628. *
  629. * @hook mt_ticket_type
  630. *
  631. * @param {string} $label Label defined from settings.
  632. * @param {string} $type Ticket type key.
  633. *
  634. * @return {string}
  635. */
  636. return apply_filters( 'mt_ticket_type', $label, $type );
  637. }
  638. return '';
  639. }
  640. /**
  641. * Get type of ticket. (Adult, child, section 1, section 2, etc.)
  642. *
  643. * @param bool|string $ticket_id Ticket ID.
  644. *
  645. * @return void
  646. */
  647. function mt_ticket_type( $ticket_id = false ) {
  648. echo esc_html( mt_get_ticket_type( $ticket_id ) );
  649. }
  650. /**
  651. * Get ticket price for ticket.
  652. *
  653. * @param bool|string $ticket_id Ticket ID.
  654. *
  655. * @return string
  656. */
  657. function mt_get_ticket_price( $ticket_id = false ) {
  658. if ( ! $ticket_id ) {
  659. $ticket = mt_get_ticket();
  660. $ticket_id = mt_get_ticket_id();
  661. } else {
  662. $ticket = mt_get_ticket( $ticket_id );
  663. }
  664. $append = ': <em>' . __( 'Paid', 'my-tickets' ) . '</em>';
  665. if ( $ticket ) {
  666. $data = get_post_meta( $ticket->ID, '_' . $ticket_id, true );
  667. $receipt = $data['purchase_id'];
  668. $paid = get_post_meta( $receipt, '_is_paid', true );
  669. if ( 'Completed' !== $paid ) {
  670. $append = ': <em>' . __( 'Payment Due', 'my-tickets' ) . '</em>';
  671. }
  672. $type = apply_filters( 'mt_money_format', $data['price'] );
  673. return $type . $append;
  674. }
  675. return '';
  676. }
  677. /**
  678. * Get ticket price for ticket.
  679. *
  680. * @param bool|string $ticket_id Ticket ID.
  681. *
  682. * @return void
  683. */
  684. function mt_ticket_price( $ticket_id = false ) {
  685. echo wp_kses_post( mt_get_ticket_price( $ticket_id ) );
  686. }
  687. /**
  688. * Return image URL for printable/eticket QR codes.
  689. *
  690. * @param bool|string $ticket_id Ticket ID.
  691. */
  692. function mt_get_ticket_qrcode( $ticket_id = false ) {
  693. $options = mt_get_settings();
  694. $id = ( '' !== $options['mt_purchase_page'] && is_numeric( $options['mt_purchase_page'] ) ) ? absint( $options['mt_purchase_page'] ) : false;
  695. $ticket_id = ( $ticket_id ) ? $ticket_id : mt_get_ticket_id();
  696. $url = esc_url_raw(
  697. add_query_arg(
  698. array(
  699. 'ticket_id' => $ticket_id,
  700. 'action' => 'mt-verify',
  701. ),
  702. get_permalink( $id )
  703. )
  704. );
  705. $qrcode = array(
  706. 'version' => 9,
  707. 'outputType' => QRCODE::OUTPUT_IMAGE_PNG,
  708. 'eccLevel' => QRCODE::ECC_M,
  709. );
  710. /**
  711. * Filter QRCode configuration options passed to QROptions. See https://github.com/chillerlan/php-qrcode/wiki/Advanced-usage.
  712. *
  713. * @hook mt_qrcode_options
  714. *
  715. * @param {array} $qrcode Array of configuration options.
  716. * @param {string} $ticket_id ID of the current ticket.
  717. *
  718. * @return {array}
  719. */
  720. $qrcode = apply_filters( 'mt_qrcode_options', $qrcode, $ticket_id );
  721. $options = new QROptions( $qrcode );
  722. $code = new QRCode( $options );
  723. return $code->render( esc_url_raw( $url ) );
  724. }
  725. /**
  726. * Return image URL for printable/eticket QR codes.
  727. *
  728. * @param bool|string $ticket_id Ticket ID.
  729. */
  730. function mt_ticket_qrcode( $ticket_id = false ) {
  731. echo esc_attr( mt_get_ticket_qrcode( $ticket_id ) );
  732. }
  733. /**
  734. * Return image URL for printable/eticket QR codes.
  735. *
  736. * @param bool|string $receipt_id Receipt ID.
  737. */
  738. function mt_get_purchase_qrcode( $receipt_id = false ) {
  739. $options = mt_get_settings();
  740. $id = ( '' !== $options['mt_purchase_page'] && is_numeric( $options['mt_purchase_page'] ) ) ? absint( $options['mt_purchase_page'] ) : false;
  741. $receipt_id = ( $receipt_id ) ? $receipt_id : mt_get_receipt_id();
  742. $url = esc_url_raw(
  743. add_query_arg(
  744. array(
  745. 'receipt_id' => $receipt_id,
  746. 'action' => 'mt-verify',
  747. ),
  748. get_permalink( $id )
  749. )
  750. );
  751. $qrcode = array(
  752. 'version' => 9,
  753. 'outputType' => QRCODE::OUTPUT_IMAGE_PNG,
  754. 'eccLevel' => QRCODE::ECC_M,
  755. );
  756. /**
  757. * Filter QRCode configuration options passed to QROptions. See https://github.com/chillerlan/php-qrcode/wiki/Advanced-usage.
  758. *
  759. * @hook mt_qrcode_options
  760. *
  761. * @param {array} $qrcode Array of configuration options.
  762. * @param {string} $ticket_id ID of the current ticket.
  763. *
  764. * @return {array}
  765. */
  766. $qrcode = apply_filters( 'mt_qrcode_options', $qrcode, $receipt_id );
  767. $options = new QROptions( $qrcode );
  768. $code = new QRCode( $options );
  769. return $code->render( esc_url_raw( $url ) );
  770. }
  771. /**
  772. * Return image URL for printable/eticket QR codes.
  773. *
  774. * @param bool|string $receipt_id Receipt ID.
  775. */
  776. function mt_purchase_qrcode( $receipt_id = false ) {
  777. echo esc_attr( mt_get_purchase_qrcode( $receipt_id ) );
  778. }
  779. /**
  780. * Get ticket venue location data.
  781. *
  782. * @param bool|string $ticket_id Ticket ID.
  783. * @param bool|int $event_id Event ID.
  784. *
  785. * @uses filter mt_create_location_object
  786. *
  787. * @return string
  788. */
  789. function mt_get_ticket_venue( $ticket_id = false, $event_id = false ) {
  790. if ( ! $ticket_id ) {
  791. $ticket = mt_get_ticket();
  792. } else {
  793. $ticket = mt_get_ticket( $ticket_id );
  794. }
  795. $event_id = ( $ticket ) ? $ticket->ID : $event_id;
  796. if ( $event_id ) {
  797. $location_id = get_post_meta( $event_id, '_mc_event_location', true );
  798. $html = false;
  799. if ( $location_id ) {
  800. /**
  801. * Filter location objects saved with events. Expects a My Calendar structured location object.
  802. *
  803. * @hook mt_create_location_object
  804. *
  805. * @param {false} $location Default location object.
  806. * @param {int} $location_id ID value saved in post meta _mc_event_location.
  807. *
  808. * @return {object|false}
  809. */
  810. $location = apply_filters( 'mt_create_location_object', false, $location_id );
  811. if ( ! $location ) {
  812. return '';
  813. } else {
  814. $html = mt_hcard( $location, true );
  815. }
  816. }
  817. /**
  818. * Filter the hcard output in My Tickets.
  819. *
  820. * @hook mt_hcard
  821. *
  822. * @param {string} $html HTML output of an hCard.
  823. * @param {int} $location_id Location ID.
  824. * @param {object} $ticket Ticket object.
  825. * @param {int} $event_id Event ID.
  826. *
  827. * @return {string}
  828. */
  829. $html = apply_filters( 'mt_hcard', $html, $location_id, $ticket, $event_id );
  830. if ( $html ) {
  831. return $html;
  832. }
  833. }
  834. return '';
  835. }
  836. add_filter( 'mt_create_location_object', 'mt_get_mc_location', 10, 2 );
  837. /**
  838. * If My Calendar installed, return My Calendar location object.
  839. *
  840. * @param Object $location Location object.
  841. * @param int $location_id Location ID.
  842. *
  843. * @return mixed
  844. */
  845. function mt_get_mc_location( $location, $location_id ) {
  846. if ( function_exists( 'mc_hcard' ) ) {
  847. global $wpdb;
  848. $location = $wpdb->get_row( $wpdb->prepare( 'SELECT * FROM ' . my_calendar_locations_table() . ' WHERE location_id = %d', $location_id ) ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
  849. }
  850. return $location;
  851. }
  852. // set up hCard formatted address.
  853. /**
  854. * Produce HTML for My Tickets hCard.
  855. *
  856. * @param object $location Location object.
  857. *
  858. * @return string
  859. */
  860. function mt_hcard( $location ) {
  861. $url = trim( $location->location_url );
  862. $label = trim( stripslashes( $location->location_label ) );
  863. $street = trim( stripslashes( $location->location_street ) );
  864. $street2 = trim( stripslashes( $location->location_street2 ) );
  865. $city = trim( stripslashes( $location->location_city ) );
  866. $state = trim( stripslashes( $location->location_state ) );
  867. $zip = trim( stripslashes( $location->location_postcode ) );
  868. $country = trim( stripslashes( $location->location_country ) );
  869. $phone = trim( stripslashes( $location->location_phone ) );
  870. if ( ! $url && ! $label && ! $street && ! $street2 && ! $city && ! $state && ! $zip && ! $country && ! $phone ) {
  871. return '';
  872. }
  873. $link = ( '' !== $url ) ? "<a href='$url' class='location-link external'>$label</a>" : $label;
  874. $hcard = '<div class="address vcard">';
  875. $hcard .= '<div class="adr">';
  876. $hcard .= ( '' !== $label ) ? '<strong class="org">' . $link . '</strong><br />' : '';
  877. $hcard .= ( '' === $street . $street2 . $city . $state . $zip . $country . $phone ) ? '' : "<div class='sub-address'>";
  878. $hcard .= ( '' !== $street ) ? '<div class="street-address">' . $street . '</div>' : '';
  879. $hcard .= ( '' !== $street2 ) ? '<div class="street-address">' . $street2 . '</div>' : '';
  880. $hcard .= ( '' !== $city . $state . $zip ) ? '<div>' : '';
  881. $hcard .= ( '' !== $city ) ? '<span class="locality">' . $city . "</span><span class='sep'>, </span>" : '';
  882. $hcard .= ( '' !== $state ) ? '<span class="region">' . $state . '</span> ' : '';
  883. $hcard .= ( '' !== $zip ) ? ' <span class="postal-code">' . $zip . '</span>' : '';
  884. $hcard .= ( '' !== $city . $state . $zip ) ? '</div>' : '';
  885. $hcard .= ( '' !== $country ) ? '<div class="country-name">' . $country . '</div>' : '';
  886. $hcard .= ( '' !== $phone ) ? '<div class="tel">' . $phone . '</div>' : '';
  887. $hcard .= ( '' === $street . $street2 . $city . $state . $zip . $country . $phone ) ? '' : '</div>';
  888. $hcard .= '</div>';
  889. $hcard .= '</div>';
  890. return $hcard;
  891. }
  892. /**
  893. * Get ticket venue location data.
  894. *
  895. * @param bool|string $ticket_id Ticket ID.
  896. *
  897. * @return void
  898. */
  899. function mt_ticket_venue( $ticket_id = false ) {
  900. echo wp_kses_post( mt_get_ticket_venue( $ticket_id ) );
  901. }
  902. /**
  903. * Verify that a ticket is valid, paid for, and which event it's for.
  904. *
  905. * @param bool|string $ticket_id Ticket ID.
  906. *
  907. * @return string
  908. */
  909. function mt_get_verification( $ticket_id = false ) {
  910. $ticket_id = ( ! $ticket_id ) ? mt_get_ticket_id() : $ticket_id;
  911. $verified = mt_verify_ticket( $ticket_id );
  912. $ticket = mt_get_ticket( $ticket_id );
  913. if ( $ticket ) {
  914. $data = get_post_meta( $ticket->ID, '_' . $ticket_id, true );
  915. $purchase_id = $data['purchase_id'];
  916. $status = get_post_meta( $purchase_id, '_is_paid', true );
  917. $due = get_post_meta( $purchase_id, '_total_paid', true );
  918. $due = apply_filters( 'mt_money_format', $due );
  919. $text = ( $verified ) ? __( 'Ticket Verified', 'my-tickets' ) : __( 'Invalid Ticket ID', 'my-tickets' );
  920. // Translators: Amount due on account.
  921. $text .= ( 'Pending' === $status ) ? ' - ' . sprintf( __( 'Payment pending: %s', 'my-tickets' ), $due ) : '';
  922. $status_class = sanitize_title( $status );
  923. $used = get_post_meta( $purchase_id, '_tickets_used' );
  924. if ( ! is_array( $used ) ) {
  925. $used = array();
  926. }
  927. $is_used = false;
  928. if ( in_array( $ticket_id, $used, true ) ) {
  929. $is_used = true;
  930. $status_class .= ' used';
  931. $text .= ' (' . __( 'Ticket has been used.', 'my-tickets' ) . ')';
  932. }
  933. $text .= wpautop( mt_get_ticket_validity( $ticket ) );
  934. if ( ( current_user_can( 'mt-verify-ticket' ) || current_user_can( 'manage_options' ) ) && ! $is_used ) {
  935. $text .= wpautop( __( 'Ticket usage recorded', 'my-tickets' ) );
  936. add_post_meta( $purchase_id, '_tickets_used', $ticket_id );
  937. }
  938. do_action( 'mt_ticket_verified', $verified, $is_used, $purchase_id, $ticket_id );
  939. $ticket_info = "<ul><li class='ticket-type'>" . mt_get_ticket_type( $ticket_id ) . '</li>';
  940. $ticket_info .= "<li class='ticket-price'>" . mt_get_ticket_price( $ticket_id ) . '</li></ul>';
  941. return "<div class='$status_class'>" . $text . '</div>' . $ticket_info;
  942. }
  943. return '<div class="invalid">' . __( 'Not a valid ticket ID', 'my-tickets' ) . '</div>';
  944. }
  945. /**
  946. * Get the validity for a general admission ticket.
  947. *
  948. * @param int|object $ticket Ticket object or ID.
  949. * @param string $format Full validity statement or expiration only.
  950. *
  951. * @return string
  952. */
  953. function mt_get_ticket_validity( $ticket = false, $format = 'full' ) {
  954. if ( ! $ticket ) {
  955. $ticket = mt_get_ticket();
  956. }
  957. if ( ! is_int( $ticket ) ) {
  958. $ticket = $ticket->ID;
  959. }
  960. $ticket_id = mt_get_ticket_id();
  961. $text = '';
  962. $expires = '';
  963. $event_data = get_post_meta( $ticket, '_mc_event_data', true );
  964. if ( $event_data ) {
  965. $general = ( isset( $event_data['general_admission'] ) && ! empty( $event_data['general_admission'] ) ) ? true : false;
  966. $validity = ( isset( $event_data['event_valid'] ) && $general ) ? trim( $event_data['event_valid'] ) : false;
  967. if ( $validity ) {
  968. $data = get_post_meta( $ticket, '_' . $ticket_id, true );
  969. $sale_id = $data['purchase_id'];
  970. $format = ( '' === get_option( 'date_format' ) ) ? 'Y-m-d' : get_option( 'date_format' );
  971. /**
  972. * PHP Date format string used for the date general admission tickets are valid until.
  973. *
  974. * @hook mt_validity_date_format
  975. *
  976. * @param {string} $format Default format from WordPress date_format option.
  977. * @param {array} $event_data Saved data about this event.
  978. *
  979. * @return {string}
  980. */
  981. $format = apply_filters( 'mt_validity_date_format', $format, $event_data );
  982. $date_of_sale = get_the_date( $format, $sale_id );
  983. if ( 'expire' === $validity && isset( $event_data['expire_date'] ) && ! empty( $event_data['expire_date'] ) ) {
  984. $valid_dt = $event_data['expire_date'];
  985. } else {
  986. $valid_dt = ( 'infinite' === $validity ) ? strtotime( $date_of_sale . ' + ' . $validity ) : '';
  987. }
  988. if ( 'infinite' === $validity ) {
  989. $status = __( 'Ticket does not expire', 'my-tickets' );
  990. } else {
  991. $status = mt_date( $format, $valid_dt );
  992. }
  993. // Translators: Purchase date.
  994. $text .= wpautop( sprintf( apply_filters( 'mt_ticket_validity_sale_date', __( '<strong>Purchased:</strong> %s', 'my-tickets' ), $event_data ), '<span class="mt-date-of-sale">' . $date_of_sale . '</span>' ) );
  995. // Translators: Expiration date.
  996. $expires = wpautop( sprintf( apply_filters( 'mt_ticket_validity_expiration_date', __( '<strong>Expires:</strong> %s', 'my-tickets' ), $event_data ), '<span class="mt-date-of-validity">' . $status . '</span>' ) );
  997. $text .= $expires;
  998. if ( strtotime( $date_of_sale . ' + ' . $validity ) < time() ) {
  999. $text .= '<p class="mt-expired">' . __( 'Ticket has expired', 'my-tickets' ) . '</p>';
  1000. }
  1001. }
  1002. }
  1003. return ( 'full' === $format ) ? $text : $expires;
  1004. }
  1005. /**
  1006. * Echo ticket validity.
  1007. *
  1008. * @param int|object $ticket Ticket object or ID.
  1009. * @param string $format Full validity statement or expiration only.
  1010. */
  1011. function mt_ticket_validity( $ticket = false, $format = 'full' ) {
  1012. echo mt_get_ticket_validity( $ticket, $format );
  1013. }
  1014. /**
  1015. * Verify that a ticket is valid, paid for, and which event it's for.
  1016. *
  1017. * @param bool|string $ticket_id Ticket ID.
  1018. *
  1019. * @return void
  1020. */
  1021. function mt_verification( $ticket_id = false ) {
  1022. echo wp_kses_post( mt_get_verification( $ticket_id ) );
  1023. }
  1024. /**
  1025. * Fetch custom fields set up using the custom fields API
  1026. * This function only pulls single values; if you need arrays, you'll need to write your own custom handler.
  1027. *
  1028. * @param bool|false $field name of field as defined in custom code.
  1029. * @param string $callback name of function to call and process output.
  1030. * @param bool|string $ticket_id Ticket ID.
  1031. *
  1032. * @return string
  1033. */
  1034. function mt_get_ticket_custom_field( $field = false, $callback = false, $ticket_id = false ) {
  1035. if ( ! $ticket_id ) {
  1036. $ticket = mt_get_ticket();
  1037. } else {
  1038. $ticket = mt_get_ticket( $ticket_id );
  1039. }
  1040. if ( $field ) {
  1041. $purchase = get_post_meta( $ticket->ID, '_' . $ticket_id, true );
  1042. $purchase_id = $purchase['purchase_id'];
  1043. $meta = get_post_meta( $purchase_id, $field, true );
  1044. if ( $meta && isset( $meta[ $field ] ) ) {
  1045. if ( $callback ) {
  1046. return call_user_func( $callback, $meta );
  1047. } else {
  1048. return $meta[ $field ];
  1049. }
  1050. }
  1051. }
  1052. return '';
  1053. }
  1054. /**
  1055. * Fetch custom fields set up using the custom fields API
  1056. * This function only pulls single values; if you need arrays, you'll need to write your own custom handler.
  1057. *
  1058. * @param bool|false $field name of field as defined in custom code.
  1059. * @param bool|string $ticket_id Ticket ID.
  1060. *
  1061. * @return void
  1062. */
  1063. function mt_ticket_custom_field( $field = false, $ticket_id = false ) {
  1064. echo wp_kses_post( mt_get_ticket_custom_field( $field, false, $ticket_id ) );
  1065. }