| @@ -50,9 +50,30 @@ | ||
| 50 | 50 | */ |
| 51 | 51 | private const BUILDER_META_KEYS = [ |
| 52 | 52 | '_breakdance_data', // Oxygen 6+ / Breakdance |
| 53 | 53 | '_oxygen_data', // Oxygen (earlier releases) |
| 54 | - 'ct_builder_shortcodes', // Oxygen classic | |
| 54 | + // Oxygen classic. 4.x writes the tree as JSON to `ct_builder_json` | |
| 55 | + // while still keeping `ct_builder_shortcodes`. A post carrying only | |
| 56 | + // the JSON key used to match no key at all and fall through to an | |
| 57 | + // empty `post_content`, which reads as a one-word page (#776). | |
| 58 | + // | |
| 59 | + // Oxygen 4.8.3 then renamed every `ct_*` post meta key to `_ct_*` | |
| 60 | + // (`oxygen_vsb_update_4_8_3()` runs `oxy_prefix_meta_keys()` on | |
| 61 | + // upgrade, and `oxy_get_post_meta()` only reads the prefixed name | |
| 62 | + // from then on). A current Oxygen classic site therefore has only the | |
| 63 | + // underscored keys, which nothing here listed, so every one of its | |
| 64 | + // pages resolved as empty. The prefixed keys come first because they | |
| 65 | + // are what Oxygen itself reads; the bare ones cover a site that has | |
| 66 | + // not run the migration (or reverted it with `?unprefix_meta`). | |
| 67 | + // | |
| 68 | + // These four are not read in this loop: from_oxygen_classic() pairs | |
| 69 | + // each JSON key with its shortcode sibling so the two forms can be | |
| 70 | + // compared. They are listed here because this list is also what the | |
| 71 | + // word-count index watches and what FAQ detection scans. | |
| 72 | + '_ct_builder_json', // Oxygen classic 4.8.3+ (JSON tree) | |
| 73 | + 'ct_builder_json', // Oxygen classic 4.0-4.8.2 (JSON tree) | |
| 74 | + '_ct_builder_shortcodes', // Oxygen classic 4.8.3+ (shortcode tree) | |
| 75 | + 'ct_builder_shortcodes', // Oxygen classic < 4.8.3 (shortcode tree) | |
| 55 | 76 | '_elementor_data', // Elementor |
| 56 | 77 | // Beaver Builder. Published layout first: `_fl_builder_draft` holds |
| 57 | 78 | // unsaved changes and would score content the visitor cannot see. |
| 58 | 79 | // Both are arrays of stdClass nodes, which is why the walker below |
| @@ -97,8 +118,41 @@ | ||
| 97 | 118 | */ |
| 98 | 119 | private const BRICKS_COMPONENTS_OPTION = 'bricks_components'; |
| 99 | 120 | |
| 100 | 121 | /** |
| 122 | + * Bricks' element that renders the post's own `post_content`. | |
| 123 | + * | |
| 124 | + * A Bricks page normally discards `post_content` entirely, which is why | |
| 125 | + * anything left there is invisible. Dropping this element onto the canvas | |
| 126 | + * is the one way an author puts it back on the page, so its presence flips | |
| 127 | + * `post_content` from stale leftovers to content the visitor reads. | |
| 128 | + * | |
| 129 | + * @since 2.3.1 | |
| 130 | + * @var string | |
| 131 | + */ | |
| 132 | + private const BRICKS_POST_CONTENT_ELEMENT = 'post-content'; | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * Resolved Bricks trees for this request, keyed by post ID. | |
| 136 | + * | |
| 137 | + * Rendering one page asks for the tree about twenty times — every | |
| 138 | + * description, every schema node, the FAQ guard — and resolving it is not | |
| 139 | + * free. `bricks_content_source()` clears `Bricks\Database::$active_templates` | |
| 140 | + * before asking Bricks which content template applies, which defeats | |
| 141 | + * Bricks' own early-return and re-runs its whole template-condition engine; | |
| 142 | + * `expand_bricks_components()` then walks the tree again. Measured on a | |
| 143 | + * Bricks page with no content of its own, that was ten full runs of the | |
| 144 | + * rules engine per request. | |
| 145 | + * | |
| 146 | + * Per-request only, and only ever read back within one page render — a | |
| 147 | + * request that writes Bricks content does not also render it. | |
| 148 | + * | |
| 149 | + * @since 2.3.1 | |
| 150 | + * @var array<int,array<int,mixed>> | |
| 151 | + */ | |
| 152 | + private static array $bricks_trees = []; | |
| 153 | + | |
| 154 | + /** | |
| 101 | 155 | * JSON keys whose values are user-visible text. |
| 102 | 156 | * |
| 103 | 157 | * Builder trees mix content with configuration, so a blind string sweep |
| 104 | 158 | * would count CSS classes and option slugs as words. Matching on the key |
| @@ -109,11 +163,38 @@ | ||
| 109 | 163 | private const CONTENT_KEYS = [ |
| 110 | 164 | 'text', 'title', 'subtitle', 'heading', 'subheading', 'content', |
| 111 | 165 | 'description', 'caption', 'excerpt', 'label', 'value', 'html', |
| 112 | 166 | 'editor', 'quote', 'answer', 'question', 'body', 'button_text', |
| 167 | + // Oxygen classic keeps an element's copy in `options.ct_content` | |
| 168 | + // (headline, text block, rich text, link and button labels). It is the | |
| 169 | + // field Oxygen's own serializer moves between the tags when it writes | |
| 170 | + // shortcodes (`parse_components_tree()`), and the one Relevanssi and | |
| 171 | + // Oxygen's WPML integration read. Missing from this list, the walker | |
| 172 | + // kept only copy that happened to contain markup: a page of plain | |
| 173 | + // headings and paragraphs lost almost all of its words. | |
| 174 | + 'ct_content', | |
| 175 | + // Oxygen's composite elements keep their copy under `options.original` | |
| 176 | + // instead, one key per field. Taken from the list Oxygen itself treats | |
| 177 | + // as text when it serializes (`$options_to_encode`); the numeric price | |
| 178 | + // fields and the progress bar's right-hand percentage are left out. | |
| 179 | + 'testimonial_text', 'testimonial_author', 'testimonial_author_info', | |
| 180 | + 'icon_box_heading', 'icon_box_text', | |
| 181 | + 'pricing_box_package_title', 'pricing_box_package_subtitle', 'pricing_box_content', | |
| 182 | + 'progress_bar_left_text', | |
| 113 | 183 | ]; |
| 114 | 184 | |
| 115 | 185 | /** |
| 186 | + * Oxygen classic's storage generations, as JSON key => shortcode key. | |
| 187 | + * | |
| 188 | + * @since 2.10.0 | |
| 189 | + * @var array<string,string> | |
| 190 | + */ | |
| 191 | + private const OXYGEN_CLASSIC_KEYS = [ | |
| 192 | + '_ct_builder_json' => '_ct_builder_shortcodes', | |
| 193 | + 'ct_builder_json' => 'ct_builder_shortcodes', | |
| 194 | + ]; | |
| 195 | + | |
| 196 | + /** | |
| 116 | 197 | * JSON keys whose values hold a link destination. |
| 117 | 198 | * |
| 118 | 199 | * Builders store a link's destination in a structured field separate from |
| 119 | 200 | * its label, either as a bare URL string or as a `{ url: … }` object. |
| @@ -126,8 +207,71 @@ | ||
| 126 | 207 | 'link', 'url', 'href', 'link_url', 'button_link', 'permalink', 'link_to', |
| 127 | 208 | ]; |
| 128 | 209 | |
| 129 | 210 | /** |
| 211 | + * JSON keys whose values hold an embedded video's source. | |
| 212 | + * | |
| 213 | + * A builder's video widget keeps its destination in a provider-specific | |
| 214 | + * field — Elementor picks `youtube_url`, `vimeo_url`, `dailymotion_url` or | |
| 215 | + * `hosted_url` according to the chosen source type — none of which is a | |
| 216 | + * link field or a content field, so a video on a builder page reached the | |
| 217 | + * analyzers as nothing at all. | |
| 218 | + * | |
| 219 | + * These are deliberately kept out of URL_KEYS. A video is an embed, not an | |
| 220 | + * outbound link: rendering one as `<a href>` would add a spurious external | |
| 221 | + * link to every page carrying a video and skew the link counts. They are | |
| 222 | + * reconstructed as `<iframe>`/`<video>` instead, which the video detector | |
| 223 | + * recognises and the link and image counters ignore. | |
| 224 | + * | |
| 225 | + * @since 2.3.1 | |
| 226 | + * @var string[] | |
| 227 | + */ | |
| 228 | + private const VIDEO_KEYS = [ | |
| 229 | + 'youtube_url', 'vimeo_url', 'dailymotion_url', 'videopress_url', | |
| 230 | + 'hosted_url', 'video_url', 'video_src', 'video_link', | |
| 231 | + ]; | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * File extensions that mean a video source is a file, not a provider page. | |
| 235 | + * | |
| 236 | + * @since 2.3.1 | |
| 237 | + * @var string[] | |
| 238 | + */ | |
| 239 | + private const VIDEO_FILE_EXTENSIONS = ['mp4', 'webm', 'ogv', 'mov', 'm4v']; | |
| 240 | + | |
| 241 | + /** | |
| 242 | + * Source keys to trust for a declared video source type. | |
| 243 | + * | |
| 244 | + * A widget keeps one field per provider and does not clear the others when | |
| 245 | + * the author switches source: an Elementor video moved from YouTube to Self | |
| 246 | + * Hosted still carries the earlier `youtube_url`. Reading whichever key | |
| 247 | + * turns up first then emits the video the author replaced. The widget says | |
| 248 | + * which one it is actually playing, so that is read first and the flat key | |
| 249 | + * sweep is only the fallback for a builder that declares nothing. | |
| 250 | + * | |
| 251 | + * @since 2.3.1 | |
| 252 | + * @var array<string,string[]> | |
| 253 | + */ | |
| 254 | + private const VIDEO_KEYS_BY_TYPE = [ | |
| 255 | + 'youtube' => ['youtube_url'], | |
| 256 | + 'vimeo' => ['vimeo_url'], | |
| 257 | + 'dailymotion' => ['dailymotion_url'], | |
| 258 | + 'videopress' => ['videopress_url'], | |
| 259 | + 'hosted' => ['hosted_url', 'video_url', 'video_src', 'video_link'], | |
| 260 | + 'media' => ['hosted_url', 'video_url', 'video_src', 'video_link'], | |
| 261 | + 'file' => ['hosted_url', 'video_url', 'video_src', 'video_link'], | |
| 262 | + 'self_hosted' => ['hosted_url', 'video_url', 'video_src', 'video_link'], | |
| 263 | + ]; | |
| 264 | + | |
| 265 | + /** | |
| 266 | + * Keys a builder uses to name which video source a widget is playing. | |
| 267 | + * | |
| 268 | + * @since 2.3.1 | |
| 269 | + * @var string[] | |
| 270 | + */ | |
| 271 | + private const VIDEO_TYPE_KEYS = ['video_type', 'videotype', 'video_source', 'source_type']; | |
| 272 | + | |
| 273 | + /** | |
| 130 | 274 | * JSON keys whose values hold an image, as a URL string or `{ url, alt }`. |
| 131 | 275 | * |
| 132 | 276 | * @var string[] |
| 133 | 277 | */ |
| @@ -144,9 +288,13 @@ | ||
| 144 | 288 | * |
| 145 | 289 | * @var string[] |
| 146 | 290 | */ |
| 147 | 291 | private const HEADING_TAG_KEYS = [ |
| 148 | - 'header_size', 'heading_tag', 'html_tag', 'title_tag', 'tag', 'level', 'size', | |
| 292 | + // Lower-cased on both sides of the comparison, so `headingtag` is the | |
| 293 | + // camelCase `headingTag` Bricks uses throughout its own controls and | |
| 294 | + // which ThinkRank's Bricks elements declare. Without it their section | |
| 295 | + // headings counted as body copy and never reached heading structure. | |
| 296 | + 'header_size', 'heading_tag', 'headingtag', 'html_tag', 'title_tag', 'tag', 'level', 'size', | |
| 149 | 297 | ]; |
| 150 | 298 | |
| 151 | 299 | /** |
| 152 | 300 | * Keys whose value is alternative text for a sibling image. |
| @@ -155,8 +303,22 @@ | ||
| 155 | 303 | */ |
| 156 | 304 | private const ALT_KEYS = ['alt', 'alt_text', 'image_alt', 'title']; |
| 157 | 305 | |
| 158 | 306 | /** |
| 307 | + * The global post and every global `setup_postdata()` writes. | |
| 308 | + * | |
| 309 | + * Rendering points them at the post being analyzed, then puts each one | |
| 310 | + * back exactly as it was, unset included (#860). | |
| 311 | + * | |
| 312 | + * @since 2.12.0 | |
| 313 | + * @var string[] | |
| 314 | + */ | |
| 315 | + private const POSTDATA_GLOBALS = [ | |
| 316 | + 'post', 'id', 'authordata', 'currentday', 'currentmonth', | |
| 317 | + 'page', 'pages', 'multipage', 'more', 'numpages', | |
| 318 | + ]; | |
| 319 | + | |
| 320 | + /** | |
| 159 | 321 | * Resolve the content worth analyzing for a post. |
| 160 | 322 | * |
| 161 | 323 | * @param \WP_Post $post Post being analyzed. |
| 162 | 324 | * @return string HTML/text to analyze. |
| @@ -161,12 +323,231 @@ | ||
| 161 | 323 | * @param \WP_Post $post Post being analyzed. |
| 162 | 324 | * @return string HTML/text to analyze. |
| 163 | 325 | */ |
| 164 | 326 | public static function resolve(\WP_Post $post): string { |
| 165 | - return self::resolve_markup((string) $post->post_content, $post); | |
| 327 | + $raw = (string) $post->post_content; | |
| 328 | + | |
| 329 | + // A page built in Gutenberg and then switched to Bricks keeps its old | |
| 330 | + // blocks in `post_content` forever — Bricks never clears them, and | |
| 331 | + // never renders them either. Resolving that first meant the stale draft | |
| 332 | + // beat the tree the visitor actually reads, and it did not stop at the | |
| 333 | + // score: the same string becomes the meta description, og:description, | |
| 334 | + // twitter:description and the schema description. Starting from nothing | |
| 335 | + // sends the resolution straight to Bricks' storage, which is where this | |
| 336 | + // page's words are (#651). | |
| 337 | + // | |
| 338 | + // Only for the stored path. `resolve_markup()` is also called with live | |
| 339 | + // editor content, and the Bricks panel's resolver reads the canvas — | |
| 340 | + // discarding that would replace what the author is typing with the last | |
| 341 | + // save. | |
| 342 | + if (self::bricks_supersedes_post_content((int) $post->ID)) { | |
| 343 | + $raw = ''; | |
| 344 | + } | |
| 345 | + | |
| 346 | + return self::resolve_markup($raw, $post); | |
| 166 | 347 | } |
| 167 | 348 | |
| 168 | 349 | /** |
| 350 | + * Whether Bricks renders this post and throws its `post_content` away. | |
| 351 | + * | |
| 352 | + * True means anything still stored in `post_content` is invisible: it is | |
| 353 | + * not on the page, so it must not be scored, described or published as | |
| 354 | + * structured data. False covers both a post Bricks does not own and a | |
| 355 | + * Bricks page that puts `post_content` back with a Post Content element. | |
| 356 | + * | |
| 357 | + * @since 2.3.1 | |
| 358 | + * | |
| 359 | + * @param int $post_id Post being resolved. | |
| 360 | + * @return bool | |
| 361 | + */ | |
| 362 | + public static function bricks_supersedes_post_content(int $post_id): bool { | |
| 363 | + $tree = self::bricks_tree($post_id); | |
| 364 | + | |
| 365 | + if (empty($tree)) { | |
| 366 | + return false; | |
| 367 | + } | |
| 368 | + | |
| 369 | + foreach ($tree as $element) { | |
| 370 | + if (is_array($element) | |
| 371 | + && self::BRICKS_POST_CONTENT_ELEMENT === ($element['name'] ?? null) | |
| 372 | + ) { | |
| 373 | + return false; | |
| 374 | + } | |
| 375 | + } | |
| 376 | + | |
| 377 | + return !self::bricks_tree_prints_post_content($tree); | |
| 378 | + } | |
| 379 | + | |
| 380 | + /** | |
| 381 | + * Whether a Bricks tree prints the body through a dynamic-data tag. | |
| 382 | + * | |
| 383 | + * The Post Content element is not the only way back onto the page: Bricks' | |
| 384 | + * `{post_content}` tag renders the same thing from inside an ordinary text | |
| 385 | + * element, and a single-post template written that way is a common shape. | |
| 386 | + * Missing it would mean the post's real body is discarded everywhere — | |
| 387 | + * scoring, the meta/og/twitter descriptions, the schema description — for a | |
| 388 | + * page that is displaying it. | |
| 389 | + * | |
| 390 | + * Matched over the encoded tree rather than per setting, because the tag can | |
| 391 | + * sit in any string field of any element and Bricks allows modifiers after | |
| 392 | + * the name (`{post_content:...}`). | |
| 393 | + * | |
| 394 | + * @since 2.3.1 | |
| 395 | + * | |
| 396 | + * @param array $tree Bricks element tree. | |
| 397 | + * @return bool | |
| 398 | + */ | |
| 399 | + private static function bricks_tree_prints_post_content(array $tree): bool { | |
| 400 | + $encoded = wp_json_encode($tree); | |
| 401 | + | |
| 402 | + return is_string($encoded) && false !== stripos($encoded, '{post_content'); | |
| 403 | + } | |
| 404 | + | |
| 405 | + /** | |
| 406 | + * The post's content as the visitor actually receives it. | |
| 407 | + * | |
| 408 | + * `post_content` for everything except a Bricks page that discards it, and | |
| 409 | + * there the Bricks tree's text. Descriptions are derived from a post's body | |
| 410 | + * in half a dozen places; every one of them wants this rather than the raw | |
| 411 | + * column (#651). | |
| 412 | + * | |
| 413 | + * @since 2.3.1 | |
| 414 | + * | |
| 415 | + * @param \WP_Post $post Post being described. | |
| 416 | + * @return string | |
| 417 | + */ | |
| 418 | + public static function visible_content(\WP_Post $post): string { | |
| 419 | + $superseding = self::superseding_content($post); | |
| 420 | + | |
| 421 | + return '' !== $superseding ? $superseding : (string) $post->post_content; | |
| 422 | + } | |
| 423 | + | |
| 424 | + /** | |
| 425 | + * Replacement body text for a post whose `post_content` does not render. | |
| 426 | + * | |
| 427 | + * Empty for every ordinary post, which is what makes this safe to call from | |
| 428 | + * paths that already handle excerpts their own way: they keep that handling | |
| 429 | + * and only a Bricks page is diverted. | |
| 430 | + * | |
| 431 | + * @since 2.3.1 | |
| 432 | + * | |
| 433 | + * @param \WP_Post $post Post being described. | |
| 434 | + * @return string Visible body text, or '' when `post_content` is fine. | |
| 435 | + */ | |
| 436 | + public static function superseding_content(\WP_Post $post): string { | |
| 437 | + if (!self::bricks_supersedes_post_content((int) $post->ID)) { | |
| 438 | + return ''; | |
| 439 | + } | |
| 440 | + | |
| 441 | + $bricks = self::from_bricks((int) $post->ID); | |
| 442 | + | |
| 443 | + return self::is_blank($bricks) ? '' : $bricks; | |
| 444 | + } | |
| 445 | + | |
| 446 | + /** | |
| 447 | + * Body text to derive a description from, when the usual source is wrong. | |
| 448 | + * | |
| 449 | + * A hand-written excerpt is the author's own summary and is correct however | |
| 450 | + * the page is built, so it yields '' here and the caller's normal | |
| 451 | + * `get_the_excerpt()` path keeps it. Only a Bricks page with no excerpt — | |
| 452 | + * where core would derive one from discarded `post_content` — gets diverted. | |
| 453 | + * | |
| 454 | + * @since 2.3.1 | |
| 455 | + * | |
| 456 | + * @param \WP_Post $post Post being described. | |
| 457 | + * @return string Text to summarize, or '' to leave the caller's path alone. | |
| 458 | + */ | |
| 459 | + public static function superseding_excerpt_source(\WP_Post $post): string { | |
| 460 | + if ('' !== trim((string) $post->post_excerpt)) { | |
| 461 | + return ''; | |
| 462 | + } | |
| 463 | + | |
| 464 | + return self::superseding_content($post); | |
| 465 | + } | |
| 466 | + | |
| 467 | + /** | |
| 468 | + * The Bricks element tree that renders for a post. | |
| 469 | + * | |
| 470 | + * Public because what Bricks puts on the page is not only a scoring | |
| 471 | + * question: the schema graph has to know whether a Bricks element already | |
| 472 | + * publishes the page's FAQ before adding one of its own (#649, #650). | |
| 473 | + * | |
| 474 | + * Flat, in Bricks' own storage shape — `expand_bricks_components()` | |
| 475 | + * appends component definitions to the same list rather than nesting them, | |
| 476 | + * so one `foreach` reaches every element. | |
| 477 | + * | |
| 478 | + * @since 2.3.1 | |
| 479 | + * | |
| 480 | + * @param int $post_id Post being resolved. | |
| 481 | + * @return array<int,mixed> Elements, or [] when Bricks renders nothing here. | |
| 482 | + */ | |
| 483 | + /** | |
| 484 | + * The builder meta keys, for callers that need to inspect the raw storage | |
| 485 | + * rather than the text extracted from it. | |
| 486 | + * | |
| 487 | + * The SEO Analyzer reads these to answer "is there a ThinkRank FAQ element | |
| 488 | + * on this post?", which is a question about the stored tree, not about the | |
| 489 | + * words in it (#686). | |
| 490 | + * | |
| 491 | + * @since 2.7.0 | |
| 492 | + * @return string[] | |
| 493 | + */ | |
| 494 | + public static function builder_meta_keys(): array { | |
| 495 | + return self::BUILDER_META_KEYS; | |
| 496 | + } | |
| 497 | + | |
| 498 | + public static function bricks_tree(int $post_id): array { | |
| 499 | + if (array_key_exists($post_id, self::$bricks_trees)) { | |
| 500 | + return self::$bricks_trees[$post_id]; | |
| 501 | + } | |
| 502 | + | |
| 503 | + self::$bricks_trees[$post_id] = self::resolve_bricks_tree($post_id); | |
| 504 | + | |
| 505 | + return self::$bricks_trees[$post_id]; | |
| 506 | + } | |
| 507 | + | |
| 508 | + /** | |
| 509 | + * Discard the resolved-tree memo. Test seam. | |
| 510 | + * | |
| 511 | + * @since 2.3.1 | |
| 512 | + * @return void | |
| 513 | + */ | |
| 514 | + public static function flush_bricks_cache(): void { | |
| 515 | + self::$bricks_trees = []; | |
| 516 | + } | |
| 517 | + | |
| 518 | + /** | |
| 519 | + * Read and resolve a post's Bricks tree, ignoring the memo. | |
| 520 | + * | |
| 521 | + * @since 2.3.1 | |
| 522 | + * | |
| 523 | + * @param int $post_id Post being resolved. | |
| 524 | + * @return array<int,mixed> | |
| 525 | + */ | |
| 526 | + private static function resolve_bricks_tree(int $post_id): array { | |
| 527 | + if (!self::bricks_owns_post($post_id)) { | |
| 528 | + return []; | |
| 529 | + } | |
| 530 | + | |
| 531 | + $source = self::bricks_content_source($post_id); | |
| 532 | + if (!$source) { | |
| 533 | + return []; | |
| 534 | + } | |
| 535 | + | |
| 536 | + $stored = get_post_meta($source, self::bricks_meta_key(), true); | |
| 537 | + | |
| 538 | + if (is_string($stored)) { | |
| 539 | + $stored = '' === trim($stored) ? null : json_decode($stored, true); | |
| 540 | + } | |
| 541 | + | |
| 542 | + if (!is_array($stored) || empty($stored)) { | |
| 543 | + return []; | |
| 544 | + } | |
| 545 | + | |
| 546 | + return self::expand_bricks_components($stored); | |
| 547 | + } | |
| 548 | + | |
| 549 | + /** | |
| 169 | 550 | * Resolve an arbitrary chunk of editor markup for the given post. |
| 170 | 551 | * |
| 171 | 552 | * The editor sends its live content to the scorer so an author sees their |
| 172 | 553 | * unsaved edits reflected. On a builder page that live string is the raw |
| @@ -187,9 +568,9 @@ | ||
| 187 | 568 | * @param \WP_Post $post Post the markup belongs to. |
| 188 | 569 | * @return string Content to analyze. |
| 189 | 570 | */ |
| 190 | 571 | public static function resolve_markup(string $raw, \WP_Post $post): string { |
| 191 | - $content = self::render_post_content($raw); | |
| 572 | + $content = self::render_post_content($raw, $post); | |
| 192 | 573 | |
| 193 | 574 | // Block markup that renders to nothing usually means the builder that |
| 194 | 575 | // owns those blocks did not register them in this context — Divi 5 |
| 195 | 576 | // loads its module library lazily per-request, so in CLI, REST, admin |
| @@ -237,19 +618,45 @@ | ||
| 237 | 618 | * |
| 238 | 619 | * Best-effort: a third-party block that fatals must not take the whole |
| 239 | 620 | * score down with it. |
| 240 | 621 | * |
| 241 | - * @param string $raw Raw post content. | |
| 622 | + * Runs as the post's own context, as it would on the front end. Admin and | |
| 623 | + * REST requests have no current post, so a shortcode reading | |
| 624 | + * `get_the_ID()` got nothing, and one looping a related-posts query left | |
| 625 | + * the global post on the last of them: its `wp_reset_postdata()` goes back | |
| 626 | + * to the main query's post, and there is none. On the Classic Editor this | |
| 627 | + * runs after the form prints its hidden `post_ID` and before the title and | |
| 628 | + * editor, which then showed the related post, and Update saved it over the | |
| 629 | + * original (#860). | |
| 630 | + * | |
| 631 | + * @param string $raw Raw post content. | |
| 632 | + * @param \WP_Post $post Post the content belongs to. | |
| 242 | 633 | * @return string Rendered content. |
| 243 | 634 | */ |
| 244 | - private static function render_post_content(string $raw): string { | |
| 635 | + private static function render_post_content(string $raw, \WP_Post $post): string { | |
| 245 | 636 | if ('' === trim($raw)) { |
| 246 | 637 | return ''; |
| 247 | 638 | } |
| 248 | 639 | |
| 249 | - $content = $raw; | |
| 640 | + $content = $raw; | |
| 641 | + $previous = self::snapshot_post_globals(); | |
| 250 | 642 | |
| 251 | 643 | try { |
| 644 | + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Made current for the render, restored in finally. | |
| 645 | + $GLOBALS['post'] = $post; | |
| 646 | + | |
| 647 | + // Fires `the_post`, which these paths never fired before: admin, | |
| 648 | + // REST and cron analysis had no current post at all. That is the | |
| 649 | + // same signal the front-end loop sends and it is what makes | |
| 650 | + // `get_the_ID()` work inside a shortcode, but it is a new call on a | |
| 651 | + // path that runs in bulk — the word-count index resolves every post | |
| 652 | + // it visits — so a theme that counts views on `the_post` will count | |
| 653 | + // them during indexing. Accepted deliberately: without it a | |
| 654 | + // shortcode cannot resolve its own post, which is the bug (#860). | |
| 655 | + if (function_exists('setup_postdata')) { | |
| 656 | + setup_postdata($post); | |
| 657 | + } | |
| 658 | + | |
| 252 | 659 | if (function_exists('has_blocks') && function_exists('do_blocks') && has_blocks($raw)) { |
| 253 | 660 | $content = do_blocks($raw); |
| 254 | 661 | } |
| 255 | 662 | |
| @@ -258,8 +665,10 @@ | ||
| 258 | 665 | $content = do_shortcode($content); |
| 259 | 666 | } |
| 260 | 667 | } catch (\Throwable $e) { |
| 261 | 668 | return $raw; |
| 669 | + } finally { | |
| 670 | + self::restore_post_globals($previous); | |
| 262 | 671 | } |
| 263 | 672 | |
| 264 | 673 | return self::is_blank($content) ? $raw : $content; |
| 265 | 674 | } |
| @@ -264,8 +673,49 @@ | ||
| 264 | 673 | return self::is_blank($content) ? $raw : $content; |
| 265 | 674 | } |
| 266 | 675 | |
| 267 | 676 | /** |
| 677 | + * The post globals as they are now; a global that is unset has no key. | |
| 678 | + * | |
| 679 | + * @since 2.12.0 | |
| 680 | + * | |
| 681 | + * @return array<string,mixed> | |
| 682 | + */ | |
| 683 | + private static function snapshot_post_globals(): array { | |
| 684 | + $snapshot = []; | |
| 685 | + | |
| 686 | + foreach (self::POSTDATA_GLOBALS as $name) { | |
| 687 | + if (array_key_exists($name, $GLOBALS)) { | |
| 688 | + $snapshot[$name] = $GLOBALS[$name]; | |
| 689 | + } | |
| 690 | + } | |
| 691 | + | |
| 692 | + return $snapshot; | |
| 693 | + } | |
| 694 | + | |
| 695 | + /** | |
| 696 | + * Put the post globals back as snapshot_post_globals() found them. | |
| 697 | + * | |
| 698 | + * Assigned directly rather than through `setup_postdata()`: there may have | |
| 699 | + * been no post to set up, and re-running it would fire `the_post` again. | |
| 700 | + * | |
| 701 | + * @since 2.12.0 | |
| 702 | + * | |
| 703 | + * @param array<string,mixed> $snapshot From snapshot_post_globals(). | |
| 704 | + * @return void | |
| 705 | + */ | |
| 706 | + private static function restore_post_globals(array $snapshot): void { | |
| 707 | + foreach (self::POSTDATA_GLOBALS as $name) { | |
| 708 | + if (array_key_exists($name, $snapshot)) { | |
| 709 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Core's own globals, put back as they were. | |
| 710 | + $GLOBALS[$name] = $snapshot[$name]; | |
| 711 | + } else { | |
| 712 | + unset($GLOBALS[$name]); | |
| 713 | + } | |
| 714 | + } | |
| 715 | + } | |
| 716 | + | |
| 717 | + /** | |
| 268 | 718 | * Extract text from the attributes of parsed blocks. |
| 269 | 719 | * |
| 270 | 720 | * @param string $raw Raw post content containing block markup. |
| 271 | 721 | * @return string Collected text, or '' when nothing was found. |
| @@ -319,31 +769,16 @@ | ||
| 319 | 769 | * @param int $post_id Post being resolved. |
| 320 | 770 | * @return string Extracted text, or '' when Bricks has nothing for it. |
| 321 | 771 | */ |
| 322 | 772 | private static function from_bricks(int $post_id): string { |
| 323 | - if (!self::bricks_owns_post($post_id)) { | |
| 324 | - return ''; | |
| 325 | - } | |
| 773 | + $tree = self::bricks_tree($post_id); | |
| 326 | 774 | |
| 327 | - $source = self::bricks_content_source($post_id); | |
| 328 | - if (!$source) { | |
| 775 | + if (empty($tree)) { | |
| 329 | 776 | return ''; |
| 330 | 777 | } |
| 331 | 778 | |
| 332 | - $stored = get_post_meta($source, self::bricks_meta_key(), true); | |
| 333 | - | |
| 334 | - if (is_string($stored)) { | |
| 335 | - $stored = '' === trim($stored) ? null : json_decode($stored, true); | |
| 336 | - } | |
| 337 | - | |
| 338 | - if (!is_array($stored) || empty($stored)) { | |
| 339 | - return ''; | |
| 340 | - } | |
| 341 | - | |
| 342 | 779 | return self::strip_bricks_dynamic_tags( |
| 343 | - self::text_from_tree( | |
| 344 | - self::without_bricks_element_labels(self::expand_bricks_components($stored)) | |
| 345 | - ) | |
| 780 | + self::text_from_tree(self::without_bricks_element_labels($tree)) | |
| 346 | 781 | ); |
| 347 | 782 | } |
| 348 | 783 | |
| 349 | 784 | /** |
| @@ -668,8 +1103,21 @@ | ||
| 668 | 1103 | return $bricks; |
| 669 | 1104 | } |
| 670 | 1105 | |
| 671 | 1106 | foreach (self::BUILDER_META_KEYS as $key) { |
| 1107 | + if (self::is_oxygen_classic_key($key)) { | |
| 1108 | + // Resolved as a pair, once, at the first of its keys. | |
| 1109 | + if ('_ct_builder_json' !== $key) { | |
| 1110 | + continue; | |
| 1111 | + } | |
| 1112 | + | |
| 1113 | + $oxygen = self::from_oxygen_classic($post_id); | |
| 1114 | + if (!self::is_blank($oxygen)) { | |
| 1115 | + return $oxygen; | |
| 1116 | + } | |
| 1117 | + continue; | |
| 1118 | + } | |
| 1119 | + | |
| 672 | 1120 | $stored = get_post_meta($post_id, $key, true); |
| 673 | 1121 | |
| 674 | 1122 | if (is_string($stored) && '' !== trim($stored)) { |
| 675 | 1123 | $decoded = json_decode($stored, true); |
| @@ -682,20 +1130,8 @@ | ||
| 682 | 1130 | } |
| 683 | 1131 | continue; |
| 684 | 1132 | } |
| 685 | 1133 | |
| 686 | - // Shortcode tree (Oxygen classic). | |
| 687 | - if (strpos($stored, '[') !== false && function_exists('do_shortcode')) { | |
| 688 | - try { | |
| 689 | - $rendered = do_shortcode($stored); | |
| 690 | - } catch (\Throwable $e) { | |
| 691 | - $rendered = $stored; | |
| 692 | - } | |
| 693 | - if (!self::is_blank($rendered)) { | |
| 694 | - return $rendered; | |
| 695 | - } | |
| 696 | - } | |
| 697 | - | |
| 698 | 1134 | continue; |
| 699 | 1135 | } |
| 700 | 1136 | |
| 701 | 1137 | // Some builders store an already-decoded tree — an array for most, |
| @@ -712,8 +1148,258 @@ | ||
| 712 | 1148 | return ''; |
| 713 | 1149 | } |
| 714 | 1150 | |
| 715 | 1151 | /** |
| 1152 | + * Whether a meta key is one of Oxygen classic's storage keys. | |
| 1153 | + * | |
| 1154 | + * @since 2.10.0 | |
| 1155 | + * | |
| 1156 | + * @param string $key Meta key. | |
| 1157 | + * @return bool | |
| 1158 | + */ | |
| 1159 | + private static function is_oxygen_classic_key(string $key): bool { | |
| 1160 | + return isset(self::OXYGEN_CLASSIC_KEYS[$key]) || in_array($key, self::OXYGEN_CLASSIC_KEYS, true); | |
| 1161 | + } | |
| 1162 | + | |
| 1163 | + /** | |
| 1164 | + * Text of an Oxygen classic page, from whichever stored form holds more. | |
| 1165 | + * | |
| 1166 | + * Oxygen 4.x keeps the same tree twice: as JSON, and as the shortcodes it | |
| 1167 | + * used before 4.0. The JSON is preferred because it carries copy the | |
| 1168 | + * shortcode form hides (a composite element's text is base64-encoded | |
| 1169 | + * inside `ct_options`, which is configuration and stripped). It is not | |
| 1170 | + * trusted blindly, though. Reading `ct_builder_json` first once meant a | |
| 1171 | + * key missing from CONTENT_KEYS silently threw the page away while the | |
| 1172 | + * shortcode copy sat unread next to it, because a non-empty JSON result | |
| 1173 | + * stopped the search. Comparing the two means the next such gap costs | |
| 1174 | + * nothing: the richer form wins. | |
| 1175 | + * | |
| 1176 | + * A generation is only read as a pair. The prefixed keys are what Oxygen | |
| 1177 | + * 4.8.3+ reads, so an unprefixed leftover next to them is stale. | |
| 1178 | + * | |
| 1179 | + * @since 2.10.0 | |
| 1180 | + * | |
| 1181 | + * @param int $post_id Post ID. | |
| 1182 | + * @return string Extracted text, or '' when Oxygen classic stored nothing. | |
| 1183 | + */ | |
| 1184 | + private static function from_oxygen_classic(int $post_id): string { | |
| 1185 | + foreach (self::OXYGEN_CLASSIC_KEYS as $json_key => $shortcode_key) { | |
| 1186 | + $json = get_post_meta($post_id, $json_key, true); | |
| 1187 | + $shortcodes = get_post_meta($post_id, $shortcode_key, true); | |
| 1188 | + | |
| 1189 | + $from_json = ''; | |
| 1190 | + if (is_string($json) && '' !== trim($json)) { | |
| 1191 | + $decoded = json_decode($json, true); | |
| 1192 | + if (is_array($decoded)) { | |
| 1193 | + // `[oxygen data="..."]` is a dynamic-data placeholder | |
| 1194 | + // Oxygen fills at render time. The shortcode path drops it | |
| 1195 | + // with every other tag, so it goes here too or the two | |
| 1196 | + // forms would disagree on the same page. | |
| 1197 | + $from_json = (string) preg_replace( | |
| 1198 | + '/\[oxygen\b[^\]]*\]/i', | |
| 1199 | + ' ', | |
| 1200 | + self::text_from_tree($decoded) | |
| 1201 | + ); | |
| 1202 | + } | |
| 1203 | + } | |
| 1204 | + | |
| 1205 | + $from_shortcodes = ''; | |
| 1206 | + if (is_string($shortcodes) && strpos($shortcodes, '[') !== false) { | |
| 1207 | + $from_shortcodes = self::text_from_shortcodes($shortcodes); | |
| 1208 | + } | |
| 1209 | + | |
| 1210 | + if (self::is_blank($from_json) && self::is_blank($from_shortcodes)) { | |
| 1211 | + continue; | |
| 1212 | + } | |
| 1213 | + | |
| 1214 | + return self::visible_word_count($from_json) >= self::visible_word_count($from_shortcodes) | |
| 1215 | + ? $from_json | |
| 1216 | + : $from_shortcodes; | |
| 1217 | + } | |
| 1218 | + | |
| 1219 | + return ''; | |
| 1220 | + } | |
| 1221 | + | |
| 1222 | + /** | |
| 1223 | + * Rough count of the words a visitor would read in extracted text. | |
| 1224 | + * | |
| 1225 | + * Only used to compare two extractions of the same page, so it needs to | |
| 1226 | + * be consistent rather than locale-exact. | |
| 1227 | + * | |
| 1228 | + * @since 2.10.0 | |
| 1229 | + * | |
| 1230 | + * @param string $text Extracted text or markup. | |
| 1231 | + * @return int | |
| 1232 | + */ | |
| 1233 | + private static function visible_word_count(string $text): int { | |
| 1234 | + $plain = trim((string) preg_replace('/\s+/u', ' ', wp_strip_all_tags($text))); | |
| 1235 | + | |
| 1236 | + return '' === $plain ? 0 : count(explode(' ', $plain)); | |
| 1237 | + } | |
| 1238 | + | |
| 1239 | + /** | |
| 1240 | + * Shortcode attributes that carry copy a visitor reads. | |
| 1241 | + * | |
| 1242 | + * An allow-list, not a deny-list. Oxygen Classic tags carry far more | |
| 1243 | + * attributes than they do copy — `id`, `class`, `selector`, `url`, | |
| 1244 | + * `ct_options` and friends — and a deny-list silently admits every | |
| 1245 | + * attribute a future builder release invents, which is how markup ends up | |
| 1246 | + * being counted as prose. | |
| 1247 | + * | |
| 1248 | + * @var string[] | |
| 1249 | + */ | |
| 1250 | + private const SHORTCODE_TEXT_ATTRIBUTES = [ | |
| 1251 | + 'text', | |
| 1252 | + 'content', | |
| 1253 | + 'heading', | |
| 1254 | + 'title', | |
| 1255 | + 'subtitle', | |
| 1256 | + 'label', | |
| 1257 | + 'caption', | |
| 1258 | + 'description', | |
| 1259 | + 'alt', | |
| 1260 | + 'button_text', | |
| 1261 | + 'link_text', | |
| 1262 | + ]; | |
| 1263 | + | |
| 1264 | + /** | |
| 1265 | + * Extract readable text from a shortcode tree, without rendering it. | |
| 1266 | + * | |
| 1267 | + * Oxygen Classic is the only builder whose storage is shortcodes rather | |
| 1268 | + * than JSON, and the previous implementation handed the string to | |
| 1269 | + * `do_shortcode()`. That silently depends on Oxygen having registered its | |
| 1270 | + * `ct_*` handlers in the current request — which it has on a front-end | |
| 1271 | + * view, and has not during bulk analysis, the post-list column, cron or | |
| 1272 | + * REST/MCP. With no handlers registered `do_shortcode()` returns its input | |
| 1273 | + * unchanged, so the raw shortcode source was scored as if it were the | |
| 1274 | + * page's prose: `[ct_section`, `id="section-1"` and the rest counted toward | |
| 1275 | + * the word count, while the actual copy sitting in `text="..."` attributes | |
| 1276 | + * was never counted at all (#776). | |
| 1277 | + * | |
| 1278 | + * `strip_shortcodes()` is no help either — it also only knows registered | |
| 1279 | + * shortcodes, so it leaves the same text untouched. | |
| 1280 | + * | |
| 1281 | + * Reading the stored tree directly is what every other builder here already | |
| 1282 | + * does, and it matches the class's stated design: no render engine, no | |
| 1283 | + * dependency on load order, safe during a bulk run. | |
| 1284 | + * | |
| 1285 | + * Parsing unconditionally, rather than rendering when Oxygen happens to be | |
| 1286 | + * loaded and parsing otherwise, is deliberate. It makes the extracted text | |
| 1287 | + * the same in every context, so the score in the editor matches the score | |
| 1288 | + * from a bulk run or from MCP. The old code produced whichever of the two | |
| 1289 | + * the request happened to allow, which is why the same post could report | |
| 1290 | + * two different word counts depending on how it was asked. | |
| 1291 | + * | |
| 1292 | + * The trade-off is that rendered output (resolved images, links, anything | |
| 1293 | + * Oxygen pulls in from a reusable part) is no longer reflected here. For | |
| 1294 | + * what this text feeds — word count, content scoring, meta-description | |
| 1295 | + * fallbacks and schema text — that markup was never the point, and counting | |
| 1296 | + * it only when the builder happened to be booted was the bug. | |
| 1297 | + * | |
| 1298 | + * @since 2.10.0 | |
| 1299 | + * | |
| 1300 | + * @param string $stored Raw shortcode source. | |
| 1301 | + * @return string Extracted text. | |
| 1302 | + */ | |
| 1303 | + private static function text_from_shortcodes(string $stored): string { | |
| 1304 | + // Oxygen stores each element's settings as a JSON blob in `ct_options`. | |
| 1305 | + // It is configuration, never copy, and it contains braces and brackets | |
| 1306 | + // that would otherwise confuse the tag scan below, so it goes first. | |
| 1307 | + // | |
| 1308 | + // The blob is matched as a balanced JSON object, not as "up to the | |
| 1309 | + // next quote". Oxygen wraps it in single quotes but does not escape | |
| 1310 | + // an apostrophe inside it (`"nicename":"Bob's Plumbing"`), so the | |
| 1311 | + // quote-to-quote match stopped mid-value and the rest of the blob, | |
| 1312 | + // `s Plumbing"}'` and all, was left in the tag and leaked into the | |
| 1313 | + // text. Strings inside the object are skipped whole, so neither a quote | |
| 1314 | + // nor a brace inside a value can end the match early. | |
| 1315 | + $source = (string) preg_replace( | |
| 1316 | + '/\sct_options\s*=\s*\'(?<obj>\{(?:[^{}"]++|"(?:[^"\\\\]|\\\\.)*+"|(?&obj))*+\})\'/s', | |
| 1317 | + '', | |
| 1318 | + $stored | |
| 1319 | + ); | |
| 1320 | + | |
| 1321 | + // Anything not shaped like Oxygen's JSON blob keeps the old, | |
| 1322 | + // quote-delimited strip. | |
| 1323 | + $source = (string) preg_replace( | |
| 1324 | + '/\sct_options\s*=\s*(["\']).*?\1/s', | |
| 1325 | + '', | |
| 1326 | + $source | |
| 1327 | + ); | |
| 1328 | + | |
| 1329 | + $attributes = implode('|', array_map( | |
| 1330 | + static fn(string $name): string => preg_quote($name, '/'), | |
| 1331 | + self::SHORTCODE_TEXT_ATTRIBUTES | |
| 1332 | + )); | |
| 1333 | + | |
| 1334 | + // Replace each shortcode tag with whatever readable copy its attributes | |
| 1335 | + // carry. Text between tags is left exactly where it is, so the result | |
| 1336 | + // keeps the page's reading order rather than hoisting all the headings | |
| 1337 | + // to the front. | |
| 1338 | + // The attribute blob is matched quote-aware rather than as "anything up | |
| 1339 | + // to the first `]`". Oxygen copy contains brackets often enough to | |
| 1340 | + // matter — "Best tools [2026]", "[Updated] our policy" — and a naive | |
| 1341 | + // scan ends the tag inside the `text` attribute, dropping the copy | |
| 1342 | + // before the bracket and leaking the stray `"]` after it into the | |
| 1343 | + // prose. Which is this bug's own failure mode: the wrong text scored. | |
| 1344 | + // | |
| 1345 | + // A tag name must start with a letter or underscore. `[2026]` is not a | |
| 1346 | + // shortcode anyone can register, and scanning it as one dropped the | |
| 1347 | + // year out of "Best tools [2026]". | |
| 1348 | + $text = (string) preg_replace_callback( | |
| 1349 | + '/\[\/?[a-zA-Z_][a-zA-Z0-9_-]*((?:[^\]"\']|"[^"]*"|\'[^\']*\')*)\]/', | |
| 1350 | + static function (array $matches) use ($attributes): string { | |
| 1351 | + if ('' === trim($matches[1])) { | |
| 1352 | + return ' '; | |
| 1353 | + } | |
| 1354 | + | |
| 1355 | + if (!preg_match_all( | |
| 1356 | + '/\b(' . $attributes . ')\s*=\s*(["\'])(.*?)\2/s', | |
| 1357 | + $matches[1], | |
| 1358 | + $found, | |
| 1359 | + PREG_SET_ORDER | |
| 1360 | + )) { | |
| 1361 | + return ' '; | |
| 1362 | + } | |
| 1363 | + | |
| 1364 | + $parts = []; | |
| 1365 | + foreach ($found as $attribute) { | |
| 1366 | + $value = trim($attribute[3]); | |
| 1367 | + | |
| 1368 | + // An attribute holding markup or a JSON fragment is | |
| 1369 | + // configuration that happens to share a name with a copy | |
| 1370 | + // field, not something a visitor reads. | |
| 1371 | + if ('' === $value || preg_match('/^[\[{<]/', $value)) { | |
| 1372 | + continue; | |
| 1373 | + } | |
| 1374 | + | |
| 1375 | + $parts[] = $value; | |
| 1376 | + } | |
| 1377 | + | |
| 1378 | + return empty($parts) ? ' ' : ' ' . implode(' ', $parts) . ' '; | |
| 1379 | + }, | |
| 1380 | + $source | |
| 1381 | + ); | |
| 1382 | + | |
| 1383 | + // Oxygen escapes square brackets in an element's copy before writing | |
| 1384 | + // it between the tags, so that "Best tools [2026]" cannot be mistaken | |
| 1385 | + // for a shortcode (`oxygen_vsb_filter_shortcode_content_encode()`). | |
| 1386 | + // Decoded only now, after the tag scan, for the same reason; left | |
| 1387 | + // encoded, the placeholders were scored as words of their own. | |
| 1388 | + $text = str_replace( | |
| 1389 | + ['_OXY_OPENING_BRACKET_', '_OXY_CLOSING_BRACKET_'], | |
| 1390 | + ['[', ']'], | |
| 1391 | + $text | |
| 1392 | + ); | |
| 1393 | + | |
| 1394 | + // Entities are stored encoded in attributes (&, ’), and would | |
| 1395 | + // otherwise be counted as words. | |
| 1396 | + $text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8'); | |
| 1397 | + | |
| 1398 | + return trim((string) preg_replace('/\s+/u', ' ', $text)); | |
| 1399 | + } | |
| 1400 | + | |
| 1401 | + /** | |
| 716 | 1402 | * A node's children, whether it stores them as an array or an object. |
| 717 | 1403 | * |
| 718 | 1404 | * The walker used to return immediately on `!is_array($node)`, so an |
| 719 | 1405 | * object node was dropped along with its entire subtree — silently, as |
| @@ -785,9 +1471,10 @@ | ||
| 785 | 1471 | // would emit the same URL a second time as a bare link, and |
| 786 | 1472 | // would turn an image's own `url` field into a spurious <a>. |
| 787 | 1473 | if (is_string($child_key) |
| 788 | 1474 | && (in_array(strtolower($child_key), self::URL_KEYS, true) |
| 789 | - || in_array(strtolower($child_key), self::IMAGE_KEYS, true)) | |
| 1475 | + || in_array(strtolower($child_key), self::IMAGE_KEYS, true) | |
| 1476 | + || in_array(strtolower($child_key), self::VIDEO_KEYS, true)) | |
| 790 | 1477 | ) { |
| 791 | 1478 | continue; |
| 792 | 1479 | } |
| 793 | 1480 | |
| @@ -839,8 +1526,79 @@ | ||
| 839 | 1526 | return implode("\n", $collected); |
| 840 | 1527 | } |
| 841 | 1528 | |
| 842 | 1529 | /** |
| 1530 | + * The video source a node is actually playing, if any. | |
| 1531 | + * | |
| 1532 | + * @since 2.3.1 | |
| 1533 | + * | |
| 1534 | + * @param array $node Builder node. | |
| 1535 | + * @return string Video source, or '' when the node carries none. | |
| 1536 | + */ | |
| 1537 | + private static function video_from(array $node): string { | |
| 1538 | + foreach ($node as $key => $value) { | |
| 1539 | + if (!is_string($key) || !is_string($value)) { | |
| 1540 | + continue; | |
| 1541 | + } | |
| 1542 | + | |
| 1543 | + if (!in_array(strtolower($key), self::VIDEO_TYPE_KEYS, true)) { | |
| 1544 | + continue; | |
| 1545 | + } | |
| 1546 | + | |
| 1547 | + $keys = self::VIDEO_KEYS_BY_TYPE[strtolower(trim($value))] ?? null; | |
| 1548 | + if (null === $keys) { | |
| 1549 | + continue; | |
| 1550 | + } | |
| 1551 | + | |
| 1552 | + // A recognised video_type settles it, including when that | |
| 1553 | + // provider's own field is empty. Falling through to the flat sweep | |
| 1554 | + // there handed back whichever sibling key happened to come first in | |
| 1555 | + // node order — the stale youtube_url left behind after switching | |
| 1556 | + // the widget to a hosted file, which is exactly what keying on the | |
| 1557 | + // declared type is meant to prevent. | |
| 1558 | + $declared = self::url_from($node, $keys); | |
| 1559 | + | |
| 1560 | + return self::is_video_source($declared) ? $declared : ''; | |
| 1561 | + } | |
| 1562 | + | |
| 1563 | + $url = self::url_from($node, self::VIDEO_KEYS); | |
| 1564 | + | |
| 1565 | + return self::is_video_source($url) ? $url : ''; | |
| 1566 | + } | |
| 1567 | + | |
| 1568 | + /** | |
| 1569 | + * Whether a value can be a video source. | |
| 1570 | + * | |
| 1571 | + * `looks_like_url()` also accepts `#anchor`, `mailto:` and `tel:`, which a | |
| 1572 | + * link node may legitimately hold but a video cannot: `<iframe src="#top">` | |
| 1573 | + * is not a video and would reach a video sitemap as one. | |
| 1574 | + * | |
| 1575 | + * @since 2.3.1 | |
| 1576 | + * | |
| 1577 | + * @param string $url Candidate source. | |
| 1578 | + * @return bool | |
| 1579 | + */ | |
| 1580 | + private static function is_video_source(string $url): bool { | |
| 1581 | + return '' !== $url | |
| 1582 | + && (1 === preg_match('#^(https?:)?//#i', $url) || str_starts_with($url, '/')); | |
| 1583 | + } | |
| 1584 | + | |
| 1585 | + /** | |
| 1586 | + * Whether a video source points at a file rather than a provider page. | |
| 1587 | + * | |
| 1588 | + * @since 2.3.1 | |
| 1589 | + * | |
| 1590 | + * @param string $url Video source. | |
| 1591 | + * @return bool | |
| 1592 | + */ | |
| 1593 | + private static function is_video_file(string $url): bool { | |
| 1594 | + $path = (string) wp_parse_url($url, PHP_URL_PATH); | |
| 1595 | + $ext = strtolower((string) pathinfo($path, PATHINFO_EXTENSION)); | |
| 1596 | + | |
| 1597 | + return in_array($ext, self::VIDEO_FILE_EXTENSIONS, true); | |
| 1598 | + } | |
| 1599 | + | |
| 1600 | + /** | |
| 843 | 1601 | * Rebuild the HTML a single builder node represents, if any. |
| 844 | 1602 | * |
| 845 | 1603 | * Looks only at the node's own fields (plus one level of nesting, because |
| 846 | 1604 | * builders commonly wrap a destination as `{ url: … }`). Returns an empty |
| @@ -856,12 +1614,23 @@ | ||
| 856 | 1614 | private static function markup_for_node(array $node, array &$consumed): string { |
| 857 | 1615 | $text = self::first_value($node, self::CONTENT_KEYS); |
| 858 | 1616 | $url = self::url_from($node, self::URL_KEYS); |
| 859 | 1617 | $image = self::image_from($node); |
| 1618 | + $video = self::video_from($node); | |
| 860 | 1619 | $tag = self::heading_tag_from($node); |
| 861 | 1620 | |
| 862 | 1621 | $parts = []; |
| 863 | 1622 | |
| 1623 | + // Video: an embed shape rather than a link, so the video detector can | |
| 1624 | + // see it while the link counters do not mistake it for an outbound | |
| 1625 | + // link. A file source becomes <video src>, anything else an <iframe>, | |
| 1626 | + // matching how the builder itself renders the two cases. | |
| 1627 | + if ('' !== $video) { | |
| 1628 | + $parts[] = self::is_video_file($video) | |
| 1629 | + ? sprintf('<video src="%s"></video>', esc_url_raw($video)) | |
| 1630 | + : sprintf('<iframe src="%s"></iframe>', esc_url_raw($video)); | |
| 1631 | + } | |
| 1632 | + | |
| 864 | 1633 | // Image: alt text matters as much as the tag, since alt checks run over |
| 865 | 1634 | // whatever this returns. |
| 866 | 1635 | if ('' !== $image['url']) { |
| 867 | 1636 | $alt = '' !== $image['alt'] ? $image['alt'] : (string) self::first_value($node, self::ALT_KEYS); |
| @@ -1050,13 +1819,30 @@ | ||
| 1050 | 1819 | return (bool) preg_match('#^(mailto:|tel:|\#)#i', $value); |
| 1051 | 1820 | } |
| 1052 | 1821 | |
| 1053 | 1822 | /** |
| 1054 | - * Whether a value carries no readable text. | |
| 1823 | + * Whether a value carries nothing worth analyzing. | |
| 1055 | 1824 | * |
| 1825 | + * Readable text is the usual signal, but not the only one: a page can be | |
| 1826 | + * made entirely of media. A builder section holding just a gallery | |
| 1827 | + * reconstructs to `<img>` tags and one holding just a video widget to a | |
| 1828 | + * single `<iframe>` — both strip to an empty string, so a text-only test | |
| 1829 | + * discarded them here and the page fell through to the next builder key, | |
| 1830 | + * then to the raw markup, and finally reported as having no content at all. | |
| 1831 | + * | |
| 1832 | + * Comments are dropped before the tag test: the raw markup this class falls | |
| 1833 | + * back to on a builder page is unrendered block comments, which must stay | |
| 1834 | + * blank rather than be mistaken for reconstructed media. | |
| 1835 | + * | |
| 1056 | 1836 | * @param string $value Candidate content. |
| 1057 | 1837 | * @return bool |
| 1058 | 1838 | */ |
| 1059 | 1839 | private static function is_blank(string $value): bool { |
| 1060 | - return '' === trim(wp_strip_all_tags($value)); | |
| 1840 | + if ('' !== trim(wp_strip_all_tags($value))) { | |
| 1841 | + return false; | |
| 1842 | + } | |
| 1843 | + | |
| 1844 | + $without_comments = (string) preg_replace('~<!--.*?-->~s', '', $value); | |
| 1845 | + | |
| 1846 | + return 1 !== preg_match('~<(?:a|img|iframe|video|source)\b~i', $without_comments); | |
| 1061 | 1847 | } |
| 1062 | 1848 | } |