Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.43% covered (success)
91.43%
32 / 35
66.67% covered (warning)
66.67%
2 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Feature_Upgrades
91.43% covered (success)
91.43%
32 / 35
66.67% covered (warning)
66.67%
2 / 3
12.09
0.00% covered (danger)
0.00%
0 / 1
 filter_feature_upgrade
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 filter_plan_urls
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 has_top_plan
66.67% covered (warning)
66.67%
6 / 9
0.00% covered (danger)
0.00%
0 / 1
5.93
1<?php
2/**
3 * Sends Jetpack's upgrade and plan links on WoA to WordPress.com's plans.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * Answers My Jetpack's `jetpack_feature_upgrade` and `jetpack_my_jetpack_plan_urls` filters with WordPress.com's plans.
12 */
13class WPCOM_Feature_Upgrades {
14
15    /**
16     * The hosting feature page behind each My Jetpack feature slug.
17     *
18     * @var array<string, class-string<WPCOM_Hosting_Feature_Page>>
19     */
20    const PAGES = array(
21        'backup'            => WPCOM_Backup::class,
22        'protect-dashboard' => WPCOM_Scan::class,
23    );
24
25    /**
26     * Plans with none above them: Commerce, and Woo Hosted Pro, whose stores the upgrade flow keeps on Woo Hosted plans.
27     *
28     * Slugs, because `Current_Plan` gives Commerce the same `business` class as Business.
29     *
30     * @var string[]
31     */
32    const TOP_PLANS = array(
33        'ecommerce-bundle',
34        'ecommerce-bundle-monthly',
35        'ecommerce-bundle-2y',
36        'ecommerce-bundle-3y',
37        'woo_hosted_pro_plan_monthly',
38        'woo_hosted_pro_plan_yearly',
39    );
40
41    /**
42     * Send a feature's upgrade to the WordPress.com plans; a site that has it keeps Jetpack's own manage link.
43     *
44     * @param array  $upgrade          Where the upgrade goes: `path`, `name`, `back_url`, and optionally `url`.
45     * @param string $slug             The My Jetpack feature slug.
46     * @param bool   $site_has_feature Whether the site already has the feature.
47     * @return array
48     */
49    public static function filter_feature_upgrade( $upgrade, $slug, $site_has_feature ) {
50        if ( $site_has_feature || ! isset( self::PAGES[ $slug ] ) ) {
51            return $upgrade;
52        }
53
54        $page = self::PAGES[ $slug ];
55
56        return array(
57            'path' => '',
58            'name' => 'WordPress.com Business', // Product name, do not translate.
59            'url'  => $page::get_plan_upgrade_url( (string) ( $upgrade['back_url'] ?? '' ) ),
60        );
61    }
62
63    /**
64     * Send My Jetpack's plan links to the WordPress.com plan upgrade flow and the site's purchases.
65     *
66     * @param array $urls Where the `purchase` and `manage` links go, or false to hide one.
67     * @return array
68     */
69    public static function filter_plan_urls( $urls ) {
70        $urls = (array) $urls;
71
72        // As the Simple My Jetpack page and the Upgrades and Purchases menus do, offer these sites no plan links.
73        if ( get_option( 'wpcom_is_staging_site' ) || is_fully_managed_agency_site() ) {
74            $urls['purchase'] = false;
75            $urls['manage']   = false;
76
77            return $urls;
78        }
79
80        $site_slug  = wpcom_get_site_slug();
81        $my_jetpack = rawurlencode( admin_url( 'admin.php?page=my-jetpack' ) );
82
83        // No `feature`, which would narrow the plans the flow offers to those with one feature.
84        $upgrade_url = add_query_arg(
85            array(
86                'siteSlug'    => rawurlencode( $site_slug ),
87                'back_to'     => $my_jetpack,
88                'redirect_to' => $my_jetpack,
89            ),
90            'https://wordpress.com/setup/plan-upgrade'
91        );
92
93        $urls['purchase'] = self::has_top_plan() ? false : $upgrade_url;
94        $urls['manage']   = 'https://wordpress.com/purchases/subscriptions/' . $site_slug;
95
96        return $urls;
97    }
98
99    /**
100     * Whether the site's plan has none above it for the upgrade flow to offer.
101     *
102     * @return bool
103     */
104    private static function has_top_plan() {
105        // Tests fill the expiry notices' wrapper; without it, wpcomsh answers directly.
106        if ( function_exists( 'wpcom_expiry_get_purchases' ) ) {
107            $purchases = wpcom_expiry_get_purchases();
108        } elseif ( function_exists( 'wpcom_get_site_purchases' ) ) {
109            $purchases = wpcom_get_site_purchases();
110        } else {
111            $purchases = array();
112        }
113
114        foreach ( (array) $purchases as $purchase ) {
115            if ( in_array( $purchase->product_slug ?? '', self::TOP_PLANS, true ) ) {
116                return true;
117            }
118        }
119
120        return false;
121    }
122}