Skip to content

Integrate PluginPass

R.Brown edited this page Aug 31, 2019 · 29 revisions

To be able to use PluginPass functions you need to define plugin as a dependency to your plugin and/or theme.

Define PluginPass as a dependency

Following options can be suggested to achieve this.

Method 1 - Using register_activation_hook

Add the following code snippet to the activation hook of your plugin:

<?php
register_activation_hook( __FILE__, 'my_plugin_activate' );

function my_plugin_activate() {

    // Require PluginPass plugin
    if ( is_admin() && current_user_can( 'activate_plugins' ) &&  !is_plugin_active( 'pluginpass-pro-plugintheme-licensing/pluginpass.php' ) ) {
        // Stop activation redirect and show error
        wp_die('This plugin/theme requires PluginPass plugin to be installed and active. <br><a href="' . admin_url( 'plugins.php' ) . '">Install PluginPass now &raquo;</a>');
    }

}

Method 2 - Using admin_init and admin_notices

Add admin area action with the following code snippet:

<?php
add_action( 'admin_init', 'my_plugin_check_pluginpass' );

function my_plugin_check_pluginpass() {
    if ( is_admin() && current_user_can( 'activate_plugins' ) &&  !is_plugin_active( 'pluginpass-pro-plugintheme-licensing/pluginpass.php' ) ) {
        add_action( 'admin_notices', 'my_plugin_notice' );

        deactivate_plugins( plugin_basename( __FILE__ ) ); 

        if ( isset( $_GET['activate'] ) ) {
            unset( $_GET['activate'] );
        }
    }
}

function my_plugin_notice() {
    ?><div class="error"><p>This plugin/theme requires PluginPass plugin to be installed and active</p></div>
<?php
}

Validate plugin & theme features

The \PluginPass\Inc\Common\PluginPass_Guard class contains methods for performing plugin/teme validation as well as other useful functions to work with the NetLicensing RESTful API.

Constructor arguments

  • string $api_key : NetLicensing API Key (API Key with role Licensee is recommended)
  • string $product_number : NetLicensing product number
  • string $plugin_folder : Relative path to your plugin folder
  • boolean $has_consent : (optional) False by default. Indicate whether you have the user's consent to use his personal data and send it to NetLicensing.

Examples

<?php

$api_key        = 'My NetLicensing API Key';
$product_number = 'My Product Number';
$plugin_folder  = 'my-plugin/my-plugin.php';

$quard = new \PluginPass\Inc\Common\PluginPass_Guard( $api_key, $product_number, $plugin_folder );

Method validate

Validate plugin feature(-s) for the current WordPress instance.

Arguments

  • string $feature : The plugin feature to be checked, equal NetLicensing "product module number", or if you use feature license model equal "product module number.license template number".

Returns

  • boolean : The status, whether this feature is available.

Examples

Validation for license models:

<?php

$api_key        = 'My NetLicensing API Key';
$product_number = 'My Product Number';
$plugin_folder  = 'my-plugin/my-plugin.php';

$quard = new \PluginPass\Inc\Common\PluginPass_Guard( $api_key, $product_number, $plugin_folder );

$feature = 'my_product_module_number'; // "my_product_module_number" is number of product module

if ( $quard->validate( $feature ) ) {
	// user has had a valid license for product module with number "my_product_module_number"
	// ...
} 

License Models Validation:

In this case $feature argument will contain the product module number and the license template number joined by a dot, example "my_product_module_number.my_license_template_number".

Examples

<?php

$api_key        = 'My NetLicensing API Key';
$product_number = 'My Product Number';
$plugin_folder  = 'my-plugin/my-plugin.php';

$quard = new \PluginPass\Inc\Common\PluginPass_Guard( $api_key, $product_number, $plugin_folder );

// let's imagine that we have product module number: "articles"
// and several license template numbers: "create", "edit", "delete"


if ( $quard->validate( 'articles.create' ) ) {
	// user has had a valid license for product module with number "articles" and license template with the number "create"
	// ...
}

if ( $quard->validate( 'articles.edit' ) ) {
	// user has had a valid license for product module with number "articles" and license template with the number "edit"
	// ...
}

if ( $quard->validate( 'articles.delete' ) ) {
	// user has had a valid license for product module with number "articles" and license template with the number "delete"
	// ...
}

Method validation_result

Return array of the validation result

Arguments

  • string $feature : The plugin feature to return result (optional), if doesn't set returns the entire validation result.

Returns

  • array || null : Return array of validation result or null if validation doen't exists.

Examples

<?php

$api_key        = 'My NetLicensing API Key';
$product_number = 'My Product Number';
$plugin_folder  = 'my-plugin/my-plugin.php';

$quard = new \PluginPass\Inc\Common\PluginPass_Guard( $api_key, $product_number, $plugin_folder );

print_r($quard->validation_result());

/*
 *  Array
 * (
 *   [Some Product Module Number] => Array
 *       (
 *           [valid] => true
 *           [licensingModel] => Subscription
 *           [expires] => 2019-08-19T19:44:00.224Z
 *           [productModuleName] => Some Product Module Name
 *           [productModuleNumber] => Some Product Module Number
 *      )
 *
 * )
 */

print_r($quard->validation_result('Some Product Module Number'));

/*
 *  Array
 * (
 *   [valid] => true
 *   [licensingModel] => Subscription
 *   [expires] => 2019-08-19T19:44:00.224Z
 *   [productModuleName] => Some Product Module Name
 *   [productModuleNumber] => Some Product Module Number
 * )
 */

Method open_shop

Redirect the user to the Shop URL for license acquisition.

Arguments

  • string $successUrl : (optional) Take customers to this URL when they finish checkout.
  • string $successUrlTitle : (optional) Shop link title for successful checkout process.
  • string $cancelUrl : (optional) Take customers to this URL when they cancel their checkout.
  • string $cancelUrlTitle : (optional) Shop link title for cancel checkout process.

Examples

<?php

$api_key        = 'My NetLicensing API Key';
$product_number = 'My Product Number';
$plugin_folder  = 'my-plugin/my-plugin.php';

$quard = new \PluginPass\Inc\Common\PluginPass_Guard( $api_key, $product_number, $plugin_folder );

$feature = 'my_product_module_number'; // "my_product_module_number" is number of product module

if ( !$quard->validate( $feature ) ) {
	// user hasn't had a valid license for product module with number "my_product_module_number"

	// open shop
	$quard->open_shop('http://my-domain.com/success', 'Back to site', 'http://my-domain.com/cancel', 'Cancel and Back to site');
}

Method get_shop_url

Generate shop URL for license acquisition.

Arguments

  • string $successUrl : (optional) Take customers to this URL when they finish checkout.
  • string $successUrlTitle : (optional) Shop link title for successful checkout process.
  • string $cancelUrl : (optional) Take customers to this URL when they cancel their checkout.
  • string $cancelUrlTitle : (optional) Shop link title for cancel checkout process.

Examples

<?php

$api_key        = 'My NetLicensing API Key';
$product_number = 'My Product Number';
$plugin_folder  = 'my-plugin/my-plugin.php';

$quard = new \PluginPass\Inc\Common\PluginPass_Guard( $api_key, $product_number, $plugin_folder );

$feature = 'my_product_module_number'; // "my_product_module_number" is number of product module

if ( !$quard->validate( $feature ) ) {
	// user hasn't had a valid license for product module with number "my_product_module_number"

	// get shop url
	echo $quard->get_shop_url(); // https://go.netlicensing.io/shop/v2/?shoptoken=686f5554-5978-4a68-a7f0-0b4996e8826c 
}
Clone this wiki locally