[menu title, capability, hidden from menu]. */ public static function pages(): array { return array( 'dashboard' => array( __( 'Dashboard', 'translateplus' ), 'manage_options', false ), 'translate' => array( __( 'Translate site', 'translateplus' ), 'manage_options', false ), 'translations' => array( __( 'Translations', 'translateplus' ), Roles::CAP, false ), 'strings' => array( __( 'Strings', 'translateplus' ), Roles::CAP, false ), 'glossary' => array( __( 'Glossary', 'translateplus' ), Roles::CAP, false ), 'languages' => array( __( 'Languages', 'translateplus' ), 'manage_options', false ), 'tools' => array( __( 'Tools', 'translateplus' ), 'manage_options', false ), 'settings' => array( __( 'Settings', 'translateplus' ), 'manage_options', false ), 'editor' => array( __( 'Translation editor', 'translateplus' ), Roles::CAP, true ), ); } public static function slug_for( string $route ): string { return 'dashboard' === $route ? self::SLUG : self::SLUG . '-' . $route; } /** * Pages the current user may open. */ public static function visible_pages(): array { $out = array(); foreach ( self::pages() as $route => list( $title, $cap, $hidden ) ) { if ( current_user_can( $cap ) || current_user_can( 'manage_options' ) ) { $out[ $route ] = array( $title, $cap, $hidden ); } } return $out; } public function register(): void { add_action( 'admin_menu', array( $this, 'add_menu' ) ); add_action( 'admin_init', array( $this, 'activation_redirect' ) ); add_filter( 'plugin_action_links_' . plugin_basename( TRANSLATEPLUS_FILE ), array( $this, 'action_links' ) ); add_filter( 'submenu_file', array( $this, 'highlight_editor_submenu' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_global_styles' ) ); } public function add_menu(): void { $pages = self::visible_pages(); if ( ! $pages ) { return; } $icon = TRANSLATEPLUS_URL . 'assets/images/menu-icon.png'; // Translators without admin rights land on the Translations screen. $first = array_key_first( $pages ); $top_slug = self::slug_for( $first ); $top_cap = $pages[ $first ][1]; add_menu_page( __( 'TranslatePlus', 'translateplus' ), __( 'TranslatePlus', 'translateplus' ), $top_cap, $top_slug, array( $this, 'render' ), $icon, 81 ); foreach ( $pages as $route => list( $title, $cap, $hidden ) ) { add_submenu_page( $top_slug, // Hidden pages stay under this menu (their entry is hidden with CSS) so it stays highlighted. $title . ' ‹ ' . __( 'TranslatePlus', 'translateplus' ), $title, current_user_can( 'manage_options' ) ? 'manage_options' : $cap, self::slug_for( $route ), array( $this, 'render' ) ); } } /** * Styles needed on every admin screen (menu icon, posts list language column). */ public function enqueue_global_styles(): void { wp_enqueue_style( 'translateplus-admin-global', TRANSLATEPLUS_URL . 'assets/css/admin-global.css', array(), TRANSLATEPLUS_VERSION ); } /** * @param string|null $submenu_file Submenu slug. */ public function highlight_editor_submenu( $submenu_file ) { global $plugin_page; return self::slug_for( 'editor' ) === $plugin_page ? self::slug_for( 'translations' ) : $submenu_file; } public static function current_route(): string { $page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended if ( self::SLUG === $page ) { return 'dashboard'; } $route = substr( $page, strlen( self::SLUG ) + 1 ); return array_key_exists( $route, self::pages() ) ? $route : 'dashboard'; } public function render(): void { echo '
'; echo ''; echo '
'; } public function activation_redirect(): void { if ( ! get_transient( 'translateplus_activation_redirect' ) ) { return; } delete_transient( 'translateplus_activation_redirect' ); if ( wp_doing_ajax() || is_network_admin() || isset( $_GET['activate-multi'] ) || ! current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended return; } wp_safe_redirect( admin_url( 'admin.php?page=' . self::SLUG ) ); exit; } public function action_links( array $links ): array { $settings = ( new Settings() )->get( 'onboarded' ) ? '' . esc_html__( 'Settings', 'translateplus' ) . '' : '' . esc_html__( 'Get started', 'translateplus' ) . ''; array_unshift( $links, $settings ); return $links; } }