addSubMenu('Assist', $assist->slug, $cb); } // Always load the Library page. $library = new LibraryAdminPage(); $cb = [$library, 'pageContent']; $this->addSubMenu('Library', $library->slug, $cb); // Show the Launch menu for dev users. if (Config::$environment === 'DEVELOPMENT') { $this->addSubMenu('Launch', 'post-new.php?extendify=onboarding'); } }); // Hide the menu items unless in dev mode. if (Config::$environment === 'PRODUCTION') { add_action('admin_head', function () { echo ''; }); } // If the user is redirected to this while visitng our url, intercept it. \add_filter('wp_redirect', function ($url) { // Check for extendify-launch-success as other plugins will not override // this as they intercept the request. // phpcs:ignore WordPress.Security.NonceVerification if (isset($_GET['extendify-launch-success'])) { return \admin_url() . $this->getRoute(); } return $url; }, 9999); // Intercept requests and redirect as needed. // phpcs:ignore WordPress.Security.NonceVerification if (isset($_GET['page']) && $_GET['page'] === 'extendify-admin-page') { header('Location: ' . \admin_url() . $this->getRoute(), true, 302); exit; } } /** * A helper for handling sub menus * * @param string $name - The menu name. * @param string $slug - The menu slug. * @param callable $callback - The callback to render the page. * * @return void */ public function addSubMenu($name, $slug, $callback = '') { \add_submenu_page( 'extendify-admin-page', $name, $name, Config::$requiredCapability, $slug, $callback ); } /** * Adds Extendify top menu * * @return void */ public function addAdminMenu() { \add_menu_page( 'Extendify', 'Extendify', Config::$requiredCapability, 'extendify-admin-page', '__return_null', // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode 'data:image/svg+xml;base64,' . base64_encode('') ); } /** * Routes pages accordingly * * @return string */ public function getRoute() { // If dev, redirect to assist always. if (Config::$environment === 'DEVELOPMENT') { return 'admin.php?page=extendify-assist'; } // If Launch/Assist isn't enabled, show the Library page. if (!Config::$showOnboarding) { return 'admin.php?page=extendify-welcome'; } // If they've yet to complete launch, send them back to Launch. if (!Config::$launchCompleted) { return 'post-new.php?extendify=onboarding'; } // If they made it this far, they can go to Assist. return 'admin.php?page=extendify-assist'; } }