id = 'aieo_product_request'; $this->title = __( 'Product request (store notice)', 'ai-eshop-optimizer' ); $this->description = __( 'Sent to your staff when a visitor asks for a quote or asks to be contacted about a product. One mail per request. Adding more products to an open quote never re-sends it.', 'ai-eshop-optimizer' ); $this->template_html = 'emails/aieo-product-request.php'; $this->template_plain = 'emails/plain/aieo-product-request.php'; $this->customer_email = false; $root = defined( 'AIEO_DIR' ) ? AIEO_DIR : trailingslashit( dirname( __DIR__, 3 ) ); $this->template_base = trailingslashit( $root ) . 'templates/'; $this->placeholders = array( '{request_id}' => '', '{request_type}' => '' ); parent::__construct(); $this->recipient = $this->get_option( 'recipient', '' ); if ( class_exists( 'AIEO_Email_Frame' ) ) { AIEO_Email_Frame::boot(); } add_filter( 'woocommerce_prepare_email_for_preview', array( $this, 'aieo_preview' ) ); } protected $aieo_preview_trail = null; public function aieo_preview( $email ) { if ( $email !== $this ) { return $email; } $this->is_summary = false; $this->request = (object) array( 'id' => 1024, 'request_type' => 'quote', 'name' => 'Eleni Papadaki', 'email' => 'eleni@example.com', 'phone' => '+30 210 000 0000', 'message' => __( 'Is this shade coming back soon? I would like two.', 'ai-eshop-optimizer' ), 'created_at' => gmdate( 'Y-m-d H:i:s' ), 'seen_before' => '', 'session_id' => '', 'attribution' => wp_json_encode( array( 'utm_source' => 'newsletter', 'utm_medium' => 'email', 'landing_page' => home_url( '/' ) ) ), ); $requested = array(); $order = $this->object instanceof WC_Order ? $this->object : null; foreach ( $order ? $order->get_items() : array() as $item ) { $product = is_callable( array( $item, 'get_product' ) ) ? $item->get_product() : null; $requested[] = array( 'name' => $item->get_name(), 'permalink' => $product ? (string) $product->get_permalink() : home_url( '/' ), 'sku' => $product ? (string) $product->get_sku() : '', 'qty' => (int) $item->get_quantity(), ); } $this->object = null; $this->aieo_preview_trail = array( 'requested' => $requested, 'removed' => array(), 'before' => array(), 'after' => array(), 'source_flags' => array( 'tracking_on' => true ), ); $this->placeholders['{request_id}'] = '1024'; $this->placeholders['{request_type}'] = AIEO_Product_Request_Settings::types()['quote']; return $email; } public function default_recipient() { $r = AIEO_Product_Request_Mailer::recipients( 'quote' ); return implode( ', ', $r['emails'] ); } public function get_recipient() { if ( '' === trim( (string) $this->recipient ) ) { $this->recipient = $this->default_recipient(); } return parent::get_recipient(); } public function admin_options() { if ( isset( $this->form_fields['recipient'] ) ) { $this->form_fields['recipient']['description'] = sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to %s.', 'woocommerce' ), '' . esc_html( $this->default_recipient() ) . '' ); } parent::admin_options(); } public function get_default_subject() { return __( '[{site_title}] New {request_type} #{request_id}', 'ai-eshop-optimizer' ); } public function get_subject() { if ( $this->is_summary && $this->request ) { return (string) apply_filters( 'woocommerce_email_subject_' . $this->id, AIEO_Product_Request_Mailer::staff_subject( $this->request, true ), $this->object, $this ); } if ( $this->request && ! $this->has_own_subject() ) { return (string) apply_filters( 'woocommerce_email_subject_' . $this->id, AIEO_Product_Request_Mailer::staff_subject( $this->request, false ), $this->object, $this ); } return parent::get_subject(); } private function has_own_subject() { $saved = get_option( $this->get_option_key(), array() ); return is_array( $saved ) && '' !== trim( (string) ( $saved['subject'] ?? '' ) ); } public function get_default_heading() { return __( 'A visitor asked about a product', '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' => '', 'placeholder' => '', 'default' => '', 'desc_tip' => true, ); } } $this->form_fields = $fields; } public function trigger( $request_id, $recipients = array(), $is_summary = false ) { $this->setup_locale(); $this->aieo_preview_trail = null; $this->request = AIEO_Product_Request_Store::get_request( (int) $request_id ); $this->is_summary = (bool) $is_summary; if ( ! $this->request ) { $this->restore_locale(); return false; } $types = AIEO_Product_Request_Settings::types(); $this->placeholders['{request_id}'] = (string) (int) $request_id; $this->placeholders['{request_type}'] = isset( $types[ (string) $this->request->request_type ] ) ? $types[ (string) $this->request->request_type ] : (string) $this->request->request_type; $typed = trim( (string) $this->get_option( 'recipient', '' ) ); $list = '' !== $typed ? AIEO_Product_Request_Mailer::split_emails( $typed ) : (array) $recipients; $split = AIEO_Product_Request_Mailer::to_and_bcc( $list ); $to = $split['to']; $this->aieo_bcc = $split['bcc']; $sent = false; if ( $this->is_enabled() && '' !== $to ) { $sent = (bool) $this->send( $to, $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); } $this->restore_locale(); return $sent; } protected $aieo_bcc = array(); public function get_headers() { $headers = (string) parent::get_headers(); if ( ! empty( $this->aieo_bcc ) ) { $headers .= 'Bcc: ' . implode( ', ', $this->aieo_bcc ) . "\r\n"; } return $headers; } 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 ) { $s = AIEO_Product_Request_Settings::get(); return array( 'request' => $this->request, 'trail' => null !== $this->aieo_preview_trail ? $this->aieo_preview_trail : AIEO_Product_Request_Trail::for_request( $this->request ), 'attribution' => AIEO_Product_Request_Store::attribution_of( $this->request ), 'detail_url' => AIEO_Product_Request_Mailer::detail_url( (int) $this->request->id ), 'body_plain' => AIEO_Product_Request_Mailer::body_plain( $this->request, $s ), 'email_heading' => $this->get_heading(), 'additional_content' => $this->get_additional_content(), 'sent_to_admin' => true, 'plain_text' => (bool) $plain, 'email' => $this, ); } } }