id ) ) { return false; } $resolved = self::recipients( (string) $request->request_type, $s ); if ( empty( $resolved['emails'] ) ) { if ( ! $is_summary ) { AIEO_Product_Request_Store::release_notification( (int) $request->id ); } return false; } $request = AIEO_Product_Request_Store::get_request( (int) $request->id ); $emails = (array) $resolved['emails']; if ( class_exists( 'AIEO_Staff_Digest' ) ) { $split = AIEO_Staff_Digest::split( $emails ); $missed = $split['later'] ? AIEO_Staff_Digest::queue( $split['later'], self::digest_item( $request, $is_summary ) ) : array(); $emails = array_merge( $split['now'], $missed ); if ( ! $emails ) { return true; } } $sent = false; if ( self::load_wc_email() && function_exists( 'WC' ) && WC()->mailer() ) { $mails = WC()->mailer()->get_emails(); if ( isset( $mails['aieo_product_request'] ) && $mails['aieo_product_request'] instanceof AIEO_Email_Product_Request ) { $sent = (bool) $mails['aieo_product_request']->trigger( (int) $request->id, $emails, $is_summary ); } } if ( ! $sent ) { $split = self::to_and_bcc( $emails ); $sent = AIEO_Email_Frame::send_text( $split['to'], self::staff_subject( $request, $is_summary ), self::body_plain( $request, $s ), array( 'id' => 'aieo_product_request', 'customer' => false ), array_merge( array( 'Content-Type: text/plain; charset=UTF-8' ), self::bcc_headers( $split['bcc'] ) ) ); } if ( ! $sent && ! $is_summary ) { AIEO_Product_Request_Store::release_notification( (int) $request->id ); } return $sent; } public static function digest_item( $request, $is_summary = false ) { $types = AIEO_Product_Request_Settings::types(); $names = array(); foreach ( array_slice( (array) AIEO_Product_Request_Store::items( (int) $request->id ), 0, 3 ) as $row ) { $p = function_exists( 'wc_get_product' ) ? wc_get_product( (int) $row->product_id ) : null; if ( $p ) { $names[] = $p->get_name(); } } $who = array_filter( array( (string) $request->name, '' !== (string) $request->email ? '<' . $request->email . '>' : '', (string) $request->phone, ) ); return array( 'source' => $is_summary ? 'request_list' : 'request', 'source_id' => (int) $request->id, 'group' => isset( $types[ (string) $request->request_type ] ) ? $types[ (string) $request->request_type ] : (string) $request->request_type, 'title' => ( $is_summary ? __( 'Completed', 'ai-eshop-optimizer' ) . ' ' : '' ) . '#' . (int) $request->id . ( $who ? ' · ' . implode( ' ', $who ) : '' ), 'summary' => implode( ', ', $names ), 'link' => self::detail_url( (int) $request->id ), ); } private static function send_ack( $request, array $s ) { if ( 'yes' !== (string) $s['ack_enabled'] ) { return false; } $to = sanitize_email( (string) $request->email ); if ( ! is_email( $to ) ) { return false; } $subject = (string) $s['ack_subject']; $body = (string) $s['ack_message']; $raw = AIEO_Product_Request_Settings::get( true ); if ( '' === trim( (string) ( $raw['ack_message'] ?? '' ) ) ) { $body = AIEO_Product_Request_Settings::default_ack_message( (string) $request->request_type ); } $items = AIEO_Product_Request_Store::items( (int) $request->id ); if ( $items ) { $body .= "\n\n" . __( 'What you asked about:', 'ai-eshop-optimizer' ) . "\n"; foreach ( $items as $row ) { $p = (int) ( $row->variation_id ?? 0 ) > 0 ? wc_get_product( (int) $row->variation_id ) : null; $p = $p ? $p : wc_get_product( (int) $row->product_id ); if ( ! $p ) { continue; } $named = method_exists( 'AIEO_DMM_Swatches_Runtime', 'shade_title' ) ? AIEO_DMM_Swatches_Runtime::shade_title( $p ) : $p->get_name(); $body .= 'yes' === (string) ( isset( $s['field_qty'] ) ? $s['field_qty'] : 'yes' ) ? ' - ' . $named . ' x' . max( 1, (int) $row->qty ) . "\n" : ' - ' . $named . "\n"; } } return AIEO_Email_Frame::send_text( $to, $subject, $body, array( 'id' => 'aieo_product_request_ack' ), array( 'Content-Type: text/plain; charset=UTF-8' ) ); } public static function load_wc_email() { if ( class_exists( 'AIEO_Email_Product_Request' ) ) { return true; } if ( ! class_exists( 'WC_Email' ) ) { return false; } $file = __DIR__ . '/class-aieo-wc-email-product-request.php'; if ( ! is_readable( $file ) ) { return false; } require_once $file; return class_exists( 'AIEO_Email_Product_Request' ); } public static function to_and_bcc( array $emails ) { $emails = array_values( array_unique( array_filter( array_map( 'sanitize_email', $emails ) ) ) ); if ( empty( $emails ) ) { return array( 'to' => '', 'bcc' => array() ); } $to = (string) apply_filters( 'aieo_product_request_mail_to', $emails[0], $emails ); $to = is_email( $to ) ? $to : $emails[0]; $bcc = array_values( array_diff( $emails, array( $to ) ) ); return array( 'to' => $to, 'bcc' => $bcc ); } public static function bcc_headers( array $bcc ) { return empty( $bcc ) ? array() : array( 'Bcc: ' . implode( ', ', $bcc ) ); } public static function recipients( $type = 'quote', $s = null ) { if ( ! is_array( $s ) ) { $s = AIEO_Product_Request_Settings::get( true ); } $type = sanitize_key( (string) $type ); $override = self::split_emails( (string) ( $s[ 'recipients_' . $type ] ?? '' ) ); if ( $override ) { return array( 'emails' => $override, 'source' => 'type_override', 'label' => sprintf( __( 'using: the per-type override for %s', 'ai-eshop-optimizer' ), $type ), ); } $field = self::split_emails( (string) $s['recipients'] ); if ( $field ) { return array( 'emails' => $field, 'source' => 'setting', 'label' => __( 'using: the recipients set on this screen', 'ai-eshop-optimizer' ), ); } $staff = self::staff_emails(); if ( $staff ) { return array( 'emails' => $staff, 'source' => 'staff', 'label' => sprintf( _n( 'using: %d shop manager or administrator', 'using: %d shop managers and administrators', count( $staff ), 'ai-eshop-optimizer' ), count( $staff ) ), ); } $rooster = sanitize_email( (string) get_option( 'aieo_rooster_client_email', '' ) ); if ( is_email( $rooster ) ) { return array( 'emails' => array( $rooster ), 'source' => 'rooster', 'label' => __( 'using: the Rooster One contact person', 'ai-eshop-optimizer' ), ); } $admin = sanitize_email( (string) get_option( 'admin_email' ) ); return array( 'emails' => is_email( $admin ) ? array( $admin ) : array(), 'source' => 'admin_email', 'label' => __( 'using: the WordPress administration address', 'ai-eshop-optimizer' ), ); } private static function staff_emails() { $users = get_users( array( 'role__in' => array( 'shop_manager', 'administrator' ), 'fields' => array( 'user_email' ), 'number' => self::STAFF_CAP * 2, ) ); $out = array(); foreach ( (array) $users as $u ) { $e = sanitize_email( (string) $u->user_email ); if ( is_email( $e ) && ! in_array( $e, $out, true ) ) { $out[] = $e; } if ( count( $out ) >= self::STAFF_CAP ) { break; } } return $out; } public static function split_emails( $csv ) { $out = array(); foreach ( preg_split( '/[,;\s]+/', (string) $csv ) as $bit ) { $e = sanitize_email( trim( (string) $bit ) ); if ( is_email( $e ) && ! in_array( $e, $out, true ) ) { $out[] = $e; } } return $out; } public static function staff_subject( $request, $is_summary = false ) { $site = wp_specialchars_decode( (string) get_option( 'blogname' ), ENT_QUOTES ); $types = AIEO_Product_Request_Settings::types(); $label = isset( $types[ (string) $request->request_type ] ) ? $types[ (string) $request->request_type ] : (string) $request->request_type; $id = (int) $request->id; switch ( (string) $request->request_type ) { case 'quote': return sprintf( $is_summary ? __( '[%1$s] Completed quote request #%2$d', 'ai-eshop-optimizer' ) : __( '[%1$s] New quote request #%2$d', 'ai-eshop-optimizer' ), $site, $id ); case 'contact': return sprintf( $is_summary ? __( '[%1$s] Completed contact request #%2$d', 'ai-eshop-optimizer' ) : __( '[%1$s] New contact request #%2$d', 'ai-eshop-optimizer' ), $site, $id ); case 'callback': return sprintf( $is_summary ? __( '[%1$s] Completed call-back request #%2$d', 'ai-eshop-optimizer' ) : __( '[%1$s] New call-back request #%2$d', 'ai-eshop-optimizer' ), $site, $id ); } if ( $is_summary ) { return sprintf( __( '[%1$s] Completed %2$s #%3$d', 'ai-eshop-optimizer' ), $site, $label, (int) $request->id ); } return sprintf( __( '[%1$s] New %2$s #%3$d', 'ai-eshop-optimizer' ), $site, $label, (int) $request->id ); } public static function detail_url( $request_id ) { $slug = class_exists( 'AIEO_DMM' ) ? AIEO_DMM::MENU_SLUG : 'ai-eshop-dmm'; return admin_url( 'admin.php?page=' . $slug . '&dmm_tab=product_requests&request_id=' . (int) $request_id ); } public static function body_plain( $request, array $s ) { $types = AIEO_Product_Request_Settings::types(); $lines = array(); $lines[] = isset( $types[ (string) $request->request_type ] ) ? $types[ (string) $request->request_type ] : (string) $request->request_type; $lines[] = str_repeat( '=', 40 ); $lines[] = ''; $lines[] = __( 'Contact details', 'ai-eshop-optimizer' ); if ( '' !== (string) $request->name ) { $lines[] = ' ' . __( 'Name:', 'ai-eshop-optimizer' ) . ' ' . $request->name; } if ( '' !== (string) $request->email ) { $lines[] = ' ' . __( 'Email:', 'ai-eshop-optimizer' ) . ' ' . $request->email; } if ( '' !== (string) $request->phone ) { $lines[] = ' ' . __( 'Phone:', 'ai-eshop-optimizer' ) . ' ' . $request->phone; } if ( '' !== trim( (string) $request->message ) ) { $lines[] = ''; $lines[] = __( 'Message', 'ai-eshop-optimizer' ); $lines[] = ' ' . str_replace( "\n", "\n ", (string) $request->message ); } $lines[] = ''; $lines[] = AIEO_Product_Request_Trail::as_text( $request ); $attr = AIEO_Product_Request_Store::attribution_of( $request ); $lines[] = ''; $lines[] = __( 'Campaign attribution', 'ai-eshop-optimizer' ); foreach ( array( 'utm_source' => __( 'Source', 'ai-eshop-optimizer' ), 'utm_medium' => _x( 'Medium', 'UTM parameter', 'ai-eshop-optimizer' ), 'utm_campaign' => __( 'Campaign', 'ai-eshop-optimizer' ), 'utm_content' => __( 'Content', 'ai-eshop-optimizer' ), 'utm_term' => __( 'Term', 'ai-eshop-optimizer' ), 'gclid' => __( 'Google click id', 'ai-eshop-optimizer' ), 'landing_page' => __( 'Landing page', 'ai-eshop-optimizer' ), 'referrer' => __( 'Referrer', 'ai-eshop-optimizer' ), ) as $k => $label ) { $v = isset( $attr[ $k ] ) ? trim( (string) $attr[ $k ] ) : ''; $lines[] = ' ' . $label . ': ' . ( '' !== $v ? $v : '—' ); } $lines[] = ''; $lines[] = __( 'Open this request:', 'ai-eshop-optimizer' ) . ' ' . self::detail_url( (int) $request->id ); return implode( "\n", $lines ); } } }