PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.5.2
EmailKit – Email Customizer for WooCommerce & WP v1.5.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.5.2, at includes/Admin/Emails/Woocommerce/ResetPassword.php

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