'Clip It!',
'signin' => ''
), $atts ) );
$result = passbook_membercard_function( $title, $signin );
return $result;
}
function passbook_membercard_function( $title, $signin ) {
if (is_user_logged_in()) {
return '' . "$title";
} else {
return wp_login_form( array('echo' => false, 'redirect' => get_permalink()) );
}
}
add_filter( 'query_vars', 'passbook_query_vars' );
function passbook_query_vars( $public_query_vars ) {
$public_query_vars[ ] = PASSBOOK_CLIP_TRIGGER;
return $public_query_vars;
}
/**
* Passbook clipper interceptor. Will initiate pass clipping if non empty input with the name defined
* by PASSBOOK_CLIP_TRIGGER was detected.
*/
add_action( 'template_redirect', 'passbook_clip_it' );
function passbook_clip_it() {
$clip_it = get_query_var( PASSBOOK_CLIP_TRIGGER );
if (!empty($clip_it)) {
if (!is_user_logged_in()) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit();
}
global $current_user;
get_currentuserinfo();
$options = get_option( 'passbook_gateway' );
$site_id = $options[ 'site_id' ];
// Prepare data dictionary
$first_name = $current_user->user_firstname;
if (empty($first_name)) {
$first_name = 'Club';
}
$last_name = $current_user->user_lastname;
if (empty($last_name)) {
$last_name = 'Member';
}
// We use [siteID]-[user ID] as a pass serial number.
$serial = "$site_id-" . $current_user->ID;
$dictionary = array(
'user_id' => $current_user->ID,
'email' => trim( $current_user->user_email ),
'email_hash' => md5( strtolower( trim( $current_user->user_email ) ) ),
'serial' => $serial,
'first_name' => trim( $first_name ),
'last_name' => trim( $last_name )
);
// Will try to retrieve existing pass first
$pass = passbook_get_pass( $serial );
if ($pass == null) {
// No pass found, will create new one
$serial = passbook_create_pass( $dictionary );
} else {
$serial = passbook_update_pass( $serial, $dictionary );
}
if ($serial != null) {
$pass = passbook_get_pass( $serial );
} else {
$pass = null;
}
if ($pass) {
header( 'Content-disposition: attachment; filename=pass.pkpass' );
header( 'Content-type:application/vnd.apple.pkpass' );
header( 'Content-length: ' . strlen( $pass ) );
echo $pass;
}
exit;
}
}
/**
* Create Apple Passbook pass using PassGate.com Rest API
*
* @param array $data_dictionary Data dictionary
*
* @return mixed Returns null if failed to create new pass. Returns new pass serial number otherwise.
*/
function passbook_create_pass( $data_dictionary ) {
$options = get_option( 'passbook_gateway' );
$api_key = $options[ 'api_key' ];
$site_id = $options[ 'site_id' ];
$generator = $options[ 'generator' ];
$url = PASSBOOK_REST_API_URL . "/$site_id/$api_key/$generator/pass";
$headers = array(
'User-Agent' => 'Wordpress Passbook plugin',
'Content-Type' => 'application/json;charset=UTF-8'
);
$request = new WP_Http;
$response = $request->request( $url, array(
'method' => 'POST',
'timeout' => '30',
'httpversion' => '1.0',
'blocking' => true,
'headers' => $headers,
'body' => json_encode( $data_dictionary )
) );
if (is_wp_error( $response ) || (201 != $response[ 'response' ][ 'code' ])) {
return null;
}
$json_response = json_decode( $response[ 'body' ] );
return $json_response->{'serial'};
}
/**
* Update Apple Passbook pass using PassGate.com Rest API
*
* @param string $serial Pass serial number
* @param array $data_dictionary Data dictionary
*
* @return mixed Returns null if failed to update new pass. Returns pass serial number otherwise.
*/
function passbook_update_pass( $serial, $data_dictionary ) {
$options = get_option( 'passbook_gateway' );
$api_key = $options[ 'api_key' ];
$site_id = $options[ 'site_id' ];
$generator = $options[ 'generator' ];
$url = PASSBOOK_REST_API_URL . "/$site_id/$api_key/$generator/pass/$serial";
$headers = array(
'User-Agent' => 'Wordpress Passbook plugin',
'Content-Type' => 'application/json;charset=UTF-8'
);
$request = new WP_Http;
$response = $request->request( $url, array(
'method' => 'PUT',
'timeout' => '30',
'httpversion' => '1.0',
'blocking' => true,
'headers' => $headers,
'body' => json_encode( $data_dictionary )
) );
if (is_wp_error( $response ) || (200 != $response[ 'response' ][ 'code' ])) {
return null;
}
$json_response = json_decode( $response[ 'body' ] );
return $json_response->{'serial'};
}
/**
* Retrieve Apple Passbook pass using from PassGate.com using Rest API
*
* @param array $serial Pass serial number
*
* @return mixed Returns pass or null if failed to retireve the pass.
*/
function passbook_get_pass( $serial ) {
$options = get_option( 'passbook_gateway' );
$api_key = $options[ 'api_key' ];
$site_id = $options[ 'site_id' ];
$generator = $options[ 'generator' ];
$url = PASSBOOK_REST_API_URL . "/$site_id/$api_key/$generator/pass/$serial";
$headers = array(
'User-Agent' => 'Wordpress Passbook plugin',
);
$request = new WP_Http;
$response = $request->request( $url, array(
'method' => 'GET',
'timeout' => '30',
'httpversion' => '1.0',
'blocking' => true,
'headers' => $headers
) );
if (is_wp_error( $response ) || (200 != $response[ 'response' ][ 'code' ])) {
return null;
}
$json_response = json_decode( $response[ 'body' ] );
return base64_decode( $json_response->{'pass'} );
}
?>