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