array( 'badges', 'image', 'panel', 'tiles', 'on_this_page', 'motif', 'family', 'icon', 'emphasis', 'badges_place', 'eyebrow_style', 'columns' ), 'cards' => array( 'cards', 'layout', 'numbered', 'collapsible', 'columns', 'badge', 'notice', 'notice_place', 'icon', 'unit' ), 'stats' => array( 'items', 'stats' ), 'pricing' => array( 'tiers', 'enterprise', 'includes' ), 'table' => array( 'columns', 'rows', 'groups', 'highlight', 'caption', 'phone', 'marks', 'label' ), 'faq' => array( 'items' ), 'cta' => array( 'text', 'items', 'icon', 'motif' ), 'gallery' => array( 'items', 'images' ), 'prose' => array( 'body', 'content', 'markup' ), 'split' => array( 'items', 'text', 'aside', 'image', 'side', 'pattern', 'scores' ), 'pattern' => array( 'pattern', 'slug', 'fields', 'submit', 'consent', 'scores' ), 'form' => array( 'form' ), ); const FORM_FIELDS = array( 'form', 'variant', 'layout', 'tone', 'eyebrow', 'badge', 'icon', 'heading', 'lede', 'level' ); const FORM_VARIANTS = array( 'panel' => array( 'card', 'wash' ) ); const CARD_FIELDS = array( 'key', 'icon', 'title', 'text', 'href', 'ctaText', 'level', 'layout', 'badge', 'kicker', 'items', 'highlighted', 'price', 'anchor', 'products', 'features', 'docs', 'accent', 'motif', 'swatches', 'todo', 'note', 'tone', 'colour', 'ink', 'chips' ); const TIER_FIELDS = array( 'key', 'name', 'tagline', 'price', 'period', 'note', 'features', 'ctaText', 'ctaHref', 'highlighted', 'badge', 'layout', 'anchor', 'volume', 'todo', 'tone' ); const TONES = array( 'neutral', 'success', 'warning', 'danger' ); const NOT_TONES = array( 'green', 'red', 'amber', 'blue' ); const ROOSTER_ONE_FIELDS = array( 'tone', 'colour', 'ink', 'chips', 'emphasis' ); private static $display = ''; const BUTTON_FIELDS = array( 'text', 'href', 'style', 'external', 'new_tab', 'note', 'todo', 'trigger', 'solid', 'icon', 'action', 'aside' ); const BUTTON_ACTIONS = array( 'navigator' => 'aieo-navigator-trigger' ); const BUTTON_ACTION_LINKS = array( 'navigator' => '#rooster-navigator' ); const MARKS = array( 'yes', 'no', 'soon', 'partial' ); public static function load( $path ) { $path = (string) $path; if ( is_dir( $path ) ) { $out = array(); foreach ( glob( rtrim( $path, '/\\' ) . '/*.page.json' ) ?: array() as $file ) { $spec = self::read( $file ); if ( is_wp_error( $spec ) ) { return $spec; } $out[ $file ] = $spec; } if ( ! $out ) { return new WP_Error( 'aieo_content_empty', sprintf( 'No *.page.json in %s.', $path ) ); } return $out; } $spec = self::read( $path ); return is_wp_error( $spec ) ? $spec : array( $path => $spec ); } public static function read( $file ) { if ( ! is_readable( $file ) ) { return new WP_Error( 'aieo_content_unreadable', sprintf( 'Cannot read %s.', $file ) ); } $spec = wp_json_file_decode( $file, array( 'associative' => true ) ); if ( ! is_array( $spec ) ) { return new WP_Error( 'aieo_content_json', sprintf( '%s is not JSON (%s).', $file, json_last_error_msg() ) ); } return $spec; } public static function content_dir( $file ) { $dir = dirname( (string) $file ); return 'pages' === basename( $dir ) ? dirname( $dir ) : $dir; } public static function validate( array $spec, $opts = array() ) { if ( is_string( $opts ) ) { $opts = array( 'media_dir' => $opts ); } $errors = array(); $warnings = array(); $e = static function ( $message ) use ( &$errors ) { $errors[] = $message; }; $w = static function ( $message ) use ( &$warnings ) { $warnings[] = $message; }; self::$display = (string) ( $spec['display'] ?? '' ); foreach ( array_keys( $spec ) as $field ) { if ( ! in_array( (string) $field, self::PAGE_FIELDS, true ) ) { $e( sprintf( '%s is not a field the compiler knows (page level).', (string) $field ) ); } } if ( AIEO_Content::SCHEMA !== (string) ( $spec['$schema'] ?? '' ) ) { $e( sprintf( '$schema must be %s.', AIEO_Content::SCHEMA ) ); } $key = (string) ( $spec['key'] ?? '' ); if ( ! preg_match( self::KEY, $key ) ) { $e( 'key: a lower-case slug ([a-z0-9-]) is required.' ); } $slug = (string) ( $spec['slug'] ?? $key ); if ( '' === $slug && ! empty( $spec['front_page'] ) ) { $slug = $key; } if ( '' === $slug || sanitize_title( $slug ) !== $slug ) { $e( 'slug: a lower-case slug is required (empty only on the front page).' ); } if ( '' === trim( (string) ( $spec['title'] ?? '' ) ) ) { $e( 'title is required.' ); } $status = (string) ( $spec['status'] ?? 'publish' ); if ( ! in_array( $status, array( 'draft', 'pending', 'private', 'publish' ), true ) ) { $e( 'status: draft, pending, private or publish.' ); } if ( ! empty( $spec['template'] ) ) { $template = (string) $spec['template']; $known = array(); if ( function_exists( 'get_block_templates' ) ) { foreach ( get_block_templates( array(), 'wp_template' ) as $t ) { $known[] = $t->slug; } } if ( $known && ! in_array( $template, $known, true ) ) { $w( sprintf( 'template: %s is not a template of the active theme.', $template ) ); } } if ( isset( $spec['accent'] ) && '' !== $spec['accent'] && sanitize_key( (string) $spec['accent'] ) !== (string) $spec['accent'] ) { $e( 'accent: a slug.' ); } elseif ( ! empty( $spec['accent'] ) && class_exists( 'Rooster\Core\Design' ) && method_exists( 'Rooster\Core\Design', 'accents' ) && ! in_array( (string) $spec['accent'], array_column( (array) \Rooster\Core\Design::accents(), 'slug' ), true ) ) { $w( sprintf( 'accent %s is not a configured family (the theme\'s design.accents setting): the page meta is emptied by the theme until it is (wp rooster ability set-settings).', (string) $spec['accent'] ) ); } if ( array_key_exists( 'display', $spec ) && ! in_array( $spec['display'], AIEO_Content_Sections::DISPLAYS, true ) ) { $e( 'display: one of ' . implode( ', ', AIEO_Content_Sections::DISPLAYS ) . ' (the type of the legacy home, of the legacy promo template, of the legacy Rooster One landing).' ); } if ( ! empty( $spec['accent_colour'] ) ) { $w( 'accent_colour is not written by the compiler: the family colours are the theme\'s design.accents setting (wp rooster ability set-settings).' ); } foreach ( (array) ( $spec['plugin_pages'] ?? array() ) as $p ) { if ( ! is_string( $p ) || sanitize_key( $p ) !== $p ) { $e( 'plugin_pages: a list of policy keys (rooster-lms, woocommerce).' ); break; } } if ( isset( $spec['seo'] ) ) { if ( ! is_array( $spec['seo'] ) ) { $e( 'seo: an object ({ "description": "…" }).' ); } elseif ( ! empty( $spec['seo']['title'] ) ) { $w( 'seo.title is not carried: WordPress has no title override here; the page title is used.' ); } } $media = (array) ( $spec['media'] ?? array() ); foreach ( $media as $path => $alt ) { $path = (string) $path; if ( ! self::media_path_ok( $path ) ) { $e( sprintf( 'media: %s must be media/… (relative to the content folder), /wp-content/uploads/… or /wp-content/themes/….', $path ) ); continue; } if ( ! is_string( $alt ) ) { $e( sprintf( 'media: %s needs its text alternative (a string).', $path ) ); } elseif ( '' === trim( $alt ) ) { $w( sprintf( 'media: %s has an empty text alternative: it is drawn as a decorative picture (alt="", no title).', $path ) ); } if ( ! empty( $opts['check_files'] ) && ! AIEO_Content_Media::find( $path ) && '' === AIEO_Content_Media::source( $path, $opts ) ) { $e( sprintf( 'media: %s is neither in the media library nor under the media folders given.', $path ) ); } } $sections = $spec['sections'] ?? null; if ( ! is_array( $sections ) || ! $sections ) { $e( 'sections: at least one is required.' ); return array( 'errors' => $errors, 'warnings' => $warnings ); } $keys = array(); $h1s = array(); foreach ( array_values( $sections ) as $i => $s ) { self::validate_section( $s, sprintf( 'sections[%d]', $i ), $keys, $h1s, $media, $e, $w ); } foreach ( self::duplicate_ids( array_values( $sections ) ) as $warning ) { $w( $warning ); } foreach ( self::duplicate_ids( array_values( $sections ), true ) as $error ) { $e( $error ); } if ( count( $h1s ) > 1 ) { $w( 'The page has ' . count( $h1s ) . ' headings of level 1 (sections ' . implode( ', ', $h1s ) . ': a hero\'s, or a section holding blocks with level 1): a page has one.' ); } return array( 'errors' => $errors, 'warnings' => $warnings ); } private static function validate_section( $s, $at, array &$keys, array &$h1s, array $media, $e, $w, $in_tab = false ) { if ( ! is_array( $s ) ) { $e( "$at: an object." ); return; } $skey = (string) ( $s['key'] ?? '' ); if ( ! preg_match( self::KEY, $skey ) ) { $e( "$at: key: a lower-case slug is required." ); } elseif ( isset( $keys[ $skey ] ) ) { $e( "$at: key $skey is used twice." ); } $keys[ $skey ] = true; $at = '' !== $skey ? "section $skey" : $at; $kind = (string) ( $s['kind'] ?? '' ); if ( ! in_array( $kind, AIEO_Content_Sections::KINDS, true ) ) { $e( "$at: kind: one of " . implode( ', ', AIEO_Content_Sections::KINDS ) . '.' ); return; } if ( $in_tab && 'hero' === $kind ) { $e( "$at: a hero is never inside a tab (a tab's sections are its panel's; the page has its hero above)." ); return; } if ( $in_tab && isset( $s['style'] ) && 'default' !== (string) $s['style'] && ! isset( $s['padding'] ) ) { $w( "$at: style {$s['style']} inside a tab: a band inside the panel, with none of the panel's padding above it; give it its padding." ); } $layout = array_key_exists( 'blocks', $s ); foreach ( array_keys( $s ) as $field ) { if ( ! in_array( (string) $field, self::SECTION_COMMON, true ) && ! in_array( (string) $field, $layout ? self::LAYOUT_FIELDS : self::SECTION_FIELDS[ $kind ], true ) ) { $e( "$at: $field is not a field the compiler knows for a $kind section" . ( $layout ? ' holding blocks (its layout: columns, gap, align, style, tone, padding, head_column; its head: icon, badge, notice, heading_style).' : '.' ) ); } } if ( isset( $s['style'] ) && ! in_array( (string) $s['style'], AIEO_Content_Sections::STYLES, true ) ) { $e( "$at: style: one of " . implode( ', ', AIEO_Content_Sections::STYLES ) . '.' ); } elseif ( 'card' === ( $s['style'] ?? '' ) && ! in_array( $kind, array( 'hero', 'cta' ), true ) ) { $w( "$at: style card is the hero's box and the call to action's banner; a $kind section has no card style." ); } if ( isset( $s['align'] ) && ! in_array( (string) $s['align'], array( 'left', 'center' ), true ) ) { $e( "$at: align: left or center (the landings' heads read left; the home's family cards and a CTA say center)." ); } if ( isset( $s['padding'] ) ) { $sides = is_array( $s['padding'] ) ? $s['padding'] : array( 'bad' => true ); foreach ( $sides as $side => $value ) { $pairs = 'mobile' === $side && is_array( $value ) ? $value : array( $side => $value ); foreach ( $pairs as $edge => $length ) { if ( ! in_array( (string) $edge, array( 'top', 'bottom' ), true ) || '' === AIEO_Content_Sections::padding_value( $length ) ) { $e( "$at: padding: { top, bottom, mobile: { top, bottom } }, each a spacing preset (0 to 10) or a length in rem or px." ); break 2; } } } } if ( isset( $s['tone'] ) && ( '' === (string) $s['tone'] || sanitize_key( (string) $s['tone'] ) !== (string) $s['tone'] || in_array( (string) $s['tone'], self::NOT_TONES, true ) ) ) { $e( "$at: tone: " . implode( ', ', self::TONES ) . ' or a family\'s accent slug.' ); } if ( isset( $s['gap'] ) && '' === AIEO_Content_Sections::padding_value( $s['gap'] ) ) { $e( "$at: gap: a spacing preset (0 to 10) or a length in rem or px." ); } if ( $layout ) { self::validate_layout( $s, $at, $e, $w ); foreach ( is_array( $s['blocks'] ) ? $s['blocks'] : array() as $j => $item ) { if ( AIEO_Content_Tabs::is( $item ) ) { self::validate_tabs( $item, "$at: blocks[$j]", $s, $keys, $h1s, $media, $e, $w, $in_tab ); } } if ( AIEO_Content_Tabs::holds( $s ) && ! empty( $s['defer'] ) ) { $w( "$at: defer is not carried on a section holding tabs: its panels' sections wait instead (deferred regions do not nest)." ); } } if ( isset( $s['notice_place'] ) ) { if ( ! in_array( (string) $s['notice_place'], AIEO_Content_Sections::NOTICE_PLACES, true ) ) { $e( "$at: notice_place: " . implode( ' or ', AIEO_Content_Sections::NOTICE_PLACES ) . ' (lede: the notice as a pill at the start of the lede).' ); } elseif ( empty( $s['notice'] ) ) { $w( "$at: notice_place without a notice." ); } elseif ( 'lede' === (string) $s['notice_place'] && empty( $s['lede'] ) ) { $w( "$at: notice_place lede without a lede: the notice stays under the heading." ); } } self::validate_aliases( $s, $at, $e, $w ); if ( ! empty( $s['todo'] ) ) { $w( "$at: TODO: " . preg_replace( '/^TODO:\s*/', '', (string) $s['todo'] ) ); } if ( 'hero' !== $kind && '' === trim( (string) ( $s['heading'] ?? '' ) ) && empty( $s['headless'] ) && ! in_array( $kind, array( 'cta', 'prose', 'pattern', 'split', 'form' ), true ) ) { $e( "$at: a heading, or headless: true." ); } if ( isset( $s['level'] ) && ( (int) $s['level'] < 1 || (int) $s['level'] > 4 ) ) { $e( "$at: level: 1 to 4." ); } elseif ( 1 === (int) ( $s['level'] ?? 0 ) && ! $layout ) { $w( "$at: level 1 is a section holding blocks' (a hero composed as a layout container): a $kind section draws its own heading." ); } self::validate_heading( (string) ( $s['heading'] ?? '' ), $at, $e ); if ( ( 'hero' === $kind && ! $layout && '' !== trim( (string) ( $s['heading'] ?? '' ) ) ) || ( $layout && 1 === (int) ( $s['level'] ?? 0 ) && empty( $s['headless'] ) && '' !== trim( (string) ( $s['heading'] ?? '' ) ) ) ) { $h1s[] = $skey; } if ( array_key_exists( 'defer', $s ) && ! is_bool( $s['defer'] ) ) { $e( "$at: defer: true or false." ); } elseif ( array_key_exists( 'defer', $s ) && ! in_array( $kind, AIEO_Content_Sections::DEFERRABLE, true ) && 'later' !== $in_tab ) { $w( "$at: defer has no effect on a $kind section: it stays in the first HTML (" . implode( ', ', AIEO_Content_Sections::DEFERRABLE ) . ' defer; in a tab after the first, any section).' ); } self::validate_buttons( (array) ( $s['buttons'] ?? array() ), $at, $e, $w ); self::validate_links( $s['links'] ?? null, $at, $e ); self::inline_links( $s, $at, $w ); if ( ! $layout ) { self::validate_kind( $kind, $s, $at, $media, $e, $w ); } } private static function validate_heading( $heading, $at, $e ) { if ( ! str_contains( $heading, '{{' ) ) { return; } preg_match_all( AIEO_Content_Inline::COLOUR_TOKEN, $heading, $found, PREG_SET_ORDER ); foreach ( $found as $m ) { if ( ! isset( AIEO_Content_Inline::COLOURS[ $m[1] ] ) ) { $e( "$at: heading: {{{$m[1]}:…}}: the coloured words take " . implode( ', ', array_keys( AIEO_Content_Inline::COLOURS ) ) . ' (4.5:1 in both schemes; warning: the palette\'s warning-ink).' ); } } if ( substr_count( $heading, '{{' ) !== count( $found ) ) { $e( "$at: heading: a {{ without its tone and words: {{danger:words}}." ); } } private static function inline_links( array $s, $at, $w ) { $token = '/\[([^\]]+)\]\(([^)\s]+)\)/u'; $walk = static function ( $value, $path ) use ( &$walk, $token, $at, $w ) { if ( is_array( $value ) ) { foreach ( $value as $k => $v ) { $walk( $v, "$path.$k" ); } } elseif ( is_string( $value ) && preg_match_all( $token, $value, $m, PREG_SET_ORDER ) ) { foreach ( $m as $link ) { if ( ! self::link_ok( $link[2] ) ) { $w( "$at: $path: the link of [{$link[1]}] is not one the compiler accepts (http(s)://, /path, #anchor, mailto:, tel:)." ); } } } }; foreach ( $s as $field => $value ) { if ( 'blocks' !== $field ) { $walk( $value, (string) $field ); } } foreach ( is_array( $s['blocks'] ?? null ) ? $s['blocks'] : array() as $j => $item ) { if ( ! is_array( $item ) || AIEO_Content_Tabs::is( $item ) ) { continue; } $walk( $item, "blocks[$j]" ); $name = (string) ( $item['block'] ?? '' ); $variant = (string) ( $item['variant'] ?? '' ); $free = array(); if ( 'card' === $name && in_array( $variant, array( 'link', 'family' ), true ) ) { $free = array( 'link' === $variant ? 'items' : 'products' ); } elseif ( 'card' === $name && in_array( $variant, array( '', 'feature' ), true ) && ! empty( $item['href'] ) && empty( $item['ctaText'] ) ) { $free = array( 'text' ); } elseif ( 'list' === $name && 'links' === (string) ( $item['layout'] ?? '' ) ) { $free = array( 'items' ); } foreach ( $free as $attr ) { if ( preg_match( $token, is_array( $item[ $attr ] ?? null ) ? implode( "\n", array_map( 'strval', $item[ $attr ] ) ) : (string) ( $item[ $attr ] ?? '' ) ) ) { $w( "$at: blocks[$j]: $attr: a link in a text that is itself a link prints as its words." ); } } if ( 'questions' === $name ) { $lines = is_array( $item['items'] ?? null ) ? array_map( 'strval', $item['items'] ) : preg_split( '/\r\n|\r|\n/', (string) ( $item['items'] ?? '' ) ); foreach ( (array) $lines as $line ) { if ( str_contains( $line, '|' ) && preg_match( $token, explode( '|', $line, 2 )[0] ) ) { $w( "$at: blocks[$j]: a link in a question prints as its words (the question is the disclosure's control)." ); } } } } } private static function validate_layout( array $s, $at, $e, $w ) { $columns = $s['columns'] ?? 1; $split = AIEO_Content_Layout::split( $columns ); $count = $split ? 2 : (int) $columns; if ( ! $split && ( ! is_numeric( $columns ) || $count < 1 || $count > 4 ) ) { $e( "$at: columns: 1, 2, 3, 4 or a split a/b (58/42, 1.15fr/1fr)." ); } if ( ! is_array( $s['blocks'] ) || ! array_is_list( $s['blocks'] ) ) { $e( "$at: blocks: a list of { block, variant, …attributes } or { pattern, column } ([] for a head-only section)." ); return; } $headed = empty( $s['headless'] ) && ( '' !== trim( (string) ( $s['heading'] ?? '' ) ) || ! empty( $s['eyebrow'] ) || ! empty( $s['lede'] ) ); if ( ! $s['blocks'] && ! $headed ) { $e( "$at: a head-only section (blocks: []) needs its head: a heading, an eyebrow or a lede, and no headless." ); } if ( isset( $s['heading_style'] ) && ! in_array( (string) $s['heading_style'], self::HEADING_STYLES, true ) ) { $e( "$at: heading_style: " . implode( ', ', self::HEADING_STYLES ) . ' (the heading\'s words in a pill of the section\'s tone).' ); } elseif ( isset( $s['heading_style'] ) && ( ! empty( $s['headless'] ) || '' === trim( (string) ( $s['heading'] ?? '' ) ) ) ) { $w( "$at: heading_style has no heading to draw (headless, or no heading)." ); } if ( isset( $s['head_column'] ) ) { $column = $s['head_column']; if ( ! is_int( $column ) || $column < 1 || $column > max( 1, $count ) || $count < 2 ) { $e( "$at: head_column: a column of the layout (1 to " . max( 1, $count ) . '), in a split or a grid of two or more columns.' ); } elseif ( ! empty( $s['headless'] ) || ! $s['blocks'] ) { $w( "$at: head_column has no head to place (headless) or no layout (blocks: [])." ); } } if ( AIEO_Content_Rooster_One::NAME === self::$display && isset( $s['style'] ) && 'default' !== (string) $s['style'] ) { $w( "$at: style: the rooster-one display draws its sections in the legacy's container, without a surface." ); } $library = class_exists( 'AIEO_Engine_Blocks', false ) ? AIEO_Engine_Blocks::LIBRARY : array(); foreach ( $s['blocks'] as $j => $item ) { $where = "$at: blocks[$j]"; if ( ! is_array( $item ) ) { $e( "$where: an object." ); continue; } if ( ! isset( $item['block'] ) && array_key_exists( 'pattern', $item ) ) { self::validate_pattern_item( $item, $where, $count, $e, $w ); continue; } if ( AIEO_Content_Tabs::is( $item ) ) { if ( isset( $item['column'] ) && ( (int) $item['column'] < 1 || (int) $item['column'] > max( 1, $count ) ) ) { $e( "$where: column: 1 to " . max( 1, $count ) . '.' ); } continue; } if ( isset( $item['pattern'] ) ) { $e( "$where: a block or a pattern, not both." ); } $name = (string) ( $item['block'] ?? '' ); if ( ! in_array( $name, AIEO_Content_Layout::BLOCKS, true ) ) { $e( "$where: block: one of " . implode( ', ', AIEO_Content_Layout::BLOCKS ) . ', ' . AIEO_Content_Tabs::NAME . '.' ); continue; } if ( isset( $item['variant'] ) && $library && ! isset( $library[ $name ][ (string) $item['variant'] ] ) ) { $e( "$where: variant: one of " . implode( ', ', array_keys( $library[ $name ] ) ) . " ($name)." ); } if ( isset( $item['column'] ) && ( (int) $item['column'] < 1 || (int) $item['column'] > max( 1, $count ) ) ) { $e( "$where: column: 1 to " . max( 1, $count ) . '.' ); } if ( isset( $item['tone'] ) && ( sanitize_key( (string) $item['tone'] ) !== (string) $item['tone'] || in_array( (string) $item['tone'], self::NOT_TONES, true ) ) ) { $e( "$where: tone: " . implode( ', ', self::TONES ) . ' or a family\'s accent slug.' ); } $type = WP_Block_Type_Registry::get_instance()->get_registered( 'rooster/' . $name ); if ( $type && is_array( $type->attributes ) ) { foreach ( array_keys( $item ) as $key ) { if ( ! in_array( (string) $key, array( 'block', 'column' ), true ) && ! isset( $type->attributes[ $key ] ) && ! in_array( (string) $key, array( 'anchor', 'align' ), true ) ) { $e( "$where: $key is not an attribute of rooster/$name." ); } } } if ( 'media' === $name && 'track' === (string) ( $item['variant'] ?? '' ) ) { $lines = is_array( $item['items'] ?? null ) ? array_map( 'strval', $item['items'] ) : preg_split( '/\r\n|\r|\n/', (string) ( $item['items'] ?? '' ) ); $lines = array_values( array_filter( array_map( 'trim', (array) $lines ), 'strlen' ) ); if ( ! $lines ) { $e( "$where: items: a track's figures, one per line: {{media-id:}} | alt | caption." ); } foreach ( $lines as $k => $line ) { $first = trim( explode( '|', $line )[0] ); if ( ! preg_match( '/^(\{\{media-id:[^}|]+\}\}|\d+)$/', $first ) ) { $e( "$where: items[$k]: the first cell is the picture, {{media-id:}}." ); } } } if ( 'card' === $name && 'grid' === (string) ( $item['variant'] ?? '' ) ) { $lines = is_array( $item['items'] ?? null ) ? array_map( 'strval', $item['items'] ) : preg_split( '/\r\n|\r|\n/', (string) ( $item['items'] ?? '' ) ); foreach ( (array) $lines as $k => $line ) { $cells = array_map( 'trim', array_slice( explode( '|', (string) $line ), 1 ) ); foreach ( $cells as $cell ) { if ( str_starts_with( $cell, 'anchor:' ) && ! preg_match( '/^anchor:[a-z0-9][a-z0-9_-]*$/', $cell ) ) { $e( "$where: items[$k]: $cell: anchor:, a lower-case slug (anchor:lmsp-pro-convert)." ); } elseif ( str_starts_with( $cell, 'link:' ) && ! preg_match( '/^link:\[[^\]]+\]\([^)\s]+\)$/u', $cell ) ) { $e( "$where: items[$k]: $cell: link:[text](url), the spec's link markup." ); } } foreach ( array( 'anchor:', 'link:' ) as $prefix ) { if ( count( array_filter( $cells, static fn ( $c ) => str_starts_with( $c, $prefix ) ) ) > 1 ) { $e( "$where: items[$k]: one $prefix cell a card." ); } } } } if ( 'action' === $name && 'buttons' !== (string) ( $item['variant'] ?? 'panel' ) && 'ink' !== (string) ( $item['layout'] ?? '' ) ) { $lines = is_array( $item['buttons'] ?? null ) ? array_map( 'strval', $item['buttons'] ) : preg_split( '/\r\n|\r|\n/', (string) ( $item['buttons'] ?? '' ) ); foreach ( array_values( array_filter( array_map( 'trim', (array) $lines ), 'strlen' ) ) as $k => $line ) { $cells = array_map( 'trim', explode( '|', $line ) ); if ( '' !== (string) ( $cells[2] ?? '' ) && ! in_array( strtolower( (string) $cells[2] ), array( 'primary', 'secondary' ), true ) ) { $e( "$where: buttons[$k]: {$cells[2]}: primary or secondary (Text | link | primary|secondary | icon)." ); } $icon = (string) ( $cells[3] ?? '' ); if ( '' !== $icon && sanitize_key( $icon ) !== $icon ) { $e( "$where: buttons[$k]: icon $icon: a Lucide name (drawn before the words; arrow-right after them)." ); } elseif ( '' !== $icon ) { self::icon_known( $icon, "$where: buttons[$k]", $w ); } if ( count( $cells ) > 4 ) { $w( "$where: buttons[$k]: a fifth cell is not drawn (Text | link | primary|secondary | icon)." ); } } } if ( 'card' === $name && ( isset( $item['chips'] ) || isset( $item['chipsLayout'] ) ) && ! in_array( (string) ( $item['variant'] ?? 'feature' ), array( '', 'feature' ), true ) ) { $w( "$where: chips and chipsLayout are the feature card's (package F)." ); } if ( 'table' === $name ) { $cols = is_array( $item['columns'] ?? null ) ? count( $item['columns'] ) : count( array_filter( array_map( 'trim', explode( '|', (string) ( $item['columns'] ?? '' ) ) ), 'strlen' ) ); if ( isset( $item['highlight'] ) && ! self::highlight_ok( $item['highlight'], $cols, true ) ) { $e( "$where: highlight: column numbers from 1 within the columns, the feature column being 0 (2, \"2, 3, 4\" or [2, 3, 4])." ); } if ( isset( $item['marks'] ) && ! self::marks_ok( $item['marks'] ) ) { $e( "$where: marks: the words of yes, no, soon and partial, \"Included | Not included | Coming soon\" (or a list of up to four)." ); } } } } private static function validate_pattern_item( array $item, $where, $count, $e, $w ) { foreach ( array_keys( $item ) as $key ) { if ( ! in_array( (string) $key, AIEO_Content_Layout::PATTERN_FIELDS, true ) ) { $e( "$where: $key is not a field of a pattern item (" . implode( ', ', AIEO_Content_Layout::PATTERN_FIELDS ) . ').' ); } } $slug = trim( (string) $item['pattern'] ); if ( '' === $slug || ! preg_match( '#^[a-z0-9-]+/[a-z0-9-]+$#', $slug ) ) { $e( "$where: pattern: a registered pattern's slug (rooster-site/cmi5-signup-form)." ); } else { self::pattern_known( $slug, $where, $w ); } if ( isset( $item['column'] ) && ( (int) $item['column'] < 1 || (int) $item['column'] > max( 1, $count ) ) ) { $e( "$where: column: 1 to " . max( 1, $count ) . '.' ); } if ( ! empty( $item['fields'] ) || ! empty( $item['submit'] ) || ! empty( $item['consent'] ) ) { $w( "$where: a form (fields, submit, consent) is not page content; the pattern $slug must provide it." ); } if ( ! empty( $item['scores'] ) ) { $w( "$where: scores are not carried: the pattern $slug reads its own data (a site-plugin block fed from the scores JSON, plan 4.6)." ); } if ( ! empty( $item['todo'] ) ) { $w( "$where: TODO: " . preg_replace( '/^TODO:\s*/', '', (string) $item['todo'] ) ); } } private static function validate_tabs( array $item, $where, array $s, array &$keys, array &$h1s, array $media, $e, $w, $in_tab ) { if ( $in_tab ) { $e( "$where: a tabs block is never inside a tab." ); return; } foreach ( array_keys( $item ) as $field ) { if ( ! in_array( (string) $field, AIEO_Content_Tabs::FIELDS, true ) ) { $e( "$where: $field is not a field of the tabs (" . implode( ', ', AIEO_Content_Tabs::FIELDS ) . ').' ); } } $slug = static fn ( $v ) => is_string( $v ) && '' !== $v && sanitize_title( $v ) === $v; if ( isset( $item['label'] ) && ! is_string( $item['label'] ) ) { $e( "$where: label: the tab list's name for a screen reader, a string (default the section's heading)." ); } if ( isset( $item['tone'] ) ) { self::tab_tone( $item['tone'], $where, $e ); } if ( isset( $item['anchor'] ) && ! $slug( $item['anchor'] ) ) { $e( "$where: anchor: an id (a lower-case slug, without the #)." ); } $tabs = $item['tabs'] ?? null; if ( ! is_array( $tabs ) || ! $tabs || ! array_is_list( $tabs ) ) { $e( "$where: tabs: a list of { key, label, icon, tone, anchor, aliases, sections }." ); return; } if ( 1 === count( $tabs ) ) { $w( "$where: one tab: a tab list of one gives nothing to choose." ); } $names = array(); foreach ( $tabs as $k => $tab ) { $at = "$where: tabs[$k]"; if ( ! is_array( $tab ) ) { $e( "$at: an object." ); continue; } foreach ( array_keys( $tab ) as $field ) { if ( ! in_array( (string) $field, AIEO_Content_Tabs::TAB_FIELDS, true ) ) { $e( "$at: $field is not a field of a tab (" . implode( ', ', AIEO_Content_Tabs::TAB_FIELDS ) . ').' ); } } if ( isset( $tab['key'] ) ) { if ( ! is_string( $tab['key'] ) || ! preg_match( self::KEY, $tab['key'] ) ) { $e( "$at: key: a lower-case slug." ); } elseif ( isset( $names[ $tab['key'] ] ) ) { $e( "$at: key {$tab['key']} is used twice in the tabs." ); } $names[ (string) $tab['key'] ] = true; } if ( ! is_string( $tab['label'] ?? null ) || '' === trim( $tab['label'] ) ) { $e( "$at: label: the tab's words are required." ); } if ( isset( $tab['icon'] ) ) { if ( ! is_string( $tab['icon'] ) || '' === $tab['icon'] || sanitize_key( $tab['icon'] ) !== $tab['icon'] ) { $e( "$at: icon: a Lucide name." ); } elseif ( 'none' !== $tab['icon'] && ! self::has_icon( $tab['icon'] ) ) { $e( "$at: icon {$tab['icon']} is not in the theme's collection." ); } } if ( isset( $tab['tone'] ) ) { self::tab_tone( $tab['tone'], $at, $e ); } if ( isset( $tab['anchor'] ) && ! $slug( $tab['anchor'] ) ) { $e( "$at: anchor: the panel's id (a lower-case slug, without the #)." ); } if ( isset( $tab['aliases'] ) && ( ! is_array( $tab['aliases'] ) || ! array_is_list( $tab['aliases'] ) || array_filter( $tab['aliases'], static fn ( $a ) => ! $slug( $a ) ) ) ) { $e( "$at: aliases: a list of ids (lower-case slugs, without the #)." ); } $list = $tab['sections'] ?? null; if ( ! is_array( $list ) || ! $list || ! array_is_list( $list ) ) { $e( "$at: sections: the panel's sections, at least one." ); continue; } foreach ( $list as $m => $n ) { self::validate_section( $n, "$at: sections[$m]", $keys, $h1s, $media, $e, $w, 0 === $k ? 'first' : 'later' ); } $aliases = AIEO_Content_Tabs::aliases( $tab ); if ( ! $aliases || ! empty( array_filter( (array) ( $tab['aliases'] ?? array() ), static fn ( $a ) => ! $slug( $a ) ) ) ) { continue; } $carrier = AIEO_Content_Tabs::carrier( $list ); if ( $carrier < 0 ) { $w( "$at: aliases " . implode( ', ', $aliases ) . " not carried: none of the tab's sections holds blocks (the first that does carries them)." ); continue; } $n = $list[ $carrier ]; $n['aliases'] = array_merge( is_array( $n['aliases'] ?? null ) ? $n['aliases'] : array(), $aliases ); $left = array_diff( $aliases, array_values( AIEO_Content_Layout::aliases( $n ) ) ); if ( $left ) { $w( "$at: aliases " . implode( ', ', $left ) . ' not carried: its section ' . (string) ( $n['key'] ?? '' ) . ' draws ' . count( AIEO_Content_Layout::alias_targets( $n ) ) . ' element(s) that take one, after its own aliases.' ); } } } private static function tab_tone( $tone, $at, $e ) { if ( ! self::tone_word( $tone ) ) { $e( "$at: tone: " . implode( ', ', self::TONES ) . ' or a family\'s accent slug.' ); return; } $families = class_exists( 'Rooster\Core\Design' ) && method_exists( 'Rooster\Core\Design', 'accents' ) ? array_column( (array) \Rooster\Core\Design::accents(), 'slug' ) : array(); if ( ! in_array( $tone, self::TONES, true ) && $families && ! in_array( $tone, $families, true ) ) { $e( "$at: tone $tone is neither a semantic tone nor a family of the theme's design.accents (" . implode( ', ', $families ) . ').' ); } } private static function validate_aliases( array $s, $at, $e, $w ) { if ( ! isset( $s['aliases'] ) ) { return; } $aliases = $s['aliases']; if ( ! is_array( $aliases ) || ! array_is_list( $aliases ) || array_filter( $aliases, static fn ( $a ) => ! is_string( $a ) || '' === $a || sanitize_title( $a ) !== $a ) ) { $e( "$at: aliases: a list of ids (lower-case slugs, without the #)." ); return; } if ( ! AIEO_Content_Layout::applies( $s ) ) { if ( $aliases ) { $w( "$at: aliases are carried by a section holding blocks only: " . implode( ', ', $aliases ) . ' need a redirect or the theme\'s alias rule.' ); } return; } $own = array_diff( $aliases, AIEO_Content_Layout::alias_ids( $s ) ); if ( $own ) { $w( "$at: aliases: " . implode( ', ', $own ) . ' the section gives already (its anchor, or a block\'s own anchor: a fragment reaches the block), not carried: an alias is an old id only.' ); } $ids = AIEO_Content_Layout::alias_ids( $s ); $targets = AIEO_Content_Layout::alias_targets( $s ); if ( count( $ids ) > count( $targets ) ) { $w( "$at: aliases: " . implode( ', ', array_slice( $ids, count( $targets ) ) ) . ' not carried: the section draws ' . count( $targets ) . ' element(s) that take one (' . implode( ', ', $targets ) . ').' ); } } public static function duplicate_ids( array $sections, $tabs = false ) { $seen = array(); $strict = array(); foreach ( self::page_ids( $sections ) as [ $id, $key, $in_tabs ] ) { $seen[ $id ][] = $key; if ( $in_tabs ) { $strict[ $id ] = true; } } $out = array(); foreach ( $seen as $id => $keys ) { if ( count( $keys ) > 1 && (bool) $tabs === isset( $strict[ $id ] ) ) { $out[] = "id $id is given twice (sections " . implode( ', ', $keys ) . '): a fragment link reaches the first only' . ( $tabs ? ', and a tab or a panel must be found by it.' : '.' ); } } return $out; } private static function page_ids( array $sections, $in_tabs = false ) { $out = array(); foreach ( $sections as $s ) { if ( ! is_array( $s ) ) { continue; } $key = (string) ( $s['key'] ?? '' ); $ids = array( AIEO_Content_Sections::anchor( $s + array( 'key' => '' ) ) ); if ( AIEO_Content_Layout::applies( $s ) ) { $ids = array_merge( $ids, array_values( AIEO_Content_Layout::aliases( $s ) ) ); } $tabs = array(); foreach ( array_merge( is_array( $s['blocks'] ?? null ) ? $s['blocks'] : array(), is_array( $s['tiers'] ?? null ) ? $s['tiers'] : array(), is_array( $s['cards'] ?? null ) ? $s['cards'] : array() ) as $item ) { if ( AIEO_Content_Tabs::is( $item ) ) { $tabs[] = $item; continue; } if ( is_array( $item ) && ! empty( $item['anchor'] ) && is_string( $item['anchor'] ) ) { $ids[] = sanitize_title( $item['anchor'] ); } if ( is_array( $item ) ) { $ids = array_merge( $ids, AIEO_Content_Layout::card_anchors( $item ) ); } } foreach ( array_filter( $ids, 'strlen' ) as $id ) { $out[] = array( $id, $key, $in_tabs ); } foreach ( $tabs as $item ) { $out[] = array( AIEO_Content_Tabs::container_id( $item, $s ), $key, true ); $panels = AIEO_Content_Tabs::panel_ids( $item, $s ); foreach ( AIEO_Content_Tabs::tabs( $item ) as $n => $tab ) { $out[] = array( $panels[ $n ], $key, true ); $out[] = array( $panels[ $n ] . '-tab', $key, true ); $list = AIEO_Content_Tabs::sections( $tab ); $carrier = AIEO_Content_Tabs::carrier( $list ); if ( $carrier >= 0 && AIEO_Content_Tabs::aliases( $tab ) ) { $list[ $carrier ]['aliases'] = array_merge( is_array( $list[ $carrier ]['aliases'] ?? null ) ? $list[ $carrier ]['aliases'] : array(), AIEO_Content_Tabs::aliases( $tab ) ); } $out = array_merge( $out, self::page_ids( $list, true ) ); } } } return $out; } private static function validate_r1_fields( array $item, $at, $e, $w ) { $used = array_intersect( self::ROOSTER_ONE_FIELDS, array_keys( $item ) ); if ( ! $used ) { return; } if ( AIEO_Content_Rooster_One::NAME !== self::$display ) { $w( "$at: " . implode( ', ', $used ) . ': drawn by the rooster-one display type only.' ); } if ( isset( $item['tone'] ) && ( sanitize_key( (string) $item['tone'] ) !== (string) $item['tone'] || in_array( (string) $item['tone'], self::NOT_TONES, true ) || '' === (string) $item['tone'] ) ) { $e( "$at: tone: " . implode( ', ', self::TONES ) . ' or a family\'s accent slug (one, lms, zero, …).' ); } if ( isset( $item['colour'] ) && ! sanitize_hex_color( (string) $item['colour'] ) ) { $e( "$at: colour: a hex colour (#4b8208)." ); } if ( isset( $item['ink'] ) ) { $inks = preg_split( '/[\s,]+/', trim( (string) $item['ink'] ), -1, PREG_SPLIT_NO_EMPTY ) ?: array(); if ( ! $inks || count( $inks ) > 2 || array_filter( $inks, static fn ( $hex ) => ! sanitize_hex_color( $hex ) ) ) { $e( "$at: ink: one or two hex colours, the light scheme's then the dark's (#4b8208 #d5faa9)." ); } } if ( isset( $item['chips'] ) && ( ! is_array( $item['chips'] ) || ! array_is_list( $item['chips'] ) || array_filter( $item['chips'], static fn ( $c ) => ! is_string( $c ) || '' === trim( $c ) ) ) ) { $e( "$at: chips: a list of words." ); } } private static function validate_buttons( array $buttons, $at, $e, $w ) { foreach ( array_values( $buttons ) as $j => $button ) { if ( ! is_array( $button ) || '' === trim( (string) ( $button['text'] ?? '' ) ) || '' === trim( (string) ( $button['href'] ?? '' ) ) ) { $e( "$at: buttons[$j]: text and href are required." ); continue; } foreach ( array_keys( $button ) as $field ) { if ( ! in_array( (string) $field, self::BUTTON_FIELDS, true ) ) { $e( "$at: buttons[$j]: $field is not a button field the compiler knows." ); } } if ( ! self::link_ok( (string) $button['href'] ) ) { $e( "$at: buttons[$j]: href {$button['href']} is not a link the compiler accepts." ); } if ( isset( $button['action'] ) && ! isset( self::BUTTON_ACTIONS[ (string) $button['action'] ] ) ) { $e( "$at: buttons[$j]: action: " . implode( ', ', array_keys( self::BUTTON_ACTIONS ) ) . '.' ); } if ( isset( $button['icon'] ) && AIEO_Content_Rooster_One::NAME === self::$display && ! isset( AIEO_Content_Blocks::BUTTON_ICONS[ (string) $button['icon'] ] ) ) { if ( sanitize_key( (string) $button['icon'] ) !== (string) $button['icon'] ) { $e( "$at: buttons[$j]: icon: a Lucide name." ); } elseif ( ! in_array( (string) $button['icon'], AIEO_Content_Rooster_One::OWN_ICONS, true ) ) { self::icon_known( (string) $button['icon'], "$at: buttons[$j]", $w ); } } elseif ( isset( $button['icon'] ) && ! isset( AIEO_Content_Blocks::BUTTON_ICONS[ (string) $button['icon'] ] ) ) { if ( ! is_string( $button['icon'] ) || sanitize_key( $button['icon'] ) !== $button['icon'] || '' === $button['icon'] ) { $e( "$at: buttons[$j]: icon: a Lucide name (drawn before the words; arrow-right after them)." ); } else { self::icon_known( (string) $button['icon'], "$at: buttons[$j]", $w ); } } elseif ( isset( $button['icon'] ) && ! AIEO_Content_Blocks::button_icon_style( (string) $button['icon'] ) ) { $w( "$at: buttons[$j]: icon {$button['icon']}: the theme does not register its Button style yet; the button prints without it." ); } if ( array_key_exists( 'new_tab', $button ) ) { if ( false !== $button['new_tab'] ) { $e( "$at: buttons[$j]: new_tab: false (a link off the site kept in this tab; external: true opens a new one)." ); } elseif ( ! empty( $button['external'] ) ) { $e( "$at: buttons[$j]: new_tab false and external true contradict each other." ); } elseif ( AIEO_Content_Rooster_One::NAME === self::$display ) { $w( "$at: buttons[$j]: new_tab: the rooster-one display's row opens a link off the site in a new tab (core's buttons carry it)." ); } } if ( isset( $button['aside'] ) && ! is_bool( $button['aside'] ) ) { $e( "$at: buttons[$j]: aside: true (a hero panel's button under its aside, behind the rule)." ); } elseif ( ! empty( $button['aside'] ) && ! str_ends_with( (string) $at, ': panel' ) ) { $w( "$at: buttons[$j]: aside is a hero panel's button's (under the panel's aside): here it is an ordinary button." ); } if ( isset( $button['solid'] ) && ! is_bool( $button['solid'] ) ) { $e( "$at: buttons[$j]: solid: true (the family's own colour, its light value, in both schemes, with white words, or the theme's dark foreground where white reads under 4.5:1)." ); } if ( isset( $button['style'] ) && ! in_array( (string) $button['style'], array( 'primary', 'secondary', 'outline' ), true ) ) { $e( "$at: buttons[$j]: style: primary, secondary or outline." ); } if ( ! empty( $button['trigger'] ) ) { $w( "$at: buttons[$j]: trigger {$button['trigger']} is not carried: a script trigger is not page content (the button keeps its href)." ); } if ( ! empty( $button['todo'] ) ) { $w( "$at: buttons[$j]: TODO: " . preg_replace( '/^TODO:\s*/', '', (string) $button['todo'] ) ); } } } private static function validate_links( $links, $at, $e ) { if ( null === $links ) { return; } if ( ! is_array( $links ) ) { $e( "$at: links: a list of { text, href } or { heading, items }." ); return; } $items = array_is_list( $links ) ? $links : (array) ( $links['items'] ?? array() ); foreach ( array_values( $items ) as $j => $link ) { if ( ! is_array( $link ) || '' === trim( (string) ( $link['text'] ?? '' ) ) || '' === trim( (string) ( $link['href'] ?? '' ) ) ) { $e( "$at: links[$j]: text and href are required." ); } elseif ( ! self::link_ok( (string) $link['href'] ) ) { $e( "$at: links[$j]: href {$link['href']} is not a link the compiler accepts." ); } } } private static function validate_kind( $kind, array $s, $at, array $media, $e, $w ) { $image = static function ( $ref, $what ) use ( $media, $e, $at ) { foreach ( array( 'desktop', 'mobile', 'src', 'path' ) as $side ) { $value = $ref[ $side ] ?? ''; $path = (string) ( is_array( $value ) ? ( $value['src'] ?? '' ) : $value ); if ( '' !== $path && ! array_key_exists( $path, $media ) ) { $e( "$at: $what.$side: $path is not in the spec's media map." ); } } }; switch ( $kind ) { case 'hero': if ( '' === trim( (string) ( $s['heading'] ?? '' ) ) ) { $e( "$at: heading is required." ); } if ( ! empty( $s['icon'] ) ) { self::icon_known( (string) $s['icon'], "$at: icon", $w ); } $e2 = array_intersect( array( 'badges_place', 'eyebrow_style', 'columns', 'gap' ), array_keys( $s ) ); if ( isset( $s['badges_place'] ) && ! in_array( (string) $s['badges_place'], AIEO_Content_Sections::BADGES_PLACES, true ) ) { $e( "$at: badges_place: " . implode( ' or ', AIEO_Content_Sections::BADGES_PLACES ) . ' (above: the badges over the heading).' ); } elseif ( isset( $s['badges_place'] ) && empty( $s['badges'] ) ) { $w( "$at: badges_place without badges." ); } if ( isset( $s['eyebrow_style'] ) && ! in_array( (string) $s['eyebrow_style'], AIEO_Content_Sections::EYEBROW_STYLES, true ) ) { $e( "$at: eyebrow_style: " . implode( ', ', AIEO_Content_Sections::EYEBROW_STYLES ) . ' (the library\'s pill, the hero\'s icon in it).' ); } elseif ( isset( $s['eyebrow_style'] ) && empty( $s['eyebrow'] ) ) { $w( "$at: eyebrow_style without an eyebrow." ); } elseif ( ! empty( $s['icon'] ) && ! AIEO_Content_Sections::is_pill_eyebrow( $s ) && '' === self::$display ) { $w( "$at: icon: the hero draws its icon in the eyebrow's pill (eyebrow_style: pill) only." ); } if ( isset( $s['columns'] ) && null === AIEO_Content_Layout::split( $s['columns'] ) ) { $e( "$at: columns: the split of the words and the picture or the panel, a/b (1.2fr/1fr, 58/42)." ); } elseif ( isset( $s['columns'] ) && ( empty( $s['image'] ) && empty( $s['panel'] ) || 'center' === (string) ( $s['align'] ?? '' ) ) ) { $w( "$at: columns has no split to make: the hero has no picture or panel beside its words (or it is centred, the picture under them)." ); } if ( isset( $s['gap'] ) && ( ( empty( $s['image'] ) && empty( $s['panel'] ) ) || 'center' === (string) ( $s['align'] ?? '' ) || ! empty( $s['tiles'] ) ) ) { $w( "$at: gap has no split to space: the hero has no picture or panel beside its words (or it is centred, or the home's)." ); } if ( ! empty( $s['tiles'] ) && ( 'center' === (string) ( $s['align'] ?? '' ) || AIEO_Content_Sections::is_pill_eyebrow( $s ) ) ) { $w( "$at: the home's hero (tiles) draws its own pill and its words at the left: align and eyebrow_style are a landing hero's." ); } if ( ( $e2 || 'center' === (string) ( $s['align'] ?? '' ) ) && '' !== self::$display && 'home' !== self::$display ) { $w( "$at: " . implode( ', ', array_merge( $e2, 'center' === (string) ( $s['align'] ?? '' ) ? array( 'align' ) : array() ) ) . ': drawn by the theme\'s landing hero, not the ' . self::$display . ' display\'s.' ); } if ( isset( $s['emphasis'] ) && ! str_contains( (string) ( $s['heading'] ?? '' ), (string) $s['emphasis'] ) ) { $e( "$at: emphasis: words of the heading (they are drawn in the template's red)." ); } self::validate_r1_fields( array_intersect_key( $s, array( 'emphasis' => 1 ) ), $at, $e, $w ); if ( ! empty( $s['image'] ) ) { $image( (array) $s['image'], 'image' ); } if ( ! empty( $s['panel'] ) ) { $panel = (array) $s['panel']; foreach ( array_keys( $panel ) as $field ) { if ( ! in_array( (string) $field, array( 'heading', 'text', 'aside', 'items', 'buttons', 'note', 'pattern', 'fields', 'submit', 'consent', 'todo' ), true ) ) { $e( "$at: panel.$field is not a panel field the compiler knows." ); } } self::validate_buttons( (array) ( $panel['buttons'] ?? array() ), "$at: panel", $e, $w ); if ( ! empty( $panel['fields'] ) || ! empty( $panel['submit'] ) || ! empty( $panel['consent'] ) ) { $w( "$at: panel: a form (fields, submit, consent) is not page content; the pattern " . (string) ( $panel['pattern'] ?? '(none named)' ) . ' must provide it.' ); } if ( ! empty( $panel['pattern'] ) ) { self::pattern_known( (string) $panel['pattern'], "$at: panel", $w ); } if ( ! empty( $panel['todo'] ) ) { $w( "$at: panel: TODO: " . preg_replace( '/^TODO:\s*/', '', (string) $panel['todo'] ) ); } foreach ( (array) ( $panel['items'] ?? array() ) as $j => $item ) { if ( ! is_array( $item ) || '' === trim( (string) ( $item['title'] ?? '' ) ) ) { $e( "$at: panel.items[$j]: title is required." ); } elseif ( ! empty( $item['icon'] ) ) { self::icon_known( (string) $item['icon'], "$at: panel.items[$j]", $w ); } } } foreach ( (array) ( $s['tiles'] ?? array() ) as $j => $tile ) { if ( ! is_array( $tile ) || '' === trim( (string) ( $tile['title'] ?? '' ) ) ) { $e( "$at: tiles[$j]: title is required." ); } elseif ( ! empty( $tile['motif'] ) ) { self::pattern_known( (string) $tile['motif'], "$at: tiles[$j].motif", $w ); } } foreach ( (array) ( $s['on_this_page'] ?? array() ) as $j => $link ) { if ( ! is_array( $link ) || '' === trim( (string) ( $link['text'] ?? '' ) ) || '' === trim( (string) ( $link['href'] ?? '' ) ) ) { $e( "$at: on_this_page[$j]: text and href are required." ); } } break; case 'cards': $cards = $s['cards'] ?? null; if ( ! is_array( $cards ) || ! $cards ) { $e( "$at: cards: at least one card." ); break; } if ( isset( $s['layout'] ) && ! in_array( (string) $s['layout'], array( 'stack', 'row' ), true ) ) { $e( "$at: layout: stack or row." ); } if ( ! empty( $s['icon'] ) ) { self::icon_known( (string) $s['icon'], "$at: icon", $w ); } foreach ( array_values( $cards ) as $j => $card ) { if ( ! is_array( $card ) || ( '' === trim( (string) ( $card['title'] ?? '' ) ) && '' === trim( (string) ( $card['text'] ?? '' ) ) ) ) { $e( "$at: cards[$j]: a title or a text is required." ); continue; } foreach ( array_keys( $card ) as $field ) { if ( ! in_array( (string) $field, self::CARD_FIELDS, true ) ) { $e( "$at: cards[$j]: $field is not a card field the compiler knows." ); } } $family = (bool) array_intersect( AIEO_Content_Sections::FAMILY_FIELDS, array_keys( $card ) ); if ( ! $family && empty( $card['items'] ) ) { self::icon_known( (string) ( $card['icon'] ?? 'award' ), "$at: cards[$j]", $w ); } if ( ! empty( $card['href'] ) && ! self::link_ok( (string) $card['href'] ) ) { $e( "$at: cards[$j]: href {$card['href']} is not a link the compiler accepts." ); } if ( isset( $card['level'] ) && ( (int) $card['level'] < 2 || (int) $card['level'] > 4 ) ) { $e( "$at: cards[$j]: level: 2 to 4." ); } if ( isset( $card['layout'] ) && ! in_array( (string) $card['layout'], array( 'stack', 'row' ), true ) ) { $e( "$at: cards[$j]: layout: stack or row." ); } self::validate_r1_fields( $card, "$at: cards[$j]", $e, $w ); if ( ! empty( $card['items'] ) ) { $with_icons = array_filter( (array) $card['items'], static fn ( $i ) => is_array( $i ) && ! empty( $i['icon'] ) ); if ( $with_icons ) { $w( "$at: cards[$j]: the sub-list is a core list inside the card; its " . count( $with_icons ) . ' item icons are not shown.' ); } } if ( ! empty( $card['motif'] ) ) { self::pattern_known( (string) $card['motif'], "$at: cards[$j].motif", $w ); } if ( ! empty( $card['todo'] ) ) { $w( "$at: cards[$j]: TODO: " . preg_replace( '/^TODO:\s*/', '', (string) $card['todo'] ) ); } if ( ! empty( $card['note'] ) ) { $w( "$at: cards[$j]: note is not carried on a card (put it in the section's note)." ); } } break; case 'stats': $stats = $s['items'] ?? $s['stats'] ?? null; if ( ! is_array( $stats ) || ! $stats ) { $e( "$at: items: at least one { value, label }." ); break; } foreach ( array_values( $stats ) as $j => $stat ) { if ( ! is_array( $stat ) || '' === trim( (string) ( $stat['value'] ?? '' ) ) || '' === trim( (string) ( $stat['label'] ?? '' ) ) ) { $e( "$at: items[$j]: value and label are required." ); continue; } self::validate_r1_fields( array_intersect_key( $stat, array( 'tone' => 1 ) ), "$at: items[$j]", $e, $w ); } break; case 'pricing': $tiers = $s['tiers'] ?? null; if ( ! is_array( $tiers ) || ! $tiers ) { $e( "$at: tiers: at least one plan." ); break; } $all = array_values( $tiers ); if ( ! empty( $s['enterprise'] ) ) { $all[] = $s['enterprise']; } foreach ( $all as $j => $tier ) { $label = $j < count( $tiers ) ? "tiers[$j]" : 'enterprise'; if ( ! is_array( $tier ) || '' === trim( (string) ( $tier['name'] ?? '' ) ) ) { $e( "$at: $label: name is required." ); continue; } foreach ( array_keys( $tier ) as $field ) { if ( ! in_array( (string) $field, self::TIER_FIELDS, true ) ) { $e( "$at: $label: $field is not a tier field the compiler knows." ); } } if ( isset( $tier['features'] ) && ! is_array( $tier['features'] ) ) { $e( "$at: $label: features: a list of strings (a leading - marks one not included)." ); } if ( isset( $tier['tagline'] ) && ! is_string( $tier['tagline'] ) ) { $e( "$at: $label: tagline: a line of words (who the plan is for: \"For one shop\")." ); } elseif ( isset( $tier['tagline'] ) && AIEO_Content_Inline::plain( $tier['tagline'] ) !== $tier['tagline'] ) { $w( "$at: $label: tagline: plain words; its inline markup is not drawn (" . AIEO_Content_Inline::plain( $tier['tagline'] ) . ').' ); } self::validate_r1_fields( $tier, "$at: $label", $e, $w ); if ( ! empty( $tier['ctaHref'] ) && ! self::link_ok( (string) $tier['ctaHref'] ) ) { $e( "$at: $label: ctaHref {$tier['ctaHref']} is not a link the compiler accepts." ); } if ( ! empty( $tier['volume'] ) ) { $v = (array) $tier['volume']; if ( count( (array) ( $v['columns'] ?? array() ) ) < 2 || empty( $v['rows'] ) ) { $e( "$at: $label: volume: columns and rows are required." ); } $w( "$at: $label: the volume table makes the plan a core Card with a core table inside (the ruling), not a price-card block." ); } if ( ! empty( $tier['todo'] ) ) { $w( "$at: $label: TODO: " . preg_replace( '/^TODO:\s*/', '', (string) $tier['todo'] ) ); } } if ( ! empty( $s['includes'] ) && ( ! is_array( $s['includes'] ) || empty( $s['includes']['items'] ) ) ) { $e( "$at: includes: { heading, items }." ); } break; case 'table': $columns = $s['columns'] ?? null; if ( ! is_array( $columns ) || count( $columns ) < 2 ) { $e( "$at: columns: the feature column and at least one plan." ); } $rows = (array) ( $s['rows'] ?? array() ); foreach ( (array) ( $s['groups'] ?? array() ) as $g => $group ) { if ( ! is_array( $group ) || '' === trim( (string) ( $group['name'] ?? '' ) ) || empty( $group['rows'] ) ) { $e( "$at: groups[$g]: name and rows are required." ); continue; } if ( array_diff( array_keys( $group ), array( 'name', 'icon', 'tone', 'rows' ) ) ) { $e( "$at: groups[$g]: a group is { name, icon, tone, rows }." ); } if ( ! empty( $group['icon'] ) ) { self::icon_known( (string) $group['icon'], "$at: groups[$g]", $w ); } if ( isset( $group['tone'] ) && ! self::tone_word( $group['tone'] ) ) { $e( "$at: groups[$g]: tone: " . implode( ', ', self::TONES ) . ' or a family\'s accent slug (one, lms, zero, …).' ); } $rows = array_merge( $rows, (array) $group['rows'] ); } if ( ! $rows ) { $e( "$at: rows (or groups with rows): at least one." ); } foreach ( array_values( $rows ) as $j => $row ) { if ( ! is_array( $row ) || '' === trim( (string) ( $row[0] ?? '' ) ) ) { $e( "$at: rows[$j]: a list of cells, the first the feature." ); continue; } if ( is_array( $columns ) && count( $row ) !== count( $columns ) ) { $w( "$at: rows[$j]: " . count( $row ) . ' cells for ' . count( $columns ) . ' columns.' ); } foreach ( array_slice( $row, 1 ) as $c ) { if ( is_array( $c ) && ( ! in_array( (string) ( $c['mark'] ?? '' ), self::MARKS, true ) || array_diff( array_keys( $c ), array( 'mark', 'note' ) ) ) ) { $e( "$at: rows[$j]: a cell object is { mark: yes|no|soon|partial, note }." ); } elseif ( ! is_array( $c ) && ! is_scalar( $c ) ) { $e( "$at: rows[$j]: a cell is a string or { mark, note }." ); } } } if ( isset( $s['highlight'] ) && ! self::highlight_ok( $s['highlight'], is_array( $columns ) ? count( $columns ) : 0 ) ) { $e( "$at: highlight: a column index from 1 (0 for none), or a list of them ([2, 3, 4]), within the columns." ); } if ( isset( $s['marks'] ) && ! self::marks_ok( $s['marks'] ) ) { $e( "$at: marks: the words of yes, no, soon and partial, \"Included | Not included | Coming soon\" (or a list of up to four)." ); } if ( isset( $s['label'] ) && ! is_string( $s['label'] ) ) { $e( "$at: label: the scroll region's name, a string." ); } if ( isset( $s['phone'] ) && ! in_array( (string) $s['phone'], array( 'stack', 'scroll' ), true ) ) { $e( "$at: phone: stack or scroll." ); } break; case 'faq': $items = $s['items'] ?? null; if ( ! is_array( $items ) || ! $items ) { $e( "$at: items: at least one { q, a }." ); break; } foreach ( array_values( $items ) as $j => $item ) { $a = $item['a'] ?? ''; if ( ! is_array( $item ) || '' === trim( (string) ( $item['q'] ?? '' ) ) || ( is_array( $a ) ? ! $a : '' === trim( (string) $a ) ) ) { $e( "$at: items[$j]: q and a are required." ); } } break; case 'cta': if ( '' === trim( (string) ( $s['heading'] ?? '' ) ) && '' === trim( (string) ( $s['text'] ?? '' ) ) && empty( $s['buttons'] ) ) { $e( "$at: a heading, a text or buttons." ); } if ( ! empty( $s['icon'] ) ) { self::icon_known( (string) $s['icon'], "$at: icon", $w ); } if ( ! empty( $s['motif'] ) ) { self::pattern_known( (string) $s['motif'], "$at: motif", $w ); } break; case 'gallery': $images = $s['items'] ?? $s['images'] ?? null; if ( ! is_array( $images ) || ! $images ) { $e( "$at: items: at least one { src, alt }." ); break; } foreach ( array_values( $images ) as $j => $img ) { $path = (string) ( is_array( $img ) ? ( $img['src'] ?? $img['path'] ?? '' ) : $img ); if ( '' === $path || ! array_key_exists( $path, $media ) ) { $e( "$at: items[$j]: $path is not in the spec's media map." ); } if ( is_array( $img ) ) { foreach ( array_keys( $img ) as $field ) { if ( ! in_array( (string) $field, array( 'src', 'path', 'alt', 'caption', 'title', 'note', 'width', 'height', 'inline_svg' ), true ) ) { $e( "$at: items[$j]: $field is not a gallery item field the compiler knows." ); } } if ( ! empty( $img['inline_svg'] ) ) { $w( "$at: items[$j]: inline_svg is not carried: the SVG is an image block (one node, cached), not inline markup." ); } } } break; case 'prose': if ( empty( $s['body'] ) && empty( $s['content'] ) && empty( $s['markup'] ) && '' === trim( (string) ( $s['heading'] ?? '' ) ) ) { $e( "$at: body (a text or lines), markup or a heading." ); } break; case 'split': if ( empty( $s['image'] ) && empty( $s['aside'] ) && empty( $s['pattern'] ) ) { $w( "$at: neither image nor aside nor pattern: the section is one column of text." ); } if ( ! empty( $s['pattern'] ) ) { self::pattern_known( (string) $s['pattern'], "$at: pattern", $w ); } if ( ! empty( $s['scores'] ) ) { $w( "$at: scores are not carried: the pattern reads its own data (a site-plugin block fed from the scores JSON, plan 4.6)." ); } if ( ! empty( $s['image'] ) ) { if ( ! is_array( $s['image'] ) ) { $e( "$at: image: { desktop, mobile } (each a path, or { src, alt })." ); } else { $image( $s['image'], 'image' ); } } if ( isset( $s['side'] ) && ! in_array( (string) $s['side'], array( 'left', 'right' ), true ) ) { $e( "$at: side: left or right." ); } break; case 'form': self::validate_form( $s['form'] ?? null, $at, $e, $w ); break; case 'pattern': $slug = (string) ( $s['pattern'] ?? $s['slug'] ?? '' ); if ( '' === $slug ) { $e( "$at: pattern (the slug) is required." ); } else { self::pattern_known( $slug, $at, $w ); } if ( ! empty( $s['fields'] ) || ! empty( $s['submit'] ) || ! empty( $s['consent'] ) ) { $w( "$at: a form (fields, submit, consent) is not page content; the pattern $slug must provide it." ); } if ( ! empty( $s['scores'] ) ) { $w( "$at: scores are not carried: the pattern $slug reads its own data (a site-plugin block fed from the scores JSON, plan 4.6)." ); } break; } } private static function tone_word( $tone ) { return is_string( $tone ) && '' !== $tone && sanitize_key( $tone ) === $tone && ! in_array( $tone, self::NOT_TONES, true ); } private static function highlight_ok( $value, $count, $text = false ) { if ( $text && is_string( $value ) ) { if ( ! preg_match( '/^\s*\d+(?:\s*,\s*\d+)*\s*$/', $value ) ) { return '' === trim( $value ); } $list = array_map( 'intval', preg_split( '/\s*,\s*/', trim( $value ) ) ); return self::highlight_ok( 1 === count( $list ) ? $list[0] : $list, $count ); } $list = is_array( $value ); $items = $list ? $value : array( $value ); if ( ! $items || ( $list && ! array_is_list( $value ) ) ) { return false; } foreach ( $items as $c ) { if ( ! is_int( $c ) || $c < ( $list ? 1 : 0 ) || ( $count > 0 && $c >= $count ) ) { return false; } } return true; } private static function marks_ok( $value ) { if ( is_string( $value ) ) { return count( explode( '|', $value ) ) <= 4; } return is_array( $value ) && array_is_list( $value ) && count( $value ) <= 4 && ! array_filter( $value, static fn ( $w ) => ! is_string( $w ) ); } private static function icon_known( $slug, $at, $w ) { if ( 'none' !== (string) $slug && ! self::has_icon( $slug ) ) { $w( "$at: icon $slug is not in the theme's collection (added on request): the card renders without it." ); } } private static function validate_form( $form, $at, $e, $w ) { if ( ! is_array( $form ) ) { $e( "$at: form: an object, { \"form\": \"\", \"variant\": \"panel\", \"layout\": \"card|wash\", … }." ); return; } foreach ( array_keys( $form ) as $field ) { if ( ! in_array( (string) $field, self::FORM_FIELDS, true ) ) { $e( "$at: form.$field is not a field of a form placement (" . implode( ', ', self::FORM_FIELDS ) . ').' ); } } $key = (string) ( $form['form'] ?? '' ); if ( '' === $key || sanitize_title( $key ) !== $key ) { $e( "$at: form.form: the form's key (a slug) is required." ); } elseif ( class_exists( 'AIEO_Form_Render' ) && ! AIEO_Form_Render::find( $key ) ) { $w( "$at: form $key is not on this site yet: the section renders nothing until `wp aieo form apply` makes it." ); } $variant = (string) ( $form['variant'] ?? 'panel' ); if ( ! isset( self::FORM_VARIANTS[ $variant ] ) ) { $e( "$at: form.variant: " . implode( ', ', array_keys( self::FORM_VARIANTS ) ) . '.' ); } elseif ( isset( $form['layout'] ) && ! in_array( (string) $form['layout'], self::FORM_VARIANTS[ $variant ], true ) ) { $e( "$at: form.layout: " . implode( ' or ', self::FORM_VARIANTS[ $variant ] ) . '.' ); } if ( isset( $form['tone'] ) && ( '' === (string) $form['tone'] || sanitize_key( (string) $form['tone'] ) !== (string) $form['tone'] || in_array( (string) $form['tone'], self::NOT_TONES, true ) ) ) { $e( "$at: form.tone: " . implode( ', ', self::TONES ) . ' or a family\'s accent slug.' ); } if ( isset( $form['icon'] ) && ! self::has_icon( (string) $form['icon'] ) ) { $e( "$at: form.icon: " . (string) $form['icon'] . ' is not an icon of the theme (a Lucide slug).' ); } elseif ( isset( $form['icon'] ) && empty( $form['badge'] ) ) { $w( "$at: form.icon is the badge's: without a badge it is not drawn." ); } if ( isset( $form['level'] ) && ( (int) $form['level'] < 2 || (int) $form['level'] > 6 ) ) { $e( "$at: form.level: 2 to 6." ); } } private static function pattern_known( $slug, $at, $w ) { if ( class_exists( 'WP_Block_Patterns_Registry' ) && ! WP_Block_Patterns_Registry::get_instance()->is_registered( $slug ) ) { $w( "$at: pattern $slug is not registered on this site: it renders nothing until its plugin registers it." ); } } public static function has_icon( $slug ) { $slug = (string) $slug; if ( function_exists( 'rooster_has_icon' ) ) { return (bool) rooster_has_icon( $slug ); } if ( class_exists( 'Rooster\Core\Setup' ) && method_exists( 'Rooster\Core\Setup', 'icon_slugs' ) ) { return in_array( $slug, (array) \Rooster\Core\Setup::icon_slugs(), true ); } return '' !== $slug; } public static function link_ok( $href ) { $href = trim( (string) $href ); return (bool) preg_match( '#^(https?://[^\s]+|/[^\s]*|\#[^\s]*|mailto:[^\s]+|tel:[^\s]+)$#i', $href ); } public static function media_path_ok( $path ) { $path = (string) $path; if ( '' === $path || str_contains( $path, '..' ) || strpbrk( $path, "\\:\0" ) ) { return false; } return 0 === strpos( $path, 'media/' ) || 0 === strpos( $path, '/wp-content/uploads/' ) || 0 === strpos( $path, '/wp-content/themes/' ); } }