<?php
/**
 * Neutral administration hub shared by all Papy3D plugins.
 *
 * Every Papy3D plugin ships the same broker and one versioned implementation.
 * The broker selects the newest registered implementation while exposing one
 * stable API, option name, and menu slug to the entire suite.
 *
 * @package Papy3DSuite
 */

declare(strict_types=1);

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! class_exists( 'P3DSUITE_Admin_Hub', false ) ) {
	final class P3DSUITE_Admin_Hub {
		private const MENU_SLUG = 'p3dsuite-papy3d';

		/** @var array<string, class-string> */
		private static array $implementations = array();

		/** @var array<string, array<string, mixed>> */
		private static array $runtime_plugins = array();

		private static string $selected_class = '';

		/** Registers one versioned shared implementation. */
		public static function register_implementation( string $version, string $class ): void {
			if ( '' === $version || '' === $class || ! class_exists( $class, false ) ) {
				return;
			}
			self::$implementations[ $version ] = $class;
			self::$selected_class              = '';
		}

		/** Registers one active plugin with runtime labels and metadata. */
		public static function register_plugin( array $plugin ): void {
			$slug = isset( $plugin['slug'] ) && is_scalar( $plugin['slug'] ) ? sanitize_key( (string) $plugin['slug'] ) : '';
			if ( '' === $slug ) {
				return;
			}
			$plugin['slug']                 = $slug;
			self::$runtime_plugins[ $slug ] = $plugin;

			$legacy_class = self::legacy_hub_class();
			if ( '' !== $legacy_class && is_callable( array( $legacy_class, 'register_plugin' ) ) ) {
				call_user_func( array( $legacy_class, 'register_plugin' ), $plugin );
				return;
			}

			$class = self::implementation();
			if ( '' !== $class && is_callable( array( $class, 'boot' ) ) ) {
				call_user_func( array( $class, 'boot' ) );
			}
		}

		/** Persists locale-neutral routing metadata. */
		public static function persist_plugin( array $plugin ): void {
			$legacy_class = self::legacy_hub_class();
			if ( '' !== $legacy_class && is_callable( array( $legacy_class, 'persist_plugin' ) ) ) {
				call_user_func( array( $legacy_class, 'persist_plugin' ), $plugin );
				return;
			}

			$class = self::implementation();
			if ( '' !== $class && is_callable( array( $class, 'persist_plugin' ) ) ) {
				call_user_func( array( $class, 'persist_plugin' ), $plugin );
			}
		}

		/** Removes only one plugin card from the shared registry. */
		public static function unregister_plugin( string $slug ): void {
			$slug = sanitize_key( $slug );
			if ( '' === $slug ) {
				return;
			}
			unset( self::$runtime_plugins[ $slug ] );

			$legacy_class = self::legacy_hub_class();
			if ( '' !== $legacy_class && is_callable( array( $legacy_class, 'unregister_plugin' ) ) ) {
				call_user_func( array( $legacy_class, 'unregister_plugin' ), $slug );
			}

			$class = self::implementation();
			if ( '' !== $class && is_callable( array( $class, 'unregister_plugin' ) ) ) {
				call_user_func( array( $class, 'unregister_plugin' ), $slug );
			}
		}

		/** Returns the single shared parent menu slug. */
		public static function menu_slug(): string {
			$legacy_class = self::legacy_hub_class();
			if ( '' !== $legacy_class && is_callable( array( $legacy_class, 'menu_slug' ) ) ) {
				$slug = call_user_func( array( $legacy_class, 'menu_slug' ) );
				if ( is_string( $slug ) && '' !== $slug ) {
					return sanitize_key( $slug );
				}
			}

			$class = self::implementation();
			if ( '' !== $class && is_callable( array( $class, 'menu_slug' ) ) ) {
				$slug = call_user_func( array( $class, 'menu_slug' ) );
				if ( is_string( $slug ) && '' !== $slug ) {
					return sanitize_key( $slug );
				}
			}
			return self::MENU_SLUG;
		}

		/** Returns the selected implementation version. */
		public static function version(): string {
			$class = self::implementation();
			if ( '' !== $class && is_callable( array( $class, 'version' ) ) ) {
				$version = call_user_func( array( $class, 'version' ) );
				return is_string( $version ) ? $version : '';
			}
			return '';
		}

		/** Returns runtime metadata registered by all active Papy3D plugins. */
		public static function runtime_plugins(): array {
			return self::$runtime_plugins;
		}

		/** Returns a real historical hub, excluding P3DSUITE compatibility adapters. */
		private static function legacy_hub_class(): string {
			foreach ( array( 'P3DSG_Admin_Hub', 'P3DWAF_Admin_Hub', 'Papy3D_Admin_Hub' ) as $class ) {
				if ( ! class_exists( $class, false ) ) {
					continue;
				}
				if ( is_callable( array( $class, 'p3dsuite_bridge_version' ) ) ) {
					continue;
				}
				return $class;
			}
			return '';
		}

		/** Selects the newest compatible implementation registered by active plugins. */
		private static function implementation(): string {
			if ( '' !== self::$selected_class ) {
				return self::$selected_class;
			}
			if ( array() === self::$implementations ) {
				return '';
			}

			$versions = array_keys( self::$implementations );
			usort( $versions, 'version_compare' );
			$versions = array_reverse( $versions );
			foreach ( $versions as $version ) {
				$class = self::$implementations[ $version ];
				if ( class_exists( $class, false ) ) {
					self::$selected_class = $class;
					return $class;
				}
			}
			return '';
		}
	}
}

if ( ! class_exists( 'P3DSUITE_Admin_Hub_Implementation_1_4_0', false ) ) {
	final class P3DSUITE_Admin_Hub_Implementation_1_4_0 {

		private const OPTION_NAME = 'p3dsuite_admin_hub_plugins';
		private const MENU_SLUG   = 'p3dsuite-papy3d';
		private const CAPABILITY  = 'manage_options';
		private const HUB_VERSION = '1.4.0';


		/** @var string[] */
		private static array $page_hooks = array();

		private static bool $hooks_registered = false;

		private static string $resolved_menu_slug = '';

		/** Returns the stable top-level menu slug. */
		public static function menu_slug(): string {
			return '' !== self::$resolved_menu_slug ? self::$resolved_menu_slug : self::MENU_SLUG;
		}

		/** Returns the shared implementation version. */
		public static function version(): string {
			return self::HUB_VERSION;
		}

		/** Registers shared WordPress hooks for the selected implementation. */
		public static function boot(): void {
			self::register_hooks();
		}

		/**
		 * Persists only locale-neutral routing metadata.
		 *
		 * @param array<string, mixed> $plugin Plugin metadata.
		 */
		public static function persist_plugin( array $plugin ): void {

			$plugin = self::normalize_plugin( $plugin, false );
			if ( array() === $plugin ) {
				return;
			}

			$plugins                     = self::stored_plugins();
			$plugins[ $plugin['slug'] ] = self::persistent_plugin( $plugin );
			self::write_plugins( $plugins );
		}

		/** Removes only one plugin from the shared registry. */
		public static function unregister_plugin( string $slug ): void {

			$slug = sanitize_key( $slug );
			if ( '' === $slug ) {
				return;
			}


			$plugins = self::stored_plugins();
			unset( $plugins[ $slug ] );
			self::write_plugins( $plugins );
			self::remove_from_legacy_registries( $slug );
		}

		/** Synchronizes neutral metadata from active compatible plugins. */
		public static function sync_runtime_plugins(): void {
			if ( array() === P3DSUITE_Admin_Hub::runtime_plugins() ) {
				return;
			}

			$plugins = self::stored_plugins();
			foreach ( P3DSUITE_Admin_Hub::runtime_plugins() as $slug => $plugin ) {
				$plugins[ $slug ] = self::persistent_plugin( $plugin );
			}
			self::write_plugins( $plugins );
		}

		/** Registers shared WordPress hooks once. */
		private static function register_hooks(): void {
			if ( self::$hooks_registered ) {
				return;
			}

			self::$hooks_registered = true;
			add_action( 'admin_init', array( __CLASS__, 'sync_runtime_plugins' ), 5 );
			add_action( 'admin_menu', array( __CLASS__, 'admin_menu' ), 20 );
			add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue_assets' ) );
		}

		/** Creates the stable Papy 3D menu and its neutral home page. */
		public static function admin_menu(): void {
			if ( array() === P3DSUITE_Admin_Hub::runtime_plugins() ) {
				return;
			}

			$existing_slug = self::find_existing_menu_slug();
			if ( '' !== $existing_slug ) {
				self::$resolved_menu_slug = $existing_slug;
				return;
			}

			$labels = self::hub_labels();

			$parent_hook = add_menu_page(
				$labels['menu_title'],
				$labels['menu_title'],
				self::CAPABILITY,
				self::MENU_SLUG,
				array( __CLASS__, 'render_home' ),
				'dashicons-admin-plugins',
				58
			);

			$home_hook = add_submenu_page(
				self::MENU_SLUG,
				$labels['home_page_title'],
				$labels['home_menu_title'],
				self::CAPABILITY,
				self::MENU_SLUG,
				array( __CLASS__, 'render_home' )
			);

			self::$page_hooks = array_values(
				array_unique(
					array_filter(
						array( $parent_hook, $home_hook ),
						static function ( mixed $hook ): bool {
							return is_string( $hook ) && '' !== $hook;
						}
					)
				)
			);
		}

		/** Loads the readable inline stylesheet only on the hub home page. */
		public static function enqueue_assets( string $hook_suffix ): void {
			if ( ! in_array( $hook_suffix, self::$page_hooks, true ) ) {
				return;
			}

			wp_register_style( 'p3dsuite-admin-hub', false, array(), self::HUB_VERSION );
			wp_enqueue_style( 'p3dsuite-admin-hub' );
			wp_add_inline_style( 'p3dsuite-admin-hub', self::stylesheet() );
		}

		/** Renders the shared home page. */
		public static function render_home(): void {
			$labels = self::hub_labels();

			if ( ! current_user_can( self::CAPABILITY ) ) {
				wp_die( esc_html( $labels['permission_denied'] ) );
			}

			$plugins = self::registered_plugins();
			uasort(
				$plugins,
				static function ( array $left, array $right ): int {
					return strnatcasecmp(
						(string) ( $left['name'] ?? '' ),
						(string) ( $right['name'] ?? '' )
					);
				}
			);

			$count       = count( $plugins );
			$count_label = self::count_label( $labels, $count );
			?>
			<div class="wrap p3dsuite-hub-wrap">
				<div class="p3dsuite-hub-header">
					<div>
						<h1><?php echo esc_html( $labels['menu_title'] ); ?></h1>
						<p><?php echo esc_html( $labels['home_description'] ); ?></p>
					</div>
					<span class="p3dsuite-hub-count"><?php echo esc_html( $count_label ); ?></span>
				</div>

				<?php if ( array() === $plugins ) : ?>
					<div class="notice notice-info inline"><p><?php echo esc_html( $labels['empty_message'] ); ?></p></div>
				<?php else : ?>
					<div class="p3dsuite-hub-grid">
						<?php foreach ( $plugins as $plugin ) : ?>
							<?php
							$is_active    = self::is_plugin_active( $plugin['plugin_file'] );
							$page_url     = self::plugin_page_url( $plugin );
							$version_text = '' !== $plugin['version']
								? str_replace( '%s', $plugin['version'], $labels['version_format'] )
								: '';
							?>
							<article class="p3dsuite-hub-card">
								<div class="p3dsuite-hub-card-head">
									<?php if ( '' !== $plugin['icon_url'] ) : ?>
										<img src="<?php echo esc_url( $plugin['icon_url'] ); ?>" alt="" width="64" height="64" />
									<?php else : ?>
										<span class="dashicons dashicons-admin-plugins p3dsuite-hub-fallback-icon" aria-hidden="true"></span>
									<?php endif; ?>
									<div>
										<h2><?php echo esc_html( $plugin['name'] ); ?></h2>
										<?php if ( '' !== $version_text ) : ?>
											<p class="p3dsuite-hub-version"><?php echo esc_html( $version_text ); ?></p>
										<?php endif; ?>
									</div>
									<span class="p3dsuite-hub-status <?php echo esc_attr( $is_active ? 'is-active' : 'is-inactive' ); ?>">
										<?php echo esc_html( $is_active ? $labels['active'] : $labels['inactive'] ); ?>
									</span>
								</div>
								<?php if ( '' !== $plugin['description'] ) : ?>
									<p class="p3dsuite-hub-description"><?php echo esc_html( $plugin['description'] ); ?></p>
								<?php endif; ?>
								<div class="p3dsuite-hub-card-actions">
									<?php if ( $is_active && '' !== $page_url ) : ?>
										<a class="button button-primary" href="<?php echo esc_url( $page_url ); ?>"><?php echo esc_html( $labels['open'] ); ?></a>
									<?php else : ?>
										<span class="description"><?php echo esc_html( $labels['inactive_help'] ); ?></span>
									<?php endif; ?>
								</div>
							</article>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			</div>
			<?php
		}

		/**
		 * Reuse a top-level Papy3D menu already registered by another compatible plugin.
		 *
		 * This late lookup avoids duplicate menus even when the historical shared hub
		 * is loaded or registers its menu after Security Guard's bootstrap.
		 *
		 * @return string Existing parent slug, or an empty string.
		 */
		private static function find_existing_menu_slug(): string {
			global $menu;

			$candidates = array( self::MENU_SLUG, 'p3dsg-papy3d', 'p3dwaf-papy3d', 'papy3d' );
			foreach ( is_array( $menu ) ? $menu : array() as $item ) {
				$slug = isset( $item[2] ) && is_scalar( $item[2] ) ? sanitize_key( (string) $item[2] ) : '';
				if ( in_array( $slug, $candidates, true ) ) {
					return $slug;
				}
			}

			return '';
		}

		/** Returns labels supplied at runtime by an active plugin. */
		private static function hub_labels(): array {
			foreach ( P3DSUITE_Admin_Hub::runtime_plugins() as $plugin ) {
				if ( isset( $plugin['labels'] ) && is_array( $plugin['labels'] ) && array() !== $plugin['labels'] ) {
					return $plugin['labels'];
				}
			}

			return self::default_labels();
		}


		/**
		 * Provides locale-neutral emergency labels.
		 *
		 * Compatible plugins supply translated labels at runtime. These English
		 * fallbacks keep the shared component independent from every plugin text domain.
		 */
		private static function default_labels(): array {
			return array(
				'menu_title'        => 'Papy 3D',
				'home_page_title'   => 'Papy 3D Home',
				'home_menu_title'   => 'Home',
				'home_description'  => 'Manage the Papy3D plugins installed on this site.',
				'count_singular'    => '%d plugin',
				'count_plural'      => '%d plugins',
				'empty_message'     => 'No Papy3D plugin is currently registered.',
				'version_format'    => 'Version %s',
				'active'            => 'Active',
				'inactive'          => 'Inactive',
				'open'              => 'Open',
				'inactive_help'     => 'Activate the plugin to access its settings.',
				'permission_denied' => 'Sorry, you are not allowed to access this page.',
			);
		}

		/** Creates a locale-aware count label when a plugin supplied one. */
		private static function count_label( array $labels, int $count ): string {
			if ( isset( $labels['count_nooped'] ) && is_array( $labels['count_nooped'] ) ) {
				$format = translate_nooped_plural( $labels['count_nooped'], $count );
			} else {
				$format = 1 === $count ? $labels['count_singular'] : $labels['count_plural'];
			}

			return sprintf( $format, $count );
		}

		/** Returns persisted entries merged with active runtime metadata. */
		private static function registered_plugins(): array {
			$plugins = self::stored_plugins();
			foreach ( P3DSUITE_Admin_Hub::runtime_plugins() as $slug => $plugin ) {
				$plugins[ $slug ] = $plugin;
			}

			foreach ( $plugins as $slug => $plugin ) {
				$plugins[ $slug ] = self::hydrate_plugin( $plugin );
			}

			return $plugins;
		}

		/** Reads and migrates the persistent registry. */
		private static function stored_plugins(): array {
			self::migrate_legacy_registries();
			$stored  = get_option( self::OPTION_NAME, array() );
			$plugins = array();
			if ( ! is_array( $stored ) ) {
				return $plugins;
			}

			foreach ( $stored as $key => $plugin ) {
				if ( ! is_array( $plugin ) ) {
					continue;
				}
				if ( ! isset( $plugin['slug'] ) && is_scalar( $key ) ) {
					$plugin['slug'] = (string) $key;
				}

				$plugin = self::normalize_plugin( $plugin, false );
				if ( array() !== $plugin ) {
					$plugins[ $plugin['slug'] ] = self::persistent_plugin( $plugin );
				}
			}

			return $plugins;
		}

		/** Migrates every historical Papy3D registry into the neutral suite option. */
		private static function migrate_legacy_registries(): void {
			static $migrated_sites = array();
			$site_id = function_exists( 'get_current_blog_id' ) ? get_current_blog_id() : 0;
			if ( isset( $migrated_sites[ $site_id ] ) ) {
				return;
			}
			$migrated_sites[ $site_id ] = true;

			$current = get_option( self::OPTION_NAME, array() );
			$current = is_array( $current ) ? $current : array();
			$changed = false;

			foreach ( array( 'papy3d_admin_hub_plugins', 'p3dsg_admin_hub_plugins', 'p3dwaf_admin_hub_plugins' ) as $legacy_option ) {
				$legacy = get_option( $legacy_option, false );
				if ( ! is_array( $legacy ) ) {
					continue;
				}

				foreach ( $legacy as $key => $plugin ) {
					if ( ! is_array( $plugin ) ) {
						continue;
					}
					if ( ! isset( $plugin['slug'] ) && is_scalar( $key ) ) {
						$plugin['slug'] = (string) $key;
					}
					$plugin = self::normalize_plugin( $plugin, false );
					if ( array() === $plugin || isset( $current[ $plugin['slug'] ] ) ) {
						continue;
					}
					$current[ $plugin['slug'] ] = self::persistent_plugin( $plugin );
					$changed = true;
				}

				delete_option( $legacy_option );
			}

			if ( $changed ) {
				self::write_plugins( $current );
			}
		}


		/** Removes one plugin card from any historical registry without touching others. */
		private static function remove_from_legacy_registries( string $slug ): void {
			foreach ( array( 'papy3d_admin_hub_plugins', 'p3dsg_admin_hub_plugins', 'p3dwaf_admin_hub_plugins' ) as $legacy_option ) {
				$stored = get_option( $legacy_option, false );
				if ( ! is_array( $stored ) ) {
					continue;
				}
				$changed = false;
				foreach ( $stored as $key => $plugin ) {
					$key_slug   = is_scalar( $key ) ? sanitize_key( (string) $key ) : '';
					$entry_slug = is_array( $plugin ) && isset( $plugin['slug'] ) && is_scalar( $plugin['slug'] )
						? sanitize_key( (string) $plugin['slug'] )
						: '';
					if ( $slug === $key_slug || $slug === $entry_slug ) {
						unset( $stored[ $key ] );
						$changed = true;
					}
				}
				if ( ! $changed ) {
					continue;
				}
				if ( array() === $stored ) {
					delete_option( $legacy_option );
				} else {
					update_option( $legacy_option, $stored, false );
				}
			}
		}
		/** Stores the registry without autoloading it on public requests. */
		private static function write_plugins( array $plugins ): void {
			if ( array() === $plugins ) {
				delete_option( self::OPTION_NAME );
				return;
			}

			ksort( $plugins, SORT_NATURAL | SORT_FLAG_CASE );
			if ( false === get_option( self::OPTION_NAME, false ) ) {
				add_option( self::OPTION_NAME, $plugins, '', false );
				return;
			}

			update_option( self::OPTION_NAME, $plugins, false );
		}

		/** Normalizes runtime or persistent public metadata. */
		private static function normalize_plugin( array $plugin, bool $runtime ): array {
			$slug = isset( $plugin['slug'] ) && is_scalar( $plugin['slug'] ) ? sanitize_key( (string) $plugin['slug'] ) : '';
			if ( '' === $slug ) {
				return array();
			}

			$labels = $runtime && isset( $plugin['labels'] ) && is_array( $plugin['labels'] )
				? self::normalize_labels( $plugin['labels'] )
				: array();

			return array(
				'slug'        => $slug,
				'name'        => $runtime && isset( $plugin['name'] ) && is_scalar( $plugin['name'] ) ? sanitize_text_field( (string) $plugin['name'] ) : '',
				'description' => $runtime && isset( $plugin['description'] ) && is_scalar( $plugin['description'] ) ? sanitize_text_field( (string) $plugin['description'] ) : '',
				'version'     => $runtime && isset( $plugin['version'] ) && is_scalar( $plugin['version'] ) ? sanitize_text_field( (string) $plugin['version'] ) : '',
				'plugin_file' => isset( $plugin['plugin_file'] ) && is_scalar( $plugin['plugin_file'] ) ? sanitize_text_field( (string) $plugin['plugin_file'] ) : '',
				'page_slug'   => isset( $plugin['page_slug'] ) && is_scalar( $plugin['page_slug'] ) ? sanitize_key( (string) $plugin['page_slug'] ) : '',
				'icon_url'    => isset( $plugin['icon_url'] ) && is_scalar( $plugin['icon_url'] ) ? esc_url_raw( (string) $plugin['icon_url'] ) : '',
				'labels'      => $labels,
			);
		}

		/** Removes translated fields before writing the persistent registry. */
		private static function persistent_plugin( array $plugin ): array {
			return array(
				'slug'        => $plugin['slug'],
				'plugin_file' => $plugin['plugin_file'],
				'page_slug'   => $plugin['page_slug'],
				'icon_url'    => $plugin['icon_url'],
			);
		}

		/** Reads inactive plugin headers at render time instead of persisting translations. */
		private static function hydrate_plugin( array $plugin ): array {
			$plugin = array_merge(
				array(
					'slug'        => '',
					'name'        => '',
					'description' => '',
					'version'     => '',
					'plugin_file' => '',
					'page_slug'   => '',
					'icon_url'    => '',
					'labels'      => array(),
				),
				$plugin
			);

			$plugin['slug']        = is_scalar( $plugin['slug'] ) ? sanitize_key( (string) $plugin['slug'] ) : '';
			$plugin['name']        = is_scalar( $plugin['name'] ) ? sanitize_text_field( (string) $plugin['name'] ) : '';
			$plugin['description'] = is_scalar( $plugin['description'] ) ? sanitize_text_field( (string) $plugin['description'] ) : '';
			$plugin['version']     = is_scalar( $plugin['version'] ) ? sanitize_text_field( (string) $plugin['version'] ) : '';
			$plugin['plugin_file'] = is_scalar( $plugin['plugin_file'] ) ? sanitize_text_field( (string) $plugin['plugin_file'] ) : '';
			$plugin['page_slug']   = is_scalar( $plugin['page_slug'] ) ? sanitize_key( (string) $plugin['page_slug'] ) : '';
			$plugin['icon_url']    = is_scalar( $plugin['icon_url'] ) ? esc_url_raw( (string) $plugin['icon_url'] ) : '';
			$plugin['labels']      = is_array( $plugin['labels'] ) ? $plugin['labels'] : array();

			if ( '' !== $plugin['name'] ) {
				return $plugin;
			}

			$plugin['name'] = '' !== $plugin['slug'] ? $plugin['slug'] : $plugin['plugin_file'];
			if ( '' === $plugin['plugin_file'] || 0 !== validate_file( $plugin['plugin_file'] ) ) {
				return $plugin;
			}

			require_once ABSPATH . 'wp-admin/includes/plugin.php';
			$all_plugins = get_plugins();
			$data        = isset( $all_plugins[ $plugin['plugin_file'] ] ) && is_array( $all_plugins[ $plugin['plugin_file'] ] )
				? $all_plugins[ $plugin['plugin_file'] ]
				: array();
			if ( ! is_array( $data ) ) {
				return $plugin;
			}

			$plugin['name']        = ! empty( $data['Name'] ) ? sanitize_text_field( $data['Name'] ) : $plugin['slug'];
			$plugin['description'] = ! empty( $data['Description'] ) ? wp_strip_all_tags( $data['Description'] ) : '';
			$plugin['version']     = ! empty( $data['Version'] ) ? sanitize_text_field( $data['Version'] ) : '';

			return $plugin;
		}

		/** Normalizes labels and preserves a nooped plural translation structure. */
		private static function normalize_labels( array $labels ): array {
			$normalized = self::default_labels();
			$string_keys = array(
				'menu_title',
				'home_page_title',
				'home_menu_title',
				'home_description',
				'empty_message',
				'version_format',
				'active',
				'inactive',
				'open',
				'inactive_help',
				'permission_denied',
			);

			foreach ( $string_keys as $key ) {
				if ( isset( $labels[ $key ] ) && is_scalar( $labels[ $key ] ) ) {
					$value = sanitize_text_field( (string) $labels[ $key ] );
					if ( '' !== $value ) {
						$normalized[ $key ] = $value;
					}
				}
			}

			if ( isset( $labels['count_nooped'] ) && is_array( $labels['count_nooped'] ) ) {
				$candidate = $labels['count_nooped'];
				if (
					isset( $candidate['singular'], $candidate['plural'], $candidate['domain'] )
					&& is_string( $candidate['singular'] )
					&& is_string( $candidate['plural'] )
					&& is_string( $candidate['domain'] )
					&& false !== strpos( $candidate['singular'], '%d' )
					&& false !== strpos( $candidate['plural'], '%d' )
				) {
					$normalized['count_nooped'] = $candidate;
				}
			} elseif ( isset( $labels['count_singular'], $labels['count_plural'] ) ) {
				$singular = is_scalar( $labels['count_singular'] ) ? sanitize_text_field( (string) $labels['count_singular'] ) : '';
				$plural   = is_scalar( $labels['count_plural'] ) ? sanitize_text_field( (string) $labels['count_plural'] ) : '';
				if ( false !== strpos( $singular, '%d' ) && false !== strpos( $plural, '%d' ) ) {
					$normalized['count_singular'] = $singular;
					$normalized['count_plural']   = $plural;
				}
			}

			if ( false === strpos( $normalized['version_format'], '%s' ) ) {
				$normalized['version_format'] = 'Version %s';
			}

			return $normalized;
		}

		/** Determines whether a registered plugin is active. */
		private static function is_plugin_active( string $plugin_file ): bool {
			if ( '' === $plugin_file ) {
				return false;
			}

			if ( ! function_exists( 'is_plugin_active' ) || ! function_exists( 'is_plugin_active_for_network' ) ) {
				require_once ABSPATH . 'wp-admin/includes/plugin.php';
			}

			return is_plugin_active( $plugin_file ) || is_plugin_active_for_network( $plugin_file );
		}

		/** Builds the administration URL declared by one plugin. */
		private static function plugin_page_url( array $plugin ): string {
			if ( '' === $plugin['page_slug'] ) {
				return '';
			}

			return add_query_arg( 'page', $plugin['page_slug'], admin_url( 'admin.php' ) );
		}

		/** Returns readable, WPCS-formatted shared styling. */
		private static function stylesheet(): string {
			return <<<'CSS'
.p3dsuite-hub-wrap {
	max-width: 1180px;
}

.p3dsuite-hub-header {
	align-items: center;
	display: flex;
	gap: 20px;
	justify-content: space-between;
	margin: 24px 0;
}

.p3dsuite-hub-header h1 {
	font-size: 30px;
	margin: 0 0 6px;
}

.p3dsuite-hub-header p {
	color: #50575e;
	font-size: 15px;
	margin: 0;
}

.p3dsuite-hub-count {
	align-items: center;
	background: #fff;
	border: 1px solid #c3c4c7;
	border-radius: 999px;
	display: inline-flex;
	font-weight: 600;
	min-height: 32px;
	padding: 0 14px;
}

.p3dsuite-hub-grid {
	display: grid;
	gap: 18px;
	grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

.p3dsuite-hub-card {
	background: #fff;
	border: 1px solid #c3c4c7;
	border-radius: 10px;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
	display: flex;
	flex-direction: column;
	min-height: 210px;
	padding: 22px;
}

.p3dsuite-hub-card-head {
	align-items: center;
	display: grid;
	gap: 16px;
	grid-template-columns: 64px 1fr auto;
}

.p3dsuite-hub-card-head img {
	border-radius: 12px;
	display: block;
}

.p3dsuite-hub-fallback-icon {
	align-items: center;
	background: #f0f0f1;
	border-radius: 12px;
	display: flex;
	font-size: 34px;
	height: 64px;
	justify-content: center;
	width: 64px;
}

.p3dsuite-hub-card h2 {
	font-size: 18px;
	margin: 0 0 4px;
}

.p3dsuite-hub-version {
	color: #646970;
	margin: 0;
}

.p3dsuite-hub-status {
	border-radius: 999px;
	font-size: 12px;
	font-weight: 700;
	padding: 4px 9px;
}

.p3dsuite-hub-status.is-active {
	background: #edfaef;
	color: #0a6b1f;
}

.p3dsuite-hub-status.is-inactive {
	background: #f0f0f1;
	color: #50575e;
}

.p3dsuite-hub-description {
	color: #3c434a;
	flex: 1;
	line-height: 1.55;
	margin: 18px 0;
}

.p3dsuite-hub-card-actions {
	align-items: center;
	display: flex;
	min-height: 32px;
}

@media (max-width: 600px) {
	.p3dsuite-hub-header {
		align-items: flex-start;
		flex-direction: column;
	}

	.p3dsuite-hub-grid {
		grid-template-columns: 1fr;
	}

	.p3dsuite-hub-card-head {
		grid-template-columns: 56px 1fr;
	}

	.p3dsuite-hub-card-head img,
	.p3dsuite-hub-fallback-icon {
		height: 56px;
		width: 56px;
	}

	.p3dsuite-hub-status {
		grid-column: 2;
		justify-self: start;
	}
}
CSS;
		}
	}
}


P3DSUITE_Admin_Hub::register_implementation( '1.4.0', 'P3DSUITE_Admin_Hub_Implementation_1_4_0' );
