schema map. * * @param string $dir Absolute path to the sections directory. * @return array Associative array of section_slug => schema. */ public function scan_dir( $dir ) { $sections = array(); if ( ! is_dir( $dir ) ) { return $sections; } $files = glob( $dir . '/*.php' ); if ( ! $files ) { return $sections; } foreach ( $files as $file ) { $schema = $this->parse_file( $file ); if ( null === $schema ) { continue; } $error = $this->validate( $schema ); if ( true !== $error ) { continue; } $slug = basename( $file, '.php' ); $sections[ $slug ] = $schema; } return $sections; } /** * Validate a parsed schema array. * * @param array $schema * @return true|string True if valid, error message string if not. */ public function validate( $schema ) { if ( empty( $schema['name'] ) ) { return 'Missing required field: name'; } if ( ! isset( $schema['settings'] ) || ! is_array( $schema['settings'] ) ) { return 'Missing required field: settings'; } $organizer_types = array( 'header', 'paragraph' ); foreach ( $schema['settings'] as $i => $setting ) { if ( empty( $setting['type'] ) ) { return "Setting {$i}: missing type"; } if ( empty( $setting['id'] ) && ! in_array( $setting['type'], $organizer_types, true ) ) { return "Setting {$i}: missing id"; } } // Validate blocks array if present (PRD ยง4.6) if ( isset( $schema['blocks'] ) ) { if ( ! is_array( $schema['blocks'] ) ) { return 'blocks must be an array'; } foreach ( $schema['blocks'] as $i => $block_type ) { if ( empty( $block_type['type'] ) ) { return "Block type {$i}: missing type"; } if ( empty( $block_type['name'] ) ) { return "Block type {$i}: missing name"; } if ( ! isset( $block_type['settings'] ) || ! is_array( $block_type['settings'] ) ) { return "Block type {$i}: missing settings array"; } } } // Validate optional assets key (per-section script/style dependencies). if ( isset( $schema['assets'] ) ) { if ( ! is_array( $schema['assets'] ) ) { return 'assets must be an object'; } foreach ( array( 'scripts', 'styles' ) as $group ) { if ( ! isset( $schema['assets'][ $group ] ) ) { continue; } if ( ! is_array( $schema['assets'][ $group ] ) ) { return "assets.{$group} must be an array"; } foreach ( $schema['assets'][ $group ] as $i => $item ) { if ( empty( $item['handle'] ) ) { return "assets.{$group}[{$i}]: missing handle"; } if ( empty( $item['src'] ) ) { return "assets.{$group}[{$i}]: missing src"; } } } } return true; } }