PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.2.0
EmailKit – Email Customizer for WooCommerce & WP v1.2.0
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Emails / Woocommerce / NewAccount.php

NewAccount.php in EmailKit – Email Customizer for WooCommerce & WP 1.2.0, at includes/Admin/Emails/Woocommerce/NewAccount.php

110 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 ];
83
84 $message = str_replace(array_keys($details), array_values($details), apply_filters('emailkit_shortcode_filter', $html));
85
86 $to = get_option('admin_email');
87 $subject = "Your account has been created on " . esc_attr(get_bloginfo('name'));
88 $headers = [
89 'From: ' . $email . "\r\n",
90 'Reply-To: ' . $email . "\r\n",
91 'Content-Type: text/html; charset=UTF-8',
92 ];
93
94 wp_mail($to, $subject, $message, $headers);
95 }
96 }
97
98 protected function generate_set_password_url()
99 {
100 // Generate a magic link so user can set initial password.
101 $key = get_password_reset_key($this->object);
102 if (!is_wp_error($key)) {
103 $action = 'newaccount';
104 return wc_get_account_endpoint_url('lost-password') . "?action=$action&key=$key&login=" . rawurlencode($this->object->user_login);
105 } else {
106 // Something went wrong while getting the key for new password URL, send customer to the generic password reset.
107 return wc_get_account_endpoint_url('lost-password');
108 }
109 }
110 }