1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
<?php
/**
* It will handle the license activate & deactivate funtionality.
* @author Tyche Softwares
* @package Abandoned-Cart-Pro-for-WooCommerce/Admin/License
* @since 5.0
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( !class_exists('Wcap_EDD' ) ) {
/**
* It will handle the license activate & deactivate funtionality.
*/
class Wcap_EDD{
/**
* It will register the license setting.
* @since 2.3.1
*/
public static function wcap_edd_ac_register_option() {
// creates our settings in the options table
register_setting( 'edd_sample_license', 'edd_sample_license_key_ac_woo', array( 'Wcap_EDD', 'wcap_edd_sanitize_license' ) );
}
/**
* It will sanitize the license key entred by the admin.
* @param string $new Licese key
* @return string $new Licese key
* @since 2.3.1
*/
public static function wcap_edd_sanitize_license( $new ) {
$old = get_option( 'edd_sample_license_key_ac_woo' );
if ( $old && $old != $new ) {
delete_option( 'edd_sample_license_status_ac_woo' ); // new license has been entered, so must reactivate
}
return $new;
}
/**
* It will activate the licese key on our server.
* @since 2.3.1
*/
public static function wcap_edd_ac_activate_license() {
// listen for our activate button to be clicked
if ( isset( $_POST['edd_ac_license_activate'] ) ) {
// 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 button
// retrieve the license from the database
$license = trim( get_option( 'edd_sample_license_key_ac_woo' ) );
// data to send in our API request
$api_params = array(
'edd_action'=> 'activate_license',
'license' => $license,
'item_name' => urlencode( EDD_SL_ITEM_NAME_AC_WOO ) // the name of our product in EDD
);
// Call the custom API.
$response = wp_remote_get( add_query_arg( $api_params, EDD_SL_STORE_URL_AC_WOO ), array( 'timeout' => 15, 'sslverify' => false ) );
// 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( 'edd_sample_license_status_ac_woo', $license_data->license );
}
}
/**
* It will deactivate a license key.
* This will descrease the site count.
* @since 2.3.1
*/
public static function wcap_edd_ac_deactivate_license() {
// listen for our activate button to be clicked
if ( isset( $_POST['edd_ac_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 button
// retrieve the license from the database
$license = trim( get_option( 'edd_sample_license_key_ac_woo' ) );
// data to send in our API request
$api_params = array(
'edd_action'=> 'deactivate_license',
'license' => $license,
'item_name' => urlencode( EDD_SL_ITEM_NAME_AC_WOO ) // the name of our product in EDD
);
// Call the custom API.
$response = wp_remote_get( add_query_arg( $api_params, EDD_SL_STORE_URL_AC_WOO ), array( 'timeout' => 15, 'sslverify' => false ) );
// 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 "deactivated" or "failed"
if ( $license_data->license == 'deactivated' )
delete_option( 'edd_sample_license_status_ac_woo' );
}
}
/**
* This illustrates how to check if a license key is still valid the updater does this for you, so this is only needed if you want to do something custom.
* @since 2.3.1
*/
public static function edd_sample_check_license() {
global $wp_version;
$license = trim( get_option( 'edd_sample_license_key_ac_woo' ) );
$api_params = array(
'edd_action' => 'check_license',
'license' => $license,
'item_name' => urlencode( EDD_SL_ITEM_NAME_AC_WOO )
);
// Call the custom API.
$response = wp_remote_get( add_query_arg( $api_params, EDD_SL_STORE_URL_AC_WOO ), array( 'timeout' => 15, 'sslverify' => false ) );
if ( is_wp_error( $response ) )
return false;
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( $license_data->license == 'valid' ) {
echo 'valid';
exit;
// this license is still valid
} else {
echo 'invalid';
exit;
// this license is no longer valid
}
}
}
}