Skip to content

License Validation

Validate and manage licenses using the PackEdge SDK.

Installation

bash
composer require packedge-sdk/license

Initialize

php
use PackEdge\License;

$license = License::init('pk_your_public_key', __FILE__);

The SDK:

  • Stores license in WordPress options
  • Provides REST endpoint for saving from JS
  • Localizes data for admin scripts

Check License Status

php
if ($license->is_valid()) {
    // License is valid and active
    // Enable premium features
}

The is_valid() method checks:

  • License key exists
  • Status is active
  • Not expired (if expiration date set)

Get License Key

php
$key = $license->get_key();

if ($key) {
    echo "Licensed: " . $key;
}

Enable Auto Updates

php
// One line to enable WordPress auto-updates
$license->updater();

See Auto Updates for details.

Complete Example

php
<?php
/**
 * Plugin Name: My Plugin
 * Version: 1.0.0
 */

require_once plugin_dir_path(__FILE__) . 'vendor/autoload.php';

use PackEdge\License;

// Initialize (singleton)
$license = License::init('pk_your_public_key', __FILE__);

// Enable auto-updates
$license->updater();

// Gate premium features
add_action('init', function() use ($license) {
    if ($license->is_valid()) {
        // Load premium features
        require_once __DIR__ . '/includes/premium.php';
    }
});

// Admin notice for unlicensed users
add_action('admin_notices', function() use ($license) {
    if ($license->is_valid()) return;

    echo '<div class="notice notice-warning"><p>';
    echo 'My Plugin requires a license for premium features. ';
    echo '<a href="' . admin_url('options-general.php?page=my-plugin') . '">Enter your license key</a>';
    echo '</p></div>';
});

JavaScript Integration

The SDK automatically localizes license data for your admin scripts:

javascript
// Available in admin pages
console.log(PackEdgeLicense);
// {
//   restUrl: "https://example.com/wp-json/packedge/v1/license",
//   nonce: "abc123",
//   license: { key: "...", status: "active", expires_at: "..." }
// }

// Save license from JS
fetch(PackEdgeLicense.restUrl, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
        'X-WP-Nonce': PackEdgeLicense.nonce,
    },
    body: JSON.stringify({ license_key: 'NEW-LICENSE-KEY' }),
});

Manual REST API

If not using the SDK, call the API directly:

php
$response = wp_remote_post('https://api.packedge.dev/public/v1/licenses/validate', [
    'body' => json_encode([
        'public_key' => 'pk_your_public_key',
        'license_key' => $license_key,
        'domain' => home_url(),
    ]),
    'headers' => ['Content-Type' => 'application/json'],
]);

$body = json_decode(wp_remote_retrieve_body($response), true);

if ($body['valid']) {
    update_option('my_plugin_license', $body['license']);
}