settings->get_setting( 'customer-notes-email' );
if ( ! $email_id ) { return; }
$email_address = $ewd_otp_controller->settings->get_setting( 'admin-email' ) ? $ewd_otp_controller->settings->get_setting( 'admin-email' ) : get_option( 'admin_email' );
if ( $email_id < 0 ) {
$args = array(
'email_id' => $email_id * -1,
'order_id' => $order->id,
'email_address' => $email_address
);
if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
}
else {
$this->send_email( $email_id, $email_address, $order );
}
}
/**
* Send an email to the site admin when a customer order is submitted, if selected
*
* @since 3.0.0
*/
public function admin_customer_order_email( $order ) {
global $ewd_otp_controller;
$email_id = $ewd_otp_controller->settings->get_setting( 'customer-order-email' );
if ( ! $email_id ) { return; }
$email_address = $ewd_otp_controller->settings->get_setting( 'admin-email' ) ? $ewd_otp_controller->settings->get_setting( 'admin-email' ) : get_option( 'admin_email' );
if ( $email_id < 0 ) {
$args = array(
'email_id' => $email_id * -1,
'order_id' => $order->id,
'email_address' => $email_address
);
if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
}
else {
$this->send_email( $email_id, $email_address, $order );
}
}
/**
* Send an email to the client when an order is created, if selected
*
* @since 3.0.0
*/
public function user_order_created_email( $order ) {
global $ewd_otp_controller;
if ( $ewd_otp_controller->settings->get_setting( 'email-frequency' ) == 'never' ) { return; }
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
foreach ( $statuses as $status ) {
if ( $status->status == $order->status ) { $email_id = $status->email; }
}
if ( empty( $email_id ) ) { return; }
$email_addresses = explode( ',', $order->email );
if ( empty( $email_addresses ) ) { return; }
foreach ( $email_addresses as $email_address ) {
if ( $email_id < 0 ) {
$args = array(
'email_id' => $email_id * -1,
'order_id' => $order->id,
'email_address' => $email_address
);
if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
}
else {
$this->send_email( $email_id, $email_address, $order );
}
}
}
/**
* Send an email to the client when an order status is changed, if selected
*
* @since 3.0.0
*/
public function user_status_updated_email( $order ) {
global $ewd_otp_controller;
if ( $ewd_otp_controller->settings->get_setting( 'email-frequency' ) != 'change' ) { return; }
$statuses = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'statuses' ) );
foreach ( $statuses as $status ) {
if ( $status->status == $order->status ) { $email_id = $status->email; }
}
if ( empty( $email_id ) ) { return; }
$email_addresses = explode( ',', $order->email );
if ( empty( $email_addresses ) ) { return; }
foreach ( $email_addresses as $email_address ) {
if ( $email_id < 0 ) {
$args = array(
'email_id' => $email_id * -1,
'order_id' => $order->id,
'email_address' => $email_address
);
if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
}
else {
$this->send_email( $email_id, $email_address, $order );
}
}
}
/**
* Send an email to the sales rep when an order status is changed, if selected
*
* @since 3.0.0
*/
public function sales_rep_status_updated_email( $order ) {
global $ewd_otp_controller;
if ( empty( $ewd_otp_controller->settings->get_setting( 'sales-rep-status-notifications' ) ) ) { return; }
$email_id = $ewd_otp_controller->settings->get_setting( 'sales-rep-status-email' );
if ( empty( $email_id ) ) { return; }
if ( empty( $order->sales_rep ) ) { return; }
$sales_rep = new ewdotpSalesRep();
$sales_rep->load_sales_rep_from_id( $order->sales_rep );
if ( empty( $sales_rep->email ) ) { return; }
if ( $email_id < 0 ) {
$args = array(
'email_id' => $email_id * -1,
'order_id' => $order->id,
'email_address' => $sales_rep->email
);
if ( function_exists( 'ewd_uwpm_send_email' ) ) { ewd_uwpm_send_email( $args ); }
}
else {
$this->send_email( $email_id, $sales_rep->email, $order );
}
}
/**
* Send an email using an admin created template
*
* @since 3.0.0
*/
public function send_email( $email_id, $email_address, $order ) {
global $ewd_otp_controller;
$email_messages = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'email-messages' ) );
foreach ( $email_messages as $email_message ) {
if ( $email_message->id != $email_id ) { continue; }
$message = $this->substitute_email_text( $this->get_email_template( $email_message ), $order );
$subject = $this->substitute_email_text( $email_message->subject, $order);
$headers = array( 'Content-Type: text/html; charset=UTF-8' );
$mail_success = wp_mail( $email_address, $subject, $message, $headers );
return $mail_success;
}
}
/**
* Replace plugin-defined tags with order information
*
* @since 3.0.0
*/
function substitute_email_text( $text, $order ) {
global $ewd_otp_controller;
$confirmation_code = ewd_random_string();
$order->set_tracking_link_code( $confirmation_code );
$args = array(
'tracking_number' => $order->number,
'email' => $order->email,
'tracking_link_code' => $confirmation_code
);
$tracking_url = add_query_arg( $args, $ewd_otp_controller->settings->get_setting( 'tracking-page-url' ) );
$tracking_link = "[button link='" . $tracking_url . "']" . __( 'Track your order', 'order-tracking' ) . "[/button]";
date_default_timezone_set( get_option( 'timezone_string' ) );
$search = array(
"[order-name]",
"[order-number]",
"[order-status]",
"[order-notes]",
"[customer-notes]",
"[order-time]",
"[tracking-link]",
"[customer-name]",
"[customer-number]",
"[customer-id]",
"[sales-rep]",
"[sales-rep-number]"
);
$replace = array(
! empty( $order->name ) ? $order->name : '',
! empty( $order->number ) ? $order->number : '',
! empty( $order->external_status ) ? $order->external_status : '',
! empty( $order->notes_public ) ? $order->notes_public : '',
! empty( $order->customer_notes ) ? $order->customer_notes : '',
date( $ewd_otp_controller->settings->get_setting( 'date-format' ), strtotime( $order->status_updated ) ),
$tracking_link,
$ewd_otp_controller->customer_manager->get_customer_field( 'name', $order->customer ),
$ewd_otp_controller->customer_manager->get_customer_field( 'number', $order->customer ),
! empty( $order->customer ) ? $order->customer : '',
$ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'first_name', $order->sales_rep ) . ' ' . $ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'last_name', $order->sales_rep ),
$ewd_otp_controller->sales_rep_manager->get_sales_rep_field( 'number', $order->sales_rep ),
);
$custom_fields = ewd_otp_decode_infinite_table_setting( $ewd_otp_controller->settings->get_setting( 'custom-fields' ) );
foreach ( $custom_fields as $custom_field ) {
$value = $ewd_otp_controller->order_manager->get_field_value( $custom_field->id, $order->id );
$search[] = '[' . $custom_field->slug . ']';
$replace[] = $value;
}
$order_text = str_replace( $search, $replace, $text );
return $this->replace_email_content( $order_text );
}
/**
* Returns a template of the email message, along with admin styling for it
*
* @since 3.0.0
*/
public function get_email_template( $email_message ) {
global $ewd_otp_controller;
$message_title = $email_message->subject;
$message_content = $this->replace_email_content( stripslashes( $email_message->message ) );
$message = <<< EOT
$message_title
|
|
EOT;
return $message;
}
/**
* Replace the structure elements of an email template
*
* @since 3.0.0
*/
public function replace_email_content( $unprocessed_message ) {
$search = array('[section]', '[/section]', '[footer]', '[/footer]', '[/button]');
$replace = array(
' | | ',
' | ',
'
|
',
''
);
$intemediate_message = str_replace( $search, $replace, $unprocessed_message );
$processed_message = $this->replace_email_links( $intemediate_message );
return $processed_message;
}
/**
* Replace all of the button links used in the email template
*
* @since 3.0.0
*/
public function replace_email_links( $unprocessed_message ) {
$pattern = "/\[button link=\'(.*?)\'\]/";
preg_match_all( $pattern, $unprocessed_message, $matches );
$replace = '| ';
$message = preg_replace( $pattern, $replace, $unprocessed_message );
if ( is_array( $matches[1] ) ) {
foreach ( $matches[1] as $link ) {
$pos = strpos( $message, "INSERTED_LINK" );
if ($pos !== false) {
$intermediate_message = substr_replace( $message, $link, $pos, 13 );
$message = $intermediate_message;
}
}
}
return $message;
}
}
} // endif;
|