`. * * @since 4.0.5 * * @var string[] */ const PAGE_KEYS = [ 'redirects', 'logs', 'settings', 'about' ]; /** * Hook suffix of every registered plugin page, keyed by page key. * * @since 4.0.5 * * @var array */ private static $screens = []; /** * Get the plugin slug. * * Matches the wp.org slug and is used as the text domain. * * @since 4.0.0 * * @return string */ public static function slug(): string { return self::SLUG; } /** * Get the human-readable plugin name. * * Intentionally not translated — used in places where the brand * name needs to stay consistent (admin menu, plugin row). * * @since 4.0.0 * * @return string */ public static function name(): string { return '404 to 301'; } /** * Get the current plugin version. * * Resolves to whatever the `Version:` header in the main plugin * file declares (loaded into `AIOSEO_404_TO_301_VERSION` at boot). * * @since 4.0.0 * * @return string */ public static function version(): string { return AIOSEO_404_TO_301_VERSION; } /** * Get every plugin admin page as { url, slug } pairs. * * @since 4.0.0 * * @return array */ public static function pages(): array { $pages = []; foreach ( array_merge( self::PAGE_KEYS, PromoMenu::keys() ) as $key ) { $slug = '404-to-301-' . $key; $pages[ $key ] = [ 'url' => admin_url( 'admin.php?page=' . $slug ), 'slug' => $slug, ]; } return $pages; } /** * Get the admin URL of one of the plugin pages. * * @since 4.0.0 * * @param string $page Page key (logs, settings, redirects, about). * * @return string Absolute admin URL, or an empty string for an unknown page key. */ public static function getUrl( string $page = 'settings' ): string { $pages = self::pages(); return $pages[ $page ]['url'] ?? ''; } /** * Build the admin URL of the settings page. * * Convenience accessor used by plugin row links and CTAs. * * @since 4.0.0 * * @return string */ public static function settingsUrl(): string { return self::getUrl( 'settings' ); } /** * Remember the hook suffix WordPress returned for a plugin page. * * WordPress builds it from the translated parent menu title, so it can't be predicted. * * @since 4.0.5 * * @param string $page Page key. * @param string|false $hook Hook suffix from `add_menu_page()` / `add_submenu_page()`. * @return void */ public static function registerScreen( string $page, $hook ): void { if ( is_string( $hook ) && '' !== $hook ) { self::$screens[ $page ] = $hook; } } /** * The plugin page a screen id belongs to. * * @since 4.0.5 * * @param string $screenId Screen id or admin page hook suffix. * @return string Page key, or '' when the screen is not one of the plugin's. */ public static function pageKey( string $screenId ): string { $page = array_search( $screenId, self::$screens, true ); return false === $page ? '' : (string) $page; } /** * Whether the current admin screen is one of the plugin's pages. * * @since 4.0.0 * * @param string|null $page Optional page key to match against. * * @return bool */ public static function isPluginScreen( ?string $page = null ): bool { if ( ! function_exists( 'get_current_screen' ) ) { return false; } $screen = get_current_screen(); if ( ! $screen || empty( $screen->id ) ) { return false; } $key = self::pageKey( $screen->id ); return null === $page ? '' !== $key : $page === $key; } }