| @@ -38,16 +38,8 @@ | ||
| 38 | 38 | * Current post metadata |
| 39 | 39 | * |
| 40 | 40 | * @var array |
| 41 | 41 | */ |
| 42 | - /** | |
| 43 | - * Page-specific schemas the free tier renders on one page. | |
| 44 | - * | |
| 45 | - * @since 2.0.1 | |
| 46 | - * @var int | |
| 47 | - */ | |
| 48 | - private const FREE_PAGE_SCHEMA_LIMIT = 2; | |
| 49 | - | |
| 50 | 42 | private array $current_metadata = []; |
| 51 | 43 | |
| 52 | 44 | /** |
| 53 | 45 | * Term ID of the archive being rendered, when the request is a term archive. |
| @@ -120,8 +112,16 @@ | ||
| 120 | 112 | */ |
| 121 | 113 | private ?\ThinkRank\SEO\Image_SEO_Manager $image_seo_manager = null; |
| 122 | 114 | |
| 123 | 115 | /** |
| 116 | + * External Links Manager instance | |
| 117 | + * | |
| 118 | + * @since 2.5.0 | |
| 119 | + * @var \ThinkRank\SEO\External_Links_Manager|null | |
| 120 | + */ | |
| 121 | + private ?\ThinkRank\SEO\External_Links_Manager $external_links_manager = null; | |
| 122 | + | |
| 123 | + /** | |
| 124 | 124 | * Current page context |
| 125 | 125 | * |
| 126 | 126 | * @var string |
| 127 | 127 | */ |
| @@ -175,14 +175,14 @@ | ||
| 175 | 175 | |
| 176 | 176 | // Initialize Global SEO Schema Output |
| 177 | 177 | $this->initialize_global_seo_schema(); |
| 178 | 178 | |
| 179 | - // Initialize Google Analytics Tracking Manager | |
| 180 | - $this->initialize_google_analytics_tracking(); | |
| 181 | - | |
| 182 | 179 | // Initialize Image SEO Manager |
| 183 | 180 | $this->initialize_image_seo_manager(); |
| 184 | 181 | |
| 182 | + // Initialize External Links Manager (rel=nofollow / target=_blank) | |
| 183 | + $this->initialize_external_links_manager(); | |
| 184 | + | |
| 185 | 185 | // Initialize current post and context data first |
| 186 | 186 | add_action('wp', [$this, 'initialize_current_context']); |
| 187 | 187 | |
| 188 | 188 | // ...then let it be corrected if the request turns into a 404 later. |
| @@ -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 |
| @@ -299,8 +307,17 @@ | ||
| 299 | 307 | // Serve the Site Identity favicon through core's site-icon pipeline so |
| 300 | 308 | // wp_site_icon() outputs it on the front-end (and previews pick it up) |
| 301 | 309 | add_filter('get_site_icon_url', [$this, 'filter_site_icon_url'], 10, 2); |
| 302 | 310 | |
| 311 | + // Rewrite outbound anchors (rel=nofollow / target=_blank). Runs at | |
| 312 | + // the very end of the_content, after core's formatting AND after the | |
| 313 | + // image filter above, so it sees the markup the visitor will get. The | |
| 314 | + // stored post_content is never touched — turning the settings off | |
| 315 | + // restores the author's markup exactly. | |
| 316 | + add_filter('the_content', [$this, 'filter_external_links'], 100000); | |
| 317 | + add_filter('the_excerpt', [$this, 'filter_external_links'], 100000); | |
| 318 | + add_filter('widget_text_content', [$this, 'filter_external_links'], 100000); | |
| 319 | + | |
| 303 | 320 | // Process image SEO in content |
| 304 | 321 | add_filter('the_content', [$this, 'filter_content_images'], 99999); |
| 305 | 322 | add_filter('post_thumbnail_html', [$this, 'filter_content_images'], 11, 2); |
| 306 | 323 | add_filter('woocommerce_single_product_image_thumbnail_html', [$this, 'filter_content_images'], 11); |
| @@ -364,35 +381,61 @@ | ||
| 364 | 381 | $this->global_seo_schema->init(); |
| 365 | 382 | } |
| 366 | 383 | |
| 367 | 384 | /** |
| 368 | - * Initialize Google Analytics Tracking Manager | |
| 385 | + * Initialize Image SEO Manager | |
| 369 | 386 | * |
| 370 | 387 | * @return void |
| 371 | 388 | */ |
| 372 | - private function initialize_google_analytics_tracking(): void { | |
| 373 | - if (!class_exists('ThinkRank\\Frontend\\Google_Analytics_Tracking_Manager')) { | |
| 374 | - require_once THINKRANK_PLUGIN_DIR . 'includes/frontend/class-google-analytics-tracking-manager.php'; | |
| 389 | + private function initialize_image_seo_manager(): void { | |
| 390 | + if (!class_exists('ThinkRank\\SEO\\Image_SEO_Manager')) { | |
| 391 | + require_once THINKRANK_PLUGIN_DIR . 'includes/seo/class-image-seo-manager.php'; | |
| 375 | 392 | } |
| 376 | 393 | |
| 377 | - // Initialize Google Analytics Tracking Manager | |
| 378 | - new \ThinkRank\Frontend\Google_Analytics_Tracking_Manager(); | |
| 394 | + $this->image_seo_manager = new \ThinkRank\SEO\Image_SEO_Manager(); | |
| 379 | 395 | } |
| 380 | 396 | |
| 381 | 397 | /** |
| 382 | - * Initialize Image SEO Manager | |
| 398 | + * Initialize External Links Manager | |
| 383 | 399 | * |
| 400 | + * @since 2.5.0 | |
| 384 | 401 | * @return void |
| 385 | 402 | */ |
| 386 | - private function initialize_image_seo_manager(): void { | |
| 387 | - if (!class_exists('ThinkRank\\SEO\\Image_SEO_Manager')) { | |
| 388 | - require_once THINKRANK_PLUGIN_DIR . 'includes/seo/class-image-seo-manager.php'; | |
| 403 | + private function initialize_external_links_manager(): void { | |
| 404 | + if (!class_exists('ThinkRank\\SEO\\External_Links_Manager')) { | |
| 405 | + require_once THINKRANK_PLUGIN_DIR . 'includes/seo/class-external-links-manager.php'; | |
| 389 | 406 | } |
| 390 | 407 | |
| 391 | - $this->image_seo_manager = new \ThinkRank\SEO\Image_SEO_Manager(); | |
| 408 | + $this->external_links_manager = new \ThinkRank\SEO\External_Links_Manager(); | |
| 392 | 409 | } |
| 393 | 410 | |
| 394 | 411 | /** |
| 412 | + * Filter rendered content to annotate external links | |
| 413 | + * | |
| 414 | + * @since 2.5.0 | |
| 415 | + * @param mixed $content Content to filter; passed through untouched when | |
| 416 | + * it is not a string. | |
| 417 | + * @return mixed Filtered content. | |
| 418 | + */ | |
| 419 | + public function filter_external_links($content) { | |
| 420 | + // No return type: a filter value another plugin hands through as null | |
| 421 | + // or an object belongs to whoever set it, and coercing it to '' would | |
| 422 | + // silently drop their content on the floor. | |
| 423 | + if (!is_string($content) || $content === '' || !$this->external_links_manager) { | |
| 424 | + return $content; | |
| 425 | + } | |
| 426 | + | |
| 427 | + // Feeds carry the same markup to a reader we do not control; leave | |
| 428 | + // them as authored rather than annotating for a context that has no | |
| 429 | + // browser tab to open. | |
| 430 | + if (is_feed()) { | |
| 431 | + return $content; | |
| 432 | + } | |
| 433 | + | |
| 434 | + return $this->external_links_manager->process_content($content); | |
| 435 | + } | |
| 436 | + | |
| 437 | + /** | |
| 395 | 438 | * Filter content to inject image SEO attributes |
| 396 | 439 | * |
| 397 | 440 | * @since 1.0.0 |
| 398 | 441 | * @param string $content Content to filter |
| @@ -622,8 +665,14 @@ | ||
| 622 | 665 | * @param string $title Original title |
| 623 | 666 | * @return string Modified title |
| 624 | 667 | */ |
| 625 | 668 | public function override_document_title($title): string { |
| 669 | + // A content type with metas switched off keeps whatever title the theme | |
| 670 | + // and WordPress produce (#660). | |
| 671 | + if (!$this->metas_enabled()) { | |
| 672 | + return $title; | |
| 673 | + } | |
| 674 | + | |
| 626 | 675 | // First priority: Post-specific ThinkRank metadata |
| 627 | 676 | if ($this->has_thinkrank_metadata() && !empty($this->current_metadata['title'])) { |
| 628 | 677 | return self::with_page_suffix($this->current_metadata['title']); |
| 629 | 678 | } |
| @@ -650,9 +699,86 @@ | ||
| 650 | 699 | * |
| 651 | 700 | * @param string $title Resolved title. |
| 652 | 701 | * @return string Title with the page indicator, when there is one. |
| 653 | 702 | */ |
| 703 | + /** | |
| 704 | + * The archive's subject, without the label WordPress prefixes it with. | |
| 705 | + * | |
| 706 | + * `get_the_archive_title()` returns "Month: September 2026", "Archives: | |
| 707 | + * Recipes", "Category: Uncategorized" — the label is core's, aimed at an | |
| 708 | + * archive heading on the page, and it reads badly in a browser tab, an | |
| 709 | + * og:title or a search result. Category, tag and author contexts already | |
| 710 | + * avoid it by using the raw name; the generic archive context did not, so | |
| 711 | + * date, custom-post-type and custom-taxonomy archives carried it (#640). | |
| 712 | + * | |
| 713 | + * Removed through core's own `get_the_archive_title_prefix` filter rather | |
| 714 | + * than by matching the prefix text, because that text is translated and | |
| 715 | + * differs per archive type — a string comparison would work in English and | |
| 716 | + * silently stop working everywhere else. | |
| 717 | + * | |
| 718 | + * A site that wants a prefix can put one in its title template, where it is | |
| 719 | + * visible and editable, instead of inheriting one it cannot see. | |
| 720 | + * | |
| 721 | + * @since 2.7.0 | |
| 722 | + * | |
| 723 | + * @return string Archive subject, with markup and the core prefix removed. | |
| 724 | + */ | |
| 725 | + private static function archive_subject(): string { | |
| 726 | + $drop_prefix = static function (): string { | |
| 727 | + return ''; | |
| 728 | + }; | |
| 729 | + | |
| 730 | + add_filter('get_the_archive_title_prefix', $drop_prefix, 99); | |
| 731 | + | |
| 732 | + $title = (string) get_the_archive_title(); | |
| 733 | + | |
| 734 | + remove_filter('get_the_archive_title_prefix', $drop_prefix, 99); | |
| 735 | + | |
| 736 | + // The <span> core wraps the subject in survives the prefix filter. | |
| 737 | + return trim(wp_strip_all_tags($title)); | |
| 738 | + } | |
| 739 | + | |
| 740 | + /** | |
| 741 | + * Remove HTML from a title that is about to be emitted. | |
| 742 | + * | |
| 743 | + * A title carrying markup is broken twice over, in two different ways, and | |
| 744 | + * both were reaching real pages: inside `<title>` the tags render literally, | |
| 745 | + * because that element is RCDATA and never parses them; inside `og:title` | |
| 746 | + * and `twitter:title` they are attribute-escaped, so the reader sees | |
| 747 | + * `<em>` as visible text (#640). | |
| 748 | + * | |
| 749 | + * Applied at the point of emission rather than at each source, so it covers | |
| 750 | + * every branch that can produce a title — post meta, Global SEO templates, | |
| 751 | + * Site Identity templates — without each having to remember. | |
| 752 | + * | |
| 753 | + * Unconditional rather than a setting: there is no title for which markup is | |
| 754 | + * the correct output. The filter is the escape hatch for anyone who | |
| 755 | + * disagrees, and lets a site keep entities it deliberately encoded. | |
| 756 | + * | |
| 757 | + * @since 2.7.0 | |
| 758 | + * | |
| 759 | + * @param string $title Title about to be emitted. | |
| 760 | + * @return string Title with any markup removed. | |
| 761 | + */ | |
| 762 | + public static function strip_title_tags(string $title): string { | |
| 763 | + /** | |
| 764 | + * Filter whether HTML is stripped from generated titles. | |
| 765 | + * | |
| 766 | + * @since 2.7.0 | |
| 767 | + * | |
| 768 | + * @param bool $strip Whether to strip. Default true. | |
| 769 | + * @param string $title The title being emitted. | |
| 770 | + */ | |
| 771 | + if (!apply_filters('thinkrank_strip_title_tags', true, $title)) { | |
| 772 | + return $title; | |
| 773 | + } | |
| 774 | + | |
| 775 | + return trim(wp_strip_all_tags($title)); | |
| 776 | + } | |
| 777 | + | |
| 654 | 778 | public static function with_page_suffix(string $title): string { |
| 779 | + $title = self::strip_title_tags($title); | |
| 780 | + | |
| 655 | 781 | $page = self::current_page_number(); |
| 656 | 782 | |
| 657 | 783 | if ($page <= 1 || '' === $title) { |
| 658 | 784 | return $title; |
| @@ -677,8 +803,12 @@ | ||
| 677 | 803 | * @param string $sep Title separator |
| 678 | 804 | * @return string Modified title |
| 679 | 805 | */ |
| 680 | 806 | public function override_wp_title(string $title, string $sep = ''): string { |
| 807 | + if (!$this->metas_enabled()) { | |
| 808 | + return $title; | |
| 809 | + } | |
| 810 | + | |
| 681 | 811 | // First priority: Post-specific ThinkRank metadata |
| 682 | 812 | if ($this->has_thinkrank_metadata() && !empty($this->current_metadata['title'])) { |
| 683 | 813 | $site_name = get_bloginfo('name'); |
| 684 | 814 | return self::with_page_suffix( |
| @@ -695,8 +825,25 @@ | ||
| 695 | 825 | return $title; |
| 696 | 826 | } |
| 697 | 827 | |
| 698 | 828 | /** |
| 829 | + * Whether ThinkRank owns the title and meta description for this request. | |
| 830 | + * | |
| 831 | + * Metas are on site-wide by default; the per-content-type matrix can switch | |
| 832 | + * them off for one content type, in which case ThinkRank stops overriding | |
| 833 | + * the document title and prints no meta description (#660). | |
| 834 | + * | |
| 835 | + * @since 2.5.0 | |
| 836 | + * @return bool | |
| 837 | + */ | |
| 838 | + private function metas_enabled(): bool { | |
| 839 | + return \ThinkRank\SEO\Content_Type_Settings::is_enabled_for_current( | |
| 840 | + \ThinkRank\SEO\Content_Type_Settings::FEATURE_META, | |
| 841 | + true | |
| 842 | + ); | |
| 843 | + } | |
| 844 | + | |
| 845 | + /** | |
| 699 | 846 | * Output meta description (HIGH PRIORITY) |
| 700 | 847 | * Priority: Post-specific metadata > Global SEO templates > Site Identity templates > WordPress defaults |
| 701 | 848 | * |
| 702 | 849 | * Author archives are skipped entirely: Author_Archives_Manager owns that |
| @@ -711,8 +858,12 @@ | ||
| 711 | 858 | if (is_author()) { |
| 712 | 859 | return; |
| 713 | 860 | } |
| 714 | 861 | |
| 862 | + if (!$this->metas_enabled()) { | |
| 863 | + return; | |
| 864 | + } | |
| 865 | + | |
| 715 | 866 | $description = $this->get_meta_description(); |
| 716 | 867 | |
| 717 | 868 | if ($description) { |
| 718 | 869 | // Output main ThinkRank SEO header comment (only once) |
| @@ -718,11 +869,13 @@ | ||
| 718 | 869 | // Output main ThinkRank SEO header comment (only once) |
| 719 | 870 | self::note_opening_comment(); |
| 720 | 871 | |
| 721 | 872 | // Ensure description is within optimal length (150-160 characters) |
| 722 | - if (strlen($description) > 160) { | |
| 723 | - $description = wp_trim_words($description, 25, '...'); | |
| 724 | - } | |
| 873 | + // Measure and cut in CHARACTERS. strlen() counts bytes, so a Thai or | |
| 874 | + // CJK description tripped this limit at a third of its length, and | |
| 875 | + // wp_trim_words() then cut by a unit the locale chooses — 25 words in | |
| 876 | + // English, 25 characters in Thai (#687). | |
| 877 | + $description = \ThinkRank\Core\Seo_Text::trim_to_length($description); | |
| 725 | 878 | |
| 726 | 879 | echo "<!-- ThinkRank SEO Meta Description -->\n"; |
| 727 | 880 | echo '<meta name="description" content="' . esc_attr($description) . '" />' . "\n"; |
| 728 | 881 | echo "<!-- /ThinkRank SEO Meta Description -->\n"; |
| @@ -773,8 +926,34 @@ | ||
| 773 | 926 | echo "<!-- /ThinkRank SEO Meta Tags -->\n"; |
| 774 | 927 | } |
| 775 | 928 | |
| 776 | 929 | /** |
| 930 | + * Build the basic robots directive list from a set of robots flags. | |
| 931 | + * | |
| 932 | + * Shared by the search/404 branch of get_robots_meta_content() so those | |
| 933 | + * pages resolve their directives through the same rules as everything else | |
| 934 | + * rather than a hardcoded literal. | |
| 935 | + * | |
| 936 | + * @since 2.5.0 | |
| 937 | + * @param array $settings Robots flags (index/noindex/nofollow/...). | |
| 938 | + * @return string[] Directives. | |
| 939 | + */ | |
| 940 | + private static function build_robots_directives(array $settings): array { | |
| 941 | + $robots = []; | |
| 942 | + | |
| 943 | + $robots[] = !empty($settings['noindex']) ? 'noindex' : 'index'; | |
| 944 | + $robots[] = !empty($settings['nofollow']) ? 'nofollow' : 'follow'; | |
| 945 | + | |
| 946 | + foreach (['noarchive', 'noimageindex', 'nosnippet'] as $directive) { | |
| 947 | + if (!empty($settings[$directive])) { | |
| 948 | + $robots[] = $directive; | |
| 949 | + } | |
| 950 | + } | |
| 951 | + | |
| 952 | + return $robots; | |
| 953 | + } | |
| 954 | + | |
| 955 | + /** | |
| 777 | 956 | * Get robots meta content based on context and settings |
| 778 | 957 | * |
| 779 | 958 | * @return string Robots meta content |
| 780 | 959 | */ |
| @@ -780,13 +959,22 @@ | ||
| 780 | 959 | */ |
| 781 | 960 | private function get_robots_meta_content(): string { |
| 782 | 961 | $robots = []; |
| 783 | 962 | |
| 784 | - // 404 and search results must never be indexed, regardless of the | |
| 785 | - // configured global/post-type directives. Links are still followed so | |
| 786 | - // crawlers can discover the rest of the site. | |
| 963 | + // 404 and search results are noindex/follow by default — the behaviour | |
| 964 | + // that used to be hardcoded here. It is now settings-driven (#660): the | |
| 965 | + // Content Type Matrix can give either its own robots directives, and an | |
| 966 | + // install that never touched them resolves to exactly the old pair. | |
| 787 | 967 | if (is_404() || is_search()) { |
| 788 | - $robots = apply_filters('thinkrank_robots_meta', ['noindex', 'follow']); | |
| 968 | + $entity = is_404() | |
| 969 | + ? \ThinkRank\SEO\Content_Type_Settings::ENTITY_404 | |
| 970 | + : \ThinkRank\SEO\Content_Type_Settings::ENTITY_SEARCH; | |
| 971 | + | |
| 972 | + $robots = self::build_robots_directives( | |
| 973 | + \ThinkRank\SEO\Content_Type_Settings::resolve_robots_meta($entity) | |
| 974 | + ); | |
| 975 | + | |
| 976 | + $robots = apply_filters('thinkrank_robots_meta', $robots); | |
| 789 | 977 | return implode(', ', array_unique($robots)); |
| 790 | 978 | } |
| 791 | 979 | |
| 792 | 980 | // 1. Get global robot meta settings (Base) |
| @@ -815,8 +1003,24 @@ | ||
| 815 | 1003 | $current_settings = array_merge($current_settings, $global_seo_settings[$post_type]['robots_meta']); |
| 816 | 1004 | } |
| 817 | 1005 | } |
| 818 | 1006 | |
| 1007 | + // 2b. Apply the per-entity directives for the non-singular content | |
| 1008 | + // types the matrix covers — taxonomy archives plus author and date | |
| 1009 | + // archives. Terms keep their own per-term override, applied further | |
| 1010 | + // down so it still wins over the taxonomy-wide value (#660). | |
| 1011 | + if (!is_singular()) { | |
| 1012 | + $entity_key = \ThinkRank\SEO\Content_Type_Settings::current_entity_key(); | |
| 1013 | + | |
| 1014 | + if ($entity_key !== null) { | |
| 1015 | + $entity_settings = \ThinkRank\SEO\Content_Type_Settings::get_entity_settings($entity_key); | |
| 1016 | + | |
| 1017 | + if (!empty($entity_settings['robots_meta_enabled']) && is_array($entity_settings['robots_meta'] ?? null)) { | |
| 1018 | + $current_settings = array_merge($current_settings, $entity_settings['robots_meta']); | |
| 1019 | + } | |
| 1020 | + } | |
| 1021 | + } | |
| 1022 | + | |
| 819 | 1023 | // Determine Index/Noindex based on merged settings |
| 820 | 1024 | // Priority: if noindex is true, it overrides index |
| 821 | 1025 | if (!empty($current_settings['noindex'])) { |
| 822 | 1026 | $robots[] = 'noindex'; |
| @@ -1183,9 +1387,9 @@ | ||
| 1183 | 1387 | * |
| 1184 | 1388 | * @param array $og_tags Open Graph tags array |
| 1185 | 1389 | * @return void |
| 1186 | 1390 | */ |
| 1187 | - private function output_social_og_tags(array $og_tags): void { | |
| 1391 | + private function output_social_og_tags(array $og_tags, array $extra_images = []): void { | |
| 1188 | 1392 | // Honor the thinkrank_og_type filter here too — this "Enhanced" path is |
| 1189 | 1393 | // the active OG emitter, so add-ons (e.g. Pro's WooCommerce module which |
| 1190 | 1394 | // sets 'product' on product pages) must be applied to it, not only to |
| 1191 | 1395 | // output_open_graph_tags(). |
| @@ -1229,12 +1433,62 @@ | ||
| 1229 | 1433 | echo '<meta property="' . esc_attr($property) . '" content="' . $this->esc_meta_value($property, $content) . '" />' . "\n"; |
| 1230 | 1434 | } |
| 1231 | 1435 | } |
| 1232 | 1436 | |
| 1437 | + // Alternatives, after the primary and everything belonging to it. | |
| 1438 | + // Order is the whole point: a consumer reads og:image tags in document | |
| 1439 | + // order and treats the first as primary, and a structured property | |
| 1440 | + // attaches to the most recently declared image — so each alternative's | |
| 1441 | + // companions have to follow its own URL, not be grouped at the end. | |
| 1442 | + self::output_extra_og_images($extra_images); | |
| 1443 | + | |
| 1233 | 1444 | echo "<!-- /ThinkRank SEO Open Graph Tags -->\n"; |
| 1234 | 1445 | } |
| 1235 | 1446 | |
| 1236 | 1447 | /** |
| 1448 | + * Emit the secondary og:image tags a page offers. | |
| 1449 | + * | |
| 1450 | + * Shared by the enhanced and basic emitters so both describe an | |
| 1451 | + * alternative image the same way (#636). | |
| 1452 | + * | |
| 1453 | + * @since 2.7.0 | |
| 1454 | + * | |
| 1455 | + * @param array $images Each with url, and width/height/type/alt where known. | |
| 1456 | + * @return void | |
| 1457 | + */ | |
| 1458 | + private static function output_extra_og_images(array $images): void { | |
| 1459 | + foreach ($images as $image) { | |
| 1460 | + $url = isset($image['url']) ? (string) $image['url'] : ''; | |
| 1461 | + | |
| 1462 | + if ('' === $url) { | |
| 1463 | + continue; | |
| 1464 | + } | |
| 1465 | + | |
| 1466 | + echo '<meta property="og:image" content="' . esc_url($url) . '" />' . "\n"; | |
| 1467 | + | |
| 1468 | + if (strpos($url, 'https://') === 0) { | |
| 1469 | + echo '<meta property="og:image:secure_url" content="' . esc_url($url) . '" />' . "\n"; | |
| 1470 | + } | |
| 1471 | + | |
| 1472 | + // Only what is actually known: a dimension guessed for a remote | |
| 1473 | + // image is a number a consumer lays a card out with before it has | |
| 1474 | + // fetched the file. | |
| 1475 | + if (!empty($image['width']) && !empty($image['height'])) { | |
| 1476 | + echo '<meta property="og:image:width" content="' . esc_attr((string) $image['width']) . '" />' . "\n"; | |
| 1477 | + echo '<meta property="og:image:height" content="' . esc_attr((string) $image['height']) . '" />' . "\n"; | |
| 1478 | + } | |
| 1479 | + | |
| 1480 | + if (!empty($image['type'])) { | |
| 1481 | + echo '<meta property="og:image:type" content="' . esc_attr((string) $image['type']) . '" />' . "\n"; | |
| 1482 | + } | |
| 1483 | + | |
| 1484 | + if (!empty($image['alt'])) { | |
| 1485 | + echo '<meta property="og:image:alt" content="' . esc_attr((string) $image['alt']) . '" />' . "\n"; | |
| 1486 | + } | |
| 1487 | + } | |
| 1488 | + } | |
| 1489 | + | |
| 1490 | + /** | |
| 1237 | 1491 | * Output social media Twitter Card tags from Social Meta Manager |
| 1238 | 1492 | * |
| 1239 | 1493 | * @param array $twitter_tags Twitter Card tags array |
| 1240 | 1494 | * @return void |
| @@ -1360,8 +1614,17 @@ | ||
| 1360 | 1614 | * |
| 1361 | 1615 | * @return void |
| 1362 | 1616 | */ |
| 1363 | 1617 | public function output_open_graph_tags(): void { |
| 1618 | + // Per-content-type Open Graph switch. 'inherit' (the default) keeps the | |
| 1619 | + // site-wide Social Media setting, which the emitters below read (#660). | |
| 1620 | + if (!\ThinkRank\SEO\Content_Type_Settings::is_enabled_for_current( | |
| 1621 | + \ThinkRank\SEO\Content_Type_Settings::FEATURE_OPEN_GRAPH, | |
| 1622 | + true | |
| 1623 | + )) { | |
| 1624 | + return; | |
| 1625 | + } | |
| 1626 | + | |
| 1364 | 1627 | // An error page has no shareable identity. Emitting Open Graph here |
| 1365 | 1628 | // advertised the homepage as the og:url of a URL that does not exist. |
| 1366 | 1629 | if ($this->current_context === '404') { |
| 1367 | 1630 | return; |
| @@ -1388,9 +1651,12 @@ | ||
| 1388 | 1651 | // The Social Meta Manager ran, so it owns Open Graph output. If OG is |
| 1389 | 1652 | // toggled off, emit nothing — do NOT fall through to the basic |
| 1390 | 1653 | // emitter (which would re-add a full OG block despite the toggle). |
| 1391 | 1654 | if (!empty($social_data['og_enabled'])) { |
| 1392 | - $this->output_social_og_tags($social_data['og_tags']); | |
| 1655 | + $this->output_social_og_tags( | |
| 1656 | + $social_data['og_tags'], | |
| 1657 | + $social_data['og_extra_images'] ?? [] | |
| 1658 | + ); | |
| 1393 | 1659 | } |
| 1394 | 1660 | return; |
| 1395 | 1661 | } |
| 1396 | 1662 | |
| @@ -1458,9 +1724,9 @@ | ||
| 1458 | 1724 | // "There is no excerpt because this is a protected post." placeholder, |
| 1459 | 1725 | // so this is not a leak — but publishing that sentence as the social |
| 1460 | 1726 | // description is worse than publishing none (#363). |
| 1461 | 1727 | if (!$description && !$this->is_content_password_protected()) { |
| 1462 | - $description = is_singular() ? wp_trim_words(get_the_excerpt(), 30) : get_bloginfo('description'); | |
| 1728 | + $description = is_singular() ? \ThinkRank\Core\Seo_Text::trim_words(get_the_excerpt(), 30) : get_bloginfo('description'); | |
| 1463 | 1729 | } |
| 1464 | 1730 | |
| 1465 | 1731 | $url = is_singular() ? get_permalink() : home_url(); |
| 1466 | 1732 | $site_name = $this->site_identity_data && !empty($this->site_identity_data['identity']['site_name']) |
| @@ -1488,11 +1754,11 @@ | ||
| 1488 | 1754 | $og_type = apply_filters('thinkrank_og_type', $og_type); |
| 1489 | 1755 | |
| 1490 | 1756 | echo "<!-- ThinkRank SEO Open Graph Meta Tags -->\n"; |
| 1491 | 1757 | echo "<meta property=\"og:type\" content=\"" . esc_attr($og_type) . "\" />\n"; |
| 1492 | - echo "<meta property=\"og:title\" content=\"" . esc_attr($title) . "\" />\n"; | |
| 1758 | + echo "<meta property=\"og:title\" content=\"" . esc_attr(self::strip_title_tags($title)) . "\" />\n"; | |
| 1493 | 1759 | echo "<meta property=\"og:description\" content=\"" . esc_attr($description) . "\" />\n"; |
| 1494 | - echo "<meta property=\"og:url\" content=\"" . esc_url($url) . "\" />\n"; | |
| 1760 | + echo "<meta property=\"og:url\" content=\"" . esc_url(\ThinkRank\SEO\Url_Scheme::apply($url)) . "\" />\n"; | |
| 1495 | 1761 | echo "<meta property=\"og:site_name\" content=\"" . esc_attr($site_name) . "\" />\n"; |
| 1496 | 1762 | /** |
| 1497 | 1763 | * Filter the og:locale value. |
| 1498 | 1764 | * |
| @@ -1509,14 +1775,17 @@ | ||
| 1509 | 1775 | $og_locale = (string) apply_filters('thinkrank_og_locale', get_locale()); |
| 1510 | 1776 | echo "<meta property=\"og:locale\" content=\"" . esc_attr($og_locale) . "\" />\n"; |
| 1511 | 1777 | |
| 1512 | 1778 | // Add OG image — per-post override > featured image |
| 1779 | + $primary_og_image = ''; | |
| 1513 | 1780 | if (is_singular() && $this->current_post_id) { |
| 1514 | 1781 | if (!empty($og_image_override)) { |
| 1782 | + $primary_og_image = (string) $og_image_override; | |
| 1515 | 1783 | echo "<meta property=\"og:image\" content=\"" . esc_url($og_image_override) . "\" />\n"; |
| 1516 | 1784 | echo "<meta property=\"og:image:secure_url\" content=\"" . esc_url($og_image_override) . "\" />\n"; |
| 1517 | 1785 | } elseif (has_post_thumbnail($this->current_post_id)) { |
| 1518 | 1786 | $image_url = get_the_post_thumbnail_url($this->current_post_id, 'large'); |
| 1787 | + $primary_og_image = (string) $image_url; | |
| 1519 | 1788 | echo "<meta property=\"og:image\" content=\"" . esc_url($image_url) . "\" />\n"; |
| 1520 | 1789 | echo "<meta property=\"og:image:secure_url\" content=\"" . esc_url($image_url) . "\" />\n"; |
| 1521 | 1790 | |
| 1522 | 1791 | // Get image dimensions and alt text |
| @@ -1545,8 +1814,30 @@ | ||
| 1545 | 1814 | echo "<meta property=\"og:image:alt\" content=\"" . esc_attr($image_alt) . "\" />\n"; |
| 1546 | 1815 | } |
| 1547 | 1816 | } |
| 1548 | 1817 | |
| 1818 | + // Alternatives, same as the enhanced emitter above. This path only | |
| 1819 | + // runs when the Social Meta Manager is unavailable, but the issue | |
| 1820 | + // reported against it (#636) and a site that lands here should not | |
| 1821 | + // silently lose a feature it switched on. | |
| 1822 | + if (!empty($primary_og_image)) { | |
| 1823 | + $social_settings = $this->social_manager | |
| 1824 | + ? $this->social_manager->get_settings( | |
| 1825 | + $this->current_context === 'homepage' ? 'site' : $this->current_context, | |
| 1826 | + $this->current_post_id | |
| 1827 | + ) | |
| 1828 | + : []; | |
| 1829 | + | |
| 1830 | + if (!empty($social_settings['og_multiple_images'])) { | |
| 1831 | + self::output_extra_og_images( | |
| 1832 | + \ThinkRank\SEO\Social_Images::additional( | |
| 1833 | + (int) $this->current_post_id, | |
| 1834 | + $primary_og_image | |
| 1835 | + ) | |
| 1836 | + ); | |
| 1837 | + } | |
| 1838 | + } | |
| 1839 | + | |
| 1549 | 1840 | // Add article specific tags for posts only |
| 1550 | 1841 | if ($og_type === 'article') { |
| 1551 | 1842 | echo '<meta property="article:published_time" content="' . esc_attr(get_the_date('c', $this->current_post_id)) . '" />' . "\n"; |
| 1552 | 1843 | echo '<meta property="article:modified_time" content="' . esc_attr(get_the_modified_date('c', $this->current_post_id)) . '" />' . "\n"; |
| @@ -1574,8 +1865,16 @@ | ||
| 1574 | 1865 | * |
| 1575 | 1866 | * @return void |
| 1576 | 1867 | */ |
| 1577 | 1868 | public function output_twitter_card_tags(): void { |
| 1869 | + // Per-content-type Twitter card switch; see output_open_graph_tags(). | |
| 1870 | + if (!\ThinkRank\SEO\Content_Type_Settings::is_enabled_for_current( | |
| 1871 | + \ThinkRank\SEO\Content_Type_Settings::FEATURE_TWITTER, | |
| 1872 | + true | |
| 1873 | + )) { | |
| 1874 | + return; | |
| 1875 | + } | |
| 1876 | + | |
| 1578 | 1877 | // Same reasoning as the Open Graph block: nothing on a 404 is shareable. |
| 1579 | 1878 | if ($this->current_context === '404') { |
| 1580 | 1879 | return; |
| 1581 | 1880 | } |
| @@ -1664,9 +1963,9 @@ | ||
| 1664 | 1963 | // "There is no excerpt because this is a protected post." placeholder, |
| 1665 | 1964 | // so this is not a leak — but publishing that sentence as the social |
| 1666 | 1965 | // description is worse than publishing none (#363). |
| 1667 | 1966 | if (!$description && !$this->is_content_password_protected()) { |
| 1668 | - $description = is_singular() ? wp_trim_words(get_the_excerpt(), 30) : get_bloginfo('description'); | |
| 1967 | + $description = is_singular() ? \ThinkRank\Core\Seo_Text::trim_words(get_the_excerpt(), 30) : get_bloginfo('description'); | |
| 1669 | 1968 | } |
| 1670 | 1969 | |
| 1671 | 1970 | // Determine card type based on image availability |
| 1672 | 1971 | $card_type = 'summary'; |
| @@ -1675,9 +1974,9 @@ | ||
| 1675 | 1974 | } |
| 1676 | 1975 | |
| 1677 | 1976 | echo "<!-- ThinkRank SEO Twitter Card Meta Tags -->\n"; |
| 1678 | 1977 | echo '<meta name="twitter:card" content="' . esc_attr($card_type) . '" />' . "\n"; |
| 1679 | - echo "<meta name=\"twitter:title\" content=\"" . esc_attr($title) . "\" />\n"; | |
| 1978 | + echo "<meta name=\"twitter:title\" content=\"" . esc_attr(self::strip_title_tags($title)) . "\" />\n"; | |
| 1680 | 1979 | echo "<meta name=\"twitter:description\" content=\"" . esc_attr($description) . "\" />\n"; |
| 1681 | 1980 | |
| 1682 | 1981 | // Add Twitter image with proper fallback priority |
| 1683 | 1982 | $twitter_image_url = $this->get_twitter_image_with_fallback(); |
| @@ -1752,8 +2051,13 @@ | ||
| 1752 | 2051 | if (empty($canonical_url)) { |
| 1753 | 2052 | return; |
| 1754 | 2053 | } |
| 1755 | 2054 | |
| 2055 | + // After the filter, so a canonical an add-on supplied is normalized | |
| 2056 | + // too — and a cross-domain one is left alone, since Url_Scheme only | |
| 2057 | + // touches URLs on this site's own host. | |
| 2058 | + $canonical_url = \ThinkRank\SEO\Url_Scheme::apply($canonical_url); | |
| 2059 | + | |
| 1756 | 2060 | echo "<!-- ThinkRank SEO Canonical URL -->\n"; |
| 1757 | 2061 | echo "<link rel=\"canonical\" href=\"" . esc_url($canonical_url) . "\" />\n"; |
| 1758 | 2062 | echo "<!-- /ThinkRank SEO Canonical URL -->\n"; |
| 1759 | 2063 | |
| @@ -1800,9 +2104,9 @@ | ||
| 1800 | 2104 | |
| 1801 | 2105 | if ($current > 1) { |
| 1802 | 2106 | printf( |
| 1803 | 2107 | "<link rel=\"prev\" href=\"%s\" />\n", |
| 1804 | - esc_url(self::with_pagination($base, $current - 1)) | |
| 2108 | + esc_url(\ThinkRank\SEO\Url_Scheme::apply(self::with_pagination($base, $current - 1))) | |
| 1805 | 2109 | ); |
| 1806 | 2110 | } |
| 1807 | 2111 | |
| 1808 | 2112 | if ($current < $total) { |
| @@ -1807,9 +2111,9 @@ | ||
| 1807 | 2111 | |
| 1808 | 2112 | if ($current < $total) { |
| 1809 | 2113 | printf( |
| 1810 | 2114 | "<link rel=\"next\" href=\"%s\" />\n", |
| 1811 | - esc_url(self::with_pagination($base, $current + 1)) | |
| 2115 | + esc_url(\ThinkRank\SEO\Url_Scheme::apply(self::with_pagination($base, $current + 1))) | |
| 1812 | 2116 | ); |
| 1813 | 2117 | } |
| 1814 | 2118 | } |
| 1815 | 2119 | |
| @@ -2309,9 +2613,9 @@ | ||
| 2309 | 2613 | // Stripped: get_the_archive_title() wraps its subject in a |
| 2310 | 2614 | // <span>, and this placeholder feeds the document <title> as |
| 2311 | 2615 | // well as og:title and twitter:title — a date archive rendered |
| 2312 | 2616 | // as "Month: <span>August 2026</span> | Site". |
| 2313 | - $placeholders['%archive_title%'] = wp_strip_all_tags((string) get_the_archive_title()); | |
| 2617 | + $placeholders['%archive_title%'] = self::archive_subject(); | |
| 2314 | 2618 | break; |
| 2315 | 2619 | |
| 2316 | 2620 | case 'homepage': |
| 2317 | 2621 | // The page template resolved for a static posts page needs the |
| @@ -2506,11 +2810,13 @@ | ||
| 2506 | 2810 | if ($description === '') { |
| 2507 | 2811 | return null; |
| 2508 | 2812 | } |
| 2509 | 2813 | |
| 2510 | - if (strlen($description) > 160) { | |
| 2511 | - $description = wp_trim_words($description, 25, '...'); | |
| 2512 | - } | |
| 2814 | + // Measure and cut in CHARACTERS. strlen() counts bytes, so a Thai or | |
| 2815 | + // CJK description tripped this limit at a third of its length, and | |
| 2816 | + // wp_trim_words() then cut by a unit the locale chooses — 25 words in | |
| 2817 | + // English, 25 characters in Thai (#687). | |
| 2818 | + $description = \ThinkRank\Core\Seo_Text::trim_to_length($description); | |
| 2513 | 2819 | |
| 2514 | 2820 | return $description; |
| 2515 | 2821 | } |
| 2516 | 2822 | |
| @@ -2557,11 +2863,13 @@ | ||
| 2557 | 2863 | $description = preg_replace('/\s+/', ' ', $description); |
| 2558 | 2864 | $description = trim($description); |
| 2559 | 2865 | |
| 2560 | 2866 | // Ensure description doesn't exceed recommended length (160 characters) |
| 2561 | - if (strlen($description) > 160) { | |
| 2562 | - $description = wp_trim_words($description, 25, '...'); | |
| 2563 | - } | |
| 2867 | + // Measure and cut in CHARACTERS. strlen() counts bytes, so a Thai or | |
| 2868 | + // CJK description tripped this limit at a third of its length, and | |
| 2869 | + // wp_trim_words() then cut by a unit the locale chooses — 25 words in | |
| 2870 | + // English, 25 characters in Thai (#687). | |
| 2871 | + $description = \ThinkRank\Core\Seo_Text::trim_to_length($description); | |
| 2564 | 2872 | |
| 2565 | 2873 | return $description; |
| 2566 | 2874 | } |
| 2567 | 2875 | |
| @@ -2628,9 +2936,19 @@ | ||
| 2628 | 2936 | |
| 2629 | 2937 | $page_specific_schemas = $this->schema_manager->get_deployed_schemas($context_type, $context_id); |
| 2630 | 2938 | |
| 2631 | 2939 | if (!empty($page_specific_schemas)) { |
| 2632 | - // Apply filter for Pro to allow multiple schemas | |
| 2940 | + // Every deployed schema is rendered, on every plan. How many a | |
| 2941 | + // page carries is decided when schemas are activated in the | |
| 2942 | + // editor, not trimmed here by plan (#673). | |
| 2943 | + | |
| 2944 | + /** | |
| 2945 | + * Filter the page-specific schemas rendered on the current page. | |
| 2946 | + * | |
| 2947 | + * @param array $page_specific_schemas Deployed schemas keyed by schema type. | |
| 2948 | + * @param string $context_type Context type (post, page, product, site). | |
| 2949 | + * @param int $context_id Post ID. | |
| 2950 | + */ | |
| 2633 | 2951 | $page_specific_schemas = apply_filters( |
| 2634 | 2952 | 'thinkrank_page_schemas_to_render', |
| 2635 | 2953 | $page_specific_schemas, |
| 2636 | 2954 | $context_type, |
| @@ -2636,27 +2954,8 @@ | ||
| 2636 | 2954 | $context_type, |
| 2637 | 2955 | $context_id |
| 2638 | 2956 | ); |
| 2639 | 2957 | |
| 2640 | - // Free tier renders at most self::FREE_PAGE_SCHEMA_LIMIT | |
| 2641 | - // page-specific schemas; Pro renders all of them. | |
| 2642 | - // | |
| 2643 | - // Both comments here used to say the free limit was 1 while the | |
| 2644 | - // code allowed 2 (#405). The number the code enforces is what | |
| 2645 | - // has shipped, so that is what stands — lowering it would take | |
| 2646 | - // a schema away from every free site on upgrade — and it now | |
| 2647 | - // lives in one named place instead of twice in prose and twice | |
| 2648 | - // in a literal. | |
| 2649 | - if (!\ThinkRank\Core\Plan_Config::is_pro() | |
| 2650 | - && count($page_specific_schemas) > self::FREE_PAGE_SCHEMA_LIMIT) { | |
| 2651 | - $page_specific_schemas = array_slice( | |
| 2652 | - $page_specific_schemas, | |
| 2653 | - 0, | |
| 2654 | - self::FREE_PAGE_SCHEMA_LIMIT, | |
| 2655 | - true | |
| 2656 | - ); | |
| 2657 | - } | |
| 2658 | - | |
| 2659 | 2958 | foreach ($page_specific_schemas as $schema_type => $schema_info) { |
| 2660 | 2959 | Schema_Graph::instance()->add_primary($schema_info['data'], (string) $schema_type, 'schema_manager'); |
| 2661 | 2960 | } |
| 2662 | 2961 | $has_schema_manager_output = true; |
| @@ -2720,17 +3019,41 @@ | ||
| 2720 | 3019 | if (!empty($description)) { |
| 2721 | 3020 | $schema['description'] = $description; |
| 2722 | 3021 | } |
| 2723 | 3022 | |
| 2724 | - $schema['potentialAction'] = [ | |
| 2725 | - '@type' => 'SearchAction', | |
| 2726 | - 'target' => [ | |
| 2727 | - '@type' => 'EntryPoint', | |
| 2728 | - 'urlTemplate' => home_url('/?s={search_term_string}'), | |
| 2729 | - ], | |
| 2730 | - 'query-input' => 'required name=search_term_string', | |
| 2731 | - ]; | |
| 3023 | + // Site Identity has accepted an alternate name since the setup wizard | |
| 3024 | + // shipped, and the MCP ability describes it as "published as schema | |
| 3025 | + // alternateName" — but no producer ever read it, so the promise was | |
| 3026 | + // false and every imported Yoast/Rank Math value sat unused (#692). | |
| 3027 | + $alternate_name = \ThinkRank\SEO\Site_Identity_Manager::alternate_name_for_schema($settings['alternate_name'] ?? null); | |
| 3028 | + if (null !== $alternate_name) { | |
| 3029 | + $schema['alternateName'] = $alternate_name; | |
| 3030 | + } | |
| 2732 | 3031 | |
| 3032 | + // The sitelinks searchbox switch was honoured only for a deployed | |
| 3033 | + // WebSite row; this live fallback added potentialAction unconditionally, | |
| 3034 | + // so website_enable_search = 0 still shipped the SearchAction (#688). | |
| 3035 | + // Absent means not configured, which stays enabled. | |
| 3036 | + $search_enabled = true; | |
| 3037 | + if ($this->schema_manager) { | |
| 3038 | + $schema_settings = $this->schema_manager->get_settings('site', null); | |
| 3039 | + | |
| 3040 | + if (array_key_exists('website_enable_search', $schema_settings)) { | |
| 3041 | + $search_enabled = !empty($schema_settings['website_enable_search']); | |
| 3042 | + } | |
| 3043 | + } | |
| 3044 | + | |
| 3045 | + if ($search_enabled) { | |
| 3046 | + $schema['potentialAction'] = [ | |
| 3047 | + '@type' => 'SearchAction', | |
| 3048 | + 'target' => [ | |
| 3049 | + '@type' => 'EntryPoint', | |
| 3050 | + 'urlTemplate' => home_url('/?s={search_term_string}'), | |
| 3051 | + ], | |
| 3052 | + 'query-input' => 'required name=search_term_string', | |
| 3053 | + ]; | |
| 3054 | + } | |
| 3055 | + | |
| 2733 | 3056 | return $schema; |
| 2734 | 3057 | } |
| 2735 | 3058 | |
| 2736 | 3059 | /** |
| @@ -2941,8 +3264,22 @@ | ||
| 2941 | 3264 | if (empty($settings['breadcrumbs_enabled'])) { |
| 2942 | 3265 | return; |
| 2943 | 3266 | } |
| 2944 | 3267 | |
| 3268 | + // Schema Manager's own breadcrumb switch. Only Site Identity's | |
| 3269 | + // breadcrumbs_enabled was consulted here, so enable_breadcrumbs_schema | |
| 3270 | + // = 0 removed a deployed BreadcrumbList row and left this live one | |
| 3271 | + // emitting the node anyway (#688). Absent means not configured, which | |
| 3272 | + // stays enabled. | |
| 3273 | + if ($this->schema_manager) { | |
| 3274 | + $schema_settings = $this->schema_manager->get_settings('site', null); | |
| 3275 | + | |
| 3276 | + if (array_key_exists('enable_breadcrumbs_schema', $schema_settings) | |
| 3277 | + && empty($schema_settings['enable_breadcrumbs_schema'])) { | |
| 3278 | + return; | |
| 3279 | + } | |
| 3280 | + } | |
| 3281 | + | |
| 2945 | 3282 | $breadcrumbs = $this->generate_breadcrumbs($settings); |
| 2946 | 3283 | |
| 2947 | 3284 | if (!empty($breadcrumbs['schema'])) { |
| 2948 | 3285 | Schema_Graph::instance()->add_supporting($breadcrumbs['schema'], 'BreadcrumbList'); |
| @@ -3197,8 +3534,102 @@ | ||
| 3197 | 3534 | * @since 1.32.0 |
| 3198 | 3535 | * |
| 3199 | 3536 | * @return void |
| 3200 | 3537 | */ |
| 3538 | + /** | |
| 3539 | + * Serve a ThinkRank sitemap document for this request, when it is one. | |
| 3540 | + * | |
| 3541 | + * Only acts in dynamic delivery mode. In static mode a real file exists and | |
| 3542 | + * the web server returns it without WordPress ever loading, so answering | |
| 3543 | + * here as well would mean two sources for the same bytes. | |
| 3544 | + * | |
| 3545 | + * @since 2.9.0 | |
| 3546 | + * | |
| 3547 | + * @return void | |
| 3548 | + */ | |
| 3549 | + public function maybe_serve_sitemap(): void { | |
| 3550 | + $filename = $this->requested_sitemap_filename(); | |
| 3551 | + if ('' === $filename) { | |
| 3552 | + return; | |
| 3553 | + } | |
| 3554 | + | |
| 3555 | + try { | |
| 3556 | + // Read-only instance: passing false keeps it from registering a | |
| 3557 | + // second copy of the auto-generation hooks. | |
| 3558 | + $generator = new \ThinkRank\SEO\Sitemap_Generator(false); | |
| 3559 | + $settings = $generator->get_settings('site'); | |
| 3560 | + | |
| 3561 | + if (empty($settings['enabled'])) { | |
| 3562 | + return; | |
| 3563 | + } | |
| 3564 | + | |
| 3565 | + if ('dynamic' !== $generator->resolve_delivery_mode($settings)) { | |
| 3566 | + return; | |
| 3567 | + } | |
| 3568 | + | |
| 3569 | + if (!$generator->publishes_document_name($filename, $settings)) { | |
| 3570 | + return; | |
| 3571 | + } | |
| 3572 | + | |
| 3573 | + $xml = $generator->render_document($filename, $settings); | |
| 3574 | + } catch (\Throwable $e) { | |
| 3575 | + // A failed render must not replace the sitemap with a fatal. Leave | |
| 3576 | + // the request alone so WordPress answers as it otherwise would. | |
| 3577 | + return; | |
| 3578 | + } | |
| 3579 | + | |
| 3580 | + if (!is_string($xml) || '' === trim($xml)) { | |
| 3581 | + return; | |
| 3582 | + } | |
| 3583 | + | |
| 3584 | + status_header(200); | |
| 3585 | + header('Content-Type: application/xml; charset=UTF-8'); | |
| 3586 | + header('X-Robots-Tag: noindex, follow', true); | |
| 3587 | + | |
| 3588 | + // Built XML, escaped by the builders as they assemble it; escaping the | |
| 3589 | + // document here would corrupt it. | |
| 3590 | + echo $xml; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 3591 | + exit; | |
| 3592 | + } | |
| 3593 | + | |
| 3594 | + /** | |
| 3595 | + * The sitemap file name this request is asking for, if it looks like one. | |
| 3596 | + * | |
| 3597 | + * Deliberately a cheap shape test. Whether the site actually publishes the | |
| 3598 | + * name is settled by the caller against the generator, so that a request | |
| 3599 | + * for someone else's sitemap is never answered here. | |
| 3600 | + * | |
| 3601 | + * @since 2.9.0 | |
| 3602 | + * | |
| 3603 | + * @return string File name, or '' when this is not a sitemap request. | |
| 3604 | + */ | |
| 3605 | + private function requested_sitemap_filename(): string { | |
| 3606 | + if (empty($_SERVER['REQUEST_URI'])) { | |
| 3607 | + return ''; | |
| 3608 | + } | |
| 3609 | + | |
| 3610 | + $path = wp_parse_url(sanitize_text_field(wp_unslash($_SERVER['REQUEST_URI'])), PHP_URL_PATH); | |
| 3611 | + if (!is_string($path) || '' === $path) { | |
| 3612 | + return ''; | |
| 3613 | + } | |
| 3614 | + | |
| 3615 | + // Strip the install's home path so subdirectory installs match too. | |
| 3616 | + $home_path = (string) wp_parse_url(home_url('/'), PHP_URL_PATH); | |
| 3617 | + if ('' !== $home_path && '/' !== $home_path && 0 === strpos($path, $home_path)) { | |
| 3618 | + $path = substr($path, strlen($home_path)); | |
| 3619 | + } | |
| 3620 | + | |
| 3621 | + $candidate = strtolower(trim($path, '/')); | |
| 3622 | + | |
| 3623 | + // One path segment ending in .xml. Anything nested is not a file we | |
| 3624 | + // publish to the web root. | |
| 3625 | + if ('' === $candidate || strpos($candidate, '/') !== false) { | |
| 3626 | + return ''; | |
| 3627 | + } | |
| 3628 | + | |
| 3629 | + return substr($candidate, -4) === '.xml' ? $candidate : ''; | |
| 3630 | + } | |
| 3631 | + | |
| 3201 | 3632 | public function maybe_serve_llms_txt(): void { |
| 3202 | 3633 | if (!$this->is_llms_txt_request()) { |
| 3203 | 3634 | return; |
| 3204 | 3635 | } |
| @@ -3399,11 +3830,22 @@ | ||
| 3399 | 3830 | // second copy of the save_post/term auto-generation hooks. |
| 3400 | 3831 | $generator = new \ThinkRank\SEO\Sitemap_Generator(false); |
| 3401 | 3832 | $settings = $generator->get_settings('site'); |
| 3402 | 3833 | |
| 3834 | + // "Can ThinkRank actually answer its sitemap URL right now?" In | |
| 3835 | + // static mode that means the file is on disk; in dynamic mode | |
| 3836 | + // maybe_serve_sitemap() answers it, so there is nothing to look | |
| 3837 | + // for. Keeping the file test as the only answer would have left | |
| 3838 | + // core's sitemap in place on every dynamic site, which is the | |
| 3839 | + // crawl conflict this suppression exists to prevent (#752). | |
| 3840 | + // The #346 behaviour is unchanged: a static site with nothing | |
| 3841 | + // published still falls through to core rather than 404ing. | |
| 3842 | + $can_serve = 'dynamic' === $generator->resolve_delivery_mode($settings) | |
| 3843 | + || $generator->primary_sitemap_file_exists($settings); | |
| 3844 | + | |
| 3403 | 3845 | $this->thinkrank_sitemap_enabled = !empty($settings['enabled']) |
| 3404 | 3846 | && !$this->publishes_at_core_sitemap_url($settings) |
| 3405 | - && $generator->primary_sitemap_file_exists($settings); | |
| 3847 | + && $can_serve; | |
| 3406 | 3848 | |
| 3407 | 3849 | if ($this->thinkrank_sitemap_enabled) { |
| 3408 | 3850 | $this->thinkrank_sitemap_url = $generator->get_primary_sitemap_url($settings); |
| 3409 | 3851 | } |