Skip to content

Commit

Permalink
Merge pull request #3 from newfold-labs/solutions
Browse files Browse the repository at this point in the history
Solutions
  • Loading branch information
arunshenoy99 authored Oct 9, 2024
2 parents 3ab3723 + dc816af commit ffa1517
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 16 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"require-dev": {
"newfold-labs/wp-php-standards": "^1.2",
"wp-cli/i18n-command": "^2.4.3"
"wp-cli/i18n-command": "^2.4.3",
"wp-cli/wp-cli": "^2.11"
},
"require": {
"newfold-labs/wp-module-data": "^2.0"
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions includes/PLS.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace NewfoldLabs\WP\Module\PLS;

use NewfoldLabs\WP\Module\PLS\RestApi\RestApi;
use NewfoldLabs\WP\Module\PLS\WPCLI\WPCLI;
use NewfoldLabs\WP\ModuleLoader\Container;

/**
Expand All @@ -28,5 +29,7 @@ public function __construct( Container $container ) {
if ( Permissions::rest_is_authorized_admin() ) {
new RestApi();
}

new WPCLI();
}
}
4 changes: 2 additions & 2 deletions includes/RestApi/Controllers/PLSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function register_routes() {
*/
public function get_args() {
return array(
'plugin_slug' => array(
'pluginSlug' => array(
'required' => true,
'validate_callback' => function ( $param ) {
return is_string( $param ) && ! empty( $param );
Expand All @@ -91,7 +91,7 @@ public function get_args() {
* @return \WP_REST_Response|\WP_Error
*/
public function create_license( $request ) {
$plugin_slug = sanitize_text_field( $request->get_param( 'plugin_slug' ) );
$plugin_slug = sanitize_text_field( $request->get_param( 'pluginSlug' ) );

$response = PLSUtility::provision_license( $plugin_slug );

Expand Down
2 changes: 2 additions & 0 deletions includes/Utilities/HiiveUtility.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace NewfoldLabs\WP\Module\PLS\Utilities;

use NewfoldLabs\WP\Module\Data\HiiveConnection;

/**
Expand Down
52 changes: 45 additions & 7 deletions includes/Utilities/PLSUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,53 @@ class PLSUtility {

/**
* Provisions a new license via the Hiive PLS API using the plugin slug.
* If the license is already stored in the WordPress option, it returns the stored license.
*
* @param string $plugin_slug The plugin slug.
*
* @return array|WP_Error License status or WP_Error on failure.
* @return array|WP_Error License data or WP_Error on failure.
*/
public static function provision_license( $plugin_slug ) {
// TODO: Replace this dummy with an actual API call to Hiive for provisioning a new license.
return array(
'status' => 'new',
// Retrieve the stored license data from the WordPress option.
$option_name = 'nfd_module_pls_license_data_' . $plugin_slug;
$license_data = get_option( $option_name );

// Check if the license data already exists in the options.
// TODO: Update this to store only the license ID in the appropriate plugin option once the Hiive API is ready and can store the download URL as well.
if ( $license_data ) {
return json_decode( $license_data, true );
}

// If no license is found, proceed with an API request to provision a new license.
$endpoint = '/sites/v2/pls/license';
$body = array(
'pluginSlug' => $plugin_slug,
);

$hiive_request = new HiiveUtility( $endpoint, $body, 'POST' );
$response = $hiive_request->send_request();
if ( is_wp_error( $response ) ) {
return $response;
}

$response_body = json_decode( $response, true );
// Check if the response contains the necessary license ID and download URL.
if ( isset( $response_body['license_id'], $response_body['download_url'] ) ) {
$license_data = array(
'licenseId' => $response_body['license_id'],
'downloadUrl' => $response_body['download_url'],
'storageMethod' => 'wp_option',
'storageKey' => $option_name,
);

update_option( $option_name, wp_json_encode( $license_data ) );

return $license_data;
}

return new \WP_Error(
'nfd_pls_error',
__( 'Unexpected response format from the API.', 'wp-module-pls' )
);
}

Expand All @@ -28,10 +66,10 @@ public static function provision_license( $plugin_slug ) {
*
* @param string $plugin_slug The plugin slug.
*
* @return string|WP_Error License status or WP_Error on failure.
* @return string|WP_Error The license status or WP_Error on failure.
*/
public static function retrieve_license_status( $plugin_slug ) {
// TODO: Replace this dummy with an actual API call to Hiive to retrieve the license status.
// TODO: Replace this dummy method with an actual API call to Hiive to retrieve the license status.
$statuses = array( 'active', 'new', 'expired', 'not_generated' );
$random_status = $statuses[ array_rand( $statuses ) ];

Expand All @@ -46,7 +84,7 @@ public static function retrieve_license_status( $plugin_slug ) {
* @return array|WP_Error License status or WP_Error on failure.
*/
public static function activate_license( $plugin_slug ) {
// TODO: Replace this dummy with an actual API call to Hiive to activate the license.
// TODO: Replace this dummy method with an actual API call to Hiive to activate the license.
return array(
'status' => 'active',
);
Expand Down
94 changes: 94 additions & 0 deletions includes/WPCLI/Handlers/PLSCommandHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
namespace NewfoldLabs\WP\Module\PLS\WPCLI\Handlers;

use NewfoldLabs\WP\Module\PLS\Utilities\PLSUtility;
use WP_CLI;

/**
* Class PLSCommandHandler
*
* Handles WP-CLI custom commands for the PLS module, including provisioning
*/
class PLSCommandHandler {

/**
* Provisions a new license for the given plugin.
*
* ## OPTIONS
*
* <plugin_slug>
* : The slug of the plugin for which to provision the license.
*
* ## EXAMPLES
*
* wp pls provision <plugin_slug>
*
* @param array $args Positional arguments, where the first element is the plugin slug.
* @return void
*/
public function provision( $args ) {
$plugin_slug = $args[0];

$result = PLSUtility::provision_license( $plugin_slug );

if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} else {
WP_CLI::success( 'License provisioned: ' . wp_json_encode( $result ) );
}
}

/**
* Retrieves the current license status for the given plugin.
*
* ## OPTIONS
*
* <plugin_slug>
* : The slug of the plugin for which to retrieve the license status.
*
* ## EXAMPLES
*
* wp pls status <plugin_slug>
*
* @param array $args Positional arguments, where the first element is the plugin slug.
* @return void
*/
public function status( $args ) {
$plugin_slug = $args[0];

$result = PLSUtility::retrieve_license_status( $plugin_slug );

if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} else {
WP_CLI::success( "License status: $result" );
}
}

/**
* Activates a license for the given plugin.
*
* ## OPTIONS
*
* <plugin_slug>
* : The slug of the plugin for which to activate the license.
*
* ## EXAMPLES
*
* wp pls activate <plugin_slug>
*
* @param array $args Positional arguments, where the first element is the plugin slug.
* @return void
*/
public function activate( $args ) {
$plugin_slug = $args[0];

$result = PLSUtility::activate_license( $plugin_slug );

if ( is_wp_error( $result ) ) {
WP_CLI::error( $result->get_error_message() );
} else {
WP_CLI::success( 'License activated: ' . wp_json_encode( $result ) );
}
}
}
22 changes: 22 additions & 0 deletions includes/WPCLI/WPCLI.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace NewfoldLabs\WP\Module\PLS\WPCLI;

use NewfoldLabs\WP\Module\PLS\WPCLI\Handlers\PLSCommandHandler;
use WP_CLI;

/**
* Class WPCLI
*
* Handles registering WP-CLI commands for the PLS module.
*/
class WPCLI {
/**
* Constructor for WPCLI class.
*/
public function __construct() {
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_command( 'pls', PLSCommandHandler::class );
}
}
}

0 comments on commit ffa1517

Please sign in to comment.