| @@ -201,13 +201,43 @@ | ||
| 201 | 201 | 'WebPage' => [ |
| 202 | 202 | 'name' => 'WebPage', |
| 203 | 203 | 'description' => 'Individual web pages', |
| 204 | 204 | 'required_properties' => ['name', 'url'], |
| 205 | + // 'post' as well as 'page': the per-page selector reaches this for | |
| 206 | + // any post type, and a registry limited to 'page' silently produced | |
| 207 | + // nothing for the rest (#624). | |
| 205 | 208 | 'recommended_properties' => ['description', 'author', 'datePublished', 'breadcrumb'], |
| 206 | 209 | 'rich_snippets' => ['webpage', 'breadcrumb'], |
| 207 | - 'context_types' => ['page'], | |
| 210 | + 'context_types' => ['page', 'post'], | |
| 208 | 211 | 'priority' => 'medium' |
| 209 | 212 | ], |
| 213 | + 'AboutPage' => [ | |
| 214 | + 'name' => 'AboutPage', | |
| 215 | + 'description' => 'A page describing the organisation or person behind the site', | |
| 216 | + 'required_properties' => ['name', 'url'], | |
| 217 | + 'recommended_properties' => ['description', 'author', 'datePublished', 'breadcrumb'], | |
| 218 | + 'rich_snippets' => ['webpage', 'breadcrumb'], | |
| 219 | + 'context_types' => ['page', 'post'], | |
| 220 | + 'priority' => 'medium' | |
| 221 | + ], | |
| 222 | + 'ContactPage' => [ | |
| 223 | + 'name' => 'ContactPage', | |
| 224 | + 'description' => 'A page giving contact details', | |
| 225 | + 'required_properties' => ['name', 'url'], | |
| 226 | + 'recommended_properties' => ['description', 'author', 'datePublished', 'breadcrumb'], | |
| 227 | + 'rich_snippets' => ['webpage', 'breadcrumb'], | |
| 228 | + 'context_types' => ['page', 'post'], | |
| 229 | + 'priority' => 'medium' | |
| 230 | + ], | |
| 231 | + 'ProfilePage' => [ | |
| 232 | + 'name' => 'ProfilePage', | |
| 233 | + 'description' => 'A page about a single person or organisation', | |
| 234 | + 'required_properties' => ['name', 'url'], | |
| 235 | + 'recommended_properties' => ['description', 'author', 'datePublished', 'breadcrumb'], | |
| 236 | + 'rich_snippets' => ['webpage', 'breadcrumb'], | |
| 237 | + 'context_types' => ['page', 'post'], | |
| 238 | + 'priority' => 'medium' | |
| 239 | + ], | |
| 210 | 240 | 'FAQPage' => [ |
| 211 | 241 | 'name' => 'FAQPage', |
| 212 | 242 | 'description' => 'Frequently Asked Questions pages', |
| 213 | 243 | 'required_properties' => ['mainEntity'], |
| @@ -1599,8 +1629,19 @@ | ||
| 1599 | 1629 | } elseif ($context_id && in_array($context_type, ['post', 'page', 'product'], true)) { |
| 1600 | 1630 | // Post/page/product data |
| 1601 | 1631 | $post = get_post($context_id); |
| 1602 | 1632 | if ($post) { |
| 1633 | + // Resolve the featured image's URL to its ID here, where the ID | |
| 1634 | + // is in hand, so the schema builder does not query for an | |
| 1635 | + // attachment it was just given (#847). Offered as a hint rather | |
| 1636 | + // than asserted: `post_thumbnail_url` can swap the URL for one | |
| 1637 | + // the featured image does not own. | |
| 1638 | + $thumbnail_url = get_the_post_thumbnail_url($post->ID, 'full'); | |
| 1639 | + | |
| 1640 | + if ($thumbnail_url) { | |
| 1641 | + Attachment_Lookup::id_from_url((string) $thumbnail_url, (int) get_post_thumbnail_id($post->ID)); | |
| 1642 | + } | |
| 1643 | + | |
| 1603 | 1644 | $content_data = [ |
| 1604 | 1645 | 'title' => $post->post_title, |
| 1605 | 1646 | 'url' => get_permalink($post->ID), |
| 1606 | 1647 | 'excerpt' => $post->post_excerpt ?: \ThinkRank\Core\Seo_Text::trim_words($post->post_content, 30), |
| @@ -1614,9 +1655,9 @@ | ||
| 1614 | 1655 | // Google rejects as "Invalid value in field datePublished" |
| 1615 | 1656 | // and drops the Article rich result (#465). |
| 1616 | 1657 | 'date' => get_the_date('c', $post), |
| 1617 | 1658 | 'modified' => get_the_modified_date('c', $post), |
| 1618 | - 'image' => get_the_post_thumbnail_url($post->ID, 'full'), | |
| 1659 | + 'image' => $thumbnail_url, | |
| 1619 | 1660 | 'focus_keywords' => Focus_Keywords::get($post->ID), |
| 1620 | 1661 | 'business_data' => $this->get_business_data_from_local_seo(), |
| 1621 | 1662 | 'site_data' => $this->get_site_data_for_schema(), |
| 1622 | 1663 | 'social_data' => $this->get_social_data_for_schema() |
| @@ -2009,12 +2050,10 @@ | ||
| 2009 | 2050 | |
| 2010 | 2051 | /** |
| 2011 | 2052 | * Get deployed schemas for frontend integration |
| 2012 | 2053 | * |
| 2013 | - * PERFORMANCE OPTIMIZED: This method now uses: | |
| 2014 | - * 1. Schema caching layer (90% reduction in database queries) | |
| 2015 | - * 2. Window function approach instead of correlated subquery (80-90% query performance improvement) | |
| 2016 | - * 3. Composite index: idx_context_schema_active | |
| 2054 | + * Returns the newest active, deployed row of each schema type for the | |
| 2055 | + * context. Results are cached per context (see Schema_Cache_Manager). | |
| 2017 | 2056 | * |
| 2018 | 2057 | * @since 1.0.0 |
| 2019 | 2058 | * |
| 2020 | 2059 | * @param string $context_type Context type |
| @@ -2037,48 +2076,57 @@ | ||
| 2037 | 2076 | |
| 2038 | 2077 | // Use existing seo_schema table |
| 2039 | 2078 | $table_name = $wpdb->prefix . 'thinkrank_seo_schema'; |
| 2040 | 2079 | |
| 2041 | - // OPTIMIZED QUERY: Use window function approach to eliminate correlated subquery | |
| 2042 | - // This leverages the new composite index: idx_context_schema_active (context_type, schema_type, is_active, created_at DESC) | |
| 2080 | + // The newest row per type used to be picked with ROW_NUMBER() OVER | |
| 2081 | + // (PARTITION BY schema_type ...). Window functions need MySQL 8.0 / | |
| 2082 | + // MariaDB 10.2, and WordPress still runs on MySQL 5.7, where that is | |
| 2083 | + // a syntax error on every page view and no deployed schema is ever | |
| 2084 | + // output. A row is the newest of its type when no other row of the | |
| 2085 | + // same context and type outranks it, so NOT EXISTS keeps the | |
| 2086 | + // greatest-per-group in the database, and schema_data — JSON, and | |
| 2087 | + // large — is only transferred for the rows that are output. | |
| 2088 | + // | |
| 2089 | + // `<=>` is NULL-safe equality: the site context stores context_id | |
| 2090 | + // as NULL, and `n.context_id = s.context_id` is never true for it. | |
| 2091 | + // schema_id breaks a same-second tie, which the window function | |
| 2092 | + // left to chance. | |
| 2093 | + $args = [$context_type]; | |
| 2043 | 2094 | if (null === $context_id) { |
| 2044 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2045 | - $sql = sprintf( | |
| 2046 | - 'SELECT schema_type, schema_data FROM (SELECT schema_type, schema_data, ROW_NUMBER() OVER (PARTITION BY schema_type ORDER BY created_at DESC) as rn FROM %s WHERE context_type = %%s AND context_id IS NULL AND is_active = 1 AND validation_status IN (\'deployed\', \'valid\')) ranked WHERE rn = 1 ORDER BY schema_type', | |
| 2047 | - $table_name | |
| 2048 | - ); | |
| 2049 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2050 | - $deployed_schemas = $wpdb->get_results( | |
| 2051 | - $wpdb->prepare( | |
| 2052 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL is properly prepared with placeholders | |
| 2053 | - $sql, | |
| 2054 | - $context_type | |
| 2055 | - ), | |
| 2056 | - ARRAY_A | |
| 2057 | - ); | |
| 2095 | + $context_where = 's.context_id IS NULL'; | |
| 2058 | 2096 | } else { |
| 2059 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2060 | - $sql = sprintf( | |
| 2061 | - 'SELECT schema_type, schema_data FROM (SELECT schema_type, schema_data, ROW_NUMBER() OVER (PARTITION BY schema_type ORDER BY created_at DESC) as rn FROM %s WHERE context_type = %%s AND context_id = %%d AND is_active = 1 AND validation_status IN (\'deployed\', \'valid\')) ranked WHERE rn = 1 ORDER BY schema_type', | |
| 2062 | - $table_name | |
| 2063 | - ); | |
| 2064 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2065 | - $deployed_schemas = $wpdb->get_results( | |
| 2066 | - $wpdb->prepare( | |
| 2067 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL is properly prepared with placeholders | |
| 2068 | - $sql, | |
| 2069 | - $context_type, | |
| 2070 | - $context_id | |
| 2071 | - ), | |
| 2072 | - ARRAY_A | |
| 2073 | - ); | |
| 2097 | + $context_where = 's.context_id = %d'; | |
| 2098 | + $args[] = $context_id; | |
| 2074 | 2099 | } |
| 2075 | 2100 | |
| 2101 | + $sql = sprintf( | |
| 2102 | + 'SELECT s.schema_type, s.schema_data FROM %1$s s' | |
| 2103 | + . ' WHERE s.context_type = %%s AND %2$s AND s.is_active = 1 AND s.validation_status IN (\'deployed\', \'valid\')' | |
| 2104 | + . ' AND NOT EXISTS (' | |
| 2105 | + . 'SELECT 1 FROM %1$s n' | |
| 2106 | + . ' WHERE n.context_type = s.context_type AND n.context_id <=> s.context_id AND n.schema_type = s.schema_type' | |
| 2107 | + . ' AND n.is_active = 1 AND n.validation_status IN (\'deployed\', \'valid\')' | |
| 2108 | + . ' AND (n.created_at > s.created_at OR (n.created_at = s.created_at AND n.schema_id > s.schema_id))' | |
| 2109 | + . ')' | |
| 2110 | + . ' ORDER BY s.schema_type', | |
| 2111 | + $table_name, | |
| 2112 | + $context_where | |
| 2113 | + ); | |
| 2114 | + | |
| 2115 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2116 | + $deployed_schemas = $wpdb->get_results( | |
| 2117 | + $wpdb->prepare( | |
| 2118 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL is properly prepared with placeholders | |
| 2119 | + $sql, | |
| 2120 | + ...$args | |
| 2121 | + ), | |
| 2122 | + ARRAY_A | |
| 2123 | + ); | |
| 2124 | + | |
| 2076 | 2125 | // Deliberately no early return on an empty result: it has to reach the |
| 2077 | 2126 | // cache write below. Most URLs have no deployed schema, so gating the |
| 2078 | 2127 | // write on a non-empty result made the majority of front-end requests |
| 2079 | - // permanent cache misses, re-running a ROW_NUMBER() OVER (PARTITION BY | |
| 2080 | - // ...) query with two filesorts on every pageview (#392). | |
| 2128 | + // permanent cache misses, re-running the query on every pageview (#392). | |
| 2081 | 2129 | $deployed_schemas = $deployed_schemas ?: []; |
| 2082 | 2130 | |
| 2083 | 2131 | // Process schemas for return |
| 2084 | 2132 | $processed_schemas = []; |
| @@ -2258,9 +2306,12 @@ | ||
| 2258 | 2306 | } |
| 2259 | 2307 | |
| 2260 | 2308 | $type = $schema['@type'] ?? ''; |
| 2261 | 2309 | $type = is_array($type) ? reset($type) : $type; |
| 2262 | - $is_entity = in_array((string) $type, self::ENTITY_URL_TYPES, true); | |
| 2310 | + // A LocalBusiness is deployed under the subtype the site chose, so the | |
| 2311 | + // exemption has to cover every subtype, not only the literal root. | |
| 2312 | + $is_entity = in_array((string) $type, self::ENTITY_URL_TYPES, true) | |
| 2313 | + || \ThinkRank\Config\Local_Business_Types_Config::is_local_business($type); | |
| 2263 | 2314 | |
| 2264 | 2315 | if (isset($schema['url']) && !$is_entity) { |
| 2265 | 2316 | $schema['url'] = $permalink; |
| 2266 | 2317 | } |
| @@ -2288,9 +2339,15 @@ | ||
| 2288 | 2339 | * Covers non-ISO-8601 dates (#465) and WP locales in inLanguage, which must |
| 2289 | 2340 | * be a BCP-47 tag — en-US, not en_US (#473). Walks nested nodes so values |
| 2290 | 2341 | * inside author/publisher/@graph entries are covered too. |
| 2291 | 2342 | * |
| 2343 | + * Also decodes HTML entities in plain-text properties. Schema_Builder | |
| 2344 | + * stored the block editor's `&` as-is until 2.10.0, and nothing | |
| 2345 | + * decodes JSON-LD downstream, so every deployed node built from post text | |
| 2346 | + * published the entity literally. | |
| 2347 | + * | |
| 2292 | 2348 | * @since 1.16.0 |
| 2349 | + * @since 2.10.0 Decodes entities in plain-text properties. | |
| 2293 | 2350 | * |
| 2294 | 2351 | * @param array $schema Decoded schema data. |
| 2295 | 2352 | * @return array Normalised schema. |
| 2296 | 2353 | */ |
| @@ -2299,8 +2356,16 @@ | ||
| 2299 | 2356 | 'datePublished', 'dateModified', 'dateCreated', 'uploadDate', |
| 2300 | 2357 | 'startDate', 'endDate', 'validFrom', 'validThrough', 'expires', |
| 2301 | 2358 | ]; |
| 2302 | 2359 | |
| 2360 | + // Plain text in schema.org. Answer/HowToStep `text` is deliberately | |
| 2361 | + // absent: Google reads Answer.text as HTML, where an entity is correct | |
| 2362 | + // and decoding `<` would turn escaped text into live markup. | |
| 2363 | + static $text_keys = [ | |
| 2364 | + 'name', 'headline', 'alternativeHeadline', 'description', | |
| 2365 | + 'reviewBody', 'about', 'abstract', 'caption', | |
| 2366 | + ]; | |
| 2367 | + | |
| 2303 | 2368 | foreach ($schema as $key => $value) { |
| 2304 | 2369 | if (is_array($value)) { |
| 2305 | 2370 | $schema[$key] = $this->normalize_stored_schema($value); |
| 2306 | 2371 | continue; |
| @@ -2307,8 +2372,16 @@ | ||
| 2307 | 2372 | } |
| 2308 | 2373 | |
| 2309 | 2374 | if ('inLanguage' === $key && is_string($value) && '' !== $value) { |
| 2310 | 2375 | $schema[$key] = str_replace('_', '-', $value); |
| 2376 | + continue; | |
| 2377 | + } | |
| 2378 | + | |
| 2379 | + // Decode only: a snapshot already truncated with an ellipsis must | |
| 2380 | + // keep it, which the full Seo_Text::normalize_schema_text() would | |
| 2381 | + // strip as an excerpt marker. | |
| 2382 | + if (in_array($key, $text_keys, true) && is_string($value) && '' !== $value) { | |
| 2383 | + $schema[$key] = \ThinkRank\Core\Seo_Text::decode_schema_entities($value); | |
| 2311 | 2384 | continue; |
| 2312 | 2385 | } |
| 2313 | 2386 | |
| 2314 | 2387 | if (!in_array($key, $date_keys, true) || !is_string($value) || '' === $value) { |