cliff
2/14/2017 - 11:32 PM

MT | ET | Override the QR code look for Event Tickets Plus 4.4

MT | ET | Override the QR code look for Event Tickets Plus 4.4

<?php

/**
 * An example for overriding the QR code look for Event Tickets Plus 4.4
 */
class Tribe_Custom_QR_Example {

	public function replace_existing() {
		remove_action( 'tribe_tickets_ticket_email_ticket_bottom', array( Tribe__Tickets_Plus__Main::instance()->qr(), 'inject_qr' ) );
		add_action( 'tribe_tickets_ticket_email_ticket_bottom', array( $this, 'inject_qr' ) );
	}

	/**
	 * Generates the QR image, stores is locally and injects it into the tickets email
	 *
	 * @param $ticket array
	 *
	 * @return string
	 */
	public function inject_qr( $ticket ) {

		$link = $this->_get_link( $ticket['qr_ticket_id'], $ticket['event_id'] );
		$qr   = $this->_get_image( $link );

		if ( ! $qr ) {
			return;
		}
		?>
		<table class="content" align="center" width="620" cellspacing="0" cellpadding="0" border="0" bgcolor="#ffffff" style="margin:15px auto 0; padding:0;">
			<tr>
				<td align="center" valign="top" class="wrapper" width="620">
					<table class="inner-wrapper" border="0" cellpadding="0" cellspacing="0" width="620" bgcolor="#f7f7f7" style="margin:0 auto !important; width:620px; padding:0;">
						<tr>
							<td valign="top" class="ticket-content" align="left" width="140" border="0" cellpadding="20" cellspacing="0" style="padding:20px; background:#f7f7f7;">
								<img src="<?php echo esc_url( $qr ); ?>" width="140" height="140" alt="QR Code Image" style="border:0; outline:none; height:auto; max-width:100%; display:block;"/>
							</td>
							<td valign="top" class="ticket-content" align="left" border="0" cellpadding="20" cellspacing="0" style="padding:20px; background:#f7f7f7;">
								<h3 style="color:#0a0a0e; margin:0 0 10px 0 !important; font-family: 'Helvetica Neue', Helvetica, sans-serif; font-style:normal; font-weight:700; font-size:28px; letter-spacing:normal; text-align:left;line-height: 100%;">
									<span style="color:#0a0a0e !important"><?php esc_html_e( 'Check in for this event', 'event-tickets-plus' ); ?></span>
								</h3>
								<p>
									<?php esc_html_e( 'Scan this QR code at the event to check in.', 'event-tickets-plus' ); ?>
								</p>
							</td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
		<?php
	}


	/**
	 * Generates the link for the QR image
	 *
	 * @param $ticket_id
	 * @param $event_id
	 *
	 * @return string
	 */
	private function _get_link( $ticket_id, $event_id ) {

		$url = add_query_arg( 'event_qr_code', 1, home_url() );
		$url = add_query_arg( 'ticket_id', $ticket_id, $url );
		$url = add_query_arg( 'event_id', $event_id, $url );

		return $url;
	}

	/**
	 * Generates the QR image for a given link and stores it in /wp-content/uploads.
	 * Returns the link to the new image.
	 *
	 * @param $link
	 *
	 * @return string
	 */
	private function _get_image( $link ) {
		if ( ! function_exists( 'ImageCreate' ) ) {
			// The phpqrcode library requires GD but doesn't actually check if it is available
			return null;
		}
		if ( ! class_exists( 'QRencode' ) ) {
			include_once( EVENT_TICKETS_PLUS_DIR . '/vendor/phpqrcode/qrlib.php' );
		}

		$uploads   = wp_upload_dir();
		$file_name = 'qr_' . md5( $link ) . '.png';
		$path      = trailingslashit( $uploads['path'] ) . $file_name;
		$url       = trailingslashit( $uploads['url'] ) . $file_name;

		if ( ! file_exists( $path ) ) {
			QRcode::png( $link, $path, QR_ECLEVEL_L, 3 );
		}

		return $url;
	}

}

$tribe_custom_qr_example = new Tribe_Custom_QR_Example;
$tribe_custom_qr_example->replace_existing();