Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new "Deactivate License" endpoint for deactivating licenses on WPJMCOM #2585

Merged
merged 4 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions includes/helper/class-wp-job-manager-helper-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,24 @@ public function bulk_activate( $license_key, $product_slugs ) {
* @return array|false JSON response or false if failed.
*/
public function deactivate( $args ) {
$args = wp_parse_args( $args );
$args['wc-api'] = 'wp_plugin_licencing_activation_api';
$args['request'] = 'deactivate';
return $this->request( $args, false );
$args = wp_parse_args( $args );
$response = $this->request_endpoint(
'wp-json/wpjmcom-licensing/v1/deactivate',
[
'method' => 'POST',
'body' => wp_json_encode(
[
'product_slug' => $args['api_product_id'],
'license_key' => $args['license_key'],
'site_url' => $this->get_site_url(),
]
),
]
);
if ( ! is_array( $response ) || ! array_key_exists( 'success', $response ) ) {
return false;
}
return $response;
Comment on lines +161 to +164
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about adding some handling here to return the same format that we have for errors here, like we have on WPJMCOM for maximum compatibility.

However, I decided to just return false here whenever success isn't present on the $response because I think it matches the return value of the method more.

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,8 @@ public function test_activate_invalid() {
*/
public function test_deactivate_valid() {
$base_args = $this->get_base_args();
$this->set_expected_response(
[
'args' => wp_parse_args(
[
'wc-api' => 'wp_plugin_licencing_activation_api',
'request' => 'deactivate',
],
$base_args
),
]
$this->mock_http_request( '/wp-json/wpjmcom-licensing/v1/deactivate',
$this->default_valid_response()
);
$instance = new WP_Job_Manager_Helper_API();
$response = $instance->deactivate( $base_args );
Expand All @@ -151,6 +143,12 @@ public function test_deactivate_valid() {
*/
public function test_deactivate_invalid() {
$base_args = $this->get_base_args();
$this->mock_http_request( '/wp-json/wpjmcom-licensing/v1/deactivate',
[
'error_code' => 'license_not_found'
],
404
);
$instance = new WP_Job_Manager_Helper_API();
$response = $instance->deactivate( $base_args );

Expand All @@ -162,18 +160,19 @@ public function test_deactivate_invalid() {
*
* @param string $endpoint The request URI/path of the endpoint to mock the response for.
* @param mixed $body The response body to return for the specified endpoint.
* @param int $status The HTTP status code to return for the specified endpoint.
*
* @return void
*/
protected function mock_http_request($endpoint, $body) {
add_filter('pre_http_request', function($preempt, $args, $url) use ($endpoint, $body) {
protected function mock_http_request($endpoint, $body, $status = 200) {
add_filter('pre_http_request', function($preempt, $args, $url) use ($endpoint, $body, $status) {
if ($endpoint === wp_parse_url($url, PHP_URL_PATH)) {
return array(
'headers' => array(),
'body' => wp_json_encode($body),
'response' => array(
'code' => 200,
'message' => 'OK',
'code' => $status,
'message' => 200 === $status ? 'OK' : 'Error'
),
'cookies' => array(),
);
Expand Down Expand Up @@ -213,7 +212,7 @@ protected function set_expected_response( $test_data ) {
}

protected function default_valid_response() {
return [ 'status' => 1 ];
return [ 'success' => true ];
}

protected function default_invalid_response() {
Expand Down
Loading