-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add possibility to force sync the WPCOM license (#2812)
Co-authored-by: Peter Kiss <[email protected]> Co-authored-by: gikaragia <[email protected]>
- Loading branch information
1 parent
ad9a9dc
commit 84006d9
Showing
5 changed files
with
281 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
/** | ||
* File containing the class WP_Job_Manager_Helper_Nonce. | ||
* | ||
* @package wp-job-manager | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* WP_Job_Manager_Helper_Nonce | ||
*/ | ||
class WP_Job_Manager_Helper_Nonce { | ||
|
||
/** | ||
* The prefix for the custom nonce. | ||
*/ | ||
private const PREFIX_NONCE = 'wpjm-custom-nonce-'; | ||
|
||
/** | ||
* The default expiration time for the custom nonce. | ||
*/ | ||
private const DEFAULT_EXPIRE = MINUTE_IN_SECONDS; | ||
|
||
/** | ||
* Creates a custom nonce for the given plugin slug. | ||
* | ||
* @param string $action The action name. | ||
* | ||
* @return string The custom nonce. | ||
*/ | ||
public function create_custom_nonce( $action ) { | ||
$custom_nonce = wp_generate_password( 15, false ); | ||
set_transient( self::PREFIX_NONCE . $action, $custom_nonce, self::DEFAULT_EXPIRE ); | ||
|
||
return $custom_nonce; | ||
} | ||
|
||
/** | ||
* Checks if the given nonce is valid for the given action. | ||
* | ||
* @param string $nonce The nonce to check. | ||
* @param string $action The action name. | ||
* | ||
* @return bool True if the nonce is valid, false otherwise. | ||
*/ | ||
public function check_custom_nonce( $nonce, $action ) { | ||
$saved_nonce = get_transient( self::PREFIX_NONCE . $action ); | ||
|
||
return ! empty( $saved_nonce ) && hash_equals( $saved_nonce, $nonce ); | ||
} | ||
} |
139 changes: 139 additions & 0 deletions
139
includes/helper/class-wp-job-manager-helper-rest-api.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
/** | ||
* File containing the class WP_Job_Manager_Helper_REST_API. | ||
* | ||
* @package wp-job-manager | ||
*/ | ||
|
||
if ( ! defined( 'ABSPATH' ) ) { | ||
exit; | ||
} | ||
|
||
/** | ||
* WP_Job_Manager_Helper_REST_API | ||
*/ | ||
class WP_Job_Manager_Helper_REST_API { | ||
|
||
/** | ||
* The nonce helper to validate the request. | ||
* | ||
* @var WP_Job_Manager_Helper_Nonce | ||
*/ | ||
private WP_Job_Manager_Helper_Nonce $nonce; | ||
|
||
/** | ||
* The namespace. | ||
* | ||
* @var string | ||
*/ | ||
private const NAMESPACE = 'wpjm-internal/v1'; | ||
|
||
/** | ||
* Rest base for the current object. | ||
* | ||
* @var string | ||
*/ | ||
private const REST_BASE = '/licensing'; | ||
|
||
/** | ||
* Construct the REST API class. | ||
* | ||
* @param WP_Job_Manager_Helper_Nonce $nonce | ||
*/ | ||
public function __construct( WP_Job_Manager_Helper_Nonce $nonce ) { | ||
$this->nonce = $nonce; | ||
} | ||
|
||
/** | ||
* Initialize the hooks for the REST API. | ||
* | ||
* @return void | ||
*/ | ||
public function init() { | ||
add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); | ||
} | ||
|
||
/** | ||
* Register the REST routes related to licensing. | ||
* | ||
* @return void | ||
*/ | ||
public function register_rest_routes() { | ||
register_rest_route( | ||
self::NAMESPACE, | ||
self::REST_BASE . '/receive-wpcom-license-key', | ||
[ | ||
[ | ||
'methods' => \WP_REST_Server::CREATABLE, | ||
'callback' => [ $this, 'receive_wpcom_license_key' ], | ||
'permission_callback' => '__return_true', | ||
'args' => [ | ||
'plugin_slug' => [ | ||
'type' => 'string', | ||
'required' => true, | ||
], | ||
'license_key' => [ | ||
'type' => 'string', | ||
'required' => true, | ||
], | ||
'custom_nonce' => [ | ||
'type' => 'string', | ||
'required' => true, | ||
], | ||
], | ||
], | ||
] | ||
); | ||
} | ||
|
||
|
||
/** | ||
* Receives the license key for the given plugin slug from the WPCOM website. | ||
* This endpoint is expected to be called as the `activation_url` from flush WPCOM license. | ||
* | ||
* @param \WP_REST_Request $request The current request. | ||
* | ||
* @return \WP_REST_Response|\WP_Error | ||
*/ | ||
public function receive_wpcom_license_key( $request ) { | ||
$license_key = sanitize_text_field( $request->get_param( 'license_key' ) ); | ||
$plugin_slug = sanitize_text_field( $request->get_param( 'plugin_slug' ) ); | ||
$custom_nonce = $request->get_param( 'custom_nonce' ); | ||
$response = [ | ||
'success' => false, | ||
'messages' => [], | ||
]; | ||
|
||
if ( $this->nonce->check_custom_nonce( $custom_nonce, 'receive-license-' . $plugin_slug ) ) { | ||
$instance = WP_Job_Manager_Helper::instance(); | ||
$instance->activate_license( $plugin_slug, $license_key ); | ||
$messages = $instance->get_messages( $plugin_slug ); | ||
$success = ! empty( $messages ); | ||
if ( ! $success ) { | ||
$messages[] = [ | ||
'type' => 'error', | ||
'message' => __( 'An error occurred while activating the license.', 'wp-job-manager' ), | ||
]; | ||
} | ||
foreach ( $messages as $message ) { | ||
if ( 'error' === $message['type'] ) { | ||
$success = false; | ||
break; | ||
} | ||
} | ||
|
||
$response['success'] = $success; | ||
$response['messages'] = $messages; | ||
} else { | ||
$response['messages'][] = [ | ||
'type' => 'error', | ||
'message' => __( 'Invalid nonce.', 'wp-job-manager' ), | ||
]; | ||
} | ||
|
||
// Pass through the API response from the license server. | ||
return rest_ensure_response( $response ); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters