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