Skip to content

Commit 70db18b

Browse files
authored
Settings: Reuse My Jetpack's cached lookup for the site's purchases (#55213)
* Settings: Reuse My Jetpack's cached lookup for the site's purchases * Test the site purchases endpoint with no purchases
1 parent 4366a79 commit 70db18b

3 files changed

Lines changed: 125 additions & 20 deletions

File tree

‎projects/plugins/jetpack/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77

88
use Automattic\Jetpack\Connection\Client;
9+
use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
910
use Automattic\Jetpack\Publicize\Publicize;
1011
use Automattic\Jetpack\Stats\WPCOM_Stats;
1112

@@ -64,35 +65,20 @@ public static function get_features() {
6465
/**
6566
* Returns the result of `/upgrades` endpoint call.
6667
*
67-
* @return array of site purchases.
68+
* @return WP_REST_Response|WP_Error The site purchases, or an error if they could not be fetched.
6869
*/
6970
public static function get_purchases() {
70-
// Make the API request.
71-
$request = sprintf( '/upgrades?site=%d', Jetpack_Options::get_option( 'id' ) );
72-
$response = Client::wpcom_json_api_request_as_blog( $request, '1.2' );
73-
74-
// Bail if there was an error or malformed response.
75-
if ( is_wp_error( $response ) || ! is_array( $response ) || ! isset( $response['body'] ) ) {
76-
return self::get_failed_fetch_error();
77-
}
78-
79-
if ( 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
80-
return self::get_failed_fetch_error();
81-
}
82-
83-
// Decode the results.
84-
$results = json_decode( $response['body'], true );
85-
86-
// Bail if there were no results or purchase details returned.
87-
if ( ! is_array( $results ) ) {
71+
// Share My Jetpack's cached lookup, which other Jetpack pages also read, instead of asking WordPress.com again.
72+
$purchases = Wpcom_Products::get_site_current_purchases();
73+
if ( ! is_array( $purchases ) ) {
8874
return self::get_failed_fetch_error();
8975
}
9076

9177
return rest_ensure_response(
9278
array(
9379
'code' => 'success',
9480
'message' => esc_html__( 'Site purchases correctly received.', 'jetpack' ),
95-
'data' => wp_remote_retrieve_body( $response ),
81+
'data' => wp_json_encode( $purchases, JSON_UNESCAPED_SLASHES ),
9682
)
9783
);
9884
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: enhancement
3+
4+
Settings: Reuse the site's purchases already loaded for other Jetpack pages instead of asking WordPress.com for them again.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
use Automattic\Jetpack\Connection\Tokens;
4+
use Automattic\Jetpack\Constants;
5+
use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
6+
use PHPUnit\Framework\Attributes\CoversClass;
7+
use PHPUnit\Framework\Attributes\DataProvider;
8+
9+
require_once JETPACK__PLUGIN_DIR . '/_inc/lib/core-api/class.jetpack-core-api-site-endpoints.php';
10+
11+
/**
12+
* @covers \Jetpack_Core_API_Site_Endpoint
13+
*/
14+
#[CoversClass( Jetpack_Core_API_Site_Endpoint::class )]
15+
class Jetpack_Core_Api_Site_Purchases_Test extends WP_UnitTestCase {
16+
use \Automattic\Jetpack\PHPUnit\WP_UnitTestCase_Fix;
17+
18+
/**
19+
* Requests sent to WordPress.com.
20+
*
21+
* @var int
22+
*/
23+
private $requests = 0;
24+
25+
/**
26+
* Status code the mocked `/upgrades` request answers with.
27+
*
28+
* @var int
29+
*/
30+
private $status = 200;
31+
32+
/**
33+
* Body the mocked `/upgrades` request answers with.
34+
*
35+
* @var string
36+
*/
37+
private $body = '';
38+
39+
public function set_up() {
40+
parent::set_up();
41+
42+
( new Tokens() )->update_blog_token( 'test.test' );
43+
Jetpack_Options::update_option( 'id', 123 );
44+
Constants::set_constant( 'JETPACK__WPCOM_JSON_API_BASE', 'https://public-api.wordpress.com' );
45+
Wpcom_Products::reset_purchases_cache();
46+
Wpcom_Products::reset_request_failures();
47+
add_filter( 'pre_http_request', array( $this, 'mock_upgrades' ), 10, 3 );
48+
}
49+
50+
public function tear_down() {
51+
remove_filter( 'pre_http_request', array( $this, 'mock_upgrades' ) );
52+
Wpcom_Products::reset_purchases_cache();
53+
Wpcom_Products::reset_request_failures();
54+
Constants::clear_constants();
55+
Jetpack_Options::delete_option( array( 'id', 'blog_token' ) );
56+
57+
parent::tear_down();
58+
}
59+
60+
/**
61+
* @param false|array $preempt Whether to preempt the request.
62+
* @param array $args Request arguments.
63+
* @param string $url Request URL.
64+
* @return false|array
65+
*/
66+
public function mock_upgrades( $preempt, $args, $url ) {
67+
if ( ! str_contains( $url, '/upgrades' ) ) {
68+
return $preempt;
69+
}
70+
++$this->requests;
71+
72+
return array(
73+
'body' => $this->body,
74+
'response' => array(
75+
'code' => $this->status,
76+
'message' => '',
77+
),
78+
);
79+
}
80+
81+
/**
82+
* @return array[]
83+
*/
84+
public static function provide_purchases() {
85+
return array(
86+
'one purchase' => array( '[{"product_slug":"jetpack_complete","expiry_date":"2027-11-18T00:00:00+00:00"}]' ),
87+
'no purchases' => array( '[]' ),
88+
);
89+
}
90+
91+
/**
92+
* @dataProvider provide_purchases
93+
*
94+
* @param string $body The `/upgrades` response body.
95+
*/
96+
#[DataProvider( 'provide_purchases' )]
97+
public function test_reuses_the_my_jetpack_purchases_lookup_and_keeps_the_payload( $body ) {
98+
$this->body = $body;
99+
Wpcom_Products::get_site_current_purchases();
100+
101+
$response = Jetpack_Core_API_Site_Endpoint::get_purchases();
102+
103+
$this->assertSame( 1, $this->requests );
104+
$this->assertSame( json_decode( $body, true ), json_decode( $response->get_data()['data'], true ) );
105+
}
106+
107+
public function test_failed_lookup_returns_an_error() {
108+
$this->status = 500;
109+
110+
$result = Jetpack_Core_API_Site_Endpoint::get_purchases();
111+
112+
$this->assertWPError( $result );
113+
$this->assertSame( 'failed_to_fetch_data', $result->get_error_code() );
114+
}
115+
}

0 commit comments

Comments
 (0)