status, self::done_statuses(), true ) ) { return; } $order = function_exists( 'wc_get_order' ) ? wc_get_order( (int) $row->order_id ) : null; $to = self::recipient( $order ); if ( '' === $to ) { return; } $sent = false; try { $sent = (bool) wp_mail( $to, self::subject(), self::body( $order, $to ), self::headers() ); } catch ( Throwable $e ) { $sent = false; } self::stamp_sent( $declaration_id, $order ); do_action( 'aieo_withdrawal_survey_sent', $declaration_id, $order, $sent ); } private static function subject() { return sprintf( __( 'A question about your return, from %s', 'ai-eshop-optimizer' ), wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES ) ); } private static function body( $order, $email ) { $url = self::survey_url( $order, $email ); $lines = array( __( 'Your withdrawal is complete and there is nothing further you need to do.', 'ai-eshop-optimizer' ), '', __( 'If you have a moment, we would like to know what led to it. It helps us fix what is worth fixing.', 'ai-eshop-optimizer' ), '', $url, '', __( 'Answering is entirely optional. It has no effect on your withdrawal, your refund or your account, and you did not have to give a reason for any of them.', 'ai-eshop-optimizer' ), ); return implode( "\n", $lines ) . "\n"; } private static function survey_url( $order, $email ) { $s = self::settings(); $url = trim( (string) ( $s['survey_url'] ?? '' ) ); if ( '' === $url || ! $order || ! class_exists( 'AIEO_DMM_Surveys' ) || ! method_exists( 'AIEO_DMM_Surveys', 'signed_survey_url' ) ) { return $url; } return (string) AIEO_DMM_Surveys::signed_survey_url( $url, (int) $order->get_id(), (string) $email ); } private static function headers() { $from = wp_specialchars_decode( (string) get_bloginfo( 'name' ), ENT_QUOTES ); $mail = (string) get_option( 'admin_email' ); if ( function_exists( 'WC' ) && WC()->mailer() ) { $mail = (string) get_option( 'woocommerce_email_from_address', $mail ); $from = (string) get_option( 'woocommerce_email_from_name', $from ); } return array( sprintf( 'From: %s <%s>', $from, $mail ) ); } private static function done_statuses() { return class_exists( 'AIEO_Withdrawal' ) ? array( AIEO_Withdrawal::STATUS_REFUNDED, AIEO_Withdrawal::STATUS_CANCELLED ) : array( 'refunded', 'cancelled' ); } private static function table() { global $wpdb; return $wpdb->prefix . 'aieo_dmm_withdrawal_declarations'; } private static function row( $declaration_id ) { global $wpdb; $t = self::table(); return $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$t} WHERE id = %d", (int) $declaration_id ) ); } private static function recipient( $order ) { if ( ! $order || ! is_a( $order, 'WC_Order' ) ) { return ''; } if ( apply_filters( 'aieo_suppress_order_customer_email', false, $order, 'withdrawal_survey' ) ) { return ''; } $email = (string) $order->get_billing_email(); return is_email( $email ) ? $email : ''; } private static function meta_key( $declaration_id ) { return '_aieo_withdrawal_survey_sent_' . (int) $declaration_id; } private static function already_sent( $declaration_id ) { $row = self::row( $declaration_id ); if ( ! $row || ! function_exists( 'wc_get_order' ) ) { return false; } $order = wc_get_order( (int) $row->order_id ); return $order ? '' !== (string) $order->get_meta( self::meta_key( $declaration_id ) ) : false; } private static function stamp_sent( $declaration_id, $order ) { if ( ! $order || ! is_a( $order, 'WC_Order' ) ) { return; } $order->update_meta_data( self::meta_key( $declaration_id ), gmdate( 'Y-m-d H:i:s' ) ); $order->save(); } } }