Last active
December 19, 2015 10:19
-
-
Save jgalea/5939198 to your computer and use it in GitHub Desktop.
Activation and deactivation procedure for licenses in Easy Digital Downloads, Software Licensing add-on.
This file contains 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 | |
add_action( 'admin_init', 'wprss_et_activate_deactivate_license' ); | |
/** | |
* Handles the activation/deactivation process | |
* | |
* @since 1.1 | |
*/ | |
function wprss_et_activate_deactivate_license() { | |
// listen for our activate button to be clicked | |
if( isset( $_POST['wprss_et_license_activate'] ) || isset( $_POST['wprss_et_license_deactivate'] ) ) { | |
// run a quick security check | |
if( ! check_admin_referer( 'edd_sample_nonce', 'edd_sample_nonce' ) ) | |
return; // get out if we didn't click the Activate/Deactivate button | |
// retrieve the license from the database | |
$license = trim( get_option( 'wprss_et_license_key' ) ); | |
if ( isset( $_POST['wprss_et_license_activate'] ) ) { | |
// data to send in our API request | |
$api_params = array( | |
'edd_action'=> 'activate_license', | |
'license' => $license, | |
'item_name' => urlencode( WPRSS_ET_SL_ITEM_NAME ) // the name of our product in EDD | |
); | |
} | |
else if ( isset( $_POST['wprss_et_license_deactivate'] ) ) { | |
// data to send in our API request | |
$api_params = array( | |
'edd_action'=> 'deactivate_license', | |
'license' => $license, | |
'item_name' => urlencode( WPRSS_ET_SL_ITEM_NAME ) // the name of our product in EDD | |
); | |
} | |
// Call the custom API. | |
$response = wp_remote_get( add_query_arg( $api_params, WPRSS_ET_SL_STORE_URL ) ); | |
// make sure the response came back okay | |
if ( is_wp_error( $response ) ) | |
return false; | |
// decode the license data | |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
// $license_data->license will be either "active" or "inactive" | |
update_option( 'wprss_et_license_status', $license_data->license ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment