PluginProbe
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO / 2.10.0
ThinkRank AI SEO – AI SEO Plugin for WordPress: Schema, XML Sitemaps, Meta Tags, Search Console & Local SEO v2.10.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 1.0.0 All 52 releases
← All changes | includes/frontend/class-global-seo-schema-output.php +392 -30 1.32.0 → 2.10.0 View file →
@@ -46,8 +46,34 @@
46 46 */
47 47 private const SCHEMA_CONTEXT = 'https://schema.org';
48 48
49 49 /**
50 + * Returns the description already resolved for this request, or null.
51 + *
52 + * Injected rather than resolved here, because the chain behind it (post
53 + * meta, global template, archive, site-identity default, derived excerpt,
54 + * tagline) reads request state that Seo_Manager owns. Duplicating it would
55 + * be a second implementation to keep in step; this way schema and the meta
56 + * tags cannot disagree (#766).
57 + *
58 + * @since 2.10.0
59 + * @var callable|null
60 + */
61 + private $description_resolver = null;
62 +
63 + /**
64 + * Supply the request's resolved description.
65 + *
66 + * @since 2.10.0
67 + *
68 + * @param callable $resolver Returns string|null.
69 + * @return void
70 + */
71 + public function set_description_resolver(callable $resolver): void {
72 + $this->description_resolver = $resolver;
73 + }
74 +
75 + /**
50 76 * Initialize the schema output
51 77 *
52 78 * @since 1.0.0
53 79 */
@@ -53,11 +79,116 @@
53 79 */
54 80 public function init(): void {
55 81 // Hook into wp_head to output schema markup
56 82 add_action('wp_head', [$this, 'output_global_seo_schema'], 15);
83 +
84 + // One Product entity per product page: when ThinkRank emits the
85 + // Product schema (the default for WooCommerce products), WooCommerce
86 + // core's own JSON-LD must stand down, or the page carries two
87 + // aggregateRating blocks and Search Console raises the critical
88 + // "Review has multiple aggregate ratings" error. Registered eagerly
89 + // and decided lazily inside the callback, because WooCommerce
90 + // generates its data during the product template render — which on
91 + // block themes can run before wp_head, too early for a flag set at
92 + // output time to exist yet.
93 + add_filter('woocommerce_structured_data_product', [$this, 'suppress_woocommerce_product_schema'], 20, 2);
57 94 }
58 95
59 96 /**
97 + * Yield WooCommerce's Product structured data when ThinkRank emits the
98 + * Product entity for the page being viewed.
99 + *
100 + * Mirrors what other SEO plugins do with WC_Structured_Data: exactly one
101 + * plugin may describe the product. Suppression is surgical — only the
102 + * queried product on its own singular view, only when this class's
103 + * settings resolution says a Product schema will be generated (explicit
104 + * or the WooCommerce default), and WooCommerce's breadcrumb and other
105 + * structured data are never touched. With ThinkRank's product schema
106 + * disabled or set to another type, WooCommerce's markup passes through
107 + * unchanged.
108 + *
109 + * @since 2.0.1
110 + * @param array $markup WooCommerce's generated Product markup.
111 + * @param mixed $product WC_Product being described.
112 + * @return array Original markup, or empty to suppress.
113 + */
114 + public function suppress_woocommerce_product_schema($markup, $product = null) {
115 + if (!is_array($markup) || !is_singular()) {
116 + return $markup;
117 + }
118 +
119 + // Only the main product of this page — a card grid or related-products
120 + // widget describing other products is not ours to silence.
121 + $queried_id = (int) get_queried_object_id();
122 + $product_id = is_object($product) && method_exists($product, 'get_id') ? (int) $product->get_id() : 0;
123 + if (!$queried_id || !$product_id || $queried_id !== $product_id) {
124 + return $markup;
125 + }
126 +
127 + $post_type = (string) get_post_type($queried_id);
128 + if ($post_type === '') {
129 + return $markup;
130 + }
131 +
132 + $settings = $this->get_global_seo_settings($post_type);
133 + if (($settings['schema_type'] ?? '') === 'Product') {
134 + // ...but only if this class is actually going to emit it. The
135 + // per-content-type Schema switch (#660) makes
136 + // output_global_seo_schema() return before it builds anything, so
137 + // claiming the entity here as well left the page with NO product
138 + // structured data at all — strictly worse than the duplicate this
139 + // method exists to prevent, and the opposite of what the docblock
140 + // above promises for "ThinkRank's product schema disabled".
141 + if (!\ThinkRank\SEO\Content_Type_Settings::is_enabled_for_current(
142 + \ThinkRank\SEO\Content_Type_Settings::FEATURE_SCHEMA,
143 + true
144 + )) {
145 + return $markup;
146 + }
147 +
148 + return [];
149 + }
150 +
151 + // A per-post DEPLOYED Product schema duplicates WooCommerce's markup
152 + // just the same, even when the post-type-wide setting points elsewhere.
153 + // Checked second because the default path above answers without a
154 + // query; this one is a single indexed lookup and only runs on the
155 + // rare configured-away sites.
156 + if ($this->post_has_deployed_product_schema($queried_id)) {
157 + return [];
158 + }
159 +
160 + return $markup;
161 + }
162 +
163 + /**
164 + * Whether an active per-post Product schema deployment exists for a post.
165 + *
166 + * Reads the deployment table directly rather than constructing
167 + * Schema_Management_System — this runs inside WooCommerce's structured
168 + * data filter on product pages, where spinning up the full manager (and
169 + * its builder) to answer a yes/no question would be waste. Query shape
170 + * matches get_deployed_schemas(): active rows for the post context.
171 + *
172 + * @since 2.0.1
173 + * @param int $post_id Post to check.
174 + * @return bool
175 + */
176 + private function post_has_deployed_product_schema(int $post_id): bool {
177 + global $wpdb;
178 +
179 + $table = $wpdb->prefix . 'thinkrank_seo_schema';
180 +
181 + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- one indexed EXISTS-style lookup on the render path; the deployment cache layer belongs to the full manager this deliberately avoids constructing.
182 + $found = $wpdb->get_var($wpdb->prepare(
183 + "SELECT 1 FROM {$table} WHERE context_type = 'post' AND context_id = %d AND schema_type = 'Product' AND is_active = 1 LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- table name from $wpdb->prefix.
184 + $post_id
185 + ));
186 +
187 + return '1' === (string) $found;
188 + }
189 +
190 + /**
60 191 * Output JSON-LD schema markup based on Global SEO settings
61 192 *
62 193 * @since 1.0.0
63 194 * @return void
@@ -62,8 +193,17 @@
62 193 * @since 1.0.0
63 194 * @return void
64 195 */
65 196 public function output_global_seo_schema(): void {
197 + // Per-content-type schema switch. 'inherit' (the default) keeps schema
198 + // on, exactly as before the matrix existed (#660).
199 + if (!\ThinkRank\SEO\Content_Type_Settings::is_enabled_for_current(
200 + \ThinkRank\SEO\Content_Type_Settings::FEATURE_SCHEMA,
201 + true
202 + )) {
203 + return;
204 + }
205 +
66 206 // Archives get a CollectionPage schema instead of the per-post-type one
67 207 if (!is_singular()) {
68 208 $this->output_archive_schema();
69 209 return;
@@ -140,9 +280,22 @@
140 280 $name = $posts_page_id ? get_the_title($posts_page_id) : __('Blog', 'thinkrank');
141 281 $url = $posts_page_id ? (string) get_permalink($posts_page_id) : home_url('/');
142 282 } elseif (is_post_type_archive()) {
143 283 $post_type_object = get_queried_object();
284 +
285 + // WooCommerce maps the shop archive onto a real page, so
286 + // get_queried_object() returns that WP_Post while
287 + // is_post_type_archive() is still true. Bailing here left every
288 + // store's main archive with no CollectionPage (#466). Fall back to
289 + // the query var, exactly as the canonical resolver already does.
144 290 if (!$post_type_object instanceof \WP_Post_Type) {
291 + $queried_post_type = (string) get_query_var('post_type');
292 + $post_type_object = $queried_post_type
293 + ? get_post_type_object($queried_post_type)
294 + : null;
295 + }
296 +
297 + if (!$post_type_object instanceof \WP_Post_Type) {
145 298 return;
146 299 }
147 300 $name = $post_type_object->labels->name ?? $post_type_object->label;
148 301 $url = (string) get_post_type_archive_link($post_type_object->name);
@@ -166,8 +319,17 @@
166 319 if (empty($url)) {
167 320 return;
168 321 }
169 322
323 + // Page 2 of an archive is a different URL and must be a different node.
324 + // The link above is always the un-paginated one, so Schema_Graph::base_url()
325 + // minted the identical #collectionpage and #breadcrumb @id on every
326 + // page — distinct URLs claiming the same node identity (#397).
327 + $url = \ThinkRank\Frontend\SEO_Manager::with_pagination(
328 + (string) $url,
329 + \ThinkRank\Frontend\SEO_Manager::current_page_number()
330 + );
331 +
170 332 $schema = [
171 333 '@context' => self::SCHEMA_CONTEXT,
172 334 '@type' => 'CollectionPage',
173 335 'name' => $name,
@@ -178,10 +340,12 @@
178 340 'url' => home_url('/'),
179 341 ],
180 342 ];
181 343
182 - $description = trim(wp_strip_all_tags($description));
183 - if (!empty($description)) {
344 + // Normalised like every other description: an entity or a trailing
345 + // excerpt marker is as wrong in a CollectionPage as anywhere (#766).
346 + $description = self::normalize_description((string) $description);
347 + if ('' !== $description) {
184 348 $schema['description'] = $description;
185 349 }
186 350
187 351 /**
@@ -214,8 +378,16 @@
214 378 * @param string $post_type Post type slug.
215 379 * @return bool True when structured data would be output for this post type.
216 380 */
217 381 public function would_output_schema(string $post_type): bool {
382 + if (!\ThinkRank\SEO\Content_Type_Settings::is_enabled(
383 + \ThinkRank\SEO\Content_Type_Settings::FEATURE_SCHEMA,
384 + $post_type,
385 + true
386 + )) {
387 + return false;
388 + }
389 +
218 390 $settings = $this->get_global_seo_settings($post_type);
219 391
220 392 return !empty($settings['schema_type']);
221 393 }
@@ -220,8 +392,27 @@
220 392 return !empty($settings['schema_type']);
221 393 }
222 394
223 395 /**
396 + * Whether this post type has a SAVED schema type, ignoring the built-in
397 + * per-post-type default.
398 + *
399 + * would_output_schema() answers "will JSON-LD be emitted?", which the
400 + * fallback in get_global_seo_settings() makes true for every public post
401 + * type. The audit needs the different question "has the user configured
402 + * anything?", so this reads the stored option without the default merge.
403 + *
404 + * @since 2.2.0
405 + * @param string $post_type Post type.
406 + * @return bool True when an explicit schema_type is stored for this type.
407 + */
408 + public function has_explicit_schema_type(string $post_type): bool {
409 + $all_settings = get_option(self::OPTION_NAME, []);
410 +
411 + return !empty($all_settings[$post_type]['schema_type']);
412 + }
413 +
414 + /**
224 415 * Get Global SEO settings for a specific post type
225 416 *
226 417 * @since 1.0.0
227 418 * @param string $post_type Post type
@@ -360,12 +551,15 @@
360 551 'datePublished' => get_the_date('c', $post),
361 552 'dateModified' => get_the_modified_date('c', $post),
362 553 ];
363 554
364 - // Add description
365 - $excerpt = get_the_excerpt($post);
366 - if (!empty($excerpt)) {
367 - $schema['description'] = wp_strip_all_tags($excerpt);
555 + // Add description. Prefers the request's resolved description so the
556 + // article describes itself the same way in JSON-LD as in the head
557 + // (#766); a Bricks page's stored `post_content` is not on the page, so
558 + // the excerpt fallback must not describe it either (#651).
559 + $description = $this->schema_description($post);
560 + if ('' !== $description) {
561 + $schema['description'] = $description;
368 562 }
369 563
370 564 // Add author
371 565 $author_id = $post->post_author;
@@ -434,9 +628,9 @@
434 628 }
435 629 require_once $builder_file;
436 630 }
437 631
438 - $excerpt = get_the_excerpt($post);
632 + $excerpt = $this->post_excerpt_text($post);
439 633
440 634 $builder = new \ThinkRank\SEO\Schema_Builder();
441 635 $schema = $builder->build_schema(
442 636 'FAQPage',
@@ -441,9 +635,9 @@
441 635 $schema = $builder->build_schema(
442 636 'FAQPage',
443 637 [
444 638 'title' => get_the_title($post),
445 - 'content' => $post->post_content,
639 + 'content' => \ThinkRank\SEO\Builder_Content::visible_content($post),
446 640 'excerpt' => $excerpt ? wp_strip_all_tags($excerpt) : '',
447 641 'url' => get_permalink($post),
448 642 ],
449 643 get_post_type($post) === 'page' ? 'page' : 'post'
@@ -513,12 +707,13 @@
513 707 'datePublished' => get_the_date('c', $post),
514 708 'dateModified' => get_the_modified_date('c', $post),
515 709 ];
516 710
517 - // Add description
518 - $excerpt = get_the_excerpt($post);
519 - if (!empty($excerpt)) {
520 - $schema['description'] = wp_strip_all_tags($excerpt);
711 + // Add description, preferring the one already resolved for this
712 + // request over core's auto excerpt (#766).
713 + $description = $this->schema_description($post);
714 + if ('' !== $description) {
715 + $schema['description'] = $description;
521 716 }
522 717
523 718 // Add featured image if available
524 719 if (has_post_thumbnail($post)) {
@@ -553,9 +748,9 @@
553 748 // Add caption/description
554 749 $caption = wp_get_attachment_caption($post->ID);
555 750 if (!empty($caption)) {
556 751 $schema['caption'] = $caption;
557 - $schema['description'] = $caption;
752 + $schema['description'] = self::normalize_description((string) $caption);
558 753 }
559 754
560 755 // Add dimensions
561 756 if (!empty($image_meta['width']) && !empty($image_meta['height'])) {
@@ -583,18 +778,18 @@
583 778 'name' => get_the_title($post),
584 779 'url' => get_permalink($post),
585 780 ];
586 781
587 - // Add description
588 - $description = get_the_excerpt($post);
589 - if (empty($description)) {
590 - $caption = wp_get_attachment_caption($post->ID);
591 - if (!empty($caption)) {
592 - $description = $caption;
593 - }
782 + // Add description. Same resolution as the page-level types (#766); an
783 + // attachment's caption remains the last resort.
784 + $description = $this->schema_description($post);
785 + if ('' === $description) {
786 + $description = self::normalize_description(
787 + (string) wp_get_attachment_caption($post->ID)
788 + );
594 789 }
595 - if (!empty($description)) {
596 - $schema['description'] = wp_strip_all_tags($description);
790 + if ('' !== $description) {
791 + $schema['description'] = $description;
597 792 }
598 793
599 794 // For video attachments, add contentUrl
600 795 if ($post->post_type === 'attachment') {
@@ -750,18 +945,31 @@
750 945 private function get_product_description(\WP_Post $post): string {
751 946 // Try custom meta field first
752 947 $description = get_post_meta($post->ID, '_thinkrank_product_description', true);
753 948
754 - // Fallback to excerpt or content
949 + // Then the description resolved for this request, so a product with a
950 + // hand-written meta description does not describe itself differently
951 + // in its Product node than in the head (#766). schema_description()
952 + // falls through to the excerpt on its own, and on a Bricks page that
953 + // excerpt comes from the visible body rather than the discarded
954 + // `post_content` (#651).
755 955 if (empty($description)) {
756 - $description = get_the_excerpt($post);
956 + $description = $this->schema_description($post);
757 957 }
758 958
759 959 if (empty($description)) {
760 - $description = wp_trim_words(wp_strip_all_tags($post->post_content), 30);
960 + $description = \ThinkRank\SEO\Pattern_Resolver::derive_excerpt(
961 + \ThinkRank\SEO\Builder_Content::visible_content($post),
962 + 30
963 + );
761 964 }
762 965
763 - return wp_strip_all_tags($description);
966 + // Normalised like every other description rather than merely stripped.
967 + // `_thinkrank_product_description` is the branch a product author is
968 + // most likely to be using, and it reached the Product node verbatim:
969 + // an `&` stayed an entity and a trailing `[…]` stayed a marker,
970 + // which is the bug this was supposed to fix (#766).
971 + return self::normalize_description((string) $description);
764 972 }
765 973
766 974 /**
767 975 * Get product image
@@ -778,15 +986,27 @@
778 986
779 987 if ($image_url) {
780 988 $image_meta = wp_get_attachment_metadata($image_id);
781 989
782 - return [
990 + // SVGs, offloaded media and failed metadata regeneration all
991 + // report no dimensions. Omit the keys entirely — a literal JSON
992 + // null is an invalid value that Google flags, which is what the
993 + // previous `: null` fallback emitted (#471). Matches
994 + // Schema_Builder::format_image_schema().
995 + $image_object = [
783 996 '@type' => 'ImageObject',
784 997 'url' => $image_url,
785 - // SVGs report 0x0 — send null rather than a zero dimension.
786 - 'width' => !empty($image_meta['width']) ? (int) $image_meta['width'] : null,
787 - 'height' => !empty($image_meta['height']) ? (int) $image_meta['height'] : null,
788 998 ];
999 +
1000 + if (!empty($image_meta['width'])) {
1001 + $image_object['width'] = (int) $image_meta['width'];
1002 + }
1003 +
1004 + if (!empty($image_meta['height'])) {
1005 + $image_object['height'] = (int) $image_meta['height'];
1006 + }
1007 +
1008 + return $image_object;
789 1009 }
790 1010 }
791 1011
792 1012 // Try custom meta field
@@ -1010,8 +1230,150 @@
1010 1230 }
1011 1231 }
1012 1232
1013 1233 return $reviews;
1234 + }
1235 +
1236 + /**
1237 + * The post's excerpt, taken from content the page actually renders.
1238 + *
1239 + * `get_the_excerpt()` falls back to trimming `post_content`, which a Bricks
1240 + * page discards — so on one of those it describes text no visitor sees. A
1241 + * hand-written excerpt is the author's own summary and still wins, because
1242 + * `superseding_excerpt_source()` yields nothing for a post that has one
1243 + * (#651).
1244 + *
1245 + * @since 2.3.1
1246 + * @param \WP_Post $post Post being described.
1247 + * @return string
1248 + */
1249 + private function post_excerpt_text(\WP_Post $post): string {
1250 + $superseding = \ThinkRank\SEO\Builder_Content::superseding_excerpt_source($post);
1251 +
1252 + return '' !== $superseding
1253 + ? \ThinkRank\SEO\Pattern_Resolver::derive_excerpt($superseding, 30)
1254 + : (string) get_the_excerpt($post);
1255 + }
1256 +
1257 + /**
1258 + * The description a schema node should carry for a post.
1259 + *
1260 + * Prefers the description ThinkRank already resolved for this request —
1261 + * the same value behind `<meta name="description">`, og:description and
1262 + * twitter:description, with the author's own meta at the top of its
1263 + * fallback chain. Schema used the auto excerpt instead, so a page with a
1264 + * hand-written description described itself one way to crawlers reading
1265 + * the head and another way to answer engines reading the JSON-LD (#766).
1266 + *
1267 + * The resolver is only consulted for the post the request is actually
1268 + * about. A node describing some other post (a related item, a listing
1269 + * entry) must not inherit this page's description, so those keep deriving
1270 + * their own excerpt.
1271 + *
1272 + * @since 2.10.0
1273 + *
1274 + * @param \WP_Post $post Post being described.
1275 + * @return string Description, or '' when nothing resolves.
1276 + */
1277 + private function schema_description(\WP_Post $post): string {
1278 + if (is_callable($this->description_resolver) && $this->describes_queried_object($post)) {
1279 + $resolved = (string) call_user_func($this->description_resolver);
1280 +
1281 + if ('' !== trim($resolved)) {
1282 + return self::normalize_description($resolved);
1283 + }
1284 + }
1285 +
1286 + return self::normalize_description($this->post_excerpt_text($post));
1287 + }
1288 +
1289 + /**
1290 + * Whether this post is the one the current request is about.
1291 + *
1292 + * @since 2.10.0
1293 + *
1294 + * @param \WP_Post $post Post being described.
1295 + * @return bool
1296 + */
1297 + private function describes_queried_object(\WP_Post $post): bool {
1298 + if (!function_exists('is_singular') || !is_singular()) {
1299 + return false;
1300 + }
1301 +
1302 + return (int) $post->ID === (int) get_queried_object_id();
1303 + }
1304 +
1305 + /**
1306 + * Make a description fit to appear in JSON-LD.
1307 + *
1308 + * Delegates to Seo_Text so the Schema Manager's builder, whose deployed
1309 + * nodes outrank this class's, normalises exactly the same way (#766).
1310 + *
1311 + * @since 2.10.0
1312 + *
1313 + * @param string $description Raw description.
1314 + * @return string
1315 + */
1316 + private static function normalize_description(string $description): string {
1317 + return \ThinkRank\Core\Seo_Text::normalize_schema_text($description);
1318 + }
1319 +
1320 + /**
1321 + * Types a deployed node describes with the post's own description.
1322 + *
1323 + * Schema_Builder fills `description` for these from the post excerpt or
1324 + * content and nothing else; the Schema Manager form has no description
1325 + * field for them. Types with such a field (Product, Event, HowTo,
1326 + * SoftwareApplication, VideoObject, Person) are absent on purpose: what the
1327 + * author typed there is theirs, not a stale copy of the page summary.
1328 + *
1329 + * @since 2.10.0
1330 + * @var string[]
1331 + */
1332 + private const POST_DESCRIBED_TYPES = [
1333 + 'WebPage', 'AboutPage', 'ContactPage', 'ProfilePage',
1334 + 'Article', 'BlogPosting', 'NewsArticle', 'TechnicalArticle', 'ScholarlyArticle', 'Report',
1335 + ];
1336 +
1337 + /**
1338 + * Give a deployed node the description the automatic node would carry.
1339 + *
1340 + * A node deployed through the Schema Manager is a snapshot taken when the
1341 + * author pressed Deploy, and it outranks the node this class builds. So a
1342 + * page that deployed AboutPage, ContactPage or ProfilePage (#624), or an
1343 + * Article, published a frozen excerpt instead of the description the head
1344 + * resolves, and kept it after the meta description was edited. Replacing
1345 + * it here, at output, makes the two nodes agree and fixes existing
1346 + * deployments without a redeploy.
1347 + *
1348 + * Leaves the node alone when it is not about the queried post, or when
1349 + * nothing resolves, so the stored value still stands in that case.
1350 + *
1351 + * @since 2.10.0
1352 + *
1353 + * @param array $node Deployed schema node.
1354 + * @param string $schema_type Deployed schema type.
1355 + * @param \WP_Post $post Post the node was deployed on.
1356 + * @return array
1357 + */
1358 + public function refresh_deployed_description(array $node, string $schema_type, \WP_Post $post): array {
1359 + $type = '' !== $schema_type ? $schema_type : (string) ($node['@type'] ?? '');
1360 +
1361 + if (!in_array($type, self::POST_DESCRIBED_TYPES, true)) {
1362 + return $node;
1363 + }
1364 +
1365 + if (!is_callable($this->description_resolver) || !$this->describes_queried_object($post)) {
1366 + return $node;
1367 + }
1368 +
1369 + $resolved = self::normalize_description((string) call_user_func($this->description_resolver));
1370 +
1371 + if ('' !== $resolved) {
1372 + $node['description'] = $resolved;
1373 + }
1374 +
1375 + return $node;
1014 1376 }
1015 1377
1016 1378 /**
1017 1379 * Register generated schema with the request's schema graph.