'missing_portal_url']); return false; } return true; } /** * Register and enqueue the widget for footer output. * * This runs on wp_enqueue_scripts after the frontend request has been * resolved. Enqueuing from wp_footer would be too late because WordPress * prints the footer script queue at wp_footer priority 20. * * @since 1.0.5 * @return void */ function vasp_render_widget() { // Check if widget should be displayed if (!vasp_is_widget_enabled()) { return; } // Get configuration from config file $config = vasp_get_config(); $portal_url = $config['widget_portal_url'] ?? ''; $tenant_id = vasp_get_widget_tenant_id(); // Tenant ID synchronized from the OAuth token binding // A never-connected installation has no safe public routing target yet. // Omit the loader quietly; this is expected state, not a page-level warning. if (empty($tenant_id)) { return; } // The portal URL is checked by vasp_is_widget_enabled(), but retain this // guard so this function remains safe if that check changes later. if (empty($portal_url)) { vasp_log('Widget not displayed', 'warning', [ 'reason' => 'missing_portal_url' ]); return; } // Build the widget script URL. The script reads the tenant ID from its data attribute. $widget_script_url = untrailingslashit($portal_url) . '/valuespace-chat-widget.js'; // Get WordPress user ID if logged in (this covers both WordPress and WooCommerce users) $user_id = is_user_logged_in() ? get_current_user_id() : ''; $widget_config = [ 'tenantId' => (string) $tenant_id, 'webhookUrl' => (string) $portal_url, 'origin' => 'wordpress', 'identityEndpointUrl' => esc_url_raw(rest_url('valuespace/v1/event-session')), ]; if ($user_id) { $widget_config['userId'] = (string) $user_id; } $GLOBALS['vasp_widget_script_attributes'] = [ 'tenant_id' => (string) $tenant_id, 'user_id' => (string) $user_id, ]; wp_register_script('valuespace-widget', esc_url_raw($widget_script_url), [], VASP_VERSION, true); wp_add_inline_script( 'valuespace-widget', 'window.ValueSpaceConfig = Object.assign({}, window.ValueSpaceConfig || {}, ' . wp_json_encode($widget_config) . ');', 'before' ); wp_enqueue_script('valuespace-widget'); } /** * Add the widget's documented data attributes without emitting raw script tags. * * @param string $tag Generated script tag. * @param string $handle Registered script handle. * @param string $src Script URL. * @return string */ function vasp_widget_script_tag($tag, $handle, $src) { unset($src); if ($handle !== 'valuespace-widget') { return $tag; } $attributes = isset($GLOBALS['vasp_widget_script_attributes']) && is_array($GLOBALS['vasp_widget_script_attributes']) ? $GLOBALS['vasp_widget_script_attributes'] : []; $tenant_id = isset($attributes['tenant_id']) ? (string) $attributes['tenant_id'] : ''; $user_id = isset($attributes['user_id']) ? (string) $attributes['user_id'] : ''; if ($tenant_id === '') { return $tag; } $injected = ' async data-tenant-id="' . esc_attr($tenant_id) . '"'; if ($user_id !== '') { $injected .= ' data-user-id="' . esc_attr($user_id) . '"'; } // WordPress may pass the inline "before" block and the external script in // one tag string. Add embed attributes to the script that owns src, not to // the preceding inline configuration block. return preg_replace('/]*\bsrc=)/', '