(string) ($pageValue['menu_id'] ?? ''), 'side_bar_visible' => self::toggle($pageValue['side_bar_visible'] ?? true), 'pages' => [], ]; if (isset($pageValue['existing_menu'])) { $post['existing_menu'] = [ 'menu_name' => (string) ($pageValue['existing_menu']['menu_name'] ?? ''), 'menu_slug' => (string) ($pageValue['existing_menu']['menu_slug'] ?? ''), ]; } else { $menu = (array) ($pageValue['menu'] ?? []); $post['menu'] = [ 'menu_title' => (string) ($menu['menu_title'] ?? ''), 'menu_icon' => (string) ($menu['icon_url'] ?? ''), 'menu_position' => (string) ($menu['position'] ?? ''), ]; } foreach (array_values((array) ($pageValue['pages'] ?? [])) as $key => $page) { $post['pages'][(string) $key] = self::page((array) $page); } return $post; } private static function page(array $page): array { $post = [ 'menu_title' => (string) ($page['menu_title'] ?? ''), 'subpage_title' => (string) ($page['subpage_title'] ?? ''), 'select_title' => (string) ($page['select_title'] ?? 'None'), 'role' => ($page['capability'] ?? '') === 'edit_others_posts' ? 'editor' : 'administrator', 'subpage_position' => isset($page['subpage_position']) ? (string) $page['subpage_position'] : '', 'subpage_toggle' => self::toggle($page['subpage_toggle'] ?? true), 'subpage_slug' => (string) ($page['menu_slug'] ?? ''), 'subpage_slug_suffix' => (string) ($page['subpage_slug_suffix'] ?? ''), 'fields' => [], ]; // A page of 1.1 has its roles; a page of 1.0 posts its level, as the editor of 1.0 did. if (isset($page['roles']) && is_array($page['roles'])) { unset($post['role']); $post = self::insertAfter($post, 'select_title', ['roles' => array_values($page['roles'])]); } if (! empty($page['include_tab'])) { $post['subpage_tab'] = '1'; } foreach (array_values((array) ($page['fields'] ?? [])) as $key => $field) { $post['fields'][(string) $key] = self::field((array) $field); } return $post; } /** * @param array $array * @param string $key The key to insert after. * @param array $items */ private static function insertAfter(array $array, string $key, array $items): array { $position = array_search($key, array_keys($array), true); return array_slice($array, 0, $position + 1, true) + $items + array_slice($array, $position + 1, null, true); } /** * A field posts what it stores, apart from its page, which the form calls `page_slug`. Values the editor never * posted (null) are left out. */ private static function field(array $field): array { $post = []; foreach ($field as $key => $value) { if ($value === null) { continue; } // The form posts the autoload only when it is switched off; "yes" is what a missing value means. if ($key === 'toggle_autoload' && $value === 'yes') { continue; } if ($key === 'page_name') { $key = 'page_slug'; } $post[$key] = is_bool($value) ? self::toggle($value) : (is_scalar($value) ? (string) $value : $value); } return $post; } private static function toggle($value): string { return $value === true || $value === 'true' ? 'true' : 'false'; } }