init(); // Shared backend header Screens::init(); // Add Separator add_action('admin_init', [$this, 'separator']); } /** * Initialize all menus * @return void */ public function fire(): void { // Get all Menus $menus = $this->all(); foreach ($menus as $menu) { // Init Menu if ($menu instanceof Initiable) $menu->init(); } } public function register(): void { // Get all Menus $menus = $this->all(); foreach ($menus as $menu) { // Not an instance of Menu if (!($menu instanceof Menu)) continue; // Main Menu if ($menu->type() === Base::MAIN) { $hook = add_menu_page( $menu->title(), $menu->label(), $menu->capability(), $menu->slug(), $menu->callback(), $menu->icon(), $menu->position() ); } // Sub-Menu else { $hook = add_submenu_page( $menu->parent(), $menu->title(), $menu->label(), $menu->capability(), $menu->slug(), $menu->callback(), $menu->position() ); } if (is_string($hook) && $hook !== '') Screens::menu($hook, $menu); } // Keep these routes registered, including their capabilities and load hooks. // They are reached through the Bookings and Settings header tabs. remove_submenu_page('', Resources::PAGE); remove_submenu_page('', Onboarding::PAGE); // WordPress creates the first submenu item from the main menu label. // Keep the main menu branded as DayTidy, but present its home link as // Home in the expanded submenu. global $submenu; if (isset($submenu['daytidy'][0][0])) { $submenu['daytidy'][0][0] = esc_html__('Home', 'daytidy'); } } /** * Get all the Menus * @return mixed[] */ public function all(): array { static $menus = null; // Fetch Menus if (!$menus) { $menus = []; $menus[] = new Dashboard(); if (post_type_exists(Offering::PT)) $menus[] = new Offerings(); $menus[] = new Settings(); $menus[] = new HelpSupport(); if (Permissions::manage()) $menus[] = new WebiliaConnect(); // Third Party Menus $menus = apply_filters('DayTidy/F__DayTidy/Menus__all/menus', $menus); } // Sort usort($menus, function ($a, $b) { // Ensure 'menu' type always comes first if ($a->type() === 'menu' && $b->type() !== 'menu') return -1; if ($a->type() !== 'menu' && $b->type() === 'menu') return 1; return $a->position() <=> $b->position(); }); return $menus; } /** * Add separator before DayTidy menus * @return void */ public function separator(): void { if (!is_admin()) return; global $menu; if (Arr::not($menu)) return; $sep = null; $do_start = null; $start = null; $end = null; $do_end = null; $i = 0; $previous = null; foreach ($menu as $m) { // Next menu of end is separator, so we don't need to add separator again if ($end and is_null($do_end) and isset($m['4']) and strpos($m['4'], 'menu-separator') !== false) $do_end = false; else if ($end and is_null($do_end)) $do_end = true; if (!$sep and isset($m['4']) and strpos($m['4'], 'menu-separator') !== false) $sep = $m; if (!$start and isset($m['5']) and strpos($m['5'], 'page_daytidy') !== false) $start = $i; if (!$end and isset($m['5']) and strpos($m['5'], 'page_daytidy') !== false) $end = $i + 1; // Previous menu of start is separator, so we don't need to add separator again if ($start and is_null($do_start) and isset($previous['4']) and strpos($previous['4'], 'menu-separator') !== false) $do_start = false; else if ($start and is_null($do_start)) $do_start = true; $i++; $previous = $m; if ($sep and $start and $end and !is_null($do_end)) break; } if (is_null($do_start)) $do_start = true; // Start not found. Maybe the current user is not an administrator if (!$start) return; // Separator not found if (!$sep) return; // Add First Separator if ($do_start) $menu = array_merge( array_slice($menu, 0, $start), [$sep], array_slice($menu, $start) ); if (isset($menu[$start - 1])) $menu[$start - 1][4] .= ' menu-top-last'; if (isset($menu[$start + 1])) $menu[$start + 1][4] .= ' menu-top-first'; if (isset($menu[$end - 1])) $menu[$end - 1][4] .= ' menu-top-last'; if (isset($menu[$end + 1])) $menu[$end + 1][4] .= ' menu-top-first'; } }