Updater Implementation for WordPress Plugins:
Adding support for automatic upgrades to your WordPress plugins with the Sellcodes Licensing is really simple. After you have setup your offer on Sellcodes and configured the licensing options, there is just a small code snippet and one file that you need to include in your WordPress plugin.
There are two components to the updater:
The sellcodes_plugin_updater.php file. This contains the class that makes the handles the updating process.
The code snippet that you place somewhere in your plugin that loads the updater provided by the sellcodes_plugin_updater.php class file.
Step 1 - Define the updater constants
In your main plugin file, preferably near the top, or where you have other constants defined, add the following code:
// this is the URL our updater / license checker pings.
define( SELLCODES_STORE_URL, 'https://sellcodes.com/api/v2/licenses' ); // IMPORTANT: change the name of this constant to something unique to prevent conflicts with other plugins using this system
// OFFER ID of your offer Ex: https://sellcodes.com/jJPUX40f
define( SELLCODES_OFFER_ID, 'jJPUX40f' ); // IMPORTANT: change the name of this constant to something unique to prevent conflicts with other plugins using this system
Step 2 - Include the updater class:
At this link: <link> you can download the sellcodes_plugin_updater.php. You need to copy this file into your own plugin's folder.
Either way you simply need to refer to it properly in the code below, like so:
if( !class_exists( 'Sellcodes_Plugin_Updater' ) ) {
// load our custom updater
include( dirname( __FILE__ ) . '/sellcodes_plugin_updater.php' );
}
You may need to adjust the file path, depending on where you have decided to place the file within your plugin's folder structure.
Step 3 - Initiate the updater class:
We now instantiate our updater class like this:
// retrieve our license key from the DB
$license_key = trim( get_option( 'sellcodes_sample_license_key' ) );
// setup the updater
$sellcodes_updater = new Sellcodes_Plugin_Updater( SELLCODES_STORE_URL, __FILE__, array(
'version' => '1.0', // current version number
'license' => $license_key, // license key (used get_option above to retrieve from DB)
'item_name' => SELLCODES_OFFER_ID, // offer id of this plugin
) );
The license key in this example is stored in an option called "sellcodes_sample_license_key". You will need to adjust this to retrieve the license key from the option you have setup for your plugin. If you need help setting up an option to store the license key in, take a look at the sample plugin as it includes a fully functional settings page.
The complete code for the updater will look something like this:
/*
Plugin Name: Sample Plugin
Plugin URI: http://sellcodes.com/
Description: Illustrates how to include an updater in your plugin for Sellcodes Licensing
Author: Nick
Author URI: http://sellcodes.com/
Version: 1.0
*/
// this is the URL our updater / license checker pings.
define( SELLCODES_STORE_URL, 'https://api.sellcodes.com/v2/licenses' );
define( 'SELLCODES_PLUGIN_LICENSE_PAGE', 'sellcodes-license' );
// the offer id of your offer on Sellcodes
define( SELLCODES_OFFER_ID, “jJPUX40f” );
if( !class_exists( Sellcodes_Plugin_Updater ) ) {
// load our custom updater
include( dirname( __FILE__ ) . '/Sellcodes_Plugin_Updater.php' );
}
// retrieve our license key from the DB
$license_key = trim( get_option( sellcodes_sample_license_key ) );
// setup the updater
$sellcodes_updater = new Sellcodes_Plugin_updater( SELLCODES_STORE_URL, __FILE__, array(
'version' => '1.0', // current version number
'license' => $license_key, // license key (used get_option above to retrieve from DB)
'item_name' => SELLCODES_OFFER_ID)
);
Step 4 - Create settings page
In order for a plugin to receive one-click updates, the license key needs to be activated. To activate a license key the customer will need to enter the key an a field within your plugin settings and then that key needs to be sent to the Sellcodes Licensing API.
The sample plugin shared at: https://sellcodes.com/push-file?file=cFduM2gvWThQUktvbm5MR3ZGZzRuQkgvemVxRTg2bE5SUUdhSVZiVmZwQUxWYVRnWVFrK2c2N0dXMEs3RVlZRm5ETHJrekNhNU4yRVFkRm95RlhWTVdubGFVR1JYbFNxMm80OXVsbzBmLzA9 uses a simple settings page with a single input field. This can work quite well but is meant primarily for demonstrative purposes. It is recommended that you integrate the license key field on your existing settings pages.
Here's sample code for creating a simple settings page:
https://gist.github.com/monadmjeswani/e225ee03eba5d11d35d8125a06f20d3c
This code sets up submenu to the Plugins menu called "Plugin License":
The idea here is that we first enter a license key and click "Save Changes", which causes the license key to be stored in our plugin/theme options. Once the option is stored, we click the "Activate License" button to trigger the API call.
The activate button is just a simple input field with a type of "submit" and a name attribute that is different than our save button. The names must be different so that we can know when the activate license button was clicked.
Step 5 - Activate the license key
To activate a license key, we "listen" for the Activate License button to be clicked then grab the value entered in the license key field and send an activation request to the
<?php
function sellcodes_sample_activate_license()
{
// listen for our activate button to be clicked
if( isset( $_POST['sellcodes_license_activate'] ) && !empty($_POST['sellcodes_sample_license_key']) )
{
update_option( 'sellcodes_sample_license_key', trim($_POST['sellcodes_sample_license_key']) );
// run a quick security check
if( ! check_admin_referer( 'sellcodes_sample_nonce', 'sellcodes_sample_nonce' ) )
return; // get out if we didn't click the Activate button
// retrieve the license from the database
$license = trim( get_option( 'sellcodes_sample_license_key') );
// data to send in our API request
$api_params = array(
'product_id' => SELLCODES_OFFER_ID,
'license_key' => $license,
'baseurl' => home_url()
);
// Call the API.
$response = wp_remote_post( SELLCODES_STORE_URL."/activate_license", array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
// make sure the response came back okay
$httpCode = wp_remote_retrieve_response_code( $response );
if ( 200 !== $httpCode) {
switch ($httpCode) {
case 500:case 0;
$message = __( 'An error occurred, please try again.' );
break;
case 400:
$license_data = json_decode(str_replace("\xEF\xBB\xBF",'',wp_remote_retrieve_body($response)));
if ( false === $license_data->success){
$message = $license_data->message;
}
break;
}
}
else
{
$license_data = json_decode(str_replace("\xEF\xBB\xBF",'',wp_remote_retrieve_body($response)));
if ( false === $license_data->success){
$message = $license_data->message;
}
else{
if(isset($license_data->License_key_valid) && false === $license_data->License_key_valid){
$message = __( 'Your license key has been disabled.' );
}
if(isset($license_data->Activation_ok) && false === $license_data->Activation_ok){
if($license_data->Activation_count == $license_data->Maximum_activations){
$message = __( 'Your license key has reached its activation limit.' );
}
}
}
}
// Check if anything passed on a message constituting a failure
$base_url = admin_url( 'plugins.php?page=' . 'SELLCODES_PLUGIN_LICENSE_PAGE' );
if (!empty( $message ))
{
$redirect = add_query_arg( array( 'sl_activation' => 'false', 'message' => urlencode( $message ) ), $base_url );
wp_redirect( $redirect );
exit();
}
$redirect = add_query_arg( array( 'sl_activation' => 'true', 'message' => 'true'), $base_url );
update_option( 'sellcodes_sample_license_status’, 'valid' );
wp_redirect( $redirect );
exit();
}
}
add_action('admin_init', 'sellcodes_sample_activate_license');
?>
Note: this is purely a sample and may look slightly different in your own implementation.
If everything runs okay after clicking the "Activate License" button, the activate button will be replaced with the word "active", and the license status will reflect the newly activated state in your Sellcodes’s store. If there is an error when activating the license key, the page will be reloaded and an error and message parameter will be added to the URL. We can then use the admin_notices hook (or any other applicable method) to display the error to the customer:
Please check code at this link:
https://gist.github.com/monadmjeswani/3365dd374366c7085c600c1288f683be
Tell people about Sellcodes and get 20-40% on every resulting sale!