id = 'aieo_withdrawal_request'; $this->title = __('Withdrawal declaration (store notice)', 'ai-eshop-optimizer'); $this->description = __('Sent to your staff when a customer records a withdrawal declaration that needs processing. Orders auto-cancelled inside the courtesy window are not included — WooCommerce\'s "Cancelled order" email already covers those.', 'ai-eshop-optimizer'); $this->template_html = 'emails/aieo-withdrawal-request.php'; $this->template_plain = 'emails/plain/aieo-withdrawal-request.php'; $dir = __DIR__; $root = ('withdrawal' === basename($dir)) ? dirname($dir, 2) : dirname($dir); $this->template_base = trailingslashit($root) . 'templates/'; $this->placeholders = array('{order_number}' => ''); parent::__construct(); $this->recipient = $this->get_option('recipient', $this->default_recipient()); if (class_exists('AIEO_Email_Frame')) { AIEO_Email_Frame::boot(); } add_filter('woocommerce_prepare_email_for_preview', array($this, 'aieo_preview')); } public function aieo_preview($email) { if ($email !== $this) { return $email; } $order = $this->object instanceof WC_Order ? $this->object : null; $items = array(); foreach ($order ? $order->get_items() : array() as $item) { $qty = max(1, (int) $item->get_quantity()); $items[] = array('name' => $item->get_name(), 'qty' => $qty, 'withdraw_qty' => $qty); break; } $this->declaration = (object) array( 'declared_at_gmt' => gmdate('Y-m-d H:i:s'), 'is_full' => 0, 'reason_note' => __('The colour is not what I expected.', 'ai-eshop-optimizer'), ); $this->context = array('items' => $items); if ($order) { $this->placeholders['{order_number}'] = $order->get_order_number(); } return $email; } public function default_recipient() { $new_order = get_option('woocommerce_new_order_settings'); if (is_array($new_order) && !empty($new_order['recipient'])) { return (string) $new_order['recipient']; } return (string) get_option('admin_email'); } public function get_default_subject() { return __('[{site_title}] Withdrawal declaration for order {order_number}', 'ai-eshop-optimizer'); } public function get_default_heading() { return __('Withdrawal declaration received', 'ai-eshop-optimizer'); } public function init_form_fields() { parent::init_form_fields(); $fields = array(); foreach ($this->form_fields as $key => $field) { $fields[$key] = $field; if ('enabled' === $key) { $fields['recipient'] = array( 'title' => __('Recipient(s)', 'woocommerce'), 'type' => 'text', 'description' => sprintf(__('Enter recipients (comma separated) for this email. Defaults to %s.', 'woocommerce'), '' . esc_html($this->default_recipient()) . ''), 'placeholder' => '', 'default' => '', 'desc_tip' => true, ); } } $this->form_fields = $fields; } public function trigger($declaration_id, $order = null, array $context = array()) { $this->setup_locale(); $this->declaration = $this->load_declaration((int) $declaration_id); $this->context = $context; if ($order instanceof WC_Order) { $this->object = $order; $this->placeholders['{order_number}'] = $order->get_order_number(); } if ($this->object instanceof WC_Order && $this->is_enabled() && $this->get_recipient()) { $this->send($this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments()); } $this->restore_locale(); } protected function load_declaration($id) { global $wpdb; if ($id <= 0) { return null; } return $wpdb->get_row($wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aieo_dmm_withdrawal_declarations WHERE id = %d", $id )); } public function queue_url() { $slug = class_exists('AIEO_DMM') ? AIEO_DMM::MENU_SLUG : 'ai-eshop-dmm'; return admin_url('admin.php?page=' . $slug . '&dmm_tab=withdrawal'); } public function get_content_html() { return wc_get_template_html($this->template_html, $this->template_args(false), '', $this->template_base); } public function get_content_plain() { return wc_get_template_html($this->template_plain, $this->template_args(true), '', $this->template_base); } protected function template_args($plain) { return array( 'order' => $this->object, 'declaration' => $this->declaration, 'items' => (isset($this->context['items']) && is_array($this->context['items'])) ? $this->context['items'] : array(), 'queue_url' => $this->queue_url(), 'email_heading' => $this->get_heading(), 'additional_content' => $this->get_additional_content(), 'sent_to_admin' => true, 'plain_text' => (bool) $plain, 'email' => $this, ); } } }