'heading', 'core/paragraph' => 'paragraph', 'core/image' => 'image', 'core/list' => 'list', ); /** * Converts a parsed block tree into a flat, depth-first ContentUnit * sequence. Null-blockName "freeform HTML between blocks" pseudo-entries * are skipped — matching ABDS_SEO_Block_Recommendations' existing * flatten() behavior exactly, since the per-element rules this feeds * never considered them either. * * @param array $blocks parse_blocks() output. * @return array */ public static function units_from_blocks( array $blocks ): array { return array_map( array( __CLASS__, 'unit_from_entry' ), self::flatten( $blocks ) ); } /** * Depth-first-flattens a block tree, skipping the null-blockName * "freeform HTML between blocks" pseudo-entries parse_blocks() inserts * (side effects of parsing serialized HTML — no editor-side equivalent * exists to line up with, and the per-element rules never considered * them). No positional path is tracked — see ABDS_SEO_Content_Ref for * why ref is a content hash instead. * * @param array $blocks Blocks at the current level. * @return array */ protected static function flatten( array $blocks ): array { $flat = array(); foreach ( $blocks as $block ) { if ( empty( $block['blockName'] ) ) { continue; } $flat[] = $block; if ( ! empty( $block['innerBlocks'] ) ) { $flat = array_merge( $flat, self::flatten( $block['innerBlocks'] ) ); } } return $flat; } /** * Builds one ContentUnit from a flattened block. * * @param array $block A single parse_blocks() entry (real block, i.e. non-null blockName). * @return array{kind:string, level:?int, html:string, text:string, ref:string, source_type:string} */ protected static function unit_from_entry( array $block ): array { $block_name = (string) ( $block['blockName'] ?? '' ); $html = (string) ( $block['innerHTML'] ?? '' ); $kind = self::KIND_MAP[ $block_name ] ?? 'other'; $text = self::strip_html( $html ); return array( 'kind' => $kind, 'level' => 'heading' === $kind ? (int) ( $block['attrs']['level'] ?? 2 ) : null, 'html' => $html, 'text' => $text, 'ref' => ABDS_SEO_Content_Ref::of( $text ), 'source_type' => $block_name, ); } /** * Strips HTML down to plain, whitespace-normalized text. Duplicated * from ABDS_SEO_Block_Recommendations/ABDS_SEO_Keyword_Extractor * rather than shared, matching the existing pattern where each * pure-logic class carries its own copy (tags become a space, not * nothing, so adjacent text doesn't glue together). * * @param string $text Text that may contain HTML. * @return string Plain text, tags replaced with spaces, whitespace collapsed and trimmed. */ protected static function strip_html( string $text ): string { $text = (string) preg_replace( '@<(script|style)[^>]*?>.*?@si', ' ', $text ); $text = (string) preg_replace( '/<[^>]*>/', ' ', $text ); return trim( (string) preg_replace( '/\s+/u', ' ', $text ) ); } }