Created
December 18, 2015 00:27
-
-
Save elimn/89fbec4850beb115f7a1 to your computer and use it in GitHub Desktop.
MT | ETP | Ties into the Zxing Barcode Scanner app for iOS and Android, allowing one-click scans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /* | |
| * This adds a fancy one-click scan QR link tie-in to the native zxing Barcode scanner app | |
| * Android: https://play.google.com/store/apps/details?id=com.google.zxing.client.android&hl=en | |
| * iOS: https://itunes.apple.com/us/app/barcodes-scanner/id417257150?mt=8 | |
| */ | |
| function tribe_qr_scan_link_notice () { | |
| if ( isset($_GET['page']) && $_GET['page'] === 'tickets-attendees' ) { | |
| $qr_url = urlencode( site_url().'?event_qr_code={CODE}' ); | |
| echo '<div class="updated" style="border:none;padding:0;background:transparent;box-shadow:none;"><p>'; | |
| echo '<a href="zxing://scan/?ret=' . $qr_url . '" class="button button-primary button-large" style="width:100%;text-align:center;" id="qr-scan-link">Scan a QR Code</a>'; | |
| echo '</p></div>'; | |
| } | |
| } | |
| add_filter( 'admin_notices', 'tribe_qr_scan_link_notice', 100 ); | |
| /* | |
| * This handles the QR submitted data | |
| */ | |
| function tribe_handle_qr_redirects() { | |
| if ( ! class_exists('Tribe__Tickets_Plus__Main') ) return; | |
| // Returns a valid URL or an empty string | |
| $url = isset( $_GET['event_qr_code'] ) ? esc_url( $_GET['event_qr_code'] ) : ''; | |
| // the following code is mostly copied from the Tribe__Tickets_Plus__QR | |
| // Had to be copied to avoid private methods :( | |
| if ( ! empty( $url ) && is_user_logged_in() && current_user_can( 'edit_posts' )) { | |
| $url_components = parse_url( htmlspecialchars_decode ( $url ) ); | |
| parse_str( $url_components['query'], $query ); | |
| // Check if it's our time to shine. | |
| // Not as fancy as a custom permalink handler, but way less likely to fail depending on setup and settings | |
| if ( ! isset( $query['event_qr_code'] ) ) { | |
| return; | |
| } | |
| // Check all the data we need is there | |
| if ( empty( $query['ticket_id'] ) || empty( $query['event_id'] ) ) { | |
| return; | |
| } | |
| // If the user is the site owner (or similar), Check in the user to the event | |
| if ( is_user_logged_in() && current_user_can( 'edit_posts' ) ) { | |
| $modules = Tribe__Tickets__Tickets::modules(); | |
| foreach ( $modules as $class => $module ) { | |
| if ( ! is_callable( array( $class, 'get_instance' ) ) ) { | |
| continue; | |
| } | |
| $obj = call_user_func( array( $class, 'get_instance' ) ); | |
| $obj->checkin( $query['ticket_id'] ); | |
| } | |
| $post = get_post( $query['event_id'] ); | |
| if ( empty( $post ) ) { | |
| return; | |
| } | |
| $url = add_query_arg( array( | |
| 'post_type' => $post->post_type, | |
| 'page' => Tribe__Tickets__Tickets_Handler::$attendees_slug, | |
| 'event_id' => $query['event_id'], | |
| 'qr_checked_in' => $query['ticket_id'], | |
| ), admin_url( 'edit.php' ) ); | |
| } else { // Probably just the ticket holder, redirect to the event front end single | |
| $url = get_permalink( $query['event_id'] ); | |
| } | |
| wp_redirect( $url ); | |
| exit; | |
| } | |
| } | |
| add_filter( 'init', 'tribe_handle_qr_redirects', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment