| @@ -40,18 +40,22 @@ | ||
| 40 | 40 | |
| 41 | 41 | /** |
| 42 | 42 | * Post meta keys that hold builder data, in priority order. |
| 43 | 43 | * |
| 44 | - * Several generations of the same builder are listed on purpose: Oxygen 6 | |
| 45 | - * is Breakdance under the hood (`_breakdance_data`), while earlier Oxygen | |
| 46 | - * releases used `_oxygen_data` or the shortcode-based | |
| 47 | - * `ct_builder_shortcodes`. A site can only have one of them. | |
| 44 | + * Several generations of the same builder are listed on purpose. Oxygen 6 | |
| 45 | + * is Breakdance under the hood and writes the same tree, under its own | |
| 46 | + * prefix: Breakdance keeps it in `_breakdance_data`, Oxygen 6 in | |
| 47 | + * `_oxygen_data` (the key is `__bdox('_meta_prefix') . 'data'`, and the | |
| 48 | + * prefix is `_oxygen_` under Oxygen). Both store it inside a | |
| 49 | + * `tree_json_string` envelope, see unwrap_tree_envelope(). Earlier Oxygen | |
| 50 | + * releases used the shortcode-based `ct_builder_shortcodes` and its JSON | |
| 51 | + * sibling. A site can only have one of them. | |
| 48 | 52 | * |
| 49 | 53 | * @var string[] |
| 50 | 54 | */ |
| 51 | 55 | private const BUILDER_META_KEYS = [ |
| 52 | - '_breakdance_data', // Oxygen 6+ / Breakdance | |
| 53 | - '_oxygen_data', // Oxygen (earlier releases) | |
| 56 | + '_breakdance_data', // Breakdance | |
| 57 | + '_oxygen_data', // Oxygen 6+ (Breakdance engine, Oxygen prefix) | |
| 54 | 58 | // Oxygen classic. 4.x writes the tree as JSON to `ct_builder_json` |
| 55 | 59 | // while still keeping `ct_builder_shortcodes`. A post carrying only |
| 56 | 60 | // the JSON key used to match no key at all and fall through to an |
| 57 | 61 | // empty `post_content`, which reads as a one-word page (#776). |
| @@ -131,8 +135,42 @@ | ||
| 131 | 135 | */ |
| 132 | 136 | private const BRICKS_POST_CONTENT_ELEMENT = 'post-content'; |
| 133 | 137 | |
| 134 | 138 | /** |
| 139 | + * Bricks' Heading element, and the tag it renders when none is stored. | |
| 140 | + * | |
| 141 | + * Bricks leaves a setting out of storage while it equals its default, so a | |
| 142 | + * Heading left on its default tag is stored with no `tag` at all. Bricks | |
| 143 | + * 2.4.1 renders it as `h3` (`Element_Heading::$tag`, overridable by the | |
| 144 | + * active theme style's `tag`), and the walker, which only wraps text whose | |
| 145 | + * node names a tag, read it as body copy (#908). | |
| 146 | + * | |
| 147 | + * @since 2.15.0 | |
| 148 | + * @var string | |
| 149 | + */ | |
| 150 | + private const BRICKS_HEADING_ELEMENT = 'heading'; | |
| 151 | + | |
| 152 | + /** | |
| 153 | + * Tag a Bricks Heading renders when neither it nor a theme style sets one. | |
| 154 | + * | |
| 155 | + * @since 2.15.0 | |
| 156 | + * @var string | |
| 157 | + */ | |
| 158 | + private const BRICKS_HEADING_DEFAULT_TAG = 'h3'; | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Tag an Elementor Heading widget renders when `header_size` is not stored. | |
| 162 | + * | |
| 163 | + * Elementor saves `settings.toJSON({ remove: ['default'] })`, so a heading | |
| 164 | + * left on its default size has no `header_size` in `_elementor_data`, and | |
| 165 | + * that default is `h2` (#908). | |
| 166 | + * | |
| 167 | + * @since 2.15.0 | |
| 168 | + * @var string | |
| 169 | + */ | |
| 170 | + private const ELEMENTOR_HEADING_DEFAULT_TAG = 'h2'; | |
| 171 | + | |
| 172 | + /** | |
| 135 | 173 | * Resolved Bricks trees for this request, keyed by post ID. |
| 136 | 174 | * |
| 137 | 175 | * Rendering one page asks for the tree about twenty times — every |
| 138 | 176 | * description, every schema node, the FAQ guard — and resolving it is not |
| @@ -303,8 +341,22 @@ | ||
| 303 | 341 | */ |
| 304 | 342 | private const ALT_KEYS = ['alt', 'alt_text', 'image_alt', 'title']; |
| 305 | 343 | |
| 306 | 344 | /** |
| 345 | + * The global post and every global `setup_postdata()` writes. | |
| 346 | + * | |
| 347 | + * Rendering points them at the post being analyzed, then puts each one | |
| 348 | + * back exactly as it was, unset included (#860). | |
| 349 | + * | |
| 350 | + * @since 2.12.0 | |
| 351 | + * @var string[] | |
| 352 | + */ | |
| 353 | + private const POSTDATA_GLOBALS = [ | |
| 354 | + 'post', 'id', 'authordata', 'currentday', 'currentmonth', | |
| 355 | + 'page', 'pages', 'multipage', 'more', 'numpages', | |
| 356 | + ]; | |
| 357 | + | |
| 358 | + /** | |
| 307 | 359 | * Resolve the content worth analyzing for a post. |
| 308 | 360 | * |
| 309 | 361 | * @param \WP_Post $post Post being analyzed. |
| 310 | 362 | * @return string HTML/text to analyze. |
| @@ -528,12 +580,82 @@ | ||
| 528 | 580 | if (!is_array($stored) || empty($stored)) { |
| 529 | 581 | return []; |
| 530 | 582 | } |
| 531 | 583 | |
| 532 | - return self::expand_bricks_components($stored); | |
| 584 | + return self::expand_bricks_components(self::bricks_render_order($stored)); | |
| 533 | 585 | } |
| 534 | 586 | |
| 535 | 587 | /** |
| 588 | + * A flat Bricks element list, in the order Bricks renders it. | |
| 589 | + * | |
| 590 | + * Bricks stores one flat list and links it with `parent` and `children` | |
| 591 | + * ids. `Frontend::render_data()` renders the root elements in list order | |
| 592 | + * and each element's children in the order of its `children` array, so a | |
| 593 | + * child's position in the list says nothing about where it appears on the | |
| 594 | + * page. Walking the list as stored put a section's contents wherever they | |
| 595 | + * happened to be saved (#907). | |
| 596 | + * | |
| 597 | + * Anything the walk does not reach (an orphan, a cycle) keeps its stored | |
| 598 | + * position after the rest, so no copy is dropped. | |
| 599 | + * | |
| 600 | + * @since 2.15.0 | |
| 601 | + * | |
| 602 | + * @param array $elements Flat Bricks element list. | |
| 603 | + * @return array The same elements, in render order. | |
| 604 | + */ | |
| 605 | + private static function bricks_render_order(array $elements): array { | |
| 606 | + $by_id = []; | |
| 607 | + foreach ($elements as $index => $element) { | |
| 608 | + $id = is_array($element) ? ($element['id'] ?? null) : null; | |
| 609 | + if (is_scalar($id) && '' !== (string) $id && !isset($by_id[(string) $id])) { | |
| 610 | + $by_id[(string) $id] = $index; | |
| 611 | + } | |
| 612 | + } | |
| 613 | + | |
| 614 | + if (empty($by_id)) { | |
| 615 | + return $elements; | |
| 616 | + } | |
| 617 | + | |
| 618 | + $ordered = []; | |
| 619 | + $placed = []; | |
| 620 | + | |
| 621 | + $place = static function ($index) use (&$place, &$ordered, &$placed, $elements, $by_id): void { | |
| 622 | + if (isset($placed[$index])) { | |
| 623 | + return; | |
| 624 | + } | |
| 625 | + | |
| 626 | + $placed[$index] = true; | |
| 627 | + $ordered[] = $elements[$index]; | |
| 628 | + | |
| 629 | + $children = is_array($elements[$index]) ? ($elements[$index]['children'] ?? []) : []; | |
| 630 | + if (!is_array($children)) { | |
| 631 | + return; | |
| 632 | + } | |
| 633 | + | |
| 634 | + foreach ($children as $child_id) { | |
| 635 | + if (is_scalar($child_id) && isset($by_id[(string) $child_id])) { | |
| 636 | + $place($by_id[(string) $child_id]); | |
| 637 | + } | |
| 638 | + } | |
| 639 | + }; | |
| 640 | + | |
| 641 | + foreach ($elements as $index => $element) { | |
| 642 | + $parent = is_array($element) ? ($element['parent'] ?? null) : null; | |
| 643 | + if (empty($parent) || !is_scalar($parent) || !isset($by_id[(string) $parent])) { | |
| 644 | + $place($index); | |
| 645 | + } | |
| 646 | + } | |
| 647 | + | |
| 648 | + foreach ($elements as $index => $element) { | |
| 649 | + if (!isset($placed[$index])) { | |
| 650 | + $ordered[] = $element; | |
| 651 | + } | |
| 652 | + } | |
| 653 | + | |
| 654 | + return $ordered; | |
| 655 | + } | |
| 656 | + | |
| 657 | + /** | |
| 536 | 658 | * Resolve an arbitrary chunk of editor markup for the given post. |
| 537 | 659 | * |
| 538 | 660 | * The editor sends its live content to the scorer so an author sees their |
| 539 | 661 | * unsaved edits reflected. On a builder page that live string is the raw |
| @@ -554,9 +676,9 @@ | ||
| 554 | 676 | * @param \WP_Post $post Post the markup belongs to. |
| 555 | 677 | * @return string Content to analyze. |
| 556 | 678 | */ |
| 557 | 679 | public static function resolve_markup(string $raw, \WP_Post $post): string { |
| 558 | - $content = self::render_post_content($raw); | |
| 680 | + $content = self::render_post_content($raw, $post); | |
| 559 | 681 | |
| 560 | 682 | // Block markup that renders to nothing usually means the builder that |
| 561 | 683 | // owns those blocks did not register them in this context — Divi 5 |
| 562 | 684 | // loads its module library lazily per-request, so in CLI, REST, admin |
| @@ -604,19 +726,45 @@ | ||
| 604 | 726 | * |
| 605 | 727 | * Best-effort: a third-party block that fatals must not take the whole |
| 606 | 728 | * score down with it. |
| 607 | 729 | * |
| 608 | - * @param string $raw Raw post content. | |
| 730 | + * Runs as the post's own context, as it would on the front end. Admin and | |
| 731 | + * REST requests have no current post, so a shortcode reading | |
| 732 | + * `get_the_ID()` got nothing, and one looping a related-posts query left | |
| 733 | + * the global post on the last of them: its `wp_reset_postdata()` goes back | |
| 734 | + * to the main query's post, and there is none. On the Classic Editor this | |
| 735 | + * runs after the form prints its hidden `post_ID` and before the title and | |
| 736 | + * editor, which then showed the related post, and Update saved it over the | |
| 737 | + * original (#860). | |
| 738 | + * | |
| 739 | + * @param string $raw Raw post content. | |
| 740 | + * @param \WP_Post $post Post the content belongs to. | |
| 609 | 741 | * @return string Rendered content. |
| 610 | 742 | */ |
| 611 | - private static function render_post_content(string $raw): string { | |
| 743 | + private static function render_post_content(string $raw, \WP_Post $post): string { | |
| 612 | 744 | if ('' === trim($raw)) { |
| 613 | 745 | return ''; |
| 614 | 746 | } |
| 615 | 747 | |
| 616 | - $content = $raw; | |
| 748 | + $content = $raw; | |
| 749 | + $previous = self::snapshot_post_globals(); | |
| 617 | 750 | |
| 618 | 751 | try { |
| 752 | + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- Made current for the render, restored in finally. | |
| 753 | + $GLOBALS['post'] = $post; | |
| 754 | + | |
| 755 | + // Fires `the_post`, which these paths never fired before: admin, | |
| 756 | + // REST and cron analysis had no current post at all. That is the | |
| 757 | + // same signal the front-end loop sends and it is what makes | |
| 758 | + // `get_the_ID()` work inside a shortcode, but it is a new call on a | |
| 759 | + // path that runs in bulk — the word-count index resolves every post | |
| 760 | + // it visits — so a theme that counts views on `the_post` will count | |
| 761 | + // them during indexing. Accepted deliberately: without it a | |
| 762 | + // shortcode cannot resolve its own post, which is the bug (#860). | |
| 763 | + if (function_exists('setup_postdata')) { | |
| 764 | + setup_postdata($post); | |
| 765 | + } | |
| 766 | + | |
| 619 | 767 | if (function_exists('has_blocks') && function_exists('do_blocks') && has_blocks($raw)) { |
| 620 | 768 | $content = do_blocks($raw); |
| 621 | 769 | } |
| 622 | 770 | |
| @@ -625,8 +773,10 @@ | ||
| 625 | 773 | $content = do_shortcode($content); |
| 626 | 774 | } |
| 627 | 775 | } catch (\Throwable $e) { |
| 628 | 776 | return $raw; |
| 777 | + } finally { | |
| 778 | + self::restore_post_globals($previous); | |
| 629 | 779 | } |
| 630 | 780 | |
| 631 | 781 | return self::is_blank($content) ? $raw : $content; |
| 632 | 782 | } |
| @@ -631,8 +781,49 @@ | ||
| 631 | 781 | return self::is_blank($content) ? $raw : $content; |
| 632 | 782 | } |
| 633 | 783 | |
| 634 | 784 | /** |
| 785 | + * The post globals as they are now; a global that is unset has no key. | |
| 786 | + * | |
| 787 | + * @since 2.12.0 | |
| 788 | + * | |
| 789 | + * @return array<string,mixed> | |
| 790 | + */ | |
| 791 | + private static function snapshot_post_globals(): array { | |
| 792 | + $snapshot = []; | |
| 793 | + | |
| 794 | + foreach (self::POSTDATA_GLOBALS as $name) { | |
| 795 | + if (array_key_exists($name, $GLOBALS)) { | |
| 796 | + $snapshot[$name] = $GLOBALS[$name]; | |
| 797 | + } | |
| 798 | + } | |
| 799 | + | |
| 800 | + return $snapshot; | |
| 801 | + } | |
| 802 | + | |
| 803 | + /** | |
| 804 | + * Put the post globals back as snapshot_post_globals() found them. | |
| 805 | + * | |
| 806 | + * Assigned directly rather than through `setup_postdata()`: there may have | |
| 807 | + * been no post to set up, and re-running it would fire `the_post` again. | |
| 808 | + * | |
| 809 | + * @since 2.12.0 | |
| 810 | + * | |
| 811 | + * @param array<string,mixed> $snapshot From snapshot_post_globals(). | |
| 812 | + * @return void | |
| 813 | + */ | |
| 814 | + private static function restore_post_globals(array $snapshot): void { | |
| 815 | + foreach (self::POSTDATA_GLOBALS as $name) { | |
| 816 | + if (array_key_exists($name, $snapshot)) { | |
| 817 | + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Core's own globals, put back as they were. | |
| 818 | + $GLOBALS[$name] = $snapshot[$name]; | |
| 819 | + } else { | |
| 820 | + unset($GLOBALS[$name]); | |
| 821 | + } | |
| 822 | + } | |
| 823 | + } | |
| 824 | + | |
| 825 | + /** | |
| 635 | 826 | * Extract text from the attributes of parsed blocks. |
| 636 | 827 | * |
| 637 | 828 | * @param string $raw Raw post content containing block markup. |
| 638 | 829 | * @return string Collected text, or '' when nothing was found. |
| @@ -693,9 +884,9 @@ | ||
| 693 | 884 | return ''; |
| 694 | 885 | } |
| 695 | 886 | |
| 696 | 887 | return self::strip_bricks_dynamic_tags( |
| 697 | - self::text_from_tree(self::without_bricks_element_labels($tree)) | |
| 888 | + self::text_from_tree(self::with_bricks_heading_tags(self::without_bricks_element_labels($tree))) | |
| 698 | 889 | ); |
| 699 | 890 | } |
| 700 | 891 | |
| 701 | 892 | /** |
| @@ -893,9 +1084,9 @@ | ||
| 893 | 1084 | continue; |
| 894 | 1085 | } |
| 895 | 1086 | |
| 896 | 1087 | if (isset($component['id']) && $component['id'] === $cid && !empty($component['elements'])) { |
| 897 | - return is_array($component['elements']) ? $component['elements'] : []; | |
| 1088 | + return is_array($component['elements']) ? self::bricks_render_order($component['elements']) : []; | |
| 898 | 1089 | } |
| 899 | 1090 | } |
| 900 | 1091 | |
| 901 | 1092 | return []; |
| @@ -928,8 +1119,116 @@ | ||
| 928 | 1119 | return $tree; |
| 929 | 1120 | } |
| 930 | 1121 | |
| 931 | 1122 | /** |
| 1123 | + * Give each Bricks Heading the tag it renders with when none is stored. | |
| 1124 | + * | |
| 1125 | + * Applied on the Bricks path only. Most other builders' text nodes carry no | |
| 1126 | + * tag because they are not headings, so a generic "text without a tag is a | |
| 1127 | + * heading" rule in heading_tag_from() would turn every paragraph into one. | |
| 1128 | + * | |
| 1129 | + * A `tag` of `custom` is left alone: the element then renders its | |
| 1130 | + * `customTag`, which is not necessarily a heading. | |
| 1131 | + * | |
| 1132 | + * @since 2.15.0 | |
| 1133 | + * | |
| 1134 | + * @param array $tree Bricks content area. | |
| 1135 | + * @return array Tree with each untagged Heading's default tag filled in. | |
| 1136 | + */ | |
| 1137 | + private static function with_bricks_heading_tags(array $tree): array { | |
| 1138 | + $default = null; | |
| 1139 | + | |
| 1140 | + foreach ($tree as $index => $element) { | |
| 1141 | + if (!is_array($element) || self::BRICKS_HEADING_ELEMENT !== ($element['name'] ?? null)) { | |
| 1142 | + continue; | |
| 1143 | + } | |
| 1144 | + | |
| 1145 | + $settings = $element['settings'] ?? []; | |
| 1146 | + if (!is_array($settings)) { | |
| 1147 | + continue; | |
| 1148 | + } | |
| 1149 | + | |
| 1150 | + $tag = $settings['tag'] ?? ''; | |
| 1151 | + if (is_string($tag) && '' !== trim($tag)) { | |
| 1152 | + continue; | |
| 1153 | + } | |
| 1154 | + | |
| 1155 | + if (null === $default) { | |
| 1156 | + $default = self::bricks_default_heading_tag(); | |
| 1157 | + } | |
| 1158 | + | |
| 1159 | + $settings['tag'] = $default; | |
| 1160 | + $tree[$index]['settings'] = $settings; | |
| 1161 | + } | |
| 1162 | + | |
| 1163 | + return $tree; | |
| 1164 | + } | |
| 1165 | + | |
| 1166 | + /** | |
| 1167 | + * The tag Bricks gives a Heading that does not set one. | |
| 1168 | + * | |
| 1169 | + * The active theme style can change it. Bricks only loads theme styles for | |
| 1170 | + * a front-end render, so in admin, REST and CLI requests this is the | |
| 1171 | + * element's own default. | |
| 1172 | + * | |
| 1173 | + * @since 2.15.0 | |
| 1174 | + * | |
| 1175 | + * @return string Heading tag, h1 to h6. | |
| 1176 | + */ | |
| 1177 | + private static function bricks_default_heading_tag(): string { | |
| 1178 | + if (class_exists('\\Bricks\\Theme_Styles') | |
| 1179 | + && method_exists('\\Bricks\\Theme_Styles', 'get_setting_by_key') | |
| 1180 | + ) { | |
| 1181 | + try { | |
| 1182 | + $styled = \Bricks\Theme_Styles::get_setting_by_key(self::BRICKS_HEADING_ELEMENT, 'tag'); | |
| 1183 | + } catch (\Throwable $e) { | |
| 1184 | + $styled = null; | |
| 1185 | + } | |
| 1186 | + | |
| 1187 | + if (is_string($styled) && preg_match('/^h[1-6]$/i', trim($styled))) { | |
| 1188 | + return strtolower(trim($styled)); | |
| 1189 | + } | |
| 1190 | + } | |
| 1191 | + | |
| 1192 | + return self::BRICKS_HEADING_DEFAULT_TAG; | |
| 1193 | + } | |
| 1194 | + | |
| 1195 | + /** | |
| 1196 | + * Give each Elementor Heading widget its default `header_size` if unstored. | |
| 1197 | + * | |
| 1198 | + * @since 2.15.0 | |
| 1199 | + * | |
| 1200 | + * @param array $elements Decoded `_elementor_data`. | |
| 1201 | + * @return array The same tree, with untagged Heading widgets tagged. | |
| 1202 | + */ | |
| 1203 | + private static function with_elementor_heading_tags(array $elements): array { | |
| 1204 | + foreach ($elements as $index => $element) { | |
| 1205 | + if (!is_array($element)) { | |
| 1206 | + continue; | |
| 1207 | + } | |
| 1208 | + | |
| 1209 | + if ('heading' === ($element['widgetType'] ?? null)) { | |
| 1210 | + $settings = $element['settings'] ?? []; | |
| 1211 | + if (is_array($settings)) { | |
| 1212 | + $size = $settings['header_size'] ?? ''; | |
| 1213 | + if (!is_string($size) || '' === trim($size)) { | |
| 1214 | + $settings['header_size'] = self::ELEMENTOR_HEADING_DEFAULT_TAG; | |
| 1215 | + $element['settings'] = $settings; | |
| 1216 | + } | |
| 1217 | + } | |
| 1218 | + } | |
| 1219 | + | |
| 1220 | + if (!empty($element['elements']) && is_array($element['elements'])) { | |
| 1221 | + $element['elements'] = self::with_elementor_heading_tags($element['elements']); | |
| 1222 | + } | |
| 1223 | + | |
| 1224 | + $elements[$index] = $element; | |
| 1225 | + } | |
| 1226 | + | |
| 1227 | + return $elements; | |
| 1228 | + } | |
| 1229 | + | |
| 1230 | + /** | |
| 932 | 1231 | * Remove Bricks dynamic-data tags from extracted text. |
| 933 | 1232 | * |
| 934 | 1233 | * Bricks stores `{post_title}`, `{post_meta:price}`, `{echo:my_fn}` and the |
| 935 | 1234 | * like verbatim and resolves them when it renders. Extraction reads the |
| @@ -1037,12 +1336,15 @@ | ||
| 1037 | 1336 | $stored = get_post_meta($post_id, $key, true); |
| 1038 | 1337 | |
| 1039 | 1338 | if (is_string($stored) && '' !== trim($stored)) { |
| 1040 | 1339 | $decoded = json_decode($stored, true); |
| 1340 | + if ('_elementor_data' === $key && is_array($decoded)) { | |
| 1341 | + $decoded = self::with_elementor_heading_tags($decoded); | |
| 1342 | + } | |
| 1041 | 1343 | |
| 1042 | 1344 | // JSON node tree (Breakdance/Oxygen 6, Elementor). |
| 1043 | 1345 | if (is_array($decoded)) { |
| 1044 | - $text = self::text_from_tree($decoded); | |
| 1346 | + $text = self::text_from_tree(self::unwrap_tree_envelope($decoded)); | |
| 1045 | 1347 | if (!self::is_blank($text)) { |
| 1046 | 1348 | return $text; |
| 1047 | 1349 | } |
| 1048 | 1350 | continue; |
| @@ -1054,8 +1356,9 @@ | ||
| 1054 | 1356 | // Some builders store an already-decoded tree — an array for most, |
| 1055 | 1357 | // an array of objects for Beaver Builder (#449). |
| 1056 | 1358 | $tree = self::as_children($stored); |
| 1057 | 1359 | if (null !== $tree) { |
| 1360 | + $tree = self::unwrap_tree_envelope($tree); | |
| 1058 | 1361 | $text = self::text_from_tree($tree); |
| 1059 | 1362 | if (!self::is_blank($text)) { |
| 1060 | 1363 | return $text; |
| 1061 | 1364 | } |
| @@ -1065,8 +1368,48 @@ | ||
| 1065 | 1368 | return ''; |
| 1066 | 1369 | } |
| 1067 | 1370 | |
| 1068 | 1371 | /** |
| 1372 | + * The node tree inside a Breakdance / Oxygen 6 storage envelope. | |
| 1373 | + * | |
| 1374 | + * Neither builder stores its tree directly. The meta value is | |
| 1375 | + * `{"tree_json_string": "<the tree, JSON-encoded again>"}`, so one | |
| 1376 | + * json_decode() yields the envelope, not the tree. Walked as a tree, the | |
| 1377 | + * envelope is a single string leaf: kept whole as "content" when any | |
| 1378 | + * element held rich text (the encoded JSON then reached scoring, the | |
| 1379 | + * get-post-content ability and Markdown for AI), dropped when none did, | |
| 1380 | + * leaving the page empty (#905). | |
| 1381 | + * | |
| 1382 | + * An envelope whose inner string does not decode returns an empty tree, | |
| 1383 | + * never the string: handing the raw JSON back to the walker would bring | |
| 1384 | + * the JSON-as-content failure back on corrupt data. Anything that is not | |
| 1385 | + * an envelope is returned unchanged, so a bare tree still resolves. | |
| 1386 | + * | |
| 1387 | + * @since 2.15.0 | |
| 1388 | + * | |
| 1389 | + * @param array $decoded Decoded meta value. | |
| 1390 | + * @return array The node tree. | |
| 1391 | + */ | |
| 1392 | + private static function unwrap_tree_envelope(array $decoded): array { | |
| 1393 | + if (!array_key_exists('tree_json_string', $decoded)) { | |
| 1394 | + return $decoded; | |
| 1395 | + } | |
| 1396 | + | |
| 1397 | + $inner = is_string($decoded['tree_json_string']) | |
| 1398 | + ? json_decode($decoded['tree_json_string'], true) | |
| 1399 | + : $decoded['tree_json_string']; | |
| 1400 | + | |
| 1401 | + if (is_array($inner)) { | |
| 1402 | + return $inner; | |
| 1403 | + } | |
| 1404 | + | |
| 1405 | + // Re-serialised envelopes can carry the tree as an object. | |
| 1406 | + $inner = self::as_children($inner); | |
| 1407 | + | |
| 1408 | + return null !== $inner ? $inner : []; | |
| 1409 | + } | |
| 1410 | + | |
| 1411 | + /** | |
| 1069 | 1412 | * Whether a meta key is one of Oxygen classic's storage keys. |
| 1070 | 1413 | * |
| 1071 | 1414 | * @since 2.10.0 |
| 1072 | 1415 | * |
| @@ -1355,90 +1698,117 @@ | ||
| 1355 | 1698 | * |
| 1356 | 1699 | * Values are joined with block-level markup so downstream heading, link and |
| 1357 | 1700 | * image detection keeps working on the result. |
| 1358 | 1701 | * |
| 1702 | + * One depth-first walk, so the output follows the tree's own order, which | |
| 1703 | + * is the order the builders read here render in. This used to be two | |
| 1704 | + * passes over the whole tree, one for the reconstructed headings, links and | |
| 1705 | + * images and one for the remaining text, and the output followed pass | |
| 1706 | + * order: every heading and button on the page first, every paragraph after | |
| 1707 | + * them. That order became the meta description, og:description, the schema | |
| 1708 | + * description and Pro's Markdown for AI document (#907). | |
| 1709 | + * | |
| 1359 | 1710 | * @param array $tree Decoded builder tree. |
| 1360 | 1711 | * @return string Collected HTML. |
| 1361 | 1712 | */ |
| 1362 | 1713 | private static function text_from_tree(array $tree): string { |
| 1363 | - $collected = []; | |
| 1714 | + // Each entry is [value, is_markup], in tree order. | |
| 1715 | + $entries = []; | |
| 1364 | 1716 | |
| 1365 | 1717 | // Strings already represented inside reconstructed markup, so the plain |
| 1366 | - // sweep below doesn't emit a link label or heading a second time and | |
| 1367 | - // double it in the word count. | |
| 1718 | + // text doesn't emit a link label or heading a second time and double it | |
| 1719 | + // in the word count. Applied after the walk, against the whole tree: | |
| 1720 | + // a string folded into markup anywhere is dropped everywhere, exactly | |
| 1721 | + // as it was when the markup pass ran over the whole tree first. Checking | |
| 1722 | + // it during the walk instead would let a bare copy that appears before | |
| 1723 | + // its heading through. | |
| 1368 | 1724 | $consumed = []; |
| 1369 | 1725 | |
| 1370 | - // Pass 1 — rebuild <a>, <img> and <hN> from node *shape*. This has to | |
| 1371 | - // happen per node rather than per leaf: a link's label and its | |
| 1372 | - // destination are separate sibling fields, so once the tree is | |
| 1373 | - // flattened to leaves the pairing is gone. | |
| 1374 | - $reconstruct = static function ($node) use (&$reconstruct, &$collected, &$consumed): void { | |
| 1375 | - $node = self::as_children($node); | |
| 1376 | - if (null === $node) { | |
| 1726 | + // Markup is content wherever it appears; bare strings only count when | |
| 1727 | + // their key says they are content, so slugs and class names stay out of | |
| 1728 | + // the word count. | |
| 1729 | + $leaf = static function ($value, $key) use (&$entries): void { | |
| 1730 | + if (!is_string($value) || '' === trim($value)) { | |
| 1377 | 1731 | return; |
| 1378 | 1732 | } |
| 1379 | 1733 | |
| 1380 | - $markup = self::markup_for_node($node, $consumed); | |
| 1734 | + $is_content_key = is_string($key) | |
| 1735 | + && in_array(strtolower($key), self::CONTENT_KEYS, true); | |
| 1736 | + | |
| 1737 | + if ($is_content_key || strpos($value, '<') !== false) { | |
| 1738 | + $entries[] = [$value, false]; | |
| 1739 | + } | |
| 1740 | + }; | |
| 1741 | + | |
| 1742 | + // Text only, no reconstruction. Used for a `link` / `image` / video | |
| 1743 | + // sub-object: a destination descriptor the parent has already folded | |
| 1744 | + // into its markup. Rebuilding inside it would emit the same URL a second | |
| 1745 | + // time as a bare link and turn an image's own `url` field into a | |
| 1746 | + // spurious <a>, but any copy it carries still counts. | |
| 1747 | + $sweep = static function ($node, $key) use (&$sweep, $leaf): void { | |
| 1748 | + $children = self::as_children($node); | |
| 1749 | + if (null === $children) { | |
| 1750 | + $leaf($node, $key); | |
| 1751 | + return; | |
| 1752 | + } | |
| 1753 | + | |
| 1754 | + foreach ($children as $child_key => $child) { | |
| 1755 | + $sweep($child, is_string($child_key) ? $child_key : $key); | |
| 1756 | + } | |
| 1757 | + }; | |
| 1758 | + | |
| 1759 | + // Rebuild <a>, <img> and <hN> from node *shape*, then carry on through | |
| 1760 | + // the node's own fields in order. This has to happen per node rather | |
| 1761 | + // than per leaf: a link's label and its destination are separate | |
| 1762 | + // sibling fields, so once the tree is flattened to leaves the pairing | |
| 1763 | + // is gone. | |
| 1764 | + $walk = static function ($node, $key = null) use (&$walk, $sweep, $leaf, &$entries, &$consumed): void { | |
| 1765 | + $children = self::as_children($node); | |
| 1766 | + if (null === $children) { | |
| 1767 | + $leaf($node, $key); | |
| 1768 | + return; | |
| 1769 | + } | |
| 1770 | + | |
| 1771 | + $markup = self::markup_for_node($children, $consumed); | |
| 1381 | 1772 | if ('' !== $markup) { |
| 1382 | - $collected[] = $markup; | |
| 1773 | + $entries[] = [$markup, true]; | |
| 1383 | 1774 | } |
| 1384 | 1775 | |
| 1385 | - foreach ($node as $child_key => $child) { | |
| 1386 | - // A `link` / `image` sub-object is a destination descriptor the | |
| 1387 | - // parent has already folded into its markup. Descending into it | |
| 1388 | - // would emit the same URL a second time as a bare link, and | |
| 1389 | - // would turn an image's own `url` field into a spurious <a>. | |
| 1776 | + foreach ($children as $child_key => $child) { | |
| 1777 | + $next_key = is_string($child_key) ? $child_key : $key; | |
| 1778 | + | |
| 1390 | 1779 | if (is_string($child_key) |
| 1391 | 1780 | && (in_array(strtolower($child_key), self::URL_KEYS, true) |
| 1392 | 1781 | || in_array(strtolower($child_key), self::IMAGE_KEYS, true) |
| 1393 | 1782 | || in_array(strtolower($child_key), self::VIDEO_KEYS, true)) |
| 1394 | 1783 | ) { |
| 1784 | + $sweep($child, $next_key); | |
| 1395 | 1785 | continue; |
| 1396 | 1786 | } |
| 1397 | 1787 | |
| 1398 | - $reconstruct($child); | |
| 1788 | + $walk($child, $next_key); | |
| 1399 | 1789 | } |
| 1400 | 1790 | }; |
| 1401 | - $reconstruct($tree); | |
| 1402 | 1791 | |
| 1403 | - // Pass 2 — remaining visible text. | |
| 1404 | - $walk = static function ($node, $key = null) use (&$walk, &$collected, &$consumed): void { | |
| 1405 | - $children = self::as_children($node); | |
| 1406 | - if (null !== $children) { | |
| 1407 | - foreach ($children as $child_key => $child) { | |
| 1408 | - $walk($child, is_string($child_key) ? $child_key : $key); | |
| 1409 | - } | |
| 1410 | - return; | |
| 1411 | - } | |
| 1792 | + $walk($tree); | |
| 1412 | 1793 | |
| 1413 | - if (!is_string($node) || '' === trim($node)) { | |
| 1414 | - return; | |
| 1415 | - } | |
| 1416 | - | |
| 1794 | + $collected = []; | |
| 1795 | + foreach ($entries as [$value, $is_markup]) { | |
| 1417 | 1796 | // Already inside a reconstructed tag. |
| 1418 | - if (in_array($node, $consumed, true)) { | |
| 1419 | - return; | |
| 1797 | + if (!$is_markup && in_array($value, $consumed, true)) { | |
| 1798 | + continue; | |
| 1420 | 1799 | } |
| 1421 | 1800 | |
| 1422 | - $is_content_key = is_string($key) | |
| 1423 | - && in_array(strtolower($key), self::CONTENT_KEYS, true); | |
| 1801 | + $collected[] = $value; | |
| 1802 | + } | |
| 1424 | 1803 | |
| 1425 | - // Markup is content wherever it appears; bare strings only count | |
| 1426 | - // when their key says they are content, so slugs and class names | |
| 1427 | - // stay out of the word count. | |
| 1428 | - if ($is_content_key || strpos($node, '<') !== false) { | |
| 1429 | - $collected[] = $node; | |
| 1430 | - } | |
| 1431 | - }; | |
| 1432 | - | |
| 1433 | - $walk($tree); | |
| 1434 | - | |
| 1435 | 1804 | if (empty($collected)) { |
| 1436 | 1805 | return ''; |
| 1437 | 1806 | } |
| 1438 | 1807 | |
| 1439 | 1808 | // De-duplicate: builder trees often repeat a value across responsive |
| 1440 | - // breakpoints, which would otherwise multiply the word count. | |
| 1809 | + // breakpoints, which would otherwise multiply the word count. Keeps the | |
| 1810 | + // first occurrence, so a repeat never moves a value later in the page. | |
| 1441 | 1811 | $collected = array_unique($collected); |
| 1442 | 1812 | |
| 1443 | 1813 | return implode("\n", $collected); |
| 1444 | 1814 | } |