Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.52% covered (success)
95.52%
64 / 67
83.33% covered (warning)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Feature_Upgrades
95.52% covered (success)
95.52%
64 / 67
83.33% covered (warning)
83.33%
5 / 6
22
0.00% covered (danger)
0.00%
0 / 1
 filter_feature_upgrade
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
6
 filter_monetize_upgrade
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 get_plan_upgrade_url
100.00% covered (success)
100.00%
13 / 13
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
3
 can_buy_plans
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 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 plan feature the upgrade flow filters on, for My Jetpack features without a hosting page.
27     *
28     * The flow reads Calypso's plan definitions, so a value must be a feature Calypso lists for exactly the plans that grant it.
29     *
30     * @var array<string, string>
31     */
32    const PLAN_FEATURES = array(
33        'monetize' => self::MONETIZE_PLAN_FEATURE,
34        // Calypso lists no podcasting feature, and every plan above Personal grants it, so the flow offers them all.
35        'podcast'  => '',
36        'social'   => 'republicize',
37    );
38
39    /**
40     * The slug the Monetize page's upgrade links pass to `jetpack_feature_upgrade`.
41     *
42     * @var string
43     */
44    const MONETIZE_SLUG = 'monetize-dashboard';
45
46    /**
47     * The plan feature the flow filters Monetize's upgrade on: Calypso's plan lists name ads `wordads-instant`.
48     *
49     * @var string
50     */
51    const MONETIZE_PLAN_FEATURE = 'wordads-instant';
52
53    /**
54     * Plans with none above them: Commerce, and Woo Hosted Pro, whose stores the upgrade flow keeps on Woo Hosted plans.
55     *
56     * Slugs, because `Current_Plan` gives Commerce the same `business` class as Business.
57     *
58     * @var string[]
59     */
60    const TOP_PLANS = array(
61        'ecommerce-bundle',
62        'ecommerce-bundle-monthly',
63        'ecommerce-bundle-2y',
64        'ecommerce-bundle-3y',
65        'woo_hosted_pro_plan_monthly',
66        'woo_hosted_pro_plan_yearly',
67    );
68
69    /**
70     * Send a feature's upgrade to the WordPress.com plans that include it; a site that has it keeps Jetpack's own manage link.
71     *
72     * A site that can't buy a plan gets an empty `url`, which offers nothing.
73     *
74     * @param array  $upgrade          Where the upgrade goes: `path`, `name`, `back_url`, and optionally `url`.
75     * @param string $slug             The My Jetpack feature slug.
76     * @param bool   $site_has_feature Whether the site already has the feature.
77     * @return array
78     */
79    public static function filter_feature_upgrade( $upgrade, $slug, $site_has_feature ) {
80        if ( $site_has_feature || ( ! isset( self::PAGES[ $slug ] ) && ! isset( self::PLAN_FEATURES[ $slug ] ) ) ) {
81            return $upgrade;
82        }
83
84        if ( ! self::can_buy_plans() ) {
85            return array( 'url' => '' ) + $upgrade;
86        }
87
88        $back_url = (string) ( $upgrade['back_url'] ?? '' );
89
90        if ( isset( self::PAGES[ $slug ] ) ) {
91            $page = self::PAGES[ $slug ];
92
93            return array(
94                'path' => '',
95                'name' => 'WordPress.com Business', // Product name, do not translate.
96                'url'  => $page::get_plan_upgrade_url( $back_url ),
97            );
98        }
99
100        return array(
101            'path' => '',
102            'name' => 'WordPress.com', // Product name, do not translate.
103            'url'  => self::get_plan_upgrade_url( self::PLAN_FEATURES[ $slug ], $back_url, admin_url( 'admin.php?page=my-jetpack' ) ),
104        );
105    }
106
107    /**
108     * Send the Monetize page's upgrade links to the WordPress.com plans with ads, which come back to the page.
109     *
110     * A site that can't buy a plan gets no upgrade link, as in filter_plan_urls().
111     *
112     * @since $$next-version$$
113     *
114     * @param array  $upgrade Where the upgrade goes: `path`, `name`, `back_url`, and optionally `url`.
115     * @param string $slug    The feature slug.
116     * @return array
117     */
118    public static function filter_monetize_upgrade( $upgrade, $slug ) {
119        if ( self::MONETIZE_SLUG !== $slug ) {
120            return $upgrade;
121        }
122
123        if ( ! self::can_buy_plans() ) {
124            return array( 'url' => '' ) + $upgrade;
125        }
126
127        return array(
128            'path' => '',
129            'name' => 'WordPress.com', // Product name, do not translate.
130            'url'  => self::get_plan_upgrade_url( self::MONETIZE_PLAN_FEATURE, (string) ( $upgrade['back_url'] ?? '' ), admin_url( 'admin.php?page=jetpack-monetize' ) ),
131        );
132    }
133
134    /**
135     * WordPress.com's plan upgrade flow, which comes back to the page the link was on after checkout or Back.
136     *
137     * @param string $feature  The plan feature to filter plans on, as Calypso names it; '' offers every higher plan.
138     * @param string $back_url The page the link is on.
139     * @param string $fallback Where to come back to when `$back_url` is empty or off this site.
140     * @return string
141     */
142    private static function get_plan_upgrade_url( $feature, $back_url, $fallback ) {
143        $back = '' === $back_url ? '' : wp_validate_redirect( $back_url, '' );
144        $back = rawurlencode( $back ? $back : $fallback );
145
146        return add_query_arg(
147            array_filter(
148                array(
149                    'siteSlug'    => rawurlencode( wpcom_get_site_slug() ),
150                    'feature'     => $feature,
151                    'back_to'     => $back,
152                    'redirect_to' => $back,
153                )
154            ),
155            'https://wordpress.com/setup/plan-upgrade'
156        );
157    }
158
159    /**
160     * Send My Jetpack's plan links to the WordPress.com plan upgrade flow and the site's purchases.
161     *
162     * @param array $urls Where the `purchase` and `manage` links go, or false to hide one.
163     * @return array
164     */
165    public static function filter_plan_urls( $urls ) {
166        $urls = (array) $urls;
167
168        // As the Simple My Jetpack page and the Upgrades and Purchases menus do, offer these sites no plan links.
169        if ( ! self::can_buy_plans() ) {
170            $urls['purchase'] = false;
171            $urls['manage']   = false;
172
173            return $urls;
174        }
175
176        $site_slug  = wpcom_get_site_slug();
177        $my_jetpack = rawurlencode( admin_url( 'admin.php?page=my-jetpack' ) );
178
179        // No `feature`, which would narrow the plans the flow offers to those with one feature.
180        $upgrade_url = add_query_arg(
181            array(
182                'siteSlug'    => rawurlencode( $site_slug ),
183                'back_to'     => $my_jetpack,
184                'redirect_to' => $my_jetpack,
185            ),
186            'https://wordpress.com/setup/plan-upgrade'
187        );
188
189        $urls['purchase'] = self::has_top_plan() ? false : $upgrade_url;
190        $urls['manage']   = 'https://wordpress.com/purchases/subscriptions/' . $site_slug;
191
192        return $urls;
193    }
194
195    /**
196     * Whether the site can buy a plan: staging and fully managed agency sites can't.
197     *
198     * @return bool
199     */
200    private static function can_buy_plans() {
201        return ! get_option( 'wpcom_is_staging_site' ) && ! is_fully_managed_agency_site();
202    }
203
204    /**
205     * Whether the site's plan has none above it for the upgrade flow to offer.
206     *
207     * @return bool
208     */
209    private static function has_top_plan() {
210        // Tests fill the expiry notices' wrapper; without it, wpcomsh answers directly.
211        if ( function_exists( 'wpcom_expiry_get_purchases' ) ) {
212            $purchases = wpcom_expiry_get_purchases();
213        } elseif ( function_exists( 'wpcom_get_site_purchases' ) ) {
214            $purchases = wpcom_get_site_purchases();
215        } else {
216            $purchases = array();
217        }
218
219        foreach ( (array) $purchases as $purchase ) {
220            if ( in_array( $purchase->product_slug ?? '', self::TOP_PLANS, true ) ) {
221                return true;
222            }
223        }
224
225        return false;
226    }
227}