Complete Licensing System for WordPress Plugins

We have explain how to integrate automatic update for wordpress plugins and how to create sample license form for activating licensing system now we post here complete code for that tutorial:

<?php

/*
Plugin Name: WPLS Sample Plugin
Plugin URI: http://licenshop.com/
Description: Illustrates how to include an updater in your plugin for WordPress Licensing System
Author: Hassan Noor
Author URI: http://hassan.noor.pw
Version: 1.0
*/

set_site_transient( 'update_plugins', null );
// this is the URL our updater / license checker pings. This should be the URL of the site with WPLS installed
define( 'WPLS_SAMPLE_STORE_URL', 'http://pro.licenshop.com' ); // you should use your own CONSTANT server url, and be sure to replace it throughout this file

// the name of your product. This should match the download name in WPLS exactly
define( 'WPLS_SAMPLE_ITEM_CODE', 'plugin3520' ); // you should use your own CONSTANT CODE, and be sure to replace it throughout this file

/***** WPLS PLUGIN UPDATER START *****/
require ('plugin-updates/plugin-update-checker.php');

// retrieve our license key from the DB
$license = get_option( 'wpls_sample_license_key' ); // get_option('305994sample_license');

$userip = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'];
$dataSample = array(
            'wpls_action' => 'get_version',
            'license' => $license == false ? 'getlatestversion' : $license,
            'code' => WPLS_SAMPLE_ITEM_CODE,
            'url' => $_SERVER['SERVER_NAME'],
            'host' => $userip
    );

    
$PSampleUpdateChecker = new PluginUpdateChecker( add_query_arg( $dataSample, WPLS_SAMPLE_STORE_URL), __FILE__ );
/***** WPLS PLUGIN UPDATER END *****/
        
/************************************
* the code below is just a standard
* options page. Substitute with
* your own.
*************************************/

function wpls_sample_license_menu() {
    add_plugins_page( 'Licensed Plugin', 'Licensed Plugin', 'manage_options', 'licensed-plugin', 'wpls_sample_license_page' );
}
add_action('admin_menu', 'wpls_sample_license_menu');

function wpls_sample_license_page() {
    $license     = get_option( 'wpls_sample_license_key' );
    $status     = get_option( 'wpls_sample_license_status' );
    $type         = get_option( 'wpls_sample_license_key_type' );
    ?>
    <div class="wrap">
        <h2><?php _e('Licensed Plugin Options'); ?></h2>
        <form method="post" action="options.php">
        
            <?php settings_fields('wpls_sample_license'); ?>
            
            <table class="form-table">
                <tbody>
                    <tr valign="top">    
                        <th scope="row" valign="top">
                            <label for="wpls_sample_license_url"><?php _e('Where you get license?'); ?></label>
                        </th>
                        <td>
                            <?php _e('Please visit <a href="http://pro.licenshop.com">http://pro.licenshop.com</a> to get your license for free.'); ?>
                        </td>
                    </tr>                    
                    <tr valign="top">    
                        <th scope="row" valign="top">
                            <label for="wpls_sample_license_key"><?php _e('License Key:'); ?></label>
                        </th>
                        <td>
                            <input id="wpls_sample_license_key" name="wpls_sample_license_key" type="text" class="regular-text" value="<?php esc_attr_e( $license ); ?>" />
                            <p class="description"><?php _e('Enter your license key'); ?></p>
                        </td>
                    </tr>
                    <?php if( false !== $license ) { ?>
                        <tr valign="top">    
                            <th scope="row" valign="top">
                                <label for="act_button"><?php _e('Action Button:'); ?></label>
                            </th>
                            <td>
                                <?php if( $status !== false && ($status == 'active' || $status == 'activated') ) { ?>
                                    <?php wp_nonce_field( 'wpls_sample_nonce', 'wpls_sample_nonce' ); ?>
                                    <input type="submit" class="button-secondary" name="wpls_license_deactivate" value="<?php _e('Deactivate License'); ?>"/>
                                    <span style="color:green;"><?php _e('Active'); ?></span>
                                <?php } else {
                                    wp_nonce_field( 'wpls_sample_nonce', 'wpls_sample_nonce' ); ?>
                                    <input type="submit" class="button-secondary" name="wpls_license_activate" value="<?php _e('Activate License'); ?>"/>
                                <?php } ?>
                            </td>
                        </tr>
                        <tr valign="top">
                        <th scope="row" valign="top"><?php _e('License data');?></th>
                        <td><?php echo wpls_sample_get_license_data();?></td>
                        </tr>
                        <tr valign="top">
                        <th scope="row" valign="top"><?php _e('Advance Feature');?></th>
                        <td>
                            <p><strong><?php _e('BASIC FEATURES');?></strong></p>
                            <p>
                                <ol>
                                    <li><?php _e('FEATURE #1');?></li>
                                    <li><?php _e('FEATURE #2');?></li>
                                    <li><?php _e('FEATURE #3');?></li>
                                </ol>
                            </p>
                            <p><strong><?php _e('MORE FEATURES?');?></strong></p>
                            <?php if($type == 'PRO' || $type == 'DEVELOPER'){?>
                            <p>
                                <ol>
                                    <li><?php _e('FEATURE #4');?></li>
                                    <li><?php _e('FEATURE #5');?></li>
                                    <li><?php _e('FEATURE #6');?></li>
                                </ol>
                            </p>
                            <?php
                            }
                            else {
                                echo '<p><strong><a href="'.wpls_sample_ugeturl('PRO').'">'.__('Upgrade to more...').'</a></strong></p>';
                            }        
                            if($type == 'DEVELOPER'){?>
                            <p>
                                <ol>
                                    <li><?php _e('FEATURE #7');?></li>
                                    <li><?php _e('FEATURE #8');?></li>
                                    <li><?php _e('FEATURE #9');?></li>
                                </ol>
                            </p>
                            <?php
                            }
                            else {
                                echo '<p><strong><a href="'.wpls_sample_ugeturl('DEVELOPER').'">'.__('Upgrade to more...').'</a></strong></p>';
                            }        
                            ?>
                        </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>    
            <?php submit_button(); ?>
        
        </form>
    <?php
}

function wpls_sample_register_option() {
    // creates our settings in the options table
    register_setting('wpls_sample_license', 'wpls_sample_license_key', 'wpls_sanitize_license' );
}
add_action('admin_init', 'wpls_sample_register_option');

function wpls_sanitize_license( $new ) {
    $old = get_option( 'wpls_sample_license_key' );
    if( $old && $old != $new ) {
        delete_option( 'wpls_sample_license_status' ); // new license has been entered, so must reactivate
    }
    return $new;
}

/************************************
* this illustrates how to activate
* a license key
*************************************/

function wpls_sample_activate_license() {

    // listen for our activate button to be clicked
    if( isset( $_POST['wpls_license_activate'] ) ) {

        // run a quick security check
         if( ! check_admin_referer( 'wpls_sample_nonce', 'wpls_sample_nonce' ) )     
            return; // get out if we didn't click the Activate button

        // retrieve the license from the database
        $license = get_option( 'wpls_sample_license_key' );    

        // data to send in our API request
        $api_params = array(
            'wpls_action' => 'activate',
            'license' => $license,
            'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ), // the CODE of our product in WPLS
            'url' => home_url(),
            'host' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']
        );
        
        // Call the custom API.
        $response = wp_remote_get( add_query_arg( $api_params, WPLS_SAMPLE_STORE_URL ), 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_status will be either "valid" or "invalid"

        update_option( 'wpls_sample_license_status', $license_data->license_status );
        update_option( 'wpls_sample_license_key_type', $license_data->license_type );

    }
}

add_action('admin_init', 'wpls_sample_activate_license');

/***********************************************
* Illustrates how to deactivate a license key.
* This will descrease the site count
***********************************************/

function wpls_sample_deactivate_license() {

    // listen for our activate button to be clicked
    if( isset( $_POST['wpls_license_deactivate'] ) ) {

        // run a quick security check
         if( ! check_admin_referer( 'wpls_sample_nonce', 'wpls_sample_nonce' ) )     
            return; // get out if we didn't click the Activate button

        // retrieve the license from the database
        $license = get_option( 'wpls_sample_license_key' );
            
        // data to send in our API request
        $api_params = array(
            'wpls_action' => 'deactivate',
            'license' => $license,
            'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ), // the CODE of our product in WPLS
            'url' => home_url(),
            'host' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']
        );
        
        // Call the custom API.
        $response = wp_remote_get( add_query_arg( $api_params, WPLS_SAMPLE_STORE_URL ), 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_status will be either "deactivated" or "failed"
        if( $license_data->license_status == 'deactivated' )
            delete_option( 'wpls_sample_license_status' );
            delete_option( 'wpls_sample_license_key_type');
    }
}
add_action('admin_init', 'wpls_sample_deactivate_license');

/************************************
* 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 wpls_sample_check_license() {

    global $wp_version;

    $license = get_option( 'wpls_sample_license_key' );
        
    $api_params = array(
        'wpls_action' => 'check_license',
        'license' => $license,
        'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ),
        'url' => home_url(),
        'host' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']
    );

    // Call the custom API.
    $response = wp_remote_get( add_query_arg( $api_params, WPLS_SAMPLE_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );

    if ( is_wp_error( $response ) )
        return false;

    $license_data = json_decode( wp_remote_retrieve_body( $response ) );
    
    return $license_data;
}

function wpls_sample_get_license_data() {

    global $wp_version;

    $license = get_option( 'wpls_sample_license_key' );
        
    $api_params = array(
        'wpls_action' => 'get_data',
        'license' => $license,
        'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ),
        'url' => home_url(),
        'host' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR']
    );

    // Call the custom API.
    $response = wp_remote_get( add_query_arg( $api_params, WPLS_SAMPLE_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );

    if ( is_wp_error( $response ) )
        return false;

    $license_data = json_decode( wp_remote_retrieve_body( $response ) );
    
    $data = "";
    foreach ($license_data as $key => $value){
        $data .= $key.": <strong>".$value."</strong>"."<br />";
    }
    
    return $data;
}

function wpls_sample_ugeturl($type) {

    $license = trim( get_option( 'wpls_sample_license_key' ) );

    $api_params = array(
        'wpls_action' => 'do_upgrade',
        'license' => $license,
        'code' => urlencode( WPLS_SAMPLE_ITEM_CODE ), // the CODE of our product in WPLS
        'url' => home_url(),
        'host' => isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : $_SERVER['LOCAL_ADDR'],
        'type' => $type
    );

    $response = wp_remote_get( add_query_arg( $api_params, WPLS_SAMPLE_STORE_URL ), array( 'timeout' => 15, 'sslverify' => false ) );

    if ( is_wp_error( $response ) )
        return false;

    $license_data = json_decode( wp_remote_retrieve_body( $response ) );
    
    return ($license_data->upgrade_url == false ? '#' : $license_data->upgrade_url);
}

function wpls_sample_cron() {
    if( !wp_next_scheduled( 'wpls_sample_cronhook' ) ) {
        wp_schedule_event( time(), 'twicedaily', 'wpls_sample_cronhook' );
    }
    add_action( 'wpls_sample_cronhook', 'wpls_sample_activate_license' );

    if ( ! wp_next_scheduled( 'wpls_sample_typehook' ) ) {
      wp_schedule_event( time(), 'hourly', 'wpls_sample_typehook' );
    }
    add_action( 'wpls_sample_typehook', 'wpls_sample_update_type' );
}
add_action( 'init', 'wpls_sample_cron');

function wpls_sample_update_type(){
    $license_data = wpls_sample_check_license();
    update_option( 'wpls_sample_license_key_type', $license_data->license_type);
}

Content is Locked

This locked content is for owner Professional and Developer license only.
Please provide your license to DOWNLOAD hidden content
Please wait...
You are successfully validated!
There was an error with provided license.
License validated but license type is does not meet content criteria.

Recent Post