Skip to content

Commit

Permalink
Fix/my jetpack currency (#38977)
Browse files Browse the repository at this point in the history
* Allow currency to be set for a connected user, reload the cache with new user connection

* changelog

* Update search pricing fetch as well

* version management

* Upate lock file conflicts

* Rebase

---------

Co-authored-by: Dylan Munson <[email protected]>
  • Loading branch information
jboland88 and CodeyGuyDylan authored Aug 21, 2024
1 parent bae165e commit fee6c98
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: fixed

allow user currency in My Jetpack pricing
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
"link-template": "https://github.com/Automattic/jetpack-my-jetpack/compare/${old}...${new}"
},
"branch-alias": {
"dev-trunk": "4.32.x-dev"
"dev-trunk": "4.33.x-dev"
},
"version-constants": {
"::PACKAGE_VERSION": "src/class-initializer.php"
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "@automattic/jetpack-my-jetpack",
"version": "4.32.4-alpha",
"version": "4.33.0-alpha",
"description": "WP Admin page with information and configuration shared among all Jetpack stand-alone plugins",
"homepage": "https://github.com/Automattic/jetpack/tree/HEAD/projects/packages/my-jetpack/#readme",
"bugs": {
Expand Down
2 changes: 1 addition & 1 deletion projects/packages/my-jetpack/src/class-initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Initializer {
*
* @var string
*/
const PACKAGE_VERSION = '4.32.4-alpha';
const PACKAGE_VERSION = '4.33.0-alpha';

/**
* HTML container ID for the IDC screen on My Jetpack page.
Expand Down
57 changes: 51 additions & 6 deletions projects/packages/my-jetpack/src/class-wpcom-products.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Automattic\Jetpack\My_Jetpack;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Status\Visitor;
use Jetpack_Options;
use WP_Error;
Expand All @@ -30,6 +31,8 @@ class Wpcom_Products {
*/
const CACHE_META_NAME = 'my-jetpack-cache';

const CACHE_CHECK_HASH_NAME = 'my-jetpack-wpcom-product-check-hash';

const MY_JETPACK_PURCHASES_TRANSIENT_KEY = 'my-jetpack-purchases';

/**
Expand All @@ -38,17 +41,25 @@ class Wpcom_Products {
* @return Object|WP_Error
*/
private static function get_products_from_wpcom() {
$blog_id = \Jetpack_Options::get_option( 'id' );
$ip = ( new Visitor() )->get_ip( true );
$headers = array(
$connection = new Connection_Manager();
$blog_id = \Jetpack_Options::get_option( 'id' );
$ip = ( new Visitor() )->get_ip( true );
$headers = array(
'X-Forwarded-For' => $ip,
);

// If has a blog id, use connected endpoint.

if ( $blog_id ) {
// If has a blog id, use connected endpoint.
$endpoint = sprintf( '/sites/%d/products/?_locale=%s&type=jetpack', $blog_id, get_user_locale() );

// If available in the user data, set the user's currency as one of the params
if ( $connection->is_user_connected() ) {
$user_details = $connection->get_connected_user_data();
if ( $user_details['user_currency'] && $user_details['user_currency'] !== 'USD' ) {
$endpoint .= sprintf( '&currency=%s', $user_details['user_currency'] );
}
}

$wpcom_request = Client::wpcom_json_api_request_as_blog(
$endpoint,
'1.1',
Expand Down Expand Up @@ -81,6 +92,32 @@ private static function get_products_from_wpcom() {
}
}

/**
* Super unintelligent hash string that can help us reset the cache after connection changes
* This is important because the currency can change after a user connects depending on what is set in their profile
*
* @return string
*/
private static function build_check_hash() {
$hash_string = 'check_hash_';
$connection = new Connection_Manager();

if ( $connection->is_connected() ) {
$hash_string .= 'site_connected_';
}

if ( $connection->is_user_connected() ) {
$hash_string .= 'user_connected';
// Add the user's currency
$user_details = $connection->get_connected_user_data();
if ( $user_details['user_currency'] ) {
$hash_string .= '_' . $user_details['user_currency'];
}
}

return md5( $hash_string );
}

/**
* Update the cache with new information retrieved from WPCOM
*
Expand All @@ -92,6 +129,7 @@ private static function get_products_from_wpcom() {
*/
private static function update_cache( $products_list ) {
update_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, time() );
update_user_meta( get_current_user_id(), self::CACHE_CHECK_HASH_NAME, self::build_check_hash() );
return update_user_meta( get_current_user_id(), self::CACHE_META_NAME, $products_list );
}

Expand All @@ -102,8 +140,15 @@ private static function is_cache_old() {
if ( empty( self::get_products_from_cache() ) ) {
return true;
}

// This allows the cache to reset after the site or user connects/ disconnects
$check_hash = get_user_meta( get_current_user_id(), self::CACHE_CHECK_HASH_NAME, true );
if ( $check_hash !== self::build_check_hash() ) {
return true;
}

$cache_date = get_user_meta( get_current_user_id(), self::CACHE_DATE_META_NAME, true );
return time() - (int) $cache_date > ( 7 * DAY_IN_SECONDS );
return time() - (int) $cache_date > DAY_IN_SECONDS;
}

/**
Expand Down
20 changes: 16 additions & 4 deletions projects/packages/my-jetpack/src/products/class-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Automattic\Jetpack\My_Jetpack\Products;

use Automattic\Jetpack\Connection\Client;
use Automattic\Jetpack\Connection\Manager as Connection_Manager;
use Automattic\Jetpack\Constants;
use Automattic\Jetpack\My_Jetpack\Hybrid_Product;
use Automattic\Jetpack\My_Jetpack\Wpcom_Products;
Expand Down Expand Up @@ -222,22 +223,33 @@ public static function get_status() {
*/
public static function get_pricing_from_wpcom( $record_count ) {
static $pricings = array();
$connection = new Connection_Manager();
$blog_id = \Jetpack_Options::get_option( 'id' );

if ( isset( $pricings[ $record_count ] ) ) {
return $pricings[ $record_count ];
}

if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
// For simple sites fetch the response directly.
// If the site is connected, request pricing with the blog token
if ( $blog_id ) {
$endpoint = sprintf( '/jetpack-search/pricing?record_count=%1$d&locale=%2$s', $record_count, get_user_locale() );

// If available in the user data, set the user's currency as one of the params
if ( $connection->is_user_connected() ) {
$user_details = $connection->get_connected_user_data();
if ( $user_details['user_currency'] && $user_details['user_currency'] !== 'USD' ) {
$endpoint .= sprintf( '&currency=%s', $user_details['user_currency'] );
}
}

$response = Client::wpcom_json_api_request_as_blog(
sprintf( '/jetpack-search/pricing?record_count=%1$d&locale=%2$s', $record_count, get_user_locale() ),
$endpoint,
'2',
array( 'timeout' => 5 ),
null,
'wpcom'
);
} else {
// For non-simple sites we have to use the wp_remote_get, as connection might not be available.
$response = wp_remote_get(
sprintf( Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ) . '/wpcom/v2/jetpack-search/pricing?record_count=%1$d&locale=%2$s', $record_count, get_user_locale() ),
array( 'timeout' => 5 )
Expand Down
5 changes: 5 additions & 0 deletions projects/plugins/backup/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/backup/composer.lock

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

5 changes: 5 additions & 0 deletions projects/plugins/boost/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/boost/composer.lock

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

5 changes: 5 additions & 0 deletions projects/plugins/jetpack/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: other
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/jetpack/composer.lock

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

5 changes: 5 additions & 0 deletions projects/plugins/migration/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/migration/composer.lock

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

5 changes: 5 additions & 0 deletions projects/plugins/protect/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/protect/composer.lock

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

5 changes: 5 additions & 0 deletions projects/plugins/search/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/search/composer.lock

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

5 changes: 5 additions & 0 deletions projects/plugins/social/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/social/composer.lock

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/starter-plugin/composer.lock

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

5 changes: 5 additions & 0 deletions projects/plugins/videopress/changelog/fix-my-jetpack-currency
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: changed
Comment: Updated composer.lock.


4 changes: 2 additions & 2 deletions projects/plugins/videopress/composer.lock

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

0 comments on commit fee6c98

Please sign in to comment.