'; } 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'] ?? '' ) ) ) . '>' . $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( $column ) . ' | '; } $html .= '
|---|
| ' . esc_html( $words[ strtolower( $cell ) ] ?? $cell ) . ' | '; } $html .= '