Skip to content

Commit

Permalink
Merge pull request #6 from newfold-labs/enhance/activate-premium-plugin
Browse files Browse the repository at this point in the history
Implement License Activation Functionality
  • Loading branch information
arunshenoy99 authored Oct 16, 2024
2 parents dc816af + f40d897 commit 807fb96
Show file tree
Hide file tree
Showing 4 changed files with 473 additions and 58 deletions.
99 changes: 99 additions & 0 deletions includes/Data/Providers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace NewfoldLabs\WP\Module\PLS\Data;

/**
* Class Providers
*
* Handles provider-specific actions for the PLS module.
*/
class Providers {

/**
* Environment for constructing option names.
*
* @var string
*/
private $environment;

/**
* Providers array for option name rules and storage method.
*
* @var array
*/
private $providers;

/**
* Constructor for Providers.
*/
public function __construct() {
// Define the environment using NFD_PLS_ENV.
if ( ! defined( 'NFD_PLS_ENV' ) ) {
define( 'NFD_PLS_ENV', 'production' );
}
$this->environment = constant( 'NFD_PLS_ENV' );

// Initialize provider-specific option rules and storage method.
$this->providers = array(
'yith' => array(
'storage' => array(
'license_id' => "pls_license_id_{$this->environment}_%s",
'activation_key' => "pls_activation_key_{$this->environment}_%s",
'method' => 'wp_option',
),
),
);
}

/**
* Get the option name based on the provider, storage type, and identifier (license_id or activation_key).
*
* @param string $provider The provider name (e.g., "yith").
* @param string $type The type of option name to retrieve ('license_id' or 'activation_key').
* @param string $identifier The identifier to insert into the option name (plugin_slug or license_id).
*
* @return string|false The constructed option name or false if provider/type is not found.
*/
public function get_option_name( $provider, $type, $identifier ) {
if ( isset( $this->providers[ $provider ]['storage'][ $type ] ) ) {
return sprintf( $this->providers[ $provider ]['storage'][ $type ], $identifier );
}

return false; // Provider or type not found.
}

/**
* Get the option name for storing the license ID for a given provider.
*
* @param string $provider The provider name (e.g., "yith").
* @param string $plugin_slug The plugin slug.
*
* @return string|false The option name for storing the license ID.
*/
public function get_license_id_option_name( $provider, $plugin_slug ) {
return $this->get_option_name( $provider, 'license_id', $plugin_slug );
}

/**
* Get the option name for storing the activation key for a given provider.
*
* @param string $provider The provider name (e.g., "yith").
* @param string $license_id The license ID.
*
* @return string|false The option name for storing the activation key.
*/
public function get_activation_key_option_name( $provider, $license_id ) {
return $this->get_option_name( $provider, 'activation_key', $license_id );
}

/**
* Get the storage method for a given provider.
*
* @param string $provider The provider name (e.g., "yith").
*
* @return string|false The storage method for the provider.
*/
public function get_storage_method( $provider ) {
return $this->providers[ $provider ]['storage']['method'] ?? false;
}
}
28 changes: 23 additions & 5 deletions includes/RestApi/Controllers/PLSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ class PLSController extends \WP_REST_Controller {
*/
protected $rest_base = 'license';

/**
* Instance of the PLSUtility class.
*
* @var PLSUtility
*/
protected $pls_utility;

/**
* Constructor for the PLSController class.
*/
public function __construct() {
// Instantiate PLSUtility for license handling
$this->pls_utility = new PLSUtility();
}

/**
* Registers the routes for the objects of the controller.
*/
Expand Down Expand Up @@ -93,7 +108,8 @@ public function get_args() {
public function create_license( $request ) {
$plugin_slug = sanitize_text_field( $request->get_param( 'pluginSlug' ) );

$response = PLSUtility::provision_license( $plugin_slug );
// Use the instance of PLSUtility to provision a new license
$response = $this->pls_utility->provision_license( $plugin_slug );

if ( is_wp_error( $response ) ) {
return new \WP_REST_Response(
Expand All @@ -115,9 +131,10 @@ public function create_license( $request ) {
* @return \WP_REST_Response|\WP_Error
*/
public function get_license_status( $request ) {
$plugin_slug = sanitize_text_field( $request->get_param( 'plugin_slug' ) );
$plugin_slug = sanitize_text_field( $request->get_param( 'pluginSlug' ) );

$license_status = PLSUtility::retrieve_license_status( $plugin_slug );
// Use the instance of PLSUtility to retrieve license status
$license_status = $this->pls_utility->retrieve_license_status( $plugin_slug );
if ( is_wp_error( $license_status ) ) {
return new \WP_REST_Response(
array(
Expand All @@ -138,9 +155,10 @@ public function get_license_status( $request ) {
* @return \WP_REST_Response|WP_Error
*/
public function activate_license( $request ) {
$plugin_slug = sanitize_text_field( $request->get_param( 'plugin_slug' ) );
$plugin_slug = sanitize_text_field( $request->get_param( 'pluginSlug' ) );

$activation_result = PLSUtility::activate_license( $plugin_slug );
// Use the instance of PLSUtility to activate the license
$activation_result = $this->pls_utility->activate_license( $plugin_slug );
if ( is_wp_error( $activation_result ) ) {
return new \WP_REST_Response(
array(
Expand Down
Loading

0 comments on commit 807fb96

Please sign in to comment.