init_hooks(); } /** * Inizializza gli hook di WordPress */ private function init_hooks() { // Hook per l'admin add_action('admin_menu', array($this, 'add_admin_menu')); add_action('admin_init', array($this, 'register_settings')); add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts')); // Hook per il frontend add_action('wp_enqueue_scripts', array($this, 'enqueue_chatbot_script')); // Hook per i link del plugin add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'add_plugin_action_links')); } /** * Aggiunge il menu di amministrazione */ public function add_admin_menu() { add_options_page( __('AgentoWine Chatbot', 'agentowine-chatbot'), __('AgentoWine Chatbot', 'agentowine-chatbot'), 'manage_options', 'agentowine-chatbot', array($this, 'render_settings_page') ); } /** * Registra le impostazioni */ public function register_settings() { // Registra Client ID register_setting( 'agentowine_chatbot_settings', 'agentowine_chatbot_client_id', array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => '' ) ); // Registra Posizione register_setting( 'agentowine_chatbot_settings', 'agentowine_chatbot_position', array( 'type' => 'string', 'sanitize_callback' => array($this, 'sanitize_position'), 'default' => 'bottom-right' ) ); // Registra Posizione Personalizzata (abilitata/disabilitata) register_setting( 'agentowine_chatbot_settings', 'agentowine_chatbot_custom_position', array( 'type' => 'boolean', 'sanitize_callback' => array($this, 'sanitize_custom_position_enabled'), 'default' => false ) ); // Registra coordinate Desktop register_setting( 'agentowine_chatbot_settings', 'agentowine_chatbot_custom_position_desktop', array( 'type' => 'array', 'sanitize_callback' => array($this, 'sanitize_position_coords'), 'default' => array('top' => '', 'bottom' => '24', 'left' => '', 'right' => '24') ) ); // Registra coordinate Mobile register_setting( 'agentowine_chatbot_settings', 'agentowine_chatbot_custom_position_mobile', array( 'type' => 'array', 'sanitize_callback' => array($this, 'sanitize_position_coords'), 'default' => array('top' => '', 'bottom' => '16', 'left' => '', 'right' => '16') ) ); add_settings_section( 'agentowine_chatbot_main_section', __('Configurazione Chatbot', 'agentowine-chatbot'), array($this, 'render_section_description'), 'agentowine-chatbot' ); add_settings_field( 'agentowine_chatbot_client_id', __('Client ID', 'agentowine-chatbot'), array($this, 'render_client_id_field'), 'agentowine-chatbot', 'agentowine_chatbot_main_section' ); add_settings_field( 'agentowine_chatbot_position', __('Posizione', 'agentowine-chatbot'), array($this, 'render_position_field'), 'agentowine-chatbot', 'agentowine_chatbot_main_section' ); } /** * Sanitizza il valore della posizione */ public function sanitize_position($value) { $valid_positions = array('bottom-right', 'bottom-left', 'top-right', 'top-left', 'custom'); return in_array($value, $valid_positions) ? $value : 'bottom-right'; } /** * Sanitizza il flag posizione personalizzata */ public function sanitize_custom_position_enabled($value) { return (bool) $value; } /** * Sanitizza le coordinate di posizione (desktop o mobile) */ public function sanitize_position_coords($value) { if (!is_array($value)) { return array('top' => '', 'bottom' => '24', 'left' => '', 'right' => '24'); } $sanitized = array(); foreach (array('top', 'bottom', 'left', 'right') as $key) { $val = isset($value[$key]) ? trim($value[$key]) : ''; if ($val === '') { $sanitized[$key] = ''; } else { $num = intval($val); $sanitized[$key] = $num >= 0 ? (string) $num : ''; } } return $sanitized; } /** * Descrizione della sezione */ public function render_section_description() { echo '

' . esc_html__('Inserisci il tuo Client ID per attivare il chatbot AgentoWine sul tuo sito.', 'agentowine-chatbot') . '

'; } /** * Campo Client ID */ public function render_client_id_field() { $client_id = get_option('agentowine_chatbot_client_id', ''); ?>

'', 'bottom' => '24', 'left' => '', 'right' => '24')); $mobile = get_option('agentowine_chatbot_custom_position_mobile', array('top' => '', 'bottom' => '16', 'left' => '', 'right' => '16')); $positions = array( 'bottom-right' => __('In basso a destra', 'agentowine-chatbot'), 'bottom-left' => __('In basso a sinistra', 'agentowine-chatbot'), 'top-right' => __('In alto a destra', 'agentowine-chatbot'), 'top-left' => __('In alto a sinistra', 'agentowine-chatbot'), ); ?>

/>

<?php esc_attr_e('AgentoWine', 'agentowine-chatbot'); ?>

render_status_box(); ?>

agentowine.com info@agentowine.com

__('In basso a destra', 'agentowine-chatbot'), 'bottom-left' => __('In basso a sinistra', 'agentowine-chatbot'), 'top-right' => __('In alto a destra', 'agentowine-chatbot'), 'top-left' => __('In alto a sinistra', 'agentowine-chatbot'), 'custom' => __('Personalizzata', 'agentowine-chatbot'), ); ?>


Desktop: $v) { if ($v !== '') $parts[] = "$k: {$v}px"; } echo esc_html(implode(', ', $parts) ?: '—'); ?>
Mobile: $v) { if ($v !== '') $parts[] = "$k: {$v}px"; } echo esc_html(implode(', ', $parts) ?: '—'); ?>

get_admin_styles()); wp_enqueue_script( 'agentowine-chatbot-admin', AGENTOWINE_CHATBOT_PLUGIN_URL . 'assets/admin.js', array(), AGENTOWINE_CHATBOT_VERSION, true ); } /** * Restituisce gli stili CSS per l'admin */ private function get_admin_styles() { return ' .agentowine-chatbot-settings { max-width: 800px; } .agentowine-chatbot-header { margin-bottom: 12px; } .agentowine-banner { display: block; width: 100%; max-width: 500px; height: auto; border-radius: 8px; } .agentowine-subtitle { margin: 0 0 20px; color: #666; } .agentowine-status-box { background: #f8f9fa; border-left: 4px solid #ccc; padding: 15px 20px; margin: 20px 0; border-radius: 0 8px 8px 0; } .agentowine-status-box.active { background: #ecf7ed; border-left-color: #46b450; } .agentowine-status-box.inactive { background: #fff8e5; border-left-color: #ffb900; } .agentowine-status-box h3 { margin-top: 0; } .status-indicator { display: flex; align-items: center; gap: 8px; font-weight: 500; } .status-indicator.active { color: #46b450; } .status-indicator.inactive { color: #826200; } .client-id-info, .position-info { margin-top: 10px; } .client-id-info code { background: #fff; padding: 3px 8px; border-radius: 4px; } .agentowine-chatbot-help { background: #f0f0f1; padding: 15px 20px; border-radius: 8px; margin-top: 20px; } .agentowine-chatbot-help h3 { margin-top: 0; } .agentowine-chatbot-help a { color: #722F37; } .agentowine-coords-table td { padding: 4px 8px 4px 0; } .agentowine-coords-table label { display: block; font-size: 12px; color: #666; margin-bottom: 2px; } '; } /** * Accoda lo script del chatbot nel frontend */ public function enqueue_chatbot_script() { $client_id = get_option('agentowine_chatbot_client_id', ''); // Non caricare nulla se non c'è il Client ID if (empty($client_id)) { return; } wp_enqueue_script( 'agentowine-chatbot', AGENTOWINE_CHATBOT_SCRIPT_URL, array(), AGENTOWINE_CHATBOT_VERSION, true ); // Ottieni la posizione configurata $position = get_option('agentowine_chatbot_position', 'bottom-right'); // Costruisci oggetto configurazione $config = array( 'position' => $position, 'theme' => 'light', 'clientId' => $client_id, ); // Aggiungi dati posizione personalizzata se abilitata if ($position === 'custom') { $desktop = get_option('agentowine_chatbot_custom_position_desktop', array('top' => '', 'bottom' => '24', 'left' => '', 'right' => '24')); $mobile = get_option('agentowine_chatbot_custom_position_mobile', array('top' => '', 'bottom' => '16', 'left' => '', 'right' => '16')); $config['customPosition'] = array( 'desktop' => $desktop, 'mobile' => $mobile, ); } $inline_script = sprintf( 'document.addEventListener("DOMContentLoaded", function() { if (window.Chatbot) { window.Chatbot.init(%s); } });', wp_json_encode($config) ); wp_add_inline_script('agentowine-chatbot', $inline_script, 'after'); } /** * Aggiunge link alle azioni del plugin */ public function add_plugin_action_links($links) { $settings_link = '' . esc_html__('Impostazioni', 'agentowine-chatbot') . ''; array_unshift($links, $settings_link); return $links; } } // Inizializza il plugin function agentowine_chatbot_init() { return AgentoWine_Chatbot::get_instance(); } add_action('plugins_loaded', 'agentowine_chatbot_init'); /** * Attivazione del plugin */ function agentowine_chatbot_activate() { // Nessuna azione speciale richiesta all'attivazione } register_activation_hook(__FILE__, 'agentowine_chatbot_activate'); /** * Disattivazione del plugin */ function agentowine_chatbot_deactivate() { // Nessuna azione speciale richiesta alla disattivazione } register_deactivation_hook(__FILE__, 'agentowine_chatbot_deactivate'); /** * Disinstallazione del plugin */ function agentowine_chatbot_uninstall() { // Rimuove le opzioni salvate delete_option('agentowine_chatbot_client_id'); delete_option('agentowine_chatbot_position'); delete_option('agentowine_chatbot_custom_position'); delete_option('agentowine_chatbot_custom_position_desktop'); delete_option('agentowine_chatbot_custom_position_mobile'); } register_uninstall_hook(__FILE__, 'agentowine_chatbot_uninstall');