Page slug => screen hook suffix, filled in on registration. */ private $screens = array(); public function __construct( Plugin $plugin ) { $this->plugin = $plugin; } public function register(): void { add_action( 'admin_menu', array( $this, 'addMenu' ) ); // The menu appears on every admin screen, so its styling must not depend on being on ours. add_action( 'admin_enqueue_scripts', array( $this, 'enqueueMenuStyles' ) ); ( new Assets( $this->plugin, $this ) )->register(); } public function addMenu(): void { $capability = 'manage_options'; add_menu_page( __( 'MigrateX', 'migratex' ), __( 'MigrateX', 'migratex' ), $capability, self::SLUG_EXPORT, // No callback here: the first submenu below shares this slug and renders the page. // Giving both a callback makes WordPress print the screen twice. '', // "none", not an SVG data URI: WordPress repaints those in the scheme's grey. The icon is // set from CSS instead (see enqueueMenuStyles), which keeps its colour. 'none', 80 ); $this->screens[ self::SLUG_EXPORT ] = (string) add_submenu_page( self::SLUG_EXPORT, __( 'Export Site', 'migratex' ), __( 'Export', 'migratex' ), $capability, self::SLUG_EXPORT, array( new ExportPage( $this->plugin ), 'render' ) ); $this->screens[ self::SLUG_IMPORT ] = (string) add_submenu_page( self::SLUG_EXPORT, __( 'Import Site', 'migratex' ), __( 'Import', 'migratex' ), $capability, self::SLUG_IMPORT, array( new ImportPage( $this->plugin ), 'render' ) ); $this->screens[ self::SLUG_BACKUPS ] = (string) add_submenu_page( self::SLUG_EXPORT, __( 'Backups', 'migratex' ), __( 'Backups', 'migratex' ), $capability, self::SLUG_BACKUPS, array( new BackupsPage( $this->plugin ), 'render' ) ); $this->screens[ self::SLUG_SCHEDULES ] = (string) add_submenu_page( self::SLUG_EXPORT, __( 'Schedules', 'migratex' ), __( 'Schedules', 'migratex' ), $capability, self::SLUG_SCHEDULES, array( new SchedulesPage( $this->plugin ), 'render' ) ); $this->screens[ self::SLUG_REMOTE ] = (string) add_submenu_page( self::SLUG_EXPORT, __( 'Site-to-Site', 'migratex' ), __( 'Site-to-Site', 'migratex' ), $capability, self::SLUG_REMOTE, array( new SiteToSitePage( $this->plugin ), 'render' ) ); $this->screens[ self::SLUG_RESET ] = (string) add_submenu_page( self::SLUG_EXPORT, __( 'Reset Hub', 'migratex' ), __( 'Reset Hub', 'migratex' ), $capability, self::SLUG_RESET, array( new ResetPage( $this->plugin ), 'render' ) ); } /** * The admin-menu icon and its brand colours, on every admin page. * * The two icon images (Royal Blue at rest, white on the coloured active/hover background) are * passed in as CSS custom properties, so assets/css/menu.css stays free of url() and the * icon artwork lives in one place, Icons. */ public function enqueueMenuStyles(): void { $menu_css = MIGRATEX_DIR . 'assets/css/menu.css'; $menu_ver = file_exists( $menu_css ) ? (string) filemtime( $menu_css ) : MIGRATEX_VERSION; // a changed file gets a new URL, so page caches and CDNs cannot serve the old one. wp_enqueue_style( 'migratex-menu', MIGRATEX_URL . 'assets/css/menu.css', array(), $menu_ver ); wp_add_inline_style( 'migratex-menu', sprintf( '#adminmenu #toplevel_page_migratex{--om-menu-icon:url("%s");--om-menu-icon-active:url("%s");}', Icons::menuIcon(), Icons::menuIconActive() ) ); } /** * Which of our screens a WordPress hook suffix is, or null for any other admin page. */ public function pageForHook( string $hook_suffix ): ?string { $page = array_search( $hook_suffix, $this->screens, true ); return false === $page ? null : (string) $page; } }