diff --git a/projects/packages/my-jetpack/changelog/fix-my-jetpack-currency b/projects/packages/my-jetpack/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..f30bc187aaa9d --- /dev/null +++ b/projects/packages/my-jetpack/changelog/fix-my-jetpack-currency @@ -0,0 +1,4 @@ +Significance: minor +Type: fixed + +allow user currency in My Jetpack pricing diff --git a/projects/packages/my-jetpack/composer.json b/projects/packages/my-jetpack/composer.json index 5761a341a83e0..a5a95ec159f38 100644 --- a/projects/packages/my-jetpack/composer.json +++ b/projects/packages/my-jetpack/composer.json @@ -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" diff --git a/projects/packages/my-jetpack/package.json b/projects/packages/my-jetpack/package.json index bfc75aa875f95..a1a7c67cc5e0c 100644 --- a/projects/packages/my-jetpack/package.json +++ b/projects/packages/my-jetpack/package.json @@ -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": { diff --git a/projects/packages/my-jetpack/src/class-initializer.php b/projects/packages/my-jetpack/src/class-initializer.php index b998a9f2b7985..6e73695b51fb9 100644 --- a/projects/packages/my-jetpack/src/class-initializer.php +++ b/projects/packages/my-jetpack/src/class-initializer.php @@ -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. diff --git a/projects/packages/my-jetpack/src/class-wpcom-products.php b/projects/packages/my-jetpack/src/class-wpcom-products.php index e4439084a752b..83cdabfbf56b0 100644 --- a/projects/packages/my-jetpack/src/class-wpcom-products.php +++ b/projects/packages/my-jetpack/src/class-wpcom-products.php @@ -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; @@ -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'; /** @@ -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( '¤cy=%s', $user_details['user_currency'] ); + } + } + $wpcom_request = Client::wpcom_json_api_request_as_blog( $endpoint, '1.1', @@ -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 * @@ -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 ); } @@ -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; } /** diff --git a/projects/packages/my-jetpack/src/products/class-search.php b/projects/packages/my-jetpack/src/products/class-search.php index df81d2e98d9a1..af38892f444de 100644 --- a/projects/packages/my-jetpack/src/products/class-search.php +++ b/projects/packages/my-jetpack/src/products/class-search.php @@ -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; @@ -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( '¤cy=%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 ) diff --git a/projects/plugins/backup/changelog/fix-my-jetpack-currency b/projects/plugins/backup/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/backup/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/backup/composer.lock b/projects/plugins/backup/composer.lock index 1f1aab1d8837f..31a6f4f5c101e 100644 --- a/projects/plugins/backup/composer.lock +++ b/projects/plugins/backup/composer.lock @@ -1160,7 +1160,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1198,7 +1198,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" diff --git a/projects/plugins/boost/changelog/fix-my-jetpack-currency b/projects/plugins/boost/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/boost/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/boost/composer.lock b/projects/plugins/boost/composer.lock index 3284aeb48a445..fe23191fa0b34 100644 --- a/projects/plugins/boost/composer.lock +++ b/projects/plugins/boost/composer.lock @@ -1079,7 +1079,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1117,7 +1117,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" diff --git a/projects/plugins/jetpack/changelog/fix-my-jetpack-currency b/projects/plugins/jetpack/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..a1c1831fa1ef7 --- /dev/null +++ b/projects/plugins/jetpack/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Updated composer.lock. + + diff --git a/projects/plugins/jetpack/composer.lock b/projects/plugins/jetpack/composer.lock index 2ce6fee251893..8b822b17dc624 100644 --- a/projects/plugins/jetpack/composer.lock +++ b/projects/plugins/jetpack/composer.lock @@ -1744,7 +1744,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1782,7 +1782,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" diff --git a/projects/plugins/migration/changelog/fix-my-jetpack-currency b/projects/plugins/migration/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/migration/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/migration/composer.lock b/projects/plugins/migration/composer.lock index 83b08ff4e171d..3f7fef027ef99 100644 --- a/projects/plugins/migration/composer.lock +++ b/projects/plugins/migration/composer.lock @@ -1160,7 +1160,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1198,7 +1198,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" diff --git a/projects/plugins/protect/changelog/fix-my-jetpack-currency b/projects/plugins/protect/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/protect/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/protect/composer.lock b/projects/plugins/protect/composer.lock index 8302711f79c69..49ca3394784ae 100644 --- a/projects/plugins/protect/composer.lock +++ b/projects/plugins/protect/composer.lock @@ -1073,7 +1073,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1111,7 +1111,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" diff --git a/projects/plugins/search/changelog/fix-my-jetpack-currency b/projects/plugins/search/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/search/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/search/composer.lock b/projects/plugins/search/composer.lock index 7b3759662a2e6..bd7cdbf0b9fe1 100644 --- a/projects/plugins/search/composer.lock +++ b/projects/plugins/search/composer.lock @@ -1016,7 +1016,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1054,7 +1054,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" diff --git a/projects/plugins/social/changelog/fix-my-jetpack-currency b/projects/plugins/social/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/social/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/social/composer.lock b/projects/plugins/social/composer.lock index 71a8cec7c7221..b7cf6d1e8c8f6 100644 --- a/projects/plugins/social/composer.lock +++ b/projects/plugins/social/composer.lock @@ -1016,7 +1016,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1054,7 +1054,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" diff --git a/projects/plugins/starter-plugin/changelog/fix-my-jetpack-currency b/projects/plugins/starter-plugin/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/starter-plugin/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/starter-plugin/composer.lock b/projects/plugins/starter-plugin/composer.lock index d3e18dd6ae524..2fe739f511bf1 100644 --- a/projects/plugins/starter-plugin/composer.lock +++ b/projects/plugins/starter-plugin/composer.lock @@ -1016,7 +1016,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1054,7 +1054,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" diff --git a/projects/plugins/videopress/changelog/fix-my-jetpack-currency b/projects/plugins/videopress/changelog/fix-my-jetpack-currency new file mode 100644 index 0000000000000..9aa70e3ec1f75 --- /dev/null +++ b/projects/plugins/videopress/changelog/fix-my-jetpack-currency @@ -0,0 +1,5 @@ +Significance: patch +Type: changed +Comment: Updated composer.lock. + + diff --git a/projects/plugins/videopress/composer.lock b/projects/plugins/videopress/composer.lock index 2828923ed7187..90d82594aab41 100644 --- a/projects/plugins/videopress/composer.lock +++ b/projects/plugins/videopress/composer.lock @@ -1016,7 +1016,7 @@ "dist": { "type": "path", "url": "../../packages/my-jetpack", - "reference": "661b9575e9a137bbfa69319abe670453181a1449" + "reference": "6b23d8933ae1026ffd3d3015bc60afba30a45930" }, "require": { "automattic/jetpack-admin-ui": "@dev", @@ -1054,7 +1054,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"