| @@ -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'], |
| @@ -397,9 +427,25 @@ | ||
| 397 | 427 | 'business_phone', 'business_email', 'business_hours', |
| 398 | 428 | 'business_latitude', 'business_longitude', 'business_price_range', |
| 399 | 429 | ]; |
| 400 | 430 | |
| 401 | - if (empty(array_intersect_key($settings, array_flip($business_keys)))) { | |
| 431 | + // Which deployed types a Site Identity key can invalidate. The business | |
| 432 | + // block feeds LocalBusiness and Organization; alternate_name feeds the | |
| 433 | + // WebSite node, which had no entry here at all — so editing it left the | |
| 434 | + // deployed schema showing the previous value until something else | |
| 435 | + // happened to redeploy (#692). | |
| 436 | + $refresh_types = []; | |
| 437 | + | |
| 438 | + if (!empty(array_intersect_key($settings, array_flip($business_keys)))) { | |
| 439 | + $refresh_types[] = 'LocalBusiness'; | |
| 440 | + $refresh_types[] = 'Organization'; | |
| 441 | + } | |
| 442 | + | |
| 443 | + if (array_key_exists('alternate_name', $settings)) { | |
| 444 | + $refresh_types[] = 'WebSite'; | |
| 445 | + } | |
| 446 | + | |
| 447 | + if (empty($refresh_types)) { | |
| 402 | 448 | return; |
| 403 | 449 | } |
| 404 | 450 | |
| 405 | 451 | $schema_settings = $this->get_settings($context_type, $context_id); |
| @@ -409,9 +455,9 @@ | ||
| 409 | 455 | |
| 410 | 456 | // Only refresh types that are actually deployed, so this never adds a |
| 411 | 457 | // type the admin did not enable. |
| 412 | 458 | $deployed = array_keys((array) $this->get_deployed_schemas($context_type, $context_id)); |
| 413 | - $affected = array_values(array_intersect($deployed, ['LocalBusiness', 'Organization'])); | |
| 459 | + $affected = array_values(array_intersect($deployed, $refresh_types)); | |
| 414 | 460 | |
| 415 | 461 | if (empty($affected)) { |
| 416 | 462 | return; |
| 417 | 463 | } |
| @@ -1279,9 +1325,9 @@ | ||
| 1279 | 1325 | $this->cache_manager->invalidate_all_cache(); |
| 1280 | 1326 | } |
| 1281 | 1327 | |
| 1282 | 1328 | // AUTO-DEPLOY: Automatically regenerate and deploy schema when settings change |
| 1283 | - if ($success && !empty($settings['auto_deploy'])) { | |
| 1329 | + if ($success && self::should_auto_deploy($settings, $this->get_settings($context_type, $context_id))) { | |
| 1284 | 1330 | $this->auto_deploy_schema_on_settings_change($context_type, $context_id, $settings); |
| 1285 | 1331 | } |
| 1286 | 1332 | |
| 1287 | 1333 | return $success; |
| @@ -1286,9 +1332,35 @@ | ||
| 1286 | 1332 | |
| 1287 | 1333 | return $success; |
| 1288 | 1334 | } |
| 1289 | 1335 | |
| 1336 | + | |
| 1290 | 1337 | /** |
| 1338 | + * Whether a save should redeploy the schema it changed. | |
| 1339 | + * | |
| 1340 | + * `auto_deploy` is a stored setting (on by default), not something a save | |
| 1341 | + * restates. Reading it off the incoming patch meant a partial save — which | |
| 1342 | + * is what the admin screen sends, one field at a time — skipped | |
| 1343 | + * auto-deploy on a site that had it switched on, and the deployed snapshot | |
| 1344 | + * the front end serves kept the name, logo and sameAs it was deployed | |
| 1345 | + * with, however often the user saved (#904, the gate #12 left in place). | |
| 1346 | + * | |
| 1347 | + * A patch that does carry the key still wins, so a caller can deploy or | |
| 1348 | + * hold deliberately. | |
| 1349 | + * | |
| 1350 | + * @param array $patch Settings being saved | |
| 1351 | + * @param array $stored Settings as stored, after the save | |
| 1352 | + * @return bool | |
| 1353 | + */ | |
| 1354 | + public static function should_auto_deploy(array $patch, array $stored): bool { | |
| 1355 | + if (array_key_exists('auto_deploy', $patch)) { | |
| 1356 | + return !empty($patch['auto_deploy']); | |
| 1357 | + } | |
| 1358 | + | |
| 1359 | + return !empty($stored['auto_deploy']); | |
| 1360 | + } | |
| 1361 | + | |
| 1362 | + /** | |
| 1291 | 1363 | * Auto-deploy schema when settings change |
| 1292 | 1364 | * |
| 1293 | 1365 | * Automatically regenerates and deploys schema markup when organization or other |
| 1294 | 1366 | * schema settings are modified, ensuring the frontend output stays in sync. |
| @@ -1583,12 +1655,23 @@ | ||
| 1583 | 1655 | } elseif ($context_id && in_array($context_type, ['post', 'page', 'product'], true)) { |
| 1584 | 1656 | // Post/page/product data |
| 1585 | 1657 | $post = get_post($context_id); |
| 1586 | 1658 | if ($post) { |
| 1659 | + // Resolve the featured image's URL to its ID here, where the ID | |
| 1660 | + // is in hand, so the schema builder does not query for an | |
| 1661 | + // attachment it was just given (#847). Offered as a hint rather | |
| 1662 | + // than asserted: `post_thumbnail_url` can swap the URL for one | |
| 1663 | + // the featured image does not own. | |
| 1664 | + $thumbnail_url = get_the_post_thumbnail_url($post->ID, 'full'); | |
| 1665 | + | |
| 1666 | + if ($thumbnail_url) { | |
| 1667 | + Attachment_Lookup::id_from_url((string) $thumbnail_url, (int) get_post_thumbnail_id($post->ID)); | |
| 1668 | + } | |
| 1669 | + | |
| 1587 | 1670 | $content_data = [ |
| 1588 | 1671 | 'title' => $post->post_title, |
| 1589 | 1672 | 'url' => get_permalink($post->ID), |
| 1590 | - 'excerpt' => $post->post_excerpt ?: wp_trim_words($post->post_content, 30), | |
| 1673 | + 'excerpt' => $post->post_excerpt ?: \ThinkRank\Core\Seo_Text::trim_words($post->post_content, 30), | |
| 1591 | 1674 | 'content' => $post->post_content, |
| 1592 | 1675 | 'author' => [ |
| 1593 | 1676 | 'name' => get_the_author_meta('display_name', $post->post_author), |
| 1594 | 1677 | 'url' => get_author_posts_url($post->post_author) |
| @@ -1598,9 +1681,9 @@ | ||
| 1598 | 1681 | // Google rejects as "Invalid value in field datePublished" |
| 1599 | 1682 | // and drops the Article rich result (#465). |
| 1600 | 1683 | 'date' => get_the_date('c', $post), |
| 1601 | 1684 | 'modified' => get_the_modified_date('c', $post), |
| 1602 | - 'image' => get_the_post_thumbnail_url($post->ID, 'full'), | |
| 1685 | + 'image' => $thumbnail_url, | |
| 1603 | 1686 | 'focus_keywords' => Focus_Keywords::get($post->ID), |
| 1604 | 1687 | 'business_data' => $this->get_business_data_from_local_seo(), |
| 1605 | 1688 | 'site_data' => $this->get_site_data_for_schema(), |
| 1606 | 1689 | 'social_data' => $this->get_social_data_for_schema() |
| @@ -1993,12 +2076,10 @@ | ||
| 1993 | 2076 | |
| 1994 | 2077 | /** |
| 1995 | 2078 | * Get deployed schemas for frontend integration |
| 1996 | 2079 | * |
| 1997 | - * PERFORMANCE OPTIMIZED: This method now uses: | |
| 1998 | - * 1. Schema caching layer (90% reduction in database queries) | |
| 1999 | - * 2. Window function approach instead of correlated subquery (80-90% query performance improvement) | |
| 2000 | - * 3. Composite index: idx_context_schema_active | |
| 2080 | + * Returns the newest active, deployed row of each schema type for the | |
| 2081 | + * context. Results are cached per context (see Schema_Cache_Manager). | |
| 2001 | 2082 | * |
| 2002 | 2083 | * @since 1.0.0 |
| 2003 | 2084 | * |
| 2004 | 2085 | * @param string $context_type Context type |
| @@ -2021,48 +2102,57 @@ | ||
| 2021 | 2102 | |
| 2022 | 2103 | // Use existing seo_schema table |
| 2023 | 2104 | $table_name = $wpdb->prefix . 'thinkrank_seo_schema'; |
| 2024 | 2105 | |
| 2025 | - // OPTIMIZED QUERY: Use window function approach to eliminate correlated subquery | |
| 2026 | - // This leverages the new composite index: idx_context_schema_active (context_type, schema_type, is_active, created_at DESC) | |
| 2106 | + // The newest row per type used to be picked with ROW_NUMBER() OVER | |
| 2107 | + // (PARTITION BY schema_type ...). Window functions need MySQL 8.0 / | |
| 2108 | + // MariaDB 10.2, and WordPress still runs on MySQL 5.7, where that is | |
| 2109 | + // a syntax error on every page view and no deployed schema is ever | |
| 2110 | + // output. A row is the newest of its type when no other row of the | |
| 2111 | + // same context and type outranks it, so NOT EXISTS keeps the | |
| 2112 | + // greatest-per-group in the database, and schema_data — JSON, and | |
| 2113 | + // large — is only transferred for the rows that are output. | |
| 2114 | + // | |
| 2115 | + // `<=>` is NULL-safe equality: the site context stores context_id | |
| 2116 | + // as NULL, and `n.context_id = s.context_id` is never true for it. | |
| 2117 | + // schema_id breaks a same-second tie, which the window function | |
| 2118 | + // left to chance. | |
| 2119 | + $args = [$context_type]; | |
| 2027 | 2120 | if (null === $context_id) { |
| 2028 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2029 | - $sql = sprintf( | |
| 2030 | - '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', | |
| 2031 | - $table_name | |
| 2032 | - ); | |
| 2033 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2034 | - $deployed_schemas = $wpdb->get_results( | |
| 2035 | - $wpdb->prepare( | |
| 2036 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL is properly prepared with placeholders | |
| 2037 | - $sql, | |
| 2038 | - $context_type | |
| 2039 | - ), | |
| 2040 | - ARRAY_A | |
| 2041 | - ); | |
| 2121 | + $context_where = 's.context_id IS NULL'; | |
| 2042 | 2122 | } else { |
| 2043 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2044 | - $sql = sprintf( | |
| 2045 | - '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', | |
| 2046 | - $table_name | |
| 2047 | - ); | |
| 2048 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2049 | - $deployed_schemas = $wpdb->get_results( | |
| 2050 | - $wpdb->prepare( | |
| 2051 | - // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL is properly prepared with placeholders | |
| 2052 | - $sql, | |
| 2053 | - $context_type, | |
| 2054 | - $context_id | |
| 2055 | - ), | |
| 2056 | - ARRAY_A | |
| 2057 | - ); | |
| 2123 | + $context_where = 's.context_id = %d'; | |
| 2124 | + $args[] = $context_id; | |
| 2058 | 2125 | } |
| 2059 | 2126 | |
| 2127 | + $sql = sprintf( | |
| 2128 | + 'SELECT s.schema_type, s.schema_data FROM %1$s s' | |
| 2129 | + . ' WHERE s.context_type = %%s AND %2$s AND s.is_active = 1 AND s.validation_status IN (\'deployed\', \'valid\')' | |
| 2130 | + . ' AND NOT EXISTS (' | |
| 2131 | + . 'SELECT 1 FROM %1$s n' | |
| 2132 | + . ' WHERE n.context_type = s.context_type AND n.context_id <=> s.context_id AND n.schema_type = s.schema_type' | |
| 2133 | + . ' AND n.is_active = 1 AND n.validation_status IN (\'deployed\', \'valid\')' | |
| 2134 | + . ' AND (n.created_at > s.created_at OR (n.created_at = s.created_at AND n.schema_id > s.schema_id))' | |
| 2135 | + . ')' | |
| 2136 | + . ' ORDER BY s.schema_type', | |
| 2137 | + $table_name, | |
| 2138 | + $context_where | |
| 2139 | + ); | |
| 2140 | + | |
| 2141 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Schema retrieval requires direct database access | |
| 2142 | + $deployed_schemas = $wpdb->get_results( | |
| 2143 | + $wpdb->prepare( | |
| 2144 | + // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter -- SQL is properly prepared with placeholders | |
| 2145 | + $sql, | |
| 2146 | + ...$args | |
| 2147 | + ), | |
| 2148 | + ARRAY_A | |
| 2149 | + ); | |
| 2150 | + | |
| 2060 | 2151 | // Deliberately no early return on an empty result: it has to reach the |
| 2061 | 2152 | // cache write below. Most URLs have no deployed schema, so gating the |
| 2062 | 2153 | // write on a non-empty result made the majority of front-end requests |
| 2063 | - // permanent cache misses, re-running a ROW_NUMBER() OVER (PARTITION BY | |
| 2064 | - // ...) query with two filesorts on every pageview (#392). | |
| 2154 | + // permanent cache misses, re-running the query on every pageview (#392). | |
| 2065 | 2155 | $deployed_schemas = $deployed_schemas ?: []; |
| 2066 | 2156 | |
| 2067 | 2157 | // Process schemas for return |
| 2068 | 2158 | $processed_schemas = []; |
| @@ -2242,9 +2332,12 @@ | ||
| 2242 | 2332 | } |
| 2243 | 2333 | |
| 2244 | 2334 | $type = $schema['@type'] ?? ''; |
| 2245 | 2335 | $type = is_array($type) ? reset($type) : $type; |
| 2246 | - $is_entity = in_array((string) $type, self::ENTITY_URL_TYPES, true); | |
| 2336 | + // A LocalBusiness is deployed under the subtype the site chose, so the | |
| 2337 | + // exemption has to cover every subtype, not only the literal root. | |
| 2338 | + $is_entity = in_array((string) $type, self::ENTITY_URL_TYPES, true) | |
| 2339 | + || \ThinkRank\Config\Local_Business_Types_Config::is_local_business($type); | |
| 2247 | 2340 | |
| 2248 | 2341 | if (isset($schema['url']) && !$is_entity) { |
| 2249 | 2342 | $schema['url'] = $permalink; |
| 2250 | 2343 | } |
| @@ -2272,9 +2365,15 @@ | ||
| 2272 | 2365 | * Covers non-ISO-8601 dates (#465) and WP locales in inLanguage, which must |
| 2273 | 2366 | * be a BCP-47 tag — en-US, not en_US (#473). Walks nested nodes so values |
| 2274 | 2367 | * inside author/publisher/@graph entries are covered too. |
| 2275 | 2368 | * |
| 2369 | + * Also decodes HTML entities in plain-text properties. Schema_Builder | |
| 2370 | + * stored the block editor's `&` as-is until 2.10.0, and nothing | |
| 2371 | + * decodes JSON-LD downstream, so every deployed node built from post text | |
| 2372 | + * published the entity literally. | |
| 2373 | + * | |
| 2276 | 2374 | * @since 1.16.0 |
| 2375 | + * @since 2.10.0 Decodes entities in plain-text properties. | |
| 2277 | 2376 | * |
| 2278 | 2377 | * @param array $schema Decoded schema data. |
| 2279 | 2378 | * @return array Normalised schema. |
| 2280 | 2379 | */ |
| @@ -2283,8 +2382,16 @@ | ||
| 2283 | 2382 | 'datePublished', 'dateModified', 'dateCreated', 'uploadDate', |
| 2284 | 2383 | 'startDate', 'endDate', 'validFrom', 'validThrough', 'expires', |
| 2285 | 2384 | ]; |
| 2286 | 2385 | |
| 2386 | + // Plain text in schema.org. Answer/HowToStep `text` is deliberately | |
| 2387 | + // absent: Google reads Answer.text as HTML, where an entity is correct | |
| 2388 | + // and decoding `<` would turn escaped text into live markup. | |
| 2389 | + static $text_keys = [ | |
| 2390 | + 'name', 'headline', 'alternativeHeadline', 'description', | |
| 2391 | + 'reviewBody', 'about', 'abstract', 'caption', | |
| 2392 | + ]; | |
| 2393 | + | |
| 2287 | 2394 | foreach ($schema as $key => $value) { |
| 2288 | 2395 | if (is_array($value)) { |
| 2289 | 2396 | $schema[$key] = $this->normalize_stored_schema($value); |
| 2290 | 2397 | continue; |
| @@ -2294,8 +2401,16 @@ | ||
| 2294 | 2401 | $schema[$key] = str_replace('_', '-', $value); |
| 2295 | 2402 | continue; |
| 2296 | 2403 | } |
| 2297 | 2404 | |
| 2405 | + // Decode only: a snapshot already truncated with an ellipsis must | |
| 2406 | + // keep it, which the full Seo_Text::normalize_schema_text() would | |
| 2407 | + // strip as an excerpt marker. | |
| 2408 | + if (in_array($key, $text_keys, true) && is_string($value) && '' !== $value) { | |
| 2409 | + $schema[$key] = \ThinkRank\Core\Seo_Text::decode_schema_entities($value); | |
| 2410 | + continue; | |
| 2411 | + } | |
| 2412 | + | |
| 2298 | 2413 | if (!in_array($key, $date_keys, true) || !is_string($value) || '' === $value) { |
| 2299 | 2414 | continue; |
| 2300 | 2415 | } |
| 2301 | 2416 | |
| @@ -2598,8 +2713,11 @@ | ||
| 2598 | 2713 | 'site_url' => home_url(), |
| 2599 | 2714 | 'admin_email' => get_option('admin_email'), |
| 2600 | 2715 | 'language' => get_locale(), |
| 2601 | 2716 | 'timezone' => get_option('timezone_string'), |
| 2717 | + // Read by populate_website_schema(), so the deployed WebSite node | |
| 2718 | + // carries the same alternateName as the default one (#692). | |
| 2719 | + 'alternate_name' => $site_identity_settings['alternate_name'] ?? '', | |
| 2602 | 2720 | 'founded_date' => $site_identity_settings['founded_date'] ?? '', |
| 2603 | 2721 | 'founder_name' => $site_identity_settings['founder_name'] ?? '', |
| 2604 | 2722 | 'company_type' => $site_identity_settings['company_type'] ?? 'Organization', |
| 2605 | 2723 | // Site Identity assets |