From 742315d3f0fa531d40c5a8f0bd862cd922f77a93 Mon Sep 17 00:00:00 2001 From: Fernando Jorge Mota Date: Thu, 21 Sep 2023 22:39:36 -0300 Subject: [PATCH 1/3] Change endpoint used by the plugin_information method on WP_Job_Manager_Helper_API --- .../class-wp-job-manager-helper-api.php | 43 ++++++++++++++++--- 1 file changed, 38 insertions(+), 5 deletions(-) diff --git a/includes/helper/class-wp-job-manager-helper-api.php b/includes/helper/class-wp-job-manager-helper-api.php index d0644cba1..4be107e43 100644 --- a/includes/helper/class-wp-job-manager-helper-api.php +++ b/includes/helper/class-wp-job-manager-helper-api.php @@ -77,13 +77,46 @@ public function bulk_update_check( array $plugins ) { * Sends and receives data related to plugin information from the WPJobManager.com API. * * @param array $args The arguments to pass to the endpoint. - * @return array|false The response, or false if the request failed. + * @return \stdClass|false The response, or false if the request failed. */ public function plugin_information( $args ) { - $args = wp_parse_args( $args ); - $args['wc-api'] = 'wp_plugin_licencing_update_api'; - $args['request'] = 'plugininformation'; - return $this->request( $args ); + $args = wp_parse_args( $args ); + $data = $this->request_endpoint( + 'wp-json/wpjmcom-licensing/v1/plugin-information', + [ + 'method' => 'POST', + 'body' => wp_json_encode( + [ + 'site_url' => $this->get_site_url(), + 'license_key' => $args['license_key'], + 'product_slug' => $args['api_product_id'], + ] + ), + ] + ); + if ( ! is_array( $data ) ) { + return false; + } + $response = new \stdClass(); + $response->name = $data['name']; + $slug = $data['slug']; + $response->plugin = $slug . '/' . $slug . '.php'; + $response->slug = $slug; + $response->version = $data['version']; + $response->last_updated = $data['last_updated']; + $response->author = $data['author']; + $response->requires = $data['requires']; + $response->tested = $data['tested']; + $response->homepage = $data['homepage']; + + // set sections. + $response->sections = [ + 'description' => $data['sections']['description'], + 'changelog' => $data['sections']['changelog'], + ]; + + $response->download_link = $data['download_link']; + return $response; } /** From fefab691ddb4451914e923b1d15e7d7948202b13 Mon Sep 17 00:00:00 2001 From: Fernando Jorge Mota Date: Fri, 22 Sep 2023 19:11:43 -0300 Subject: [PATCH 2/3] Use GET as method for the plugin-information endpoint, and pass information correctly --- .../helper/class-wp-job-manager-helper-api.php | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/includes/helper/class-wp-job-manager-helper-api.php b/includes/helper/class-wp-job-manager-helper-api.php index 4be107e43..53990e9c9 100644 --- a/includes/helper/class-wp-job-manager-helper-api.php +++ b/includes/helper/class-wp-job-manager-helper-api.php @@ -84,14 +84,12 @@ public function plugin_information( $args ) { $data = $this->request_endpoint( 'wp-json/wpjmcom-licensing/v1/plugin-information', [ - 'method' => 'POST', - 'body' => wp_json_encode( - [ - 'site_url' => $this->get_site_url(), - 'license_key' => $args['license_key'], - 'product_slug' => $args['api_product_id'], - ] - ), + 'method' => 'GET', + 'body' => [ + 'site_url' => $this->get_site_url(), + 'license_key' => $args['license_key'], + 'product_slug' => $args['api_product_id'], + ], ] ); if ( ! is_array( $data ) ) { From 0ef43c61736ef5a3c999998f21e4e2d6f71884b3 Mon Sep 17 00:00:00 2001 From: Fernando Jorge Mota Date: Fri, 22 Sep 2023 19:21:22 -0300 Subject: [PATCH 3/3] Update tests to the plugin_information_valid API --- .../test_class.wp-job-manager-helper-api.php | 46 ++++++++++++++----- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php index 622c972ab..c92ff5b44 100644 --- a/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php +++ b/tests/php/tests/includes/helper/test_class.wp-job-manager-helper-api.php @@ -38,22 +38,44 @@ public function test_wp_job_manager_api_instance() { */ public function test_plugin_information_valid() { $base_args = $this->get_base_args(); - $this->set_expected_response( - [ - 'args' => wp_parse_args( - [ - 'wc-api' => 'wp_plugin_licencing_update_api', - 'request' => 'plugininformation', - ], - $base_args - ), - ] - ); + $data = [ + 'name' => 'Sample Plugin', + 'slug' => 'sample-plugin', + 'version' => '1.0.0', + 'last_updated' => '2023-09-22', + 'author' => 'John Doe', + 'requires' => 'WordPress 5.0+', + 'tested' => 'WordPress 5.8', + 'homepage' => 'https://www.example.com/sample-plugin', + 'sections' => array( + 'description' => 'This is a sample plugin for demonstration purposes.', + 'changelog' => 'Version 1.0.0 - Initial release', + ), + 'download_link' => 'https://www.example.com/sample-plugin/sample-plugin.zip', + ]; + $this->mock_http_request( '/wp-json/wpjmcom-licensing/v1/plugin-information', $data); $instance = new WP_Job_Manager_Helper_API(); $response = $instance->plugin_information( $base_args ); // If a request was made that we don't expect, `$response` would be false. - $this->assertEquals( $this->default_valid_response(), $response ); + + $this->assertInstanceOf(stdClass::class, $response); + $this->assertEquals($data['name'], $response->name); + $this->assertEquals($data['slug'], $response->slug); + $this->assertEquals($data['version'], $response->version); + $this->assertEquals($data['last_updated'], $response->last_updated); + $this->assertEquals($data['author'], $response->author); + $this->assertEquals($data['requires'], $response->requires); + $this->assertEquals($data['tested'], $response->tested); + $this->assertEquals($data['homepage'], $response->homepage); + + $this->assertEquals($data['sections']['description'], $response->sections['description']); + $this->assertEquals($data['sections']['changelog'], $response->sections['changelog']); + + $this->assertEquals($data['download_link'], $response->download_link); + + $expectedPlugin = $data['slug'] . '/' . $data['slug'] . '.php'; + $this->assertEquals($expectedPlugin, $response->plugin); } /**