Skip to content

Commit

Permalink
Fix site specific features passed to front-end for simple sites (#39817)
Browse files Browse the repository at this point in the history
* Add `Current_Plan::get_simple_site_specific_features()` method

* Pass the features for simple sites to the script data

* Create siteHasFeature utility

* Add changelog
  • Loading branch information
manzoorwanijk authored Oct 21, 2024
1 parent 1ac7d92 commit cc5911c
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed the site features for Simple sites
11 changes: 11 additions & 0 deletions projects/js-packages/script-data/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,14 @@ export function getMyJetpackUrl( section = '' ) {
export function getActiveFeatures() {
return getScriptData().site.plan?.features?.active ?? [];
}

/**
* Check if the site has a specific feature.
*
* @param {string} feature - The feature to check. e.g. "republicize".
*
* @return {boolean} Whether the site has the feature.
*/
export function siteHasFeature( feature: string ) {
return getActiveFeatures().includes( feature );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed the site features for Simple sites
45 changes: 44 additions & 1 deletion projects/packages/plans/src/class-current-plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ class Current_Plan {
*/
private static $active_plan_cache;

/**
* Simple Site-specific features available.
* Their calculation can be expensive and slow, so we're caching it for the request.
*
* @var array Site-specific features
*/
private static $simple_site_specific_features = array();

/**
* The name of the option that will store the site's plan.
*
Expand Down Expand Up @@ -374,7 +382,7 @@ function_exists( 'wpcom_feature_exists' ) &&
return true;
}

// As of 05 2023 - all plans support Earn features (minus 'simple-payments')
// As of 05 2023 - all plans support Earn features (minus 'simple-payments').
if ( in_array( $feature, array( 'donations', 'recurring-payments', 'premium-content/container' ), true ) ) {
return true;
}
Expand All @@ -390,4 +398,39 @@ function_exists( 'wpcom_feature_exists' ) &&

return false;
}

/**
* Retrieve site-specific features for Simple sites.
*
* See Jetpack_Gutenberg::get_site_specific_features()
*
* @return array
*/
public static function get_simple_site_specific_features() {
$is_simple_site = defined( 'IS_WPCOM' ) && constant( 'IS_WPCOM' );

if ( ! $is_simple_site ) {
return array(
'active' => array(),
'available' => array(),
);
}

$current_blog_id = get_current_blog_id();

// Return the cached value if it exists.
if ( isset( self::$simple_site_specific_features[ $current_blog_id ] ) ) {
return self::$simple_site_specific_features[ $current_blog_id ];
}

if ( ! class_exists( '\Store_Product_List' ) ) {
require WP_CONTENT_DIR . '/admin-plugins/wpcom-billing/store-product-list.php';
}

$simple_site_specific_features = \Store_Product_List::get_site_specific_features_data( $current_blog_id );

self::$simple_site_specific_features[ $current_blog_id ] = $simple_site_specific_features;

return $simple_site_specific_features;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Fixed the site features for Simple sites
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public static function set_admin_script_data( $data ) {
$data['site']['plan'] = Current_Plan::get();
}

// Override features for simple sites.
if ( ( new Host() )->is_wpcom_simple() ) {
$data['site']['plan']['features'] = Current_Plan::get_simple_site_specific_features();
}

return $data;
}

Expand Down

0 comments on commit cc5911c

Please sign in to comment.