PermitPlane Docs
All docs pages

Add licensing to a PHP or WordPress product

At the end, your plugin, theme or PHP app activates a licence key and unlocks its features offline.

From your product page in PermitPlane (app.permitplane.com/ops/vendor/), click Download permitplane.json and Create test key.

1. Install

You need PHP 8.1+.

Download permitplane-php-0.1.0.zip and compare its sha256 with manifest.json. It is pure PHP and needs only the openssl and mbstring extensions. Add it to your project as a Composer path repository:

Shell
sha256sum permitplane-php-0.1.0.zip
unzip permitplane-php-0.1.0.zip -d lib
composer config repositories.pp path lib/permitplane-php-0.1.0
composer require "permitplane/client:*@dev"

2. Activate a licence key

Load the config, create the client and activate once:

PHP
<?php
require __DIR__ . '/vendor/autoload.php';

use PermitPlane\Client\{Client, Config, EcKeyStore, JsonStateStore};

$json = file_get_contents(__DIR__ . '/permitplane.json');
$client = new Client(
    Config::fromJson($json),
    EcKeyStore::file('/var/lib/acme/license-key.json'),
    JsonStateStore::file('/var/lib/acme/license-state.json'),
);
$client->initialize();  // verifies the saved grant offline

if ($client->status() === 'free') {
    // paste your test key here; in your real app, ask the user for it
    $client->activateWithKey(
        'PP-7KQ4M-X9T2H-B6WRN-3FJ8D',
        'acme.example.com'
    );
}

3. Unlock features in your app

Turn parts of your app on or off based on what the customer's licence includes. The check reads the signed grant and works offline up to your policy's offline days.

PHP
if ($client->hasFeature('export')) {
    enableExport();
} else {
    showUpgradeOffer();
}
PHP
$sites = $client->getLimit('sites');
// ['unlimited' => false, 'value' => 5]

// free, trial, paid, online_required, expired,
// recovery_required or seat_unavailable
$status = $client->status();

4. Keep it current

  • Call $client->refresh() daily from cron or WP-Cron, or hourly on hosts without a boot id.
  • Call $client->persistObservations() at the end of a long-running request.
  • Call $client->release() to free the device.

Errors

ErrorWhat to do
InvalidLicenseKeyErrorThe key is mistyped. Ask the user to check it.
ApiFailure with apiCode invalid_license_keyThe key is unknown or revoked. Ask for the key from the purchase email.
ApiFailure with apiCode device_limitEvery device slot is taken. Release another site first.
ApiFailure with apiCode license_expired, license_suspended or license_revokedShow a renew or contact link.
TransportErrorThe network failed. Retry later. Unlocked features keep working.
PHP
try {
    $client->refresh();
} catch (\PermitPlane\Client\ApiFailure $e) {
    error_log("licence: {$e->apiCode} {$e->status}");
}

Going further

Reference

How the client works

  • No FFI, no native library and no extension beyond openssl and mbstring. It runs on shared hosting.
  • It uses curl when present and PHP streams otherwise.
  • Requests are https only, with no redirects and a 1 MiB response cap.
  • It verifies the signed grant itself, with RS256 through openssl_verify.
  • The public key comes from your permitplane.json.
  • It enforces the grant claims, the clock checks and the 21-day offline cap.
  • Without Composer, unzip the package and add its src/ to your autoloader.
  • Downloads need no sign-in. On Windows, check a file with Get-FileHash -Algorithm SHA256 <file>.
  • Ship permitplane.json with your code. It holds no secrets.

Storage

  • Storage sits behind small interfaces: KeyStore, StateStore and Blob.
  • The state holds a bearer installation token.
  • Keep the files outside the web root, with mode 0600.
  • You can also use a database or an option store.

WordPress

Store the key and state in wp_options with a small Blob. Check the feature on admin_init.

PHP
final class Acme_Option_Blob implements \PermitPlane\Client\Blob
{
    public function __construct(private string $name) {}

    public function read(): ?string
    {
        $v = get_option($this->name, null);
        return is_string($v) ? $v : null;
    }

    public function write(string $data): void
    {
        update_option($this->name, $data, false);
    }

    public function delete(): void
    {
        delete_option($this->name);
    }
}

$client = new Client(
    Config::fromJson(ACME_PUBLIC_CONFIG_JSON),
    new EcKeyStore(new Acme_Option_Blob('acme_license_key')),
    new JsonStateStore(new Acme_Option_Blob('acme_license_state')),
);
add_action('admin_init', function () use ($client) {
    $client->initialize();
    if (!$client->hasFeature('export')) {
        // show the upgrade notice
    }
});
  • The full example is sdk/php/examples/wordpress.php in the package.
  • It adds a daily refresh through a transient and an activate() for your settings form.
  • Any plugin can read an option with get_option(). So can anyone with database access.
  • That exposes the installation key and token to all of them.
  • Where you can, use a file outside the web root instead.
  • Use EcKeyStore::file() and JsonStateStore::file() with a path above ABSPATH.

Error classes

ClassMeaning
InvalidLicenseKeyErrorThe key is not a valid licence key.
ApiFailureThe service refused. Read apiCode, status and retryAfter.
SeatsExhaustedErrorEvery floating seat is in use.
LeaseRequiredErrorCheck out a seat first.
TransportErrorThe network failed.

The client never stores, logs or puts the licence key in a message.

Floating seats

  • checkoutSeat(), renewSeat() and releaseSeat() handle floating seats.
  • A PHP request has no timer.
  • Renew an online seat at about half its lease time.

Offline files

  • createOfflineRequest('activate') returns a request file for a site without outbound access.
  • importOfflineResponse($text) verifies the vendor's response file and stores the grant.
  • Deactivation requests work the same way.

Hosts without a boot id

  • The default clock reads /proc/sys/kernel/random/boot_id on Linux.
  • Where it cannot read it (open_basedir, Windows, macOS), it cannot tell a reboot from a clock change.
  • It then trusts the server time from each activate or refresh.
  • Cached feature checks work for one hour after the current grant was issued, never past its exp.
  • Change that window with online_only_window_seconds in the config (60 to 86400).
  • A wall clock set back is refused.
  • Offline imports and borrowed seats answer online_required.
  • Refresh at least hourly. WP-Cron hourly is enough.
  • Pass your own Clock if your host gives you another boot identity.

Limits

  • No machine-wide silent activation. The key and state belong to one site and one web-server user.
  • There is no installer or service identity and no activateSilently.
  • The site owner enters the key, or your code activates once from a settings screen or deploy step.
  • Without a boot id, a server admin can roll back both the clock and a saved state.
  • That bypasses local checks until the grant expires. It is inherent to such hosts.
  • The clock anchor in the state file is not authenticated, as in the other clients.
  • A customer who edits the state file can lose their own grant, not extend it.
  • The signed exp and the 21-day cap still bind.
  • Two requests that refresh at once may both write the state. The last write wins, and both are valid.