'; } return $open . " -->\n" . $inner . "\n'; } public static function serialize( array $block ) { $inner = ''; $index = 0; foreach ( (array) ( $block['innerContent'] ?? array() ) as $chunk ) { if ( is_string( $chunk ) ) { $inner .= $chunk; } elseif ( isset( $block['innerBlocks'][ $index ] ) ) { $inner .= self::serialize( (array) $block['innerBlocks'][ $index++ ] ); } } $name = (string) ( $block['blockName'] ?? '' ); if ( '' === $name ) { return $inner; } $name = 0 === strpos( $name, 'core/' ) ? substr( $name, 5 ) : $name; $attrs = is_array( $block['attrs'] ?? null ) && $block['attrs'] ? ' ' . self::attributes( $block['attrs'] ) : ''; if ( empty( $block['innerBlocks'] ) && '' === $inner ) { return ''; } return '' . $inner . ''; } public static function block( $name, array $attrs = array() ) { $full = 'rooster/' . $name; $attrs = self::library_defaults( (string) $name, self::without_defaults( $full, $attrs ) ); if ( ! WP_Block_Type_Registry::get_instance()->is_registered( $full ) && isset( self::STANDINS[ $name . ':' . ( $attrs['variant'] ?? '' ) ] ) ) { $name = self::STANDINS[ $name . ':' . ( $attrs['variant'] ?? '' ) ]; unset( $attrs['variant'] ); $attrs = array_map( static fn ( $v ) => is_string( $v ) ? AIEO_Content_Inline::plain( $v ) : $v, $attrs ); if ( 'compare-table' === $name && isset( $attrs['highlight'] ) ) { $attrs['highlight'] = (int) ( preg_split( '/[^0-9]+/', (string) $attrs['highlight'], -1, PREG_SPLIT_NO_EMPTY )[0] ?? 0 ); } } if ( WP_Block_Type_Registry::get_instance()->is_registered( $full ) ) { return self::comment( $full, $attrs ); } if ( function_exists( 'rooster_block' ) ) { $theme = (string) rooster_block( $name, $attrs ); if ( '' !== $theme ) { return $theme; } } if ( class_exists( 'Rooster\Core\Patterns' ) ) { $theme = (string) \Rooster\Core\Patterns::block( $name, $attrs ); if ( '' !== $theme ) { return $theme; } } return self::standin( $name, $attrs ); } const BUTTON_ICONS = array( 'arrow-right' => 'arrow' ); const BUTTON_ICON_CLASS = 'rooster-button-icon-'; const BUTTON_NOTE_CLASS = 'rooster-button-note'; const SOLID_TEXT = array( '#ffffff', '#111111' ); public static function solid_text( $background ) { $luminance = static function ( $hex ) { $hex = ltrim( (string) $hex, '#' ); if ( 3 === strlen( $hex ) ) { $hex = $hex[0] . $hex[0] . $hex[1] . $hex[1] . $hex[2] . $hex[2]; } $sum = 0.0; foreach ( array( 0.2126, 0.7152, 0.0722 ) as $i => $weight ) { $c = hexdec( substr( $hex, 2 * $i, 2 ) ) / 255; $sum += $weight * ( $c <= 0.04045 ? $c / 12.92 : ( ( $c + 0.055 ) / 1.055 ) ** 2.4 ); } return $sum; }; $contrast = static function ( $a, $b ) use ( $luminance ) { $la = $luminance( $a ); $lb = $luminance( $b ); return ( max( $la, $lb ) + 0.05 ) / ( min( $la, $lb ) + 0.05 ); }; $hex = (string) ( sanitize_hex_color( (string) $background ) ?? '' ); if ( '' === $hex ) { return self::SOLID_TEXT[0]; } [ $white, $dark ] = self::SOLID_TEXT; if ( $contrast( $hex, $white ) >= 4.5 ) { return $white; } return $contrast( $hex, $dark ) >= $contrast( $hex, $white ) ? $dark : $white; } public static function button_icon_style( $icon ) { $name = (string) ( self::BUTTON_ICONS[ (string) $icon ] ?? '' ); if ( '' === $name || ! class_exists( 'WP_Block_Styles_Registry' ) || ! WP_Block_Styles_Registry::get_instance()->is_registered( 'core/button', $name ) ) { return ''; } return 'is-style-' . $name; } public static function button_icon_class( $icon ) { $icon = sanitize_key( (string) $icon ); return '' === $icon || isset( self::BUTTON_ICONS[ $icon ] ) ? '' : self::BUTTON_ICON_CLASS . $icon; } public static function is_external( $href ) { $host = (string) wp_parse_url( (string) $href, PHP_URL_HOST ); return '' !== $host && strtolower( $host ) !== strtolower( (string) wp_parse_url( home_url( '/' ), PHP_URL_HOST ) ); } const STANDINS = array( 'card:feature' => 'feature-card', 'card:' => 'feature-card', 'plan:price' => 'price-card', 'plan:' => 'price-card', 'table:compare' => 'compare-table', 'table:' => 'compare-table', ); public static function library_defaults( $name, array $attrs ) { if ( ! class_exists( 'AIEO_Engine_Blocks', false ) || ! isset( AIEO_Engine_Blocks::LIBRARY[ $name ] ) ) { return $attrs; } $variants = AIEO_Engine_Blocks::LIBRARY[ $name ]; $variant = isset( $attrs['variant'] ) && isset( $variants[ $attrs['variant'] ] ) ? (string) $attrs['variant'] : (string) array_key_first( $variants ); if ( isset( $attrs['layout'] ) && '' !== (string) $variants[ $variant ] && (string) $variants[ $variant ] === (string) $attrs['layout'] ) { unset( $attrs['layout'] ); } if ( isset( $attrs['variant'] ) && (string) array_key_first( $variants ) === (string) $attrs['variant'] ) { unset( $attrs['variant'] ); } return $attrs; } public static function without_defaults( $block, array $attrs ) { $type = WP_Block_Type_Registry::get_instance()->get_registered( $block ); if ( ! $type || ! is_array( $type->attributes ) ) { return $attrs; } foreach ( $attrs as $key => $value ) { if ( isset( $type->attributes[ $key ] ) && array_key_exists( 'default', $type->attributes[ $key ] ) && $type->attributes[ $key ]['default'] === $value ) { unset( $attrs[ $key ] ); } } return $attrs; } public static function css_var( $value ) { $value = (string) $value; if ( 0 === strpos( $value, 'var:' ) ) { return 'var(--wp--' . str_replace( '|', '--', substr( $value, 4 ) ) . ')'; } return $value; } public static function style_attr( array $style ) { $out = array(); if ( isset( $style['color']['text'] ) ) { $out[] = 'color:' . $style['color']['text']; } if ( isset( $style['color']['gradient'] ) ) { $out[] = 'background:' . $style['color']['gradient']; } elseif ( isset( $style['color']['background'] ) ) { $out[] = 'background-color:' . $style['color']['background']; } foreach ( array( 'margin', 'padding' ) as $box ) { foreach ( (array) ( $style['spacing'][ $box ] ?? array() ) as $side => $value ) { $out[] = $box . '-' . $side . ':' . self::css_var( $value ); } } foreach ( array( 'top', 'right', 'bottom', 'left' ) as $edge ) { foreach ( array( 'color', 'style', 'width' ) as $part ) { if ( isset( $style['border'][ $edge ][ $part ] ) ) { $out[] = 'border-' . $edge . '-' . $part . ':' . self::css_var( $style['border'][ $edge ][ $part ] ); } } } if ( isset( $style['border']['radius'] ) && is_scalar( $style['border']['radius'] ) ) { $out[] = 'border-radius:' . self::css_var( $style['border']['radius'] ); } if ( isset( $style['border']['color'] ) ) { $out[] = 'border-color:' . self::css_var( $style['border']['color'] ); } if ( isset( $style['border']['style'] ) ) { $out[] = 'border-style:' . $style['border']['style']; } if ( isset( $style['border']['width'] ) ) { $out[] = 'border-width:' . $style['border']['width']; } foreach ( array( 'fontSize' => 'font-size', 'fontWeight' => 'font-weight', 'letterSpacing' => 'letter-spacing', 'lineHeight' => 'line-height', 'fontStyle' => 'font-style', 'textTransform' => 'text-transform' ) as $key => $prop ) { if ( isset( $style['typography'][ $key ] ) ) { $out[] = $prop . ':' . self::css_var( $style['typography'][ $key ] ); } } if ( isset( $style['dimensions']['minHeight'] ) ) { $out[] = 'min-height:' . $style['dimensions']['minHeight']; } return implode( ';', $out ); } public static function support_classes( array $attrs ) { $classes = array(); if ( empty( $attrs['backgroundColor'] ) && ( ! empty( $attrs['style']['color']['background'] ) || ! empty( $attrs['style']['color']['gradient'] ) ) ) { $classes[] = 'has-background'; } if ( empty( $attrs['textColor'] ) && ! empty( $attrs['style']['color']['text'] ) ) { $classes[] = 'has-text-color'; } if ( ! empty( $attrs['backgroundColor'] ) || ! empty( $attrs['textColor'] ) ) { if ( ! empty( $attrs['textColor'] ) ) { $classes[] = 'has-' . $attrs['textColor'] . '-color'; } if ( ! empty( $attrs['backgroundColor'] ) ) { $classes[] = 'has-' . $attrs['backgroundColor'] . '-background-color'; } if ( ! empty( $attrs['textColor'] ) ) { $classes[] = 'has-text-color'; } if ( ! empty( $attrs['backgroundColor'] ) ) { $classes[] = 'has-background'; } } if ( ! empty( $attrs['style']['border']['color'] ) || ! empty( $attrs['borderColor'] ) ) { $classes[] = 'has-border-color'; } if ( ! empty( $attrs['borderColor'] ) ) { $classes[] = 'has-' . $attrs['borderColor'] . '-border-color'; } if ( ! empty( $attrs['fontSize'] ) ) { $classes[] = 'has-' . preg_replace( '/(\d)([a-z])/i', '$1-$2', (string) $attrs['fontSize'] ) . '-font-size'; } return $classes; } public static function html_attrs( array $classes, $style = '', array $extra = array() ) { $classes = array_values( array_filter( array_map( 'trim', $classes ), 'strlen' ) ); $out = $classes ? ' class="' . esc_attr( implode( ' ', $classes ) ) . '"' : ''; foreach ( $extra as $name => $value ) { if ( '' !== (string) $value ) { $out .= ' ' . $name . '="' . esc_attr( (string) $value ) . '"'; } } if ( '' !== $style ) { $out .= ' style="' . esc_attr( $style ) . '"'; } return $out; } public static function group( array $attrs, $inner ) { $classes = array_merge( array( 'wp-block-group', ! empty( $attrs['align'] ) ? 'align' . $attrs['align'] : '', (string) ( $attrs['className'] ?? '' ) ), self::support_classes( $attrs ) ); $html = ' (string) ( $attrs['anchor'] ?? '' ) ) ) . '>' . $inner . ''; return self::comment( 'core/group', $attrs, $html ); } public static function heading( array $attrs, $html ) { $level = (int) ( $attrs['level'] ?? 2 ); $classes = array_merge( array( 'wp-block-heading', ! empty( $attrs['textAlign'] ) ? 'has-text-align-' . $attrs['textAlign'] : '', ! empty( $attrs['align'] ) ? 'align' . $attrs['align'] : '', (string) ( $attrs['className'] ?? '' ) ), self::support_classes( $attrs ) ); $tag = 'h' . min( 6, max( 1, $level ) ); return self::comment( 'core/heading', $attrs, '<' . $tag . self::html_attrs( $classes, self::style_attr( (array) ( $attrs['style'] ?? array() ) ), array( 'id' => (string) ( $attrs['anchor'] ?? '' ) ) ) . '>' . $html . '' ); } public static function paragraph( array $attrs, $html ) { $classes = array_merge( array( ! empty( $attrs['align'] ) ? 'has-text-align-' . $attrs['align'] : '', (string) ( $attrs['className'] ?? '' ) ), self::support_classes( $attrs ) ); return self::comment( 'core/paragraph', $attrs, ' (string) ( $attrs['anchor'] ?? '' ) ) ) . '>' . $html . '

' ); } public static function buttons( array $attrs, array $buttons ) { $inner = ''; foreach ( $buttons as $button ) { $text = trim( (string) ( $button['text'] ?? '' ) ); $href = trim( (string) ( $button['href'] ?? '' ) ); if ( '' === $text ) { continue; } $b_attrs = array(); $class = array(); if ( 'outline' === (string) ( $button['style'] ?? '' ) ) { $class[] = 'is-style-outline'; } elseif ( ! empty( $button['icon'] ) && '' !== self::button_icon_style( (string) $button['icon'] ) ) { $class[] = self::button_icon_style( (string) $button['icon'] ); } if ( ! empty( $button['icon'] ) && '' !== self::button_icon_class( (string) $button['icon'] ) ) { $class[] = self::button_icon_class( (string) $button['icon'] ); } $note = trim( (string) ( $button['note'] ?? '' ) ); if ( '' !== $note ) { $class[] = self::BUTTON_NOTE_CLASS; } if ( ! empty( $button['class'] ) ) { $class[] = (string) $button['class']; } if ( $class ) { $b_attrs['className'] = implode( ' ', $class ); } if ( ! empty( $button['solid'] ) ) { $b_attrs['style'] = array( 'color' => array( 'text' => self::solid_text( (string) $button['solid'] ), 'background' => (string) $button['solid'] ) ); } if ( ! empty( $button['backgroundColor'] ) ) { $b_attrs['backgroundColor'] = (string) $button['backgroundColor']; } if ( ! empty( $button['textColor'] ) ) { $b_attrs['textColor'] = (string) $button['textColor']; } if ( 'large' === (string) ( $button['size'] ?? '' ) ) { $edge = 'outline' === (string) ( $button['style'] ?? '' ) ? 'calc(0.75rem - 1px)' : '0.75rem'; $b_attrs['fontSize'] = 'md'; $b_attrs['style'] = (array) ( $b_attrs['style'] ?? array() ); if ( 'outline' === (string) ( $button['style'] ?? '' ) && ! empty( $button['wash'] ) ) { $b_attrs['style']['color'] = array( 'background' => 'color-mix(in srgb, var(--wp--preset--color--fg) 5%, transparent)' ); } $b_attrs['style'] += array( 'border' => array( 'radius' => '0.75rem' ), 'spacing' => array( 'padding' => array( 'left' => 'var:preset|spacing|5', 'right' => 'var:preset|spacing|5', 'top' => $edge, 'bottom' => $edge ) ), 'typography' => array( 'lineHeight' => '1.5' ), ); } if ( ! empty( $button['fontSize'] ) ) { $b_attrs['fontSize'] = (string) $button['fontSize']; } if ( ! empty( $button['attrs'] ) && is_array( $button['attrs'] ) ) { $b_attrs = array_replace_recursive( $b_attrs, $button['attrs'] ); } $extra = array( 'href' => '' !== $href ? esc_url( $href ) : '' ); if ( ! empty( $button['external'] ) ) { $b_attrs['linkTarget'] = '_blank'; $b_attrs['rel'] = 'noreferrer noopener'; $extra['target'] = '_blank'; $extra['rel'] = 'noreferrer noopener'; } $wrap = array( 'wp-block-button', (string) ( $b_attrs['className'] ?? '' ) ); $link = array_merge( array( 'wp-block-button__link' ), self::support_classes( $b_attrs ), self::button_save_classes( $b_attrs ), array( 'wp-element-button' ) ); $a = '' . esc_html( $text ) . ( '' !== $note ? '
' . esc_html( $note ) : '' ) . ''; $inner .= self::comment( 'core/button', $b_attrs, '' . $a . '' ); } if ( '' === $inner ) { return ''; } $size = ! empty( $attrs['fontSize'] ) || ! empty( $attrs['style']['typography']['fontSize'] ) ? 'has-custom-font-size' : ''; return self::comment( 'core/buttons', $attrs, '' . $inner . '' ); } private static function button_save_classes( array $attrs ) { $classes = array(); if ( isset( $attrs['style']['border']['radius'] ) && 0 === $attrs['style']['border']['radius'] ) { $classes[] = 'no-border-radius'; } if ( ! empty( $attrs['fontSize'] ) || ! empty( $attrs['style']['typography']['fontSize'] ) ) { $classes[] = 'has-custom-font-size'; } return $classes; } public static function list_block( array $attrs, array $items, $ordered = false ) { $inner = ''; foreach ( $items as $item ) { $inner .= self::comment( 'core/list-item', array(), '
  • ' . $item . '
  • ' ); } $tag = $ordered ? 'ol' : 'ul'; $classes = array_merge( array( 'wp-block-list', (string) ( $attrs['className'] ?? '' ) ), self::support_classes( $attrs ) ); if ( $ordered ) { $attrs = array( 'ordered' => true ) + $attrs; } return self::comment( 'core/list', $attrs, '<' . $tag . self::html_attrs( $classes ) . '>' . $inner . '' ); } public static function image( $id, $url, $alt, array $extra = array() ) { $attrs = array( 'id' => (int) $id, 'sizeSlug' => 'large', 'linkDestination' => 'none' ) + $extra; $img = '' . esc_attr( (string) $alt ) . ''; return self::comment( 'core/image', $attrs, '
    ' . $img . '
    ' ); } public static function icon( $slug, $width = '32px', $class = '' ) { $attrs = array( 'icon' => 'rooster/' . sanitize_key( (string) $slug ) ); if ( '' !== $class ) { $attrs['className'] = $class; } $attrs['style'] = array( 'dimensions' => array( 'width' => $width ) ); return self::comment( 'core/icon', $attrs ); } public static function details( $summary, $inner, array $extra = array() ) { $attrs = array( 'summary' => (string) $summary ) + $extra; return self::comment( 'core/details', $attrs, '
    ' . esc_html( (string) $summary ) . '' . $inner . '
    ' ); } public static function table( array $columns, array $rows, $caption = '', $class = '' ) { $head = ''; foreach ( $columns as $column ) { $head .= '' . esc_html( (string) $column ) . ''; } $head .= ''; $body = ''; foreach ( $rows as $row ) { $body .= ''; foreach ( (array) $row as $cell ) { $body .= '' . esc_html( (string) $cell ) . ''; } $body .= ''; } $html = '
    ' . $head . '' . $body . '
    ' . ( '' !== (string) $caption ? '
    ' . esc_html( (string) $caption ) . '
    ' : '' ) . '
    '; $attrs = array( 'hasFixedLayout' => false ); if ( '' !== $class ) { $attrs['className'] = $class; } return self::comment( 'core/table', $attrs, $html ); } public static function standin( $name, array $attrs ) { switch ( $name ) { case 'feature-card': return self::standin_feature_card( $attrs ); case 'price-card': return self::standin_price_card( $attrs ); case 'compare-table': return self::standin_compare_table( $attrs ); } return ''; } private static function standin_heading( $text, $level, $size ) { return self::heading( array( 'level' => (int) $level, 'fontSize' => $size ), esc_html( (string) $text ) ); } private static function standin_button( $text, $href, $outline = false ) { return self::buttons( array( 'layout' => array( 'type' => 'flex', 'justifyContent' => 'left' ) ), array( array( 'text' => (string) $text, 'href' => (string) $href, 'style' => $outline ? 'outline' : 'primary' ) ) ); } private static function standin_feature_card( array $a ) { $title = trim( (string) ( $a['title'] ?? '' ) ); $text = trim( (string) ( $a['text'] ?? '' ) ); if ( '' === $title && '' === $text ) { return ''; } $row = 'row' === (string) ( $a['layout'] ?? 'stack' ); $href = trim( (string) ( $a['href'] ?? '' ) ); $inner = ''; if ( '' !== $title ) { $inner .= self::standin_heading( $title, (int) ( $a['level'] ?? 3 ), 'lg' ); } if ( '' !== $text ) { $inner .= self::paragraph( array( 'fontSize' => 'sm', 'textColor' => 'muted' ), AIEO_Content_Inline::html( $text ) ); } if ( '' !== $href ) { $cta = trim( (string) ( $a['ctaText'] ?? '' ) ); $inner .= self::standin_button( '' !== $cta ? $cta : __( 'Learn more', 'ai-eshop-optimizer' ), $href, true ); } return self::group( array( 'className' => 'is-style-card rooster-feature-card', 'style' => array( 'spacing' => array( 'blockGap' => 'var:preset|spacing|3' ) ), 'layout' => $row ? array( 'type' => 'flex', 'flexWrap' => 'nowrap', 'verticalAlignment' => 'top' ) : array( 'type' => 'flex', 'orientation' => 'vertical', 'flexWrap' => 'nowrap' ), ), $inner ); } private static function standin_price_card( array $a ) { $name = trim( (string) ( $a['name'] ?? '' ) ); if ( '' === $name ) { return ''; } $highlighted = ! empty( $a['highlighted'] ); $inner = self::standin_heading( $name, 3, 'lg' ); $price = trim( (string) ( $a['price'] ?? '' ) ); $period = trim( (string) ( $a['period'] ?? '' ) ); $note = trim( (string) ( $a['note'] ?? '' ) ); if ( '' !== $price || '' !== $note ) { $line = esc_html( $price ); if ( '' !== $period ) { $line .= ' ' . esc_html( $period ) . ''; } if ( '' !== $note ) { $line .= ( '' !== $price ? '
    ' : '' ) . '' . AIEO_Content_Inline::html( $note ) . ''; } $inner .= self::paragraph( array( 'fontSize' => '3xl', 'style' => array( 'typography' => array( 'fontWeight' => '700', 'lineHeight' => '1' ) ) ), $line ); } $features = array_values( array_filter( array_map( 'trim', preg_split( '/\r\n|\r|\n/', (string) ( $a['features'] ?? '' ) ) ?: array() ), 'strlen' ) ); if ( $features ) { $items = ''; foreach ( $features as $feature ) { $excluded = 0 === strpos( $feature, '-' ); $feature = $excluded ? trim( substr( $feature, 1 ) ) : $feature; $items .= self::comment( 'core/list-item', $excluded ? array( 'className' => 'is-excluded' ) : array(), ( $excluded ? '
  • ' : '
  • ' ) . AIEO_Content_Inline::html( $feature ) . ( $excluded ? ' (' . esc_html__( 'not included', 'ai-eshop-optimizer' ) . ')' : '' ) . '
  • ' ); } $inner .= self::comment( 'core/list', array( 'className' => 'is-style-checkmark-list', 'fontSize' => 'sm' ), '
      ' . $items . '
    ' ); } $href = trim( (string) ( $a['ctaHref'] ?? '' ) ); if ( '' !== $href ) { $cta = trim( (string) ( $a['ctaText'] ?? '' ) ); $inner .= self::standin_button( '' !== $cta ? $cta : sprintf( __( 'Choose %s', 'ai-eshop-optimizer' ), $name ), $href ); } $attrs = array( 'className' => 'is-style-card rooster-price-card', 'style' => array( 'spacing' => array( 'blockGap' => 'var:preset|spacing|4' ) ) ); if ( $highlighted ) { $attrs['backgroundColor'] = 'contrast'; $attrs['textColor'] = 'bg'; } $attrs['layout'] = array( 'type' => 'flex', 'orientation' => 'vertical', 'flexWrap' => 'nowrap' ); return self::group( $attrs, $inner ); } private static function standin_compare_table( array $a ) { $split = static function ( $line ) { return array_map( 'trim', explode( '|', (string) $line ) ); }; $columns = array_values( array_filter( $split( (string) ( $a['columns'] ?? '' ) ), 'strlen' ) ); $rows = array_values( array_filter( array_map( 'trim', preg_split( '/\r\n|\r|\n/', (string) ( $a['rows'] ?? '' ) ) ?: array() ), 'strlen' ) ); if ( ! $columns || ! $rows ) { return ''; } $words = array( 'yes' => __( 'Yes', 'ai-eshop-optimizer' ), 'no' => __( 'No', 'ai-eshop-optimizer' ), 'soon' => __( 'Soon', 'ai-eshop-optimizer' ), 'partial' => __( 'Partial', 'ai-eshop-optimizer' ) ); $html = '
    '; foreach ( $columns as $column ) { $html .= ''; } $html .= ''; foreach ( $rows as $row ) { $cells = $split( $row ); $html .= ''; foreach ( array_keys( $columns ) as $i ) { $cell = (string) ( $cells[ $i ] ?? '' ); $html .= ''; } $html .= ''; } $html .= '
    ' . esc_html( $column ) . '
    ' . esc_html( $words[ strtolower( $cell ) ] ?? $cell ) . '
    '; $caption = trim( (string) ( $a['caption'] ?? '' ) ); if ( '' !== $caption ) { $html .= '
    ' . esc_html( $caption ) . '
    '; } return self::comment( 'core/table', array( 'className' => 'rooster-compare' ), $html . '
    ' ); } public static function canonical( array $block ) { $name = (string) ( $block['blockName'] ?? '' ); $attrs = is_array( $block['attrs'] ?? null ) ? $block['attrs'] : array(); if ( isset( $attrs['metadata']['rooster']['hash'] ) ) { unset( $attrs['metadata']['rooster']['hash'] ); } $attrs = self::without_defaults( $name, $attrs ); $attrs = self::sorted( $attrs ); $html = array(); foreach ( (array) ( $block['innerContent'] ?? array() ) as $piece ) { if ( is_string( $piece ) ) { $piece = trim( (string) preg_replace( '/\s+/u', ' ', $piece ) ); $piece = (string) preg_replace( '/>\s+<', $piece ); if ( '' !== $piece ) { $html[] = $piece; } } } $inner = array(); foreach ( (array) ( $block['innerBlocks'] ?? array() ) as $child ) { if ( is_array( $child ) && ! empty( $child['blockName'] ) ) { $inner[] = self::canonical( $child ); } } return array( 'n' => $name, 'a' => $attrs, 'h' => $html, 'i' => $inner ); } public static function hash( $block ) { if ( is_string( $block ) ) { $parsed = self::first_block( $block ); if ( ! $parsed ) { return ''; } $block = $parsed; } return sha1( (string) wp_json_encode( self::canonical( $block ) ) ); } public static function first_block( $markup ) { foreach ( parse_blocks( (string) $markup ) as $block ) { if ( ! empty( $block['blockName'] ) ) { return $block; } } return null; } private static function sorted( array $data ) { if ( ! array_is_list( $data ) ) { ksort( $data ); } foreach ( $data as $key => $value ) { if ( is_array( $value ) ) { $data[ $key ] = self::sorted( $value ); } } return $data; } }