PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.12.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.12.0
2.12.0 2.11.0 2.10.0 2.9.0 2.8.0 2.7.0 2.6.0 2.5.0 2.4.0 2.3.0 2.2.0 2.1.1 2.1.0 2.0.2 2.0.1 2.0.0 1.32.0 1.31.0 1.30.0 1.29.0 1.28.0 1.27.0 1.26.0 1.25.0 trunk All 53 releases
← All changes | includes/frontend/class-schema-graph.php +292 -170 2.4.0 → 2.12.0 View file →
@@ -87,23 +87,34 @@
87 87 'Book', 'Movie', 'Service', 'ImageObject', 'VideoObject',
88 88 ];
89 89
90 90 /**
91 - * Gutenberg FAQ block name.
91 + * Types that may carry a `breadcrumb` property.
92 + *
93 + * Schema.org limits `breadcrumb` to WebPage and its subtypes. Attaching it
94 + * to a BlogPosting, Product or Recipe primary fails validation with
95 + * "Unexpected property" on every URL; the standalone BreadcrumbList node is
96 + * emitted either way (#693).
97 + *
98 + * @since 2.7.0
99 + * @var array<int,string>
92 100 */
93 - private const FAQ_BLOCK = 'thinkrank/faq';
101 + private const BREADCRUMB_TYPES = [
102 + 'WebPage', 'AboutPage', 'CheckoutPage', 'CollectionPage', 'ContactPage',
103 + 'FAQPage', 'ItemPage', 'MedicalWebPage', 'ProfilePage', 'QAPage',
104 + 'RealEstateListing', 'SearchResultsPage', 'MediaGallery', 'ImageGallery',
105 + 'VideoGallery',
106 + ];
94 107
95 108 /**
96 - * Elementor FAQ widget name.
97 - */
98 - private const FAQ_WIDGET = 'thinkrank-faq';
99 -
100 - /**
101 - * Bricks FAQ element name.
109 + * The FAQ surfaces themselves are read by FAQ_Content, which owns their
110 + * names, their stored shapes and their per-surface schema toggles. Only the
111 + * *foreign* FAQ producers are listed below, because standing down for them
112 + * is this class's decision rather than a fact about where ThinkRank keeps
113 + * its own content (#767).
102 114 *
103 - * @since 2.3.1
115 + * @since 2.10.1
104 116 */
105 - private const FAQ_BRICKS_ELEMENT = 'thinkrank-faq';
106 117
107 118 /**
108 119 * Third-party Elementor widgets that publish their own FAQPage.
109 120 *
@@ -139,8 +150,16 @@
139 150 */
140 151 private static ?self $instance = null;
141 152
142 153 /**
154 + * Memoised master switch, or null when it has not been read this request.
155 + *
156 + * @since 2.7.0
157 + * @var bool|null
158 + */
159 + private static ?bool $master_switch_on = null;
160 +
161 + /**
143 162 * Competing page-level entities: ['rank' => int, 'schema' => array, 'type' => string].
144 163 *
145 164 * @var array<int,array>
146 165 */
@@ -222,8 +241,10 @@
222 241 * @return void
223 242 */
224 243 public static function reset(): void {
225 244 self::$instance = null;
245 + // Or a test that seeds the switch inherits the previous test's answer.
246 + self::$master_switch_on = null;
226 247 }
227 248
228 249 /**
229 250 * Register a candidate for the page's single page-level entity.
@@ -292,8 +313,61 @@
292 313 return (is_string($actual) && $actual !== '') ? $actual : $declared;
293 314 }
294 315
295 316 /**
317 + * Whether a page entity may carry a `breadcrumb` property.
318 + *
319 + * Reading the node's own `@type` key is not enough, and every case it
320 + * misses ends with a real WebPage losing a valid property:
321 + *
322 + * - A deployed schema whose stored JSON omits `@type` carries the type in
323 + * the `schema_type` column instead. `effective_type()` already resolves
324 + * that, which is why the node's `@id` reads `#webpage` even though the
325 + * node itself has no `@type` — so the resolved value is what has to be
326 + * consulted here too.
327 + * - JSON-LD permits several types on one node. `["WebPage", "FAQPage"]` is
328 + * a WebPage, but a strict in_array() against the array as a whole is
329 + * false, so the trail would be dropped from a page that may carry it.
330 + * - A list naming nothing usable (`[]`, `[null]`) is the first case wearing
331 + * the second's clothes, and resolves the same way.
332 + *
333 + * @since 2.7.0
334 + * @param array $node The page entity.
335 + * @param string $resolved_type Type the graph resolved for it.
336 + * @return bool
337 + */
338 + private function allows_breadcrumb(array $node, string $resolved_type): bool {
339 + $declared = $node['@type'] ?? '';
340 +
341 + // One path for both shapes. Splitting them invites the list branch to
342 + // grow its own idea of what an absent type means, which is the mistake
343 + // being corrected here in the first place.
344 + $named = false;
345 +
346 + foreach (is_array($declared) ? $declared : [$declared] as $type) {
347 + if (!is_string($type) || '' === $type) {
348 + continue;
349 + }
350 +
351 + $named = true;
352 +
353 + if (in_array($type, self::BREADCRUMB_TYPES, true)) {
354 + return true;
355 + }
356 + }
357 +
358 + // The node named a type, and none of them may carry a breadcrumb.
359 + if ($named) {
360 + return false;
361 + }
362 +
363 + // It named none, so it is whatever the graph resolved for it — the same
364 + // value its @id was minted from. An empty list is no more informative
365 + // than a missing key and must not read as "definitely not a WebPage".
366 + return in_array($resolved_type, self::BREADCRUMB_TYPES, true);
367 + }
368 +
369 + /**
296 370 * Register a node that does not compete for the page-level slot.
297 371 *
298 372 * @since 1.32.0
299 373 * @param array $schema Schema array.
@@ -474,201 +548,151 @@
474 548 // page throws `post_content` away, so a FAQ block left there when the
475 549 // page was switched over never renders. Publishing its questions would
476 550 // put schema on the page for content no visitor can see — which Google
477 551 // treats as a violation, not merely a duplicate (#650).
478 - if (!$this->bricks_supersedes_post_content((int) $post->ID)) {
479 - $this->collect_block_faq($post);
480 - }
552 + foreach (self::faq_groups($post) as $group) {
553 + // The stored toggle decides whether a producer contributes schema.
554 + // FAQ_Content reports it rather than applying it, because a block
555 + // with schema switched off is still visible FAQ content that the
556 + // abilities have to describe (#767).
557 + if (empty($group['schema'])) {
558 + continue;
559 + }
481 560
482 - $this->collect_elementor_faq($post);
483 - $this->collect_bricks_faq($post);
484 - }
485 -
486 - /**
487 - * Whether Bricks renders this post and discards its `post_content`.
488 - *
489 - * @since 2.3.1
490 - * @param int $post_id Post being viewed.
491 - * @return bool
492 - */
493 - private function bricks_supersedes_post_content(int $post_id): bool {
494 - if (!class_exists('ThinkRank\\SEO\\Builder_Content')) {
495 - $file = THINKRANK_PLUGIN_DIR . 'includes/seo/class-builder-content.php';
496 - if (!file_exists($file)) {
497 - return false;
498 - }
499 - require_once $file;
561 + $this->absorb_content_faq($this->questions_from_pairs($group['pairs']));
500 562 }
501 -
502 - return \ThinkRank\SEO\Builder_Content::bricks_supersedes_post_content($post_id);
503 563 }
504 564
505 565 /**
506 - * Record that a body FAQ producer's content is represented in the graph.
566 + * Whether ThinkRank would publish a FAQPage for this post.
507 567 *
508 - * Deliberately not keyed on the entity count growing: when a block asks the
509 - * same question as the per-post deployment, dedup means nothing is added,
510 - * but the block's content *is* covered and it must still stay quiet.
568 + * Answers the question `get-faq` has to put to an agent — "is this actually
569 + * being emitted?" — without rendering the page. Kept here rather than in the
570 + * ability because every term of it is this class's own rule: the password
571 + * form, the master switch, the per-content-type Schema switch, the other
572 + * plugins whose FAQ output makes ThinkRank stand down, and the filter that
573 + * overrides all of it.
511 574 *
512 - * @since 1.32.0
513 - * @param array $entities Questions found on that producer.
514 - * @return void
515 - */
516 - private function absorb_content_faq(array $entities): void {
517 - if (empty($entities)) {
518 - return;
519 - }
520 -
521 - $this->add_faq_entities($entities);
522 - $this->absorbed_content_faq = true;
523 - }
524 -
525 - /**
526 - * Collect FAQ questions from thinkrank/faq blocks, including nested ones.
575 + * It answers for the post as a visitor gets it. A draft or a scheduled post
576 + * is reported on what it will emit once it is served, because that is the
577 + * question an agent preparing one is asking; a password-protected post is
578 + * not, because publishing it changes nothing — the form stays.
527 579 *
528 - * @since 1.32.0
529 - * @param \WP_Post $post Post being viewed.
530 - * @return void
580 + * @since 2.10.1
581 + * @param \WP_Post $post Post to test.
582 + * @return bool
531 583 */
532 - private function collect_block_faq(\WP_Post $post): void {
533 - if (!function_exists('parse_blocks') || !has_blocks($post->post_content)) {
534 - return;
584 + public static function will_emit_faqpage(\WP_Post $post): bool {
585 + // The same gate collect_post_faq() applies, and for the same reason:
586 + // behind a password form the block never renders, so no FAQPage is
587 + // published. Without this the ability answered "yes, it is emitted"
588 + // about a page that emits nothing — which is the answer that stops an
589 + // agent looking any further.
590 + if (function_exists('post_password_required') && post_password_required($post)) {
591 + return false;
535 592 }
536 593
537 - $this->walk_blocks(parse_blocks($post->post_content));
538 - }
594 + $has_questions = false;
539 595
540 - /**
541 - * Recurse a parsed block tree collecting FAQ entries.
542 - *
543 - * @since 1.32.0
544 - * @param array $blocks Parsed blocks.
545 - * @return void
546 - */
547 - private function walk_blocks(array $blocks): void {
548 - foreach ($blocks as $block) {
549 - if (!is_array($block)) {
596 + foreach (self::faq_groups($post) as $group) {
597 + if (empty($group['schema'])) {
550 598 continue;
551 599 }
552 600
553 - if (($block['blockName'] ?? '') === self::FAQ_BLOCK) {
554 - $attrs = $block['attrs'] ?? [];
555 -
556 - // Mirrors Blocks_Manager: schema is on unless explicitly disabled.
557 - $disabled = array_key_exists('outputSchema', $attrs) && false === $attrs['outputSchema'];
558 -
559 - if (!$disabled) {
560 - $this->absorb_content_faq($this->questions_from_pairs($attrs['faqs'] ?? []));
601 + foreach ($group['pairs'] as $pair) {
602 + if ('' !== trim((string) ($pair['question'] ?? '')) && '' !== trim((string) ($pair['answer'] ?? ''))) {
603 + $has_questions = true;
604 + break 2;
561 605 }
562 606 }
607 + }
563 608
564 - if (!empty($block['innerBlocks']) && is_array($block['innerBlocks'])) {
565 - $this->walk_blocks($block['innerBlocks']);
566 - }
609 + if (!$has_questions) {
610 + return false;
567 611 }
612 +
613 + if (!self::schema_allowed_for_post($post)) {
614 + return false;
615 + }
616 +
617 + $emit = !(new self())->has_foreign_faq_source($post);
618 +
619 + /** This filter is documented in includes/frontend/class-schema-graph.php */
620 + return (bool) apply_filters('thinkrank_emit_faqpage', $emit, $post);
568 621 }
569 622
570 623 /**
571 - * Collect FAQ questions from Elementor FAQ widgets.
624 + * output_allowed(), asked about a named post instead of the current query.
572 625 *
573 - * @since 1.32.0
574 - * @param \WP_Post $post Post being viewed.
575 - * @return void
626 + * output_allowed() resolves the content type from the main query, which in
627 + * an admin or MCP request is not the post being asked about.
628 + *
629 + * @since 2.10.1
630 + * @param \WP_Post $post Post to test.
631 + * @return bool
576 632 */
577 - private function collect_elementor_faq(\WP_Post $post): void {
578 - $raw = get_post_meta($post->ID, '_elementor_data', true);
579 - if (empty($raw) || !is_string($raw)) {
580 - return;
633 + private static function schema_allowed_for_post(\WP_Post $post): bool {
634 + if (null === self::$master_switch_on) {
635 + // Resolve the site-wide half through the existing reader.
636 + self::output_allowed();
581 637 }
582 638
583 - $elements = json_decode($raw, true);
584 - if (!is_array($elements)) {
585 - return;
639 + if (!self::$master_switch_on) {
640 + return false;
586 641 }
587 642
588 - $this->walk_elementor($elements);
589 - }
643 + if (class_exists('ThinkRank\\SEO\\Content_Type_Settings')) {
644 + return \ThinkRank\SEO\Content_Type_Settings::is_enabled(
645 + \ThinkRank\SEO\Content_Type_Settings::FEATURE_SCHEMA,
646 + (string) $post->post_type,
647 + true
648 + );
649 + }
590 650
591 - /**
592 - * Collect FAQ questions from Bricks FAQ elements.
593 - *
594 - * Reads the tree Bricks will actually render — resolved through
595 - * `Builder_Content`, so a page whose content lives on a content template or
596 - * inside a component is covered, and one switched back to the block editor
597 - * is not.
598 - *
599 - * Unlike the block, this is not gated on Bricks owning `post_content`: a
600 - * Bricks element is on the page whenever Bricks renders the page, which is
601 - * exactly what resolving the tree already establishes (#626).
602 - *
603 - * @since 2.3.1
604 - * @param \WP_Post $post Post being viewed.
605 - * @return void
606 - */
607 - private function collect_bricks_faq(\WP_Post $post): void {
608 - $this->walk_bricks($this->bricks_tree((int) $post->ID));
651 + return true;
609 652 }
610 653
611 654 /**
612 - * Collect FAQ entries from a resolved Bricks tree.
655 + * Every FAQ producer on a post, loaded defensively.
613 656 *
614 - * The tree is flat, so no recursion: `Builder_Content::bricks_tree()`
615 - * splices component definitions into the same list.
657 + * The reader lives in the SEO namespace, and this class runs in contexts
658 + * where that autoloader is not guaranteed — which is why the Bricks gate it
659 + * replaced carried the same require.
616 660 *
617 - * The element's own settings are read here rather than through
618 - * `FAQ_Element`, whose class extends `Bricks\Element` and so cannot even be
619 - * loaded when the theme is inactive — which is exactly the case that still
620 - * has a stored tree, on a site that has since switched themes. The repeater
621 - * uses the same `question` / `answer` keys as the block, so the shared
622 - * builder below already understands it.
623 - *
624 - * @since 2.3.1
625 - * @param array $elements Bricks elements.
626 - * @return void
661 + * @since 2.10.1
662 + * @param \WP_Post $post Post being read.
663 + * @return array<int, array{source: string, schema: bool, pairs: array}>
627 664 */
628 - private function walk_bricks(array $elements): void {
629 - foreach ($elements as $element) {
630 - if (!is_array($element) || ($element['name'] ?? '') !== self::FAQ_BRICKS_ELEMENT) {
631 - continue;
665 + private static function faq_groups(\WP_Post $post): array {
666 + if (!class_exists('ThinkRank\\SEO\\FAQ_Content')) {
667 + $file = THINKRANK_PLUGIN_DIR . 'includes/seo/class-faq-content.php';
668 + if (!file_exists($file)) {
669 + return [];
632 670 }
671 + require_once $file;
672 + }
633 673
634 - $settings = is_array($element['settings'] ?? null) ? $element['settings'] : [];
635 -
636 - // Mirrors FAQ_Element: a cleared Bricks checkbox loses its key.
637 - if (empty($settings['outputSchema'])) {
638 - continue;
639 - }
640 -
641 - $this->absorb_content_faq($this->questions_from_pairs($settings['faqs'] ?? []));
642 - }
674 + return \ThinkRank\SEO\FAQ_Content::groups($post);
643 675 }
644 676
645 677 /**
646 - * Recurse an Elementor element tree collecting FAQ entries.
678 + * Record that a body FAQ producer's content is represented in the graph.
647 679 *
680 + * Deliberately not keyed on the entity count growing: when a block asks the
681 + * same question as the per-post deployment, dedup means nothing is added,
682 + * but the block's content *is* covered and it must still stay quiet.
683 + *
648 684 * @since 1.32.0
649 - * @param array $elements Elementor elements.
685 + * @param array $entities Questions found on that producer.
650 686 * @return void
651 687 */
652 - private function walk_elementor(array $elements): void {
653 - foreach ($elements as $element) {
654 - if (!is_array($element)) {
655 - continue;
656 - }
688 + private function absorb_content_faq(array $entities): void {
689 + if (empty($entities)) {
690 + return;
691 + }
657 692
658 - if (($element['widgetType'] ?? '') === self::FAQ_WIDGET) {
659 - $settings = $element['settings'] ?? [];
660 -
661 - // Mirrors FAQ_Widget: schema unless the toggle is off.
662 - if ('yes' === ($settings['output_schema'] ?? 'yes')) {
663 - $this->absorb_content_faq($this->questions_from_pairs($settings['faqs'] ?? []));
664 - }
665 - }
666 -
667 - if (!empty($element['elements']) && is_array($element['elements'])) {
668 - $this->walk_elementor($element['elements']);
669 - }
670 - }
693 + $this->add_faq_entities($entities);
694 + $this->absorbed_content_faq = true;
671 695 }
672 696
673 697 /**
674 698 * Turn stored question/answer pairs into Question entities.
@@ -727,8 +751,59 @@
727 751 return !empty($this->primary_candidates) || !empty($this->supporting) || !empty($this->faq_entities);
728 752 }
729 753
730 754 /**
755 + * Whether ThinkRank may emit structured data for this request at all.
756 + *
757 + * The master switch and the matrix's per-content-type Schema switch, asked
758 + * once. #461 put the master switch inside output_site_schema_markup(),
759 + * which is one of four producers; the other three never learned about it,
760 + * so turning Schema off removed the deployed rows and left the live
761 + * generator running — the page emitted *more* types with the switch off
762 + * than with it on (#688).
763 + *
764 + * Static because the producers that need it do not share a base class: the
765 + * three graph producers converge on render(), but Blocks_Manager emits its
766 + * own script tag from a content filter and never touches the graph, so it
767 + * has to ask the same question independently.
768 + *
769 + * @since 2.7.0
770 + * @return bool True when structured data may be emitted.
771 + */
772 + public static function output_allowed(): bool {
773 + // Memoised because inject_block_schema() asks once per matching block,
774 + // and Schema_Management_System's constructor builds a schema builder and
775 + // a cache manager and registers listeners — it is not something to spin
776 + // up per block. The switch is site-wide, so it cannot change within a
777 + // request; the per-content-type check below is query-dependent and stays
778 + // live.
779 + if (null === self::$master_switch_on) {
780 + self::$master_switch_on = true;
781 +
782 + if (class_exists('ThinkRank\\SEO\\Schema_Management_System')) {
783 + $settings = (new \ThinkRank\SEO\Schema_Management_System())->get_settings('site', null);
784 +
785 + // Absent means "not configured", which every other reader treats
786 + // as enabled; only a value that is present and off disables.
787 + self::$master_switch_on = !(array_key_exists('enabled', $settings) && empty($settings['enabled']));
788 + }
789 + }
790 +
791 + if (!self::$master_switch_on) {
792 + return false;
793 + }
794 +
795 + if (class_exists('ThinkRank\\SEO\\Content_Type_Settings')) {
796 + return \ThinkRank\SEO\Content_Type_Settings::is_enabled_for_current(
797 + \ThinkRank\SEO\Content_Type_Settings::FEATURE_SCHEMA,
798 + true
799 + );
800 + }
801 +
802 + return true;
803 + }
804 +
805 + /**
731 806 * Assemble and emit the graph. Safe to call more than once.
732 807 *
733 808 * @since 1.32.0
734 809 * @return void
@@ -737,8 +812,14 @@
737 812 if ($this->rendered || !$this->has_nodes()) {
738 813 return;
739 814 }
740 815
816 + // Every graph producer converges here, so this is the one place the
817 + // master switch has to hold for all of them (#688).
818 + if (!self::output_allowed()) {
819 + return;
820 + }
821 +
741 822 // A 404 response represents no content, so there is nothing for
742 823 // structured data to describe. The page-level producers already skip
743 824 // this context, but the site-identity entity does not, so without this
744 825 // guard every miss — including crawlers probing URLs that never existed
@@ -773,8 +854,18 @@
773 854 if (empty($graph)) {
774 855 return;
775 856 }
776 857
858 + // One pass over the assembled graph, rather than at each producer.
859 + // @id and url values arrive from a dozen of them — some derived from
860 + // WordPress, some read straight out of stored settings — and on a
861 + // misconfigured site that produced a single graph carrying both
862 + // schemes at once, with @ids that no longer matched the canonical they
863 + // are supposed to identify (#638). Normalizing where the graph is
864 + // serialized is the only place that catches all of them, including
865 + // nodes an add-on added through the filter above.
866 + $graph = \ThinkRank\SEO\Url_Scheme::apply_deep($graph);
867 +
777 868 $json = wp_json_encode(
778 869 ['@context' => self::SCHEMA_CONTEXT, '@graph' => array_values($graph)],
779 870 JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT
780 871 | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT
@@ -784,9 +875,9 @@
784 875 return;
785 876 }
786 877
787 878 echo "<!-- ThinkRank Schema Graph -->\n";
788 - echo '<script type="application/ld+json">' . "\n";
879 + echo '<script type="application/ld+json" data-thinkrank="schema-graph">' . "\n";
789 880 echo $json . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- wp_json_encode with JSON_HEX_* cannot break out of the script block.
790 881 echo '</script>' . "\n";
791 882 echo "<!-- /ThinkRank Schema Graph -->\n";
792 883 }
@@ -897,11 +988,12 @@
897 988 $primary = ['schema' => $faq, 'type' => 'FAQPage'];
898 989 $faq = null;
899 990 }
900 991
901 - $nodes = [];
902 - $primary_id = '';
903 - $used_ids = [];
992 + $nodes = [];
993 + $primary_id = '';
994 + $primary_type = '';
995 + $used_ids = [];
904 996
905 997 if (null !== $primary) {
906 998 $node = $primary['schema'];
907 999
@@ -909,9 +1001,10 @@
909 1001 // so an "Article" setting that renders BlogPosting reads #blogposting.
910 1002 $resolved_type = $this->effective_type($node, $primary['type']);
911 1003
912 1004 $node = $this->assign_id($node, $base . '#' . strtolower($resolved_type), $used_ids);
913 - $primary_id = $node['@id'];
1005 + $primary_id = $node['@id'];
1006 + $primary_type = $resolved_type;
914 1007 $nodes['primary'] = $node;
915 1008 }
916 1009
917 1010 // Entities deployed alongside the winner (Pro's Multi-Schema lets a post
@@ -952,8 +1045,9 @@
952 1045 $person_nodes = [];
953 1046
954 1047 foreach ($this->supporting as $index => $node) {
955 1048 $type = $node['@type'] ?? '';
1049 + $slot = $this->site_level_slot($type);
956 1050
957 1051 if ('BreadcrumbList' === $type) {
958 1052 $node = $this->assign_id($node, $base . '#breadcrumb', $used_ids);
959 1053 $breadcrumb_id = $node['@id'];
@@ -962,9 +1056,9 @@
962 1056 $website_id = $node['@id'];
963 1057 } elseif ('Organization' === $type) {
964 1058 $node = $this->assign_id($node, home_url('/#organization'), $used_ids);
965 1059 $organization_nodes[] = $node;
966 - } elseif (in_array($type, self::SITE_LEVEL_TYPES, true)) {
1060 + } elseif ('' !== $slot) {
967 1061 // Site-level entities describe the site, not the page, so their
968 1062 // @id must be stable across URLs. Falling through to the
969 1063 // page-scoped branch minted a fresh identity on every URL, so
970 1064 // one business became N entities in a crawler's graph and
@@ -974,9 +1068,9 @@
974 1068 // arrive here claiming the same @id. assign_id() would resolve
975 1069 // that collision by minting "#person-2", turning a duplicate
976 1070 // into two competing entities that split the identity a
977 1071 // knowledge graph is meant to consolidate (#479).
978 - $duplicate_key = $this->find_same_entity($nodes, $type, $node);
1072 + $duplicate_key = $this->find_same_entity($nodes, $slot, $node);
979 1073
980 1074 if (null !== $duplicate_key) {
981 1075 $nodes[$duplicate_key] = $this->merge_entity($nodes[$duplicate_key], $node);
982 1076 continue;
@@ -981,11 +1075,11 @@
981 1075 $nodes[$duplicate_key] = $this->merge_entity($nodes[$duplicate_key], $node);
982 1076 continue;
983 1077 }
984 1078
985 - $node = $this->assign_id($node, home_url('/#' . strtolower($type)), $used_ids);
1079 + $node = $this->assign_id($node, home_url('/#' . strtolower($slot)), $used_ids);
986 1080
987 - if ('Person' === $type) {
1081 + if ('Person' === $slot) {
988 1082 $person_nodes[] = $node;
989 1083 }
990 1084 } elseif (is_string($type) && $type !== '') {
991 1085 $node = $this->assign_id($node, $base . '#' . strtolower($type), $used_ids);
@@ -998,9 +1092,13 @@
998 1092 if (isset($nodes['primary'])) {
999 1093 if ($website_id !== '' && !isset($nodes['primary']['isPartOf'])) {
1000 1094 $nodes['primary']['isPartOf'] = ['@id' => $website_id];
1001 1095 }
1002 - if ($breadcrumb_id !== '' && !isset($nodes['primary']['breadcrumb'])) {
1096 + if (
1097 + $breadcrumb_id !== ''
1098 + && !isset($nodes['primary']['breadcrumb'])
1099 + && $this->allows_breadcrumb($nodes['primary'], $primary_type)
1100 + ) {
1003 1101 $nodes['primary']['breadcrumb'] = ['@id' => $breadcrumb_id];
1004 1102 }
1005 1103
1006 1104 // Point publisher/author at the full nodes already in the graph.
@@ -1074,8 +1172,32 @@
1074 1172 return ['winner' => array_shift($kept), 'siblings' => array_values($kept)];
1075 1173 }
1076 1174
1077 1175 /**
1176 + * The site-level entity a node's @type makes it, or '' for none.
1177 + *
1178 + * A LocalBusiness is published under the subtype the site chose in Local
1179 + * SEO ("Dentist", "Restaurant"), but it is still the one business entity,
1180 + * so every subtype shares the LocalBusiness slot: the same home-scoped
1181 + * `/#localbusiness` @id it always had, and the same entity for
1182 + * find_same_entity() to fold a per-post copy into. Matching the literal
1183 + * type instead would have given a Dentist a fresh page-scoped @id on every
1184 + * URL, the exact split #471 closed.
1185 + *
1186 + * @since 2.10.0
1187 + *
1188 + * @param mixed $type Node @type.
1189 + * @return string 'LocalBusiness', 'Person' or ''.
1190 + */
1191 + private function site_level_slot($type): string {
1192 + if (\ThinkRank\Config\Local_Business_Types_Config::is_local_business($type)) {
1193 + return 'LocalBusiness';
1194 + }
1195 +
1196 + return (is_string($type) && in_array($type, self::SITE_LEVEL_TYPES, true)) ? $type : '';
1197 + }
1198 +
1199 + /**
1078 1200 * Find an already-placed node describing the same entity as $node.
1079 1201 *
1080 1202 * Identity is `email` when both carry one — two people can share a name,
1081 1203 * but not a mailbox — and a case-insensitive `name` match otherwise. A node
@@ -1084,9 +1206,9 @@
1084 1206 *
1085 1207 * @since 2.0.2
1086 1208 *
1087 1209 * @param array $nodes Nodes placed so far, keyed.
1088 - * @param string $type Schema type to match within.
1210 + * @param string $type Site-level slot to match within (see site_level_slot()).
1089 1211 * @param array $node Candidate node.
1090 1212 * @return string|null Key of the matching node, or null.
1091 1213 */
1092 1214 private function find_same_entity(array $nodes, string $type, array $node): ?string {
@@ -1097,9 +1219,9 @@
1097 1219 return null;
1098 1220 }
1099 1221
1100 1222 foreach ($nodes as $key => $placed) {
1101 - if (($placed['@type'] ?? '') !== $type) {
1223 + if ($this->site_level_slot($placed['@type'] ?? '') !== $type) {
1102 1224 continue;
1103 1225 }
1104 1226
1105 1227 $placed_email = isset($placed['email']) ? strtolower(trim((string) $placed['email'])) : '';