(string) $key_or_id, 'slug' => (string) $key_or_id ) ); $post = $found ? $found['post'] : null; } if ( ! $post instanceof WP_Post || 'page' !== $post->post_type ) { return new WP_Error( 'aieo_content_no_page', sprintf( 'No page for %s.', (string) $key_or_id ) ); } if ( ! current_user_can( 'edit_post', (int) $post->ID ) ) { return new WP_Error( 'aieo_content_forbidden', sprintf( 'You may not edit the page %d.', (int) $post->ID ), array( 'status' => 403 ) ); } $recipe = (string) get_post_meta( $post->ID, AIEO_Content::META_RECIPE, true ); $key = 0 === strpos( $recipe, AIEO_Content::RECIPE_PREFIX ) ? substr( $recipe, strlen( AIEO_Content::RECIPE_PREFIX ) ) : (string) $post->post_name; $media = array(); $spec = array( '$schema' => AIEO_Content::SCHEMA, 'key' => $key, 'slug' => (string) $post->post_name, 'title' => (string) $post->post_title, ); $template = (string) get_post_meta( $post->ID, '_wp_page_template', true ); if ( '' !== $template && 'default' !== $template ) { $spec['template'] = $template; } $spec['status'] = (string) $post->post_status; $accent = (string) get_post_meta( $post->ID, AIEO_Content::META_ACCENT, true ); if ( '' !== $accent ) { $spec['accent'] = $accent; } $plugin_pages = get_post_meta( $post->ID, AIEO_Content::META_PLUGIN_PAGES, true ); if ( is_array( $plugin_pages ) && $plugin_pages ) { $spec['plugin_pages'] = array_values( $plugin_pages ); } if ( '' !== (string) $post->post_excerpt ) { $spec['seo'] = array( 'description' => (string) $post->post_excerpt ); } if ( 'page' === get_option( 'show_on_front' ) && (int) get_option( 'page_on_front' ) === (int) $post->ID ) { $spec['front_page'] = true; } $spec['media'] = $media; $spec['sections'] = self::sections( (string) $post->post_content, $media ); $spec['media'] = $media; $display = self::display_of( parse_blocks( (string) $post->post_content ) ); if ( '' !== $display ) { $out = array(); foreach ( $spec as $k => $v ) { $out[ $k ] = $v; if ( 'status' === $k ) { $out['display'] = $display; } } $spec = $out; } return $spec; } private static function display_of( array $blocks ) { $found = ''; $walk = static function ( array $list ) use ( &$walk, &$found ) { foreach ( $list as $b ) { $name = (string) ( $b['blockName'] ?? '' ); $size = (string) ( $b['attrs']['style']['typography']['fontSize'] ?? '' ); if ( 'core/group' === $name && preg_match( '/\brooster-r1\b/', (string) ( $b['attrs']['className'] ?? '' ) ) ) { $found = 'rooster-one'; return; } if ( in_array( self::shape( $b ), self::PROMO_BLOCKS, true ) || ( 'core/heading' === $name && AIEO_Content_Promo::H1['fontSize'] === $size ) ) { $found = 'promo'; return; } if ( 'core/heading' === $name && in_array( $size, array( AIEO_Content_Sections::DISPLAY_H1['fontSize'], AIEO_Content_Sections::DISPLAY_H2['fontSize'] ), true ) ) { $found = 'home'; } if ( ! empty( $b['innerBlocks'] ) ) { $walk( (array) $b['innerBlocks'] ); if ( 'promo' === $found ) { return; } } } }; $walk( $blocks ); return $found; } const PROMO_BLOCKS = array( 'rooster/mark:crumb', 'rooster/list:facts', 'rooster/media:frame', 'rooster/card:grid', 'rooster/list:checks', 'rooster/mark:note', 'rooster/table:data', 'rooster/plan:plan', 'rooster/action:panel', 'rooster/questions:rows', 'rooster/card:link', 'rooster/card:detail' ); private static function shape( array $block ) { $name = (string) ( $block['blockName'] ?? '' ); $lib = 0 === strpos( $name, 'rooster/' ) ? substr( $name, 8 ) : ''; if ( '' === $lib || ! class_exists( 'AIEO_Engine_Blocks', false ) || ! isset( AIEO_Engine_Blocks::LIBRARY[ $lib ] ) ) { return $name; } $variant = (string) ( $block['attrs']['variant'] ?? '' ); return $name . ':' . ( isset( AIEO_Engine_Blocks::LIBRARY[ $lib ][ $variant ] ) ? $variant : (string) array_key_first( AIEO_Engine_Blocks::LIBRARY[ $lib ] ) ); } private static function rows( $value, $limit = PHP_INT_MAX ) { $out = array(); foreach ( preg_split( '/\r\n|\r|\n/', (string) $value ) ?: array() as $line ) { if ( '' !== trim( $line ) ) { $out[] = array_map( 'trim', explode( '|', $line, $limit ) ); } } return $out; } private static function promo( array $block, $kind, array &$media ) { if ( ! array_intersect( self::names( $block ), self::PROMO_BLOCKS ) ) { return null; } $get = fn ( $name ) => self::find( $block, static fn ( $b ) => $name === self::shape( $b ) ); $attrs = fn ( $found ) => $found ? self::attrs( $found ) : array(); $s = array( 'kind' => $kind, '_promo' => true ); $anch = (string) ( $block['attrs']['anchor'] ?? '' ); if ( '' !== $anch && $anch !== (string) ( $block['attrs']['metadata']['rooster']['key'] ?? '' ) ) { $s['anchor'] = $anch; } if ( preg_match( '/\bis-style-section-subtle\b/', (string) ( $block['attrs']['className'] ?? '' ) ) ) { $s['style'] = 'subtle'; } $eyebrow = self::find( $block, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-eyebrow' ), 6 ); if ( $eyebrow ) { $s['eyebrow'] = self::text( $eyebrow ); } $heading = self::find( $block, static fn ( $b ) => 'core/heading' === $b['blockName'], 6 ); if ( $heading ) { $s['heading'] = self::heading_text( $heading ); if ( 'hero' !== $kind && 2 !== (int) ( $heading['attrs']['level'] ?? 2 ) ) { $s['level'] = (int) $heading['attrs']['level']; } } $lede = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && __( 'Lede', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ), 6 ); if ( $lede ) { $s['lede'] = implode( "\n\n", array_map( static fn ( $b ) => self::text( $b ), (array) $lede['innerBlocks'] ) ); } switch ( $kind ) { case 'hero': $buttons = self::buttons( $block ); foreach ( $buttons as $i => $button ) { if ( 'arrow-right' === ( $button['icon'] ?? '' ) ) { unset( $buttons[ $i ]['icon'] ); } } if ( $buttons ) { $s['buttons'] = $buttons; } $frame = $attrs( $get( 'rooster/media:frame' ) ); if ( ! empty( $frame['desktopId'] ) ) { $image = self::responsive_image( array( 'blockName' => 'rooster/responsive-image', 'attrs' => array_intersect_key( $frame, array_flip( array( 'desktopId', 'mobileId', 'desktopAlt' ) ) ) ), $media ); if ( '' === (string) ( $frame['desktopAlt'] ?? '' ) ) { unset( $image['alt'] ); } $s['image'] = $image; } if ( ! empty( $frame['motif'] ) ) { $s['motif'] = (string) $frame['motif']; } $facts = $attrs( $get( 'rooster/list:facts' ) ); if ( ! empty( $facts['items'] ) ) { $s['_strip'] = array_map( static fn ( $r ) => array( 'value' => $r[0], 'label' => (string) ( $r[1] ?? '' ) ), self::rows( $facts['items'], 2 ) ); } break; case 'cards': if ( $get( 'rooster/card:link' ) ) { $s['layout'] = 'row'; unset( $s['eyebrow'] ); $s['cards'] = array_map( static fn ( $r ) => array( 'title' => $r[0], 'text' => (string) ( $r[1] ?? '' ), 'href' => (string) ( $r[2] ?? '' ) ), self::rows( $attrs( $get( 'rooster/card:link' ) )['items'] ?? '' ) ); } elseif ( $get( 'rooster/card:detail' ) ) { $a = $attrs( $get( 'rooster/card:detail' ) ); $s['collapsible'] = true; if ( '' !== (string) ( $a['unit'] ?? '' ) ) { $s['unit'] = (string) $a['unit']; } $cards = array(); foreach ( self::rows( $a['items'] ?? '' ) as $r ) { if ( str_starts_with( $r[0], '#' ) ) { $cards[] = array( 'icon' => (string) ( $r[1] ?? '' ), 'title' => trim( substr( $r[0], 1 ) ), 'items' => array() ); } elseif ( $cards ) { $cards[ count( $cards ) - 1 ]['items'][] = array( 'title' => $r[0], 'text' => implode( ' | ', array_slice( $r, 1 ) ) ); } } $s['cards'] = $cards; } else { $a = $attrs( $get( 'rooster/card:grid' ) ); $steps = 'steps' === (string) ( $a['layout'] ?? '' ); if ( $steps ) { $s['numbered'] = true; } elseif ( in_array( (int) ( $a['columns'] ?? 3 ), array( 2, 4 ), true ) ) { $s['columns'] = (int) $a['columns']; } $s['cards'] = array_map( static fn ( $r ) => $steps ? array( 'title' => $r[0], 'text' => (string) ( $r[1] ?? '' ) ) : array( 'icon' => $r[0], 'title' => (string) ( $r[1] ?? '' ), 'text' => (string) ( $r[2] ?? '' ) ), self::rows( $a['items'] ?? '', $steps ? 2 : 3 ) ); } break; case 'split': $s['items'] = array_map( static fn ( $r ) => implode( ' | ', $r ), self::rows( $attrs( $get( 'rooster/list:checks' ) )['items'] ?? '' ) ); $note = $attrs( $get( 'rooster/mark:note' ) ); if ( '' !== (string) ( $note['text'] ?? '' ) ) { $s['aside'] = (string) $note['text']; } break; case 'prose': $s['body'] = implode( "\n", array_map( static fn ( $r ) => '- ' . implode( ' | ', $r ), self::rows( $attrs( $get( 'rooster/list:checks' ) )['items'] ?? '' ) ) ); break; case 'table': $a = $attrs( $get( 'rooster/table:data' ) ); $s['columns'] = array_map( 'trim', explode( '|', (string) ( $a['columns'] ?? '' ) ) ); $s['rows'] = self::rows( $a['rows'] ?? '' ); break; case 'pricing': $tiers = array(); foreach ( self::find_all( $block, static fn ( $b ) => 'rooster/plan:plan' === self::shape( $b ) ) as $card ) { $a = self::attrs( $card ); $tier = array( 'name' => (string) $a['name'], 'tagline' => (string) ( $a['tagline'] ?? '' ), 'price' => (string) $a['price'], 'period' => (string) $a['period'], 'note' => (string) $a['note'], 'features' => array_map( static fn ( $r ) => implode( ' | ', $r ), self::rows( $a['features'] ) ) ); if ( ! empty( $a['highlighted'] ) ) { $tier['highlighted'] = true; } if ( '' !== (string) $a['badge'] ) { $tier['badge'] = (string) $a['badge']; } $tier['ctaText'] = (string) $a['ctaText']; $tier['ctaHref'] = (string) $a['ctaHref']; if ( ! empty( $card['attrs']['anchor'] ) ) { $tier['anchor'] = (string) $card['attrs']['anchor']; } $tiers[] = array_filter( $tier, static fn ( $v ) => '' !== $v && array() !== $v ); } $s['tiers'] = $tiers; $panel = self::find( $block, static fn ( $b ) => 'rooster/list:checks' === self::shape( $b ) && 'panel' === ( $b['attrs']['layout'] ?? '' ) ); if ( $panel ) { $a = self::attrs( $panel ); $s['includes'] = array( 'heading' => (string) $a['heading'], 'items' => array_map( static fn ( $r ) => implode( ' | ', $r ), self::rows( $a['items'] ) ) ); } $note = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && __( 'Note', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ), 2 ); if ( $note ) { $s['note'] = implode( "\n\n", array_map( static fn ( $b ) => self::text( $b ), (array) $note['innerBlocks'] ) ); } break; case 'faq': $s['items'] = array_map( static fn ( $r ) => array( 'q' => $r[0], 'a' => (string) ( $r[1] ?? '' ) ), self::rows( $attrs( $get( 'rooster/questions:rows' ) )['items'] ?? '', 2 ) ); $links = self::find( $block, static fn ( $b ) => 'rooster/list:checks' === self::shape( $b ) && 'links' === ( $b['attrs']['layout'] ?? '' ) ); if ( $links ) { $items = array_map( static fn ( $r ) => array( 'text' => $r[0], 'href' => (string) ( $r[1] ?? '' ) ), self::rows( self::attrs( $links )['items'], 2 ) ); $intro = self::find( $block, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && empty( $b['attrs']['className'] ), 4 ); $s['links'] = $intro ? array( 'heading' => self::text( $intro ), 'items' => $items ) : $items; } break; case 'cta': $a = $attrs( $get( 'rooster/action:panel' ) ); foreach ( array( 'eyebrow', 'heading', 'text' ) as $field ) { if ( '' !== (string) ( $a[ $field ] ?? '' ) ) { $s[ $field ] = (string) $a[ $field ]; } } if ( '' !== (string) ( $a['items'] ?? '' ) ) { $s['items'] = array_map( static fn ( $r ) => implode( ' | ', $r ), self::rows( $a['items'] ) ); } $s['buttons'] = array_map( static fn ( $r ) => array( 'text' => $r[0], 'href' => (string) ( $r[1] ?? '' ), 'style' => 'secondary' === strtolower( (string) ( $r[2] ?? '' ) ) ? 'secondary' : 'primary' ) + ( '' !== (string) ( $r[3] ?? '' ) ? array( 'icon' => (string) $r[3] ) : array() ), self::rows( $a['buttons'] ?? '' ) ); if ( 'center' !== (string) ( $a['layout'] ?? 'band' ) ) { $s['align'] = 'left'; } break; } return $s; } private static function padding( array $block, $kind, array $section, $default = null ) { $style = (array) ( $block['attrs']['style'] ?? array() ); $read = static function ( $value ) { $value = (string) $value; if ( preg_match( '/^var:preset\|spacing\|(\d+)$/', $value, $m ) ) { return (int) $m[1]; } return '0' === $value ? 0 : $value; }; if ( is_array( $default ) ) { } elseif ( 'hero' === $kind ) { $default = array( 'top' => 7, 'bottom' => 9, 'mobile' => array( 'bottom' => 8 ) ); } elseif ( 'cta' === $kind && 'card' === ( $section['style'] ?? '' ) ) { $default = array( 'top' => 9, 'bottom' => 9, 'mobile' => array( 'top' => 8, 'bottom' => 8 ) ); } else { $pad = (int) ( AIEO_Content_Sections::PADDING[ $kind ] ?? 9 ); $default = array( 'top' => $pad, 'bottom' => $pad ) + ( in_array( $kind, AIEO_Content_Sections::MOBILE_PADDING, true ) ? array( 'mobile' => array( 'top' => 8, 'bottom' => 8 ) ) : array() ); } $found = array(); foreach ( array( 'top', 'bottom' ) as $edge ) { if ( isset( $style['spacing']['padding'][ $edge ] ) ) { $found[ $edge ] = $read( $style['spacing']['padding'][ $edge ] ); } } foreach ( array( 'top', 'bottom' ) as $edge ) { if ( isset( $style['@mobile']['spacing']['padding'][ $edge ] ) ) { $found['mobile'][ $edge ] = $read( $style['@mobile']['spacing']['padding'][ $edge ] ); } } if ( $found == $default ) { return array(); } return $found; } public static function sections( $content, array &$media ) { return self::read_sections( parse_blocks( (string) $content ), $media ); } private static function read_sections( array $blocks, array &$media, $panel = null ) { $out = array(); $added = 0; $blocks = array_values( array_filter( $blocks, static fn ( $b ) => is_array( $b ) && ! empty( $b['blockName'] ) ) ); if ( null !== $panel ) { $flat = array(); foreach ( $blocks as $b ) { if ( 'core/group' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), AIEO_Content_Tabs::DEFERRED ) ) { foreach ( (array) ( $b['innerBlocks'] ?? array() ) as $inner ) { if ( is_array( $inner ) && ! empty( $inner['blockName'] ) ) { $flat[] = array( '_whole' => true ) + $inner; } } continue; } $flat[] = $b; } $blocks = $flat; } foreach ( $blocks as $at => $whole ) { $holds = false; $block = self::without_tabs( $whole, $holds ); $meta = (array) ( $block['attrs']['metadata']['rooster'] ?? array() ); $kind = (string) ( $meta['kind'] ?? self::guess_kind( $block ) ); $key = (string) ( $meta['key'] ?? '' ); if ( '' === $key ) { $key = '' !== $kind ? $kind . '-' . ( ++$added ) : 'added-' . ( ++$added ); } $layout = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-section__blocks' ), 2 ); $section = $layout || isset( $meta['blocks'] ) ? self::read_layout( $block, $layout, $kind, $media ) : null; $section = null === $section && '' !== $kind ? self::rooster_one( $block, $kind, $media ) : $section; $section = null === $section && '' !== $kind ? self::promo( $block, $kind, $media ) : $section; if ( null === $section && '' !== $kind && method_exists( __CLASS__, 'read_' . $kind ) ) { $method = 'read_' . $kind; $section = self::$method( $block, $media ); } if ( null === $section ) { $section = array( 'kind' => 'prose', 'markup' => AIEO_Content_Blocks::serialize( $whole ) ); } elseif ( isset( $meta['kind'] ) && empty( $section['_promo'] ) ) { $padding = self::padding( $block, $kind, $section, null !== $panel ? AIEO_Content_Tabs::padding( count( $blocks ) - 1 === $at ) : null ); if ( $padding ) { $section['padding'] = $padding; } } if ( ! empty( $whole['_whole'] ) || ( ! isset( $section['defer'] ) && self::find( $block, static fn ( $b ) => ! empty( $b['attrs']['roosterDefer']['enabled'] ), 4 ) ) ) { $section['defer'] = true; } $default = null !== $panel ? AIEO_Content_Tabs::defer_default( (string) $section['kind'], ! empty( $panel['first'] ) ) : AIEO_Content_Sections::defer_default( (string) $section['kind'], count( $out ) ); if ( $holds || ! empty( $section['defer'] ) === $default ) { unset( $section['defer'] ); } else { $section['defer'] = ! $default; } $strip = $section['_strip'] ?? null; $parts = (array) ( $section['_parts'] ?? array() ); unset( $section['_strip'], $section['_promo'], $section['_parts'] ); $out[] = array( 'key' => $key ) + $section; foreach ( $parts as $part ) { $out[] = array( 'key' => (string) $part['key'], 'kind' => (string) $part['kind'] ) + array_diff_key( $part, array( 'key' => 1, 'kind' => 1 ) ); } if ( is_array( $strip ) ) { $taken = array_column( $out, 'key' ); $out[] = array( 'key' => in_array( 'stats', $taken, true ) ? $key . '-stats' : 'stats', 'kind' => 'stats', 'headless' => true, 'items' => $strip ); } } return $out; } private static function rooster_one( array $block, $kind, array &$media ) { if ( ! self::has_class( $block, 'rooster-r1', false ) ) { return null; } $get = fn ( $shape ) => self::find( $block, static fn ( $b ) => $shape === self::shape( $b ) ); $all = fn ( $shape ) => self::find_all( $block, static fn ( $b ) => $shape === self::shape( $b ) ); $rows = static fn ( $value, $limit = PHP_INT_MAX ) => self::rows( (string) $value, $limit ); $s = array( 'kind' => $kind, '_promo' => true ); $meta = (array) ( $block['attrs']['metadata']['rooster'] ?? array() ); $anch = (string) ( $block['attrs']['anchor'] ?? '' ); if ( '' !== $anch && $anch !== (string) ( $meta['key'] ?? '' ) ) { $s['anchor'] = $anch; } $head = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && 'rooster-section__head' === (string) ( $b['attrs']['className'] ?? '' ), 1 ); if ( $head ) { $ledes = array(); foreach ( (array) $head['innerBlocks'] as $b ) { if ( 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-eyebrow' ) ) { $s['eyebrow'] = self::text( $b ); } elseif ( 'core/heading' === $b['blockName'] ) { $s['heading'] = self::heading_text( $b ); if ( 2 !== (int) ( $b['attrs']['level'] ?? 2 ) ) { $s['level'] = (int) $b['attrs']['level']; } } elseif ( 'core/paragraph' === $b['blockName'] ) { $ledes[] = self::text( $b ); } } if ( $ledes ) { $s['lede'] = implode( "\n\n", $ledes ); } } $note = $get( 'rooster/mark:note' ); $notes = $note ? (string) ( $note['attrs']['text'] ?? '' ) : ''; $parts = array(); foreach ( (array) ( $meta['parts'] ?? array() ) as $part ) { $parts[] = array_filter( array( 'key' => (string) ( $part['key'] ?? '' ), 'kind' => (string) ( $part['kind'] ?? '' ), 'heading' => (string) ( $part['heading'] ?? '' ), 'level' => (int) ( $part['level'] ?? 0 ) ) ) + array( 'headless' => true ); } $part_of = static function ( $part_kind ) use ( &$parts ) { foreach ( $parts as $i => $part ) { if ( $part_kind === ( $part['kind'] ?? '' ) ) { return $i; } } return null; }; $tone_of = static fn ( $tone, $badge ) => $tone === ( AIEO_Content_Rooster_One::BADGE_TONES[ strtolower( trim( (string) $badge ) ) ] ?? 'one' ) ? '' : $tone; $buttons = static fn ( $lines ) => self::r1_buttons( (string) $lines ); $figures = function ( array $list ) use ( &$media ) { $items = array(); foreach ( $list as $b ) { $a = (array) $b['attrs']; $id = (int) ( $a['desktopId'] ?? 0 ); $path = $id ? AIEO_Content_Media::key_of( $id ) : ''; $path = '' !== $path ? $path : ( $id ? (string) wp_get_attachment_url( $id ) : '' ); $alt = (string) ( $a['desktopAlt'] ?? '' ); if ( '' !== $path ) { $media[ $path ] = $alt; } $items[] = array_filter( array( 'src' => $path, 'alt' => $alt, 'caption' => (string) ( $a['caption'] ?? '' ), 'width' => (int) ( $a['width'] ?? 0 ), 'height' => (int) ( $a['height'] ?? 0 ) ) ); } return $items; }; $tagged = static function ( array $a, $numbered ) use ( $tone_of ) { $cards = array(); $row = false; foreach ( preg_split( '/\r\n|\r|\n/', (string) ( $a['items'] ?? '' ) ) ?: array() as $line ) { $line = trim( $line ); if ( '' === $line ) { continue; } if ( str_starts_with( $line, '#' ) ) { $c = array_map( 'trim', explode( '|', substr( $line, 1 ) ) ); $flags = preg_split( '/\s+/', (string) ( $c[3] ?? '' ) ) ?: array(); $row = $row || ! in_array( 'top', $flags, true ); $card = array( 'badge' => $c[0], 'title' => (string) ( $c[2] ?? '' ), 'text' => '' ); $tone = $tone_of( (string) ( $c[1] ?? '' ), $c[0] ); if ( '' !== $tone ) { $card['tone'] = $tone; } $cards[] = $card; continue; } if ( ! $cards ) { continue; } $at = count( $cards ) - 1; if ( preg_match( '/^~!?\s+(.+)$/u', $line, $m ) ) { $cards[ $at ]['chips'] = array_merge( (array) ( $cards[ $at ]['chips'] ?? array() ), array_map( 'trim', explode( '|', $m[1] ) ) ); continue; } $cards[ $at ]['text'] .= ( '' !== $cards[ $at ]['text'] ? ( $numbered ? "\n\n" : "\n" ) : '' ) . $line; } return array( $cards, $row ); }; switch ( $kind ) { case 'hero': $pill = $get( 'rooster/mark:pill' ); if ( $pill ) { $s['eyebrow'] = (string) ( $pill['attrs']['text'] ?? '' ); if ( '' !== (string) ( $pill['attrs']['icon'] ?? '' ) ) { $s['icon'] = (string) $pill['attrs']['icon']; } } $h1 = self::find( $block, static fn ( $b ) => 'core/heading' === $b['blockName'] && 1 === (int) ( $b['attrs']['level'] ?? 0 ), 1 ); if ( $h1 ) { $html = implode( '', array_filter( (array) $h1['innerContent'], 'is_string' ) ); if ( preg_match( '#]*>(.*?)#s', $html, $m ) ) { $s['emphasis'] = html_entity_decode( wp_strip_all_tags( $m[1] ), ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } $s['heading'] = implode( "\n", array_map( static fn ( $part ) => AIEO_Content_Inline::text( $part ), preg_split( '##', preg_replace( '#]*>#', '', $html ) ) ?: array() ) ); } $ledes = array(); foreach ( (array) $block['innerBlocks'] as $b ) { if ( 'core/paragraph' === $b['blockName'] ) { $ledes[] = self::text( $b ); } } if ( $ledes ) { $s['lede'] = implode( "\n\n", $ledes ); } $row = $get( 'rooster/action:buttons' ); if ( $row ) { $s['buttons'] = $buttons( $row['attrs']['buttons'] ?? '' ); if ( '' !== (string) ( $row['attrs']['note'] ?? '' ) ) { $s['note'] = (string) $row['attrs']['note']; } } break; case 'stats': $facts = $get( 'rooster/list:facts' ); $a = $facts ? (array) $facts['attrs'] : array(); if ( 'counters' === (string) ( $a['layout'] ?? '' ) ) { $s['headless'] = true; } $s['items'] = array_map( static fn ( $r ) => array_filter( array( 'value' => $r[0], 'label' => (string) ( $r[1] ?? '' ), 'tone' => (string) ( $r[2] ?? '' ) ), 'strlen' ), $rows( $a['items'] ?? '', 3 ) ); $panels = $get( 'rooster/card:tagged' ); $i = $part_of( 'cards' ); if ( $panels && null !== $i ) { [ $cards, $row ] = $tagged( (array) $panels['attrs'], false ); $parts[ $i ] += array( 'cards' => $cards ) + ( $row && 2 !== count( $cards ) ? array( 'layout' => 'row' ) : array() ); if ( 'wide' === (string) ( $panels['attrs']['columns'] ?? '' ) ) { $parts[ $i ]['layout'] = 'row'; } if ( '' !== $notes ) { $parts[ $i ]['note'] = $notes; $notes = ''; } } break; case 'cards': $stack = $get( 'rooster/media:stack' ); if ( $stack ) { $a = (array) $stack['attrs']; $s['style'] = 'contrast'; foreach ( array( 'eyebrow', 'heading' ) as $field ) { if ( '' !== (string) ( $a[ $field ] ?? '' ) ) { $s[ $field ] = (string) $a[ $field ]; } } if ( '' !== (string) ( $a['text'] ?? '' ) ) { $s['lede'] = (string) $a['text']; } $s['cards'] = array_map( static fn ( $r ) => array( 'title' => $r[0], 'text' => (string) ( $r[1] ?? '' ) ) + ( 'hi' === (string) ( $r[2] ?? '' ) ? array( 'highlighted' => true ) : array() ), $rows( $a['items'] ?? '', 3 ) ); break; } if ( $matrix = $get( 'rooster/table:matrix' ) ) { $s['cards'] = array(); foreach ( $rows( $matrix['attrs']['items'] ?? '' ) as $r ) { $card = array( 'badge' => $r[0], 'title' => (string) ( $r[2] ?? '' ), 'text' => (string) ( $r[3] ?? '' ), 'colour' => (string) ( $r[4] ?? '' ), 'ink' => trim( (string) ( $r[5] ?? '' ) . ' ' . (string) ( $r[6] ?? '' ) ) ); $tone = $tone_of( (string) ( $r[1] ?? '' ), $r[0] ); if ( '' !== $tone ) { $card['tone'] = $tone; } if ( 'wide' === (string) ( $r[7] ?? '' ) ) { $card['layout'] = 'row'; } $s['cards'][] = array_filter( $card, 'strlen' ); } } elseif ( $roles = $get( 'rooster/card:role' ) ) { $s['cards'] = array_map( static fn ( $r ) => array_filter( array( 'icon' => $r[0], 'badge' => (string) ( $r[1] ?? '' ), 'title' => (string) ( $r[2] ?? '' ), 'text' => (string) ( $r[3] ?? '' ), 'colour' => (string) ( $r[4] ?? '' ) ), 'strlen' ), $rows( $roles['attrs']['items'] ?? '', 5 ) ); } elseif ( ( $steps = $get( 'rooster/card:grid' ) ) && 'steps-plain' === (string) ( $steps['attrs']['layout'] ?? '' ) ) { $s['numbered'] = true; $s['cards'] = array_map( static fn ( $r ) => array( 'title' => $r[0], 'text' => (string) ( $r[1] ?? '' ) ), $rows( $steps['attrs']['items'] ?? '', 2 ) ); } elseif ( $panels = $get( 'rooster/card:tagged' ) ) { $numbered = 'numbered' === (string) ( $panels['attrs']['layout'] ?? '' ); [ $cards, $row ] = $tagged( (array) $panels['attrs'], $numbered ); $s['cards'] = $cards; if ( $numbered ) { $s['numbered'] = true; } elseif ( $row && 2 !== count( $cards ) || 'wide' === (string) ( $panels['attrs']['columns'] ?? '' ) ) { $s['layout'] = 'row'; } elseif ( $row ) { $s['layout'] = 'row'; } } $i = $part_of( 'gallery' ); if ( null !== $i ) { $parts[ $i ]['items'] = $figures( $all( 'rooster/media:figure' ) ); } break; case 'gallery': $s['items'] = $figures( $all( 'rooster/media:figure' ) ); break; case 'pricing': $s['tiers'] = array(); foreach ( $all( 'rooster/plan:plan' ) as $card ) { $a = (array) $card['attrs']; $tier = array( 'name' => (string) ( $a['name'] ?? '' ) ); foreach ( array( 'tone', 'tagline', 'price', 'period', 'note', 'ctaText', 'ctaHref', 'anchor' ) as $field ) { if ( '' !== (string) ( $a[ $field ] ?? '' ) ) { $tier[ $field ] = (string) $a[ $field ]; } } $tier['features'] = AIEO_Engine_Blocks::lines( (string) ( $a['features'] ?? '' ) ); if ( '' !== (string) ( $a['volume'] ?? '' ) ) { $v = $rows( $a['volume'] ); $tier['volume'] = array_filter( array( 'caption' => (string) ( $a['volumeCaption'] ?? '' ), 'columns' => (array) array_shift( $v ), 'rows' => $v, 'note' => (string) ( $a['volumeNote'] ?? '' ) ) ); } $s['tiers'][] = $tier; } break; case 'prose': $stages = $get( 'rooster/list:stages' ); $wash = $get( 'rooster/action:panel' ); if ( $stages ) { $a = (array) $stages['attrs']; $body = '' !== (string) ( $a['heading'] ?? '' ) ? array( '### ' . (string) $a['heading'] ) : array(); $s['body'] = implode( "\n\n", array_merge( $body, array_filter( preg_split( '/\n\s*\n/', (string) ( $a['text'] ?? '' ) ) ?: array(), 'strlen' ) ) ); $i = $part_of( 'cards' ); if ( null !== $i ) { $parts[ $i ]['numbered'] = true; $parts[ $i ]['cards'] = array(); foreach ( $rows( $a['items'] ?? '', 5 ) as $r ) { $card = array( 'badge' => $r[0], 'title' => (string) ( $r[2] ?? '' ), 'kicker' => (string) ( $r[3] ?? '' ), 'text' => (string) ( $r[4] ?? '' ) ); $tone = $tone_of( (string) ( $r[1] ?? '' ), $r[0] ); if ( '' !== $tone ) { $card['tone'] = $tone; } $parts[ $i ]['cards'][] = $card; } if ( '' !== (string) ( $a['note'] ?? '' ) ) { $parts[ $i ]['note'] = (string) $a['note']; } } } elseif ( $wash ) { $a = (array) $wash['attrs']; foreach ( array( 'eyebrow', 'heading' ) as $field ) { if ( '' !== (string) ( $a[ $field ] ?? '' ) ) { $s[ $field ] = (string) $a[ $field ]; } } $s['body'] = (string) ( $a['text'] ?? '' ); $s['buttons'] = array_map( static fn ( $r ) => array( 'text' => $r[0], 'href' => (string) ( $r[1] ?? '' ) ) + ( '' !== (string) ( $r[3] ?? '' ) ? array( 'icon' => (string) $r[3] ) : array() ), $rows( $a['buttons'] ?? '', 4 ) ); } break; case 'faq': $list = $get( 'rooster/questions:boxed' ); $s['items'] = array_map( static fn ( $r ) => array( 'q' => $r[0], 'a' => (string) ( $r[1] ?? '' ) ), $rows( $list ? ( $list['attrs']['items'] ?? '' ) : '', 2 ) ); break; case 'cta': $ink = $get( 'rooster/action:panel' ); $a = $ink ? (array) $ink['attrs'] : array(); foreach ( array( 'heading', 'text', 'note' ) as $field ) { if ( '' !== (string) ( $a[ $field ] ?? '' ) ) { $s[ $field ] = (string) $a[ $field ]; } } $s['buttons'] = $buttons( $a['buttons'] ?? '' ); $notes = ''; break; } if ( '' !== $notes ) { $s['note'] = $notes; } if ( $parts ) { $s['_parts'] = $parts; } return $s; } private static function read_layout( array $block, $layout, $kind, array &$media = array() ) { $s = self::common( $block, '' !== $kind ? $kind : 'cards' ); if ( ! $layout ) { $s['blocks'] = array(); return self::read_layout_extras( $block, null, $s ); } $gap = (string) ( $layout['attrs']['style']['spacing']['blockGap'] ?? '' ); $columns = self::find( $layout, static fn ( $b ) => 'core/columns' === $b['blockName'], 1 ); $blocks = array(); $is_head = static fn ( array $b ) => 'core/group' === $b['blockName'] && __( 'Section head', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ); $id = (string) ( $block['attrs']['anchor'] ?? '' ); $take = static function ( array $b, $column ) use ( &$blocks, &$s, $is_head, &$media, $id ) { if ( $is_head( $b ) ) { $s['head_column'] = max( 1, (int) $column ); return; } if ( 'core/pattern' === $b['blockName'] ) { $item = array( 'pattern' => (string) ( $b['attrs']['slug'] ?? '' ) ); } elseif ( 'rooster/tabs' === $b['blockName'] ) { $item = self::read_tabs( (array) ( $b['_tabs'] ?? $b ), $media, $s, $id ); } elseif ( 0 === strpos( (string) $b['blockName'], 'rooster/' ) ) { $item = array( 'block' => substr( (string) $b['blockName'], 8 ) ) + array_diff_key( (array) $b['attrs'], array( 'roosterDefer' => 1, 'style' => 1, 'align' => 1 ) ); } else { return; } if ( $column ) { $item['column'] = $column; } $blocks[] = $item; }; if ( $columns ) { $widths = array(); foreach ( array_values( array_filter( (array) $columns['innerBlocks'], static fn ( $b ) => 'core/column' === $b['blockName'] ) ) as $i => $col ) { $widths[] = rtrim( (string) ( $col['attrs']['width'] ?? '50%' ), '%' ); foreach ( (array) $col['innerBlocks'] as $b ) { $take( $b, $i + 1 ); } } $s['columns'] = implode( '/', $widths ); } else { $count = (int) ( $layout['attrs']['layout']['columnCount'] ?? 1 ); if ( $count > 1 ) { $s['columns'] = $count; } foreach ( array_values( (array) $layout['innerBlocks'] ) as $i => $b ) { if ( 'core/group' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-section__cell' ) ) { foreach ( (array) $b['innerBlocks'] as $inner ) { $take( $inner, $i + 1 ); } } else { $take( $b, $is_head( $b ) ? $i + 1 : 0 ); } } } if ( preg_match( '/^var:preset\|spacing\|(\d+)$/', $gap, $m ) ) { if ( 5 !== (int) $m[1] ) { $s['gap'] = (int) $m[1]; } } elseif ( '' !== $gap ) { $s['gap'] = $gap; } $s['blocks'] = $blocks; return self::read_layout_extras( $block, $layout, $s ); } private static function without_tabs( array $block, &$holds = false ) { if ( 'rooster/tabs' === (string) ( $block['blockName'] ?? '' ) ) { $holds = true; return array( 'blockName' => 'rooster/tabs', 'attrs' => (array) ( $block['attrs'] ?? array() ), 'innerBlocks' => array(), 'innerHTML' => '', 'innerContent' => array(), '_tabs' => $block ); } foreach ( (array) ( $block['innerBlocks'] ?? array() ) as $i => $child ) { $block['innerBlocks'][ $i ] = self::without_tabs( (array) $child, $holds ); } return $block; } private static function read_tabs( array $block, array &$media, array $s, $id ) { $a = (array) ( $block['attrs'] ?? array() ); $item = array( 'block' => 'tabs' ); $label = (string) ( $a['label'] ?? '' ); if ( '' !== $label && AIEO_Content_Inline::heading_plain( (string) ( $s['heading'] ?? '' ) ) !== $label ) { $item['label'] = $label; } $tone = (string) ( $a['tone'] ?? '' ); if ( '' !== $tone && (string) ( $s['tone'] ?? '' ) !== $tone ) { $item['tone'] = $tone; } $anchor = (string) ( $a['anchor'] ?? '' ); if ( '' !== $anchor && $anchor !== AIEO_Content_Tabs::container_id( array(), array( 'anchor' => $id, 'key' => $id ) ) ) { $item['anchor'] = $anchor; } $tabs = array(); foreach ( (array) ( $block['innerBlocks'] ?? array() ) as $inner ) { if ( 'rooster/tab' !== (string) ( $inner['blockName'] ?? '' ) ) { continue; } $t = (array) ( $inner['attrs'] ?? array() ); $own = (array) ( $t['metadata']['rooster'] ?? array() ); $tab = array(); if ( '' !== (string) ( $own['key'] ?? '' ) ) { $tab['key'] = (string) $own['key']; } foreach ( array( 'label', 'icon', 'tone', 'anchor' ) as $field ) { if ( '' !== (string) ( $t[ $field ] ?? '' ) ) { $tab[ $field ] = (string) $t[ $field ]; } } $aliases = array_values( array_map( 'strval', (array) ( $own['aliases'] ?? array() ) ) ); if ( $aliases ) { $tab['aliases'] = $aliases; } $sections = self::read_sections( (array) ( $inner['innerBlocks'] ?? array() ), $media, array( 'first' => ! $tabs ) ); foreach ( $aliases ? $sections : array() as $j => $section ) { if ( ! empty( $section['aliases'] ) ) { $left = array_values( array_diff( (array) $section['aliases'], $aliases ) ); if ( $left ) { $sections[ $j ]['aliases'] = $left; } else { unset( $sections[ $j ]['aliases'] ); } } } $tab['sections'] = $sections; $tabs[] = $tab; } $item['tabs'] = $tabs; return $item; } private static function read_layout_extras( array $block, $layout, array $s ) { $head = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && __( 'Section head', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ), 5 ); $of = static function ( $class ) use ( $head ) { return $head ? self::find( $head, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), $class ), 1 ) : null; }; $heading = $head ? self::find( $head, static fn ( $b ) => 'core/heading' === $b['blockName'], 2 ) : null; $found = array( 'heading' => $heading, 'head' => $head, 'eyebrow' => $of( 'rooster-eyebrow' ), 'badge' => $of( 'rooster-badge' ), 'notice' => $of( 'rooster-notice' ) ?: ( $head ? self::notice_pill_paragraph( $head ) : null ), 'body' => $layout, ); $aliases = array(); foreach ( AIEO_Content_Layout::ALIAS_TARGETS as $target ) { $id = (string) ( $found[ $target ]['attrs']['anchor'] ?? '' ); if ( '' !== $id ) { $aliases[] = $id; } } if ( $aliases ) { $s['aliases'] = $aliases; } if ( self::has_class( $block, 'rooster-r1', false ) ) { $s['_promo'] = true; foreach ( (array) $block['innerBlocks'] as $b ) { if ( 'rooster/action:buttons' === self::shape( $b ) ) { $s['buttons'] = self::r1_buttons( (string) ( $b['attrs']['buttons'] ?? '' ) ); } elseif ( 'rooster/mark:note' === self::shape( $b ) && '' !== (string) ( $b['attrs']['text'] ?? '' ) ) { $s['note'] = (string) $b['attrs']['text']; } } } return $s; } private static function r1_buttons( $lines ) { $out = array(); $home = (string) wp_parse_url( home_url( '/' ), PHP_URL_HOST ); $act = array_flip( AIEO_Content_Spec::BUTTON_ACTION_LINKS ); foreach ( self::rows( (string) $lines ) as $r ) { $kind = (string) ( $r[2] ?? '' ); $button = array( 'text' => $r[0], 'href' => (string) ( $r[1] ?? '' ) ); $button['style'] = 'light' === $kind ? 'outline' : ( 'download' === $kind ? 'primary' : 'secondary' ); if ( isset( $act[ $button['href'] ] ) ) { $button['action'] = (string) $act[ $button['href'] ]; unset( $button['href'] ); } if ( '' !== (string) ( $r[3] ?? '' ) ) { $button['note'] = (string) $r[3]; } $host = (string) wp_parse_url( (string) ( $button['href'] ?? '' ), PHP_URL_HOST ); if ( '' !== $host && $host !== $home ) { $button['external'] = true; } if ( '' !== (string) ( $r[4] ?? '' ) ) { $button['icon'] = (string) $r[4]; } $out[] = $button; } return $out; } private static function guess_kind( array $block ) { $name = (string) $block['blockName']; if ( 'core/cover' === $name ) { return 'hero'; } if ( 'core/pattern' === $name ) { return 'pattern'; } if ( 'core/group' !== $name ) { return ''; } if ( self::has_class( $block, 'rooster-hero', false ) ) { return 'hero'; } $names = self::names( $block ); if ( in_array( 'rooster/plan:price', $names, true ) || self::has_class( $block, 'rooster-price-card', true ) ) { return 'pricing'; } if ( in_array( 'rooster/card:feature', $names, true ) || self::has_class( $block, 'rooster-feature-card', true ) ) { return 'cards'; } if ( in_array( 'rooster/table:compare', $names, true ) ) { return 'table'; } if ( in_array( 'core/accordion', $names, true ) ) { return 'faq'; } if ( in_array( 'core/gallery', $names, true ) ) { return 'gallery'; } if ( in_array( 'core/pattern', $names, true ) ) { return 'pattern'; } if ( in_array( 'core/columns', $names, true ) ) { return 'split'; } $grid = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && 'grid' === ( $b['attrs']['layout']['type'] ?? '' ) ); if ( $grid && self::find( $grid, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && '3xl' === ( $b['attrs']['fontSize'] ?? '' ) ) ) { return 'stats'; } if ( in_array( 'core/buttons', $names, true ) && ! $grid && self::has_class( $block, 'rooster-cta', false ) ) { return 'cta'; } return 'prose'; } private static function common( array $block, $kind ) { $s = array( 'kind' => $kind ); $className = (string) ( $block['attrs']['className'] ?? '' ); $anchor = (string) ( $block['attrs']['anchor'] ?? '' ); if ( '' !== $anchor && $anchor !== (string) ( $block['attrs']['metadata']['rooster']['key'] ?? '' ) ) { $s['anchor'] = $anchor; } if ( preg_match( '/\bis-style-section-([a-z]+)\b/', $className, $m ) ) { if ( $m[1] !== ( AIEO_Content_Sections::DEFAULT_STYLE[ $kind ] ?? 'default' ) ) { $s['style'] = $m[1]; } } elseif ( isset( AIEO_Content_Sections::DEFAULT_STYLE[ $kind ] ) && isset( $block['attrs']['metadata']['rooster'] ) ) { $s['style'] = 'default'; } if ( preg_match( '/\bis-style-accent-([a-z0-9_-]+)\b/', $className, $m ) ) { $s['tone'] = $m[1]; } elseif ( preg_match( '/\brooster-section--tone-([a-z]+)\b/', $className, $m ) ) { $s['tone'] = $m[1]; } $head = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && ( __( 'Section head', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ) || str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-section__head' ) ), 4 ); $scope = $head ?: $block; $pill = $head ? self::find( $head, static fn ( $b ) => 'core/group' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-section__pill' ), 1 ) : null; $icon = self::find( $scope, static fn ( $b ) => 'core/icon' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-section__icon' ), $pill ? 2 : 1 ); if ( $icon ) { $s['icon'] = preg_replace( '#^rooster/#', '', (string) ( $icon['attrs']['icon'] ?? '' ) ); } $eyebrow = self::find( $scope, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-eyebrow' ), 2 ); if ( $eyebrow ) { $s['eyebrow'] = self::text( $eyebrow ); } $badge = self::find( $scope, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-badge' ), 2 ); if ( $badge && $head ) { $s['badge'] = self::text( $badge ); } $heading = self::find( $pill ?: $scope, static fn ( $b ) => 'core/heading' === $b['blockName'] && ( $head || (int) ( $b['attrs']['level'] ?? 2 ) <= 3 ), $head ? 1 : 3 ); if ( $heading ) { $s['heading'] = self::heading_text( $heading ); $level = (int) ( $heading['attrs']['level'] ?? 2 ); if ( 2 !== $level ) { $s['level'] = $level; } $centred = 'center' === (string) ( $heading['attrs']['textAlign'] ?? '' ); if ( $centred && 'cta' !== $kind ) { $s['align'] = 'center'; } elseif ( ! $centred && 'cta' === $kind ) { $s['align'] = 'left'; } $ruled = 1 !== $level && ! empty( AIEO_Content_Sections::RULE[ $kind ] ); if ( $pill ) { $s['heading_style'] = 'pill'; } elseif ( isset( $heading['attrs']['className'] ) && str_contains( (string) $heading['attrs']['className'], 'is-style-accent-rule' ) ) { if ( ! $ruled ) { $s['rule'] = true; } } elseif ( $ruled ) { $s['rule'] = false; } } else { $details = self::find( $block, static fn ( $b ) => 'core/details' === $b['blockName'], 2 ); if ( $details ) { $s['heading'] = (string) ( $details['attrs']['summary'] ?? '' ); $s['collapsible'] = true; } elseif ( ! in_array( $kind, array( 'cta', 'prose', 'pattern', 'split' ), true ) ) { $s['headless'] = true; } } $lede = self::paragraphs_after( $scope, $pill ?: ( $heading ?: $eyebrow ), array( 'rooster-notice', 'rooster-badge' ) ); if ( '' !== $lede ) { $s['lede'] = $lede; } $notice = self::find( $scope, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-notice' ), 2 ); if ( $notice ) { $s['notice'] = self::text( $notice ); } elseif ( '' !== ( $at_lede = self::notice_pill( $scope ) ) ) { $s['notice'] = $at_lede; $s['notice_place'] = 'lede'; } $links = self::links( $block ); if ( $links ) { $s['links'] = $links; } $buttons = self::buttons( $block ); if ( $buttons ) { $s['buttons'] = $buttons; } $note = self::note( $block ); if ( '' !== $note ) { $s['note'] = $note; } return $s; } private static function notice_pill_paragraph( array $scope ) { return self::find( $scope, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( implode( '', array_filter( (array) $b['innerContent'], 'is_string' ) ), AIEO_Content_Sections::NOTICE_PILL ), 3 ); } private static function notice_pill( array $scope ) { $p = self::notice_pill_paragraph( $scope ); $html = $p ? implode( '', array_filter( (array) $p['innerContent'], 'is_string' ) ) : ''; return preg_match( '#(.*?)#su', $html, $m ) ? AIEO_Content_Inline::text( $m[1] ) : ''; } private static function paragraphs_after( array $root, $target, array $skip ) { if ( null === $target ) { return ''; } $siblings = self::siblings( $root, $target ); if ( null === $siblings ) { return ''; } [ $children, $i ] = $siblings; $lines = array(); for ( $j = $i + 1; $j < count( $children ); $j++ ) { $b = $children[ $j ]; if ( 'core/group' === $b['blockName'] && ( str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-hero__lede' ) || __( 'Lede', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ) ) ) { foreach ( (array) $b['innerBlocks'] as $p ) { if ( 'core/paragraph' === $p['blockName'] ) { $lines[] = self::text( $p ); } } } elseif ( 'core/paragraph' === $b['blockName'] ) { $class = (string) ( $b['attrs']['className'] ?? '' ); foreach ( $skip as $s ) { if ( str_contains( $class, $s ) ) { continue 2; } } $lines[] = self::text( $b ); } elseif ( 'core/list' === $b['blockName'] && '' === (string) ( $b['attrs']['className'] ?? '' ) ) { $lines[] = self::list_text( $b ); } else { break; } } return implode( "\n\n", $lines ); } private static function list_text( array $list ) { $lines = array(); $ordered = ! empty( $list['attrs']['ordered'] ); $n = 0; foreach ( (array) $list['innerBlocks'] as $li ) { if ( 'core/list-item' === $li['blockName'] ) { $lines[] = ( $ordered ? ( ++$n ) . '. ' : '- ' ) . self::text( $li ); } } return implode( "\n", $lines ); } private static function note( array $block ) { $lines = array(); foreach ( self::find_all( $block, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-section__note' ), self::item_stop() ) as $p ) { $lines[] = self::text( $p ); } return implode( "\n\n", $lines ); } private static function links( array $block ) { $p = self::find( $block, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-section__links' ), 4 ); if ( ! $p ) { return array(); } $html = implode( '', array_filter( (array) $p['innerContent'], 'is_string' ) ); $items = array(); if ( preg_match_all( '#]*href="([^"]*)"[^>]*>(.*?)#su', $html, $m, PREG_SET_ORDER ) ) { foreach ( $m as $a ) { $items[] = array( 'text' => AIEO_Content_Inline::text( $a[2] ), 'href' => html_entity_decode( $a[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); } } if ( preg_match( '#^\s*]*>\s*(.*?)#su', $html, $m ) ) { return array( 'heading' => AIEO_Content_Inline::text( $m[1] ), 'items' => $items ); } return $items; } private static function item_stop() { return static fn ( $b ) => in_array( self::shape( $b ), array( 'rooster/card:feature', 'rooster/plan:price' ), true ) || self::has_class( $b, 'rooster-feature-card', false ) || self::has_class( $b, 'rooster-price-card', false ) || self::has_class( $b, 'rooster-family-card', false ) || self::has_class( $b, 'rooster-hero__panel', false ); } private static function buttons( array $block ) { $found = self::find_all( $block, static fn ( $b ) => 'core/buttons' === $b['blockName'], self::item_stop() ); if ( ! $found ) { return array(); } $buttons = array(); $block_b = end( $found ); foreach ( (array) $block_b['innerBlocks'] as $button ) { if ( 'core/button' !== $button['blockName'] ) { continue; } $html = implode( '', array_filter( (array) $button['innerContent'], 'is_string' ) ); $words = preg_match( '#]*>(.*?)#su', $html, $m ) ? $m[1] : $html; $note = ''; if ( str_contains( (string) ( $button['attrs']['className'] ?? '' ), AIEO_Content_Blocks::BUTTON_NOTE_CLASS ) && preg_match( '#^(.*?)(.*)$#su', $words, $parts ) ) { [ , $words, $note ] = $parts; } $row = array( 'text' => AIEO_Content_Inline::text( $words ) ); if ( preg_match( '#href="([^"]*)"#', $html, $m ) ) { $row['href'] = html_entity_decode( $m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } $row['style'] = str_contains( (string) ( $button['attrs']['className'] ?? '' ), 'is-style-outline' ) ? 'secondary' : 'primary'; if ( '_blank' === (string) ( $button['attrs']['linkTarget'] ?? '' ) ) { $row['external'] = true; } elseif ( AIEO_Content_Blocks::is_external( (string) ( $row['href'] ?? '' ) ) ) { $row['new_tab'] = false; } $class = (string) ( $button['attrs']['className'] ?? '' ); foreach ( AIEO_Content_Spec::BUTTON_ACTIONS as $action => $action_class ) { if ( str_contains( $class, $action_class ) ) { $row['action'] = $action; } } foreach ( AIEO_Content_Blocks::BUTTON_ICONS as $icon => $style ) { if ( '' !== $style && preg_match( '/\bis-style-' . preg_quote( $style, '/' ) . '\b/', $class ) ) { $row['icon'] = $icon; } } if ( preg_match( '/(?:^|\s)' . preg_quote( AIEO_Content_Blocks::BUTTON_ICON_CLASS, '/' ) . '([a-z0-9_-]+)(?:\s|$)/', $class, $m ) ) { $row['icon'] = $m[1]; } if ( in_array( (string) ( $button['attrs']['style']['color']['text'] ?? '' ), AIEO_Content_Blocks::SOLID_TEXT, true ) && ! empty( $button['attrs']['style']['color']['background'] ) ) { $row['solid'] = true; } if ( '' !== $note ) { $row['note'] = AIEO_Content_Inline::text( $note ); } $buttons[] = $row; } return $buttons; } private static function read_hero( array $block, array &$media ) { $s = array( 'kind' => 'hero' ); if ( preg_match( '/\bis-style-accent-([a-z0-9_-]+)\b/', (string) ( $block['attrs']['className'] ?? '' ), $m ) ) { $s['tone'] = $m[1]; } $anchor = (string) ( $block['attrs']['anchor'] ?? '' ); if ( '' !== $anchor && $anchor !== (string) ( $block['attrs']['metadata']['rooster']['key'] ?? '' ) ) { $s['anchor'] = $anchor; } $head = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && in_array( (string) ( $b['attrs']['metadata']['name'] ?? '' ), array( __( 'Hero text', 'ai-eshop-optimizer' ), __( 'Section head', 'ai-eshop-optimizer' ) ), true ), 4 ) ?: self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && 'constrained' === ( $b['attrs']['layout']['type'] ?? '' ), 4 ) ?: $block; $chips = self::find( $head, static fn ( $b ) => 'core/list' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'is-style-chips' ) && ! str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-on-this-page' ), 2 ); if ( $chips ) { $s['badges'] = array_values( array_map( static fn ( $b ) => self::text( $b ), array_filter( (array) $chips['innerBlocks'], static fn ( $b ) => 'core/list-item' === $b['blockName'] ) ) ); } $eyebrow = self::find( $head, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-eyebrow' ), 3 ); if ( $eyebrow ) { $s['eyebrow'] = self::text( $eyebrow ); } $pill = self::find( $head, static fn ( $b ) => 'rooster/mark:pill' === self::shape( $b ), 1 ); if ( $pill ) { $s['eyebrow'] = (string) ( $pill['attrs']['text'] ?? '' ); $s['eyebrow_style'] = 'pill'; if ( '' !== (string) ( $pill['attrs']['icon'] ?? '' ) ) { $s['icon'] = (string) $pill['attrs']['icon']; } } $heading = self::find( $head, static fn ( $b ) => 'core/heading' === $b['blockName'], 2 ); if ( $chips && $heading ) { $order = array_map( static fn ( $b ) => $b['blockName'] . ':' . (string) ( $b['attrs']['className'] ?? '' ), (array) $head['innerBlocks'] ); $at_h = array_search( 'core/heading:' . (string) ( $heading['attrs']['className'] ?? '' ), $order, true ); $at_c = array_search( 'core/list:' . (string) ( $chips['attrs']['className'] ?? '' ), $order, true ); if ( false !== $at_h && false !== $at_c && $at_c < $at_h ) { $s['badges_place'] = 'above'; } } if ( str_contains( (string) ( $block['attrs']['className'] ?? '' ), 'rooster-hero--center' ) ) { $s['align'] = 'center'; } if ( $heading ) { $s['heading'] = self::heading_text( $heading ); $lede = self::paragraphs_after( $head, $heading, array( 'rooster-section__note', 'rooster-buttons__note' ) ); if ( '' !== $lede ) { $s['lede'] = $lede; } } $buttons = self::buttons( $head ); if ( $buttons ) { $s['buttons'] = $buttons; } $note = self::note( $head ); if ( '' !== $note ) { $s['note'] = $note; } $toc = self::find( $head, static fn ( $b ) => 'core/list' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-on-this-page' ), 2 ); if ( $toc ) { $s['on_this_page'] = array(); foreach ( (array) $toc['innerBlocks'] as $li ) { $html = implode( '', array_filter( (array) $li['innerContent'], 'is_string' ) ); if ( preg_match( '#]*href="([^"]*)"[^>]*>(.*?)#su', $html, $m ) ) { $s['on_this_page'][] = array( 'text' => AIEO_Content_Inline::text( $m[2] ), 'href' => html_entity_decode( $m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) ); } } } $image = self::find( $block, static fn ( $b ) => 'rooster/responsive-image' === $b['blockName'] ); if ( $image ) { $s['image'] = self::responsive_image( $image, $media ); } if ( str_contains( (string) ( $block['attrs']['className'] ?? '' ), 'is-style-hero-card' ) ) { $s['style'] = 'card'; } $strip = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && self::has_class( $b, 'rooster-stat-strip', false ), 5 ); if ( $strip ) { $s['_strip'] = self::stats_items( $strip ); } $panel = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && self::has_class( $b, 'rooster-hero__panel', false ) ); if ( $panel ) { $s['panel'] = self::panel( $panel ); } $cols = self::find( $block, static fn ( $b ) => 'core/columns' === $b['blockName'] && self::has_class( $b, 'rooster-hero__columns', false ), 2 ); if ( $cols ) { $widths = array(); foreach ( (array) $cols['innerBlocks'] as $col ) { if ( 'core/column' === $col['blockName'] ) { $widths[] = (string) ( $col['attrs']['width'] ?? '' ); } } $default = empty( $s['tiles'] ) && ! self::find( $block, static fn ( $b ) => self::has_class( $b, 'rooster-hero__tiles', false ), 4 ) ? array( '58%', '42%' ) : array( '53.49%', '46.51%' ); if ( 2 === count( $widths ) && $widths !== $default && '' !== $widths[0] && '' !== $widths[1] ) { $s['columns'] = rtrim( $widths[0], '%' ) . '/' . rtrim( $widths[1], '%' ); } $left = (string) ( $cols['attrs']['style']['spacing']['blockGap']['left'] ?? '' ); if ( array( '58%', '42%' ) === $default && '' !== $left && AIEO_Content_Sections::HERO_GAP !== $left ) { $s['gap'] = preg_match( '/^var:preset\|spacing\|(\d+)$/', $left, $m ) ? (int) $m[1] : ( '0' === $left ? 0 : $left ); } } $blocks = self::find_all( $block, static fn ( $b ) => 'rooster/card:family' === self::shape( $b ) && 'tile' === ( $b['attrs']['layout'] ?? '' ) ); if ( $blocks ) { $s['tiles'] = array(); foreach ( $blocks as $tile ) { $a = (array) $tile['attrs']; $family = (string) ( $a['family'] ?? '' ); $s['tiles'][] = array_filter( array( 'key' => $family, 'title' => (string) ( $a['name'] ?? '' ), 'href' => (string) ( $a['href'] ?? '' ), 'accent' => $family, 'motif' => (string) ( $a['motif'] ?? '' ) ), 'strlen' ); } } $tiles = $blocks ? array() : self::find_all( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && self::has_class( $b, 'rooster-family-tile', false ) ); if ( $tiles ) { $s['tiles'] = array(); foreach ( $tiles as $tile ) { $h = self::find( $tile, static fn ( $b ) => 'core/heading' === $b['blockName'] ); $html = $h ? implode( '', array_filter( (array) $h['innerContent'], 'is_string' ) ) : ''; $row = array( 'title' => $h ? AIEO_Content_Inline::plain( self::text( $h ) ) : '' ); if ( preg_match( '#href="([^"]*)"#', $html, $m ) ) { $row['href'] = html_entity_decode( $m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } if ( preg_match( '/\bis-style-accent-([a-z0-9_-]+)\b/', (string) ( $tile['attrs']['className'] ?? '' ), $m ) ) { $row['key'] = $m[1]; $row['accent'] = $m[1]; } $motif = self::find( $tile, static fn ( $b ) => 'core/pattern' === $b['blockName'] ); if ( $motif ) { $row['motif'] = (string) ( $motif['attrs']['slug'] ?? '' ); } $s['tiles'][] = $row; } } return $s; } private static function panel( array $panel ) { $out = array(); $heading = self::find( $panel, static fn ( $b ) => 'core/heading' === $b['blockName'], 1 ); if ( $heading ) { $out['heading'] = self::text( $heading ); } $aside = null; $children = array(); foreach ( (array) $panel['innerBlocks'] as $b ) { if ( null === $aside && 'core/group' === $b['blockName'] && self::has_class( $b, 'rooster-hero__aside', false ) ) { $aside = $b; } else { $children[] = $b; } } $panel['innerBlocks'] = $children; $text = array(); foreach ( $children as $i => $b ) { if ( 'core/paragraph' === $b['blockName'] ) { $class = (string) ( $b['attrs']['className'] ?? '' ); if ( 'xs' === (string) ( $b['attrs']['fontSize'] ?? '' ) && 'core/buttons' === ( $children[ $i - 1 ]['blockName'] ?? '' ) ) { $out['note'] = self::text( $b ); } elseif ( ! str_contains( $class, 'rooster-buttons__note' ) ) { $text[] = self::text( $b ); } } elseif ( 'core/list' === $b['blockName'] ) { $text[] = self::list_text( $b ); } } if ( $text ) { $out['text'] = implode( "\n\n", $text ); } $items = self::find_all( $panel, static fn ( $b ) => 'rooster/card:feature' === self::shape( $b ) || self::has_class( $b, 'rooster-feature-card', false ) ); if ( $items ) { $out['items'] = array(); foreach ( $items as $item ) { $card = self::feature_card( $item ); $out['items'][] = array_intersect_key( $card, array( 'icon' => 1, 'title' => 1, 'text' => 1 ) ); } } $pattern = self::find( $panel, static fn ( $b ) => 'core/pattern' === $b['blockName'], 1 ); if ( $pattern ) { $out['pattern'] = (string) ( $pattern['attrs']['slug'] ?? '' ); } $buttons = self::buttons( $panel ); if ( $aside ) { $words = self::find( $aside, static fn ( $b ) => 'core/paragraph' === $b['blockName'], 1 ); if ( $words ) { $out['aside'] = self::text( $words ); } foreach ( self::buttons( $aside ) as $button ) { $buttons[] = $button + array( 'aside' => true ); } } if ( $buttons ) { $out['buttons'] = $buttons; } return $out; } private static function read_cards( array $block, array &$media ) { $s = self::common( $block, 'cards' ); $palettes = self::find( $block, static fn ( $b ) => 'rooster/card:family' === self::shape( $b ) && 'palettes' === ( $b['attrs']['layout'] ?? '' ), 3 ); if ( $palettes ) { $s['cards'] = array(); foreach ( AIEO_Engine_Blocks::lines( (string) ( $palettes['attrs']['swatches'] ?? '' ) ) as $line ) { $cells = array_map( 'trim', explode( '|', $line ) ); $s['cards'][] = array( 'key' => sanitize_title( $cells[0] ), 'title' => $cells[0], 'text' => '', 'swatches' => array_values( array_slice( $cells, 1 ) ) ); } return $s; } $cards = array(); $n = 0; $links = false; foreach ( self::find_all( $block, static fn ( $b ) => in_array( self::shape( $b ), array( 'rooster/card:feature', 'rooster/card:family' ), true ) || self::has_class( $b, 'rooster-feature-card', false ) || self::has_class( $b, 'rooster-family-card', false ) ) as $item ) { if ( 'rooster/card:family' === self::shape( $item ) ) { $card = self::family_block( (array) $item['attrs'] ); $links = $links || 'links' === ( $item['attrs']['layout'] ?? '' ); } else { $card = self::has_class( $item, 'rooster-family-card', false ) ? self::family_card( $item ) : self::feature_card( $item ); } ++$n; if ( isset( $card['badge'] ) && (string) $n === (string) $card['badge'] ) { unset( $card['badge'] ); $s['numbered'] = true; } elseif ( isset( $card['badge'], $card['items'] ) && (string) count( (array) $card['items'] ) === (string) $card['badge'] ) { unset( $card['badge'] ); } $cards[] = $card; } $s['cards'] = $cards; if ( $links ) { $s['layout'] = 'row'; } $grid = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && 'grid' === ( $b['attrs']['layout']['type'] ?? '' ) ); if ( $grid && ! empty( $grid['attrs']['layout']['columnCount'] ) && ! self::find( $grid, static fn ( $b ) => 'rooster/card:family' === self::shape( $b ), 1 ) ) { $s['columns'] = (int) $grid['attrs']['layout']['columnCount']; } $layouts = array_unique( array_map( static fn ( $c ) => (string) ( $c['layout'] ?? 'stack' ), $cards ) ); if ( 1 === count( $layouts ) && 'row' === reset( $layouts ) && count( $cards ) > 1 ) { $s['layout'] = 'row'; foreach ( $s['cards'] as &$card ) { unset( $card['layout'] ); } unset( $card ); } return $s; } private static function feature_card( array $item ) { if ( 'rooster/card:feature' === self::shape( $item ) ) { $a = self::attrs( $item ); $card = array( 'key' => sanitize_title( (string) ( $a['title'] ?? '' ) ), 'icon' => (string) ( $a['icon'] ?? 'award' ), 'title' => (string) ( $a['title'] ?? '' ), 'text' => (string) ( $a['text'] ?? '' ) ); foreach ( array( 'href', 'ctaText', 'level', 'layout', 'badge', 'kicker', 'price', 'anchor' ) as $k ) { if ( isset( $a[ $k ] ) && '' !== $a[ $k ] && ! self::is_default( 'rooster/card', $k, $a[ $k ] ) ) { $card[ $k ] = $a[ $k ]; } } if ( ! empty( $a['highlighted'] ) ) { $card['highlighted'] = true; } return $card; } $tile = self::find( $item, static fn ( $b ) => 'rooster/trust-tile' === $b['blockName'] ); $icon = self::find( $item, static fn ( $b ) => 'core/icon' === $b['blockName'], 2 ); $heading = self::find( $item, static fn ( $b ) => 'core/heading' === $b['blockName'] ); $h_html = $heading ? implode( '', array_filter( (array) $heading['innerContent'], 'is_string' ) ) : ''; $title = $tile ? (string) ( $tile['attrs']['title'] ?? '' ) : ( $heading ? AIEO_Content_Inline::plain( self::text( $heading ) ) : '' ); $card = array( 'key' => sanitize_title( $title ), 'icon' => $tile ? (string) ( $tile['attrs']['icon'] ?? 'award' ) : ( $icon ? (string) preg_replace( '#^rooster/#', '', (string) ( $icon['attrs']['icon'] ?? 'award' ) ) : 'award' ), 'title' => $title ); $badge = self::find( $item, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-badge' ), 2 ); if ( $badge ) { $card['badge'] = self::text( $badge ); } $kicker = self::find( $item, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && 'accent-text' === ( $b['attrs']['textColor'] ?? '' ), 1 ); if ( $kicker ) { $card['kicker'] = self::text( $kicker ); } $text = array(); foreach ( (array) $item['innerBlocks'] as $b ) { if ( 'core/paragraph' === $b['blockName'] && $b !== $badge && $b !== $kicker && ! str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'rooster-badge' ) ) { $text[] = self::text( $b ); } } $card['text'] = implode( "\n\n", $text ); $list = self::find( $item, static fn ( $b ) => 'core/list' === $b['blockName'], 1 ); if ( $list ) { $card['items'] = array(); foreach ( (array) $list['innerBlocks'] as $li ) { if ( 'core/list-item' === $li['blockName'] ) { $html = implode( '', array_filter( (array) $li['innerContent'], 'is_string' ) ); if ( preg_match( '#(.*?)(?::\s*(.*?))?#su', $html, $m ) ) { $row = array( 'title' => AIEO_Content_Inline::text( $m[1] ) ); if ( ! empty( $m[2] ) ) { $row['text'] = AIEO_Content_Inline::text( $m[2] ); } $card['items'][] = $row; } else { $card['items'][] = array( 'title' => self::text( $li ) ); } } } } if ( $heading && (int) ( $heading['attrs']['level'] ?? 2 ) !== 3 ) { $card['level'] = (int) $heading['attrs']['level']; } if ( $heading && preg_match( '#href="([^"]*)"#', $h_html, $m ) ) { $card['href'] = html_entity_decode( $m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } $buttons = self::buttons( $item ); if ( $buttons ) { $card['href'] = (string) ( $buttons[0]['href'] ?? '' ); $card['ctaText'] = $buttons[0]['text']; } elseif ( $tile && ! empty( $tile['attrs']['href'] ) ) { $card['href'] = (string) $tile['attrs']['href']; } if ( self::has_class( $item, 'is-highlighted', false ) ) { $card['highlighted'] = true; } if ( ! empty( $item['attrs']['anchor'] ) ) { $card['anchor'] = (string) $item['attrs']['anchor']; } return $card; } private static function family_block( array $a ) { $rows = static fn ( $value ) => array_map( static fn ( $line ) => array_map( 'trim', explode( '|', $line ) ), AIEO_Engine_Blocks::lines( (string) $value ) ); $family = (string) ( $a['family'] ?? '' ); $card = array( 'key' => '' !== $family ? $family : sanitize_title( (string) ( $a['name'] ?? '' ) ) ); if ( '' !== $family ) { $card['accent'] = $family; } $card['title'] = (string) ( $a['name'] ?? '' ); foreach ( array( 'text' => 'text', 'href' => 'href', 'ctaText' => 'ctaText' ) as $from => $to ) { if ( '' !== (string) ( $a[ $from ] ?? '' ) ) { $card[ $to ] = (string) $a[ $from ]; } } $products = array(); foreach ( $rows( $a['products'] ?? '' ) as $row ) { $products[] = array_filter( array( 'title' => (string) ( $row[0] ?? '' ), 'text' => (string) ( $row[3] ?? '' ), 'href' => (string) ( $row[1] ?? '' ), 'badge' => (string) ( $row[2] ?? '' ) ), 'strlen' ); } if ( $products ) { $card['products'] = $products; } $features = array(); foreach ( $rows( $a['features'] ?? '' ) as $row ) { $features[] = array_filter( array( 'title' => (string) ( $row[0] ?? '' ), 'href' => (string) ( $row[1] ?? '' ) ), 'strlen' ); } if ( $features ) { $card['features'] = $features; } if ( '' !== (string) ( $a['docsHref'] ?? '' ) ) { $card['docs'] = array( 'text' => '' !== (string) ( $a['docsText'] ?? '' ) ? (string) $a['docsText'] : 'Documentation', 'href' => (string) $a['docsHref'] ); } if ( '' !== (string) ( $a['motif'] ?? '' ) ) { $card['motif'] = (string) $a['motif']; } if ( '' !== (string) ( $a['anchor'] ?? '' ) ) { $card['anchor'] = (string) $a['anchor']; } return $card; } private static function family_card( array $item ) { $heading = self::find( $item, static fn ( $b ) => 'core/heading' === $b['blockName'], 1 ); $h_html = $heading ? implode( '', array_filter( (array) $heading['innerContent'], 'is_string' ) ) : ''; $card = array( 'key' => '', 'title' => $heading ? AIEO_Content_Inline::plain( self::text( $heading ) ) : '' ); if ( preg_match( '/\bis-style-accent-([a-z0-9_-]+)\b/', (string) ( $item['attrs']['className'] ?? '' ), $m ) ) { $card['key'] = $m[1]; $card['accent'] = $m[1]; } if ( '' === $card['key'] ) { $card['key'] = sanitize_title( $card['title'] ); } if ( preg_match( '#href="([^"]*)"#', $h_html, $m ) ) { $card['href'] = html_entity_decode( $m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } $p = self::find( $item, static fn ( $b ) => 'core/paragraph' === $b['blockName'], 1 ); if ( $p ) { $card['text'] = self::text( $p ); } $links = static function ( array $list ) { $out = array(); foreach ( (array) $list['innerBlocks'] as $li ) { $html = implode( '', array_filter( (array) $li['innerContent'], 'is_string' ) ); $row = array(); if ( preg_match( '#]*href="([^"]*)"[^>]*>(.*?)(?:\s*—\s*(.*?))?#su', $html, $m ) ) { $row['title'] = AIEO_Content_Inline::text( $m[2] ); if ( ! empty( $m[3] ) ) { $row['text'] = AIEO_Content_Inline::text( $m[3] ); } $row['href'] = html_entity_decode( $m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ); } else { $row['title'] = AIEO_Content_Inline::text( $html ); } $out[] = $row; } return $out; }; foreach ( (array) $item['innerBlocks'] as $b ) { if ( 'core/list' !== $b['blockName'] ) { continue; } $class = (string) ( $b['attrs']['className'] ?? '' ); if ( str_contains( $class, 'rooster-family-card__products' ) ) { $card['products'] = $links( $b ); } elseif ( str_contains( $class, 'is-style-chips' ) ) { $card['features'] = $links( $b ); } } $swatches = self::find( $item, static fn ( $b ) => 'core/group' === $b['blockName'] && self::has_class( $b, 'rooster-swatches', false ), 1 ); if ( $swatches ) { $card['swatches'] = array_values( array_filter( array_map( static fn ( $b ) => (string) ( $b['attrs']['style']['color']['background'] ?? '' ), (array) $swatches['innerBlocks'] ) ) ); } $motif = self::find( $item, static fn ( $b ) => 'core/pattern' === $b['blockName'], 1 ); if ( $motif ) { $card['motif'] = (string) ( $motif['attrs']['slug'] ?? '' ); } foreach ( self::buttons( $item ) as $button ) { if ( 'primary' === $button['style'] ) { $card['ctaText'] = $button['text']; } else { $card['docs'] = array( 'text' => $button['text'], 'href' => (string) ( $button['href'] ?? '' ) ); } } return $card; } private static function read_pricing( array $block, array &$media ) { $s = self::common( $block, 'pricing' ); $tiers = array(); foreach ( self::find_all( $block, static fn ( $b ) => 'rooster/plan:price' === self::shape( $b ) || self::has_class( $b, 'rooster-price-card', false ) ) as $item ) { $tier = self::price_card( $item ); if ( 'row' === ( $tier['layout'] ?? '' ) ) { unset( $tier['layout'] ); $s['enterprise'] = $tier; } else { $tiers[] = $tier; } } $s['tiers'] = $tiers; $includes = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && __( 'Includes', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ), 1 ); if ( $includes ) { $h = self::find( $includes, static fn ( $b ) => 'core/heading' === $b['blockName'], 1 ); $list = self::find( $includes, static fn ( $b ) => 'core/list' === $b['blockName'], 1 ); $s['includes'] = array( 'heading' => $h ? self::text( $h ) : '', 'items' => $list ? array_values( array_map( static fn ( $b ) => self::text( $b ), array_filter( (array) $list['innerBlocks'], static fn ( $b ) => 'core/list-item' === $b['blockName'] ) ) ) : array() ); } return $s; } private static function price_card( array $item ) { if ( 'rooster/plan:price' === self::shape( $item ) ) { $a = self::attrs( $item ); $tier = array( 'key' => sanitize_title( (string) ( $a['name'] ?? '' ) ), 'name' => (string) ( $a['name'] ?? '' ), 'price' => (string) ( $a['price'] ?? '' ) ); foreach ( array( 'tagline', 'period', 'note', 'ctaText', 'ctaHref', 'badge', 'anchor' ) as $k ) { if ( ! empty( $a[ $k ] ) ) { $tier[ $k ] = (string) $a[ $k ]; } } $tier['features'] = AIEO_Engine_Blocks::lines( (string) ( $a['features'] ?? '' ) ); if ( ! empty( $a['highlighted'] ) ) { $tier['highlighted'] = true; } if ( 'row' === (string) ( $a['layout'] ?? '' ) ) { $tier['layout'] = 'row'; } return $tier; } $heading = self::find( $item, static fn ( $b ) => 'core/heading' === $b['blockName'] ); $name = $heading ? self::text( $heading ) : ''; $tier = array( 'key' => sanitize_title( $name ), 'name' => $name, 'price' => '' ); $price = self::find( $item, static fn ( $b ) => 'core/paragraph' === $b['blockName'] && '3xl' === ( $b['attrs']['fontSize'] ?? '' ) ); if ( $price ) { $html = (string) preg_replace( '#^\s*]*>|

\s*$#su', '', implode( '', array_filter( (array) $price['innerContent'], 'is_string' ) ) ); if ( preg_match( '#^(.*?)]*>(.*?)#su', $html, $m ) ) { $tier['price'] = AIEO_Content_Inline::text( $m[1] ); $tier['period'] = AIEO_Content_Inline::text( $m[2] ); } else { $tier['price'] = self::text( $price ); } } $list = self::find( $item, static fn ( $b ) => 'core/list' === $b['blockName'] ); $features = array(); foreach ( $list ? (array) $list['innerBlocks'] : array() as $li ) { if ( 'core/list-item' === $li['blockName'] ) { $html = (string) preg_replace( '#.*?#su', '', implode( '', array_filter( (array) $li['innerContent'], 'is_string' ) ) ); $text = AIEO_Content_Inline::text( $html ); $excluded = str_contains( (string) ( $li['attrs']['className'] ?? '' ), 'is-excluded' ); $features[] = $excluded ? '-' . $text : ( preg_match( '/^Not included: (.+)$/u', $text, $m ) ? '-' . $m[1] : $text ); } } $tier['features'] = $features; $table = self::find( $item, static fn ( $b ) => 'core/table' === $b['blockName'], 1 ); if ( $table ) { $tier['volume'] = self::table_spec( $table ); $after = self::siblings( $item, $table ); if ( $after ) { [ $children, $i ] = $after; $prev = $children[ $i - 1 ] ?? null; if ( $prev && 'core/paragraph' === $prev['blockName'] && str_contains( (string) ( $prev['attrs']['className'] ?? '' ), 'rooster-price-card__volume-caption' ) ) { $tier['volume'] = array( 'caption' => self::text( $prev ) ) + $tier['volume']; } $next = $children[ $i + 1 ] ?? null; if ( $next && 'core/paragraph' === $next['blockName'] && 'xs' === (string) ( $next['attrs']['fontSize'] ?? '' ) ) { $tier['volume']['note'] = self::text( $next ); } } } $buttons = self::buttons( $item ); if ( $buttons ) { $tier['ctaText'] = $buttons[0]['text']; $tier['ctaHref'] = (string) ( $buttons[0]['href'] ?? '' ); } if ( 'contrast' === (string) ( $item['attrs']['backgroundColor'] ?? '' ) ) { $tier['highlighted'] = true; } if ( ! empty( $item['attrs']['anchor'] ) ) { $tier['anchor'] = (string) $item['attrs']['anchor']; } return $tier; } private static function table_spec( array $table ) { $html = implode( '', array_filter( (array) $table['innerContent'], 'is_string' ) ); $out = array( 'columns' => array(), 'rows' => array() ); if ( preg_match( '#(.*?)#su', $html, $m ) && preg_match_all( '#]*>(.*?)#su', $m[1], $cells ) ) { $out['columns'] = array_map( array( 'AIEO_Content_Inline', 'text' ), $cells[1] ); } if ( preg_match( '#(.*?)#su', $html, $m ) && preg_match_all( '#]*>(.*?)#su', $m[1], $rows ) ) { foreach ( $rows[1] as $row ) { preg_match_all( '#]*>(.*?)#su', $row, $cells ); $out['rows'][] = array_map( array( 'AIEO_Content_Inline', 'text' ), $cells[1] ); } } if ( preg_match( '#]*>(.*?)#su', $html, $m ) ) { $out['caption'] = AIEO_Content_Inline::text( $m[1] ); } return $out; } private static function read_table( array $block, array &$media ) { $s = self::common( $block, 'table' ); $table = self::find( $block, static fn ( $b ) => 'rooster/table:compare' === self::shape( $b ) ); if ( ! $table ) { $core = self::find( $block, static fn ( $b ) => 'core/table' === $b['blockName'] ); return $core ? $s + self::table_spec( $core ) : null; } $a = self::attrs( $table ); $s['columns'] = array_values( array_filter( array_map( 'trim', explode( '|', (string) ( $a['columns'] ?? '' ) ) ), 'strlen' ) ); $n = count( $s['columns'] ); $rows = array(); $groups = array(); $group = null; foreach ( AIEO_Engine_Blocks::lines( (string) ( $a['rows'] ?? '' ) ) as $line ) { $heading = '#' === substr( $line, 0, 1 ) ? array_map( 'trim', explode( '|', substr( $line, 1 ), 3 ) ) : null; $cells = $heading ? array( $heading[0] ) : array_map( 'trim', explode( '|', $line ) ); if ( ! array_filter( array_slice( $cells, 1 ), 'strlen' ) ) { unset( $group ); $groups[] = array( 'name' => $cells[0] ) + ( '' !== (string) ( $heading[1] ?? '' ) ? array( 'icon' => (string) $heading[1] ) : array() ) + ( '' !== (string) ( $heading[2] ?? '' ) ? array( 'tone' => (string) $heading[2] ) : array() ) + array( 'rows' => array() ); $group = &$groups[ count( $groups ) - 1 ]; continue; } $row = array( $cells[0] ); foreach ( array_slice( array_pad( $cells, $n, '' ), 1, $n - 1 ) as $c ) { if ( preg_match( '/^(yes|no|soon|partial)\s*\((.*)\)$/iu', $c, $m ) ) { $row[] = array( 'mark' => strtolower( $m[1] ), 'note' => trim( $m[2] ) ); } else { $row[] = $c; } } if ( isset( $group ) ) { $group['rows'][] = $row; } else { $rows[] = $row; } } unset( $group ); $s['rows'] = $rows; if ( $groups ) { $s['groups'] = $groups; } if ( ! empty( $a['marks'] ) ) { $s['marks'] = (string) $a['marks']; } $highlight = array_values( array_filter( array_map( 'intval', preg_split( '/[^0-9]+/', (string) ( $a['highlight'] ?? '' ), -1, PREG_SPLIT_NO_EMPTY ) ?: array() ) ) ); if ( $highlight ) { $s['highlight'] = 1 === count( $highlight ) ? $highlight[0] : $highlight; } if ( ! empty( $a['caption'] ) ) { $s['caption'] = (string) $a['caption']; } if ( ! empty( $a['label'] ) ) { $s['label'] = (string) $a['label']; } if ( ! empty( $a['note'] ) ) { $s['note'] = (string) $a['note']; } if ( ! empty( $a['phone'] ) && 'stack' !== $a['phone'] ) { $s['phone'] = (string) $a['phone']; } if ( ! empty( $a['roosterDefer']['enabled'] ) ) { $s['defer'] = true; } return $s; } private static function read_faq( array $block, array &$media ) { $s = self::common( $block, 'faq' ); $items = array(); foreach ( self::find_all( $block, static fn ( $b ) => 'core/accordion-item' === $b['blockName'] ) as $item ) { $heading = self::find( $item, static fn ( $b ) => 'core/accordion-heading' === $b['blockName'] ); $panel = self::find( $item, static fn ( $b ) => 'core/accordion-panel' === $b['blockName'] ); $q = ''; if ( $heading ) { $html = implode( '', array_filter( (array) $heading['innerContent'], 'is_string' ) ); $q = AIEO_Content_Inline::text( preg_match( '#toggle-title">(.*?)#su', $html, $m ) ? $m[1] : $html ); } $a = array(); foreach ( $panel ? (array) $panel['innerBlocks'] : array() as $p ) { if ( 'core/paragraph' === $p['blockName'] ) { $a[] = self::text( $p ); } elseif ( 'core/list' === $p['blockName'] ) { $a[] = self::list_text( $p ); } } $items[] = array( 'q' => $q, 'a' => implode( "\n\n", $a ) ); } $s['items'] = $items; return $s; } private static function read_cta( array $block, array &$media ) { $banner = self::find( $block, static fn ( $b ) => 'rooster/card:family' === self::shape( $b ) && 'banner' === ( $b['attrs']['layout'] ?? '' ), 3 ); if ( $banner ) { $a = (array) $banner['attrs']; $s = array( 'kind' => 'cta' ); $anchor = (string) ( $block['attrs']['anchor'] ?? '' ); if ( '' !== $anchor && $anchor !== (string) ( $block['attrs']['metadata']['rooster']['key'] ?? '' ) ) { $s['anchor'] = $anchor; } $s['style'] = 'card'; foreach ( array( 'name' => 'eyebrow', 'heading' => 'heading', 'family' => 'tone', 'text' => 'text' ) as $from => $to ) { if ( '' !== (string) ( $a[ $from ] ?? '' ) ) { $s[ $to ] = (string) $a[ $from ]; } } $card = self::family_block( $a ); if ( ! empty( $card['features'] ) ) { $s['items'] = array_map( static fn ( $f ) => (string) $f['title'], $card['features'] ); } if ( ! empty( $card['products'] ) ) { $s['buttons'] = array(); foreach ( $card['products'] as $i => $p ) { $s['buttons'][] = array( 'text' => (string) $p['title'], 'href' => (string) ( $p['href'] ?? '' ), 'style' => 0 === $i ? 'primary' : 'secondary' ); } } if ( '' !== (string) ( $a['motif'] ?? '' ) ) { $s['motif'] = (string) $a['motif']; } return $s; } $s = self::common( $block, 'cta' ); if ( isset( $s['lede'] ) ) { $s['text'] = $s['lede']; unset( $s['lede'] ); } $list = self::find( $block, static fn ( $b ) => 'core/list' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'is-style-checkmark-list' ), 2 ); if ( $list ) { $s['items'] = array_values( array_map( static fn ( $b ) => self::text( $b ), array_filter( (array) $list['innerBlocks'], static fn ( $b ) => 'core/list-item' === $b['blockName'] ) ) ); } $motif = self::find( $block, static fn ( $b ) => 'core/pattern' === $b['blockName'], 2 ); if ( $motif ) { $s['motif'] = (string) ( $motif['attrs']['slug'] ?? '' ); } return $s; } private static function read_stats( array $block, array &$media ) { $s = self::common( $block, 'stats' ); $s['items'] = self::stats_items( $block ); return $s; } private static function stats_items( array $root ) { $stats = array(); foreach ( self::find_all( $root, static fn ( $b ) => 'core/group' === $b['blockName'] && 'flex' === ( $b['attrs']['layout']['type'] ?? '' ) && 'vertical' === ( $b['attrs']['layout']['orientation'] ?? '' ) ) as $item ) { $paragraphs = array_values( array_filter( (array) $item['innerBlocks'], static fn ( $b ) => 'core/paragraph' === $b['blockName'] ) ); $row = array(); foreach ( $paragraphs as $p ) { $class = (string) ( $p['attrs']['className'] ?? '' ); $size = (string) ( $p['attrs']['fontSize'] ?? '' ); if ( str_contains( $class, 'rooster-stat__value' ) || ( '' === $class && '3xl' === $size ) ) { $row['value'] = self::text( $p ); } elseif ( str_contains( $class, 'rooster-stat__label' ) ) { $row['label'] = self::text( $p ); } elseif ( 'xs' === $size && ! isset( $row['value'] ) ) { $row['kicker'] = self::text( $p ); } elseif ( 'xs' === $size ) { $row['note'] = self::text( $p ); } elseif ( isset( $row['value'] ) && ! isset( $row['label'] ) ) { $row['label'] = self::text( $p ); } } if ( isset( $row['value'], $row['label'] ) ) { $stats[] = $row; } } return $stats; } private static function read_gallery( array $block, array &$media ) { $s = self::common( $block, 'gallery' ); $images = array(); foreach ( self::find_all( $block, static fn ( $b ) => 'core/image' === $b['blockName'] ) as $image ) { $images[] = self::image( $image, $media ); } $s['items'] = $images; $gallery = self::find( $block, static fn ( $b ) => 'core/gallery' === $b['blockName'], 2 ); if ( $gallery && ! empty( $gallery['attrs']['roosterDefer']['enabled'] ) ) { $s['defer'] = true; } return $s; } private static function read_split( array $block, array &$media ) { $s = self::common( $block, 'split' ); $columns = self::find( $block, static fn ( $b ) => 'core/columns' === $b['blockName'] ); $cols = $columns ? array_values( array_filter( (array) $columns['innerBlocks'], static fn ( $b ) => 'core/column' === $b['blockName'] ) ) : array( $block ); foreach ( $cols as $i => $col ) { $pattern = self::find( $col, static fn ( $b ) => 'core/pattern' === $b['blockName'], 2 ); if ( $pattern ) { $s['pattern'] = (string) ( $pattern['attrs']['slug'] ?? '' ); } $screens = self::find( $col, static fn ( $b ) => 'rooster/media:screens' === self::shape( $b ) ); if ( $screens ) { $s['image'] = self::screens( (array) $screens['attrs'], $media ); } if ( $pattern || $screens ) { if ( ! $pattern ) { $list = self::find( $col, static fn ( $b ) => 'core/list' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'is-style-checkmark-list' ) ); $s['items'] = $list ? array_values( array_map( static fn ( $b ) => self::text( $b ), array_filter( (array) $list['innerBlocks'], static fn ( $b ) => 'core/list-item' === $b['blockName'] ) ) ) : array(); } continue; } $image = self::find( $col, static fn ( $b ) => in_array( $b['blockName'], array( 'rooster/responsive-image', 'core/image' ), true ) ); $aside = self::find( $col, static fn ( $b ) => 'core/group' === $b['blockName'] && self::has_class( $b, 'rooster-aside', false ) ); if ( $image || $aside ) { if ( $image ) { $s['image'] = 'rooster/responsive-image' === $image['blockName'] ? self::responsive_image( $image, $media ) : self::image( $image, $media ); } if ( $aside ) { $s['aside'] = implode( "\n\n", array_map( static fn ( $b ) => self::text( $b ), self::find_all( $aside, static fn ( $b ) => 'core/paragraph' === $b['blockName'] ) ) ); } if ( 0 === $i && $columns ) { $s['side'] = 'left'; } continue; } $list = self::find( $col, static fn ( $b ) => 'core/list' === $b['blockName'] && str_contains( (string) ( $b['attrs']['className'] ?? '' ), 'is-style-checkmark-list' ) ); if ( $list ) { $s['items'] = array_values( array_map( static fn ( $b ) => self::text( $b ), array_filter( (array) $list['innerBlocks'], static fn ( $b ) => 'core/list-item' === $b['blockName'] ) ) ); } } return $s; } private static function screens( array $a, array &$media ) { $out = array(); foreach ( array( 'desktop' => array( 'desktopId', 'desktopAlt' ), 'mobile' => array( 'mobileId', 'mobileAlt' ) ) as $side => [ $id_key, $alt_key ] ) { $id = (int) ( $a[ $id_key ] ?? 0 ); if ( ! $id ) { continue; } $path = AIEO_Content_Media::key_of( $id ); if ( '' === $path ) { $path = (string) wp_get_attachment_url( $id ); } $alt = (string) ( $a[ $alt_key ] ?? '' ); $media[ $path ] = $alt; $meta = wp_get_attachment_metadata( $id ); $out[ $side ] = array_filter( array( 'src' => $path, 'alt' => $alt, 'width' => (int) ( $meta['width'] ?? 0 ), 'height' => (int) ( $meta['height'] ?? 0 ) ) ); } return $out; } private static function read_prose( array $block, array &$media ) { $s = self::common( $block, 'prose' ); $head = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && __( 'Section head', 'ai-eshop-optimizer' ) === ( $b['attrs']['metadata']['name'] ?? '' ), 2 ); $column = self::find( $block, static fn ( $b ) => 'core/group' === $b['blockName'] && 'constrained' === ( $b['attrs']['layout']['type'] ?? '' ) && empty( $b['attrs']['metadata']['name'] ), 1 ) ?: $block; $lines = array(); $raw = ''; foreach ( (array) $column['innerBlocks'] as $b ) { if ( $b === $head ) { continue; } $class = (string) ( $b['attrs']['className'] ?? '' ); if ( 'core/heading' === $b['blockName'] ) { $lines[] = str_repeat( '#', max( 2, min( 4, (int) ( $b['attrs']['level'] ?? 2 ) ) ) ) . ' ' . self::text( $b ); } elseif ( 'core/paragraph' === $b['blockName'] ) { if ( str_contains( $class, 'rooster-section__' ) || str_contains( $class, 'rooster-buttons__note' ) ) { continue; } $lines[] = self::text( $b ); } elseif ( 'core/list' === $b['blockName'] ) { $lines[] = self::list_text( $b ); } elseif ( 'core/buttons' !== $b['blockName'] ) { $raw .= AIEO_Content_Blocks::serialize( $b ); } } if ( $lines ) { $s['body'] = implode( "\n\n", $lines ); } if ( '' !== $raw ) { $s['markup'] = $raw; } return $s; } private static function read_form( array $block, array &$media ) { $found = 'aieo/contact-form' === $block['blockName'] ? $block : self::find( $block, static fn ( $b ) => 'aieo/contact-form' === $b['blockName'], 2 ); $s = 'aieo/contact-form' === $block['blockName'] ? array( 'kind' => 'form' ) : self::common( $block, 'form' ); $attrs = $found ? (array) $found['attrs'] : array(); $form = array(); foreach ( AIEO_Content_Sections::FORM_ATTRS as $field => $attr ) { if ( isset( $attrs[ $attr ] ) && '' !== (string) $attrs[ $attr ] ) { $form[ $field ] = 'level' === $field ? (int) $attrs[ $attr ] : (string) $attrs[ $attr ]; } } $s['form'] = $form; return $s; } private static function read_pattern( array $block, array &$media ) { $pattern = 'core/pattern' === $block['blockName'] ? $block : self::find( $block, static fn ( $b ) => 'core/pattern' === $b['blockName'], 2 ); $s = 'core/pattern' === $block['blockName'] ? array( 'kind' => 'pattern' ) : self::common( $block, 'pattern' ); $s['pattern'] = $pattern ? (string) ( $pattern['attrs']['slug'] ?? '' ) : ''; return $s; } private static function responsive_image( array $block, array &$media ) { $a = self::attrs( $block ); $out = array(); foreach ( array( 'desktop' => 'desktopId', 'mobile' => 'mobileId' ) as $side => $attr ) { $id = (int) ( $a[ $attr ] ?? 0 ); if ( ! $id ) { continue; } $path = AIEO_Content_Media::key_of( $id ); if ( '' === $path ) { $path = (string) wp_get_attachment_url( $id ); } $out[ $side ] = $path; $alt = ! empty( $a['decorative'] ) ? '' : (string) ( $a[ 'desktop' === $side ? 'desktopAlt' : 'mobileAlt' ] ?? get_post_meta( $id, '_wp_attachment_image_alt', true ) ); if ( ! isset( $media[ $path ] ) ) { $media[ $path ] = $alt; } if ( 'desktop' === $side && '' !== $alt ) { $out['alt'] = $alt; } elseif ( 'mobile' === $side && '' !== $alt && $alt !== $media[ $path ] ) { $out['mobile'] = array( 'src' => $path, 'alt' => $alt ); } } return $out; } private static function image( array $block, array &$media ) { $id = (int) ( $block['attrs']['id'] ?? 0 ); $html = implode( '', array_filter( (array) $block['innerContent'], 'is_string' ) ); $alt = preg_match( '#\balt="([^"]*)"#', $html, $m ) ? html_entity_decode( $m[1], ENT_QUOTES | ENT_HTML5, 'UTF-8' ) : ''; $path = $id ? AIEO_Content_Media::key_of( $id ) : ''; if ( '' === $path ) { $path = $id ? (string) wp_get_attachment_url( $id ) : ( preg_match( '#\bsrc="([^"]*)"#', $html, $m ) ? $m[1] : '' ); } if ( ! isset( $media[ $path ] ) ) { $media[ $path ] = $alt; } $out = array( 'src' => $path, 'alt' => $alt ); if ( preg_match( '#]*>(.*?)#su', $html, $m ) ) { $out['caption'] = AIEO_Content_Inline::text( $m[1] ); } if ( ! empty( $block['attrs']['title'] ) ) { $out['title'] = (string) $block['attrs']['title']; } return $out; } private static function attrs( array $block ) { $attrs = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array(); $type = WP_Block_Type_Registry::get_instance()->get_registered( (string) $block['blockName'] ); if ( $type && is_array( $type->attributes ) ) { foreach ( $type->attributes as $key => $schema ) { if ( ! array_key_exists( $key, $attrs ) && is_array( $schema ) && array_key_exists( 'default', $schema ) ) { $attrs[ $key ] = $schema['default']; } } } return $attrs; } private static function is_default( $block, $key, $value ) { $type = WP_Block_Type_Registry::get_instance()->get_registered( $block ); return $type && isset( $type->attributes[ $key ] ) && array_key_exists( 'default', $type->attributes[ $key ] ) && $type->attributes[ $key ]['default'] === $value; } private static function text( array $block ) { $html = implode( '', array_filter( (array) $block['innerContent'], 'is_string' ) ); if ( str_contains( $html, AIEO_Content_Sections::NOTICE_PILL ) ) { $html = (string) preg_replace( '#.*?\s?#su', '', $html, 1 ); } return AIEO_Content_Inline::text( $html ); } private static function heading_text( array $block ) { return AIEO_Content_Inline::heading_text( implode( '', array_filter( (array) $block['innerContent'], 'is_string' ) ) ); } private static function names( array $block ) { $names = array(); foreach ( (array) $block['innerBlocks'] as $child ) { $names[] = self::shape( $child ); $names = array_merge( $names, self::names( $child ) ); } return $names; } private static function has_class( array $block, $class, $deep ) { if ( str_contains( (string) ( $block['attrs']['className'] ?? '' ), $class ) ) { return true; } if ( ! $deep ) { return false; } foreach ( (array) $block['innerBlocks'] as $child ) { if ( self::has_class( $child, $class, true ) ) { return true; } } return false; } private static function find( array $block, $test, $depth = 8 ) { foreach ( (array) $block['innerBlocks'] as $child ) { if ( $test( $child ) ) { return $child; } if ( $depth > 1 ) { $found = self::find( $child, $test, $depth - 1 ); if ( $found ) { return $found; } } } return null; } private static function find_all( array $block, $test, $stop = null ) { $out = array(); foreach ( (array) $block['innerBlocks'] as $child ) { if ( $test( $child ) ) { $out[] = $child; continue; } if ( $stop && $stop( $child ) ) { continue; } $out = array_merge( $out, self::find_all( $child, $test, $stop ) ); } return $out; } private static function siblings( array $root, array $target ) { $children = (array) $root['innerBlocks']; foreach ( $children as $i => $child ) { if ( $child === $target ) { return array( $children, $i ); } $found = self::siblings( $child, $target ); if ( null !== $found ) { return $found; } } return null; } }