Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.24% covered (warning)
50.24%
106 / 211
35.00% covered (danger)
35.00%
7 / 20
CRAP
0.00% covered (danger)
0.00%
0 / 1
Settings
50.24% covered (warning)
50.24%
106 / 211
35.00% covered (danger)
35.00%
7 / 20
457.38
0.00% covered (danger)
0.00%
0 / 1
 register_feature_flags
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 init
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 is_subscriptions_active
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 should_show_menu_item
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 init_hooks
80.00% covered (warning)
80.00%
12 / 15
0.00% covered (danger)
0.00%
0 / 1
3.07
 redirect_retired_subscribers_page
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
4.13
 maybe_load_wp_build
33.33% covered (danger)
33.33%
2 / 6
0.00% covered (danger)
0.00%
0 / 1
8.74
 add_wp_admin_menu
68.97% covered (warning)
68.97%
20 / 29
0.00% covered (danger)
0.00%
0 / 1
12.99
 add_wp_admin_submenu
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
56
 admin_init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 add_script_data
100.00% covered (success)
100.00%
26 / 26
100.00% covered (success)
100.00%
1 / 1
3
 load_admin_scripts
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
4
 get_subscriber_management_url
16.67% covered (danger)
16.67%
2 / 12
0.00% covered (danger)
0.00%
0 / 1
19.47
 render
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 add_reading_page_notice
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 render_reading_page_notice
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
2
 load_wp_build
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
6
 alias_screen_id_for_wp_build
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 is_modernized
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 is_newsletter_admin_request
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
1<?php
2/**
3 * A class that adds a newsletter settings screen to wp-admin.
4 *
5 * @package automattic/jetpack-newsletter
6 */
7
8namespace Automattic\Jetpack\Newsletter;
9
10use Automattic\Jetpack\Admin_UI\Admin_Menu;
11use Automattic\Jetpack\Assets;
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13use Automattic\Jetpack\Feature_Flags\Feature_Flags;
14use Automattic\Jetpack\Modules;
15use Automattic\Jetpack\Redirect;
16use Automattic\Jetpack\Status;
17use Automattic\Jetpack\Status\Host;
18use Jetpack_Tracks_Client;
19
20/**
21 * A class responsible for adding a newsletter settings screen to wp-admin.
22 */
23class Settings {
24
25    const PACKAGE_VERSION = '0.14.1';
26
27    const ADMIN_PAGE_SLUG = 'jetpack-newsletter';
28
29    /**
30     * Slug of the retired Subscribers page, kept only to redirect stale bookmarks.
31     */
32    const RETIRED_SUBSCRIBERS_PAGE_SLUG = 'jetpack-subscribers';
33
34    /**
35     * Filter name that gates the wp-build–based dashboard.
36     *
37     * When this filter returns true, "Jetpack > Newsletter" renders the new
38     * wp-build dashboard instead of the legacy Newsletter Settings React app.
39     */
40    const MODERNIZATION_FILTER = 'rsm_jetpack_ui_modernization_newsletter';
41
42    /**
43     * Feature flag for the Newsletter Overview tab.
44     */
45    const OVERVIEW_FEATURE_FLAG = 'newsletter-overview';
46
47    /**
48     * Whether the class has been initialized
49     *
50     * @var boolean
51     */
52    private static $initialized = false;
53
54    /**
55     * Register Newsletter feature flags.
56     *
57     * @return void
58     */
59    public static function register_feature_flags() {
60        Feature_Flags::register(
61            self::OVERVIEW_FEATURE_FLAG,
62            array(
63                'default'     => false,
64                'description' => 'Enable the Newsletter Overview tab.',
65                'owner'       => 'jetpack-newsletter',
66            )
67        );
68    }
69
70    /**
71     * Init Newsletter Settings if it wasn't already.
72     */
73    public static function init() {
74        self::register_feature_flags();
75
76        if ( ! self::$initialized ) {
77            self::$initialized = true;
78            ( new self() )->init_hooks();
79        }
80    }
81
82    /**
83     * Check if the subscriptions module is active.
84     *
85     * @return bool
86     */
87    private function is_subscriptions_active() {
88        return ( new Modules() )->is_active( 'subscriptions' );
89    }
90
91    /**
92     * Determine whether to show the Newsletter menu item.
93     * When true, shown regardless of subscriptions module state.
94     *
95     * @return bool
96     */
97    private function should_show_menu_item() {
98        /**
99         * Filter to control Newsletter menu item visibility.
100         * Defaults to true.
101         *
102         * @since 0.6.0
103         * @param bool $show Whether to show the menu item.
104         */
105        return apply_filters(
106            'jetpack_show_newsletter_menu_item',
107            true
108        );
109    }
110
111    /**
112     * Subscribe to necessary hooks.
113     */
114    public function init_hooks() {
115        // Priority 1 so this runs before the menu is built and the request is denied.
116        add_action( 'admin_menu', array( __CLASS__, 'redirect_retired_subscribers_page' ), 1 );
117
118        // Add the Reading settings notice as long as subscriptions are active.
119        if ( $this->is_subscriptions_active() ) {
120            add_action( 'admin_init', array( $this, 'add_reading_page_notice' ) );
121        }
122
123        // Hijack the config URLs to point to our settings page.
124        // Priority 20 to override the default URL set in subscriptions.php.
125        add_filter(
126            'jetpack_module_configuration_url_subscriptions',
127            function () {
128                return Urls::get_newsletter_settings_url();
129            },
130            20
131        );
132
133        // Defer wp-build loading to admin_menu (priority 1) on every host. The
134        // modernization filter â€” which third parties typically register from a
135        // plugins_loaded callback â€” needs to have been applied before we read it,
136        // and the wp-build render function needs to be defined before any menu
137        // callback runs (priority 999 on standalone Jetpack, priority 999999 on
138        // wpcom Simple via wpcom-admin-menu.php's call to add_wp_admin_submenu).
139        // Settings::init() runs synchronously from load-jetpack.php at
140        // plugin-file-include time â€” before any plugins_loaded callback fires â€”
141        // so an inline check here would always see the unfiltered default.
142        add_action( 'admin_menu', array( __CLASS__, 'maybe_load_wp_build' ), 1 );
143
144        $host = new Host();
145
146        // On wpcom Simple, the Jetpack menu is created at priority 999999 by wpcom-admin-menu.php,
147        // which will call add_wp_admin_submenu() directly. Skip adding the menu here to avoid
148        // trying to add a submenu before the parent menu exists.
149        if ( $host->is_wpcom_simple() ) {
150            return;
151        }
152
153        // Add admin menu item.
154        // Use priority 999 to ensure menu items are queued BEFORE Admin_Menu::admin_menu_hook_callback
155        // runs at priority 1000 to process all queued items.
156        add_action( 'admin_menu', array( $this, 'add_wp_admin_menu' ), 999 );
157    }
158
159    /**
160     * Send the retired Subscribers page to Newsletter, which absorbed it.
161     *
162     * The slug was live for about three months, so it is still in browser histories,
163     * where it would otherwise hit WordPress's generic "not allowed to access this
164     * page" and read as a permissions error rather than a move.
165     *
166     * @return void
167     */
168    public static function redirect_retired_subscribers_page() {
169        // phpcs:ignore WordPress.Security.NonceVerification.Recommended
170        $page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
171
172        if ( self::RETIRED_SUBSCRIBERS_PAGE_SLUG !== $page || ! current_user_can( 'manage_options' ) ) {
173            return;
174        }
175
176        wp_safe_redirect( admin_url( 'admin.php?page=' . self::ADMIN_PAGE_SLUG ) );
177        exit( 0 );
178    }
179
180    /**
181     * Load wp-build for the Newsletter admin page when modernization is enabled.
182     *
183     * Hooked to `admin_menu` priority 1 so the modernization filter has been
184     * registered by any opt-in code (mu-plugins, snippets, themes) before we
185     * read it, and so the wp-build render function and enqueue hook are in
186     * place before `add_wp_admin_menu` runs at priority 999.
187     *
188     * @return void
189     */
190    public static function maybe_load_wp_build() {
191        if ( ! self::is_modernized() || ! self::is_newsletter_admin_request() ) {
192            return;
193        }
194
195        self::load_wp_build();
196
197        // wp-build registers standalone modules (e.g. the init module) on
198        // wp_default_scripts, which has already fired by admin_menu. Register them
199        // directly so the init module makes it into the import map.
200        if ( function_exists( 'jetpack_newsletter_register_script_modules' ) ) {
201            jetpack_newsletter_register_script_modules(); // @phan-suppress-current-line PhanUndeclaredFunction -- Checked with function_exists(); defined in the generated build/modules.php, which Phan excludes.
202        }
203
204        add_action( 'current_screen', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
205    }
206
207    /**
208     * Add the newsletter settings submenu to the Jetpack menu.
209     *
210     * Note: This method is NOT called on wpcom Simple sites. Simple sites use
211     * add_wp_admin_submenu() called from wpcom-admin-menu.php instead.
212     */
213    public function add_wp_admin_menu() {
214        // On sites using Jetpack, only show the menu if the site is connected.
215        if ( ! ( new Connection_Manager() )->is_connected() ) {
216            return;
217        }
218
219        // On the modernized dashboard, the Newsletter screen is only useful when the
220        // subscriptions module is active, so skip registering the menu entirely when it
221        // is off. Gated on the modernization flag to leave legacy behavior unchanged.
222        if ( self::is_modernized() && ! $this->is_subscriptions_active() ) {
223            return;
224        }
225
226        $host = new Host();
227
228        // should_show_menu_item() controls visibility of the menu item.
229        $show_menu   = $this->should_show_menu_item();
230        $parent_slug = $show_menu ? 'jetpack' : '';
231
232        // On Atomic, use add_submenu_page. On standalone Jetpack, use Admin_Menu when showing in menu.
233        $use_jetpack_menu = ! $host->is_woa_site() && $show_menu;
234
235        $callback = self::is_modernized() && function_exists( 'jetpack_newsletter_jetpack_newsletter_dashboard_wp_admin_render_page' )
236            ? 'jetpack_newsletter_jetpack_newsletter_dashboard_wp_admin_render_page'
237            : array( $this, 'render' );
238
239        // Register menu item.
240        if ( $use_jetpack_menu ) {
241            $page_suffix = Admin_Menu::add_menu(
242                /** "Newsletter" is a product name, do not translate. */
243                'Newsletter',
244                'Newsletter',
245                'manage_options',
246                'jetpack-newsletter',
247                $callback
248            );
249        } else {
250            $page_suffix = add_submenu_page(
251                $parent_slug,
252                /** "Newsletter" is a product name, do not translate. */
253                'Newsletter',
254                'Newsletter',
255                'manage_options',
256                'jetpack-newsletter',
257                $callback
258            );
259        }
260
261        if ( $page_suffix ) {
262            add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
263        }
264    }
265
266    /**
267     * Add the newsletter settings submenu directly under the Jetpack menu.
268     *
269     * This method is called from wpcom-admin-menu.php on Simple sites at late priority
270     * (999999) when the Jetpack menu already exists.
271     */
272    public function add_wp_admin_submenu() {
273        // On the modernized dashboard, the Newsletter screen is only useful when the
274        // subscriptions module is active, so skip registering the menu entirely when it
275        // is off. Gated on the modernization flag to leave legacy behavior unchanged.
276        if ( self::is_modernized() && ! $this->is_subscriptions_active() ) {
277            return;
278        }
279
280        $parent_slug = $this->should_show_menu_item() ? 'jetpack' : '';
281        $callback    = self::is_modernized() && function_exists( 'jetpack_newsletter_jetpack_newsletter_dashboard_wp_admin_render_page' )
282            ? 'jetpack_newsletter_jetpack_newsletter_dashboard_wp_admin_render_page'
283            : array( $this, 'render' );
284        $page_suffix = add_submenu_page(
285            $parent_slug,
286            /** "Newsletter" is a product name, do not translate. */
287            'Newsletter',
288            'Newsletter',
289            'manage_options',
290            'jetpack-newsletter',
291            $callback
292        );
293
294        if ( $page_suffix ) {
295            add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
296        }
297    }
298
299    /**
300     * Admin init actions.
301     */
302    public function admin_init() {
303        add_filter( 'jetpack_admin_js_script_data', array( $this, 'add_script_data' ) );
304        add_action( 'admin_enqueue_scripts', array( $this, 'load_admin_scripts' ) );
305    }
306
307    /**
308     * Add newsletter-specific data to the global JetpackScriptData object.
309     *
310     * @param array $data The existing script data.
311     * @return array The modified script data.
312     */
313    public function add_script_data( $data ) {
314        $current_user = wp_get_current_user();
315        $theme        = wp_get_theme();
316
317        $host                   = new Host();
318        $status                 = new Status();
319        $site_suffix            = $status->get_site_suffix();
320        $blog_id                = (int) $host->get_wpcom_site_id();
321        $is_wpcom               = $host->is_wpcom_platform();
322        $is_block_theme         = wp_is_block_theme();
323        $setup_payment_plan_url = ( $is_wpcom ? 'https://wordpress.com/earn/payments/' : 'https://cloud.jetpack.com/monetize/payments/' ) . $site_suffix;
324
325        $wp_admin_subscriber_management_enabled = apply_filters( 'jetpack_wp_admin_subscriber_management_enabled', true );
326
327        // Populate blog_id which is needed for API calls on Simple sites.
328        $data['site']['wpcom']['blog_id'] = $blog_id;
329
330        // Add newsletter-specific data.
331        // Note: Common data like admin_url, rest_nonce, rest_root, title, is_wpcom_platform,
332        // and user.current_user.display_name are already provided by Script_Data.
333        $data['newsletter'] = array(
334            'isBlockTheme'                    => $is_block_theme,
335            'themeStylesheet'                 => $theme->get_stylesheet(),
336            'email'                           => $current_user->user_email,
337            'gravatar'                        => get_avatar_url( $current_user->ID ),
338            'dateExample'                     => gmdate( get_option( 'date_format' ), time() ),
339            'subscriberManagementUrl'         => $this->get_subscriber_management_url( $wp_admin_subscriber_management_enabled, $is_wpcom, $site_suffix, $blog_id ),
340            'subscriberManagementEnabled'     => (bool) $wp_admin_subscriber_management_enabled,
341            'overviewEnabled'                 => Feature_Flags::is_enabled( self::OVERVIEW_FEATURE_FLAG ),
342            'isSubscriptionSiteEditSupported' => $is_block_theme,
343            'setupPaymentPlansUrl'            => $setup_payment_plan_url,
344            'isSitePublic'                    => ! $status->is_private_site() && ! $status->is_coming_soon(),
345            'tracksUserData'                  => Jetpack_Tracks_Client::get_connected_user_tracks_identity(),
346        );
347
348        return $data;
349    }
350
351    /**
352     * Load the admin scripts.
353     */
354    public function load_admin_scripts() {
355        // This callback is registered via `admin_enqueue_scripts` from `admin_init`,
356        // which itself fires on `load-{$page_suffix}` in `add_wp_admin_menu()` â€” so it
357        // only fires on the Newsletter admin page; no need to re-check the page here.
358        // The Tracks transport is required on both surfaces â€” `analytics.initialize`
359        // only queues events into `window._tkq`; without `jp-tracks` loaded, no
360        // pixel.gif requests fire and the queue grows forever.
361        wp_enqueue_script( 'jp-tracks', '//stats.wp.com/w.js', array(), gmdate( 'YW' ), true );
362
363        if ( self::is_modernized() ) {
364            // The i18n loader is registered on every admin page by jetpack-assets but
365            // only enqueued when depended on; the esbuild bundles don't pull it in.
366            // Enqueue it so the wp-build dashboard's init module can download its JS
367            // translation catalogs.
368            if ( wp_script_is( 'wp-jp-i18n-loader', 'registered' ) ) {
369                wp_enqueue_script( 'wp-jp-i18n-loader' );
370            }
371
372            // wp-build manages the rest of its enqueue pipeline. The legacy
373            // newsletter script and JetpackScriptData are intentionally skipped
374            // for the wp-build dashboard.
375            return;
376        }
377
378        // The legacy bundle imports `@wordpress/ui`, which reaches
379        // `@wordpress/theme` and so lists `wp-theme` in its generated asset file.
380        // Core does not register that handle, and `load_wp_build()` â€” the only
381        // other caller â€” runs on the modernized path alone. Without this,
382        // WordPress silently drops the script over the unregistered dependency
383        // and the page renders blank. Request only the handles this bundle needs:
384        // `wp-theme` (Core never registers it), `wp-private-apis` (so the polyfill
385        // can replace Core's incomplete allowlist on older WP) and `wp-rich-text`
386        // (`@wordpress/ui` also reaches `@wordpress/dataviews`, whose dataform
387        // controls unlock rich-text's `privateApis` at module scope; WP 6.9 exports
388        // none, which throws "Cannot unlock an undefined object"). We leave out
389        // `wp-notices` so the polyfill's force-replacement never touches it.
390        if ( class_exists( \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::class ) ) {
391            \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::register(
392                'jetpack-newsletter',
393                array( 'wp-theme', 'wp-private-apis', 'wp-rich-text' )
394            );
395        }
396
397        Assets::register_script(
398            'jetpack-newsletter',
399            '../build/newsletter.js',
400            __FILE__,
401            array(
402                'in_footer'    => true,
403                'textdomain'   => 'jetpack-newsletter',
404                'enqueue'      => true,
405                'dependencies' => array( 'jetpack-script-data' ),
406            )
407        );
408    }
409
410    /**
411     * Get the subscriber management URL based on site type and filter settings.
412     *
413     * - If jetpack_wp_admin_subscriber_management_enabled filter is true: wp-admin subscribers page
414     * - If filter is false AND wpcom site: wordpress.com/subscribers/$domain
415     * - If filter is false AND Jetpack site: jetpack.com redirect URL
416     *
417     * @param bool   $wp_admin_enabled Whether wp-admin subscriber management is enabled.
418     * @param bool   $is_wpcom         Whether this is a WordPress.com site.
419     * @param string $site_suffix      The Calypso site suffix (home host, slashes as `::`).
420     * @param int    $blog_id          The blog ID.
421     * @return string The subscriber management URL.
422     */
423    private function get_subscriber_management_url( $wp_admin_enabled, $is_wpcom, $site_suffix, $blog_id ) {
424        // If wp-admin subscriber management is enabled, use the wp-admin page.
425        if ( $wp_admin_enabled ) {
426            return admin_url( 'admin.php?page=subscribers' );
427        }
428
429        // For wpcom sites, use the wordpress.com URL.
430        if ( $is_wpcom ) {
431            return 'https://wordpress.com/subscribers/' . $site_suffix;
432        }
433
434        // For Jetpack sites, use the jetpack.com redirect URL.
435        $site_id = $blog_id ? (int) $blog_id : Connection_Manager::get_site_id( true );
436        $args    = ( ! empty( $site_id ) )
437            ? array( 'site' => $site_id )
438            : array();
439
440        return Redirect::get_url(
441            'jetpack-settings-jetpack-manage-subscribers',
442            $args
443        );
444    }
445
446    /**
447     * Render the newsletter settings page.
448     */
449    public function render() {
450        ?>
451        <div id="newsletter-settings-root"></div>
452        <?php
453    }
454
455    /**
456     * Register a notice on the Reading settings page to clarify that the RSS
457     * excerpt setting does not control newsletter emails.
458     *
459     * @since 0.5.1
460     */
461    public function add_reading_page_notice() {
462        add_settings_field(
463            'jetpack_newsletter_reading_notice',
464            '',
465            array( $this, 'render_reading_page_notice' ),
466            'reading',
467            'default'
468        );
469    }
470
471    /**
472     * Render the clarifying notice on the Reading settings page.
473     *
474     * Uses JavaScript to relocate the notice next to the "For each post in a feed"
475     * (rss_use_excerpt) setting.
476     *
477     * @since 0.5.1
478     */
479    public function render_reading_page_notice() {
480        $newsletter_url = Urls::get_newsletter_settings_url();
481
482        printf(
483            '<p class="description" id="jetpack-newsletter-reading-notice">%s</p>',
484            sprintf(
485                wp_kses(
486                    /* translators: %s is a link to the Newsletter settings page. */
487                    __( 'To control what’s included in newsletter emails, visit your <a href="%s">Newsletter settings</a>.', 'jetpack-newsletter' ),
488                    array(
489                        'a' => array(
490                            'href' => array(),
491                        ),
492                    )
493                ),
494                esc_url( $newsletter_url )
495            )
496        );
497        ?>
498        <script type="text/javascript">
499            document.addEventListener( 'DOMContentLoaded', function() {
500                var notice = document.getElementById( 'jetpack-newsletter-reading-notice' );
501                var excerptInput = document.querySelector( 'input[name="rss_use_excerpt"]' );
502                var excerptRow = excerptInput ? excerptInput.closest( 'tr' ) : null;
503
504                if ( ! notice || ! excerptRow ) {
505                    return;
506                }
507
508                // Remember the original parent before moving the notice.
509                var originalTable = notice.closest( 'table' );
510                var excerptTable = excerptRow.closest( 'table' );
511
512                // Move the notice into the rss_use_excerpt row's fieldset.
513                excerptRow.querySelector( 'td' ).appendChild( notice );
514
515                // Remove the now-empty original table (if it's different from the excerpt's table).
516                if ( originalTable && originalTable !== excerptTable ) {
517                    originalTable.remove();
518                }
519            } );
520        </script>
521        <?php
522    }
523
524    /**
525     * Load the wp-build entry file and register its polyfills.
526     *
527     * Only called on `?page=jetpack-newsletter` admin requests when the
528     * modernization filter is enabled. Keeps wp-build off every other request.
529     *
530     * @return void
531     */
532    private static function load_wp_build() {
533        $build_index = dirname( __DIR__ ) . '/build/build.php';
534
535        if ( ! file_exists( $build_index ) ) {
536            return;
537        }
538
539        require_once $build_index;
540
541        \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::register(
542            'jetpack-newsletter',
543            array_merge(
544                \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::SCRIPT_HANDLES,
545                \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS
546            )
547        );
548    }
549
550    /**
551     * Alias the current screen ID to satisfy wp-build's auto-generated enqueue check.
552     *
553     * Wp-build's `<page>-wp-admin` enqueue callback enqueues only when the screen ID
554     * matches the wp-build page slug (`jetpack-newsletter-dashboard`). Our wp-admin
555     * menu slug stays `jetpack-newsletter`, so we mutate the screen object in place
556     * to make the check pass without changing the user-facing URL.
557     *
558     * Hooked only when modernization is on AND we're on the Newsletter admin page,
559     * so this never affects any other request.
560     *
561     * @param \WP_Screen|null $screen The current screen object (passed by WP).
562     * @return void
563     */
564    public static function alias_screen_id_for_wp_build( $screen ) {
565        if ( ! is_object( $screen ) ) {
566            return;
567        }
568
569        $screen->id = 'jetpack-newsletter-dashboard';
570    }
571
572    /**
573     * Returns true when the wp-build modernization filter is enabled.
574     *
575     * The modernized Newsletter dashboard, wp-admin subscriber management, and the
576     * retired Calypso Subscribers submenu now default on for every site. Hosts (and
577     * a11ns who want the legacy view back) can still force the legacy experience with
578     * `add_filter( self::MODERNIZATION_FILTER, '__return_false' );`.
579     *
580     * @return bool
581     */
582    private static function is_modernized() {
583        return (bool) apply_filters( self::MODERNIZATION_FILTER, true );
584    }
585
586    /**
587     * Returns true when the current request targets the Newsletter admin page.
588     *
589     * Used to scope wp-build loading to the one page that needs it. The
590     * `$_GET['page']` value is populated by wp-admin/admin.php before any of
591     * our hooks fire, so this check is reliable from `init_hooks()` onwards.
592     *
593     * @return bool
594     */
595    private static function is_newsletter_admin_request() {
596        if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
597            return false;
598        }
599
600        return sanitize_text_field( wp_unslash( $_GET['page'] ) ) === self::ADMIN_PAGE_SLUG; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
601    }
602}