Skip to content

Integrate PluginPass

R.Brown edited this page Sep 18, 2019 · 29 revisions

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

Define PluginPass as a dependency

Following option(-s) can be suggested to achieve this.

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() {

$pluginpass_slug   = 'pluginpass-pro-plugintheme-licensing';
$pluginpass_folder = "$pluginpass_slug/pluginpass.php";

// Require PluginPass plugin
if ( is_admin() && current_user_can( 'activate_plugins' ) && ! is_plugin_active( $pluginpass_folder ) ) {
        // plugin installed but not activated
  if ( array_key_exists( $pluginpass_folder, get_plugins() ) ) {
          $pluginpass_activate_url = wp_nonce_url(
             self_admin_url( 'plugins.php?action=activate&plugin=' . $pluginpass_folder ),
      'activate-plugin_' . $pluginpass_folder
    );

    wp_die( sprintf(
      'This plugin/theme requires PluginPass plugin to be installed and active.<br><br> <a class="button button-primary" href="%s">%s</a>',
      $pluginpass_activate_url,
      'Activate PluginPass now &raquo;'
      )
    );
  }

  $pluginpass_install_url = wp_nonce_url(
    self_admin_url( 'update.php?action=install-plugin&plugin=' . $pluginpass_slug ),
    'install-plugin_' . $pluginpass_slug
  );

  wp_die( sprintf(
          'This plugin/theme requires PluginPass plugin to be installed and active.<br><br> <a class="button button-primary" href="%s">%s</a>',
          $pluginpass_install_url,
          'Install PluginPass now &raquo;'
    )
  );
    }
}

Validate plugin & theme features

The \PluginPass\Inc\Common\PluginPass_Guard class contains methods for performing plugin/theme 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

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 set_consent

To be able to validate plugin or theme installed in customer's WordPress instance, user consent should be given to process personal data. All validate() requests without user consent will always return false. Method set_consent() need to be called only once. After a successful call, user consent will be stored in the database and used for all NetLicensing call.

Examples

<?php

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

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

print_r($quard->validate($model)); // validate() request will not be sent and return false

$quard->set_concent();

print_r($quard->validate($model)); // validate() request will be sent and return plugin/theme validation status

Method has_consent

Method has_consent() returns true if user consent is available and his personal data can be processed.

Returns

  • boolean : true if user consent is available
<?php

  function my_some_function() {

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

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

    if ( ! $quard->has_consent() ) {
      if ( isset( $_GET['has_consent'] ) ) {

        if ( $_GET['has_consent'] === true ) {
          $quard->set_consent();
        } else {
          wp_die( 'User consent must be available to process personal data!' )ж
        }

      } else {
        my_function_get_user_consent();
        exit();
      }
    }

    if ( $quard->validate( $module ) ) {
      // some code
    }
  }

  function my_function_get_user_consent() {
    // show popup or page to obtain user consent
    global $wp;

    $accept_url = esc_url( add_query_arg( array_merge( $wp->query_vars, [ 'has_consent' => true ] ), admin_url( $wp->request ) ) );

    $cancel_url = esc_url( add_query_arg( array_merge( $wp->query_vars, [ 'has_consent' => false ] ), admin_url( $wp->request ) ) );

    return "<a href='$accept_url'>Accept</a> <a href='$cancel_url'>Cancel</a>";
  }

Method validate

Validate plugin or theme module(-s) for the current WordPress instance.

Arguments

  • string $module : The plugin or theme module to be validated.

Returns

  • boolean : Validation status - true if requested feature is available and can be used, otherwise false.

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 = 'PRODUCT_MODULE_NUMBER'; // "PRODUCT_MODULE_NUMBER" is the number of product module

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

Validation for license models:

Note: For this license model $module argument should be provided as a concatenation of the product module number and the license template number to be validated; e.g. PRODUCT_MODULE_NUMBER.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 );

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

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

Note: Following NetLicensing license models aren't tested with PluginPass yet: Pricing Table, Rental, Floating, Node-Locked. Please do not hesitate to provide your use case, which might include the above models. We'll be happy to elaborate on this.

Method validation_result

Return validation result as an array.

Arguments

  • string $module : (optional) The plugin's module validation result. If not provided returns the complete validation result.

Returns

  • array || null : Return validation result array or null if the validation doesn't exist.

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] => Product Module Name
 *           [productModuleNumber] => PRODUCT_MODULE_NUMBER
 *      )
 *
 * )
 */

print_r($quard->validation_result('PRODUCT_MODULE_NUMBER'));

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

Method open_shop

Redirect the user to the NetLicensing Shop for license acquisition.

Arguments

  • string $successUrl : (optional) Forward customer to this URL after successful checkout.
  • string $successUrlTitle : (optional) Link title for successful checkout.
  • string $cancelUrl : (optional) Forward customer to this URL after cancelled checkout.
  • string $cancelUrlTitle : (optional) Link title for cancelled checkout.

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 = 'PRODUCT_MODULE_NUMBER'; // "PRODUCT_MODULE_NUMBER" is the number of product module

if ( !$quard->validate( $feature ) ) {
  // user doesn't have a valid license for product module with the number "PRODUCT_MODULE_NUMBER"

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

Method get_shop_url

Generate shop NetLicensing URL for license acquisition.

Arguments

  • string $successUrl : (optional) Forward customer to this URL after successful checkout.
  • string $successUrlTitle : (optional) Link title for successful checkout.
  • string $cancelUrl : (optional) Forward customer to this URL after cancelled checkout.
  • string $cancelUrlTitle : (optional) Link title for cancelled checkout.

Returns

  • string $url : NetLicensing Shop URL.

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 = 'PRODUCT_MODULE_NUMBER'; // "PRODUCT_MODULE_NUMBER" is the number of product module

if ( !$quard->validate( $feature ) ) {
  // user doesn't have a valid license for product module with the number "PRODUCT_MODULE_NUMBER"

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