Skip to content

Instantly share code, notes, and snippets.

@apurbajnu
Created November 9, 2020 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apurbajnu/9e8a383f37fb98317d0157d3b0faa7ef to your computer and use it in GitHub Desktop.
Save apurbajnu/9e8a383f37fb98317d0157d3b0faa7ef to your computer and use it in GitHub Desktop.
Easy Digital Download Automatic class.
<?php
/**
* Created by PhpStorm.
* User: apurbapodder
* Date: 11/9/20
* Time: 2:56 PM
*/
class BDS_License_For_PPU {
private $bds_store_url;
private $bds_product_id;
private $bds_product_name;
private $bds_license_page_slug;
private $parent_page_slug;
private $bds_license_field_key;
public function __construct(
$parent_page_slug = null,
$bds_store_url,
$bds_product_id,
$bds_product_name,
$bds_license_page_slug,
$bds_plugin_version,
$bds_plugin_main_file
) {
if ( $parent_page_slug !== null ) {
$this->parent_page_slug = $parent_page_slug;
$this->bds_license_page_slug = $bds_license_page_slug;
$this->bds_product_id = $bds_product_id;
$this->bds_product_name = $bds_product_name;
$this->bds_store_url = $bds_store_url;
$this->bds_plugin_version = $bds_plugin_version;
$this->bds_license_field_key = $bds_license_page_slug . '_key';
$this->bds_license_status = $bds_license_page_slug . '_status';
$this->bds_plugin_main_file = $bds_plugin_main_file;
add_action( 'admin_menu', array( $this, 'bds_license_page' ) );
add_action( 'init', array( $this, 'bds_plugin_updater' ) );
add_action( 'admin_init', array( $this, 'bds_license_register_option' ) );
add_action( 'admin_init', array( $this,'bds_activate_license' ));
add_action( 'admin_init', array( $this,'bds_deactivate_license' ));
add_action( 'admin_notices', array( $this, 'bds_license_admin_notices' ) );
}
}
/************************************
* this illustrates how to activate
* a license key
*************************************/
function bds_activate_license() {
// listen for our activate button to be clicked
if ( isset( $_POST['bds_license_activate'] ) ) {
// run a quick security check
if ( ! check_admin_referer( 'bds_license_nonce', 'bds_license_nonce' ) ) {
return;
} // get out if we didn't click the Activate button
// retrieve the license from the database
$license = trim( get_option( $this->bds_license_field_key ) );
// data to send in our API request
$api_params = array(
'edd_action' => 'activate_license',
'license' => $license,
'item_name' => urlencode( $this->bds_product_name ), // the name of our product in EDD
'url' => home_url(),
);
// Call the custom API.
$response = wp_remote_post( $this->bds_store_url,
array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
// make sure the response came back okay
if ( 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.' ),
$this->bds_product_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 failure
if ( ! empty( $message ) ) {
$base_url = admin_url( 'admin.php?page=' . $this->bds_license_page_slug );
$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( $this->bds_license_status, $license_data->license );
wp_redirect( admin_url( 'admin.php?page=' . $this->bds_license_page_slug ) );
exit();
}
}
/***********************************************
* Illustrates how to deactivate a license key.
* This will decrease the site count
***********************************************/
function bds_deactivate_license() {
// listen for our activate button to be clicked
if ( isset( $_POST['bds_license_deactivate'] ) ) {
// run a quick security check
if ( ! check_admin_referer( 'bds_license_nonce', 'bds_license_nonce' ) ) {
return;
} // get out if we didn't click the Activate button
// retrieve the license from the database
$license = trim( get_option( $this->bds_license_field_key ) );
// data to send in our API request
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => $license,
'item_name' => urlencode( $this->bds_product_name ), // the name of our product in EDD
'url' => home_url(),
);
// Call the custom API.
$response = wp_remote_post( $this->bds_store_url,
array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
// make sure the response came back okay
if ( 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.' );
}
$base_url = admin_url( 'admin.php?page=' . $this->bds_license_page_slug );
$redirect = add_query_arg( array( 'sl_activation' => 'false', 'message' => urlencode( $message ) ),
$base_url );
wp_redirect( $redirect );
exit();
}
// 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( $this->bds_license_status );
}
wp_redirect( admin_url( 'admin.php?page=' . $this->bds_license_page_slug ) );
exit();
}
}
/************************************
* 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
*************************************/
function bds_check_license() {
global $wp_version;
$license = trim( get_option( $this->bds_license_field_key ) );
$api_params = array(
'edd_action' => 'check_license',
'license' => $license,
'item_name' => urlencode( $this->bds_product_name ),
'url' => home_url(),
);
// Call the custom API.
$response = wp_remote_post( $this->bds_store_url,
array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
if ( is_wp_error( $response ) ) {
return false;
}
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( $license_data->license == 'valid' ) {
return true;
// this license is still valid
} else {
return false;
// this license is no longer valid
}
}
/**
* This is a means of catching errors from the activation method above and displaying it to the customer
*/
function bds_license_admin_notices() {
if(!$this->bds_check_license()){
?>
<div class="error">
<p><?php echo "Please <a href=".admin_url( 'admin.php?page=' . $this->bds_license_page_slug ).">Activate</a> $this->bds_product_name License to get Critical Update Notice"; ?></p>
</div>
<?php
}
if ( isset( $_GET['sl_activation'] ) && ! empty( $_GET['message'] ) ) {
switch ( $_GET['sl_activation'] ) {
case 'false':
$message = urldecode( $_GET['message'] );
?>
<div class="error">
<p><?php echo $message; ?></p>
</div>
<?php
break;
case 'true':
default:
?>
<div class="success">
<p><?php echo 'License has been activated successfully' ?></p>
</div>
<?php
break;
}
}
}
function bds_license_register_option() {
// creates our settings in the options table
register_setting( $this->bds_license_page_slug, $this->bds_license_field_key, 'bds_sanitize_license' );
}
function bds_sanitize_license( $new ) {
$old = get_option( $this->bds_license_field_key );
if ( $old && $old != $new ) {
delete_option( $this->bds_license_status ); // new license has been entered, so must reactivate
}
return $new;
}
/**
* Initialize the updater. Hooked into `init` to work with the
* wp_version_check cron job, which allows auto-updates.
*/
function bds_plugin_updater() {
// To support auto-updates, this needs to run during the wp_version_check cron job for privileged users.
$doing_cron = defined( 'DOING_CRON' ) && DOING_CRON;
if ( ! current_user_can( 'manage_options' ) && ! $doing_cron ) {
return;
}
// retrieve our license key from the DB
$license_key = trim( get_option( $this->bds_license_field_key ) );
// setup the updater
$bds_updater = new BDS_SL_Plugin_Updater( $this->bds_store_url, $this->bds_plugin_main_file,
array(
'version' => $this->bds_plugin_version, // current version number
'license' => $license_key, // license key (used get_option above to retrieve from DB)
'item_id' => $this->bds_product_id, // ID of the product
'author' => 'Apurba', // author of this plugin
'beta' => false,
)
);
}
public function bds_license_page() {
add_submenu_page( $this->parent_page_slug,
$this->bds_product_name,
$this->bds_product_name,
'manage_options',
$this->bds_license_page_slug,
array(
$this,
'bds_license_page_cb',
) );
}
public function bds_license_page_cb() {
$license = get_option( $this->bds_license_field_key );
$status = get_option( $this->bds_license_status );
?>
<div class="wrap">
<h2><?php _e( 'Plugin License Options' ); ?></h2>
<form method="post" action="options.php">
<?php settings_fields( $this->bds_license_page_slug ); ?>
<table class="form-table">
<tbody>
<tr valign="top">
<th scope="row" valign="top">
<?php _e( 'License Key' ); ?>
</th>
<td>
<input id="<?php echo $this->bds_license_field_key ?>"
name="<?php echo $this->bds_license_field_key ?>" type="text"
class="regular-text" value="<?php esc_attr_e( $license ); ?>"/>
<label class="description"
for="<?php echo $this->bds_license_field_key ?>"><?php _e( 'Enter your license key' ); ?></label>
</td>
</tr>
<?php if ( false !== $license ) { ?>
<tr valign="top">
<th scope="row" valign="top">
<?php _e( 'Activate License' ); ?>
</th>
<td>
<?php if ( $status !== false && $status == 'valid' ) { ?>
<span style="color:green;"><?php _e( 'active' ); ?></span>
<?php wp_nonce_field( 'bds_license_nonce', 'bds_license_nonce' ); ?>
<input type="submit" class="button-secondary" name="bds_license_deactivate"
value="<?php _e( 'Deactivate License' ); ?>"/>
<?php } else {
wp_nonce_field( 'bds_license_nonce', 'bds_license_nonce' ); ?>
<input type="submit" class="button-secondary" name="bds_license_activate"
value="<?php _e( 'Activate License' ); ?>"/>
<?php } ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php submit_button(); ?>
</form>
<?php
}
}
@apurbajnu
Copy link
Author

CONST BDS_STORE_URL = 'https://bestdecoders.com/';

CONST BDS_PRODUCT_ID = 195;

CONST BDS_PRODUCT_NAME = 'Price Per Unit Pro';

CONST BDS_PLUGIN_LICENSE_PAGE = 'price-per-unit-lincense';

//Activate License Process
New BDS_License_For_PPU('bds_plugin',self::BDS_STORE_URL,self::BDS_PRODUCT_ID,self::BDS_PRODUCT_NAME,self::BDS_PLUGIN_LICENSE_PAGE,$this->version,plugin_dir_path( dirname( FILE ) ).'price-per-unit-pro-for-woocommerce.php');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment