0, 'bottom' => 9, 'mobile' => array( 'top' => 0, 'bottom' => 8 ) ); const PADDING_LAST = array( 'top' => 0, 'bottom' => 0, 'mobile' => array( 'top' => 0, 'bottom' => 0 ) ); public static function is( $item ) { return is_array( $item ) && self::NAME === (string) ( $item['block'] ?? '' ); } public static function holds( array $s ) { foreach ( is_array( $s['blocks'] ?? null ) ? $s['blocks'] : array() as $item ) { if ( self::is( $item ) ) { return true; } } return false; } public static function container_id( array $item, array $s ) { $anchor = sanitize_title( (string) ( $item['anchor'] ?? '' ) ); return '' !== $anchor ? $anchor : AIEO_Content_Sections::anchor( $s + array( 'key' => '' ) ) . '-tabs'; } public static function tabs( array $item ) { return array_values( array_filter( is_array( $item['tabs'] ?? null ) ? $item['tabs'] : array(), 'is_array' ) ); } public static function sections( array $tab ) { return array_values( array_filter( is_array( $tab['sections'] ?? null ) ? $tab['sections'] : array(), 'is_array' ) ); } public static function panel_ids( array $item, array $s ) { $id = self::container_id( $item, $s ); $out = array(); foreach ( self::tabs( $item ) as $n => $tab ) { $anchor = sanitize_title( (string) ( $tab['anchor'] ?? '' ) ); $out[] = '' !== $anchor ? $anchor : $id . '-' . ( $n + 1 ); } return $out; } public static function aliases( array $tab ) { $own = sanitize_title( (string) ( $tab['anchor'] ?? '' ) ); $ids = array_map( static fn ( $a ) => sanitize_title( (string) $a ), is_array( $tab['aliases'] ?? null ) ? $tab['aliases'] : array() ); return array_values( array_unique( array_filter( $ids, static fn ( $id ) => '' !== $id && $id !== $own ) ) ); } public static function carrier( array $sections ) { foreach ( array_values( $sections ) as $i => $n ) { if ( is_array( $n ) && AIEO_Content_Layout::applies( $n ) && 'hero' !== (string) ( $n['kind'] ?? '' ) ) { return $i; } } return -1; } public static function padding( $last ) { return $last ? self::PADDING_LAST : self::PADDING; } public static function defer_default( $kind, $first ) { return ! $first && in_array( (string) $kind, AIEO_Content_Sections::DEFERRABLE, true ); } public static function nested( array $n, array $s, $first, $last, $prev = null ) { if ( ! array_key_exists( 'defer', $n ) ) { $n['defer'] = self::defer_default( (string) ( $n['kind'] ?? '' ), $first ); } $n['padding'] = array_replace_recursive( self::padding( $last ), is_array( $n['padding'] ?? null ) ? $n['padding'] : array() ); $n['_nested'] = true; foreach ( array( '_display', '_family' ) as $field ) { if ( isset( $s[ $field ] ) ) { $n[ $field ] = $s[ $field ]; } } if ( is_array( $prev ) ) { $n['_prev_kind'] = (string) ( $prev['kind'] ?? '' ); $n['_prev_layout'] = (string) ( $prev['layout'] ?? '' ); } return $n; } public static function compile( array $item, array $s ) { $media = is_array( $s['_media'] ?? null ) ? $s['_media'] : array(); $attrs = array(); $label = AIEO_Content_Inline::plain( trim( (string) ( $item['label'] ?? '' ) ) ); if ( '' === $label ) { $label = AIEO_Content_Inline::heading_plain( (string) ( $s['heading'] ?? '' ) ); } if ( '' !== $label ) { $attrs['label'] = $label; } $tone = sanitize_key( (string) ( $item['tone'] ?? $s['tone'] ?? '' ) ); if ( '' !== $tone ) { $attrs['tone'] = $tone; } $attrs['anchor'] = self::container_id( $item, $s ); $panels = array(); foreach ( self::tabs( $item ) as $n => $tab ) { $panels[] = self::tab( $tab, 0 === $n, $s, $media ); } if ( ! $panels ) { return ''; } return AIEO_Content_Blocks::comment( 'rooster/tabs', $attrs, implode( "\n\n", $panels ) ); } private static function tab( array $tab, $first, array $s, array $media ) { $list = self::sections( $tab ); $carrier = self::carrier( $list ); $aliases = self::aliases( $tab ); $out = array(); $prev = null; $count = count( $list ); $run = array(); foreach ( $list as $i => $n ) { if ( 'hero' === (string) ( $n['kind'] ?? '' ) || self::holds( $n ) ) { continue; } if ( $i === $carrier && $aliases ) { $n['aliases'] = array_values( array_merge( is_array( $n['aliases'] ?? null ) ? $n['aliases'] : array(), $aliases ) ); } $nested = self::nested( $n, $s, $first, $i === $count - 1, $prev ); $whole = ! $first && ! empty( $nested['defer'] ); if ( $whole ) { $nested['defer'] = false; } $markup = AIEO_Content_Sections::render( $nested, $media ); $prev = $n; if ( '' === $markup ) { continue; } if ( $whole ) { $run[] = array( $markup, $n ); continue; } if ( $run ) { $out[] = self::run( $run, $tab ); $run = array(); } $out[] = $markup; } if ( $run ) { $out[] = self::run( $run, $tab ); } $label = AIEO_Content_Inline::plain( trim( (string) ( $tab['label'] ?? '' ) ) ); $attrs = array(); if ( '' !== $label ) { $attrs['label'] = $label; } foreach ( array( 'icon', 'tone' ) as $field ) { $value = sanitize_key( (string) ( $tab[ $field ] ?? '' ) ); if ( '' !== $value ) { $attrs[ $field ] = $value; } } $anchor = sanitize_title( (string) ( $tab['anchor'] ?? '' ) ); if ( '' !== $anchor ) { $attrs['anchor'] = $anchor; } $own = array_filter( array( 'key' => sanitize_key( (string) ( $tab['key'] ?? '' ) ), 'aliases' => $aliases ) ); $attrs['metadata'] = array( 'name' => 'Tab: ' . $label ) + ( $own ? array( 'rooster' => $own ) : array() ); return AIEO_Content_Blocks::comment( 'rooster/tab', $attrs, implode( "\n\n", $out ) ); } const DEFERRED = 'rooster-tabs__deferred'; const HEAD_HEIGHT = 160; private static function run( array $run, array $tab ) { $height = 0; foreach ( $run as [ , $n ] ) { $height += self::HEAD_HEIGHT + (int) ( AIEO_Content_Sections::DEFER_HEIGHT[ (string) ( $n['kind'] ?? '' ) ] ?? '320px' ); } $label = AIEO_Content_Inline::heading_plain( (string) ( $run[0][1]['heading'] ?? '' ) ); if ( '' === $label ) { $label = AIEO_Content_Inline::plain( trim( (string) ( $tab['label'] ?? '' ) ) ); } return AIEO_Content_Blocks::group( array( 'metadata' => array( 'name' => __( 'Deferred sections', 'ai-eshop-optimizer' ) ), 'className' => self::DEFERRED, 'roosterDefer' => array( 'enabled' => true, 'height' => $height . 'px' ) + ( '' !== $label ? array( 'label' => $label ) : array() ), ), implode( "\n\n", array_column( $run, 0 ) ) ); } }