| @@ -266,8 +266,16 @@ | ||
| 266 | 266 | // LLMs_Txt_Manager for the static file) guarantees an explicit UTF-8 |
| 267 | 267 | // charset. Priority 8 keeps it ahead of redirect_canonical(). |
| 268 | 268 | add_action('template_redirect', [$this, 'maybe_serve_llms_txt'], 8); |
| 269 | 269 | |
| 270 | + // Serve the sitemap from PHP on sites whose web root cannot be written. | |
| 271 | + // ThinkRank publishes sitemaps as real files, so where that is possible | |
| 272 | + // the web server answers first and this never runs; where it is not, | |
| 273 | + // this is the only thing that answers at all, and without it the | |
| 274 | + // feature was simply unavailable (#752). Same priority 8, and for the | |
| 275 | + // same reason: ahead of redirect_canonical(). | |
| 276 | + add_action('template_redirect', [$this, 'maybe_serve_sitemap'], 8); | |
| 277 | + | |
| 270 | 278 | // Take WordPress core's own sitemap offline while ThinkRank's is active. |
| 271 | 279 | // Two sitemap indexes on one site is a crawl conflict: core keeps |
| 272 | 280 | // /wp-sitemap.xml served and injects its own "Sitemap:" line into |
| 273 | 281 | // robots.txt (WP_Sitemaps::add_robots, priority 0). Until now that line |
| @@ -369,8 +377,16 @@ | ||
| 369 | 377 | } |
| 370 | 378 | |
| 371 | 379 | // Initialize Global SEO Schema Output and store reference |
| 372 | 380 | $this->global_seo_schema = new Global_SEO_Schema_Output(); |
| 381 | + // Let schema reuse the description this class already resolves, so the | |
| 382 | + // JSON-LD and the meta/og/twitter tags cannot disagree about what the | |
| 383 | + // page is (#766). Passed as a callback rather than a value: schema is | |
| 384 | + // built during wp_head, by which point the request context this | |
| 385 | + // resolution depends on is set, and it must not be captured earlier. | |
| 386 | + $this->global_seo_schema->set_description_resolver( | |
| 387 | + fn (): string => (string) $this->get_meta_description() | |
| 388 | + ); | |
| 373 | 389 | $this->global_seo_schema->init(); |
| 374 | 390 | } |
| 375 | 391 | |
| 376 | 392 | /** |
| @@ -2946,10 +2962,22 @@ | ||
| 2946 | 2962 | $context_type, |
| 2947 | 2963 | $context_id |
| 2948 | 2964 | ); |
| 2949 | 2965 | |
| 2966 | + // A deployed node is a snapshot from Deploy time and outranks | |
| 2967 | + // the automatic node, so page and article types would publish | |
| 2968 | + // a frozen excerpt instead of the description the head | |
| 2969 | + // resolves. Give them the live one, as the automatic node has. | |
| 2970 | + $context_post = get_post($context_id); | |
| 2971 | + | |
| 2950 | 2972 | foreach ($page_specific_schemas as $schema_type => $schema_info) { |
| 2951 | - Schema_Graph::instance()->add_primary($schema_info['data'], (string) $schema_type, 'schema_manager'); | |
| 2973 | + $node = $schema_info['data']; | |
| 2974 | + | |
| 2975 | + if ($this->global_seo_schema && $context_post instanceof \WP_Post) { | |
| 2976 | + $node = $this->global_seo_schema->refresh_deployed_description($node, (string) $schema_type, $context_post); | |
| 2977 | + } | |
| 2978 | + | |
| 2979 | + Schema_Graph::instance()->add_primary($node, (string) $schema_type, 'schema_manager'); | |
| 2952 | 2980 | } |
| 2953 | 2981 | $has_schema_manager_output = true; |
| 2954 | 2982 | } |
| 2955 | 2983 | } |
| @@ -3007,8 +3035,12 @@ | ||
| 3007 | 3035 | 'url' => home_url('/'), |
| 3008 | 3036 | ]; |
| 3009 | 3037 | |
| 3010 | 3038 | $description = !empty($settings['site_description']) ? $settings['site_description'] : get_bloginfo('description'); |
| 3039 | + // The tagline is stored esc_html()'d by sanitize_option(), so a site | |
| 3040 | + // called "Fish & Chips" published `&` literally in its WebSite | |
| 3041 | + // node; nothing decodes JSON-LD downstream. | |
| 3042 | + $description = \ThinkRank\Core\Seo_Text::normalize_schema_text((string) $description); | |
| 3011 | 3043 | if (!empty($description)) { |
| 3012 | 3044 | $schema['description'] = $description; |
| 3013 | 3045 | } |
| 3014 | 3046 | |
| @@ -3526,8 +3558,102 @@ | ||
| 3526 | 3558 | * @since 1.32.0 |
| 3527 | 3559 | * |
| 3528 | 3560 | * @return void |
| 3529 | 3561 | */ |
| 3562 | + /** | |
| 3563 | + * Serve a ThinkRank sitemap document for this request, when it is one. | |
| 3564 | + * | |
| 3565 | + * Only acts in dynamic delivery mode. In static mode a real file exists and | |
| 3566 | + * the web server returns it without WordPress ever loading, so answering | |
| 3567 | + * here as well would mean two sources for the same bytes. | |
| 3568 | + * | |
| 3569 | + * @since 2.9.0 | |
| 3570 | + * | |
| 3571 | + * @return void | |
| 3572 | + */ | |
| 3573 | + public function maybe_serve_sitemap(): void { | |
| 3574 | + $filename = $this->requested_sitemap_filename(); | |
| 3575 | + if ('' === $filename) { | |
| 3576 | + return; | |
| 3577 | + } | |
| 3578 | + | |
| 3579 | + try { | |
| 3580 | + // Read-only instance: passing false keeps it from registering a | |
| 3581 | + // second copy of the auto-generation hooks. | |
| 3582 | + $generator = new \ThinkRank\SEO\Sitemap_Generator(false); | |
| 3583 | + $settings = $generator->get_settings('site'); | |
| 3584 | + | |
| 3585 | + if (empty($settings['enabled'])) { | |
| 3586 | + return; | |
| 3587 | + } | |
| 3588 | + | |
| 3589 | + if ('dynamic' !== $generator->resolve_delivery_mode($settings)) { | |
| 3590 | + return; | |
| 3591 | + } | |
| 3592 | + | |
| 3593 | + if (!$generator->publishes_document_name($filename, $settings)) { | |
| 3594 | + return; | |
| 3595 | + } | |
| 3596 | + | |
| 3597 | + $xml = $generator->render_document($filename, $settings); | |
| 3598 | + } catch (\Throwable $e) { | |
| 3599 | + // A failed render must not replace the sitemap with a fatal. Leave | |
| 3600 | + // the request alone so WordPress answers as it otherwise would. | |
| 3601 | + return; | |
| 3602 | + } | |
| 3603 | + | |
| 3604 | + if (!is_string($xml) || '' === trim($xml)) { | |
| 3605 | + return; | |
| 3606 | + } | |
| 3607 | + | |
| 3608 | + status_header(200); | |
| 3609 | + header('Content-Type: application/xml; charset=UTF-8'); | |
| 3610 | + header('X-Robots-Tag: noindex, follow', true); | |
| 3611 | + | |
| 3612 | + // Built XML, escaped by the builders as they assemble it; escaping the | |
| 3613 | + // document here would corrupt it. | |
| 3614 | + echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 3615 | + exit; | |
| 3616 | + } | |
| 3617 | + | |
| 3618 | + /** | |
| 3619 | + * The sitemap file name this request is asking for, if it looks like one. | |
| 3620 | + * | |
| 3621 | + * Deliberately a cheap shape test. Whether the site actually publishes the | |
| 3622 | + * name is settled by the caller against the generator, so that a request | |
| 3623 | + * for someone else's sitemap is never answered here. | |
| 3624 | + * | |
| 3625 | + * @since 2.9.0 | |
| 3626 | + * | |
| 3627 | + * @return string File name, or '' when this is not a sitemap request. | |
| 3628 | + */ | |
| 3629 | + private function requested_sitemap_filename(): string { | |
| 3630 | + if (empty($_SERVER['REQUEST_URI'])) { | |
| 3631 | + return ''; | |
| 3632 | + } | |
| 3633 | + | |
| 3634 | + $path = wp_parse_url(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])), PHP_URL_PATH); | |
| 3635 | + if (!is_string($path) || '' === $path) { | |
| 3636 | + return ''; | |
| 3637 | + } | |
| 3638 | + | |
| 3639 | + // Strip the install's home path so subdirectory installs match too. | |
| 3640 | + $home_path = (string) wp_parse_url(home_url('/'), PHP_URL_PATH); | |
| 3641 | + if ('' !== $home_path && '/' !== $home_path && 0 === strpos($path, $home_path)) { | |
| 3642 | + $path = substr($path, strlen($home_path)); | |
| 3643 | + } | |
| 3644 | + | |
| 3645 | + $candidate = strtolower(trim($path, '/')); | |
| 3646 | + | |
| 3647 | + // One path segment ending in .xml. Anything nested is not a file we | |
| 3648 | + // publish to the web root. | |
| 3649 | + if ('' === $candidate || strpos($candidate, '/') !== false) { | |
| 3650 | + return ''; | |
| 3651 | + } | |
| 3652 | + | |
| 3653 | + return substr($candidate, -4) === '.xml' ? $candidate : ''; | |
| 3654 | + } | |
| 3655 | + | |
| 3530 | 3656 | public function maybe_serve_llms_txt(): void { |
| 3531 | 3657 | if (!$this->is_llms_txt_request()) { |
| 3532 | 3658 | return; |
| 3533 | 3659 | } |
| @@ -3728,11 +3854,22 @@ | ||
| 3728 | 3854 | // second copy of the save_post/term auto-generation hooks. |
| 3729 | 3855 | $generator = new \ThinkRank\SEO\Sitemap_Generator(false); |
| 3730 | 3856 | $settings = $generator->get_settings('site'); |
| 3731 | 3857 | |
| 3858 | + // "Can ThinkRank actually answer its sitemap URL right now?" In | |
| 3859 | + // static mode that means the file is on disk; in dynamic mode | |
| 3860 | + // maybe_serve_sitemap() answers it, so there is nothing to look | |
| 3861 | + // for. Keeping the file test as the only answer would have left | |
| 3862 | + // core's sitemap in place on every dynamic site, which is the | |
| 3863 | + // crawl conflict this suppression exists to prevent (#752). | |
| 3864 | + // The #346 behaviour is unchanged: a static site with nothing | |
| 3865 | + // published still falls through to core rather than 404ing. | |
| 3866 | + $can_serve = 'dynamic' === $generator->resolve_delivery_mode($settings) | |
| 3867 | + || $generator->primary_sitemap_file_exists($settings); | |
| 3868 | + | |
| 3732 | 3869 | $this->thinkrank_sitemap_enabled = !empty($settings['enabled']) |
| 3733 | 3870 | && !$this->publishes_at_core_sitemap_url($settings) |
| 3734 | - && $generator->primary_sitemap_file_exists($settings); | |
| 3871 | + && $can_serve; | |
| 3735 | 3872 | |
| 3736 | 3873 | if ($this->thinkrank_sitemap_enabled) { |
| 3737 | 3874 | $this->thinkrank_sitemap_url = $generator->get_primary_sitemap_url($settings); |
| 3738 | 3875 | } |