| @@ -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 | /** |
| @@ -1783,12 +1799,16 @@ | ||
| 1783 | 1799 | // Get image dimensions and alt text |
| 1784 | 1800 | $image_id = get_post_thumbnail_id($this->current_post_id); |
| 1785 | 1801 | $image_meta = wp_get_attachment_metadata($image_id); |
| 1786 | 1802 | if ($image_meta) { |
| 1803 | + // The `large` file being published, not the original it | |
| 1804 | + // was generated from: the metadata's own width and height | |
| 1805 | + // describe an image this tag does not point at (#847). | |
| 1806 | + $image_file = \ThinkRank\SEO\Attachment_Lookup::describe((int) $image_id, (string) $image_url); | |
| 1787 | 1807 | // SVGs (and other vector uploads) report 0x0 — emitting |
| 1788 | 1808 | // those as og:image dimensions is invalid, so skip them. |
| 1789 | - $og_width = isset($image_meta['width']) ? (int) $image_meta['width'] : 0; | |
| 1790 | - $og_height = isset($image_meta['height']) ? (int) $image_meta['height'] : 0; | |
| 1809 | + $og_width = $image_file['width']; | |
| 1810 | + $og_height = $image_file['height']; | |
| 1791 | 1811 | if ($og_width > 0 && $og_height > 0) { |
| 1792 | 1812 | echo "<meta property=\"og:image:width\" content=\"" . esc_attr($og_width) . "\" />\n"; |
| 1793 | 1813 | echo "<meta property=\"og:image:height\" content=\"" . esc_attr($og_height) . "\" />\n"; |
| 1794 | 1814 | } |
| @@ -1793,9 +1813,9 @@ | ||
| 1793 | 1813 | echo "<meta property=\"og:image:height\" content=\"" . esc_attr($og_height) . "\" />\n"; |
| 1794 | 1814 | } |
| 1795 | 1815 | // Derive the real mime type instead of hardcoding image/jpeg, |
| 1796 | 1816 | // which mislabels PNG/WebP featured images. |
| 1797 | - $image_mime = get_post_mime_type($image_id); | |
| 1817 | + $image_mime = $image_file['type']; | |
| 1798 | 1818 | if ($image_mime) { |
| 1799 | 1819 | echo "<meta property=\"og:image:type\" content=\"" . esc_attr($image_mime) . "\" />\n"; |
| 1800 | 1820 | } |
| 1801 | 1821 | } |
| @@ -2732,8 +2752,22 @@ | ||
| 2732 | 2752 | if ($archive_description) { |
| 2733 | 2753 | return $archive_description; |
| 2734 | 2754 | } |
| 2735 | 2755 | |
| 2756 | + // The blog-index homepage's own description (Site Identity | |
| 2757 | + // homepage_description, #897), in the same vocabulary as its title. A | |
| 2758 | + // static front page is a page, and its description is set on it. | |
| 2759 | + if (is_front_page() && is_home() && $this->site_identity_data && $this->site_identity_data['enabled']) { | |
| 2760 | + $identity = $this->site_identity_manager->get_settings('site'); | |
| 2761 | + $template = trim((string) ($identity['homepage_description'] ?? '')); | |
| 2762 | + if ($template !== '') { | |
| 2763 | + $homepage_description = trim($this->process_title_template($template, $this->get_title_placeholders())); | |
| 2764 | + if ($homepage_description !== '') { | |
| 2765 | + return $homepage_description; | |
| 2766 | + } | |
| 2767 | + } | |
| 2768 | + } | |
| 2769 | + | |
| 2736 | 2770 | // Third priority: Site Identity default meta description |
| 2737 | 2771 | if ($this->site_identity_data && $this->site_identity_data['enabled']) { |
| 2738 | 2772 | $settings = $this->site_identity_manager->get_settings('site'); |
| 2739 | 2773 | $default_description = $settings['default_meta_description'] ?? ''; |
| @@ -2946,10 +2980,22 @@ | ||
| 2946 | 2980 | $context_type, |
| 2947 | 2981 | $context_id |
| 2948 | 2982 | ); |
| 2949 | 2983 | |
| 2984 | + // A deployed node is a snapshot from Deploy time and outranks | |
| 2985 | + // the automatic node, so page and article types would publish | |
| 2986 | + // a frozen excerpt instead of the description the head | |
| 2987 | + // resolves. Give them the live one, as the automatic node has. | |
| 2988 | + $context_post = get_post($context_id); | |
| 2989 | + | |
| 2950 | 2990 | foreach ($page_specific_schemas as $schema_type => $schema_info) { |
| 2951 | - Schema_Graph::instance()->add_primary($schema_info['data'], (string) $schema_type, 'schema_manager'); | |
| 2991 | + $node = $schema_info['data']; | |
| 2992 | + | |
| 2993 | + if ($this->global_seo_schema && $context_post instanceof \WP_Post) { | |
| 2994 | + $node = $this->global_seo_schema->refresh_deployed_description($node, (string) $schema_type, $context_post); | |
| 2995 | + } | |
| 2996 | + | |
| 2997 | + Schema_Graph::instance()->add_primary($node, (string) $schema_type, 'schema_manager'); | |
| 2952 | 2998 | } |
| 2953 | 2999 | $has_schema_manager_output = true; |
| 2954 | 3000 | } |
| 2955 | 3001 | } |
| @@ -3007,8 +3053,12 @@ | ||
| 3007 | 3053 | 'url' => home_url('/'), |
| 3008 | 3054 | ]; |
| 3009 | 3055 | |
| 3010 | 3056 | $description = !empty($settings['site_description']) ? $settings['site_description'] : get_bloginfo('description'); |
| 3057 | + // The tagline is stored esc_html()'d by sanitize_option(), so a site | |
| 3058 | + // called "Fish & Chips" published `&` literally in its WebSite | |
| 3059 | + // node; nothing decodes JSON-LD downstream. | |
| 3060 | + $description = \ThinkRank\Core\Seo_Text::normalize_schema_text((string) $description); | |
| 3011 | 3061 | if (!empty($description)) { |
| 3012 | 3062 | $schema['description'] = $description; |
| 3013 | 3063 | } |
| 3014 | 3064 | |
| @@ -3526,8 +3576,102 @@ | ||
| 3526 | 3576 | * @since 1.32.0 |
| 3527 | 3577 | * |
| 3528 | 3578 | * @return void |
| 3529 | 3579 | */ |
| 3580 | + /** | |
| 3581 | + * Serve a ThinkRank sitemap document for this request, when it is one. | |
| 3582 | + * | |
| 3583 | + * Only acts in dynamic delivery mode. In static mode a real file exists and | |
| 3584 | + * the web server returns it without WordPress ever loading, so answering | |
| 3585 | + * here as well would mean two sources for the same bytes. | |
| 3586 | + * | |
| 3587 | + * @since 2.9.0 | |
| 3588 | + * | |
| 3589 | + * @return void | |
| 3590 | + */ | |
| 3591 | + public function maybe_serve_sitemap(): void { | |
| 3592 | + $filename = $this->requested_sitemap_filename(); | |
| 3593 | + if ('' === $filename) { | |
| 3594 | + return; | |
| 3595 | + } | |
| 3596 | + | |
| 3597 | + try { | |
| 3598 | + // Read-only instance: passing false keeps it from registering a | |
| 3599 | + // second copy of the auto-generation hooks. | |
| 3600 | + $generator = new \ThinkRank\SEO\Sitemap_Generator(false); | |
| 3601 | + $settings = $generator->get_settings('site'); | |
| 3602 | + | |
| 3603 | + if (empty($settings['enabled'])) { | |
| 3604 | + return; | |
| 3605 | + } | |
| 3606 | + | |
| 3607 | + if ('dynamic' !== $generator->resolve_delivery_mode($settings)) { | |
| 3608 | + return; | |
| 3609 | + } | |
| 3610 | + | |
| 3611 | + if (!$generator->publishes_document_name($filename, $settings)) { | |
| 3612 | + return; | |
| 3613 | + } | |
| 3614 | + | |
| 3615 | + $xml = $generator->render_document($filename, $settings); | |
| 3616 | + } catch (\Throwable $e) { | |
| 3617 | + // A failed render must not replace the sitemap with a fatal. Leave | |
| 3618 | + // the request alone so WordPress answers as it otherwise would. | |
| 3619 | + return; | |
| 3620 | + } | |
| 3621 | + | |
| 3622 | + if (!is_string($xml) || '' === trim($xml)) { | |
| 3623 | + return; | |
| 3624 | + } | |
| 3625 | + | |
| 3626 | + status_header(200); | |
| 3627 | + header('Content-Type: application/xml; charset=UTF-8'); | |
| 3628 | + header('X-Robots-Tag: noindex, follow', true); | |
| 3629 | + | |
| 3630 | + // Built XML, escaped by the builders as they assemble it; escaping the | |
| 3631 | + // document here would corrupt it. | |
| 3632 | + echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 3633 | + exit; | |
| 3634 | + } | |
| 3635 | + | |
| 3636 | + /** | |
| 3637 | + * The sitemap file name this request is asking for, if it looks like one. | |
| 3638 | + * | |
| 3639 | + * Deliberately a cheap shape test. Whether the site actually publishes the | |
| 3640 | + * name is settled by the caller against the generator, so that a request | |
| 3641 | + * for someone else's sitemap is never answered here. | |
| 3642 | + * | |
| 3643 | + * @since 2.9.0 | |
| 3644 | + * | |
| 3645 | + * @return string File name, or '' when this is not a sitemap request. | |
| 3646 | + */ | |
| 3647 | + private function requested_sitemap_filename(): string { | |
| 3648 | + if (empty($_SERVER['REQUEST_URI'])) { | |
| 3649 | + return ''; | |
| 3650 | + } | |
| 3651 | + | |
| 3652 | + $path = wp_parse_url(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])), PHP_URL_PATH); | |
| 3653 | + if (!is_string($path) || '' === $path) { | |
| 3654 | + return ''; | |
| 3655 | + } | |
| 3656 | + | |
| 3657 | + // Strip the install's home path so subdirectory installs match too. | |
| 3658 | + $home_path = (string) wp_parse_url(home_url('/'), PHP_URL_PATH); | |
| 3659 | + if ('' !== $home_path && '/' !== $home_path && 0 === strpos($path, $home_path)) { | |
| 3660 | + $path = substr($path, strlen($home_path)); | |
| 3661 | + } | |
| 3662 | + | |
| 3663 | + $candidate = strtolower(trim($path, '/')); | |
| 3664 | + | |
| 3665 | + // One path segment ending in .xml. Anything nested is not a file we | |
| 3666 | + // publish to the web root. | |
| 3667 | + if ('' === $candidate || strpos($candidate, '/') !== false) { | |
| 3668 | + return ''; | |
| 3669 | + } | |
| 3670 | + | |
| 3671 | + return substr($candidate, -4) === '.xml' ? $candidate : ''; | |
| 3672 | + } | |
| 3673 | + | |
| 3530 | 3674 | public function maybe_serve_llms_txt(): void { |
| 3531 | 3675 | if (!$this->is_llms_txt_request()) { |
| 3532 | 3676 | return; |
| 3533 | 3677 | } |
| @@ -3728,11 +3872,22 @@ | ||
| 3728 | 3872 | // second copy of the save_post/term auto-generation hooks. |
| 3729 | 3873 | $generator = new \ThinkRank\SEO\Sitemap_Generator(false); |
| 3730 | 3874 | $settings = $generator->get_settings('site'); |
| 3731 | 3875 | |
| 3876 | + // "Can ThinkRank actually answer its sitemap URL right now?" In | |
| 3877 | + // static mode that means the file is on disk; in dynamic mode | |
| 3878 | + // maybe_serve_sitemap() answers it, so there is nothing to look | |
| 3879 | + // for. Keeping the file test as the only answer would have left | |
| 3880 | + // core's sitemap in place on every dynamic site, which is the | |
| 3881 | + // crawl conflict this suppression exists to prevent (#752). | |
| 3882 | + // The #346 behaviour is unchanged: a static site with nothing | |
| 3883 | + // published still falls through to core rather than 404ing. | |
| 3884 | + $can_serve = 'dynamic' === $generator->resolve_delivery_mode($settings) | |
| 3885 | + || $generator->primary_sitemap_file_exists($settings); | |
| 3886 | + | |
| 3732 | 3887 | $this->thinkrank_sitemap_enabled = !empty($settings['enabled']) |
| 3733 | 3888 | && !$this->publishes_at_core_sitemap_url($settings) |
| 3734 | - && $generator->primary_sitemap_file_exists($settings); | |
| 3889 | + && $can_serve; | |
| 3735 | 3890 | |
| 3736 | 3891 | if ($this->thinkrank_sitemap_enabled) { |
| 3737 | 3892 | $this->thinkrank_sitemap_url = $generator->get_primary_sitemap_url($settings); |
| 3738 | 3893 | } |