Introduction
In this article we will show how you can integrate your add-on with the Easy Digital Downloads Software Licensing extension to handle license key activation, deactivation, validation, and enabling access to automatic updates.
Before reading on we do recommend you read the following articles from the EDD documentation:
- Activating License Keys in WP Plugins and Themes
- Deactivating License Keys in WP Plugins and Themes
- Automatic Upgrades for WordPress Plugins
The following examples are based on the Simple Add-On from the Add-On Framework article.
The Constants
Some of the arguments used in the various requests to the EDD Software Licensing API are identical so we are going to store their values in constants.
In our Simple Add-On we have already defined the add-on version number (GF_SIMPLE_ADDON_VERSION) in the root file so we will add our two new constants there.
define( 'GF_SIMPLE_ADDON_VERSION', '2.0' );
define( 'GF_SIMPLE_ADDON_EDD_SL_STORE_URL', 'http://yoursite.com' );
define( 'GF_SIMPLE_ADDON_EDD_SL_ITEM_NAME', 'Simple Add-On' );
GF_SIMPLE_ADDON_EDD_SL_STORE_URL is the URL of the site where you have installed the Easy Digital Downloads plugin and Software Licensing extension.
GF_SIMPLE_ADDON_EDD_SL_ITEM_NAME is the name of the product as you have configured it in EDD.
The License Key Setting
The plugin_settings_fields() function of our example add-on had a simple text field named mytextbox. In the following example we have replaced that field with a new text field named license_key which will use a password type input to capture the license key.
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @return array
*/
public function plugin_settings_fields() {
return array(
array(
'title' => esc_html__( 'Simple Add-On Settings', 'simpleaddon' ),
'fields' => array(
array(
'name' => 'license_key',
'label' => esc_html__( 'License Key', 'simpleaddon' ),
'type' => 'text',
'input_type' => 'password',
'validation_callback' => array( $this, 'license_validation' ),
'feedback_callback' => array( $this, 'license_feedback' ),
'error_message' => esc_html__( 'Invalid license', 'simpleaddon' ),
'class' => 'large',
'default_value' => '',
),
),
),
);
}
Review the Settings API documentation for more details about how to define sections of fields.
Validating the License Key
The example below shows the function assigned to the feedback_callback property of the license_key field. This function runs when the markup for the text field is being prepared on page display so the validation check can determine which icon is displayed next to the field.
/**
* Determine if the license key is valid so the appropriate icon can be displayed next to the field.
*
* @param string $value The current value of the license_key field.
* @param array $field The field properties.
*
* @return bool|null
*/
public function license_feedback( $value, $field ) {
if ( empty( $value ) ) {
return null;
}
// Send the remote request to check the license is valid
$license_data = $this->perform_edd_license_request( 'check_license', $value );
$valid = null;
if ( empty( $license_data ) || $license_data->license == 'invalid' ) {
$valid = false;
} elseif ( $license_data->license == 'valid' ) {
$valid = true;
}
return $valid;
}
Activating and Deactivating the License Key
The example below shows the function assigned to the validation_callback property of the license_key field. This function runs when the plugin settings are saved. It will deactivate the old license key if the posted value doesn’t match the value found in the existing settings. It will also activate the new license key.
/**
* Handle license key activation or deactivation.
*
* @param array $field The field properties.
* @param string $field_setting The submitted value of the license_key field.
*/
public function license_validation( $field, $field_setting ) {
$old_license = $this->get_plugin_setting( 'license_key' );
if ( $old_license && $field_setting != $old_license ) {
// Send the remote request to deactivate the old license
$this->perform_edd_license_request( 'deactivate_license', $old_license );
}
if ( ! empty( $field_setting ) ) {
// Send the remote request to activate the new license
$this->perform_edd_license_request( 'activate_license', $field_setting );
}
}
Perform the Remote Request
The following example shows the helper being used by the license_feedback() and license_validation() functions to send the license key validation, activation, and deactivation requests to your EDD store url.
/**
* Send a request to the EDD store url.
*
* @param string $edd_action The action to perform (check_license, activate_license, or deactivate_license).
* @param string $license The license key.
*
* @return object
*/
public function perform_edd_license_request( $edd_action, $license ) {
// Prepare the request arguments
$args = array(
'timeout' => 10,
'sslverify' => false,
'body' => array(
'edd_action' => $edd_action,
'license' => trim( $license ),
'item_name' => urlencode( GF_SIMPLE_ADDON_EDD_SL_ITEM_NAME ),
'url' => home_url(),
),
);
// Send the remote request
$response = wp_remote_post( GF_SIMPLE_ADDON_EDD_SL_STORE_URL, $args );
return json_decode( wp_remote_retrieve_body( $response ) );
}
Plugin Updates
Adding support for automatic updates is very simple. If you have read the EDD documentation on the subject you will know that the EDD Software Licensing plugin includes a sample plugin containing a file named EDD_SL_Plugin_Updater.php. You will want to include a copy of this file with your add-on.
Next, return to your add-ons root file and add the following to the end of the file.
add_action( 'init', 'gf_simple_addon_edd_plugin_updater', 0 );
function gf_simple_addon_edd_plugin_updater() {
if ( ! class_exists( 'EDD_SL_Plugin_Updater' ) ) {
// load our custom updater if it doesn't already exist
include( dirname( __FILE__ ) . '/EDD_SL_Plugin_Updater.php' );
}
// retrieve the license key
$license_key = trim( gf_simple_addon()->get_plugin_setting( 'license_key' ) );
// setup the updater
$edd_updater = new EDD_SL_Plugin_Updater( GF_SIMPLE_ADDON_EDD_SL_STORE_URL, __FILE__, array(
'version' => GF_SIMPLE_ADDON_VERSION,
'license' => $license_key,
'item_name' => GF_SIMPLE_ADDON_EDD_SL_ITEM_NAME,
'author' => 'John Doe'
)
);
}
That’s all there is to it! As long as the license key entered on the plugin settings page is valid and active, the add-on will have access to updates.
Important
The GFAddon class includes a protected variable named $_enable_rg_autoupgrade which is set to false.
The $_enable_rg_autoupgrade variable is not intended for use in third-party add-ons. It is only used by add-ons developed by Rocketgenius, Inc. to include the class-gf-auto-upgrade.php file which handles integrating the add-ons with our licensing and updates system.
All other add-ons should leave the $_enable_rg_autoupgrade variable set to false.