__( 'Label', $domain )` instead of a pre-resolved * string, deferring the real translation lookup to render time -- always * well after `init`. * * LANDING PAGE / SINGLE-PLUGIN REACHABILITY: the parent's own page (the * callback given to `add_menu_page()`) always renders something useful -- * an informative empty state when no plugin has registered yet, * otherwise a linked row per registered plugin (label, description, * dashicon) -- see `render()`. This is deliberately how this class * satisfies the design spec's "a plugin installed alone must still be * obvious and reachable": a single registered plugin still yields a * single, clearly linked row on that landing page. * * (WordPress core separately auto-inserts a same-labelled duplicate * top-level link as an actual sidebar SUBMENU row the first time any * plugin calls `add_submenu_page()` under this parent -- a cosmetic wart * many plugins simply live with. Reliably suppressing it would require * THIS class to win an `admin_menu` priority race against every * consuming plugin's OWN `add_submenu_page()` call -- exactly the kind of * registration-order dependence the design spec explicitly warns against * for menu POSITION, and not worth reintroducing here for a purely * cosmetic wrinkle that is, besides, outside `ui-kit`'s control: the * `add_submenu_page()` calls themselves belong to each plugin's own * bootstrap, never to this file.) */ final class Menu { private const PARENT_SLUG = '404ops'; // No broader than every child needs: this is the same capability // `ConnectionScreen` uses for the identical class of screen (a // site-admin-only settings/status surface), and every 404ops plugin's // own admin pages already gate on it. private const CAPABILITY = 'manage_options'; // Past every WordPress core item (Dashboard..Settings top out at 99, // itself the final core separator) so the parent never contests a // core slot or lands in the middle of the list -- the design spec's // own complaint about the CURRENT ~20-top-level-item state. A fixed // slug + fixed title + fixed position is also what keeps this from // moving between page loads: WordPress's own add_menu_page() resolves // a same-position collision with a deterministic hash of the slug and // title (wp-admin/includes/plugin.php), not registration order, so as // long as OUR slug/title never change, our resolved position never // does either, regardless of what else is active on the site. private const POSITION = 100; // The PARENT's own single sidebar icon -- unrelated to per-plugin // dashicons (see SUGGESTED_DASHICONS below). WordPress submenu rows // carry no icon of their own, so a per-plugin dashicon is only ever // visible on the landing page's own list, never in the sidebar. private const PARENT_DASHICON = 'dashicons-networking'; /** * A curated, mutually-distinct set of dashicons for `register_plugin()` * callers to pick from (requirement: Agent Gate, Knowledge Base, Link * Sentinel and Mail Relay currently all show `dashicons-admin-generic` * because nothing coordinated the choice). Not exhaustive and not * enforced -- just a shared menu so 21 plugins don't each have to * invent, or grep every sibling's source to avoid colliding with, * their own list. * * @var list */ public const SUGGESTED_DASHICONS = array( 'dashicons-shield', 'dashicons-lock', 'dashicons-admin-links', 'dashicons-chart-bar', 'dashicons-email-alt', 'dashicons-backup', 'dashicons-database', 'dashicons-forms', 'dashicons-cart', 'dashicons-images-alt2', 'dashicons-bell', 'dashicons-search', 'dashicons-clock', 'dashicons-flag', 'dashicons-book', 'dashicons-megaphone', ); private static bool $registered = false; /** * Registers this copy's candidacy for the shared "404ops" parent menu on * its first call, and arms its `admin_menu` listener; every later call in * the same copy is a safe no-op. Whichever copy wins the election adds the * ONE top-level entry (see the class docblock). Always returns the parent * slug -- first call or not, winner or not -- so every caller can use the * return value directly as `add_submenu_page()`'s `$parent_slug`, exactly * where the design spec's own sketch calls it: * * $parent = \Ops404\ActivityLog\Vendor\Ops404\Ui\Menu::parent( '404ops-link-sentinel' ); * add_submenu_page( $parent, $title, $title, 'manage_options', '404ops-link-sentinel', $cb ); * * (In a plugin that bundles a prefixed copy, `\Ops404\ActivityLog\Vendor\Ops404\Ui\Menu` is the * plugin's own prefixed name for it.) * * @param string $text_domain Ignored since 0.6.1: this class's strings use * the literal placeholder domain, which the * consumer's build rewrites to its own (class * docblock, "TEXT DOMAIN"). Kept so every * existing call site compiles unchanged; its * removal is a named deferral for the next * consumer sweep. */ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found -- kept for call-site compatibility until the next consumer sweep; see the @param. public static function parent( string $text_domain ): string { if ( self::$registered ) { return self::PARENT_SLUG; } self::$registered = true; // One claim per copy (this class name is unique per copy); the // election itself runs in add_menu_page(), long after every plugin's // bootstrap has claimed. Contract::claim( self::class, Contract::SCHEMA, Ui::VERSION ); // Priority 9, deliberately, not the default 10. // // WordPress builds a submenu's hook suffix at REGISTRATION time, and // derives the `_page_` prefix from $admin_page_hooks -- an // array only add_menu_page() ever writes to. So a plugin whose // add_submenu_page() runs BEFORE this parent is registered binds its // render callback to `admin_page_`, a hookname WordPress never // dispatches for it: the screen comes up BLANK. That is a different // failure from the 403 you get when the parent never registers at all, // and a nastier one to read. // // Every consumer registers its own admin_menu listener at the default // 10, so running at 9 makes the parent exist first no matter what // order the plugins bootstrapped in. Without this the correctness of // the whole fleet rests on each plugin happening to call // Menu::parent() before its own add_action( 'admin_menu', ... ) -- // true today, but one refactor away from a blank screen nobody // connects to menu registration. add_action( 'admin_menu', array( self::class, 'add_menu_page' ), 9 ); return self::PARENT_SLUG; } /** * Declares a plugin for the landing page (design spec requirement: * the parent's landing page must be "useful, not a stub" -- a list of * what's installed, linking to each). Mirrors * `Ops404\Ai\KeyStore::register_plugin()`'s shape and lazy-label * handling exactly -- see the class docblock's "REGISTRY / LAZY * LABELS" section. Safe to call every request (e.g. from the calling * plugin's own bootstrap, alongside `parent()`); a later call for the * same `$plugin_slug` from the same copy simply replaces the earlier * one. The entry goes into the registry every copy shares (see * `Contract::register()` for what happens when two copies write one slug). * * @param string $plugin_slug Unique plugin * identifier (e.g. * `404ops-link-sentinel`). * @param string|callable():string $label Display name. A * callable is resolved * lazily, never by * this method itself * -- see the class * docblock. * @param string $submenu_slug The slug the plugin * passed as its own * `add_submenu_page()` * `$menu_slug` (under * the parent * `parent()` returned) * -- the landing page * links here. * @param string|callable():string $description Optional one-line * description shown * under the label. * Lazily resolved, * same as `$label`. * @param string $dashicon Optional dashicon * class for the * landing-page row * (e.g. * `dashicons-shield`; * see * `SUGGESTED_DASHICONS`). * Falls back to * WordPress's own * generic icon when * omitted or blank. * @param string $capability Optional capability a * user needs for the * landing page to list * this plugin's row -- * name the one the * plugin's own * `add_submenu_page()` * uses. Defaults to * `manage_options`. */ public static function register_plugin( string $plugin_slug, $label, string $submenu_slug, $description = '', string $dashicon = Contract::DEFAULT_DASHICON, string $capability = Contract::DEFAULT_CAPABILITY ): void { Contract::register( self::class, Ui::VERSION, $plugin_slug, $label, $submenu_slug, $description, $dashicon, $capability ); } /** * The admin hook suffix WordPress will give a page registered under the * shared parent -- i.e. what `add_submenu_page()` returns, derived * rather than remembered. * * This exists because of a silent failure that bites every plugin * moving off its own top-level menu. Screens gate their asset enqueue * on the hook suffix: * * if ( 'toplevel_page_my-plugin' !== $hook ) { return; } * * The moment that page becomes a submenu, WordPress computes a * different suffix, the comparison stops matching, and every * stylesheet and script for that screen silently stops loading. The * page still returns HTTP 200 -- it is simply unstyled and inert, so * no status-code sweep, phpcs run or unit test can see it. Nineteen * such hardcoded suffixes existed across twelve plugins when the * shared parent was introduced. * * Prefer this over hardcoding, and over storing the return value of * `add_submenu_page()`: it needs no state, works from any callback, * and if the parent slug ever changes there is exactly one place to * fix rather than nineteen. * * public function assets( string $hook ): void { * if ( Menu::hook( '404ops-link-sentinel' ) !== $hook ) { * return; * } * ... * } * * @param string $submenu_slug The slug passed to `add_submenu_page()`. */ public static function hook( string $submenu_slug ): string { return self::PARENT_SLUG . '_page_' . $submenu_slug; } /** * Registered plugins -- from EVERY copy, merged by the contract -- with * `$label` / `$description` resolved to plain strings: the landing page's * own data source, and the inspectable form tests assert against. * Resolution happens HERE, on read, never in `register_plugin()` itself -- * see the class docblock. * * @return array */ public static function known_plugins(): array { $known = array(); foreach ( Contract::plugins() as $slug => $entry ) { $known[ $slug ] = array( 'label' => self::resolve( $entry['label'] ), 'submenu_slug' => $entry['submenu_slug'], 'description' => self::resolve( $entry['description'] ), 'dashicon' => $entry['dashicon'], 'capability' => $entry['capability'], ); } return $known; } /** * @param string|callable():string $value */ private static function resolve( $value ): string { // is_string() is the discriminator, not is_callable(): a plain // string label is ALWAYS literal text, even one that happens to // spell a real PHP function name (is_callable('array_map') is // true) -- same reasoning as KeyStore::known_plugins(). return is_string( $value ) ? $value : (string) $value(); } public static function add_menu_page(): void { // Another copy won the election: it adds the one top-level entry and // renders the landing page. This copy only registered. A copy that // never claimed (add_menu_page() reached without parent()) is the // only candidate by default, as before. $owner = Contract::owner(); if ( null !== $owner && self::class !== $owner ) { return; } // Belt and braces: something else already put a top-level "404ops" // entry on this request (a copy that does not speak the contract). // A second one would be a duplicate sidebar item, so yield. global $admin_page_hooks; if ( is_array( $admin_page_hooks ) && isset( $admin_page_hooks[ self::PARENT_SLUG ] ) ) { return; } $hook_suffix = add_menu_page( '404ops', '404ops', self::CAPABILITY, self::PARENT_SLUG, array( self::class, 'render' ), self::PARENT_DASHICON, self::POSITION ); // Real WordPress core's add_menu_page() returns false when the // current user lacks the capability (its own docblock: "@return // string|false") -- same loose-falsy guard, same reasoning, as // ConnectionScreen::add_menu_page(): a real hook_suffix is never // legitimately '', and php-stubs types this narrower (string // only) than core's real behaviour. if ( ! $hook_suffix ) { return; } add_action( 'admin_enqueue_scripts', static function ( string $current_hook ) use ( $hook_suffix ): void { if ( $current_hook === $hook_suffix ) { Ui::enqueue(); } } ); } public static function render(): void { echo '
'; Render::header( __( 'Installed plugins', '404ops-activity-log' ), __( 'Every 404ops plugin active on this site, in one place.', '404ops-activity-log' ) ); self::render_plugin_list(); echo '
'; } private static function render_plugin_list(): void { // A row is for the users who can open that plugin's own screen; the // landing page itself already needs `manage_options`. Outside // WordPress (no current_user_can()) nothing is filtered. $known = array_filter( self::known_plugins(), static fn( array $plugin ): bool => ! function_exists( 'current_user_can' ) || current_user_can( $plugin['capability'] ) ); if ( array() === $known ) { Render::empty_state( __( 'No 404ops plugins have registered yet.', '404ops-activity-log' ), __( 'A plugin appears here once it calls Menu::register_plugin().', '404ops-activity-log' ) ); return; } echo ''; echo ''; echo ''; echo ''; foreach ( $known as $plugin ) { echo ''; echo ''; echo ''; echo ''; } echo '
' . esc_html( __( 'Icon', '404ops-activity-log' ) ) . '' . esc_html( __( 'Plugin', '404ops-activity-log' ) ) . '
' . esc_html( $plugin['label'] ) . ''; if ( '' !== $plugin['description'] ) { echo '
' . esc_html( $plugin['description'] ) . ''; } echo '
'; } private static function plugin_url( string $submenu_slug ): string { return admin_url( 'admin.php?page=' . $submenu_slug ); } }