Updater Implementation for WordPress Plugins -- Activating a License Key
Sample code to illustrate how to use the Updater class to activate a license key.
<?phpfunctionedd_sample_activate_license(){// listen for our activate button to be clickedif(isset($_POST['edd_license_activate'])){// run a quick security checkif(!check_admin_referer('edd_sample_nonce','edd_sample_nonce'))return;// get out if we didn't click the Activate button// retrieve the license from the database$license=trim(get_option('edd_sample_license_key'));// data to send in our API request$api_params=array('edd_action'=>'activate_license','license'=>$license,'item_name'=>urlencode(EDD_SAMPLE_ITEM_NAME),// the name of our product in EDD'url'=>home_url());// Call the custom API.$response=wp_remote_post(EDD_SAMPLE_STORE_URL,array('timeout'=>15,'sslverify'=>false,'body'=>$api_params));// make sure the response came back okayif(is_wp_error($response)||200!==wp_remote_retrieve_response_code($response)){if(is_wp_error($response)){$message=$response->get_error_message();}else{$message=__('An error occurred, please try again.');}}else{$license_data=json_decode(wp_remote_retrieve_body($response));if(false===$license_data->success){switch($license_data->error){case'expired':$message=sprintf(__('Your license key expired on %s.'),date_i18n(get_option('date_format'),strtotime($license_data->expires,current_time('timestamp'))));break;case'disabled':case'revoked':$message=__('Your license key has been disabled.');break;case'missing':$message=__('Invalid license.');break;case'invalid':case'site_inactive':$message=__('Your license is not active for this URL.');break;case'item_name_mismatch':$message=sprintf(__('This appears to be an invalid license key for %s.'),EDD_SAMPLE_ITEM_NAME);break;case'no_activations_left':$message=__('Your license key has reached its activation limit.');break;default:$message=__('An error occurred, please try again.');break;}}}// Check if anything passed on a message constituting a failureif(!empty($message)){$base_url=admin_url('plugins.php?page='.EDD_SAMPLE_PLUGIN_LICENSE_PAGE);$redirect=add_query_arg(array('sl_activation'=>'false','message'=>urlencode($message)),$base_url);wp_redirect($redirect);exit();}// $license_data->license will be either "valid" or "invalid"update_option('edd_sample_license_status',$license_data->license);wp_redirect(admin_url('plugins.php?page='.EDD_SAMPLE_PLUGIN_LICENSE_PAGE));exit();}}add_action('admin_init','edd_sample_activate_license');