| 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 |
|
| 10 |
namespace Automattic\Jetpack\SEO; |
| 11 |
|
| 12 |
use Automattic\Jetpack\Modules; |
| 13 |
use Jetpack_SEO_Utils; |
| 14 |
|
| 15 |
/** |
| 16 |
* Registers the dashboard's read routes and builds their payloads. |
| 17 |
*/ |
| 18 |
class 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 |
'sitemap_active' => $modules->is_active( 'sitemaps' ), |
| 176 |
'seo_tools_active' => $modules->is_active( 'seo-tools' ), |
| 177 |
), |
| 178 |
// Per-service booleans (a code is set or not) for the Overview's |
| 179 |
// Site verification card. |
| 180 |
'site_verification' => array( |
| 181 |
'google' => ! empty( $codes['google'] ), |
| 182 |
'bing' => ! empty( $codes['bing'] ), |
| 183 |
'pinterest' => ! empty( $codes['pinterest'] ), |
| 184 |
'yandex' => ! empty( $codes['yandex'] ), |
| 185 |
'facebook' => ! empty( $codes['facebook'] ), |
| 186 |
), |
| 187 |
'content_coverage' => Content_Coverage::get(), |
| 188 |
'plan' => array( |
| 189 |
'seo_enabled_for_site' => $seo_enabled, |
| 190 |
), |
| 191 |
); |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* Coerce the stored title formats into one `{ type, value }` token list per page type. |
| 196 |
* |
| 197 |
* The site-settings API stores a cleared page type as `''` (see JETPACK-2284), so the |
| 198 |
* option is looser than the Settings tab's type; anything not a token list becomes `array()`. |
| 199 |
* |
| 200 |
* @since 0.9.5 |
| 201 |
* |
| 202 |
* @param mixed $stored Raw option value. |
| 203 |
* @return array<string, array<int, array{type: string, value: string}>> |
| 204 |
*/ |
| 205 |
public static function normalize_title_formats( $stored ) { |
| 206 |
if ( ! is_array( $stored ) ) { |
| 207 |
return array(); |
| 208 |
} |
| 209 |
|
| 210 |
$normalized = array(); |
| 211 |
foreach ( $stored as $page_type => $format ) { |
| 212 |
$tokens = array(); |
| 213 |
if ( is_array( $format ) ) { |
| 214 |
foreach ( $format as $item ) { |
| 215 |
if ( is_array( $item ) && isset( $item['type'] ) && isset( $item['value'] ) && is_string( $item['type'] ) && is_string( $item['value'] ) ) { |
| 216 |
$tokens[] = array( |
| 217 |
'type' => $item['type'], |
| 218 |
'value' => $item['value'], |
| 219 |
); |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
$normalized[ (string) $page_type ] = $tokens; |
| 224 |
} |
| 225 |
|
| 226 |
return $normalized; |
| 227 |
} |
| 228 |
|
| 229 |
/** |
| 230 |
* Build the editable Settings state the Settings tab hydrates from. |
| 231 |
* |
| 232 |
* Read-only bootstrap only. Most writes go through the existing |
| 233 |
* `/jetpack/v4/settings` REST endpoint, which already validates and |
| 234 |
* sanitizes those flat fields. Nested Schema writes use the package's |
| 235 |
* schema-settings route; bootstrapping them here keeps the Settings UI |
| 236 |
* hydrated without a second request. |
| 237 |
* |
| 238 |
* @return array |
| 239 |
*/ |
| 240 |
public static function get_settings_data() { |
| 241 |
$modules = new Modules(); |
| 242 |
|
| 243 |
// Read the stored values directly: Jetpack_SEO_Titles::get_custom_title_formats() |
| 244 |
// intentionally hides them while another SEO plugin controls output, but the |
| 245 |
// dashboard must still show the saved values without allowing edits. |
| 246 |
$title_formats = self::normalize_title_formats( get_option( 'advanced_seo_title_formats', array() ) ); |
| 247 |
// @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_SEO_Utils lives in plugins/jetpack and is guarded by class_exists. |
| 248 |
$title_formats_editable = class_exists( 'Jetpack_SEO_Utils' ) && Jetpack_SEO_Utils::is_enabled_jetpack_seo(); |
| 249 |
// @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_SEO_Utils lives in plugins/jetpack and is guarded by class_exists. |
| 250 |
$front_page_desc = class_exists( 'Jetpack_SEO_Utils' ) ? Jetpack_SEO_Utils::get_front_page_meta_description() : ''; |
| 251 |
|
| 252 |
// A site that set a front-page description back when it was free for all |
| 253 |
// WordPress.com Simple sites keeps editing it, even when otherwise plan-gated: |
| 254 |
// the value stays live and `Jetpack_SEO_Utils` still reads/writes it via the |
| 255 |
// legacy option. The gated Settings uses this to keep that one field editable. |
| 256 |
// @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_SEO_Utils lives in plugins/jetpack and is guarded by class_exists. |
| 257 |
$has_legacy_front_page_meta = class_exists( 'Jetpack_SEO_Utils' ) && (bool) Jetpack_SEO_Utils::has_legacy_front_page_meta(); |
| 258 |
|
| 259 |
$codes = get_option( 'verification_services_codes', array() ); |
| 260 |
if ( ! is_array( $codes ) ) { |
| 261 |
$codes = array(); |
| 262 |
} |
| 263 |
|
| 264 |
$sitemap_active = $modules->is_active( 'sitemaps' ); |
| 265 |
|
| 266 |
return array( |
| 267 |
'search_engines_visible' => (int) get_option( 'blog_public', 1 ) === 1, |
| 268 |
// A private or coming-soon WordPress.com site isn't hidden from search by an |
| 269 |
// SEO setting and can't be unhidden by one — see {@see self::block_publishing_a_private_site()}. |
| 270 |
'site_is_private' => self::is_site_private(), |
| 271 |
'sitemap_active' => $sitemap_active, |
| 272 |
// The reachable sitemap URL (Jetpack serves a valid sitemap here as soon as |
| 273 |
// it's on + the site is public), or '' when sitemaps are off, so the Settings |
| 274 |
// tab shows the "View sitemap" link exactly when there's a sitemap to view. |
| 275 |
'sitemap_url' => self::get_reachable_sitemap_url( $sitemap_active ), |
| 276 |
'canonical_active' => $modules->is_active( 'canonical-urls' ), |
| 277 |
// Cast to object so an empty format set serializes as `{}`, not `[]`. |
| 278 |
'title_formats' => (object) $title_formats, |
| 279 |
// Separator WordPress joins default document-title parts with. A page type |
| 280 |
// with no stored format keeps the default title: `get_custom_title()` returns |
| 281 |
// the incoming value untouched, so core composes the title itself and the |
| 282 |
// Settings tab replays that composition to preview it. |
| 283 |
'title_separator' => self::get_default_title_separator(), |
| 284 |
'title_formats_editable' => $title_formats_editable, |
| 285 |
'front_page_description' => (string) $front_page_desc, |
| 286 |
'has_legacy_front_page_meta' => $has_legacy_front_page_meta, |
| 287 |
'verification_tools_active' => $modules->is_active( 'verification-tools' ), |
| 288 |
'verification' => array( |
| 289 |
'google' => isset( $codes['google'] ) ? (string) $codes['google'] : '', |
| 290 |
'bing' => isset( $codes['bing'] ) ? (string) $codes['bing'] : '', |
| 291 |
'pinterest' => isset( $codes['pinterest'] ) ? (string) $codes['pinterest'] : '', |
| 292 |
'yandex' => isset( $codes['yandex'] ) ? (string) $codes['yandex'] : '', |
| 293 |
'facebook' => isset( $codes['facebook'] ) ? (string) $codes['facebook'] : '', |
| 294 |
), |
| 295 |
'schema' => Schema_Settings::get_editable(), |
| 296 |
); |
| 297 |
} |
| 298 |
|
| 299 |
/** |
| 300 |
* Build the Google site-verification state for the Settings tab. |
| 301 |
* |
| 302 |
* The Settings verification card lets a connected user verify with Google via a |
| 303 |
* WordPress.com keyring OAuth popup (in addition to pasting a meta-tag code). This |
| 304 |
* bootstraps the keyring connect URL and whether the current user is connected — |
| 305 |
* the live verified status is fetched client-side from `/jetpack/v4/verify-site/google` |
| 306 |
* (a wpcom round-trip we don't want to make on every page load). |
| 307 |
* |
| 308 |
* Both `Keyring_Helper` (Publicize package) and the connection `Manager` are provided |
| 309 |
* by the host Jetpack plugin, so they're guarded with `class_exists` like the |
| 310 |
* `Jetpack_SEO_*` helpers. On a disconnected self-hosted site `is_connected` is false |
| 311 |
* and the UI falls back to manual code entry only. |
| 312 |
* |
| 313 |
* @return array |
| 314 |
*/ |
| 315 |
public static function get_google_verify_data() { |
| 316 |
$connect_url = ''; |
| 317 |
if ( class_exists( 'Automattic\\Jetpack\\Publicize\\Keyring_Helper' ) ) { |
| 318 |
// @phan-suppress-next-line PhanUndeclaredClassMethod -- guarded; Publicize package is provided by the host plugin. |
| 319 |
$connect_url = (string) \Automattic\Jetpack\Publicize\Keyring_Helper::connect_url( 'google_site_verification', 'other' ); |
| 320 |
} |
| 321 |
|
| 322 |
$is_connected = false; |
| 323 |
if ( class_exists( 'Automattic\\Jetpack\\Connection\\Manager' ) ) { |
| 324 |
$is_connected = ( new \Automattic\Jetpack\Connection\Manager() )->is_user_connected(); |
| 325 |
} |
| 326 |
|
| 327 |
return array( |
| 328 |
'connect_url' => $connect_url, |
| 329 |
'is_connected' => (bool) $is_connected, |
| 330 |
); |
| 331 |
} |
| 332 |
|
| 333 |
/** |
| 334 |
* Build the AI tab's initial state. |
| 335 |
* |
| 336 |
* The AI SEO Enhancer auto-generates SEO titles/descriptions/alt-text in the |
| 337 |
* editor (the generation itself is wpcom/AI-Assistant side); this exposes only |
| 338 |
* its persisted on/off toggle, whether it's available, and whether the AI SEO |
| 339 |
* control it sits under is on. Availability mirrors the legacy Traffic page: |
| 340 |
* the `ai_seo_enhancer_enabled` feature filter must be on (it still depends on |
| 341 |
* AI being available) AND the site's plan must support the `ai-seo-enhancer` |
| 342 |
* feature. The toggle writes through the existing `/jetpack/v4/settings` |
| 343 |
* endpoint (`ai_seo_enhancer_enabled`). |
| 344 |
* |
| 345 |
* `aiSeoEnabled` is reported separately rather than folded into availability: |
| 346 |
* with the control off the card is disabled, not hidden, so the saved choice |
| 347 |
* stays visible — the same treatment the Traffic page gives it. |
| 348 |
* |
| 349 |
* @return array |
| 350 |
*/ |
| 351 |
public static function get_ai_data() { |
| 352 |
$filter_on = (bool) apply_filters( 'ai_seo_enhancer_enabled', true ); |
| 353 |
|
| 354 |
// Jetpack_AI_Settings lives in plugins/jetpack, which bundles this package; guarded |
| 355 |
// like the other host-plugin classes here. Without the method the AI SEO control |
| 356 |
// does not exist, so the enhancer keeps its pre-control behavior. |
| 357 |
// @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_AI_Settings lives in plugins/jetpack and is guarded by is_callable. |
| 358 |
$ai_seo_on = ! is_callable( array( 'Jetpack_AI_Settings', 'is_ai_seo_enabled' ) ) || \Jetpack_AI_Settings::is_ai_seo_enabled(); |
| 359 |
|
| 360 |
// Current_Plan comes from the jetpack-plans package (a dependency of this |
| 361 |
// package since the plan-gating work), so it's always available here; the |
| 362 |
// class_exists guard is kept as belt-and-suspenders for older bundled snapshots. |
| 363 |
$plan_supports = class_exists( 'Automattic\\Jetpack\\Current_Plan' ) |
| 364 |
&& \Automattic\Jetpack\Current_Plan::supports( 'ai-seo-enhancer' ); |
| 365 |
|
| 366 |
return array( |
| 367 |
'enhancer' => array( |
| 368 |
'available' => $filter_on && $plan_supports, |
| 369 |
'enabled' => (bool) get_option( 'ai_seo_enhancer_enabled', false ), |
| 370 |
'aiSeoEnabled' => $ai_seo_on, |
| 371 |
), |
| 372 |
'llmsTxt' => array( |
| 373 |
'enabled' => Llms_Txt::is_enabled(), |
| 374 |
'url' => home_url( '/llms.txt' ), |
| 375 |
'canServe' => Llms_Txt::can_serve(), |
| 376 |
), |
| 377 |
'crawlers' => Ai_Crawlers::get_bootstrap_data(), |
| 378 |
); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Build the supported post type options for the Content tab. |
| 383 |
* |
| 384 |
* @return array{post_types:array<int,array{slug:string,label:string}>} |
| 385 |
*/ |
| 386 |
public static function get_content_data() { |
| 387 |
return array( |
| 388 |
'post_types' => Post_Types::get_supported_content_type_options(), |
| 389 |
); |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Site identity used to render the homepage search/social previews on the |
| 394 |
* Settings tab: title, tagline, URL, and representative images. The front-page |
| 395 |
* description that completes the preview is read from the Settings form |
| 396 |
* (it's editable there), not bootstrapped here. |
| 397 |
* |
| 398 |
* @return array |
| 399 |
*/ |
| 400 |
public static function get_site_data() { |
| 401 |
$icon_url = (string) get_site_icon_url(); |
| 402 |
|
| 403 |
$logo_id = (int) get_theme_mod( 'custom_logo' ); |
| 404 |
$logo_url = $logo_id ? (string) wp_get_attachment_image_url( $logo_id, 'full' ) : ''; |
| 405 |
if ( class_exists( 'Jetpack_Redux_State_Helper' ) ) { |
| 406 |
// @phan-suppress-next-line PhanUndeclaredClassMethod -- Jetpack_Redux_State_Helper lives in plugins/jetpack and is guarded by class_exists. |
| 407 |
$image_url = (string) \Jetpack_Redux_State_Helper::get_site_image(); |
| 408 |
} else { |
| 409 |
$image_url = $logo_url ? $logo_url : $icon_url; |
| 410 |
} |
| 411 |
|
| 412 |
return array( |
| 413 |
'title' => (string) get_bloginfo( 'name' ), |
| 414 |
'tagline' => (string) get_bloginfo( 'description' ), |
| 415 |
'url' => (string) home_url(), |
| 416 |
'icon' => $icon_url, |
| 417 |
'image' => $image_url, |
| 418 |
); |
| 419 |
} |
| 420 |
|
| 421 |
/** |
| 422 |
* The separator, as rendered, that WordPress joins default document-title parts |
| 423 |
* with — for previewing the title a page type with no stored format produces. |
| 424 |
* |
| 425 |
* `document_title_separator` alone is not what a visitor sees. `wp_get_document_title()` |
| 426 |
* composes the parts, then passes the whole title through the `document_title` |
| 427 |
* filter, which WordPress texturizes by default — turning the default spaced |
| 428 |
* hyphen into an en dash. Previewing the raw filter value would show `-` on a site |
| 429 |
* that renders `–`, which is every site running core's defaults. |
| 430 |
* |
| 431 |
* This applies only to the default title. A stored format short-circuits |
| 432 |
* `pre_get_document_title`, which returns before the `document_title` filter, so a |
| 433 |
* custom format keeps the separator the user typed verbatim — the front end renders |
| 434 |
* `Site - MARKER - Page` for a custom format while producing `Page – Site` for the |
| 435 |
* default one. |
| 436 |
* |
| 437 |
* @return string The rendered separator. |
| 438 |
*/ |
| 439 |
private static function get_default_title_separator() { |
| 440 |
$separator = (string) apply_filters( 'document_title_separator', '-' ); |
| 441 |
|
| 442 |
// Only texturize when the title itself would be: a site that unhooks |
| 443 |
// `wptexturize` renders the raw separator, and the preview should match. |
| 444 |
if ( has_filter( 'document_title', 'wptexturize' ) ) { |
| 445 |
$separator = trim( |
| 446 |
html_entity_decode( wptexturize( ' ' . $separator . ' ' ), ENT_QUOTES, 'UTF-8' ) |
| 447 |
); |
| 448 |
} |
| 449 |
|
| 450 |
return $separator; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* The public URL of the XML sitemap, or an empty string when none is reachable. |
| 455 |
* |
| 456 |
* A sitemap is reachable as soon as generation is enabled and the site is public: |
| 457 |
* Jetpack serves a valid (empty-until-built) sitemap at a stable URL — never a 404 — |
| 458 |
* so the link is safe to surface immediately, without waiting on (or gating against) |
| 459 |
* the cron build. A prior gate looked the master sitemap up by a mis-built filename |
| 460 |
* and so never matched, which is what left the Settings tab stuck on "Generating…". |
| 461 |
* |
| 462 |
* `jetpack_sitemap_uri()` / `jp_sitemap_filename()` and the JP_MASTER_SITEMAP_TYPE |
| 463 |
* constant live in the Jetpack plugin's Sitemaps module (loaded only for an active |
| 464 |
* module on a public site), so they are guarded; in the package-only context they |
| 465 |
* are absent and the sitemap is reported as not reachable. |
| 466 |
* |
| 467 |
* @param bool $sitemap_active Whether sitemap generation is enabled. |
| 468 |
* @return string The sitemap URL, or '' when not reachable. |
| 469 |
*/ |
| 470 |
private static function get_reachable_sitemap_url( $sitemap_active ) { |
| 471 |
// Jetpack only serves sitemaps when generation is on and the site is public. |
| 472 |
if ( ! $sitemap_active || (int) get_option( 'blog_public', 1 ) !== 1 ) { |
| 473 |
return ''; |
| 474 |
} |
| 475 |
|
| 476 |
// The `JP_MASTER_SITEMAP_TYPE` constant and the `jp_sitemap_filename()` / |
| 477 |
// `jetpack_sitemap_uri()` helpers all live together in plugins/jetpack and load |
| 478 |
// as a unit, so this single guard covers every symbol used below. |
| 479 |
if ( |
| 480 |
! defined( 'JP_MASTER_SITEMAP_TYPE' ) |
| 481 |
|| ! function_exists( 'jp_sitemap_filename' ) |
| 482 |
|| ! function_exists( 'jetpack_sitemap_uri' ) |
| 483 |
) { |
| 484 |
return ''; |
| 485 |
} |
| 486 |
|
| 487 |
// `jp_sitemap_filename()` returns an error string ("error-not-int-…") unless a |
| 488 |
// non-null number is passed; the master ignores the number, so pass 0 (matching |
| 489 |
// Jetpack's own call sites). Fail safe: the master file is always 'sitemap.xml', |
| 490 |
// so if a bundled Jetpack ever returns something else, report not-reachable |
| 491 |
// rather than surface a broken URL — the exact failure this method shipped with |
| 492 |
// before (the missing number silently produced an "error-not-int-…" link). |
| 493 |
// @phan-suppress-next-line PhanUndeclaredFunction -- guarded above; symbols live in plugins/jetpack. |
| 494 |
$filename = (string) jp_sitemap_filename( JP_MASTER_SITEMAP_TYPE, 0 ); |
| 495 |
if ( 'sitemap.xml' !== $filename ) { |
| 496 |
return ''; |
| 497 |
} |
| 498 |
|
| 499 |
// esc_url_raw (not esc_url): transported via script data and rendered by React, |
| 500 |
// so it must not be HTML-entity-encoded (e.g. the plain-permalink |
| 501 |
// `?jetpack-sitemap=` form keeps its raw `&`). |
| 502 |
// @phan-suppress-next-line PhanUndeclaredFunction -- guarded above; symbols live in plugins/jetpack. |
| 503 |
return esc_url_raw( (string) jetpack_sitemap_uri( $filename ) ); |
| 504 |
} |
| 505 |
} |
| 506 |
|