match_heading( $node, $pattern ) ) { $output[] = (string) $node['html']; ++$index; continue; } $end = $index + 1; while ( $end < $count && ! $this->is_section_break( $nodes[ $end ] ) ) { ++$end; } $inner = array(); for ( $i = $index; $i < $end; $i++ ) { $inner[] = (string) $nodes[ $i ]['html']; } $output[] = $this->wrap_block( $block, implode( "\n", $inner ) ); $did = true; $index = $end; } if ( ! $did ) { return $body_html; } return implode( "\n\n", $output ); } /** * @param array{tag: string, level: int, html: string, text: string} $node Node. */ private function match_heading( array $node, string $pattern ): bool { if ( 2 !== (int) $node['level'] ) { return false; } if ( Html_Body_Parser::heading_skips_wrap( $node ) ) { return false; } $text = trim( (string) $node['text'] ); if ( '' === $text ) { return false; } $regex = '/' . str_replace( '/', '\/', $pattern ) . '/iu'; return 1 === preg_match( $regex, $text ); } /** * @param array{tag: string, level: int, html: string, text: string} $node Node. */ private function is_section_break( array $node ): bool { $level = (int) $node['level']; return $level > 0 && $level <= 2; } private function wrap_block( string $block, string $inner ): string { $class = str_replace( '/', '-', $block ); $inner = trim( $inner ); return sprintf( "\n
\n%3\$s\n
\n", $block, esc_attr( $class ), $inner ); } }