/home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Integrations/AI/Admin/Chat
Edit: /home/techb158/public_html/wp-content/plugins/wpforms-lite/src/Integrations/AI/Admin/Chat/Chat.php (14874B)
register_bulk()`,
* then captures the active surface (if any) and attaches page-render hooks.
*
* @since 2.0.0
*/
public function init(): void {
$this->loader();
$surface_registry = wpforms()->obj( 'ai_chat_surface_registry' );
$this->surface = $surface_registry instanceof SurfaceRegistry
? $surface_registry->get_active_surface()
: null;
if ( $this->surface !== null ) {
$this->hooks();
}
}
/**
* Register the AI Chat registries, scopes, and surfaces through `wpforms()->register_bulk()`.
*
* The Loader's Pro/Lite namespace switch resolves each class to its Pro variant when present,
* falling back to the Lite class. Pure-Pro classes (Analytics, AnalyticsPage) silently no-op
* on Lite because no class is found at either resolved path.
*
* @since 2.0.0
*/
private function loader(): void {
$classes = [
// Registries first — `hook => false` runs the callback immediately
// so the registries are available before scopes/surfaces self-register.
[
'name' => 'Integrations\AI\Admin\Chat\ScopeRegistry',
'id' => 'ai_chat_scope_registry',
'hook' => false,
],
[
'name' => 'Integrations\AI\Admin\Chat\SurfaceRegistry',
'id' => 'ai_chat_surface_registry',
'hook' => false,
],
// Scopes — each init() self-registers into the scope registry.
[
'name' => 'Integrations\AI\Admin\Chat\Scope\WPFormsGeneral',
'hook' => false,
],
[
'name' => 'Integrations\AI\Admin\Chat\Scope\FormsInventory\FormsInventory',
'hook' => false,
],
[
'name' => 'Integrations\AI\Admin\Chat\Scope\Analytics\Analytics',
'hook' => false,
],
// Surfaces.
[
'name' => 'Integrations\AI\Admin\Chat\Surface\AnalyticsPage',
'hook' => false,
],
];
wpforms()->register_bulk( $classes );
}
/**
* Register page-render hooks for the active surface.
*
* @since 2.0.0
*/
private function hooks(): void {
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ] );
add_action( 'admin_footer', [ $this, 'render_templates' ] );
add_filter( 'admin_body_class', [ $this, 'filter_admin_body_class' ] );
add_filter( 'wpforms_integrations_ai_admin_chat_chat_get_localize_chat_data', [ $this, 'get_chat_mode_strings' ] );
}
/**
* Append the surface's body class.
*
* @since 2.0.0
*
* @param string|mixed $classes Existing body classes.
*
* @return string
*/
public function filter_admin_body_class( $classes ): string {
$classes = (string) $classes;
$body_class = (string) ( $this->surface['body_class'] ?? '' );
if ( $body_class === '' ) {
return $classes;
}
if ( ! $this->user_can_view() ) {
return $classes;
}
return trim( $classes . ' ' . $body_class );
}
/**
* Enqueue the chat element + chat.js + per-surface CSS, and emit both localized objects.
*
* - `wpforms_ai_chat_element` — chat-element strings + modules + nonce + actions.
* - `wpforms_ai_chat` — admin chat surface config consumed by chat.js.
*
* `chat-helpers-admin.js` is loaded as a dynamic ES module via the chat
* element's `modules` array, not as a classic script.
*
* @since 2.0.0
*/
public function enqueue_assets(): void {
if ( ! $this->user_can_view() ) {
return;
}
$min = wpforms_get_min_suffix();
// DOMPurify — required by chat-helpers-admin's sanitizeHtmlAnswer().
wp_enqueue_script(
'dom-purify',
WPFORMS_PLUGIN_URL . 'assets/lib/purify.min.js',
[],
'3.4.13',
false
);
// Chat element (CSS + JS). `wp-i18n` is a dep so dynamically-imported modules can call wp.i18n.__().
wp_enqueue_style(
self::CHAT_ELEMENT_HANDLE,
WPFORMS_PLUGIN_URL . "assets/css/integrations/ai/chat-element$min.css",
[],
WPFORMS_VERSION
);
wp_enqueue_script(
self::CHAT_ELEMENT_HANDLE,
WPFORMS_PLUGIN_URL . "assets/js/integrations/ai/chat-element/wpforms-ai-chat-element$min.js",
[ 'dom-purify', 'wp-i18n' ],
WPFORMS_VERSION,
false
);
// Admin chat CSS.
wp_enqueue_style(
self::CHAT_HANDLE,
WPFORMS_PLUGIN_URL . "assets/css/integrations/ai/wpforms-ai-chat$min.css",
[],
WPFORMS_VERSION
);
// Shared modal drag/resize utility (WPForms.Admin.AIChatModal) — reused by chat.js and the form editor.
wp_enqueue_script(
'wpforms-admin-chat-modal',
WPFORMS_PLUGIN_URL . "assets/js/admin/share/chat-modal$min.js",
[],
WPFORMS_VERSION,
true
);
// Admin chat JS — chat.js IIFE that wires FAB, modal, drag, resize, chat-element.
// Note: chat-helpers-admin is not listed as a dep — it is dynamically imported by the
// chat element via the wpforms_ai_chat_element.modules array.
wp_enqueue_script(
self::CHAT_HANDLE,
WPFORMS_PLUGIN_URL . "assets/js/integrations/ai/chat/chat$min.js",
[
'jquery',
'jquery-ui-draggable',
'jquery-ui-resizable',
'wp-util',
'wp-hooks',
'wpforms-admin-chat-modal',
self::CHAT_ELEMENT_HANDLE,
],
WPFORMS_VERSION,
true
);
wp_localize_script(
self::CHAT_ELEMENT_HANDLE,
'wpforms_ai_chat_element',
$this->get_localize_chat_data()
);
// Localize the admin chat surface config — chat.js and chat-helpers-admin read this.
wp_localize_script(
self::CHAT_HANDLE,
self::LOCALIZE_OBJECT,
$this->get_localize_data()
);
}
/**
* Render the FAB + modal templates.
*
* @since 2.0.0
*/
public function render_templates(): void {
if ( ! $this->user_can_view() ) {
return;
}
// Templates are passive `