Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
63 / 66
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_Feature_Upgrades
95.45% covered (success)
95.45%
63 / 66
80.00% covered (warning)
80.00%
4 / 5
20
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
4
 filter_monetize_upgrade
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
4
 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
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 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     * @param array  $upgrade          Where the upgrade goes: `path`, `name`, `back_url`, and optionally `url`.
73     * @param string $slug             The My Jetpack feature slug.
74     * @param bool   $site_has_feature Whether the site already has the feature.
75     * @return array
76     */
77    public static function filter_feature_upgrade( $upgrade, $slug, $site_has_feature ) {
78        if ( $site_has_feature ) {
79            return $upgrade;
80        }
81
82        $back_url = (string) ( $upgrade['back_url'] ?? '' );
83
84        if ( isset( self::PAGES[ $slug ] ) ) {
85            $page = self::PAGES[ $slug ];
86
87            return array(
88                'path' => '',
89                'name' => 'WordPress.com Business', // Product name, do not translate.
90                'url'  => $page::get_plan_upgrade_url( $back_url ),
91            );
92        }
93
94        if ( isset( self::PLAN_FEATURES[ $slug ] ) ) {
95            return array(
96                'path' => '',
97                'name' => 'WordPress.com', // Product name, do not translate.
98                'url'  => self::get_plan_upgrade_url( self::PLAN_FEATURES[ $slug ], $back_url, admin_url( 'admin.php?page=my-jetpack' ) ),
99            );
100        }
101
102        return $upgrade;
103    }
104
105    /**
106     * Send the Monetize page's upgrade links to the WordPress.com plans with ads, which come back to the page.
107     *
108     * Staging and fully managed agency sites can't buy a plan, so they get no upgrade link, as in filter_plan_urls().
109     *
110     * @since $$next-version$$
111     *
112     * @param array  $upgrade Where the upgrade goes: `path`, `name`, `back_url`, and optionally `url`.
113     * @param string $slug    The feature slug.
114     * @return array
115     */
116    public static function filter_monetize_upgrade( $upgrade, $slug ) {
117        if ( self::MONETIZE_SLUG !== $slug ) {
118            return $upgrade;
119        }
120
121        if ( get_option( 'wpcom_is_staging_site' ) || is_fully_managed_agency_site() ) {
122            return array( 'url' => '' ) + $upgrade;
123        }
124
125        return array(
126            'path' => '',
127            'name' => 'WordPress.com', // Product name, do not translate.
128            'url'  => self::get_plan_upgrade_url( self::MONETIZE_PLAN_FEATURE, (string) ( $upgrade['back_url'] ?? '' ), admin_url( 'admin.php?page=jetpack-monetize' ) ),
129        );
130    }
131
132    /**
133     * WordPress.com's plan upgrade flow, which comes back to the page the link was on after checkout or Back.
134     *
135     * @param string $feature  The plan feature to filter plans on, as Calypso names it; '' offers every higher plan.
136     * @param string $back_url The page the link is on.
137     * @param string $fallback Where to come back to when `$back_url` is empty or off this site.
138     * @return string
139     */
140    private static function get_plan_upgrade_url( $feature, $back_url, $fallback ) {
141        $back = '' === $back_url ? '' : wp_validate_redirect( $back_url, '' );
142        $back = rawurlencode( $back ? $back : $fallback );
143
144        return add_query_arg(
145            array_filter(
146                array(
147                    'siteSlug'    => rawurlencode( wpcom_get_site_slug() ),
148                    'feature'     => $feature,
149                    'back_to'     => $back,
150                    'redirect_to' => $back,
151                )
152            ),
153            'https://wordpress.com/setup/plan-upgrade'
154        );
155    }
156
157    /**
158     * Send My Jetpack's plan links to the WordPress.com plan upgrade flow and the site's purchases.
159     *
160     * @param array $urls Where the `purchase` and `manage` links go, or false to hide one.
161     * @return array
162     */
163    public static function filter_plan_urls( $urls ) {
164        $urls = (array) $urls;
165
166        // As the Simple My Jetpack page and the Upgrades and Purchases menus do, offer these sites no plan links.
167        if ( get_option( 'wpcom_is_staging_site' ) || is_fully_managed_agency_site() ) {
168            $urls['purchase'] = false;
169            $urls['manage']   = false;
170
171            return $urls;
172        }
173
174        $site_slug  = wpcom_get_site_slug();
175        $my_jetpack = rawurlencode( admin_url( 'admin.php?page=my-jetpack' ) );
176
177        // No `feature`, which would narrow the plans the flow offers to those with one feature.
178        $upgrade_url = add_query_arg(
179            array(
180                'siteSlug'    => rawurlencode( $site_slug ),
181                'back_to'     => $my_jetpack,
182                'redirect_to' => $my_jetpack,
183            ),
184            'https://wordpress.com/setup/plan-upgrade'
185        );
186
187        $urls['purchase'] = self::has_top_plan() ? false : $upgrade_url;
188        $urls['manage']   = 'https://wordpress.com/purchases/subscriptions/' . $site_slug;
189
190        return $urls;
191    }
192
193    /**
194     * Whether the site's plan has none above it for the upgrade flow to offer.
195     *
196     * @return bool
197     */
198    private static function has_top_plan() {
199        // Tests fill the expiry notices' wrapper; without it, wpcomsh answers directly.
200        if ( function_exists( 'wpcom_expiry_get_purchases' ) ) {
201            $purchases = wpcom_expiry_get_purchases();
202        } elseif ( function_exists( 'wpcom_get_site_purchases' ) ) {
203            $purchases = wpcom_get_site_purchases();
204        } else {
205            $purchases = array();
206        }
207
208        foreach ( (array) $purchases as $purchase ) {
209            if ( in_array( $purchase->product_slug ?? '', self::TOP_PLANS, true ) ) {
210                return true;
211            }
212        }
213
214        return false;
215    }
216}