'; /** * The Dashboard screen renderer -- constructed once so * `register_menu()` can wire its widget setup to the exact hook * suffix WordPress assigns the Dashboard submenu, and * `render_dashboard()` can reuse that same instance. * * @var DashboardScreen */ private DashboardScreen $dashboard_screen; /** * Constructor. * * @param SettingsRegistry $settings The settings registry. * @param ModuleManager $modules The module manager. * @param SettingsPage $settings_page The Settings screen renderer. */ public function __construct( private SettingsRegistry $settings, private ModuleManager $modules, private SettingsPage $settings_page ) { $this->dashboard_screen = new DashboardScreen( $settings ); } /** * Hook everything. * * @return void */ public function register(): void { // Priority 9: this top-level menu must exist in the global $menu // array before any module's own add_submenu_page( self::SLUG, ... ) // call runs (modules boot, and hook their own admin_menu callbacks, // before Menu::register() is even called -- see Plugin::boot()). // Registering a submenu before its parent exists doesn't break the // submenu list itself, but it does make WordPress fall back to a // generic hook name ("admin_page_{slug}") instead of the one // user_can_access_admin_page() later expects // ("all-in-one-utilities_page_{slug}"), which made every such // submenu page 403 as "not allowed to access this page". add_action( 'admin_menu', array( $this, 'register_menu' ), 9 ); // Priority 999: every sibling plugin's own admin_menu callback // (Woo Add-on, Tools add-on, Email Log -- all default priority 10) // must have already registered its submenu before this reorders // the combined result. See reorder_submenu()'s own docblock for // why this can't be done via add_submenu_page()'s $position arg. // After the add-ons' own admin_menu callbacks, so their license filters are in. add_action( 'admin_menu', array( $this, 'register_license_menu' ), 20 ); add_action( 'admin_menu', array( $this, 'reorder_submenu' ), 999 ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); UpgradeMenu::register(); PagePicker::register(); add_action( 'admin_page_access_denied', array( $this, 'redirect_legacy_pages' ) ); } /** * All-In-One Utilities → License, when an add-on with a license is active. * * @return void */ public function register_license_menu(): void { if ( ! LicenseScreen::products() ) { return; } add_submenu_page( self::SLUG, __( 'License', 'all-in-one-utilities' ), __( 'License', 'all-in-one-utilities' ), 'manage_options', self::SLUG . '-license', array( $this, 'render_license' ) ); } /** * License screen. * * @return void */ public function render_license(): void { ( new LicenseScreen() )->render(); } /** * Register menu and submenus. * * @return void */ public function register_menu(): void { add_menu_page( __( 'All-In-One Utilities', 'all-in-one-utilities' ), __( 'All-In-One Utilities', 'all-in-one-utilities' ), 'manage_options', self::SLUG, array( $this, 'render_dashboard' ), 'data:image/svg+xml;base64,' . base64_encode( self::ICON_SVG ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode -- WordPress's documented format for an SVG menu icon (see add_menu_page()). 66 ); $screens = array( array( __( 'Dashboard', 'all-in-one-utilities' ), self::SLUG, 'render_dashboard' ), array( __( 'Settings', 'all-in-one-utilities' ), self::SLUG . '-settings', 'render_settings' ), array( __( 'System Report', 'all-in-one-utilities' ), self::SLUG . '-system-report', 'render_system_report' ), array( __( 'Tools', 'all-in-one-utilities' ), self::SLUG . '-tools', 'render_tools' ), array( __( 'About', 'all-in-one-utilities' ), self::SLUG . '-about', 'render_about' ), ); foreach ( $screens as $screen ) { $hook = add_submenu_page( self::SLUG, $screen[0], $screen[0], 'manage_options', $screen[1], array( $this, $screen[2] ) ); // The Dashboard's own meta boxes (see DashboardScreen::setup()) // must be registered on load-{$hook}, which only fires for the // real hook suffix WordPress assigns this exact submenu -- // never assumed/hardcoded, since add_submenu_page() returning // it is the only reliable source. if ( self::SLUG === $screen[1] && is_string( $hook ) ) { $this->dashboard_screen->setup( $hook ); } } } /** * Reorder this plugin's own submenu, per the user's own explicit * request: "Dashboard. Settings, Woo, Tools, then other options." * * Runs at a very late `admin_menu` priority (999) so every sibling * plugin's own `admin_menu` callback (Woo Add-on, Tools add-on, * Email Log -- all default priority 10) has already registered its * own submenu before this reorders the combined `$submenu['aiou']` * array. There's no other way to do this: `add_submenu_page()`'s own * `$position` argument only ever inserts relative to whatever's * already registered *at the moment of that specific call* -- it * can't account for another plugin's own submenu that hasn't been * registered yet, since Free's own items (priority 9) are always * added before any sibling plugin's (priority 10) regardless of * what position either one asks for. * * Matched by each entry's own menu slug (index 2 of the array * `add_submenu_page()` stores), never by its visible label. The Tools * add-on has no submenu of its own: its tools render on this plugin's * Tools screen. * * @return void */ public function reorder_submenu(): void { global $submenu; if ( empty( $submenu[ self::SLUG ] ) ) { return; } $preferred = array( self::SLUG, // Dashboard. self::SLUG . '-settings', // Settings. 'aiou-woo', // WooCommerce (Woo Add-on), if active. self::SLUG . '-tools', // Tools (with the Tools add-on's cards, if active). ); $entries = $submenu[ self::SLUG ]; $ordered = array(); foreach ( $preferred as $slug ) { foreach ( $entries as $key => $entry ) { if ( isset( $entry[2] ) && $slug === $entry[2] ) { $ordered[] = $entry; unset( $entries[ $key ] ); break; } } } // Then everything else, in whatever order it was already in. foreach ( $entries as $entry ) { $ordered[] = $entry; } $submenu[ self::SLUG ] = $ordered; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- deliberate: reordering a plugin's own already-registered submenu across plugin boundaries has no other API; this only ever reorders entries add_submenu_page() itself already validated and added, never adds/removes any. } /** * Version string for one of this plugin's asset files: the plugin * version plus the file's last-modified time, so browsers fetch a new * copy whenever the file changes, not only on a version bump. * * @param string $relative Path inside the plugin, e.g. 'assets/css/admin.css'. * @return string */ public static function asset_version( string $relative ): string { $mtime = @filemtime( AIOU_PATH . $relative ); // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- a missing file just falls back to the plugin version. return AIOU_VERSION . ( $mtime ? '.' . $mtime : '' ); } /** * Enqueue admin assets on this plugin's screens only. * * @param string $hook_suffix Current admin page hook suffix. * @return void */ public function enqueue_assets( string $hook_suffix ): void { if ( false === strpos( $hook_suffix, self::SLUG ) ) { return; } wp_enqueue_style( 'aiou-admin', AIOU_URL . 'assets/css/admin.css', array(), self::asset_version( 'assets/css/admin.css' ) ); wp_enqueue_script( 'aiou-admin', AIOU_URL . 'assets/js/admin-settings.js', array(), self::asset_version( 'assets/js/admin-settings.js' ), true ); wp_localize_script( 'aiou-admin', 'aiouAdmin', array( 'mediaTitle' => __( 'Select image', 'all-in-one-utilities' ), 'mediaButton' => __( 'Use this image', 'all-in-one-utilities' ), 'confirmReset' => __( 'This cannot be undone. Continue?', 'all-in-one-utilities' ), 'copied' => __( 'Copied!', 'all-in-one-utilities' ), 'loading' => __( 'Loading...', 'all-in-one-utilities' ), 'bodyNotStored' => __( 'The message body was not stored for this email.', 'all-in-one-utilities' ), 'emailLogViewNonce' => wp_create_nonce( 'aiou-email-log-view' ), 'collapseAll' => __( 'Collapse all', 'all-in-one-utilities' ), 'expandAll' => __( 'Expand all', 'all-in-one-utilities' ), 'noResults' => __( 'No settings match your search.', 'all-in-one-utilities' ), /* translators: 1: number of settings switched on, 2: number of on/off settings in the section */ 'sectionCount' => __( '(%1$d out of %2$d on)', 'all-in-one-utilities' ), 'collapseMenu' => __( 'Collapse menu', 'all-in-one-utilities' ), 'expandMenu' => __( 'Expand menu', 'all-in-one-utilities' ), 'userPicker' => array( 'tooShort' => __( 'Keep typing -- at least 2 characters.', 'all-in-one-utilities' ), 'searching' => __( 'Searching…', 'all-in-one-utilities' ), 'none' => __( 'No users match.', 'all-in-one-utilities' ), 'error' => __( 'Could not search right now. Press Enter to try again.', 'all-in-one-utilities' ), ), 'codeEditor' => array( 'oneError' => __( '1 error -- this code may not work as expected:', 'all-in-one-utilities' ), /* translators: %d: number of errors */ 'errors' => __( '%d errors -- this code may not work as expected:', 'all-in-one-utilities' ), /* translators: %d: line number */ 'line' => __( 'Line %d', 'all-in-one-utilities' ), ), 'pagePicker' => array( 'nonce' => wp_create_nonce( PagePicker::NONCE ), 'action' => PagePicker::AJAX, 'searching' => __( 'Searching…', 'all-in-one-utilities' ), 'none' => __( 'Nothing matches. Try another word.', 'all-in-one-utilities' ), 'error' => __( 'Could not search right now. Try again.', 'all-in-one-utilities' ), /* translators: %d: number of results */ 'count' => __( '%d results. Use the up and down arrows to choose.', 'all-in-one-utilities' ), ), ) ); if ( false !== strpos( $hook_suffix, 'aiou-settings' ) ) { wp_enqueue_media(); } } /** * Dashboard screen. * * @return void */ public function render_dashboard(): void { $this->dashboard_screen->render(); } /** * Settings screen. * * @return void */ public function render_settings(): void { $this->settings_page->render(); } /** * Send the old Import/Export page (now a tab of the Tools screen) to * its new place, so old bookmarks and links keep working. WordPress * refuses an unregistered page in wp-admin/menu.php, before * admin_init, so this runs on the hook that fires right before that. * * @return void */ public function redirect_legacy_pages(): void { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only redirect. if ( ! isset( $_GET['page'] ) || self::SLUG . '-import-export' !== $_GET['page'] || ! current_user_can( 'manage_options' ) ) { return; } wp_safe_redirect( ToolsScreen::url( ImportExportScreen::TAB ) ); exit; } /** * System Report screen. * * @return void */ public function render_system_report(): void { ( new SystemReportScreen( $this->modules ) )->render(); } /** * Tools screen. * * @return void */ public function render_tools(): void { ( new ToolsScreen( $this->settings ) )->render(); } /** * About screen. * * @return void */ public function render_about(): void { ( new AboutScreen() )->render(); } }