$this->api_handler = new WA_Attr_Api_Handler(); // Initialize inbox REST API new WA_Attr_Inbox_Api(); // Initialize upload handler new WA_Attr_Inbox_Upload(); } /** * Initialize attribution module. */ private function init_attribution() { $this->attribution_engine = new WA_Attr_Attribution_Engine(); // Register welcome message handler add_action('wa_attribution_conversation_started', [$this, 'maybe_send_welcome_message'], 10, 3); } /** * Send welcome message or start chatbot flow if enabled. * * @param int $conversation_id Conversation ID. * @param string $phone Phone number. * @param array $attribution Attribution data. */ public function maybe_send_welcome_message($conversation_id, $phone, $attribution) { // Check if chatbot flow is enabled (takes priority over welcome message) $flow_engine = new WA_Attr_Core_Flow_Engine(); if ($flow_engine->is_enabled()) { $this->logger->info('Starting chatbot flow instead of welcome message', [ 'conversation_id' => $conversation_id, 'phone' => $phone, ]); $delay = (int) $this->settings->get('welcome_message_delay', 3); if ($delay > 0) { wp_schedule_single_event( time() + $delay, 'wa_attribution_send_welcome_message', [$conversation_id, $phone, '__chatbot_flow__'] ); } else { $flow_engine->start_flow($conversation_id, $phone); } return; } // Check if welcome message is enabled if (!$this->settings->get('welcome_message_enabled', false)) { return; } $message_text = $this->settings->get('welcome_message_text', ''); if (empty($message_text)) { return; } $delay = (int) $this->settings->get('welcome_message_delay', 3); $this->logger->info('Scheduling welcome message', [ 'conversation_id' => $conversation_id, 'phone' => $phone, 'delay' => $delay, ]); // Schedule the message sending with delay if ($delay > 0) { wp_schedule_single_event( time() + $delay, 'wa_attribution_send_welcome_message', [$conversation_id, $phone, $message_text] ); } else { // Send immediately $this->send_welcome_message($conversation_id, $phone, $message_text); } } /** * Actually send the welcome message (or start chatbot flow). * * @param int $conversation_id Conversation ID. * @param string $phone Phone number. * @param string $message_text Message text or '__chatbot_flow__' to start flow. */ public function send_welcome_message($conversation_id, $phone, $message_text) { // Handle chatbot flow trigger if ($message_text === '__chatbot_flow__') { $this->logger->info('Starting chatbot flow (delayed)', [ 'conversation_id' => $conversation_id, 'phone' => $phone, ]); $flow_engine = new WA_Attr_Core_Flow_Engine(); $flow_engine->start_flow($conversation_id, $phone); return; } $this->logger->info('Sending welcome message', [ 'conversation_id' => $conversation_id, 'phone' => $phone, ]); // Send the message $result = WA_Attr_Inbox_Sender::send_text($phone, $message_text); if ($result['success']) { // Store the message in database $message_id = $result['message_id'] ?? null; $this->database->insert_message([ 'conversation_id' => $conversation_id, 'wa_message_id' => $message_id, 'direction' => 'outbound', 'message_type' => 'text', 'content' => $message_text, 'status' => 'sent', 'sent_by' => 0, // System/automated 'created_at' => current_time('mysql'), ]); $this->logger->info('Welcome message sent successfully', [ 'conversation_id' => $conversation_id, 'message_id' => $message_id, ]); } else { $this->logger->error('Failed to send welcome message', [ 'conversation_id' => $conversation_id, 'error' => $result['error'] ?? 'Unknown error', ]); } } /** * Initialize integrations (WooCommerce, Sage, Pipedrive, etc). */ private function init_integrations() { // WooCommerce integration new WA_Attr_Integrations_Woocommerce(); // Sage theme integration new WA_Attr_Integrations_Sage(); // Pipedrive CRM integration if (!class_exists('WA_Attr_Integrations_Pipedrive')) { require_once WA_ATTR_PLUGIN_DIR . 'includes/integrations/class-pipedrive.php'; } if (class_exists('WA_Attr_Integrations_Pipedrive')) { new WA_Attr_Integrations_Pipedrive(); } elseif ($this->logger) { $this->logger->error('Pipedrive integration class not found'); } // Access Database Export API new WA_Attr_Integrations_Access_Export(); } /** * Handler for init action. */ public function on_init() { // Run database migrations if needed $this->run_migrations(); // Additional initialization if needed do_action('wa_attr_init'); } /** * Run database migrations for plugin updates. */ private function run_migrations() { global $wpdb; $table_conversations = $wpdb->prefix . 'wa_attribution_conversations'; // Migration: Add contact_name column for WhatsApp profile name $column_exists = $wpdb->get_results("SHOW COLUMNS FROM {$table_conversations} LIKE 'contact_name'"); if (empty($column_exists)) { $wpdb->query("ALTER TABLE {$table_conversations} ADD COLUMN contact_name VARCHAR(255) DEFAULT NULL AFTER phone_hash"); $this->logger->info('Migration: Added contact_name column to conversations table'); } } /** * Handler for rest_api_init action. */ public function on_rest_api_init() { // REST API initialization if needed do_action('wa_attr_rest_api_init'); } /** * Prevent cloning of the instance. */ private function __clone() {} /** * Prevent unserializing of the instance. */ public function __wakeup() { throw new Exception('Cannot unserialize singleton'); } }