EMAIL_TABLE = $dbManager->GetPrefix() . $prefix . '_emails'; $this->EMAIL_TRIGGER_TABLE = $dbManager->GetPrefix() . $prefix . '_emails_trigger'; $this->SCHEDULE_TABLE = $dbManager->GetPrefix() . $prefix . '_schedule_email'; $this->CART_ABANDONMENT_TABLE = $dbManager->GetPrefix() . $prefix . '_cart_abandonment'; add_action('init', array($this, 'LoadTextDomain')); parent::__construct($filePath, $prefix, $dbVersion, $fileVersion,$config); new EmailList($this); new EmailBuilderAjax($this); LogManager::Initialize($this); $this->WCEmailTemplateIds=[ 'new_order', 'cancelled_order', 'failed_order', 'customer_on_hold_order', 'customer_processing_order', 'customer_completed_order', 'customer_refunded_order', 'customer_invoice', 'customer_note', 'reset_password', 'new_account' ]; TriggerManager::Instance()->InitializeHooks(); $me = $this; if ($this->IsPR()) new PRLoader($this); add_filter('woocommerce_order_actions', array($this, 'GetOrderActions'), 20, 2); $matches = []; /** To not query the database to get a list of email ids, the plugin parse the wc_order_action and only add the needed hook */ if (isset($_POST['wc_order_action']) && preg_match('/^aefwc_send_email_(\d+)$/i', $_POST['wc_order_action'], $matches)) { if (count($matches) != 2) return; $emailId = intval($matches[1]); add_filter('woocommerce_order_action_aefwc_send_email_' . $emailId, function ($order) use ($emailId) { RNADEM()->SendEmail($emailId, $order, __('Sending "[email_name]" email manually', 'advanced-emailing-for-woocommerce'), true); }, 10, 2); } add_filter("bulk_actions-woocommerce_page_wc-orders", array($this, 'AddBulkActions'), 100); add_filter("bulk_actions-edit-shop_order", array($this, 'AddBulkActions'), 100); add_filter('wc_get_template',array($this,'MaybeOverwriteTemplate'),10,5); foreach($this->WCEmailTemplateIds as $templateId) { add_filter( 'woocommerce_settings_api_form_fields_' . $templateId,array($this,'GetFormFields'),10,1); } foreach(array_merge(['customer_invoice_paid'],$this->WCEmailTemplateIds) as $templateId) { add_filter('woocommerce_email_subject_'.$templateId,array($this,'MaybeOverwriteSubject'),10,3); } } public function MaybeOverwriteTemplate($template,$templateName,$args,$templatePath,$defaultPath) { $advancedEmailId=Sanitizer::GetStringValueFromPath($args,['email','settings','advanced_email']); if($advancedEmailId!='') { $emailRepository=new EmailRepository($this); if($emailRepository->EmailExist($advancedEmailId)) { return $this->DIR.'EmailTemplatesOverwrite/EmailContentOverwrite.php'; } } return $template; } public function MaybeOverwriteSubject($subject,$order,$email=null) { if($email==null) return $subject; $advancedEmailId=Sanitizer::GetStringValueFromPath($email,['settings','advanced_email']); if($advancedEmailId!='') { $emailRepository=new EmailRepository($this); if($emailRepository->EmailExist($advancedEmailId)) { $renderer=RNADEM()->GetEmailRenderedByTemplateId(72,$order); if($renderer!=null) { $subject=$renderer->GetSubject(); } } } return $subject; } public function GetFormFields($formFields) { $list=RNADEM()->GetEmailList(); $emails=[ ''=>__('None (don\'t overwrite template','advanced-emailing-for-woocommerce') ]; foreach($list as $currentItem) { $emails[$currentItem->Id]=$currentItem->Name; } $formFields['advanced_email'] = array( 'title' => __( 'Overwrite with advanced email', 'advanced-emailing-for-woocommerce' ), 'type' => 'select', 'description' => __( 'If you overwrite the email, ids content and subject will be replaced by the template that you select.', 'advanced-emailing-for-woocommerce' ), 'default' => '', 'class' => 'email_type wc-enhanced-select', 'options' =>$emails, 'desc_tip' => true ); return $formFields; } public function AddBulkActions($actions){ $emailRepository=new EmailRepository($this); $emails=$emailRepository->GetEmailsIdAndName(); $suffix=''; if(!$this->IsPR()) $suffix=' '.__('(Full version only)','advanced-emailing-for-woocommerce'); foreach($emails as $email) { $actions['aefwc_send_email_'.$email->Id]=__('Send Email: ',"rnadvanceemailingwc").$email->Name.$suffix; } return $actions; } public function GetOrderActions($actions,$orders=null) { $repository=new EmailRepository($this); $emails=$repository->GetEmailsIdAndName(); foreach($emails as $email) { $actions['aefwc_send_email_'.$email->Id]=__('Send Email: ',"rnadvanceemailingwc").$email->Name; } return $actions; } public function CreateHooks() { add_action('admin_menu',array($this,'CreateMenu')); } public function CreateMenu(){ add_submenu_page('woocommerce',__('Advanced Emailing','advanced-emailing-for-woocommerce'),__('Advanced Emailing','advanced-emailing-for-woocommerce'),'manage_woocommerce','rnadvanceemailing',array($this,'EmailList')); } public function EmailList(){ if(isset($_GET['emailid'])) $renderer=new EmailBuilder($this); else $renderer=new AdvanceEmailList($this); $renderer->Render(); } public function IsPR(){ return \file_exists($this->DIR.'pr'); } /** * Returns the standard WooCommerce email IDs with human-readable labels * for the frontend dropdown. * * @return array */ public function GetWCEmailList() { $labels = [ 'new_order' => __('New order', 'advanced-emailing-for-woocommerce'), 'cancelled_order' => __('Cancelled order', 'advanced-emailing-for-woocommerce'), 'failed_order' => __('Failed order', 'advanced-emailing-for-woocommerce'), 'customer_on_hold_order' => __('Order on-hold', 'advanced-emailing-for-woocommerce'), 'customer_processing_order' => __('Processing order', 'advanced-emailing-for-woocommerce'), 'customer_completed_order' => __('Completed order', 'advanced-emailing-for-woocommerce'), 'customer_refunded_order' => __('Refunded order', 'advanced-emailing-for-woocommerce'), 'customer_invoice' => __('Customer invoice / Order details', 'advanced-emailing-for-woocommerce'), 'customer_note' => __('Customer note', 'advanced-emailing-for-woocommerce'), 'reset_password' => __('Reset password', 'advanced-emailing-for-woocommerce'), 'new_account' => __('New account', 'advanced-emailing-for-woocommerce'), ]; $list = []; foreach ($this->WCEmailTemplateIds as $id) { $list[] = ['Label' => isset($labels[$id]) ? $labels[$id] : $id, 'Value' => $id]; } return $list; } public function CheckIfPDFAdmin(){ if(!current_user_can('manage_options')) { die(__('Forbidden','advanced-emailing-for-woocommerce')); } } public function LoadTextDomain(){ load_plugin_textdomain('advanced-emailing-for-woocommerce', false, dirname(plugin_basename($this->MainFilePath)) . '/languages/'); } /** * @return TwigManager */ public function GetTwigManager($paths=[]){ if($this->Twig==null) { $this->Twig=new TwigManager($this,$paths); } return $this->Twig; } public function OnCreateTable() { global $wpdb; require_once(ABSPATH.'wp-admin/includes/upgrade.php'); $charset_collate = $wpdb->get_charset_collate(); $sql="CREATE TABLE ".$this->EMAIL_TABLE." ( id int AUTO_INCREMENT, name VARCHAR(100) not null, email_header_options MEDIUMTEXT NOT NULL, content_options MEDIUMTEXT, custom_fields MEDIUMTEXT, PRIMARY KEY (id) ) $charset_collate;"; dbDelta($sql); $sql="CREATE TABLE ".$this->EMAIL_TRIGGER_TABLE." ( id int AUTO_INCREMENT, email_id int, type VARCHAR(100), options MEDIUMTEXT, PRIMARY KEY (id) ) $charset_collate;"; dbDelta($sql); $sql="CREATE TABLE ".$this->SCHEDULE_TABLE." ( id bigint AUTO_INCREMENT, email_id int, order_id int, creation_date datetime, schedule_date datetime, PRIMARY KEY (id), KEY schedule_id(email_id,order_id) ) $charset_collate;"; dbDelta($sql); $sql="CREATE TABLE ".$this->CART_ABANDONMENT_TABLE." ( id bigint AUTO_INCREMENT, user_id bigint DEFAULT 0, session_id VARCHAR(200), email VARCHAR(200) NOT NULL, cart_contents MEDIUMTEXT, cart_total DECIMAL(10,2) DEFAULT 0, date_updated datetime NOT NULL, emails_sent int DEFAULT 0, PRIMARY KEY (id), KEY user_id(user_id), KEY session_id(session_id) ) $charset_collate;"; dbDelta($sql); } public function GetLoader(){ return $this; } } /** * Gets AI model names and labels for the frontend model selector. * * @return array List of ['Name' => string, 'Label' => string] */ function rnadem_get_ai_model_names() { $models = get_option('rnadem_ai_models', []); $names = []; $labelMap = [ 'gemini25pro' => 'Gemini 2.5 Pro', 'gemini3pro' => 'Gemini 3 Pro', 'deepseek' => 'DeepSeek', ]; foreach ($models as $model) { $modelName = is_object($model) ? $model->Name : (isset($model['Name']) ? $model['Name'] : ''); if (empty($modelName)) continue; $label = isset($labelMap[$modelName]) ? $labelMap[$modelName] : $modelName; $names[] = ['Name' => $modelName, 'Label' => $label]; } return $names; } /** * Gets the default AI model name. * * @return string Model name (e.g. 'gemini25pro') or empty string */ function rnadem_get_ai_default_model() { $models = get_option('rnadem_ai_models', []); foreach ($models as $model) { $isDefault = is_object($model) ? (isset($model->Default) && $model->Default) : (isset($model['Default']) && $model['Default']); if ($isDefault) { return is_object($model) ? $model->Name : $model['Name']; } } if (count($models) > 0) { $first = $models[0]; return is_object($first) ? $first->Name : (isset($first['Name']) ? $first['Name'] : ''); } return ''; } /** * Gets the API key for a specific AI model. * * @param string $modelName Model name (e.g. 'gemini25pro', 'deepseek') * @return string API key or empty string */ function rnadem_get_ai_api_key($modelName) { $models = get_option('rnadem_ai_models', []); foreach ($models as $model) { $name = is_object($model) ? $model->Name : (isset($model['Name']) ? $model['Name'] : ''); if ($name === $modelName) { return is_object($model) ? $model->APIKey : (isset($model['APIKey']) ? $model['APIKey'] : ''); } } return ''; }