| 1 |
<?php |
| 2 |
|
| 3 |
namespace EmailKit\Admin\Emails\Woocommerce; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
use WP_User; |
| 7 |
|
| 8 |
defined('ABSPATH') || exit; |
| 9 |
|
| 10 |
class NewAccount |
| 11 |
{ |
| 12 |
public $user_login; |
| 13 |
public $user_email; |
| 14 |
public $user_pass; |
| 15 |
public $password_generated; |
| 16 |
public $set_password_url; |
| 17 |
public $recipient; |
| 18 |
public $object; |
| 19 |
|
| 20 |
private $db_query_class = null; |
| 21 |
|
| 22 |
public function __construct() |
| 23 |
{ |
| 24 |
$args = array( |
| 25 |
'post_type' => 'emailkit', |
| 26 |
'meta_query' => array( |
| 27 |
array( |
| 28 |
'key' => 'emailkit_template_type', |
| 29 |
'value' => 'New Account', |
| 30 |
), |
| 31 |
array( |
| 32 |
'key' => 'emailkit_template_status', |
| 33 |
'value' => 'Active', |
| 34 |
), |
| 35 |
), |
| 36 |
); |
| 37 |
|
| 38 |
|
| 39 |
$this->db_query_class = new WP_Query($args); |
| 40 |
|
| 41 |
if (isset($this->db_query_class->posts[0])) { |
| 42 |
add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']); |
| 43 |
} |
| 44 |
|
| 45 |
add_filter('woocommerce_created_customer_notification', [$this, 'newAccountMail'], 10, 3); |
| 46 |
} |
| 47 |
|
| 48 |
public function remove_woocommerce_emails($email_class) |
| 49 |
{ |
| 50 |
|
| 51 |
remove_action('woocommerce_created_customer_notification', array($email_class->emails['WC_Email_Customer_New_Account'], 'trigger')); |
| 52 |
} |
| 53 |
|
| 54 |
public function newAccountMail($user_id, $user_pass = '', $password_generated = false) |
| 55 |
{ |
| 56 |
|
| 57 |
|
| 58 |
$query = $this->db_query_class; |
| 59 |
$email = get_option('admin_email'); |
| 60 |
|
| 61 |
if ($user_id) { |
| 62 |
$this->object = new WP_User($user_id); |
| 63 |
$this->user_pass = $user_pass; |
| 64 |
$this->user_login = stripslashes($this->object->user_login); |
| 65 |
$this->user_email = stripslashes($this->object->user_email); |
| 66 |
$this->recipient = $this->user_email; |
| 67 |
$this->password_generated = $password_generated; |
| 68 |
$this->set_password_url = $this->generate_set_password_url(); |
| 69 |
} |
| 70 |
if (isset($query->posts[0])) { |
| 71 |
$html = get_post_meta($query->posts[0]->ID, 'emailkit_template_content_html', true); |
| 72 |
$tbody = substr($html, strpos($html, '<tbody')); |
| 73 |
$row = strpos($tbody, '</tbody>'); |
| 74 |
$rows = ''; |
| 75 |
$html = str_replace($row, $rows, $html); |
| 76 |
|
| 77 |
|
| 78 |
$details = [ |
| 79 |
"{{user_login}}" => $this->user_login, |
| 80 |
"{{user_email}}" => $this->user_email, |
| 81 |
"{{set_password_url}}" => $this->set_password_url, |
| 82 |
"{{app_name}}" => get_bloginfo('name'), // Get the blog name |
| 83 |
"{{site_url}}" => get_site_url(), // Get the site URL |
| 84 |
]; |
| 85 |
|
| 86 |
$message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html)); |
| 87 |
|
| 88 |
$to = $this->user_email; |
| 89 |
$subject = str_replace(array_keys($details), array_values($details), get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true)); |
| 90 |
$subject = !empty($subject) ? $subject : esc_html__("Your account has been created on ", 'emailkit') ." ". esc_attr(get_bloginfo('name')); |
| 91 |
|
| 92 |
$pre_header = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true); |
| 93 |
$pre_header = !empty($pre_header) ? $pre_header : esc_html__("Your account has been created on ", 'emailkit') ." ". esc_attr(get_bloginfo('name')); |
| 94 |
|
| 95 |
$headers = [ |
| 96 |
'From: ' . $email . "\r\n", |
| 97 |
'Reply-To: ' . $email . "\r\n", |
| 98 |
'Content-Type: text/html; charset=UTF-8', |
| 99 |
'X-WPMAIL-PREHEADER: ' . $pre_header, |
| 100 |
]; |
| 101 |
|
| 102 |
wp_mail($to, $subject, $message, $headers); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
protected function generate_set_password_url() |
| 107 |
{ |
| 108 |
// Generate a magic link so user can set initial password. |
| 109 |
$key = get_password_reset_key($this->object); |
| 110 |
if (!is_wp_error($key)) { |
| 111 |
$action = 'newaccount'; |
| 112 |
return wc_get_account_endpoint_url('lost-password') . "?action=$action&key=$key&login=" . rawurlencode($this->object->user_login); |
| 113 |
} else { |
| 114 |
// Something went wrong while getting the key for new password URL, send customer to the generic password reset. |
| 115 |
return wc_get_account_endpoint_url('lost-password'); |
| 116 |
} |
| 117 |
} |
| 118 |
} |