PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.2
EmailKit – Email Customizer for WooCommerce & WP v1.6.2
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.6.2, at includes/Admin/Emails/Woocommerce/NewAccount.php

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