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