$plan * @param array $changes * @return array */ /** * Where a section is: its type and page. Header and footer count as sections with no page. * * @param array|null $plan * @return array{type: string, page: string|null}|null Null when the plan has no such section. */ public static function findSection(?array $plan, string $sectionId): ?array { if ($plan === null) { return null; } if (in_array($sectionId, ['header', 'footer'], true)) { return is_array($plan[$sectionId] ?? null) ? ['type' => $sectionId, 'page' => null] : null; } foreach ($plan['pages'] ?? [] as $page) { foreach ($page['sections'] ?? [] as $section) { if (is_array($section) && ($section['id'] ?? null) === $sectionId) { return ['type' => (string) ($section['type'] ?? ''), 'page' => (string) ($page['id'] ?? '')]; } } } return null; } /** * Keep only the changes to one selected part of the store, so a request about "this" can never * touch the rest: updating the section, removing it (with the menu links that pointed to it), * or adding a new section right after it. The reply and store actions are kept as they are. * * @param array $changes * @return array */ public static function limitTo(array $changes, string $sectionId): array { $kept = array_intersect_key($changes, array_flip(['reply', 'actions'])); if (in_array($sectionId, ['header', 'footer'], true)) { if (is_array($changes[$sectionId] ?? null)) { $kept[$sectionId] = $changes[$sectionId]; } return $kept; } $updates = array_values(array_filter( self::list($changes, 'update'), fn ($update) => is_array($update) && ($update['id'] ?? null) === $sectionId, )); $additions = array_values(array_filter( self::list($changes, 'add'), fn ($addition) => is_array($addition) && ($addition['after'] ?? null) === $sectionId, )); if ($updates !== []) { $kept['update'] = $updates; } if ($additions !== []) { $kept['add'] = $additions; } if (in_array($sectionId, self::list($changes, 'remove'), true)) { $kept['remove'] = [$sectionId]; // Links to the removed section may need fixing in the menus. foreach (['header', 'footer'] as $part) { if (is_array($changes[$part] ?? null)) { $kept[$part] = $changes[$part]; } } } return $kept; } public static function apply(array $plan, array $changes): array { // Theme: merge only the parts that were given. if (is_array($changes['theme'] ?? null) && is_array($plan['theme'] ?? null)) { $plan['theme'] = array_replace_recursive($plan['theme'], $changes['theme']); } // Header and footer: a new variant and/or some content fields. foreach (['header', 'footer'] as $part) { if (is_array($changes[$part] ?? null) && is_array($plan[$part] ?? null)) { $plan[$part] = self::merge($plan[$part], $changes[$part]); } } $pages = array_values(array_filter($plan['pages'] ?? [], 'is_array')); // Remove whole pages. The home page can never be removed. $removePages = array_filter(self::list($changes, 'remove_pages'), 'is_string'); $pages = array_values(array_filter( $pages, fn (array $page) => ($page['id'] ?? null) === PlanSchema::HOME_PAGE || ! in_array($page['id'] ?? null, $removePages, true), )); // Rename pages. foreach (self::list($changes, 'update_pages') as $update) { foreach ($pages as $index => $page) { if (is_array($update) && ($page['id'] ?? null) === ($update['id'] ?? null) && is_string($update['title'] ?? null)) { $pages[$index]['title'] = $update['title']; } } } // Add whole new pages, at the end. $addedPages = []; foreach (self::list($changes, 'add_pages') as $page) { if (is_array($page)) { $pages[] = $page; $addedPages[] = $page; } } // We do not rely on the AI to keep the menus right: links to removed pages go, // and every new page gets a link so shoppers can reach it. $plan = self::dropLinksTo($plan, array_diff($removePages, [PlanSchema::HOME_PAGE])); foreach ($addedPages as $page) { $plan = self::ensureLinkTo($plan, $page); } $remove = array_filter(self::list($changes, 'remove'), 'is_string'); $order = array_values(array_filter(self::list($changes, 'order'), 'is_string')); foreach ($pages as $pageIndex => $page) { $sections = array_values(array_filter($page['sections'] ?? [], 'is_array')); // Update: change the variant and/or some content fields of existing sections. foreach (self::list($changes, 'update') as $update) { foreach ($sections as $index => $section) { if (is_array($update) && ($section['id'] ?? null) === ($update['id'] ?? null)) { $sections[$index] = self::merge($section, $update); } } } // Remove. $sections = array_values(array_filter( $sections, fn (array $section) => ! in_array($section['id'] ?? null, $remove, true), )); // Add: after a named section, or at the end of the named page (the home page if none is named). foreach (self::list($changes, 'add') as $addition) { $section = is_array($addition) ? ($addition['section'] ?? null) : null; if (! is_array($section) || ($addition['page'] ?? PlanSchema::HOME_PAGE) !== ($page['id'] ?? null)) { continue; } $position = count($sections); foreach ($sections as $index => $existing) { if (($existing['id'] ?? null) === ($addition['after'] ?? null)) { $position = $index + 1; } } array_splice($sections, $position, 0, [$section]); } // Order: only used on the page whose sections it names exactly once each. $ids = array_column($sections, 'id'); if ($order !== [] && count($order) === count($ids) && array_diff($ids, $order) === [] && array_diff($order, $ids) === []) { $byId = array_column($sections, null, 'id'); $sections = array_map(fn (string $id) => $byId[$id], $order); } $pages[$pageIndex]['sections'] = $sections; } $plan['pages'] = $pages; return $plan; } /** * Remove header and footer links that point to the given pages. * * @param array $plan * @param array $pageIds * @return array */ private static function dropLinksTo(array $plan, array $pageIds): array { if ($pageIds === []) { return $plan; } foreach (['header', 'footer'] as $part) { $links = $plan[$part]['content']['links'] ?? null; if (is_array($links)) { $plan[$part]['content']['links'] = array_values(array_filter( $links, fn (mixed $link) => ! is_array($link) || ! in_array(self::pageOf($link['href'] ?? null), $pageIds, true), )); } } return $plan; } /** * Make sure the header or the footer links to a new page. The header is used while it has room. * * @param array $plan * @param array $page * @return array */ private static function ensureLinkTo(array $plan, array $page): array { $id = $page['id'] ?? null; $title = $page['title'] ?? null; if (! is_string($id) || ! is_string($title)) { return $plan; } foreach (['header', 'footer'] as $part) { foreach ($plan[$part]['content']['links'] ?? [] as $link) { if (is_array($link) && self::pageOf($link['href'] ?? null) === $id) { return $plan; } } } // The limits match PlanSchema: 6 header links, 8 footer links. foreach (['header' => 6, 'footer' => 8] as $part => $limit) { $links = $plan[$part]['content']['links'] ?? null; if (is_array($links) && count($links) < $limit) { $plan[$part]['content']['links'][] = ['label' => mb_substr($title, 0, 30), 'href' => '/'.$id]; return $plan; } } return $plan; } /** * The page id a link such as "/about" or "/about#team" points to, or null for any other kind of link. */ private static function pageOf(mixed $href): ?string { if (! is_string($href) || ! str_starts_with($href, '/')) { return null; } return explode('/', ltrim((string) preg_replace('/[?#].*$/', '', $href), '/'))[0]; } /** * Take a new variant and replace only the content fields that were given. * * @param array $section * @param array $update * @return array */ private static function merge(array $section, array $update): array { if (is_string($update['variant'] ?? null)) { $section['variant'] = $update['variant']; } if (is_array($update['content'] ?? null)) { $section['content'] = array_replace($section['content'] ?? [], $update['content']); } return $section; } /** * @param array $changes * @return array */ private static function list(array $changes, string $key): array { return is_array($changes[$key] ?? null) ? $changes[$key] : []; } }