true ) ); if ( ! is_array( $data ) ) { return new WP_Error( 'aieo_content_json', sprintf( '%s is not JSON.', $file ) ); } return $data; } public static function menus( array $data ) { $out = array(); foreach ( (array) ( $data['menus'] ?? array() ) as $key => $menu ) { if ( ! is_array( $menu ) ) { continue; } $slug = (string) ( $menu['slug'] ?? ( is_string( $key ) ? $key : '' ) ); $out[] = array( $slug, $menu ); } return $out; } public static function validate( array $data ) { $errors = array(); $warnings = array(); if ( 0 !== strpos( (string) ( $data['$schema'] ?? '' ), 'rooster-menus@' ) ) { $errors[] = sprintf( '$schema must be %s.', AIEO_Content::MENUS_SCHEMA ); } foreach ( array_keys( $data ) as $field ) { if ( ! in_array( (string) $field, array( '$schema', 'source', 'menus', 'families' ), true ) ) { $errors[] = sprintf( '%s is not a field the compiler knows (menus file).', (string) $field ); } } if ( ! empty( $data['families'] ) ) { $warnings[] = 'families are not carried by the menus: the mega menu (plan 4.5, phase 10) reads them.'; } $menus = self::menus( $data ); if ( ! $menus ) { $errors[] = 'menus: at least one.'; return array( 'errors' => $errors, 'warnings' => $warnings ); } foreach ( $menus as [ $slug, $menu ] ) { if ( '' === $slug || sanitize_title( $slug ) !== $slug ) { $errors[] = sprintf( 'menu %s: a lower-case slug is required.', $slug ); continue; } if ( '' === trim( (string) ( $menu['title'] ?? '' ) ) ) { $errors[] = "menu $slug: title is required."; } foreach ( array_keys( $menu ) as $field ) { if ( ! in_array( (string) $field, self::MENU_FIELDS, true ) ) { $errors[] = "menu $slug: $field is not a menu field the compiler knows."; } } $family = (string) ( $menu['family'] ?? '' ); if ( ! empty( $menu['cta'] ) && '' === $family ) { $warnings[] = "menu $slug: the cta button is not carried: the header part holds its own button (a family menu's cta is its panel's call to action)."; } if ( '' !== $family && sanitize_key( $family ) !== $family ) { $errors[] = "menu $slug: family: a slug."; } if ( isset( $menu['colour'] ) || isset( $menu['colour_dark'] ) ) { $accents = self::accents(); if ( '' === $family ) { $warnings[] = "menu $slug: colour and colour_dark are not stored and name no family: the colours are the theme's design.accents (content/recipe.json)."; } elseif ( null === $accents ) { $warnings[] = "menu $slug: colour and colour_dark are not stored (the theme's design.accents carry the family's colours); the theme is not active here, so the accent $family could not be checked."; } elseif ( ! in_array( $family, $accents, true ) ) { $warnings[] = "menu $slug: colour and colour_dark are not stored, and the family $family is not among the theme's design.accents (" . implode( ', ', $accents ) . '): add the accent (content/recipe.json) or the panel draws without it.'; } } if ( empty( $menu['items'] ) || ! is_array( $menu['items'] ) ) { $errors[] = "menu $slug: items: at least one."; } else { self::validate_items( (array) $menu['items'], "menu $slug", $errors, $warnings ); } } return array( 'errors' => $errors, 'warnings' => $warnings ); } private static function is_foot_text( array $item ) { return in_array( $item['place'] ?? '', array( 'foot', 'foot-end' ), true ) && ! array_intersect_key( $item, array_flip( self::TARGETS ) ); } private static function validate_items( array $items, $at, array &$errors, array &$warnings, $mega = false ) { foreach ( array_values( $items ) as $i => $item ) { if ( ! is_array( $item ) || '' === trim( (string) ( $item['label'] ?? '' ) ) ) { $errors[] = "$at: items[$i]: label is required."; continue; } foreach ( array_keys( $item ) as $field ) { if ( ! in_array( (string) $field, self::ITEM_FIELDS, true ) ) { $errors[] = "$at: items[$i]: $field is not an item field the compiler knows."; } } $targets = array_intersect_key( $item, array_flip( self::TARGETS ) ); if ( 1 !== count( $targets ) && ! ( $mega && self::is_foot_text( $item ) ) ) { $errors[] = "$at: items[$i]: one target (page, category, course_category, shop, courses, blog, myaccount or url)."; } if ( isset( $item['place'] ) ) { if ( ! in_array( $item['place'], self::PLACES, true ) ) { $errors[] = "$at: items[$i]: place: " . implode( ', ', self::PLACES ) . '.'; } elseif ( ! $mega ) { $errors[] = "$at: items[$i]: place belongs to a child of an item whose layout is mega (its panel's picture card and foot row)."; } } if ( isset( $item['url'] ) && ! AIEO_Content_Spec::link_ok( (string) $item['url'] ) ) { $errors[] = "$at: items[$i]: url {$item['url']} is not a link the compiler accepts."; } if ( ! empty( $item['accent'] ) && sanitize_key( (string) $item['accent'] ) !== (string) $item['accent'] ) { $errors[] = "$at: items[$i]: accent: a slug."; } if ( isset( $item['separator'] ) && 'before' !== $item['separator'] ) { $errors[] = "$at: items[$i]: separator: before (a thin rule before the item, a new group in the phone drawer)."; } if ( isset( $item['group'] ) && ( ! is_string( $item['group'] ) || '' === trim( $item['group'] ) ) ) { $errors[] = "$at: items[$i]: group: the heading of the group the item starts (text)."; } if ( ! empty( $item['icon'] ) ) { if ( sanitize_key( (string) $item['icon'] ) !== (string) $item['icon'] ) { $errors[] = "$at: items[$i]: icon: a Lucide slug."; } elseif ( function_exists( 'rooster_has_icon' ) && ! rooster_has_icon( (string) $item['icon'] ) ) { $warnings[] = "$at: items[$i]: icon {$item['icon']} is not in the theme's icon set: the entry draws without it."; } } if ( isset( $item['image'] ) || isset( $item['layout'] ) ) { if ( empty( $item['children'] ) ) { $errors[] = "$at: items[$i]: image and layout belong to an item with children (its panel's picture and layout)."; } if ( isset( $item['layout'] ) && ! in_array( $item['layout'], self::PANEL_LAYOUTS, true ) ) { $errors[] = "$at: items[$i]: layout: " . implode( ', ', self::PANEL_LAYOUTS ) . '.'; } if ( isset( $item['image'] ) ) { if ( ! is_int( $item['image'] ) && ! ( is_string( $item['image'] ) && '' !== trim( $item['image'] ) ) ) { $errors[] = "$at: items[$i]: image: an attachment's id, or the address of a file in the media library."; } elseif ( 0 === self::image_id( $item['image'] ) ) { $warnings[] = "$at: items[$i]: image {$item['image']} is not in this site's media library: the panel draws without it."; } } } if ( isset( $item['column'] ) && ( ! is_int( $item['column'] ) || $item['column'] < 1 || $item['column'] > 12 ) ) { $errors[] = "$at: items[$i]: column: the item's column in its entry's panel, a whole number from 1 to 12."; } if ( ! empty( $item['children'] ) ) { self::validate_items( (array) $item['children'], "$at: items[$i]", $errors, $warnings, 'mega' === ( $item['layout'] ?? '' ) ); } } } private static function image_id( $image ) { if ( is_int( $image ) || ( is_string( $image ) && ctype_digit( $image ) ) ) { $id = (int) $image; return $id > 0 && wp_attachment_is_image( $id ) ? $id : 0; } $url = trim( (string) $image ); if ( 0 === strpos( $url, '/' ) ) { $url = home_url( $url ); } $id = (int) attachment_url_to_postid( $url ); return $id > 0 && wp_attachment_is_image( $id ) ? $id : 0; } public static function apply( array $data, array $opts = array() ) { $check = self::validate( $data ); if ( $check['errors'] || ( ! empty( $opts['strict'] ) && $check['warnings'] ) ) { return new WP_Error( 'aieo_content_invalid', implode( "\n", array_merge( $check['errors'], ! empty( $opts['strict'] ) ? $check['warnings'] : array() ) ) ); } $write = 'apply' === (string) ( $opts['mode'] ?? 'draft' ) && empty( $opts['dry_run'] ); if ( $write && ! current_user_can( 'edit_theme_options' ) ) { return new WP_Error( 'aieo_content_forbidden', 'You may not change the site\'s menus (edit_theme_options).', array( 'status' => 403 ) ); } $report = array( 'mode' => $write ? 'apply' : 'dry-run', 'warnings' => $check['warnings'], 'menus' => array() ); $wrote = false; foreach ( self::menus( $data ) as [ $slug, $menu ] ) { $left_out = array(); $content = self::items( (array) $menu['items'], $left_out ); $row = array( 'slug' => $slug, 'left_out' => $left_out, 'links' => substr_count( $content, '\n" . $inner . "\n"; continue; } $out .= '\n"; } return $out; } private static function link( array $item ) { $label = (string) $item['label']; $extra = array(); if ( ! empty( $item['title'] ) ) { $extra['title'] = (string) $item['title']; } if ( ! empty( $item['description'] ) ) { $extra['description'] = (string) $item['description']; } if ( ! empty( $item['external'] ) ) { $extra['opensInNewTab'] = true; } if ( ! empty( $item['badge'] ) ) { $extra['roosterBadge'] = (string) $item['badge']; } if ( ! empty( $item['accent'] ) ) { $extra['roosterAccent'] = sanitize_key( (string) $item['accent'] ); } if ( ! empty( $item['icon'] ) ) { $extra['roosterIcon'] = sanitize_key( (string) $item['icon'] ); } if ( isset( $item['separator'] ) && 'before' === $item['separator'] ) { $extra['roosterSeparator'] = 'before'; } if ( ! empty( $item['group'] ) && is_string( $item['group'] ) ) { $extra['roosterGroup'] = sanitize_text_field( $item['group'] ); } if ( isset( $item['column'] ) && is_int( $item['column'] ) && $item['column'] >= 1 && $item['column'] <= 12 ) { $extra['roosterColumn'] = $item['column']; } if ( isset( $item['place'] ) && in_array( $item['place'], self::PLACES, true ) ) { $extra['roosterPlace'] = (string) $item['place']; } if ( ! empty( $item['children'] ) ) { $image = isset( $item['image'] ) ? self::image_id( $item['image'] ) : 0; if ( $image > 0 ) { $extra['roosterImage'] = $image; } if ( isset( $item['layout'] ) && in_array( $item['layout'], self::PANEL_LAYOUTS, true ) ) { $extra['roosterLayout'] = (string) $item['layout']; } } if ( isset( $item['page'] ) ) { $found = AIEO_Content_Compiler::find( array( 'key' => (string) $item['page'], 'slug' => (string) $item['page'] ) ); $post = $found ? $found['post'] : null; if ( ! $post instanceof WP_Post || 'trash' === $post->post_status ) { return null; } return array( 'label' => $label, 'type' => 'page', 'id' => (int) $post->ID, 'url' => (string) get_permalink( $post ), 'kind' => 'post-type' ) + $extra; } foreach ( array( 'category' => 'product_cat', 'course_category' => 'rooster_course_category' ) as $key => $taxonomy ) { if ( isset( $item[ $key ] ) ) { $term = taxonomy_exists( $taxonomy ) ? get_term_by( 'slug', (string) $item[ $key ], $taxonomy ) : false; $link = $term instanceof WP_Term ? get_term_link( $term ) : null; if ( ! is_string( $link ) ) { return null; } return array( 'label' => $label, 'type' => $taxonomy, 'id' => (int) $term->term_id, 'url' => $link, 'kind' => 'taxonomy' ) + $extra; } } $url = null; if ( ! empty( $item['shop'] ) ) { $url = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'shop' ) : null; } elseif ( ! empty( $item['courses'] ) ) { $url = post_type_exists( 'rooster_course' ) ? (string) get_post_type_archive_link( 'rooster_course' ) : null; } elseif ( ! empty( $item['blog'] ) ) { $url = get_option( 'page_for_posts' ) ? (string) get_permalink( (int) get_option( 'page_for_posts' ) ) : home_url( '/' ); } elseif ( ! empty( $item['myaccount'] ) ) { $url = function_exists( 'wc_get_page_permalink' ) ? wc_get_page_permalink( 'myaccount' ) : wp_login_url(); } elseif ( isset( $item['url'] ) ) { $url = (string) $item['url']; if ( 0 === strpos( $url, '/' ) ) { $url = home_url( $url ); } } if ( ( null === $url || '' === $url ) && self::is_foot_text( $item ) ) { return array( 'label' => $label, 'kind' => 'custom' ) + $extra; } if ( null === $url || '' === $url ) { return null; } return array( 'label' => $label, 'url' => $url, 'kind' => 'custom' ) + $extra; } }