22-63ms across five runs. * * How: a buffer pass over the final HTML finds top-level section * containers by class (Elementor, Divi, Bricks, Oxygen, Beaver Builder), * and when no class matches falls back to the direct children of
* — so any theme or builder gets the treatment. It leaves the first N * alone (the above-fold estimate) and stamps the rest with a * `data-xspeed-turbo` attribute. One * inline '; $head_end = stripos( $html, '' ); return substr_replace( $html, $style, (int) $head_end, 0 ); } /** @return string[] */ private function section_classes(): array { $classes = $this->get_setting( 'section_classes', self::DEFAULT_CLASSES ); $classes = is_array( $classes ) ? array_values( array_filter( array_map( 'strval', $classes ) ) ) : self::DEFAULT_CLASSES; /** * Filter the class names identifying a top-level page section. * * @param string[] $classes */ return self::sanitize_classes( (array) apply_filters( 'xspeed_turbo_render_classes', $classes ) ); } /** @return string[] */ private function excluded_classes(): array { $classes = $this->get_setting( 'excluded_classes', array() ); $classes = is_array( $classes ) ? $classes : array(); /** * Filter the class names whose sections are never stamped. * * @param string[] $classes */ return self::sanitize_classes( (array) apply_filters( 'xspeed_turbo_render_excluded_classes', $classes ) ); } /** * The exclusion regex for an open tag, or '' when nothing is excluded. * Token-bounded, unlike the section scan: an exclusion is a user-typed * remedy, and "card" silently matching "cardigan-grid" would make it * look like the setting does nothing. */ private function excluded_pattern(): string { $classes = $this->excluded_classes(); if ( empty( $classes ) ) { return ''; } return '#\bclass\s*=\s*(["\'])[^"\']*(? (bool) preg_match( '/^[A-Za-z0-9_-]+$/', $c ) ) ); } /** * Structural fallback: the byte offsets of
's section children. * * Themes love wrapper chains — Kadence renders
> a hero *
+ an empty notices
+ one wrapper
holding * everything else four levels deep. Two rules recover the real * sections: drop tiny children (decoration, not layout), and while the * list is too short to stamp anything, unwrap — in place — a child * that dominates its siblings by byte share. Only a
/
* unwraps, and a dominant FIRST child only when it is the sole child: * wrappers in the wild are divs, but so are many heroes, and opening a * hero would stamp above-fold content. The cap only guards against * pathological markup. * * @param string $masked Markup with script/textarea/noscript/comments nulled. * @param int $skip The above-fold allowance in effect. * @return int[] */ private static function main_children( string $masked, int $skip ): array { if ( ! preg_match( '#]*>#i', $masked, $open, PREG_OFFSET_CAPTURE ) ) { return array(); } $children = self::element_children( $masked, (int) $open[0][1] + strlen( (string) $open[0][0] ) ); for ( $level = 0; $level < 10; $level++ ) { $children = array_values( array_filter( $children, static fn( array $c ): bool => ( $c['end'] - $c['start'] ) >= self::FALLBACK_MIN_SPAN ) ); if ( count( $children ) > $skip || empty( $children ) ) { break; } $total = array_sum( array_map( static fn( array $c ): int => $c['end'] - $c['start'], $children ) ); $unwrapped = false; foreach ( $children as $i => $child ) { if ( ! in_array( $child['tag'], array( 'div', 'article' ), true ) ) { continue; } if ( 0 === $i && count( $children ) > 1 ) { continue; // A dominant first child among siblings is a hero, not a wrapper. } if ( ( $child['end'] - $child['start'] ) < self::FALLBACK_DOMINANT * $total ) { continue; } $end = self::tag_end( $masked, $child['start'] ); if ( null === $end ) { break 2; } $inner = self::element_children( $masked, $end ); if ( empty( $inner ) ) { break 2; } array_splice( $children, $i, 1, $inner ); $unwrapped = true; break; } if ( ! $unwrapped ) { break; } } return array_map( static fn( array $c ): int => $c['start'], $children ); } /** * Byte spans of an element's direct section-shaped children. * * A tag walk with an open-tag stack, not a depth counter, so the HTML5 * that classic themes actually emit doesn't desync it: implicitly * closed tags (
  • a
  • b, an unclosed

    ) are popped by the matching * ancestor close, a stray close of an implicit tag is ignored, and a * trailing slash on a non-void tag is meaningless (HTML5) so

    is * an OPEN div. A close tag for anything not on the stack ends the walk * — normally the container's own close. Only section-shaped tags count * as children: stamping a stray

    or

    would put an 800px * intrinsic-size estimate on a one-line element and wreck the * scrollbar. * * @param string $masked Markup with script/textarea/noscript/comments nulled. * @param int $cursor Byte offset just past the container's open tag. * @return array */ private static function element_children( string $masked, int $cursor ): array { static $void = array( 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr' ); static $want = array( 'div', 'section', 'article', 'footer', 'aside', 'figure', 'table', 'ul', 'ol' ); static $implicit = array( 'p', 'li', 'dt', 'dd', 'td', 'th', 'tr', 'option', 'optgroup' ); $children = array(); $pending = null; $stack = array(); while ( preg_match( '#<(/?)([a-zA-Z][a-zA-Z0-9-]*)((?:"[^"]*"|\'[^\']*\'|[^>"\'])*)>#', $masked, $t, PREG_OFFSET_CAPTURE, $cursor ) ) { $offset = (int) $t[0][1]; $cursor = $offset + strlen( (string) $t[0][0] ); $closing = '' !== $t[1][0]; $tag = strtolower( (string) $t[2][0] ); if ( in_array( $tag, $void, true ) ) { continue; } if ( ! $closing ) { if ( in_array( $tag, $implicit, true ) && end( $stack ) === $tag ) { array_pop( $stack ); // A sibling
  • /

    / implicitly closes the previous one. } if ( empty( $stack ) && in_array( $tag, $want, true ) ) { $pending = array( 'start' => $offset, 'end' => $offset, 'tag' => $tag, ); } $stack[] = $tag; continue; } if ( ! in_array( $tag, $stack, true ) ) { if ( in_array( $tag, $implicit, true ) ) { continue; // Stray

    -style close: harmless, skip it. } break; // The container's own close tag: the walk is done. } while ( ! empty( $stack ) && array_pop( $stack ) !== $tag ) { continue; // Unclosed implicit tags between here and the match. } if ( empty( $stack ) && null !== $pending ) { $pending['end'] = $cursor; $children[] = $pending; $pending = null; } } return $children; } /** * The offset just past an element's close tag — the same tag walk as * element_children(), seeded with the element's own tag, so implicitly * closed tags don't desync it. Null when the element never closes (or * the markup is too broken to tell), in which case the caller keeps * the old behavior rather than guessing at a span. * * @param string $masked Markup with script/textarea/noscript/comments nulled. * @param int $offset Byte offset of the element's `<`. * @return int|null */ private static function element_end( string $masked, int $offset ): ?int { static $void = array( 'area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr' ); static $implicit = array( 'p', 'li', 'dt', 'dd', 'td', 'th', 'tr', 'option', 'optgroup' ); if ( ! preg_match( '#\G<([a-zA-Z][a-zA-Z0-9-]*)#', $masked, $open, 0, $offset ) ) { return null; } $cursor = self::tag_end( $masked, $offset ); if ( null === $cursor ) { return null; } $stack = array( strtolower( (string) $open[1] ) ); while ( preg_match( '#<(/?)([a-zA-Z][a-zA-Z0-9-]*)((?:"[^"]*"|\'[^\']*\'|[^>"\'])*)>#', $masked, $t, PREG_OFFSET_CAPTURE, $cursor ) ) { $cursor = (int) $t[0][1] + strlen( (string) $t[0][0] ); $closing = '' !== $t[1][0]; $tag = strtolower( (string) $t[2][0] ); if ( in_array( $tag, $void, true ) ) { continue; } if ( ! $closing ) { if ( in_array( $tag, $implicit, true ) && end( $stack ) === $tag ) { array_pop( $stack ); } $stack[] = $tag; continue; } if ( ! in_array( $tag, $stack, true ) ) { if ( in_array( $tag, $implicit, true ) ) { continue; // Stray

    -style close: harmless, skip it. } return null; // A close for an ancestor: the element never closed. } while ( ! empty( $stack ) && array_pop( $stack ) !== $tag ) { continue; } if ( empty( $stack ) ) { return $cursor; } } return null; } /** * The offset just past an open tag's `>`, quote-aware — a raw strpos * would stop at a `>` inside an attribute value. * * @param string $masked Markup with script/textarea/noscript/comments nulled. * @param int $offset Byte offset of the tag's `<`. * @return int|null Null when the tag never terminates. */ private static function tag_end( string $masked, int $offset ): ?int { if ( ! preg_match( '#\G<(/?)[a-zA-Z][a-zA-Z0-9-]*(?:"[^"]*"|\'[^\']*\'|[^>"\'])*>#', $masked, $m, 0, $offset ) ) { return null; } return $offset + strlen( $m[0] ); } private static function mask( string $html ): string { return (string) preg_replace_callback( '#' . self::MASKED_SPANS . '#is', static fn( array $m ): string => str_repeat( "\0", strlen( $m[0] ) ), $html ); } public function cli_commands(): array { return array( array( 'name' => 'xspeed turbo-render status', 'callback' => array( $this, 'cli_status' ), 'shortdesc' => 'Show Turbo Render status.', 'ai_hint' => 'Is Turbo Render (content-visibility stamping of below-fold sections) on, and how many sections render immediately? Use when diagnosing TBT / main-thread style & layout cost on long builder pages.', 'synopsis' => array(), ), ); } /** * `wp xspeed turbo-render status`. * * @param array $args Positional args (unused). * @param array $assoc Associative args (unused). */ public function cli_status( array $args, array $assoc ): void { unset( $args, $assoc ); $enabled = (bool) $this->get_setting( 'enabled', false ); \WP_CLI::log( 'Turbo Render: ' . ( $enabled ? 'enabled' : 'disabled' ) ); \WP_CLI::log( 'Immediate sections: first ' . (int) $this->get_setting( 'skip_first', self::DEFAULT_SKIP_FIRST ) . ' sections' ); \WP_CLI::log( 'Section classes: ' . implode( ', ', $this->section_classes() ) ); $excluded = $this->excluded_classes(); \WP_CLI::log( 'Excluded classes: ' . ( empty( $excluded ) ? '(none)' : implode( ', ', $excluded ) ) ); } }