diff --git a/inc/class-scripts.php b/inc/class-scripts.php index 4936a6a77..8f3943bc9 100644 --- a/inc/class-scripts.php +++ b/inc/class-scripts.php @@ -33,9 +33,9 @@ public function init(): void { add_action('init', [$this, 'register_default_styles']); - add_action('admin_init', [$this, 'enqueue_default_admin_styles']); + add_action('admin_enqueue_scripts', [$this, 'enqueue_default_admin_styles']); - add_action('admin_init', [$this, 'enqueue_default_admin_scripts']); + add_action('admin_enqueue_scripts', [$this, 'enqueue_default_admin_scripts']); add_action('wp_ajax_wu_toggle_container', [$this, 'update_use_container']); @@ -427,10 +427,19 @@ public function register_default_styles(): void { /** * Loads the default admin styles. * + * Only enqueued on WP Ultimo admin pages to avoid loading 150KB+ of CSS + * on unrelated admin pages (e.g., Posts, Plugins, Settings). + * * @since 2.0.0 + * + * @param string $hook_suffix The current admin page hook suffix. * @return void */ - public function enqueue_default_admin_styles(): void { + public function enqueue_default_admin_styles(string $hook_suffix): void { + + if ( ! wu_is_wu_page($hook_suffix)) { + return; + } wp_enqueue_style('wu-admin'); @@ -441,10 +450,19 @@ public function enqueue_default_admin_styles(): void { /** * Loads the default admin scripts. * + * Only enqueued on WP Ultimo admin pages to avoid loading scripts + * on unrelated admin pages (e.g., Posts, Plugins, Settings). + * * @since 2.0.0 + * + * @param string $hook_suffix The current admin page hook suffix. * @return void */ - public function enqueue_default_admin_scripts(): void { + public function enqueue_default_admin_scripts(string $hook_suffix): void { + + if ( ! wu_is_wu_page($hook_suffix)) { + return; + } wp_enqueue_script('wu-admin'); diff --git a/inc/functions/assets.php b/inc/functions/assets.php index d2c115194..4723302d2 100644 --- a/inc/functions/assets.php +++ b/inc/functions/assets.php @@ -27,3 +27,30 @@ function wu_get_asset($asset, $assets_dir = 'img', $base_dir = 'assets') { return wu_url("$base_dir/$assets_dir/$asset"); } + +/** + * Checks if the current admin page belongs to WP Ultimo. + * + * Used to guard asset enqueues so that WP Ultimo scripts and styles are only + * loaded on WP Ultimo admin pages, not on every page in the network admin. + * + * Detection relies on the hook suffix passed to `admin_enqueue_scripts`. All + * WP Ultimo admin pages register with an ID prefixed by `wp-ultimo`, which + * WordPress uses when generating the page hook (e.g., `toplevel_page_wp-ultimo`, + * `wp-ultimo_page_wp-ultimo-settings`). The hook suffix always contains the + * page slug, so checking for `wp-ultimo` is reliable. + * + * @since 2.4.2 + * + * @param string $hook_suffix The hook suffix passed to `admin_enqueue_scripts`. + * @return bool True if the current page is a WP Ultimo admin page. + */ +function wu_is_wu_page(string $hook_suffix = ''): bool { + + if ('' === $hook_suffix) { + $screen = get_current_screen(); + $hook_suffix = $screen ? (string) $screen->id : ''; + } + + return str_contains($hook_suffix, 'wp-ultimo'); +}