is_settings_screen( $hook ) ) { $this->enqueue_settings_page_assets(); return; } if ( Menu::main_screen_id() !== $hook ) { return; } if ( ! $this->script_tag_filter_hooked ) { add_filter( 'script_loader_tag', array( $this, 'force_module_script_tag' ), 10, 3 ); $this->script_tag_filter_hooked = true; } if ( wp_script_is( 'heartbeat', 'enqueued' ) ) { wp_dequeue_script( 'heartbeat' ); } if ( wp_script_is( 'svg-painter', 'enqueued' ) ) { wp_dequeue_script( 'svg-painter' ); } wp_enqueue_script( 'common' ); wp_enqueue_script( 'wp-hooks' ); wp_enqueue_script( 'wp-util' ); wp_enqueue_style( 'actionpanel-ai-main-page', trailingslashit( ACTIONPANEL_AI_PLUGIN_URL ) . 'assets/admin/main-page.css', array(), ACTIONPANEL_AI_PLUGIN_VERSION ); $asset_base = trailingslashit( ACTIONPANEL_AI_PLUGIN_URL . 'build' ); wp_register_script( 'actionpanel-ai-app', $asset_base . 'index.js', array( 'wp-hooks', 'wp-util' ), ACTIONPANEL_AI_PLUGIN_VERSION, true ); // Hint module intent for modern WordPress script loading. wp_script_add_data( 'actionpanel-ai-app', 'type', 'module' ); wp_localize_script( 'actionpanel-ai-app', 'actionPanelAiConfig', array( 'restNonce' => wp_create_nonce( 'wp_rest' ), 'nonceRefreshUrl' => admin_url( 'admin-ajax.php?action=actionpanel_ai_refresh_rest_nonce' ), 'nonceRefreshNonce' => wp_create_nonce( 'actionpanel_ai_refresh_rest_nonce' ), 'chatEndpoint' => $this->api_endpoint_resolver->get_base_url(), 'cssUrl' => $asset_base . 'index.css', 'restEndpoints' => array( 'settings' => rest_url( 'actionpanel-ai/v1/settings' ), 'context' => rest_url( 'actionpanel-ai/v1/context' ), 'readSite' => rest_url( 'actionpanel-ai/v1/read-site' ), 'executeChanges' => rest_url( 'actionpanel-ai/v1/execute-changes' ), ), 'settings' => array( 'assistantEnabled' => (bool) $this->settings->get_enable_assistant(), 'allowLogReads' => (bool) $this->settings->get_allow_log_reads(), 'allowWriteOperations' => (bool) $this->settings->get_allow_write_operations(), 'requireReadApproval' => (bool) $this->settings->get_require_read_approval(), 'setupCompleted' => (bool) $this->settings->is_setup_completed(), ), ) ); wp_enqueue_script( 'actionpanel-ai-app' ); } /** * Enqueue the classic settings page assets. */ private function enqueue_settings_page_assets(): void { wp_enqueue_style( 'actionpanel-ai-settings-page', trailingslashit( ACTIONPANEL_AI_PLUGIN_URL ) . 'assets/admin/settings-page.css', array(), ACTIONPANEL_AI_PLUGIN_VERSION ); wp_enqueue_script( 'actionpanel-ai-settings-page', trailingslashit( ACTIONPANEL_AI_PLUGIN_URL ) . 'assets/admin/settings-page.js', array(), ACTIONPANEL_AI_PLUGIN_VERSION, true ); } /** * Determine whether the current admin screen is the settings page. * * @param string $hook Current admin page hook suffix. */ private function is_settings_screen( string $hook ): bool { if ( Menu::settings_screen_id() === $hook ) { return true; } if ( function_exists( 'get_current_screen' ) ) { $screen = get_current_screen(); if ( $screen && isset( $screen->id ) && is_string( $screen->id ) ) { return Menu::settings_screen_id() === $screen->id; } } return false; } /** * Force the frontend bundle to load as an ES module. * * @param string $tag Generated script tag. * @param string $handle Script handle. * @param string $src Script source URL. */ public function force_module_script_tag( string $tag, string $handle, string $src ): string { unset( $src ); if ( 'actionpanel-ai-app' !== $handle ) { return $tag; } if ( false !== strpos( $tag, ' type=' ) ) { $updated = preg_replace( '/\stype=(["\']).*?\1/i', ' type="module"', $tag, 1 ); if ( is_string( $updated ) && '' !== $updated ) { return $updated; } } return str_replace( '