Plugin Integration
To integrate the client library to your plugin, you need to first include the client library to your project. You can do so by downloading the library from the github repository or by installing it using the composer dependency manager.
Run the following command to install using composer
composer require digitalduz/slswc-client
This will install the latest stable version into your project’s dependencies.
Integration
The client library provides an easy way to integrate the updates server for plugins. You can use the Plugin class and get the instance object to interact with
$license_server_url = 'http://example.com'; $license_details = array( 'license_key' => 'LICENSE_KEY', // Required 'domain' => 'THE_CURRENT_DOMAIN', // Optional: will default to WordPress site url. 'slug' => 'plugin-slug', // optional. Will use plugin text domain. Must be the product slug on license server. ); $plugin = Plugin::get_instance( $license_server_url, __FILE__, $license_details ); $plugin->init_hooks();
$license_server_url – This is the domain of the license server.
__FILE__ – Is the plugin’s main file, you can use a path to the plugin’s main file if you are initializing the client in another file.
$license_details – This is an array of key-value pairs required for the license server to activate the license. At the bare minimum, you need the license_key and the other details will be extracted from the plugin’s headers.
The $license_details array expects the following array keys:
- license_key – The license key from the license server. This can be stored and retrieved from anywhere, like in wp_options table or .env file
- domain – The current domain where the plugin is being installed or activated. The client library will assume the WordPress site URL is the domain name if no domain is given.
- slug – The plugin’s unique name. This must also be the product slug on the license server.
- version – The current version of the plugin which is being activated. This will be used to check for updates. If this is not provided, the Version from the plugin header will be used.
Activating the license
This is an example of how to activate the license. You can hook this to a form save after saving the license key.
// Example of how to update the plugin. Run this on a hook.
if ( $plugin->license->get_license_status() !== 'active' ) {
$plugin->license->validate_license();
}
Full example
Here is an example showing the integration of the client script to a plugin.
/**
* Plugin Name : Test Plugin Name
* Plugin URI : https://example.com/plugins/plugin-name
* Description : Basic WordPress plugin to test Software License Server for WooCommerce
* Text Domain : text_domain
* Author URI : https://example.com
* License : https://www.gnu.org/licenses/gpl-2.0.html
* Version : 1.0.0
* Author : Author Name
* Domain Path : /languages
* SLSWC : plugin
* SLSWC Documentation URL: https://www.gnu.org/licenses/gpl-2.0.html
* Required WP : 5.8
* Compatible To: 5.8.1
*/
require __DIR__ . '/vendor/autoload.php';
use Digitalduz\Slswc\Client\Plugin;
function test_slswc_client_for_plugin() {
$license_server_url = 'http://example.com';
$license_details = array(
'license_key' => 'LICENSE_KEY', // Required
'domain' => 'THE_CURRENT_DOMAIN', // Optional: will default to WordPress site url.
'slug' => 'plugin-slug', // optional. Will use plugin text domain. Must be the product slug on license server.
);
$plugin = Plugin::get_instance( $license_server_url, __FILE__, $license_details );
$plugin->init_hooks();
// Example of how to update the plugin. Run this on a hook.
if ( $plugin->license->get_license_status() !== 'active' ) {
$plugin->license->validate_license();
}
}
add_action( 'plugins_loaded', 'test_slswc_client_for_plugin', 11 );