57 lines
2.2 KiB
PHP
57 lines
2.2 KiB
PHP
<?php
|
|
namespace EveAI\Chat;
|
|
|
|
class Shortcode implements Loadable {
|
|
public function init() {
|
|
add_shortcode('eveai_chat', [$this, 'render_chat']);
|
|
}
|
|
|
|
public function render_chat($atts) {
|
|
$settings = get_option('eveai_chat_settings');
|
|
error_log('Rendering chat with settings: ' . print_r($settings, true));
|
|
|
|
if (empty($settings['tenant_id']) || empty($settings['api_key'])) {
|
|
return '<div class="eveai-error">' .
|
|
esc_html__('EveAI Chat is not properly configured. Please check the admin settings.', 'eveai-chat') .
|
|
'</div>';
|
|
}
|
|
|
|
// Parse shortcode attributes
|
|
$atts = shortcode_atts([
|
|
'language' => 'en',
|
|
'languages' => 'en',
|
|
'specialist_id' => '1'
|
|
], $atts, 'eveai_chat');
|
|
|
|
// Generate unique container ID
|
|
$container_id = 'eveai-chat-' . uniqid();
|
|
|
|
ob_start();
|
|
?>
|
|
<div id="<?php echo esc_attr($container_id); ?>" class="eveai-chat-container"></div>
|
|
<script>
|
|
document.addEventListener('DOMContentLoaded', async () => {
|
|
console.log('Initializing EveAI Chat');
|
|
const eveAI = new EveAI({
|
|
tenantId: <?php echo esc_js($settings['tenant_id']); ?>,
|
|
language: '<?php echo esc_js($atts['language']); ?>',
|
|
languages: '<?php echo esc_js($atts['languages']); ?>',
|
|
specialistId: '<?php echo esc_js($atts['specialist_id']); ?>',
|
|
socketUrl: '<?php echo esc_js($settings['socket_url']); ?>',
|
|
authUrl: '<?php echo esc_js($settings['auth_url']); ?>'
|
|
});
|
|
|
|
try {
|
|
await eveAI.initialize('<?php echo esc_js($container_id); ?>');
|
|
console.log('Chat initialized successfully');
|
|
} catch (error) {
|
|
console.error('Failed to initialize chat:', error);
|
|
document.getElementById('<?php echo esc_js($container_id); ?>').innerHTML =
|
|
'<div class="eveai-error">Failed to initialize chat. Please check console for details.</div>';
|
|
}
|
|
});
|
|
</script>
|
|
<?php
|
|
return ob_get_clean();
|
|
}
|
|
}
|