CustomerVerifyEmail.php
183 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\CustomerEmailVerification\Emails; |
| 6 | |
| 7 | use WC_Customer; |
| 8 | use WC_Email; |
| 9 | use WP_User; |
| 10 | |
| 11 | defined( 'ABSPATH' ) || exit; |
| 12 | |
| 13 | /** |
| 14 | * Customer email verification email. |
| 15 | * |
| 16 | * Sent to a customer with a link to confirm they own their account email address. |
| 17 | * |
| 18 | * @since 11.0.0 |
| 19 | */ |
| 20 | class CustomerVerifyEmail extends WC_Email { |
| 21 | |
| 22 | /** |
| 23 | * One-time verification URL included in the email. |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | public $verify_url; |
| 28 | |
| 29 | /** |
| 30 | * Display name used to greet the customer. |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | public $user_display_name; |
| 35 | |
| 36 | /** |
| 37 | * Customer email address. Populated by the email preview ({@see EmailPreview}) for the editor. |
| 38 | * |
| 39 | * @var string |
| 40 | */ |
| 41 | public $user_email; |
| 42 | |
| 43 | /** |
| 44 | * Customer login. Populated by the email preview ({@see EmailPreview}) for the editor. |
| 45 | * |
| 46 | * @var string |
| 47 | */ |
| 48 | public $user_login; |
| 49 | |
| 50 | /** |
| 51 | * Constructor. |
| 52 | */ |
| 53 | public function __construct() { |
| 54 | $this->id = 'customer_verify_email'; |
| 55 | $this->customer_email = true; |
| 56 | $this->title = __( 'Confirm email address', 'woocommerce' ); |
| 57 | $this->description = __( 'Sent to customers with a link to confirm they own their account email address.', 'woocommerce' ); |
| 58 | $this->template_html = 'emails/customer-verify-email.php'; |
| 59 | $this->template_plain = 'emails/plain/customer-verify-email.php'; |
| 60 | $this->email_group = 'accounts'; |
| 61 | |
| 62 | // Trigger. |
| 63 | add_action( 'woocommerce_customer_verify_email_notification', array( $this, 'trigger' ), 10, 2 ); |
| 64 | |
| 65 | // Call parent constructor. |
| 66 | parent::__construct(); |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Get email subject. |
| 71 | * |
| 72 | * @return string |
| 73 | */ |
| 74 | public function get_default_subject() { |
| 75 | return __( 'Confirm your email address for {site_title}', 'woocommerce' ); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Get email heading. |
| 80 | * |
| 81 | * @return string |
| 82 | */ |
| 83 | public function get_default_heading() { |
| 84 | return __( 'Confirm your email address', 'woocommerce' ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Default content to show below the main email content. |
| 89 | * |
| 90 | * @return string |
| 91 | */ |
| 92 | public function get_default_additional_content() { |
| 93 | return __( 'Thanks for reading.', 'woocommerce' ); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Trigger the sending of this email. |
| 98 | * |
| 99 | * @param int $user_id The user ID to send the email to. |
| 100 | * @param string $verify_url The one-time verification URL. |
| 101 | * @return void |
| 102 | */ |
| 103 | public function trigger( $user_id, $verify_url = '' ) { |
| 104 | $this->setup_locale(); |
| 105 | |
| 106 | if ( $user_id && $verify_url ) { |
| 107 | $this->object = new WP_User( $user_id ); |
| 108 | $this->verify_url = $verify_url; |
| 109 | $this->recipient = wp_unslash( $this->object->user_email ); |
| 110 | $customer = new WC_Customer( $user_id ); |
| 111 | $first_name = ! empty( $customer->get_billing_first_name() ) ? $customer->get_billing_first_name() : $this->object->first_name; |
| 112 | $this->user_display_name = ! empty( $first_name ) ? $first_name : $this->object->user_login; |
| 113 | |
| 114 | $this->send_notification(); |
| 115 | } |
| 116 | |
| 117 | $this->restore_locale(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Get content html. |
| 122 | * |
| 123 | * @return string |
| 124 | */ |
| 125 | public function get_content_html() { |
| 126 | return wc_get_template_html( |
| 127 | $this->template_html, |
| 128 | array( |
| 129 | 'email_heading' => $this->get_heading(), |
| 130 | 'additional_content' => $this->get_additional_content(), |
| 131 | 'user_display_name' => $this->user_display_name, |
| 132 | 'user_email' => $this->object instanceof WP_User ? $this->object->user_email : '', |
| 133 | 'verify_url' => $this->verify_url, |
| 134 | 'blogname' => $this->get_blogname(), |
| 135 | 'sent_to_admin' => false, |
| 136 | 'plain_text' => false, |
| 137 | 'email' => $this, |
| 138 | ) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get content plain. |
| 144 | * |
| 145 | * @return string |
| 146 | */ |
| 147 | public function get_content_plain() { |
| 148 | return wc_get_template_html( |
| 149 | $this->template_plain, |
| 150 | array( |
| 151 | 'email_heading' => $this->get_heading(), |
| 152 | 'additional_content' => $this->get_additional_content(), |
| 153 | 'user_display_name' => $this->user_display_name, |
| 154 | 'user_email' => $this->object instanceof WP_User ? $this->object->user_email : '', |
| 155 | 'verify_url' => $this->verify_url, |
| 156 | 'blogname' => $this->get_blogname(), |
| 157 | 'sent_to_admin' => false, |
| 158 | 'plain_text' => true, |
| 159 | 'email' => $this, |
| 160 | ) |
| 161 | ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get the content rendered into the block email editor's WooCommerce content placeholder. |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | public function get_block_editor_email_template_content() { |
| 170 | return wc_get_template_html( |
| 171 | $this->template_block_content, |
| 172 | array( |
| 173 | 'user_display_name' => $this->user_display_name, |
| 174 | 'user_email' => $this->object instanceof WP_User ? $this->object->user_email : '', |
| 175 | 'verify_url' => $this->verify_url, |
| 176 | 'sent_to_admin' => false, |
| 177 | 'plain_text' => false, |
| 178 | 'email' => $this, |
| 179 | ) |
| 180 | ); |
| 181 | } |
| 182 | } |
| 183 |