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 / ResetPassword.php

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

121 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 EmailKit\Admin\Emails\EmailLists;
7 use EmailKit\Admin\Emails\Helpers\Utils;
8
9 defined("ABSPATH") || exit;
10
11 class ResetPassword
12 {
13
14 public $user_id;
15 public $user_login;
16 public $user_email;
17 public $reset_key;
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' => EmailLists::RESET_PASSWORD,
30 ),
31 array(
32 'key' => 'emailkit_template_status',
33 'value' => 'Active',
34 ),
35 ),
36 );
37
38 $this->db_query_class = new WP_Query($args);
39
40 if (isset($this->db_query_class->posts[0])) {
41 add_action('woocommerce_email', [$this, 'remove_woocommerce_emails']);
42 }
43
44 add_filter('woocommerce_reset_password_notification', [$this, 'passwordReset'], 10, 2);
45 }
46
47 public function remove_woocommerce_emails($email_class)
48 {
49 remove_action('woocommerce_reset_password_notification', array($email_class->emails['WC_Email_Customer_Reset_Password'], 'trigger'));
50 }
51
52 public function passwordReset($user_login, $reset_key)
53 {
54
55
56 $query = $this->db_query_class;
57 $email = get_option('admin_email');
58
59 if ($user_login && $reset_key) {
60 $this->object = get_user_by('login', $user_login);
61 $this->user_id = $this->object->ID;
62 $this->user_login = $user_login;
63 $this->reset_key = $reset_key;
64 $this->user_email = stripslashes($this->object->user_email);
65 }
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}}" => $user_login,
77 "{{user_email}}" => $this->user_email,
78 "{{reset_url}}" => $this->generate_set_password_url(),
79 "{{site_name}}" => get_bloginfo('name'),
80 "{{site_url}}" => get_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 $to = $this->user_email;
86
87 $pre_header_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_preheader', true);
88 $pre_header = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $pre_header_template);
89 $pre_header = !empty($pre_header) ? $pre_header : esc_html__( "password reset requested", "emailkit");
90 $subject_template = get_post_meta($query->posts[0]->ID, 'emailkit_email_subject', true);
91 $subject = str_replace(array_keys(Utils::transform_details_keys($details)), array_values(Utils::transform_details_keys($details)), $subject_template);
92 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : esc_html__("Hi ", "emailkit") . esc_attr($user_login) ." ". esc_html__( "Your password reset requested on ", "emailkit") . "" . esc_attr(get_bloginfo('name')) . ' - ' . $pre_header;
93
94
95 $headers = [
96 'From: ' . $email . "\r\n",
97 'Reply-To: ' . $email . "\r\n",
98 'Content-Type: text/html; charset=UTF-8',
99 ];
100
101 wp_mail($to, $subject, $message, $headers);
102 }
103 }
104
105 protected function generate_set_password_url()
106 {
107 // Generate a magic link so user can set initial password.
108 $key = get_password_reset_key($this->object);
109
110 if (!is_wp_error($key)) {
111 $action = 'newaccount';
112 $reset_url = wc_get_account_endpoint_url('lost-password') . "?action=$action&key=$key&login=" . rawurlencode($this->object->user_login) . "&show-reset-form=true";
113
114 return $reset_url;
115 } else {
116 // Something went wrong while getting the key for a new password URL,
117 // send the customer to the generic password reset.
118 return wc_get_account_endpoint_url('lost-password') . '?show-reset-form=true&action';
119 }
120 }
121 }