Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
94.34% covered (success)
94.34%
50 / 53
75.00% covered (warning)
75.00%
3 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Feature_Upgrades
94.34% covered (success)
94.34%
50 / 53
75.00% covered (warning)
75.00%
3 / 4
16.05
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_monetize_upgrade
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
1 / 1
4
 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 WordPress.com sites to WordPress.com's plans.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace Automattic\Jetpack\Jetpack_Mu_Wpcom;
9
10/**
11 * Answers the `jetpack_feature_upgrade` and `jetpack_my_jetpack_plan_urls` filters of My Jetpack and the Monetize page 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     * The slug the Monetize page's upgrade links pass to `jetpack_feature_upgrade`.
27     *
28     * @var string
29     */
30    const MONETIZE_SLUG = 'monetize-dashboard';
31
32    /**
33     * The plan feature the flow filters Monetize's upgrade on: Calypso's plan lists name ads `wordads-instant`.
34     *
35     * @var string
36     */
37    const MONETIZE_PLAN_FEATURE = 'wordads-instant';
38
39    /**
40     * Plans with none above them: Commerce, and Woo Hosted Pro, whose stores the upgrade flow keeps on Woo Hosted plans.
41     *
42     * Slugs, because `Current_Plan` gives Commerce the same `business` class as Business.
43     *
44     * @var string[]
45     */
46    const TOP_PLANS = array(
47        'ecommerce-bundle',
48        'ecommerce-bundle-monthly',
49        'ecommerce-bundle-2y',
50        'ecommerce-bundle-3y',
51        'woo_hosted_pro_plan_monthly',
52        'woo_hosted_pro_plan_yearly',
53    );
54
55    /**
56     * Send a feature's upgrade to the WordPress.com plans; a site that has it keeps Jetpack's own manage link.
57     *
58     * @param array  $upgrade          Where the upgrade goes: `path`, `name`, `back_url`, and optionally `url`.
59     * @param string $slug             The My Jetpack feature slug.
60     * @param bool   $site_has_feature Whether the site already has the feature.
61     * @return array
62     */
63    public static function filter_feature_upgrade( $upgrade, $slug, $site_has_feature ) {
64        if ( $site_has_feature || ! isset( self::PAGES[ $slug ] ) ) {
65            return $upgrade;
66        }
67
68        $page = self::PAGES[ $slug ];
69
70        return array(
71            'path' => '',
72            'name' => 'WordPress.com Business', // Product name, do not translate.
73            'url'  => $page::get_plan_upgrade_url( (string) ( $upgrade['back_url'] ?? '' ) ),
74        );
75    }
76
77    /**
78     * Send the Monetize page's upgrade links to the WordPress.com plans with ads, which come back to the page.
79     *
80     * Staging and fully managed agency sites can't buy a plan, so they get no upgrade link, as in filter_plan_urls().
81     *
82     * @since $$next-version$$
83     *
84     * @param array  $upgrade Where the upgrade goes: `path`, `name`, `back_url`, and optionally `url`.
85     * @param string $slug    The feature slug.
86     * @return array
87     */
88    public static function filter_monetize_upgrade( $upgrade, $slug ) {
89        if ( self::MONETIZE_SLUG !== $slug ) {
90            return $upgrade;
91        }
92
93        if ( get_option( 'wpcom_is_staging_site' ) || is_fully_managed_agency_site() ) {
94            return array( 'url' => '' ) + $upgrade;
95        }
96
97        $back = rawurlencode( (string) ( $upgrade['back_url'] ?? admin_url( 'admin.php?page=jetpack-monetize' ) ) );
98
99        return array(
100            'path' => '',
101            'name' => 'WordPress.com', // Product name, do not translate.
102            'url'  => add_query_arg(
103                array(
104                    'siteSlug'    => rawurlencode( wpcom_get_site_slug() ),
105                    'feature'     => self::MONETIZE_PLAN_FEATURE,
106                    'back_to'     => $back,
107                    'redirect_to' => $back,
108                ),
109                'https://wordpress.com/setup/plan-upgrade'
110            ),
111        );
112    }
113
114    /**
115     * Send My Jetpack's plan links to the WordPress.com plan upgrade flow and the site's purchases.
116     *
117     * @param array $urls Where the `purchase` and `manage` links go, or false to hide one.
118     * @return array
119     */
120    public static function filter_plan_urls( $urls ) {
121        $urls = (array) $urls;
122
123        // As the Simple My Jetpack page and the Upgrades and Purchases menus do, offer these sites no plan links.
124        if ( get_option( 'wpcom_is_staging_site' ) || is_fully_managed_agency_site() ) {
125            $urls['purchase'] = false;
126            $urls['manage']   = false;
127
128            return $urls;
129        }
130
131        $site_slug  = wpcom_get_site_slug();
132        $my_jetpack = rawurlencode( admin_url( 'admin.php?page=my-jetpack' ) );
133
134        // No `feature`, which would narrow the plans the flow offers to those with one feature.
135        $upgrade_url = add_query_arg(
136            array(
137                'siteSlug'    => rawurlencode( $site_slug ),
138                'back_to'     => $my_jetpack,
139                'redirect_to' => $my_jetpack,
140            ),
141            'https://wordpress.com/setup/plan-upgrade'
142        );
143
144        $urls['purchase'] = self::has_top_plan() ? false : $upgrade_url;
145        $urls['manage']   = 'https://wordpress.com/purchases/subscriptions/' . $site_slug;
146
147        return $urls;
148    }
149
150    /**
151     * Whether the site's plan has none above it for the upgrade flow to offer.
152     *
153     * @return bool
154     */
155    private static function has_top_plan() {
156        // Tests fill the expiry notices' wrapper; without it, wpcomsh answers directly.
157        if ( function_exists( 'wpcom_expiry_get_purchases' ) ) {
158            $purchases = wpcom_expiry_get_purchases();
159        } elseif ( function_exists( 'wpcom_get_site_purchases' ) ) {
160            $purchases = wpcom_get_site_purchases();
161        } else {
162            $purchases = array();
163        }
164
165        foreach ( (array) $purchases as $purchase ) {
166            if ( in_array( $purchase->product_slug ?? '', self::TOP_PLANS, true ) ) {
167                return true;
168            }
169        }
170
171        return false;
172    }
173}