Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
99.21% covered (success)
99.21%
752 / 758
81.48% covered (warning)
81.48%
22 / 27
CRAP
0.00% covered (danger)
0.00%
0 / 1
Main_Features
99.21% covered (success)
99.21%
752 / 758
81.48% covered (warning)
81.48%
22 / 27
138
0.00% covered (danger)
0.00%
0 / 1
 module_works_locally
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
3
 get_feature_definitions
100.00% covered (success)
100.00%
464 / 464
100.00% covered (success)
100.00%
1 / 1
6
 get_plan_badges
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
7
 filter_upgrade
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 get_host_purchase_url
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
4
 may_host_sell_product
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_product_feature_slug
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 get_upgrade
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
6
 is_included_in_plan
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
11
 pays_for_product
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
1 / 1
8
 get_switchable_plugins
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 get_plugin_status
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 get_plugin_override
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
6.02
 get_plugin_file
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
4
 get_hosting_plugin_slug
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_hosting_plugin
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
3.58
 plugin_slug_from_path
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 is_only_my_jetpack_provider
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
11
 get_product_class_for_plugin
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
6.10
 get_module_groups
100.00% covered (success)
100.00%
55 / 55
100.00% covered (success)
100.00%
1 / 1
1
 get_hidden_modules
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 get_state
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 get_settings_entries
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 get_install_access
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 get_features
98.25% covered (success)
98.25%
56 / 57
0.00% covered (danger)
0.00%
0 / 1
25
 get_screenshot_url
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_feature_manage_url
90.91% covered (success)
90.91%
10 / 11
0.00% covered (danger)
0.00%
0 / 1
9.06
1<?php
2/**
3 * Static catalog of the main Jetpack features.
4 *
5 * @package automattic/my-jetpack
6 */
7
8namespace Automattic\Jetpack\My_Jetpack;
9
10use Automattic\Jetpack\Plugins_Installer;
11use Automattic\Jetpack\Status\Host;
12
13/**
14 * Describes the main Jetpack features: their copy, links and how a site owner gets each one.
15 *
16 * The descriptive fields may later come from a WordPress.com endpoint; what a feature is
17 * delivered by stays local, since only the site knows what is installed on it.
18 */
19class Main_Features {
20
21    /**
22     * A plugin is not on the site at all.
23     */
24    const PLUGIN_NOT_INSTALLED = 'not-installed';
25
26    /**
27     * A plugin is installed and switched off.
28     */
29    const PLUGIN_INACTIVE = 'inactive';
30
31    /**
32     * A plugin is installed and running.
33     */
34    const PLUGIN_ACTIVE = 'active';
35
36    /**
37     * The current user may install plugins here.
38     */
39    const INSTALLS_ALLOWED = 'allowed';
40
41    /**
42     * Nobody may install plugins here: file changes are turned off for the whole site.
43     */
44    const INSTALLS_DISABLED = 'disabled';
45
46    /**
47     * Others may install plugins here, but not the current user.
48     */
49    const INSTALLS_NOT_PERMITTED = 'not_permitted';
50
51    /**
52     * First My Jetpack release with offline Features enabled by default.
53     *
54     * @since 6.14.0
55     * @since 6.15.0 Raised to the first default-on release.
56     */
57    const MINIMUM_OFFLINE_FEATURES_VERSION = '6.15.0';
58
59    /**
60     * The bundles the Features tab offers as filter pills. Must match `GROUPS` in use-feature-filter.ts.
61     */
62    private const FILTER_GROUPS = array( 'security', 'growth' );
63
64    /**
65     * Whether module metadata proves it can run without either connection.
66     *
67     * @since 6.10.0
68     * @param string $slug Module slug.
69     * @return bool
70     */
71    public static function module_works_locally( $slug ) {
72        // Standalone module callbacks can ignore connection filters; missing metadata cannot prove local operation.
73        $details = ( new \Automattic\Jetpack\Modules() )->get( $slug );
74        return $details && ! $details['requires_connection'] && ! $details['requires_user_connection'];
75    }
76
77    /**
78     * The static feature catalog.
79     *
80     * Keyed by feature slug. `delivery` says what switches the feature: Protect ships inside
81     * Jetpack too, but its switch is its own plugin, so it sets `jetpack` false and names
82     * only that. `needs_account` repeats the feature module's "Requires User Connection" header.
83     * `offline_long_description` replaces `long_description` in offline Features.
84     *
85     * @return array<string, array<string, mixed>> Feature definitions keyed by feature slug.
86     */
87    public static function get_feature_definitions() {
88        // Built once per locale: the REST route asks for it on every `rest_api_init` to
89        // bound its plugin argument, and rebuilding a hundred translated strings for that
90        // is a cost every request to the site pays, not just this page.
91        static $definitions = array();
92
93        $locale = get_user_locale();
94
95        if ( isset( $definitions[ $locale ] ) ) {
96            return $definitions[ $locale ];
97        }
98
99        $definitions[ $locale ] = array(
100            'activity-log'      => array(
101                'info_url'         => 'https://jetpack.com/security/activity-log/',
102                'docs_url'         => 'https://jetpack.com/support/activity-log/',
103                'name'             => __( 'Activity Log', 'jetpack-my-jetpack' ),
104                'description'      => __( 'See what changed on your site, when it happened, and who did it.', 'jetpack-my-jetpack' ),
105                'long_description' => __( 'When something breaks, see exactly what changed and who changed it, from published posts to plugin updates and logins. Every connected site gets its 20 most recent events for free.', 'jetpack-my-jetpack' ),
106                'icon'             => 'list',
107                // The page a feature opens; `off_pages` lists its others. Each shows the turned-off card while the feature is off.
108                'admin_page'       => 'jetpack-activity-log',
109                'module'           => 'activity-log',
110                'needs_account'    => true,
111                'free_highlights'  => array(
112                    __( 'Your 20 most recent events', 'jetpack-my-jetpack' ),
113                    __( 'Who changed what, from posts to plugin updates and logins', 'jetpack-my-jetpack' ),
114                ),
115                'paid_highlights'  => array(
116                    __( '30 days of history with VaultPress Backup or Jetpack Security, a full year with Jetpack Complete', 'jetpack-my-jetpack' ),
117                    __( 'Filter events by activity type and date range', 'jetpack-my-jetpack' ),
118                ),
119                'delivery'         => array(
120                    'jetpack' => true,
121                    'plugin'  => '',
122                    'free'    => true,
123                ),
124                'plans'            => array( 'backup', 'security', 'pro', 'complete' ),
125                // The WordPress.com site feature the Activity Log package gates its paid history on.
126                'paid_feature'     => 'full-activity-log',
127            ),
128            'anti-spam'         => array(
129                'info_url'         => 'https://akismet.com/',
130                'docs_url'         => 'https://akismet.com/support/',
131                'name'             => __( 'Akismet Anti-spam', 'jetpack-my-jetpack' ),
132                'description'      => __( 'Stop comment and form spam without making visitors solve CAPTCHAs.', 'jetpack-my-jetpack' ),
133                'long_description' => __( 'Akismet filters spam out of your comments and form submissions in the background, so you stop moderating junk and visitors never have to prove they are human. When it gets one wrong, mark it and Akismet learns from your correction.', 'jetpack-my-jetpack' ),
134                'icon'             => 'comment',
135                'product'          => 'anti-spam',
136                'interstitial'     => '/add-akismet',
137                'free_highlights'  => array(
138                    __( 'Comment and form spam filtering', 'jetpack-my-jetpack' ),
139                    __( 'No CAPTCHAs for your visitors', 'jetpack-my-jetpack' ),
140                    __( 'For personal, non-commercial sites', 'jetpack-my-jetpack' ),
141                ),
142                'paid_highlights'  => array(
143                    __( 'Use it on business and commercial sites', 'jetpack-my-jetpack' ),
144                    __( 'Advanced stats and diagnostics for your spam activity', 'jetpack-my-jetpack' ),
145                    __( 'Email support', 'jetpack-my-jetpack' ),
146                ),
147                'delivery'         => array(
148                    'jetpack'     => false,
149                    'plugin'      => 'akismet',
150                    'plugin_name' => __( 'Akismet Anti-spam', 'jetpack-my-jetpack' ),
151                    'plugin_url'  => 'https://wordpress.org/plugins/akismet/',
152                    'free'        => true,
153                ),
154                'plans'            => array( 'security', 'pro', 'complete' ),
155                'paid_product'     => __( 'Jetpack Akismet Anti-spam', 'jetpack-my-jetpack' ),
156            ),
157            'backup'            => array(
158                'info_url'         => 'https://jetpack.com/backup/',
159                'docs_url'         => 'https://jetpack.com/support/backup/',
160                'name'             => __( 'VaultPress Backup', 'jetpack-my-jetpack' ),
161                'description'      => __( 'Get your site back online in one click if anything goes wrong.', 'jetpack-my-jetpack' ),
162                'long_description' => __( 'Every change you make is saved off-site as it happens, so your content and WooCommerce orders stay safe even if your host goes down. Restore your site in one click, even when it is offline.', 'jetpack-my-jetpack' ),
163                'icon'             => 'backup',
164                'product'          => 'backup',
165                // The plan decides it, whichever plugin delivers it, so its card has no switch.
166                'plan_driven'      => true,
167                'needs_account'    => true,
168                'interstitial'     => '/add-backup',
169                'setup_note'       => __( 'Backups start once you add a paid plan.', 'jetpack-my-jetpack' ),
170                'paid_highlights'  => array(
171                    __( 'Real-time backups of your files, database and WooCommerce orders', 'jetpack-my-jetpack' ),
172                    __( 'One-click restores, even from the Jetpack mobile app', 'jetpack-my-jetpack' ),
173                    __( 'Move your site to any host without a developer', 'jetpack-my-jetpack' ),
174                ),
175                'delivery'         => array(
176                    'jetpack'     => true,
177                    'plugin'      => 'jetpack-backup',
178                    'plugin_name' => __( 'Jetpack VaultPress Backup', 'jetpack-my-jetpack' ),
179                    'plugin_url'  => 'https://wordpress.org/plugins/jetpack-backup/',
180                    'free'        => false,
181                ),
182                'plans'            => array( 'security', 'pro', 'complete' ),
183                'paid_product'     => __( 'Jetpack VaultPress Backup', 'jetpack-my-jetpack' ),
184            ),
185            'blaze'             => array(
186                'info_url'         => 'https://jetpack.com/blaze/',
187                'docs_url'         => 'https://jetpack.com/support/blaze/',
188                'name'             => __( 'Blaze Ads', 'jetpack-my-jetpack' ),
189                'description'      => __( 'Put your best posts in front of new readers on WordPress.com and Tumblr.', 'jetpack-my-jetpack' ),
190                'long_description' => __( 'Turn a post or page into an ad shown across Tumblr and WordPress.com, aimed at the locations, languages and interests you choose.', 'jetpack-my-jetpack' ),
191                'icon'             => 'megaphone',
192                'admin_page'       => 'advertising',
193                'module'           => 'blaze',
194                // No plan sells Blaze, so it says how campaigns are paid for instead.
195                'pricing_notes'    => array(
196                    __( 'No subscription required', 'jetpack-my-jetpack' ),
197                    __( 'Set a budget for each campaign', 'jetpack-my-jetpack' ),
198                    __( 'Pay only for the ad views you get', 'jetpack-my-jetpack' ),
199                ),
200                'delivery'         => array(
201                    'jetpack'     => true,
202                    'plugin'      => 'blaze-ads',
203                    'plugin_name' => __( 'Blaze Ads', 'jetpack-my-jetpack' ),
204                    'plugin_url'  => 'https://wordpress.org/plugins/blaze-ads/',
205                    'free'        => false,
206                ),
207            ),
208            'boost'             => array(
209                'info_url'         => 'https://jetpack.com/boost/',
210                'docs_url'         => 'https://jetpack.com/support/jetpack-boost/',
211                'name'             => __( 'Boost', 'jetpack-my-jetpack' ),
212                'description'      => __( 'Make your site load faster in a few clicks, no developer required.', 'jetpack-my-jetpack' ),
213                'long_description' => __( 'Your pages reach visitors sooner: Boost caches them, defers non-essential JavaScript and serves resized images from a worldwide network. Mobile and desktop performance scores show you the impact of each change.', 'jetpack-my-jetpack' ),
214                'icon'             => 'trending-up',
215                'product'          => 'boost',
216                'essential'        => true,
217                'interstitial'     => '/add-boost',
218                'free_highlights'  => array(
219                    __( 'Page caching and deferred JavaScript', 'jetpack-my-jetpack' ),
220                    __( 'Resized images from a worldwide image CDN', 'jetpack-my-jetpack' ),
221                    __( 'Mobile and desktop performance scores', 'jetpack-my-jetpack' ),
222                ),
223                'paid_highlights'  => array(
224                    __( 'Critical CSS that updates automatically as your site changes', 'jetpack-my-jetpack' ),
225                    __( 'Performance history for mobile and desktop, including Core Web Vitals', 'jetpack-my-jetpack' ),
226                    __( 'Control over the quality and size of images from the image CDN', 'jetpack-my-jetpack' ),
227                ),
228                'delivery'         => array(
229                    'jetpack'     => false,
230                    'plugin'      => 'jetpack-boost',
231                    'plugin_name' => __( 'Jetpack Boost', 'jetpack-my-jetpack' ),
232                    'plugin_url'  => 'https://wordpress.org/plugins/jetpack-boost/',
233                    'free'        => true,
234                ),
235                'plans'            => array( 'pro', 'complete' ),
236                'paid_product'     => __( 'Jetpack Boost', 'jetpack-my-jetpack' ),
237            ),
238            'crm'               => array(
239                'info_url'         => 'https://jetpackcrm.com/',
240                'docs_url'         => 'https://kb.jetpackcrm.com/article-categories/getting-started/',
241                'name'             => __( 'Jetpack CRM', 'jetpack-my-jetpack' ),
242                'description'      => __( 'Keep every lead and customer in one place, right inside WordPress.', 'jetpack-my-jetpack' ),
243                'long_description' => __( 'Track leads, send quotes and invoices, and see each customer’s full history from your dashboard. Because it runs on your own site, your customer data stays yours.', 'jetpack-my-jetpack' ),
244                'icon'             => 'people',
245                'product'          => 'crm',
246                'interstitial'     => '/add-crm',
247                'free_highlights'  => array(
248                    __( 'Leads, contacts and each customer’s history', 'jetpack-my-jetpack' ),
249                    __( 'Quotes and invoices', 'jetpack-my-jetpack' ),
250                    __( 'Customer data that stays on your site', 'jetpack-my-jetpack' ),
251                ),
252                'paid_highlights'  => array(
253                    __( 'A sales dashboard, sales funnels and automations to follow up on leads', 'jetpack-my-jetpack' ),
254                    __( 'Take payments through Stripe and PayPal', 'jetpack-my-jetpack' ),
255                    __( 'Sync contacts from Gravity Forms, Contact Form 7 and Mailchimp', 'jetpack-my-jetpack' ),
256                ),
257                'delivery'         => array(
258                    'jetpack'     => false,
259                    'plugin'      => 'zero-bs-crm',
260                    'plugin_name' => __( 'Jetpack CRM', 'jetpack-my-jetpack' ),
261                    'plugin_url'  => 'https://wordpress.org/plugins/zero-bs-crm/',
262                    'free'        => true,
263                ),
264                'plans'            => array( 'pro', 'complete' ),
265                'paid_product'     => __( 'Jetpack CRM Entrepreneur', 'jetpack-my-jetpack' ),
266            ),
267            'jetpack-ai'        => array(
268                'info_url'         => 'https://jetpack.com/ai/',
269                'docs_url'         => 'https://jetpack.com/support/create-better-content-with-jetpack-ai/',
270                'name'             => __( 'Jetpack AI', 'jetpack-my-jetpack' ),
271                'description'      => __( 'Turn your ideas into ready-to-publish content without leaving the editor.', 'jetpack-my-jetpack' ),
272                'long_description' => __( 'Generate posts, tables, forms and images from a prompt, then change the tone, translate, or fix unclear sentences to make your writing easier to read. Before you publish, improve your title and create a featured image in one click.', 'jetpack-my-jetpack' ),
273                'icon'             => 'star',
274                'product'          => 'jetpack-ai',
275                'off_pages'        => array( 'jetpack-ai' ),
276                'needs_account'    => true,
277                'interstitial'     => '/add-jetpack-ai',
278                'free_highlights'  => array(
279                    /* translators: %d is the number of free requests, such as 20. */
280                    sprintf( _n( '%d request to try it out', '%d requests to try it out', Products\Jetpack_Ai::FREE_REQUESTS, 'jetpack-my-jetpack' ), Products\Jetpack_Ai::FREE_REQUESTS ),
281                    __( 'Generate, rewrite and translate in the editor', 'jetpack-my-jetpack' ),
282                ),
283                'paid_highlights'  => array(
284                    /* translators: %d is the number of free requests, such as 20. */
285                    sprintf( _n( 'Keep creating after your %d free request runs out', 'Keep creating after your %d free requests run out', Products\Jetpack_Ai::FREE_REQUESTS, 'jetpack-my-jetpack' ), Products\Jetpack_Ai::FREE_REQUESTS ),
286                    __( 'Generate a logo for your site', 'jetpack-my-jetpack' ),
287                    __( 'Priority support', 'jetpack-my-jetpack' ),
288                ),
289                'delivery'         => array(
290                    'jetpack' => true,
291                    'plugin'  => '',
292                    'free'    => true,
293                ),
294                'plans'            => array( 'pro', 'complete' ),
295                'paid_product'     => __( 'Jetpack AI Assistant', 'jetpack-my-jetpack' ),
296            ),
297            'jetpack-forms'     => array(
298                'info_url'                 => 'https://jetpack.com/forms/',
299                'docs_url'                 => 'https://jetpack.com/support/jetpack-blocks/contact-form/',
300                'name'                     => __( 'Forms', 'jetpack-my-jetpack' ),
301                'description'              => __( 'From quick contact forms to multistep surveys, everything you need is included.', 'jetpack-my-jetpack' ),
302                'long_description'         => __( 'Break long forms into steps with a progress bar, and get every response by email and in your dashboard, with no limit on responses. Upgrade to add secure file uploads that keep harmful files off your server.', 'jetpack-my-jetpack' ),
303                'offline_long_description' => __( 'Break long forms into steps with a progress bar, and get every response by email and in your dashboard, with no limit on responses. File uploads, AI, Google Sheets and push notifications need a WordPress.com connection.', 'jetpack-my-jetpack' ),
304                'icon'                     => 'list-bullets',
305                'product'                  => 'jetpack-forms',
306                'off_pages'                => array( 'jetpack-forms-responses-wp-admin', 'jetpack-forms-admin' ),
307                'essential'                => true,
308                'free_highlights'          => array(
309                    __( 'Unlimited responses, by email and in your dashboard', 'jetpack-my-jetpack' ),
310                    __( 'Multi-step forms with a progress bar', 'jetpack-my-jetpack' ),
311                ),
312                'paid_highlights'          => array(
313                    __( 'Secure file uploads that keep harmful files off your server', 'jetpack-my-jetpack' ),
314                    __( 'Visitors’ files stay private, visible only to you and your editors', 'jetpack-my-jetpack' ),
315                    __( 'Up to 10 files per field, 20 MB each', 'jetpack-my-jetpack' ),
316                ),
317                'delivery'                 => array(
318                    'jetpack' => true,
319                    'plugin'  => '',
320                    'free'    => true,
321                ),
322                'plans'                    => array( 'pro', 'complete' ),
323                // The WordPress.com site feature that gates the file upload field.
324                'paid_feature'             => 'field-file',
325            ),
326            'monetize'          => array(
327                'info_url'         => 'https://jetpack.com/growth/',
328                'docs_url'         => 'https://jetpack.com/support/features-earn/',
329                'name'             => __( 'Monetize', 'jetpack-my-jetpack' ),
330                'description'      => __( 'Earn from your site with paid subscriptions, donations, and ads.', 'jetpack-my-jetpack' ),
331                'long_description' => __( 'Sell paid subscriptions, take donations and one-time payments through Stripe, and manage your plans and paying subscribers in one place. Turn on ads to earn from your traffic too.', 'jetpack-my-jetpack' ),
332                'icon'             => 'currency-dollar',
333                'admin_page'       => 'jetpack-monetize',
334                'module'           => 'monetize',
335                'free_highlights'  => array(
336                    __( 'Payment plans for paid content and newsletters', 'jetpack-my-jetpack' ),
337                    __( 'Donations and one-time payments', 'jetpack-my-jetpack' ),
338                    __( 'A list of your paying subscribers', 'jetpack-my-jetpack' ),
339                ),
340                // No fee lines: the fee depends on the plan, and only the Monetize page can ask WordPress.com for the site's.
341                'paid_highlights'  => array(
342                    __( 'Lower fees on payments', 'jetpack-my-jetpack' ),
343                    __( 'Earn from ads shown on your site', 'jetpack-my-jetpack' ),
344                ),
345                'delivery'         => array(
346                    // The module is on offer only while the Monetize dashboard's flag is on, and never on Simple, where jetpack-mu-wpcom loads the page.
347                    'jetpack' => ! ( new Host() )->is_wpcom_simple() && in_array( 'monetize', (array) ( new \Automattic\Jetpack\Modules() )->get_available(), true ),
348                    'plugin'  => '',
349                    'free'    => true,
350                ),
351                // WordPress.com's half of the Monetize page's ads check; the bundles in `plans` cover self-hosted.
352                'paid_feature'     => ( new Host() )->is_wpcom_platform() ? 'wordads' : '',
353                'plans'            => array( 'pro', 'complete' ),
354                'also_included_in' => array( 'creator', 'growth' ),
355            ),
356            'newsletter'        => array(
357                'info_url'         => 'https://jetpack.com/newsletter/',
358                'docs_url'         => 'https://jetpack.com/support/newsletter/',
359                'name'             => __( 'Newsletter', 'jetpack-my-jetpack' ),
360                'description'      => __( 'Build your subscriber list and email your newsletter automatically.', 'jetpack-my-jetpack' ),
361                'long_description' => __( 'Every post you publish goes out by email, with no cap on subscribers or sends. New readers find you through the WordPress.com Reader, and when you’re ready, you can charge for your newsletter.', 'jetpack-my-jetpack' ),
362                'icon'             => 'envelope',
363                'product'          => 'newsletter',
364                'module'           => 'subscriptions',
365                'off_pages'        => array( 'jetpack-newsletter' ),
366                'needs_account'    => true,
367                'free_highlights'  => array(
368                    __( 'No limit on subscribers or email sends', 'jetpack-my-jetpack' ),
369                    __( 'Import up to 100 subscribers', 'jetpack-my-jetpack' ),
370                    __( 'Subscribe forms and pop-ups to collect subscribers on your site', 'jetpack-my-jetpack' ),
371                    __( 'New subscribers from the WordPress.com Reader', 'jetpack-my-jetpack' ),
372                    /* translators: %d is the percentage fee, such as 10. */
373                    sprintf( __( 'Paid newsletter with a %d%% fee on subscriptions', 'jetpack-my-jetpack' ), 10 ),
374                ),
375                'paid_highlights'  => array(
376                    __( 'Import as many subscribers as you like', 'jetpack-my-jetpack' ),
377                    /* translators: %d is the percentage fee, such as 10. */
378                    sprintf( __( 'Paid newsletter with a %d%% fee on subscriptions', 'jetpack-my-jetpack' ), 2 ),
379                ),
380                'delivery'         => array(
381                    'jetpack' => true,
382                    'plugin'  => '',
383                    'free'    => true,
384                ),
385                'plans'            => array( 'growth', 'pro', 'complete' ),
386            ),
387            'podcast'           => array(
388                'info_url'         => 'https://jetpack.com/podcast/',
389                'docs_url'         => 'https://jetpack.com/support/jetpack-podcast/',
390                'name'             => __( 'Podcast', 'jetpack-my-jetpack' ),
391                'description'      => __( 'Start a podcast on your own site and reach listeners on every major app.', 'jetpack-my-jetpack' ),
392                'long_description' => __( 'Your show lives on your own site, so the feed, the audio and your listeners stay yours, with no separate podcast service to sign up for. Submit it to Apple Podcasts, Spotify, Pocket Casts and more, and your newsletter subscribers get new episodes too.', 'jetpack-my-jetpack' ),
393                'icon'             => 'audio',
394                'admin_page'       => 'jetpack-podcast',
395                'module'           => 'podcast',
396                'free_highlights'  => array(
397                    __( 'Unlimited episodes, each one a post on your site', 'jetpack-my-jetpack' ),
398                    __( 'A podcast feed on your own domain', 'jetpack-my-jetpack' ),
399                    __( 'Submit to Apple Podcasts, Spotify and more', 'jetpack-my-jetpack' ),
400                    __( 'New episodes go out to your newsletter subscribers and the WordPress.com Reader', 'jetpack-my-jetpack' ),
401                ),
402                'paid_highlights'  => array(
403                    __( 'Listener stats by episode, app and country', 'jetpack-my-jetpack' ),
404                    __( 'An episodes dashboard for your whole back catalog', 'jetpack-my-jetpack' ),
405                    __( 'An episode player with chapters, transcripts and soundbites', 'jetpack-my-jetpack' ),
406                    __( 'Video episodes and Podcasting 2.0 support', 'jetpack-my-jetpack' ),
407                ),
408                'delivery'         => array(
409                    'jetpack' => true,
410                    'plugin'  => '',
411                    'free'    => true,
412                ),
413                'plans'            => array( 'growth', 'pro', 'complete' ),
414                // WordPress.com's half of Podcast_Gate::has_product_access(); the bundles in `plans` cover self-hosted.
415                'paid_feature'     => ( new Host() )->is_wpcom_platform() ? 'podcasting' : '',
416            ),
417            'protect-dashboard' => array(
418                'info_url'         => 'https://jetpack.com/protect/',
419                'docs_url'         => 'https://jetpack.com/support/jetpack-protect/',
420                'name'             => __( 'Protect', 'jetpack-my-jetpack' ),
421                'description'      => __( 'Get warned about vulnerable plugins and stop login attacks on your site.', 'jetpack-my-jetpack' ),
422                'long_description' => __( 'Your WordPress version, plugins and themes are checked daily against a database of known vulnerabilities, with results in your dashboard. Login attacks are blocked automatically, and you can block specific IP addresses yourself.', 'jetpack-my-jetpack' ),
423                'icon'             => 'shield',
424                'product'          => 'protect',
425                // Not `protect`: that module is Login protection, which has its own row.
426                'module'           => Products\Protect::DASHBOARD_MODULE_NAME,
427                'off_pages'        => array( 'jetpack-protect' ),
428                'essential'        => true,
429                'interstitial'     => '/add-protect',
430                'free_highlights'  => array(
431                    __( 'Daily checks for known vulnerabilities', 'jetpack-my-jetpack' ),
432                    __( 'Automatic blocking of login attacks', 'jetpack-my-jetpack' ),
433                    __( 'Block IP addresses yourself', 'jetpack-my-jetpack' ),
434                ),
435                'paid_highlights'  => array(
436                    __( 'Daily malware scanning with one-click fixes for most threats', 'jetpack-my-jetpack' ),
437                    __( 'Automatic firewall rules that block harmful requests', 'jetpack-my-jetpack' ),
438                    __( 'Instant email alerts when a threat is found', 'jetpack-my-jetpack' ),
439                ),
440                'delivery'         => array(
441                    // Never on WordPress.com Simple, whose partial bootstrap skips the module's flag.
442                    'jetpack'     => ! ( new Host() )->is_wpcom_simple() && Products\Protect::is_dashboard_module_available(),
443                    'plugin'      => 'jetpack-protect',
444                    'plugin_name' => __( 'Jetpack Protect', 'jetpack-my-jetpack' ),
445                    'plugin_url'  => 'https://wordpress.org/plugins/jetpack-protect/',
446                    'free'        => true,
447                ),
448                'plans'            => array( 'security', 'pro', 'complete' ),
449                'paid_product'     => __( 'Jetpack Scan', 'jetpack-my-jetpack' ),
450            ),
451            'search'            => array(
452                'info_url'         => 'https://jetpack.com/search/',
453                'docs_url'         => 'https://jetpack.com/support/search/',
454                'name'             => __( 'Search', 'jetpack-my-jetpack' ),
455                'description'      => __( 'Help your visitors find the right post or product as they type.', 'jetpack-my-jetpack' ),
456                'long_description' => __( 'Visitors see relevant results as they type and can filter by category, tag, date or author, so they find what they need instead of leaving. Match search to your theme without writing code.', 'jetpack-my-jetpack' ),
457                'icon'             => 'search',
458                'product'          => 'search',
459                'off_pages'        => array( 'jetpack-search' ),
460                'interstitial'     => '/add-search',
461                'setup_note'       => __( 'To start using it, choose a plan next, including a free one.', 'jetpack-my-jetpack' ),
462                'free_highlights'  => array(
463                    __( 'Up to 500 search requests per month', 'jetpack-my-jetpack' ),
464                    __( 'Results as visitors type', 'jetpack-my-jetpack' ),
465                    __( 'Filters for category, tag, date and author', 'jetpack-my-jetpack' ),
466                ),
467                'paid_highlights'  => array(
468                    __( 'Room for more records and monthly searches as your site grows', 'jetpack-my-jetpack' ),
469                    __( 'AI Answers', 'jetpack-my-jetpack' ),
470                    __( 'No Jetpack branding, plus priority support', 'jetpack-my-jetpack' ),
471                ),
472                'delivery'         => array(
473                    'jetpack'     => true,
474                    'plugin'      => 'jetpack-search',
475                    'plugin_name' => __( 'Jetpack Search', 'jetpack-my-jetpack' ),
476                    'plugin_url'  => 'https://wordpress.org/plugins/jetpack-search/',
477                    'free'        => true,
478                ),
479                'plans'            => array( 'pro', 'complete' ),
480                'paid_product'     => __( 'Jetpack Search', 'jetpack-my-jetpack' ),
481            ),
482            'social'            => array(
483                'info_url'         => 'https://jetpack.com/social/',
484                'docs_url'         => 'https://jetpack.com/support/jetpack-social/',
485                'name'             => __( 'Social', 'jetpack-my-jetpack' ),
486                'description'      => __( 'Share your posts to your social networks automatically when you publish.', 'jetpack-my-jetpack' ),
487                'long_description' => __( 'Reach your followers on Facebook, Instagram, Threads, Bluesky, LinkedIn, Mastodon, Nextdoor and Tumblr without leaving WordPress. Posts share automatically when you publish, and you can re-share or schedule them later.', 'jetpack-my-jetpack' ),
488                'icon'             => 'share',
489                'product'          => 'social',
490                'off_pages'        => array( 'jetpack-social' ),
491                'needs_account'    => true,
492                'interstitial'     => '/add-social',
493                'free_highlights'  => array(
494                    __( 'Unlimited shares', 'jetpack-my-jetpack' ),
495                    __( 'Automatic sharing when you publish', 'jetpack-my-jetpack' ),
496                    __( 'Facebook, Instagram, Threads, Bluesky, LinkedIn and more', 'jetpack-my-jetpack' ),
497                ),
498                'paid_highlights'  => array(
499                    __( 'Tailor the message and media for each network', 'jetpack-my-jetpack' ),
500                    __( 'Save reusable message templates for each account', 'jetpack-my-jetpack' ),
501                    __( 'Branded share images from Social Image Generator', 'jetpack-my-jetpack' ),
502                ),
503                'delivery'         => array(
504                    'jetpack'     => true,
505                    'plugin'      => 'jetpack-social',
506                    'plugin_name' => __( 'Jetpack Social', 'jetpack-my-jetpack' ),
507                    'plugin_url'  => 'https://wordpress.org/plugins/jetpack-social/',
508                    'free'        => true,
509                ),
510                'plans'            => array( 'growth', 'pro', 'complete' ),
511                'paid_product'     => __( 'Jetpack Social', 'jetpack-my-jetpack' ),
512            ),
513            'stats'             => array(
514                'info_url'         => 'https://jetpack.com/stats/',
515                'docs_url'         => 'https://jetpack.com/support/jetpack-stats/',
516                'name'             => __( 'Stats', 'jetpack-my-jetpack' ),
517                'description'      => __( 'See how many people visit your site and which posts they read.', 'jetpack-my-jetpack' ),
518                'long_description' => __( 'Track views, visitors, top posts, referrers and visitor countries right from your WordPress dashboard. Learn what works without seeing who your individual visitors are.', 'jetpack-my-jetpack' ),
519                'icon'             => 'chart-bar',
520                'product'          => 'stats',
521                'off_pages'        => array( 'stats' ),
522                'essential'        => true,
523                'interstitial'     => '/add-stats',
524                'free_highlights'  => array(
525                    __( 'Views, visitors and top posts', 'jetpack-my-jetpack' ),
526                    __( 'Referrers and visitor countries', 'jetpack-my-jetpack' ),
527                ),
528                'paid_highlights'  => array(
529                    __( 'UTM tracking to see which campaigns bring you traffic', 'jetpack-my-jetpack' ),
530                    __( 'Device stats to see how visitors browse your site', 'jetpack-my-jetpack' ),
531                    __( 'Region and city locations, beyond country level', 'jetpack-my-jetpack' ),
532                ),
533                'delivery'         => array(
534                    'jetpack'     => true,
535                    'plugin'      => 'jetpack-stats',
536                    'plugin_name' => __( 'Jetpack Stats', 'jetpack-my-jetpack' ),
537                    'plugin_url'  => 'https://wordpress.org/plugins/jetpack-stats/',
538                    'free'        => true,
539                ),
540                'plans'            => array( 'growth', 'pro', 'complete' ),
541                'paid_product'     => __( 'Jetpack Stats', 'jetpack-my-jetpack' ),
542            ),
543            'videopress'        => array(
544                'info_url'         => 'https://jetpack.com/videopress/',
545                'docs_url'         => 'https://jetpack.com/support/jetpack-videopress/',
546                'name'             => __( 'VideoPress', 'jetpack-my-jetpack' ),
547                'description'      => __( 'Share ad-free, high-quality video that keeps visitors on your site.', 'jetpack-my-jetpack' ),
548                'long_description' => __( 'Keep the focus on your videos with ad-free playback that fits your brand. Drag and drop videos into the WordPress editor to get started.', 'jetpack-my-jetpack' ),
549                'icon'             => 'video',
550                'product'          => 'videopress',
551                'off_pages'        => array( 'jetpack-videopress' ),
552                'interstitial'     => '/add-videopress',
553                'free_highlights'  => array(
554                    __( 'Host one video', 'jetpack-my-jetpack' ),
555                    __( 'Ad-free playback up to 4K', 'jetpack-my-jetpack' ),
556                    __( 'Match the player to your brand', 'jetpack-my-jetpack' ),
557                ),
558                'paid_highlights'  => array(
559                    __( 'Host unlimited videos', 'jetpack-my-jetpack' ),
560                    __( 'Up to 1TB of video storage', 'jetpack-my-jetpack' ),
561                ),
562                'delivery'         => array(
563                    'jetpack'     => true,
564                    'plugin'      => 'jetpack-videopress',
565                    'plugin_name' => __( 'Jetpack VideoPress', 'jetpack-my-jetpack' ),
566                    'plugin_url'  => 'https://wordpress.org/plugins/jetpack-videopress/',
567                    'free'        => true,
568                ),
569                'plans'            => array( 'pro', 'complete' ),
570                'paid_product'     => __( 'Jetpack VideoPress', 'jetpack-my-jetpack' ),
571            ),
572        );
573
574        return $definitions[ $locale ];
575    }
576
577    /**
578     * The paid bundles that include a feature, named for display.
579     *
580     * Named by the bundle products themselves, so the Features tab and the rest of My Jetpack
581     * call a plan the same thing.
582     *
583     * @param array $definition One feature's catalog entry.
584     * @param bool  $local      Skip ownership lookups.
585     * @return array List of slug/name/owned triples, without ownership in local mode.
586     */
587    private static function get_plan_badges( $definition, $local = false ) {
588        $badges = array();
589
590        foreach ( $definition['plans'] ?? array() as $slug ) {
591            $bundle_class = Products::get_product_class( $slug );
592
593            if ( $bundle_class && ( $local ? 'pro' !== $slug : ( $bundle_class::has_paid_plan_for_product() || ! empty( $bundle_class::get_pricing_for_ui()['available'] ) ) ) ) {
594                $badge = array(
595                    'slug' => $slug,
596                    'name' => $bundle_class::get_title(),
597                );
598                if ( ! $local ) {
599                    $badge['owned'] = $bundle_class::has_paid_plan_for_product();
600                }
601                $badges[] = $badge;
602            }
603        }
604
605        return $badges;
606    }
607
608    /**
609     * Lets a host send a feature's Upgrade button to its own plans instead of Jetpack's.
610     *
611     * @param array  $upgrade          The Jetpack route, from get_upgrade().
612     * @param string $slug             The feature slug.
613     * @param bool   $site_has_feature Whether the site already has the feature.
614     * @param string $back_route       The My Jetpack route the link is on, empty for the Overview.
615     * @return array{path: string, name: string, url?: string}
616     */
617    private static function filter_upgrade( array $upgrade, $slug, $site_has_feature, $back_route = '#/features' ) {
618        /**
619         * Filters where a feature's upgrade link goes, so a host can send it to its own plans.
620         *
621         * Set `url` to an absolute address to replace the My Jetpack route, which also drops the
622         * Jetpack plans the feature's details would otherwise link to; it is ignored when `$site_has_feature`.
623         *
624         * @since 6.14.0
625         * @since 6.16.0 No longer reads `manage_url`; a feature's manage link is always My Jetpack's own.
626         * @since 6.19.0 Also asked for the purchase links of the feature's product, with the Overview as `back_url`.
627         *
628         * @param array  $upgrade          `path` (a My Jetpack route) and `name` (the product sold), both empty when nothing is,
629         *                                 and `back_url`, the admin page the link is on, for a host flow to return to.
630         * @param string $slug             The My Jetpack feature slug.
631         * @param bool   $site_has_feature Whether the site already has the feature.
632         */
633        $filtered = apply_filters( 'jetpack_feature_upgrade', $upgrade + array( 'back_url' => admin_url( 'admin.php?page=my-jetpack' . $back_route ) ), $slug, $site_has_feature );
634
635        if ( ! is_array( $filtered ) ) {
636            return $upgrade;
637        }
638
639        $result = array(
640            'path' => (string) ( $filtered['path'] ?? '' ),
641            'name' => (string) ( $filtered['name'] ?? '' ),
642        );
643
644        // A site that already pays has nothing to buy, wherever a host would send it.
645        if ( ! $site_has_feature && ! empty( $filtered['url'] ) ) {
646            $result['url'] = esc_url_raw( (string) $filtered['url'] );
647        }
648
649        return $result;
650    }
651
652    /**
653     * Where a host sells the feature a product delivers, for the purchase links outside the Features tab.
654     *
655     * @since 6.19.0
656     *
657     * @param string $product_slug A My Jetpack product slug, matched to the catalog's `product` key.
658     * @return ?string Null when the host names no address, the site has the feature, or no feature is the product's.
659     */
660    public static function get_host_purchase_url( $product_slug ) {
661        // No host listening settles the answer, and spares the ownership lookups below.
662        if ( ! has_filter( 'jetpack_feature_upgrade' ) ) {
663            return null;
664        }
665
666        $slug = self::get_product_feature_slug( $product_slug );
667        if ( null === $slug ) {
668            return null;
669        }
670
671        $definition    = self::get_feature_definitions()[ $slug ];
672        $product_class = Products::get_product_class( $product_slug );
673        $included      = self::is_included_in_plan( $definition, $product_class );
674        $upgrade       = self::filter_upgrade( self::get_upgrade( $definition, $product_class, $included ), $slug, $included, '' );
675
676        return empty( $upgrade['url'] ) ? null : $upgrade['url'];
677    }
678
679    /**
680     * Whether a host could be selling the feature a product delivers, told without asking it or looking up ownership.
681     *
682     * @since 6.19.0
683     *
684     * @param string $product_slug A My Jetpack product slug.
685     * @return bool True when a host listens and the product has a feature; get_host_purchase_url() has the answer.
686     */
687    public static function may_host_sell_product( $product_slug ) {
688        return has_filter( 'jetpack_feature_upgrade' ) && null !== self::get_product_feature_slug( $product_slug );
689    }
690
691    /**
692     * The catalog feature a product delivers, which is the slug a host is asked about.
693     *
694     * @param string $product_slug A My Jetpack product slug.
695     * @return ?string The feature slug, null for a product with no feature, such as a bundle.
696     */
697    private static function get_product_feature_slug( $product_slug ) {
698        foreach ( self::get_feature_definitions() as $slug => $definition ) {
699            if ( ( $definition['product'] ?? '' ) === $product_slug ) {
700                return $slug;
701            }
702        }
703
704        return null;
705    }
706
707    /**
708     * Where the modal's Upgrade button goes: the feature's own product page, else the cheapest bundle that includes it.
709     *
710     * @param array       $definition    A single entry from the feature catalog.
711     * @param string|null $product_class The product behind the feature, when it has one.
712     * @param bool        $included      Whether the site already pays for the feature.
713     * @return array{path: string, name: string} The My Jetpack route and the product it sells, both empty when nothing does.
714     */
715    private static function get_upgrade( $definition, $product_class, $included ) {
716        $none = array(
717            'path' => '',
718            'name' => '',
719        );
720
721        // Nothing to sell a site that already pays for the feature.
722        if ( $included ) {
723            return $none;
724        }
725
726        if ( ! empty( $definition['interstitial'] ) ) {
727            return array(
728                'path' => $definition['interstitial'],
729                'name' => $definition['paid_product'] ?? '',
730            );
731        }
732
733        // Plans list the cheapest bundle first, so this sells the least a site needs to buy.
734        foreach ( $definition['plans'] ?? array() as $plan ) {
735            $bundle_class = Products::get_product_class( $plan );
736
737            if ( $bundle_class && ! empty( $bundle_class::get_pricing_for_ui()['available'] ) ) {
738                return array(
739                    'path' => '/add-' . $plan,
740                    'name' => $bundle_class::get_title(),
741                );
742            }
743        }
744
745        return $none;
746    }
747
748    /**
749     * Whether the site already pays for a feature, directly or through a bundle.
750     *
751     * @param array       $definition    A single entry from the feature catalog.
752     * @param string|null $product_class The product behind the feature, when it has one.
753     * @return bool
754     */
755    private static function is_included_in_plan( $definition, $product_class ) {
756        // An expired purchase still matches by slug; a plan-driven card offers to renew it instead.
757        $expiry_counts = ! empty( $definition['plan_driven'] );
758
759        if ( $product_class && self::pays_for_product( $product_class ) && ! ( $expiry_counts && $product_class::is_paid_plan_expired() ) ) {
760            return true;
761        }
762
763        // `also_included_in` names plans that include the feature but that the card doesn't sell.
764        foreach ( array_merge( $definition['plans'] ?? array(), $definition['also_included_in'] ?? array() ) as $plan ) {
765            $bundle_class = Products::get_product_class( $plan );
766
767            if ( $bundle_class && $bundle_class::has_paid_plan_for_product() && ! ( $expiry_counts && $bundle_class::is_paid_plan_expired() ) ) {
768                return true;
769            }
770        }
771
772        // Covers plans no bundle class lists: WordPress.com plans and the legacy Jetpack Personal, Premium and Professional.
773        return ! empty( $definition['paid_feature'] ) && Product::does_site_have_feature( $definition['paid_feature'] );
774    }
775
776    /**
777     * Whether the site pays for a product, not counting the product's own free plan.
778     *
779     * @param string $product_class The product behind the feature.
780     * @return bool
781     */
782    private static function pays_for_product( $product_class ) {
783        if ( ! $product_class::has_paid_plan_for_product() ) {
784            return false;
785        }
786
787        $free_slug = $product_class::get_wpcom_free_product_slug();
788        $purchases = Wpcom_Products::get_site_current_purchases();
789        $purchases = is_array( $purchases ) ? $purchases : array();
790        $paid      = $free_slug
791            ? array_filter( $purchases, fn( $purchase ) => false === strpos( $purchase->product_slug, $free_slug ) )
792            : $purchases;
793
794        if ( count( $paid ) === count( $purchases ) ) {
795            return true;
796        }
797
798        /*
799         * A free plan such as `jetpack_search_free` matches the paid slug by substring and gets the
800         * same site feature, so only a purchase besides it counts.
801         */
802        $paid_slugs = array_merge(
803            $product_class::get_paid_bundles_that_include_product(),
804            $product_class::get_paid_plan_product_slugs()
805        );
806
807        foreach ( $paid as $purchase ) {
808            foreach ( $paid_slugs as $paid_slug ) {
809                if ( false !== strpos( $purchase->product_slug, $paid_slug ) ) {
810                    return true;
811                }
812            }
813        }
814
815        return false;
816    }
817
818    /**
819     * The plugins the Features tab may install or switch: every standalone plugin in the map,
820     * and Jetpack itself.
821     *
822     * @return string[] WordPress.org plugin slugs.
823     */
824    public static function get_switchable_plugins() {
825        $plugins = array_filter( array_column( array_column( self::get_feature_definitions(), 'delivery' ), 'plugin' ) );
826
827        return array_values( array_unique( array_merge( array( Product::JETPACK_PLUGIN_SLUG ), $plugins ) ) );
828    }
829
830    /**
831     * Whether a plugin is missing, installed and off, or running.
832     *
833     * @param string      $slug          WordPress.org plugin slug.
834     * @param string|null $product_class The product behind the plugin, when it has one.
835     * @return string One of the PLUGIN_* constants.
836     */
837    public static function get_plugin_status( $slug, $product_class = null ) {
838        $file = self::get_plugin_file( $slug, $product_class );
839
840        if ( ! $file ) {
841            return self::PLUGIN_NOT_INSTALLED;
842        }
843
844        // A network-activated plugin is running here, whatever this site's own list says.
845        return 'inactive' === Plugins_Installer::get_plugin_status( $file )
846            ? self::PLUGIN_INACTIVE
847            : self::PLUGIN_ACTIVE;
848    }
849
850    /**
851     * Whether a plugin is forced on or off where this site's own switch can't change it.
852     *
853     * Mirrors `Jetpack_Modules_Overrides`: an `option_active_plugins` filter that adds or
854     * drops the plugin whatever the stored list says, or a network activation.
855     *
856     * @since 6.6.0
857     *
858     * @param string      $slug          WordPress.org plugin slug.
859     * @param string|null $product_class The product behind the plugin, when it has one.
860     * @return string 'active' or 'inactive' when forced, or an empty string when the owner decides.
861     */
862    public static function get_plugin_override( $slug, $product_class = null ) {
863        $file = self::get_plugin_file( $slug, $product_class );
864
865        if ( ! $file ) {
866            return '';
867        }
868
869        if ( 'network-active' === Plugins_Installer::get_plugin_status( $file ) ) {
870            return self::PLUGIN_ACTIVE;
871        }
872
873        if ( ! has_filter( 'option_active_plugins' ) ) {
874            return '';
875        }
876
877        /** This filter is documented in wp-includes/option.php */
878        if ( in_array( $file, (array) apply_filters( 'option_active_plugins', array(), 'active_plugins' ), true ) ) {
879            return self::PLUGIN_ACTIVE;
880        }
881
882        /** This filter is documented in wp-includes/option.php */
883        if ( ! in_array( $file, (array) apply_filters( 'option_active_plugins', array( $file ), 'active_plugins' ), true ) ) {
884            return self::PLUGIN_INACTIVE;
885        }
886
887        return '';
888    }
889
890    /**
891     * The installed file for a plugin, by the names its product declares where there is one.
892     *
893     * @param string      $slug          WordPress.org plugin slug.
894     * @param string|null $product_class The product behind the plugin, when it has one.
895     * @return string|false The plugin file, or false when it is not installed.
896     */
897    public static function get_plugin_file( $slug, $product_class = null ) {
898        if ( Product::JETPACK_PLUGIN_SLUG === $slug ) {
899            return Product::get_installed_plugin_filename( Product::JETPACK_PLUGIN_SLUG ) ?? false;
900        } elseif ( $product_class && $product_class::$has_standalone_plugin ) {
901            return $product_class::get_installed_plugin_filename() ?? false;
902        }
903
904        return Plugins_Installer::get_plugin_id_by_slug( $slug );
905    }
906
907    /**
908     * The plugin this copy of My Jetpack is running from.
909     *
910     * My Jetpack ships inside Jetpack, Boost, Protect, Social, Search and VideoPress, and
911     * whichever one wins the autoloader renders this page â€” so that is the plugin a switch
912     * here must never turn off.
913     *
914     * @return string The plugin's folder name, or an empty string when it is not under one.
915     */
916    public static function get_hosting_plugin_slug() {
917        return self::plugin_slug_from_path( WP_PLUGIN_DIR, __DIR__ );
918    }
919
920    /**
921     * Whether a plugin slug names the plugin this copy of My Jetpack runs from.
922     *
923     * Compared by folder rather than by slug: a checkout in `jetpack-protect-dev` serves
924     * the page just as well, and never equals the `jetpack-protect` a request carries.
925     *
926     * @param string $slug WordPress.org plugin slug.
927     * @return bool True when the plugin is the one this page is being served from.
928     */
929    public static function is_hosting_plugin( $slug ) {
930        $host = self::get_hosting_plugin_slug();
931
932        if ( '' === $host ) {
933            return false;
934        }
935
936        $file = self::get_plugin_file( $slug, self::get_product_class_for_plugin( $slug ) );
937
938        return $file ? dirname( (string) $file ) === $host : $host === $slug;
939    }
940
941    /**
942     * The plugin folder a path sits in.
943     *
944     * @param string $plugins_dir The plugin directory, as WP_PLUGIN_DIR gives it.
945     * @param string $path        The path to place.
946     * @return string The plugin's folder name, or an empty string when the path is outside.
947     */
948    public static function plugin_slug_from_path( $plugins_dir, $path ) {
949        $plugins_dir = wp_normalize_path( trailingslashit( $plugins_dir ) );
950        $path        = wp_normalize_path( $path );
951
952        if ( ! str_starts_with( $path, $plugins_dir ) ) {
953            return '';
954        }
955
956        $segments = explode( '/', substr( $path, strlen( $plugins_dir ) ) );
957
958        return $segments[0];
959    }
960
961    /**
962     * Whether no other active plugin can take over this My Jetpack page.
963     *
964     * @since 6.14.0 Added the $excluded_folders parameter.
965     * @param string   $slug             The plugin's folder name.
966     * @param string[] $excluded_folders Other plugin folders being deactivated in the same request.
967     * @return bool True when no other active plugin carries a suitable My Jetpack copy.
968     */
969    public static function is_only_my_jetpack_provider( $slug, $excluded_folders = array() ) {
970        $offline = Initializer::is_offline_features_enabled();
971        $active  = array_merge(
972            (array) get_option( 'active_plugins', array() ),
973            array_keys( (array) get_site_option( 'active_sitewide_plugins', array() ) )
974        );
975
976        foreach ( $active as $file ) {
977            $folder = dirname( (string) $file );
978
979            if ( '.' === $folder || $folder === $slug || in_array( $folder, $excluded_folders, true ) ) {
980                continue;
981            }
982
983            $package = WP_PLUGIN_DIR . '/' . $folder . '/jetpack_vendor/automattic/jetpack-my-jetpack';
984            if ( ! is_dir( $package ) ) {
985                continue;
986            }
987
988            if ( ! $offline ) {
989                return false;
990            }
991
992            // Older copies cannot keep Features open offline. Read the bundled version without loading its classes.
993            $initializer = $package . '/src/class-initializer.php';
994            if ( is_file( $initializer ) && is_readable( $initializer )
995                // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- Local bundled PHP metadata, not a remote URL.
996                && preg_match( '/\bconst\s+PACKAGE_VERSION\s*=\s*[\'\"](\d+\.\d+\.\d+(?:[.\-+][\w.\-]+)?)[\'\"]\s*;/', file_get_contents( $initializer ), $version )
997                && version_compare( $version[1], self::MINIMUM_OFFLINE_FEATURES_VERSION, '>=' ) ) {
998                return false;
999            }
1000        }
1001
1002        return true;
1003    }
1004
1005    /**
1006     * The product class behind a plugin in the map, for surfaces holding only its slug.
1007     *
1008     * @param string $slug WordPress.org plugin slug.
1009     * @return string|null The product class, or null when no product ships that plugin.
1010     */
1011    public static function get_product_class_for_plugin( $slug ) {
1012        foreach ( self::get_feature_definitions() as $definition ) {
1013            if ( ( $definition['delivery']['plugin'] ?? '' ) !== $slug || ! isset( $definition['product'] ) ) {
1014                continue;
1015            }
1016
1017            $product_class = Products::get_product_class( $definition['product'] );
1018
1019            if ( $product_class && $product_class::$has_standalone_plugin ) {
1020                return $product_class;
1021            }
1022        }
1023
1024        return null;
1025    }
1026
1027    /**
1028     * Headings for the modules the feature list does not cover.
1029     *
1030     * Grouped by the job a site owner is doing, not by Jetpack's module tags, which describe
1031     * mechanism (the Image CDN is tagged Appearance). Anything unlisted falls into Other.
1032     *
1033     * @return array Groups, each with a label and its module slugs. The UI sorts both by name.
1034     */
1035    public static function get_module_groups() {
1036        return array(
1037            array(
1038                'label'   => __( 'Protect', 'jetpack-my-jetpack' ),
1039                // The filter pills these modules also show under, like a feature's `groups`.
1040                'groups'  => array( 'security' ),
1041                'modules' => array( 'account-protection', 'monitor', 'protect', 'sso', 'waf' ),
1042            ),
1043            array(
1044                'label'   => __( 'Performance', 'jetpack-my-jetpack' ),
1045                'modules' => array( 'photon', 'photon-cdn' ),
1046            ),
1047            array(
1048                'label'   => __( 'Search engines', 'jetpack-my-jetpack' ),
1049                'modules' => array( 'sitemaps', 'seo-tools', 'canonical-urls', 'verification-tools' ),
1050            ),
1051            array(
1052                'label'   => __( 'Engagement', 'jetpack-my-jetpack' ),
1053                'modules' => array(
1054                    'comments',
1055                    'likes',
1056                    'comment-likes',
1057                    'gravatar-hovercards',
1058                    'related-posts',
1059                    'sharedaddy',
1060                    'shortlinks',
1061                ),
1062            ),
1063            array(
1064                'label'   => __( 'Writing', 'jetpack-my-jetpack' ),
1065                'modules' => array(
1066                    'blocks',
1067                    'markdown',
1068                    'latex',
1069                    'shortcodes',
1070                    'copy-post',
1071                    'custom-content-types',
1072                    'post-by-email',
1073                    'post-list',
1074                    'carousel',
1075                    'tiled-gallery',
1076                ),
1077            ),
1078            array(
1079                'label'   => __( 'Design', 'jetpack-my-jetpack' ),
1080                'modules' => array( 'google-fonts', 'widgets', 'widget-visibility' ),
1081            ),
1082            array(
1083                'label'   => __( 'Earn', 'jetpack-my-jetpack' ),
1084                'modules' => array( 'wordads' ),
1085            ),
1086            array(
1087                'label'    => __( 'Analytics', 'jetpack-my-jetpack' ),
1088                'modules'  => array( 'woocommerce-analytics' ),
1089                // Settings-only entries listed under this heading when the site has them; see get_settings_entries().
1090                'settings' => array( Google_Analytics_Settings::SLUG ),
1091            ),
1092        );
1093    }
1094
1095    /**
1096     * Slugs the host has hidden, plus the module behind each hidden feature or product.
1097     *
1098     * @since 6.11.0
1099     * @return string[] Hidden module and feature slugs.
1100     */
1101    public static function get_hidden_modules() {
1102        $hidden = Feature_Visibility::get_hidden();
1103        foreach ( self::get_feature_definitions() as $slug => $definition ) {
1104            $names = array_filter( array( $slug, $definition['product'] ?? '', $definition['module'] ?? '' ) );
1105            if ( array_intersect( $names, $hidden ) ) {
1106                $product_class = isset( $definition['product'] ) ? Products::get_product_class( $definition['product'] ) : null;
1107                $hidden[]      = $definition['module'] ?? ( $product_class ? $product_class::get_module_name() : '' );
1108            }
1109        }
1110        return array_values( array_unique( array_filter( $hidden ) ) );
1111    }
1112
1113    /**
1114     * Everything the Features tab renders from: the Jetpack plugin's status, each feature, the
1115     * headings for Jetpack's other modules, and whether the current user may install plugins.
1116     *
1117     * @param bool $local Use the local feature contract documented in get_features().
1118     * @return array{jetpack: string, features: array, module_groups: array, plugin_installs: string, hidden_modules: string[], settings_entries: array} The state.
1119     */
1120    public static function get_state( $local = false ) {
1121        return array(
1122            'jetpack'          => self::get_plugin_status( Product::JETPACK_PLUGIN_SLUG ),
1123            'features'         => self::get_features( $local ),
1124            'module_groups'    => self::get_module_groups(),
1125            'plugin_installs'  => self::get_install_access(),
1126            'hidden_modules'   => self::get_hidden_modules(),
1127            'settings_entries' => self::get_settings_entries(),
1128        );
1129    }
1130
1131    /**
1132     * Settings that have no module but open in the settings panel, for the current user on this site.
1133     *
1134     * @since 6.18.0
1135     *
1136     * @return array[] Entries with `slug`, `name`, `description` and `status` (`active`, `inactive` or `needs-setup`) keys.
1137     */
1138    public static function get_settings_entries() {
1139        return Google_Analytics_Settings::is_available() ? array( Google_Analytics_Settings::get_entry() ) : array();
1140    }
1141
1142    /**
1143     * Whether the current user may install plugins here, and if not, why not.
1144     *
1145     * @since 6.7.0
1146     *
1147     * @return string One of the INSTALLS_* constants.
1148     */
1149    public static function get_install_access() {
1150        if ( current_user_can( 'install_plugins' ) ) {
1151            return self::INSTALLS_ALLOWED;
1152        }
1153
1154        // Core's own map_meta_cap() check for install_plugins: DISALLOW_FILE_MODS or the file_mod_allowed filter.
1155        if ( ! wp_is_file_mod_allowed( 'capability_update_core' ) ) {
1156            return self::INSTALLS_DISABLED;
1157        }
1158
1159        return self::INSTALLS_NOT_PERMITTED;
1160    }
1161
1162    /**
1163     * The feature catalog merged with each feature's live state, sorted by name.
1164     *
1165     * @param bool $local Keep only local management URLs, set upgrade to null, use any offline
1166     *                    long description, and omit included, setup_note, and plan owned fields
1167     *                    without resolving ownership.
1168     * @return array List of features, each with slug, name, description, icon, manage_url,
1169     *               the plugin it ships as and the product/module join keys.
1170     */
1171    public static function get_features( $local = false ) {
1172        $features = array();
1173        $hidden   = Feature_Visibility::get_hidden();
1174
1175        foreach ( self::get_feature_definitions() as $slug => $definition ) {
1176            // A host can name the feature by its own slug or by its product's or module's.
1177            $names = array_filter( array( $slug, $definition['product'] ?? '', $definition['module'] ?? '' ) );
1178            if ( array_intersect( $names, $hidden ) ) {
1179                continue;
1180            }
1181
1182            $delivery = $definition['delivery'] ?? array();
1183            $plugin   = $delivery['plugin'] ?? '';
1184            // Any feature neither Jetpack nor a plugin delivers on this site has no card, such as one whose module is behind a flag.
1185            if ( empty( $delivery['jetpack'] ) && '' === $plugin ) {
1186                continue;
1187            }
1188
1189            $product_class = isset( $definition['product'] ) ? Products::get_product_class( $definition['product'] ) : null;
1190            $included      = ! $local && self::is_included_in_plan( $definition, $product_class );
1191            $upgrade       = $local ? null : self::filter_upgrade( self::get_upgrade( $definition, $product_class, $included ), $slug, $included );
1192
1193            $feature = array(
1194                'slug'             => $slug,
1195                'name'             => $definition['name'],
1196                'description'      => $definition['description'],
1197                'long_description' => $local && isset( $definition['offline_long_description'] ) ? $definition['offline_long_description'] : ( $definition['long_description'] ?? '' ),
1198                'icon'             => $definition['icon'],
1199                'manage_url'       => self::get_feature_manage_url( $definition, $product_class, $local ),
1200                'essential'        => ! empty( $definition['essential'] ),
1201                // An older Jetpack plugin still lets the module decide, so the card keeps its switch there.
1202                'plan_driven'      => ! empty( $definition['plan_driven'] ) && ( ! $product_class || ! method_exists( $product_class, 'is_plan_driven' ) || $product_class::is_plan_driven() ),
1203                'in_jetpack'       => $delivery['jetpack'] ?? false,
1204                'plugin'           => $plugin,
1205                'plugin_name'      => $plugin ? ( $delivery['plugin_name'] ?? $definition['name'] ) : '',
1206                'plugin_url'       => $plugin ? ( $delivery['plugin_url'] ?? '' ) : '',
1207                'plugin_status'    => $plugin ? self::get_plugin_status( $plugin, $product_class ) : self::PLUGIN_NOT_INSTALLED,
1208                'plugin_override'  => $plugin ? self::get_plugin_override( $plugin, $product_class ) : '',
1209                'free_highlights'  => $definition['free_highlights'] ?? array(),
1210                'paid_highlights'  => $definition['paid_highlights'] ?? array(),
1211                'pricing_notes'    => $definition['pricing_notes'] ?? array(),
1212                'upgrade'          => $upgrade,
1213                'included'         => $included,
1214                // Both notes tell the site what to buy next, which a site that pays already has.
1215                'setup_note'       => $included ? '' : ( $definition['setup_note'] ?? '' ),
1216                'screenshot'       => self::get_screenshot_url( $slug ),
1217                'plans'            => self::get_plan_badges( $definition, $local ),
1218                // The filter pills' categories, kept when those bundles go off sale and drop out of `plans`.
1219                'groups'           => array_values( array_intersect( array_merge( $definition['plans'] ?? array(), $definition['also_included_in'] ?? array() ), self::FILTER_GROUPS ) ),
1220                'info_url'         => $definition['info_url'],
1221                'docs_url'         => $definition['docs_url'],
1222                // Join keys: the UI reads live state from the module and plugin it names,
1223                // so the catalog never ships a status of its own.
1224                'product'          => $definition['product'] ?? '',
1225                'module'           => $definition['module'] ?? '',
1226                // Read even without Jetpack, whose module list would otherwise say so.
1227                'needs_account'    => ! empty( $definition['needs_account'] ),
1228            );
1229            if ( 'activity-log' === $slug && ! $local && true === ( Products\Pro::get_pricing_for_ui()['catalog_available'] ?? false ) ) {
1230                $feature['paid_highlights'][0] = __( '30 days of history with VaultPress Backup or Jetpack Pro, a full year with Jetpack Complete', 'jetpack-my-jetpack' );
1231            } elseif ( 'activity-log' === $slug && ! $local && false === ( Products\Security::get_pricing_for_ui()['available'] ?? null ) ) {
1232                $feature['paid_highlights'][0] = __( '30 days of history with VaultPress Backup, a full year with Jetpack Complete', 'jetpack-my-jetpack' );
1233            }
1234            if ( $local ) {
1235                unset( $feature['included'], $feature['setup_note'] );
1236            }
1237            $features[] = $feature;
1238        }
1239
1240        usort(
1241            $features,
1242            function ( $a, $b ) {
1243                return strnatcasecmp( $a['name'], $b['name'] );
1244            }
1245        );
1246
1247        return $features;
1248    }
1249
1250    /**
1251     * The feature's artwork, bundled with this package so the details window needs no third-party request.
1252     *
1253     * @param string $slug The feature's catalog slug, which names its image.
1254     * @return string URL of the image.
1255     */
1256    private static function get_screenshot_url( $slug ) {
1257        return Initializer::get_assets_url() . 'components/my-jetpack-tab-panel/features/images/' . $slug . '.webp';
1258    }
1259
1260    /**
1261     * Where an active feature lives.
1262     *
1263     * @param array       $definition    A single entry from the feature catalog.
1264     * @param string|null $product_class The product behind the feature, when it has one.
1265     * @param bool        $local         Keep local destinations without resolving ownership.
1266     * @return string Admin URL, or an empty string when the feature has nowhere to go.
1267     */
1268    private static function get_feature_manage_url( array $definition, $product_class = null, $local = false ) {
1269        if ( isset( $definition['admin_page'] ) ) {
1270            return admin_url( 'admin.php?page=' . $definition['admin_page'] );
1271        }
1272
1273        if ( $product_class ) {
1274            // Protect's normal destination depends on ownership, which is unknown offline.
1275            if ( $local && 'protect' === ( $definition['product'] ?? '' ) ) {
1276                return Products\Protect::has_protect_page() ? admin_url( 'admin.php?page=jetpack-protect' ) : '';
1277            }
1278
1279            $url = (string) $product_class::get_manage_url();
1280            if ( ! $local ) {
1281                return $url;
1282            }
1283
1284            wp_parse_str( (string) wp_parse_url( $url, PHP_URL_QUERY ), $query );
1285            return 0 === strpos( $url, admin_url() ) && 'my-jetpack' !== ( $query['page'] ?? '' ) ? $url : '';
1286        }
1287
1288        return '';
1289    }
1290}