Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
79.01% covered (warning)
79.01%
128 / 162
47.06% covered (danger)
47.06%
8 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
Dashboard_Data
79.01% covered (warning)
79.01%
128 / 162
47.06% covered (danger)
47.06%
8 / 17
73.11
0.00% covered (danger)
0.00%
0 / 1
 rest_reads
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 rest_read_paths
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_reads
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 reads_permission_check
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_settings
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
1
 is_site_private
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 block_publishing_a_private_site
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
4
 get_overview_data
95.83% covered (success)
95.83%
23 / 24
0.00% covered (danger)
0.00%
0 / 1
3
 get_settings_data
93.75% covered (success)
93.75%
30 / 32
0.00% covered (danger)
0.00%
0 / 1
11.03
 get_google_verify_data
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
3.01
 get_ai_data
100.00% covered (success)
100.00%
17 / 17
100.00% covered (success)
100.00%
1 / 1
4
 get_content_data
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get_site_data
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
4.01
 is_sitemap_enabled
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 is_canonical_enabled
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 get_default_title_separator
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 get_reachable_sitemap_url
60.00% covered (warning)
60.00%
6 / 10
0.00% covered (danger)
0.00%
0 / 1
10.14
1<?php
2/**
3 * The SEO dashboard's read-only REST routes and the payload builders behind
4 * them — one route per data-backed tab, preloaded onto the page so a normal
5 * load resolves them with no request.
6 *
7 * @package automattic/jetpack-seo-package
8 */
9
10namespace Automattic\Jetpack\SEO;
11
12use Automattic\Jetpack\Modules;
13use Jetpack_SEO_Utils;
14
15/**
16 * Registers the dashboard's read routes and builds their payloads.
17 */
18class Dashboard_Data {
19
20    /**
21     * Map of read-only dashboard routes: tab slug => data-builder callable. The
22     * single source of truth for both the registered routes and the paths
23     * preloaded onto the page, so the two can't drift.
24     *
25     * @return array<string, callable>
26     */
27    private static function rest_reads() {
28        return array(
29            'overview' => array( __CLASS__, 'get_overview_data' ),
30            'settings' => array( __CLASS__, 'get_settings_data' ),
31            'ai'       => array( __CLASS__, 'get_ai_data' ),
32            'content'  => array( __CLASS__, 'get_content_data' ),
33        );
34    }
35
36    /**
37     * REST paths the dashboard reads its initial state from, preloaded into the
38     * page (see {@see Admin_Page::inject_script_data()}) and fetched by the app.
39     *
40     * @return string[]
41     */
42    public static function rest_read_paths() {
43        return array_map(
44            static function ( $slug ) {
45                return '/jetpack/v4/seo/' . $slug;
46            },
47            array_keys( self::rest_reads() )
48        );
49    }
50
51    /**
52     * Register the read-only REST routes the dashboard hydrates from — one per
53     * data-backed tab, each returning the same builder payload previously injected
54     * synchronously onto the page. Read-only and gated to the page's own
55     * `manage_options`; writes still go through their existing endpoints.
56     *
57     * @return void
58     */
59    public static function register_rest_reads() {
60        foreach ( self::rest_reads() as $slug => $builder ) {
61            register_rest_route(
62                'jetpack/v4',
63                '/seo/' . $slug,
64                array(
65                    'methods'             => \WP_REST_Server::READABLE,
66                    'callback'            => static function () use ( $builder ) {
67                        return rest_ensure_response( call_user_func( $builder ) );
68                    },
69                    'permission_callback' => array( __CLASS__, 'reads_permission_check' ),
70                )
71            );
72        }
73    }
74
75    /**
76     * Capability gate for the dashboard's read routes — the same `manage_options`
77     * the SEO admin page itself requires.
78     *
79     * @return bool
80     */
81    public static function reads_permission_check() {
82        return current_user_can( 'manage_options' );
83    }
84
85    /**
86     * Expose the core `blog_public` option to the REST settings endpoint.
87     *
88     * Search-engine visibility is a WordPress core option, not a Jetpack one,
89     * so the Settings tab saves it through `/wp/v2/settings` — which only
90     * round-trips settings registered with `show_in_rest`. The core settings
91     * controller enforces the `manage_options` capability on writes.
92     *
93     * @return void
94     */
95    public static function register_rest_settings() {
96        register_setting(
97            'reading',
98            'blog_public',
99            array(
100                'show_in_rest' => true,
101                'type'         => 'integer',
102                'default'      => 1,
103            )
104        );
105
106        // Refuse an indexing write that would publish a private site. Scoped to core's
107        // settings route rather than attached as `blog_public`'s sanitizer, because a
108        // `register_setting()` sanitizer applies to every writer of the option for the
109        // rest of the request — including the site's own visibility control, which must
110        // stay able to change it.
111        add_filter( 'rest_pre_update_setting', array( __CLASS__, 'block_publishing_a_private_site' ), 10, 3 );
112    }
113
114    /**
115     * Whether the site is private or still in coming-soon, which WordPress.com records
116     * as a negative `blog_public`. Always false on self-hosted, where the option is a
117     * plain 0/1.
118     *
119     * @return bool
120     */
121    private static function is_site_private() {
122        return (int) get_option( 'blog_public', 1 ) < 0;
123    }
124
125    /**
126     * Keep a WordPress.com site's private or coming-soon state out of reach of an SEO
127     * toggle.
128     *
129     * `blog_public` is a plain 0/1 on self-hosted, but WordPress.com also stores `-1`
130     * for a private site and `-2` for one still in coming-soon. This dashboard only
131     * offers "allow search engines to index this site", which writes 1 or 0 — so
132     * without this, the owner of an unfinished site who flipped that toggle on
133     * published it. Publishing a site is not a search-engine setting and isn't a
134     * decision this surface asks for, so a negative stored value is left alone.
135     *
136     * The site's own visibility control is what changes it — and still can, because
137     * this hangs off core's settings route rather than the option's sanitizer.
138     * {@see self::get_settings_data()} reports `site_is_private` so the toggle can say
139     * why it's disabled rather than silently doing nothing.
140     *
141     * @param bool   $handled Whether another handler already wrote the setting.
142     * @param string $name    Setting name.
143     * @param mixed  $value   Submitted value.
144     * @return bool True to report the write as handled, which skips it.
145     */
146    public static function block_publishing_a_private_site( $handled, $name, $value ) {
147        if ( $handled || 'blog_public' !== $name ) {
148            return $handled;
149        }
150
151        // Leave a write that keeps the site unpublished alone; only a move to a public
152        // value is refused.
153        return self::is_site_private() && (int) $value >= 0;
154    }
155
156    /**
157     * Build the aggregated Overview state the dashboard renders.
158     *
159     * @return array
160     */
161    public static function get_overview_data() {
162        $modules = new Modules();
163        // @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_SEO_Utils lives in plugins/jetpack and is guarded by class_exists.
164        $seo_enabled = class_exists( 'Jetpack_SEO_Utils' ) && Jetpack_SEO_Utils::is_enabled_jetpack_seo();
165
166        $codes = get_option( 'verification_services_codes', array() );
167        if ( ! is_array( $codes ) ) {
168            $codes = array();
169        }
170
171        return array(
172            'site_visibility'   => array(
173                'search_engines_visible' => (int) get_option( 'blog_public', 1 ) === 1,
174                'site_is_private'        => self::is_site_private(),
175                // Read the durable SEO option (seeded/synced from the `sitemaps` module
176                // by the Jetpack plugin) so the state survives the module's removal. The
177                // reachable sitemap URL + "View" link live on the Settings tab.
178                'sitemap_active'         => self::is_sitemap_enabled( $modules ),
179                'seo_tools_active'       => $modules->is_active( 'seo-tools' ),
180            ),
181            // Per-service booleans (a code is set or not) for the Overview's
182            // Site verification card.
183            'site_verification' => array(
184                'google'    => ! empty( $codes['google'] ),
185                'bing'      => ! empty( $codes['bing'] ),
186                'pinterest' => ! empty( $codes['pinterest'] ),
187                'yandex'    => ! empty( $codes['yandex'] ),
188                'facebook'  => ! empty( $codes['facebook'] ),
189            ),
190            'content_coverage'  => Content_Coverage::get(),
191            'plan'              => array(
192                'seo_enabled_for_site' => $seo_enabled,
193            ),
194        );
195    }
196
197    /**
198     * Build the editable Settings state the Settings tab hydrates from.
199     *
200     * Read-only bootstrap only. Most writes go through the existing
201     * `/jetpack/v4/settings` REST endpoint, which already validates and
202     * sanitizes those flat fields. Nested Schema writes use the package's
203     * schema-settings route; bootstrapping them here keeps the Settings UI
204     * hydrated without a second request.
205     *
206     * @return array
207     */
208    public static function get_settings_data() {
209        $modules = new Modules();
210
211        // Read the stored values directly: Jetpack_SEO_Titles::get_custom_title_formats()
212        // intentionally hides them while another SEO plugin controls output, but the
213        // dashboard must still show the saved values without allowing edits.
214        $title_formats = get_option( 'advanced_seo_title_formats', array() );
215        if ( ! is_array( $title_formats ) ) {
216            $title_formats = array();
217        }
218        // @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_SEO_Utils lives in plugins/jetpack and is guarded by class_exists.
219        $title_formats_editable = class_exists( 'Jetpack_SEO_Utils' ) && Jetpack_SEO_Utils::is_enabled_jetpack_seo();
220        // @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_SEO_Utils lives in plugins/jetpack and is guarded by class_exists.
221        $front_page_desc = class_exists( 'Jetpack_SEO_Utils' ) ? Jetpack_SEO_Utils::get_front_page_meta_description() : '';
222
223        // A site that set a front-page description back when it was free for all
224        // WordPress.com Simple sites keeps editing it, even when otherwise plan-gated:
225        // the value stays live and `Jetpack_SEO_Utils` still reads/writes it via the
226        // legacy option. The gated Settings uses this to keep that one field editable.
227        // @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_SEO_Utils lives in plugins/jetpack and is guarded by class_exists.
228        $has_legacy_front_page_meta = class_exists( 'Jetpack_SEO_Utils' ) && (bool) Jetpack_SEO_Utils::has_legacy_front_page_meta();
229
230        $codes = get_option( 'verification_services_codes', array() );
231        if ( ! is_array( $codes ) ) {
232            $codes = array();
233        }
234
235        $sitemap_active = self::is_sitemap_enabled( $modules );
236
237        return array(
238            'search_engines_visible'     => (int) get_option( 'blog_public', 1 ) === 1,
239            // A private or coming-soon WordPress.com site isn't hidden from search by an
240            // SEO setting and can't be unhidden by one — see {@see self::block_publishing_a_private_site()}.
241            'site_is_private'            => self::is_site_private(),
242            // Read the durable SEO option (seeded/synced from the `sitemaps` module
243            // by the Jetpack plugin) so the state survives the module's removal.
244            'sitemap_active'             => $sitemap_active,
245            // The reachable sitemap URL (Jetpack serves a valid sitemap here as soon as
246            // it's on + the site is public), or '' when sitemaps are off, so the Settings
247            // tab shows the "View sitemap" link exactly when there's a sitemap to view.
248            'sitemap_url'                => self::get_reachable_sitemap_url( $sitemap_active ),
249            // Read the durable SEO option (seeded/synced from the `canonical-urls` module
250            // by the Jetpack plugin) so the state survives the module's removal.
251            'canonical_active'           => self::is_canonical_enabled( $modules ),
252            // Cast to object so an empty format set serializes as `{}`, not `[]`.
253            'title_formats'              => (object) $title_formats,
254            // Separator WordPress joins default document-title parts with. A page type
255            // with no stored format keeps the default title: `get_custom_title()` returns
256            // the incoming value untouched, so core composes the title itself and the
257            // Settings tab replays that composition to preview it.
258            'title_separator'            => self::get_default_title_separator(),
259            'title_formats_editable'     => $title_formats_editable,
260            'front_page_description'     => (string) $front_page_desc,
261            'has_legacy_front_page_meta' => $has_legacy_front_page_meta,
262            'verification_tools_active'  => $modules->is_active( 'verification-tools' ),
263            'verification'               => array(
264                'google'    => isset( $codes['google'] ) ? (string) $codes['google'] : '',
265                'bing'      => isset( $codes['bing'] ) ? (string) $codes['bing'] : '',
266                'pinterest' => isset( $codes['pinterest'] ) ? (string) $codes['pinterest'] : '',
267                'yandex'    => isset( $codes['yandex'] ) ? (string) $codes['yandex'] : '',
268                'facebook'  => isset( $codes['facebook'] ) ? (string) $codes['facebook'] : '',
269            ),
270            'schema'                     => Schema_Settings::get_editable(),
271        );
272    }
273
274    /**
275     * Build the Google site-verification state for the Settings tab.
276     *
277     * The Settings verification card lets a connected user verify with Google via a
278     * WordPress.com keyring OAuth popup (in addition to pasting a meta-tag code). This
279     * bootstraps the keyring connect URL and whether the current user is connected —
280     * the live verified status is fetched client-side from `/jetpack/v4/verify-site/google`
281     * (a wpcom round-trip we don't want to make on every page load).
282     *
283     * Both `Keyring_Helper` (Publicize package) and the connection `Manager` are provided
284     * by the host Jetpack plugin, so they're guarded with `class_exists` like the
285     * `Jetpack_SEO_*` helpers. On a disconnected self-hosted site `is_connected` is false
286     * and the UI falls back to manual code entry only.
287     *
288     * @return array
289     */
290    public static function get_google_verify_data() {
291        $connect_url = '';
292        if ( class_exists( 'Automattic\\Jetpack\\Publicize\\Keyring_Helper' ) ) {
293            // @phan-suppress-next-line PhanUndeclaredClassMethod -- guarded; Publicize package is provided by the host plugin.
294            $connect_url = (string) \Automattic\Jetpack\Publicize\Keyring_Helper::connect_url( 'google_site_verification', 'other' );
295        }
296
297        $is_connected = false;
298        if ( class_exists( 'Automattic\\Jetpack\\Connection\\Manager' ) ) {
299            $is_connected = ( new \Automattic\Jetpack\Connection\Manager() )->is_user_connected();
300        }
301
302        return array(
303            'connect_url'  => $connect_url,
304            'is_connected' => (bool) $is_connected,
305        );
306    }
307
308    /**
309     * Build the AI tab's initial state.
310     *
311     * The AI SEO Enhancer auto-generates SEO titles/descriptions/alt-text in the
312     * editor (the generation itself is wpcom/AI-Assistant side); this exposes only
313     * its persisted on/off toggle, whether it's available, and whether the AI SEO
314     * control it sits under is on. Availability mirrors the legacy Traffic page:
315     * the `ai_seo_enhancer_enabled` feature filter must be on (it still depends on
316     * AI being available) AND the site's plan must support the `ai-seo-enhancer`
317     * feature. The toggle writes through the existing `/jetpack/v4/settings`
318     * endpoint (`ai_seo_enhancer_enabled`).
319     *
320     * `aiSeoEnabled` is reported separately rather than folded into availability:
321     * with the control off the card is disabled, not hidden, so the saved choice
322     * stays visible — the same treatment the Traffic page gives it.
323     *
324     * @return array
325     */
326    public static function get_ai_data() {
327        $filter_on = (bool) apply_filters( 'ai_seo_enhancer_enabled', true );
328
329        // Jetpack_AI_Settings lives in plugins/jetpack, which bundles this package; guarded
330        // like the other host-plugin classes here. Without the method the AI SEO control
331        // does not exist, so the enhancer keeps its pre-control behavior.
332        // @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_AI_Settings lives in plugins/jetpack and is guarded by is_callable.
333        $ai_seo_on = ! is_callable( array( 'Jetpack_AI_Settings', 'is_ai_seo_enabled' ) ) || \Jetpack_AI_Settings::is_ai_seo_enabled();
334
335        // Current_Plan comes from the jetpack-plans package (a dependency of this
336        // package since the plan-gating work), so it's always available here; the
337        // class_exists guard is kept as belt-and-suspenders for older bundled snapshots.
338        $plan_supports = class_exists( 'Automattic\\Jetpack\\Current_Plan' )
339            && \Automattic\Jetpack\Current_Plan::supports( 'ai-seo-enhancer' );
340
341        return array(
342            'enhancer' => array(
343                'available'    => $filter_on && $plan_supports,
344                'enabled'      => (bool) get_option( 'ai_seo_enhancer_enabled', false ),
345                'aiSeoEnabled' => $ai_seo_on,
346            ),
347            'llmsTxt'  => array(
348                'enabled'  => Llms_Txt::is_enabled(),
349                'url'      => home_url( '/llms.txt' ),
350                'canServe' => Llms_Txt::can_serve(),
351            ),
352            'crawlers' => Ai_Crawlers::get_bootstrap_data(),
353        );
354    }
355
356    /**
357     * Build the supported post type options for the Content tab.
358     *
359     * @return array{post_types:array<int,array{slug:string,label:string}>}
360     */
361    public static function get_content_data() {
362        return array(
363            'post_types' => Post_Types::get_supported_content_type_options(),
364        );
365    }
366
367    /**
368     * Site identity used to render the homepage search/social previews on the
369     * Settings tab: title, tagline, URL, and representative images. The front-page
370     * description that completes the preview is read from the Settings form
371     * (it's editable there), not bootstrapped here.
372     *
373     * @return array
374     */
375    public static function get_site_data() {
376        $icon_url = (string) get_site_icon_url();
377
378        $logo_id  = (int) get_theme_mod( 'custom_logo' );
379        $logo_url = $logo_id ? (string) wp_get_attachment_image_url( $logo_id, 'full' ) : '';
380        if ( class_exists( 'Jetpack_Redux_State_Helper' ) ) {
381            // @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_Redux_State_Helper lives in plugins/jetpack and is guarded by class_exists.
382            $image_url = (string) \Jetpack_Redux_State_Helper::get_site_image();
383        } else {
384            $image_url = $logo_url ? $logo_url : $icon_url;
385        }
386
387        return array(
388            'title'   => (string) get_bloginfo( 'name' ),
389            'tagline' => (string) get_bloginfo( 'description' ),
390            'url'     => (string) home_url(),
391            'icon'    => $icon_url,
392            'image'   => $image_url,
393        );
394    }
395
396    /**
397     * Whether sitemap generation is enabled.
398     *
399     * Reads the durable {@see Initializer::SITEMAP_ENABLED_OPTION} flag. The default is only
400     * used when the option is absent (for example before the Jetpack plugin's migration
401     * has run on a freshly upgraded site), in which case it falls back to the live
402     * `sitemaps` module state so behavior is unchanged in that gap.
403     *
404     * @param Modules $modules Modules instance to read live module state from.
405     * @return bool
406     */
407    private static function is_sitemap_enabled( Modules $modules ) {
408        $enabled = get_option( Initializer::SITEMAP_ENABLED_OPTION, null );
409
410        // Only fall back to the live module state when the durable option is absent.
411        // Passing it as get_option()'s default would evaluate it on every call, since
412        // PHP resolves function arguments eagerly even when the option exists.
413        if ( null === $enabled ) {
414            $enabled = $modules->is_active( 'sitemaps' );
415        }
416
417        return (bool) $enabled;
418    }
419
420    /**
421     * Whether canonical URLs are enabled.
422     *
423     * Reads the durable {@see Initializer::CANONICAL_ENABLED_OPTION} flag. The default is only
424     * used when the option is absent (for example before the Jetpack plugin's migration
425     * has run on a freshly upgraded site), in which case it falls back to the live
426     * `canonical-urls` module state so behavior is unchanged in that gap.
427     *
428     * @param Modules $modules Modules instance to read live module state from.
429     * @return bool
430     */
431    private static function is_canonical_enabled( Modules $modules ) {
432        $enabled = get_option( Initializer::CANONICAL_ENABLED_OPTION, null );
433
434        // Only fall back to the live module state when the durable option is absent.
435        // Passing it as get_option()'s default would evaluate it on every call, since
436        // PHP resolves function arguments eagerly even when the option exists.
437        if ( null === $enabled ) {
438            $enabled = $modules->is_active( 'canonical-urls' );
439        }
440
441        return (bool) $enabled;
442    }
443
444    /**
445     * The separator, as rendered, that WordPress joins default document-title parts
446     * with — for previewing the title a page type with no stored format produces.
447     *
448     * `document_title_separator` alone is not what a visitor sees. `wp_get_document_title()`
449     * composes the parts, then passes the whole title through the `document_title`
450     * filter, which WordPress texturizes by default — turning the default spaced
451     * hyphen into an en dash. Previewing the raw filter value would show `-` on a site
452     * that renders `–`, which is every site running core's defaults.
453     *
454     * This applies only to the default title. A stored format short-circuits
455     * `pre_get_document_title`, which returns before the `document_title` filter, so a
456     * custom format keeps the separator the user typed verbatim — the front end renders
457     * `Site - MARKER - Page` for a custom format while producing `Page – Site` for the
458     * default one.
459     *
460     * @return string The rendered separator.
461     */
462    private static function get_default_title_separator() {
463        $separator = (string) apply_filters( 'document_title_separator', '-' );
464
465        // Only texturize when the title itself would be: a site that unhooks
466        // `wptexturize` renders the raw separator, and the preview should match.
467        if ( has_filter( 'document_title', 'wptexturize' ) ) {
468            $separator = trim(
469                html_entity_decode( wptexturize( ' ' . $separator . ' ' ), ENT_QUOTES, 'UTF-8' )
470            );
471        }
472
473        return $separator;
474    }
475
476    /**
477     * The public URL of the XML sitemap, or an empty string when none is reachable.
478     *
479     * A sitemap is reachable as soon as generation is enabled and the site is public:
480     * Jetpack serves a valid (empty-until-built) sitemap at a stable URL — never a 404 —
481     * so the link is safe to surface immediately, without waiting on (or gating against)
482     * the cron build. A prior gate looked the master sitemap up by a mis-built filename
483     * and so never matched, which is what left the Settings tab stuck on "Generating…".
484     *
485     * `jetpack_sitemap_uri()` / `jp_sitemap_filename()` and the JP_MASTER_SITEMAP_TYPE
486     * constant live in the Jetpack plugin's Sitemaps module (loaded only for an active
487     * module on a public site), so they are guarded; in the package-only context they
488     * are absent and the sitemap is reported as not reachable.
489     *
490     * @param bool $sitemap_active Whether sitemap generation is enabled.
491     * @return string The sitemap URL, or '' when not reachable.
492     */
493    private static function get_reachable_sitemap_url( $sitemap_active ) {
494        // Jetpack only serves sitemaps when generation is on and the site is public.
495        if ( ! $sitemap_active || (int) get_option( 'blog_public', 1 ) !== 1 ) {
496            return '';
497        }
498
499        // The `JP_MASTER_SITEMAP_TYPE` constant and the `jp_sitemap_filename()` /
500        // `jetpack_sitemap_uri()` helpers all live together in plugins/jetpack and load
501        // as a unit, so this single guard covers every symbol used below.
502        if (
503            ! defined( 'JP_MASTER_SITEMAP_TYPE' )
504            || ! function_exists( 'jp_sitemap_filename' )
505            || ! function_exists( 'jetpack_sitemap_uri' )
506        ) {
507            return '';
508        }
509
510        // `jp_sitemap_filename()` returns an error string ("error-not-int-…") unless a
511        // non-null number is passed; the master ignores the number, so pass 0 (matching
512        // Jetpack's own call sites). Fail safe: the master file is always 'sitemap.xml',
513        // so if a bundled Jetpack ever returns something else, report not-reachable
514        // rather than surface a broken URL — the exact failure this method shipped with
515        // before (the missing number silently produced an "error-not-int-…" link).
516        // @phan-suppress-next-line PhanUndeclaredFunction -- guarded above; symbols live in plugins/jetpack.
517        $filename = (string) jp_sitemap_filename( JP_MASTER_SITEMAP_TYPE, 0 );
518        if ( 'sitemap.xml' !== $filename ) {
519            return '';
520        }
521
522        // esc_url_raw (not esc_url): transported via script data and rendered by React,
523        // so it must not be HTML-entity-encoded (e.g. the plain-permalink
524        // `?jetpack-sitemap=` form keeps its raw `&`).
525        // @phan-suppress-next-line PhanUndeclaredFunction -- guarded above; symbols live in plugins/jetpack.
526        return esc_url_raw( (string) jetpack_sitemap_uri( $filename ) );
527    }
528}