Skip to content

Instantly share code, notes, and snippets.

@generatepress
Created July 27, 2014 20:03
Show Gist options
  • Save generatepress/3d53312b7bf4d4faef09 to your computer and use it in GitHub Desktop.
Save generatepress/3d53312b7bf4d4faef09 to your computer and use it in GitHub Desktop.
add_action('admin_init', 'generate_activate_customer_email');
function generate_activate_customer_email() {
if( isset( $_POST['generate_customer_email_activate'] ) ) {
if( ! check_admin_referer( 'generate_customer_email_nonce', 'generate_customer_email_nonce' ) )
return; // get out if we didn't click the Activate button
$generate_customer_email = get_option( 'generate_customer_email' );
global $wp_version;
// First, let's find the products associated with the email
$response = wp_remote_post(
'http://myurl.com/api/licenses/check-email.php',
array(
'body' =>
array(
'generate_action' => 'get_email',
'email' => $generate_customer_email,
)
)
);
// make sure the response came back okay
if ( is_wp_error( $response ) )
return false;
// Now we have our associated downloads
$downloads = json_decode(wp_remote_retrieve_body( $response ), true);
// If downloads exist, validate customer's email address
// Also store which downloads are purchased in the database - may need them
if ( !empty( $downloads ) ) :
update_option( 'generate_purchased_products', $downloads );
update_option( 'generate_customer_email_status', 'valid' );
else :
update_option( 'generate_purchased_products', '' );
update_option( 'generate_customer_email_status', 'false' );
endif;
// If downloads exist, let's do something with them
if ( !empty( $downloads) ) :
foreach ( $downloads as $download ) {
$download_name = $download;
// Let's give the server the email and download and get the license for each download back
$response_get_license = wp_remote_post(
'http://myurl.com/api/licenses/check-email.php',
array(
'body' =>
array(
'generate_action' => 'get_license',
'email' => $generate_customer_email,
'download' => $download
)
)
);
if ( is_wp_error( $response_get_license ) )
return false;
// Got our license - time to turn it into a string and send to server to activation
$licenses = json_decode(wp_remote_retrieve_body( $response_get_license ), true);
$download_db_name = str_replace(' ','_', $download);
$download_db_name = strtolower($download_db_name);
$download_db_name = $download_db_name . '_license';
$license = implode($licenses,',');
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode( $download )
);
$license_response = wp_remote_get( add_query_arg( $api_params, 'http://myurl.com' ), array( 'timeout' => 15, 'sslverify' => false ) );
if ( is_wp_error( $license_response ) )
return false;
$license_data = json_decode( wp_remote_retrieve_body( $license_response ) );
update_option( $download_db_name, $license_data->license );
}
endif;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment