entry). Part of the contract: never rename. */ public const PLUGINS_KEY = 'ops404_ui_menu_plugins'; /** Candidacy global (copy class name => claim). Part of the contract: never rename. */ public const COPIES_KEY = 'ops404_ui_menu_copies'; /** Stylesheet global (kit version => handle already enqueued). Part of the contract: never rename. */ public const STYLES_KEY = 'ops404_ui_styles'; /** The capability a registry entry gets when its writer names none. */ public const DEFAULT_CAPABILITY = 'manage_options'; /** The dashicon a registry entry gets when its writer names none. */ public const DEFAULT_DASHICON = 'dashicons-admin-generic'; /** * Record a copy's candidacy. Idempotent per copy; a later claim for the * same copy replaces the earlier one (same code, so nothing to lose). */ public static function claim( string $copy, int $schema, string $version ): void { if ( ! isset( $GLOBALS[ self::COPIES_KEY ] ) || ! is_array( $GLOBALS[ self::COPIES_KEY ] ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- the key is a contract constant, deliberately unprefixed so every copy agrees on it (see Contract's class docblock). $GLOBALS[ self::COPIES_KEY ] = array(); } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- the key is a contract constant, deliberately unprefixed so every copy agrees on it (see Contract's class docblock). $GLOBALS[ self::COPIES_KEY ][ $copy ] = array( 'schema' => $schema, 'version' => $version, ); } /** * The winning copy's class name, or null when no copy has claimed. */ public static function owner(): ?string { $winner = null; $best = null; foreach ( self::claims() as $copy => $claim ) { if ( null === $best || self::outranks( $claim, $copy, $best, (string) $winner ) ) { $winner = $copy; $best = $claim; } } return $winner; } /** * Every well-formed claim this request. Unknown fields a newer copy * added are carried, never dropped. * * @return array */ public static function claims(): array { $raw = $GLOBALS[ self::COPIES_KEY ] ?? array(); if ( ! is_array( $raw ) ) { return array(); } $claims = array(); foreach ( $raw as $copy => $claim ) { if ( is_string( $copy ) && '' !== $copy && is_array( $claim ) && isset( $claim['schema'], $claim['version'] ) && is_int( $claim['schema'] ) && $claim['schema'] >= 1 && is_string( $claim['version'] ) ) { $claims[ $copy ] = $claim; } } return $claims; } /** * Write this copy's entry for a plugin, unless a copy that outranks it * already wrote that slug. A copy never replaces an entry of a higher * `schema` (the schema is never lowered), and an entry it leaves alone * keeps every field, including ones this copy does not know. * * @param string $copy The writing copy's `Menu` class name. * @param string $version The writing copy's kit version. * @param string $slug The plugin slug (registry key). * @param string|callable():string $label Display name; a callable is resolved at render time only. * @param string $submenu_slug The plugin's own `add_submenu_page()` menu slug. * @param string|callable():string $description One-line description; lazy like `$label`. * @param string $dashicon Dashicon class for the landing-page row. * @param string $capability Capability a user needs to see the row. */ public static function register( string $copy, string $version, string $slug, $label, string $submenu_slug, $description, string $dashicon, string $capability ): void { if ( ! isset( $GLOBALS[ self::PLUGINS_KEY ] ) || ! is_array( $GLOBALS[ self::PLUGINS_KEY ] ) ) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- the key is a contract constant, deliberately unprefixed so every copy agrees on it (see Contract's class docblock). $GLOBALS[ self::PLUGINS_KEY ] = array(); } $entry = array( 'schema' => self::SCHEMA, 'label' => $label, 'submenu_slug' => $submenu_slug, 'description' => $description, 'dashicon' => '' !== $dashicon ? $dashicon : self::DEFAULT_DASHICON, 'capability' => '' !== $capability ? $capability : self::DEFAULT_CAPABILITY, 'version' => $version, 'copy' => $copy, ); $existing = isset( $GLOBALS[ self::PLUGINS_KEY ][ $slug ] ) && is_array( $GLOBALS[ self::PLUGINS_KEY ][ $slug ] ) ? self::normalize( $GLOBALS[ self::PLUGINS_KEY ][ $slug ] ) : null; // Same copy: the plugin re-registering itself, later call wins. A // different copy: only if it outranks what is there. if ( null !== $existing && $existing['copy'] !== $copy && self::outranks( $existing, $existing['copy'], $entry, $copy ) ) { return; } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- the key is a contract constant, deliberately unprefixed so every copy agrees on it (see Contract's class docblock). $GLOBALS[ self::PLUGINS_KEY ][ $slug ] = $entry; } /** * The merged registry: every well-formed entry, normalised, sorted by * slug. Unknown fields a newer copy wrote are kept. * * @return array> */ public static function plugins(): array { $raw = $GLOBALS[ self::PLUGINS_KEY ] ?? array(); if ( ! is_array( $raw ) ) { return array(); } $plugins = array(); foreach ( $raw as $slug => $entry ) { if ( ! is_string( $slug ) || '' === $slug || ! is_array( $entry ) ) { continue; } $normal = self::normalize( $entry ); if ( null !== $normal ) { $plugins[ $slug ] = $normal; } } ksort( $plugins ); return $plugins; } /** * A registry entry in canonical shape, or null when it cannot be used * (no schema, no label, no link target). Known fields first in a fixed * order, then any field a newer copy added, untouched. * * @param array $entry * @return array|null */ private static function normalize( array $entry ): ?array { if ( ! isset( $entry['schema'], $entry['label'], $entry['submenu_slug'] ) || ! is_int( $entry['schema'] ) || $entry['schema'] < 1 || ! ( is_string( $entry['label'] ) || is_callable( $entry['label'] ) ) || ! is_string( $entry['submenu_slug'] ) || '' === $entry['submenu_slug'] ) { return null; } $description = $entry['description'] ?? ''; $dashicon = $entry['dashicon'] ?? ''; $capability = $entry['capability'] ?? ''; $version = $entry['version'] ?? ''; $copy = $entry['copy'] ?? ''; $known = array( 'schema' => $entry['schema'], 'label' => $entry['label'], 'submenu_slug' => $entry['submenu_slug'], 'description' => is_string( $description ) || is_callable( $description ) ? $description : '', 'dashicon' => is_string( $dashicon ) && '' !== $dashicon ? $dashicon : self::DEFAULT_DASHICON, 'capability' => is_string( $capability ) && '' !== $capability ? $capability : self::DEFAULT_CAPABILITY, 'version' => is_string( $version ) ? $version : '', 'copy' => is_string( $copy ) ? $copy : '', ); return $known + $entry; } /** * Does $a (written by copy $a_copy) outrank $b (copy $b_copy)? Contract * schema first, then kit version, then the lowest copy name: a total * order, so the answer never depends on load order. * * @param array $a * @param array $b */ private static function outranks( array $a, string $a_copy, array $b, string $b_copy ): bool { if ( $a['schema'] !== $b['schema'] ) { return $a['schema'] > $b['schema']; } $by_version = version_compare( (string) $a['version'], (string) $b['version'] ); if ( 0 !== $by_version ) { return $by_version > 0; } return strcmp( $a_copy, $b_copy ) < 0; } }