admin = $admin; $this->title = $title; $this->menu_title = $menu_title; $this->capability = $capability; $this->menu_slug = $menu_slug; $this->view = $view; $this->parent_slug = $parent_slug; $this->icon = $icon; $this->position = $position; } /** * Returns the action for registering the page. * * @since 1.0.0 * * @return string */ private function get_action() { switch ( $this->admin ) { case static::ADMIN: return 'admin_menu'; case static::ADMIN_NETWORK: return 'network_admin_menu'; case static::ADMIN_USER: return 'user_admin_menu'; default: return ''; } } /** * Returns the callback for adding the page to the admin menu. * * @since 1.0.0 * * @return callable Callback for adding the page to the admin menu. */ private function get_callback() { if ( $this->parent_slug ) { return function() { $this->hook_name = add_submenu_page( $this->parent_slug, $this->title, $this->menu_title, $this->capability, $this->menu_slug, [ $this->view, 'render' ] ); }; } return function() { $this->hook_name = add_menu_page( $this->title, $this->menu_title, $this->capability, $this->menu_slug, [ $this->view, 'render' ], $this->icon, $this->position ); }; } /** * Returns the hook name of the menu page. * * @since 1.0.0 * * @return string The hook name of the menu page. */ public function get_hook_name() { return $this->hook_name; } /** * Registers the settings page. * * @since 1.0.0 * * @return bool Whether the page was registered successfully. */ public function register() { $action = $this->get_action(); if ( ! $action ) { return false; } return add_action( $action, $this->get_callback() ); } }