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