Skip to content

Helper Methods

Ashley Gibson edited this page Jul 13, 2021 · 1 revision

Retrieving an instance of your product

These helper methods will return an \EDD_SL_SDK\Models\Product class instance.

Retrieve a plugin

\EDD_SL_SDK\Helpers\Product::getPlugin( $pluginFile )

The $pluginFile parameter is the path to the main plugin file (__FILE__).

Retrieve a theme

\EDD_SL_SDK\Helpers\Product::getTheme( $themeSlug )

The $themeSlug parameter is the slug of your theme.

Product object

The above methods both return an instance of \EDD_SL_SDK\Models\Product. There are public properties for all settings that were passed in when registering your product. There are also the following methods:

  • getLicense(): string|false - Retrieves the license key.
  • setLicense( $newLicenseKey): void - Sets a new license key.
  • activateLicense(): \EDD_SL_SDK\Models\License - Activates the product's license key. Note that the license key must already be saved to the product (setLicense()). Returns a License object.
  • deactivateLicense(): \EDD_SL_SDK\Models\License - Deactivates the product's license key. This deactivates the license that's already saved to the product. Returns a License object.
  • licenseIsActivated(): bool - Determines whether or not a license key has been activated. Note this does not determine if the license is actually valid (not expired), just if it's been successfully activated remotely.
  • getLicenseData(): \EDD_SL_SDK\Models\License - Retrieves the License object.

License object

The \EDD_SL_SDK\Models\License class is used to represent license data retrieved from the API. It contains public properties for:

  • int $id - ID of the license.
  • string $license_key - License key.
  • bool $activated - Whether or not the license is activated on this site.
  • string $status - Status of the license.
  • int $download_id - ID of the product this license is for.
  • int $price_id - Product price ID.
  • int $payment_id - ID of the payment used to create this license.
  • string $date_created - Date the license key was first created, in Y-m-d H:i:s format.
  • string|null $expiration - Date the license key expires (Y-m-d H:i:s), or null if it never expires.
  • int $number_activations - Number of times the license has been activated.
  • int|string $activation_limit - Maximum activations allowed, or unlimited.
  • int $activations_remaining - Number of activations remaining, or 0 if there is no limit.
  • string $last_sync - Date in Y-m-d H:i:s that the license data was last updated.

Examples

Setting and activating a product license key

First you need to retrieve the product. For a plugin that can be done like this:

$product = \EDD_SL_SDK\Helpers\Product::getPlugin( $pluginFile );

For a theme, like this:

// `get_stylesheet()` returns the slug of the currently installed child theme, or theme.
$product = \EDD_SL_SDK\Helpers\Product::getTheme( get_stylesheet() );

Then activation can be done like this:

try {
    $product->setLicense( $newLicenseKeyToSet );
    $product->activateLicense();
} catch ( \Exception $e ) {
    // Optionally do something with errors.
}

Deactivating a product license key

try {
    $product = \EDD_SL_SDK\Helpers\Product::getPlugin( $pluginFile );
    $product->deactivateLicense();
    $product->setLicense( null ); // This deletes the license key from options.
} catch ( \Exception $e ) {
    // Optionally do something with errors.
}
Clone this wiki locally