1. Home
  2. Docs
  3. For Developers
  4. License Package Integration

License Package Integration

Unlike other plugins available in the market, we don’t follow the “license key” approach where users can input the license key in a form and click an “Activate” button to activate the license.

Instead, we ask them to click on a link/button that takes them to the Pluggable Dashboard which shows an activation button along with their license data like Expiry and the Site Limits.

After clicking the button, it’ll take them back to their site and the license will be automatically activated.

This ensures no one can share their license keys with others.

Adding the package

1. Go to your plugin’s directory (e.g. /wp-content/plugins/my-plugin) and install the package by running this command-

composer require pluggable/plugin

2. Include the autoloader in your main plugin file

require_once 'vendor/autoload.php';

Instantiate License class

To use the license in your plugin, you need to instantiate the License class.

use Pluggable\Plugin\License;

add_action( 'plugins_loaded', 'my_license_init' );
function my_license_init() {
    global $my_license;

    $my_license = new License( __FILE__ );
}

Here, __FILE__ is the main plugin file reference.

Checking the license status

So, now we have the $my_license the instance that we created earlier. This license class comes with a smart status checker that can tell you whether the license is activated on this site or not. Use _is_activated() the method to check the status. Example:

global $my_license;

if( $my_license->_is_activated() ) {
    _e( 'Your license is activated. Enjoy premium features.', 'my-textdomain' );
}
else {
    _e( 'Please activate your license to enjoy premium features.', 'my-textdomain' );
}

License activation

To get the activation URL, use the get_activation_url() method.

global $my_license;

printf(
    '<a href="%1$s" class="button button-primary">%2$s</a>',
    $my_license->get_activation_url(),
    __( 'Activate', 'my-textdomain' )
);

Maybe you want to enclose it with the _is_activated() method so that the users don’t see the button after activating the license. Something like this-

global $my_license;

if( ! $my_license->_is_activated() ) {
    printf(
        '<a href="%1$s" class="button button-primary">%2$s</a>',
        $my_license->get_activation_url(),
        __( 'Activate', 'my-textdomain' )
    );
}

License deactivation

Sometimes users may want to deactivate their license. For example, they may want to use the license on another site. To do so, they will have to deactivate the license from the existing site and then activate it on the new one (unless they have a bigger license plan).

To allow them to do this, you need to add a license deactivation. Here’s the code for this-

global $my_license;

printf(
    '<a href="%1$s" class="button">%2$s</a>',
    $my_license->get_deactivation_url(),
    __( 'Deactivate', 'my-textdomain' )
);

Again, let’s enclose this within the status checker-

global $my_license;

if( $my_license->_is_activated() ) {
    printf(
        '<a href="%1$s" class="button">%2$s</a>',
        $my_license->get_deactivation_url(),
        __( 'Deactivate', 'my-textdomain' )
    );
}

It’s that easy.

Some useful methods-

  1. _is_activated()  checks if the license is activated.
  2. get_activation_url() retrieves the license activation URL.
  3. get_deactivation_url() retrieves the license deactivation URL.
  4. get_license_key() retrieves the license key when it’s activated.
  5. activation_form() displays the license activation form consisting of some text and a button.
  6. get_renewal_url() gets the direct link to renew the license.
  7. get_license_status() gets the status of the license.
  8. get_license_expiry() gets the time when the license is going to expire.

Sample plugin: https://github.com/pluggableio/sample-plugin

Tags , , ,

How can we help?