From b75ceb5a04b6f31cc58a2a0f5de15062083aee55 Mon Sep 17 00:00:00 2001 From: Richard Ortiz Date: Thu, 16 Jan 2025 16:47:59 +0100 Subject: [PATCH 1/4] Code that enables retrieving the experiment assignment on wpcom --- .../features/wpcom-global-styles/index.php | 59 ++++++++++++++++++- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php index 4915adfe8c4ae..2c9ab971a5326 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php @@ -9,6 +9,16 @@ use Automattic\Jetpack\Jetpack_Mu_Wpcom\Common; use Automattic\Jetpack\Plans; +/** + * Checks if Global Styles on personal are available on the current site either by A/B test assign or feature flag. + * + * @return bool Whether Global Styles are available. + */ +function is_global_styles_on_personal_plan() { + return wpcom_site_has_global_styles_in_personal_plan() + || ( class_exists( 'WPCOM_Feature_Flags' ) && WPCOM_Feature_Flags::is_enabled( WPCOM_Feature_Flags::GLOBAL_STYLES_ON_PERSONAL_PLAN ) ); +} + /** * Checks if Global Styles should be limited on the given site. * @@ -178,7 +188,7 @@ function wpcom_global_styles_enqueue_block_editor_assets() { // @TODO Remove this once the global styles are available for all users on the Personal Plan. $upgrade_url = "$calypso_domain/plans/$site_slug?plan=value_bundle&feature=style-customization"; $plan_name = Plans::get_plan( 'value_bundle' )->product_name_short; - if ( class_exists( 'WPCOM_Feature_Flags' ) && WPCOM_Feature_Flags::is_enabled( WPCOM_Feature_Flags::GLOBAL_STYLES_ON_PERSONAL_PLAN ) ) { + if ( is_global_styles_on_personal_plan() ) { $plan_name = Plans::get_plan( 'personal-bundle' )->product_name_short; $upgrade_url = "$calypso_domain/plans/$site_slug?plan=personal-bundle&feature=style-customization"; } @@ -508,7 +518,7 @@ function wpcom_display_global_styles_launch_bar() { // @TODO Remove this once the global styles are available for all users on the Personal Plan. $gs_upgrade_plan = WPCOM_VALUE_BUNDLE; $upgrade_url = "https://wordpress.com/plans/$site_slug?plan=value_bundle&feature=style-customization"; - if ( class_exists( 'WPCOM_Feature_Flags' ) && WPCOM_Feature_Flags::is_enabled( WPCOM_Feature_Flags::GLOBAL_STYLES_ON_PERSONAL_PLAN ) ) { + if ( is_global_styles_on_personal_plan() ) { $gs_upgrade_plan = WPCOM_PERSONAL_BUNDLE; $upgrade_url = "https://wordpress.com/plans/$site_slug?plan=personal-bundle&feature=style-customization"; } @@ -724,7 +734,7 @@ function wpcom_site_has_global_styles_feature( $blog_id = 0 ) { } // If the GLOBAL_STYLES_ON_PERSONAL_PLAN feature is enabled, we need to check if the site has a Personal plan and add the sticker. - if ( class_exists( 'WPCOM_Feature_Flags' ) && WPCOM_Feature_Flags::is_enabled( 'GLOBAL_STYLES_ON_PERSONAL_PLAN' ) ) { + if ( is_global_styles_on_personal_plan() ) { if ( wpcom_site_has_personal_plan( $blog_id ) ) { $note = 'Automated sticker. See paYJgx-5w2-p2'; $user = 'a8c'; // A non-empty string avoids storing the current user as author of the sticker change. @@ -760,3 +770,46 @@ function wpcom_global_styles_is_previewing_premium_theme_without_premium_plan( $ return ! $has_premium_plan_or_higher; } + +/** + * Checks whether the site has access to Global Styles with a Personal plan as part of an A/B test. + * + * @param int $blog_id Blog ID. + * @return bool Whether the site has access to Global Styles with a Personal plan. + */ +function wpcom_site_has_global_styles_in_personal_plan( $blog_id = 0 ) { + if ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) { + return false; + } + + if ( ! function_exists( '\ExPlat\assign_given_user' ) ) { + return false; + } + + if ( ! $blog_id ) { + $blog_id = get_current_blog_id(); + } + + $cache_key = "global-styles-on-personal-$blog_id"; + $found_in_cache = false; + $has_global_styles_in_personal_plan = wp_cache_get( $cache_key, 'a8c_experiments', false, $found_in_cache ); + if ( $found_in_cache ) { + return $has_global_styles_in_personal_plan; + } + + $owner_id = wpcom_get_blog_owner( $blog_id ); + if ( ! $owner_id ) { + return false; + } + + $owner = get_userdata( $owner_id ); + if ( ! $owner ) { + return false; + } + + $experiment_assignment = \ExPlat\assign_given_user( 'calypso_global_styles_personal_v2', $owner ); + $has_global_styles_in_personal_plan = 'treatment' === $experiment_assignment; + // Cache the experiment assignment to prevent duplicate DB queries in the frontend. + wp_cache_set( $cache_key, $has_global_styles_in_personal_plan, 'a8c_experiments', MONTH_IN_SECONDS ); + return $has_global_styles_in_personal_plan; +} From d6e922079a34cb8510fc3a6ac63e44b93dbeb565 Mon Sep 17 00:00:00 2001 From: Richard Ortiz Date: Thu, 16 Jan 2025 16:54:28 +0100 Subject: [PATCH 2/4] changelog --- .../changelog/add-global-styles-on-personal-experiment | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 projects/packages/jetpack-mu-wpcom/changelog/add-global-styles-on-personal-experiment diff --git a/projects/packages/jetpack-mu-wpcom/changelog/add-global-styles-on-personal-experiment b/projects/packages/jetpack-mu-wpcom/changelog/add-global-styles-on-personal-experiment new file mode 100644 index 0000000000000..b9b5a602d37ff --- /dev/null +++ b/projects/packages/jetpack-mu-wpcom/changelog/add-global-styles-on-personal-experiment @@ -0,0 +1,4 @@ +Significance: minor +Type: changed + +Added logic for handling Global Styles on Personal experiment assignment From af5454979f75d99c91021265b391b436bf3adb6e Mon Sep 17 00:00:00 2001 From: Richard Ortiz Date: Fri, 17 Jan 2025 09:47:00 +0100 Subject: [PATCH 3/4] Updated the cache key to avoid colissions with the previous experiment --- .../api/class-global-styles-status-rest-api.php | 1 + .../src/features/wpcom-global-styles/index.php | 5 +++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php index b70887381618c..544dc2bb67082 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php @@ -67,6 +67,7 @@ public function get_global_styles_info() { return array( 'globalStylesInUse' => wpcom_global_styles_in_use(), 'shouldLimitGlobalStyles' => wpcom_should_limit_global_styles(), + 'globalStylesInPersonalPlan' => wpcom_site_has_global_styles_in_personal_plan(), ); } } diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php index 2c9ab971a5326..c917f42cb65bd 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/index.php @@ -790,7 +790,7 @@ function wpcom_site_has_global_styles_in_personal_plan( $blog_id = 0 ) { $blog_id = get_current_blog_id(); } - $cache_key = "global-styles-on-personal-$blog_id"; + $cache_key = "global-styles-on-personal-feb-2025-$blog_id"; $found_in_cache = false; $has_global_styles_in_personal_plan = wp_cache_get( $cache_key, 'a8c_experiments', false, $found_in_cache ); if ( $found_in_cache ) { @@ -807,7 +807,8 @@ function wpcom_site_has_global_styles_in_personal_plan( $blog_id = 0 ) { return false; } - $experiment_assignment = \ExPlat\assign_given_user( 'calypso_global_styles_personal_v2', $owner ); + // Placeholder experiment key, we need to update this to the new experiment key once it's created. + $experiment_assignment = \ExPlat\assign_given_user( 'calypso_post_onboarding_holdout_160125', $owner ); $has_global_styles_in_personal_plan = 'treatment' === $experiment_assignment; // Cache the experiment assignment to prevent duplicate DB queries in the frontend. wp_cache_set( $cache_key, $has_global_styles_in_personal_plan, 'a8c_experiments', MONTH_IN_SECONDS ); From 7af4d0e2321d51c0cca3f46299dea9072c93e25c Mon Sep 17 00:00:00 2001 From: Richard Ortiz Date: Fri, 17 Jan 2025 09:58:20 +0100 Subject: [PATCH 4/4] Fixed code style --- .../api/class-global-styles-status-rest-api.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php index 544dc2bb67082..8e2302fa152d6 100644 --- a/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php +++ b/projects/packages/jetpack-mu-wpcom/src/features/wpcom-global-styles/api/class-global-styles-status-rest-api.php @@ -65,8 +65,8 @@ public function permissions_check() { */ public function get_global_styles_info() { return array( - 'globalStylesInUse' => wpcom_global_styles_in_use(), - 'shouldLimitGlobalStyles' => wpcom_should_limit_global_styles(), + 'globalStylesInUse' => wpcom_global_styles_in_use(), + 'shouldLimitGlobalStyles' => wpcom_should_limit_global_styles(), 'globalStylesInPersonalPlan' => wpcom_site_has_global_styles_in_personal_plan(), ); }