'', 'login_bg_color' => '#f0f0f1', 'footer_text' => '', 'hidden_menus' => [], 'hidden_widgets' => [], 'custom_widget_title' => '', 'custom_widget_content' => '', 'login_redirect_url' => '', 'hide_admin_bar_roles' => [], ]; public static function instance(): self { if ( null === self::$instance ) { self::$instance = new self(); } return self::$instance; } private function __construct() { add_action( 'admin_menu', [ $this, 'register_admin_menu' ] ); add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] ); // Init modules. $this->init_modules(); } /* ───── Admin menu ───── */ public function register_admin_menu(): void { /* * Other plugins from the same author may already have added a shared * parent menu. When one is there the page goes under it, so the * dashboard does not grow a second top level entry for the same set of * tools. When there is none, this plugin creates its own parent under * its own prefix and never defines the shared slug. */ $parent = 'agolab-tools'; if ( empty( $GLOBALS['admin_page_hooks'][ $parent ] ) ) { $parent = self::MENU_SLUG; add_menu_page( __( 'aGo Tools', 'ago-admin' ), __( 'aGo Tools', 'ago-admin' ), 'manage_options', $parent, '__return_null', 'dashicons-hammer', 81 ); } add_submenu_page( $parent, __( 'aGo Admin', 'ago-admin' ), __( 'Admin', 'ago-admin' ), 'manage_options', 'agoadmin', [ Admin\Page::class, 'render' ] ); remove_submenu_page( $parent, $parent ); } /** * Top level admin menu items the settings screen offers to hide, keyed by * the slug WordPress gives them. * * @return array */ public static function menu_items(): array { return [ 'index.php' => __( 'Dashboard', 'ago-admin' ), 'edit.php' => __( 'Posts', 'ago-admin' ), 'upload.php' => __( 'Media', 'ago-admin' ), 'edit.php?post_type=page' => __( 'Pages', 'ago-admin' ), 'edit-comments.php' => __( 'Comments', 'ago-admin' ), 'themes.php' => __( 'Appearance', 'ago-admin' ), 'plugins.php' => __( 'Plugins', 'ago-admin' ), 'users.php' => __( 'Users', 'ago-admin' ), 'tools.php' => __( 'Tools', 'ago-admin' ), 'options-general.php' => __( 'Settings', 'ago-admin' ), ]; } /** * Core dashboard widgets the settings screen offers to hide. * * @return array */ public static function dashboard_widgets(): array { return [ 'welcome_panel' => __( 'Welcome Panel', 'ago-admin' ), 'dashboard_quick_press' => __( 'Quick Draft', 'ago-admin' ), 'dashboard_primary' => __( 'WordPress Events & News', 'ago-admin' ), 'dashboard_activity' => __( 'Activity', 'ago-admin' ), 'dashboard_right_now' => __( 'At a Glance', 'ago-admin' ), 'dashboard_site_health' => __( 'Site Health Status', 'ago-admin' ), ]; } /** * Roles the settings apply to. Administrators are left out: they always * keep the full menu, so the settings screen stays reachable. * * @return array */ public static function editable_roles(): array { $roles = wp_roles()->get_names(); unset( $roles['administrator'] ); return $roles; } /* ───── REST routes ───── */ public function register_rest_routes(): void { register_rest_route( self::REST_NAMESPACE, '/settings', [ 'methods' => 'POST', 'callback' => [ $this, 'handle_save_settings' ], 'permission_callback' => static fn () => current_user_can( 'manage_options' ), ] ); } /** * Saves the settings screen. Each field is reduced to what the screen can * produce: a hex color, an http(s) image URL, an address on this site for * the redirect, and only the roles, menu items and widgets it lists. * * @return \WP_REST_Response|\WP_Error */ public function handle_save_settings( \WP_REST_Request $request ) { $input = (array) $request->get_json_params(); $settings = $this->get_settings(); $text = static fn ( $value ): string => is_scalar( $value ) ? (string) $value : ''; if ( isset( $input['login_redirect_url'] ) ) { $raw = trim( $text( $input['login_redirect_url'] ) ); $redirect = '' === $raw ? '' : wp_validate_redirect( esc_url_raw( $raw ), '' ); if ( '' !== $raw && '' === $redirect ) { return new \WP_Error( 'agoadmin_invalid_redirect', __( 'The redirect URL must be an address on this site.', 'ago-admin' ), [ 'status' => 400 ] ); } $settings['login_redirect_url'] = $redirect; } if ( isset( $input['login_bg_color'] ) ) { $settings['login_bg_color'] = sanitize_hex_color( $text( $input['login_bg_color'] ) ) ?: self::DEFAULTS['login_bg_color']; } if ( isset( $input['login_logo_url'] ) ) { $settings['login_logo_url'] = esc_url_raw( $text( $input['login_logo_url'] ), [ 'http', 'https' ] ); } if ( isset( $input['custom_widget_title'] ) ) { $settings['custom_widget_title'] = sanitize_text_field( wp_strip_all_tags( $text( $input['custom_widget_title'] ) ) ); } foreach ( [ 'footer_text', 'custom_widget_content' ] as $key ) { if ( isset( $input[ $key ] ) ) { $settings[ $key ] = wp_kses_post( $text( $input[ $key ] ) ); } } $roles = array_keys( self::editable_roles() ); if ( isset( $input['hidden_menus'] ) ) { $items = array_keys( self::menu_items() ); $clean = []; foreach ( (array) $input['hidden_menus'] as $role => $slugs ) { if ( ! in_array( (string) $role, $roles, true ) || ! is_array( $slugs ) ) { continue; } $slugs = array_values( array_intersect( array_filter( $slugs, 'is_string' ), $items ) ); if ( $slugs ) { $clean[ $role ] = $slugs; } } $settings['hidden_menus'] = $clean; } if ( isset( $input['hidden_widgets'] ) ) { $settings['hidden_widgets'] = array_values( array_intersect( array_filter( (array) $input['hidden_widgets'], 'is_string' ), array_keys( self::dashboard_widgets() ) ) ); } if ( isset( $input['hide_admin_bar_roles'] ) ) { $settings['hide_admin_bar_roles'] = array_values( array_intersect( array_filter( (array) $input['hide_admin_bar_roles'], 'is_string' ), $roles ) ); } update_option( 'agoadmin_settings', $settings ); return new \WP_REST_Response( [ 'saved' => true ] ); } /* ───── Assets ───── */ public function enqueue_assets( string $hook ): void { if ( ! str_ends_with( $hook, '_page_agoadmin' ) ) { return; } wp_enqueue_media(); wp_enqueue_style( 'wp-color-picker' ); wp_enqueue_style( 'agoadmin-admin', AGOADMIN_URL . 'assets/css/admin.css', [], AGOADMIN_VERSION ); wp_enqueue_script( 'agoadmin-admin', AGOADMIN_URL . 'assets/js/admin.js', [ 'wp-color-picker' ], AGOADMIN_VERSION, true ); wp_localize_script( 'agoadmin-admin', 'agoadminAdmin', [ 'restUrl' => rest_url( self::REST_NAMESPACE ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'i18n' => [ 'saving' => __( 'Saving...', 'ago-admin' ), 'saved' => __( 'Settings saved.', 'ago-admin' ), 'error' => __( 'Could not save settings.', 'ago-admin' ), 'selectImage' => __( 'Select Login Logo', 'ago-admin' ), 'useImage' => __( 'Use this image', 'ago-admin' ), 'logoAlt' => __( 'Login logo preview', 'ago-admin' ), ], ] ); } /* ───── Modules ───── */ private function init_modules(): void { Modules\Login::init( $this->get_settings() ); Modules\Menu::init( $this->get_settings() ); Modules\Dashboard::init( $this->get_settings() ); } /** @return array */ public function get_settings(): array { $saved = get_option( 'agoadmin_settings', [] ); return wp_parse_args( $saved, self::DEFAULTS ); } }