CustomerStockNotificationEmail.php
1 month ago
CustomerStockNotificationVerifiedEmail.php
1 month ago
CustomerStockNotificationVerifyEmail.php
1 month ago
EmailActionController.php
1 month ago
EmailManager.php
1 month ago
EmailTemplatesController.php
10 months ago
EmailManager.php
320 lines
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace Automattic\WooCommerce\Internal\StockNotifications\Emails; |
| 6 | |
| 7 | use Automattic\WooCommerce\Internal\StockNotifications\Notification; |
| 8 | use Automattic\WooCommerce\Internal\StockNotifications\Factory; |
| 9 | use Automattic\WooCommerce\Internal\StockNotifications\Emails\CustomerStockNotificationEmail; |
| 10 | use Automattic\WooCommerce\Internal\StockNotifications\Emails\CustomerStockNotificationVerifyEmail; |
| 11 | use Automattic\WooCommerce\Internal\StockNotifications\Emails\CustomerStockNotificationVerifiedEmail; |
| 12 | use Automattic\WooCommerce\Internal\StockNotifications\Emails\EmailTemplatesController; |
| 13 | /** |
| 14 | * Emails manager. |
| 15 | */ |
| 16 | class EmailManager { |
| 17 | |
| 18 | /** |
| 19 | * List of all core email IDs. |
| 20 | * |
| 21 | * @var array |
| 22 | */ |
| 23 | public static $email_ids = array( |
| 24 | 'customer_stock_notification', |
| 25 | 'customer_stock_notification_verify', |
| 26 | 'customer_stock_notification_verified', |
| 27 | ); |
| 28 | |
| 29 | /** |
| 30 | * Initialize the emails. |
| 31 | * |
| 32 | * @internal |
| 33 | * |
| 34 | * @return void |
| 35 | */ |
| 36 | final public function init() { |
| 37 | |
| 38 | // Setup email hooks & handlers. |
| 39 | add_filter( 'woocommerce_email_classes', array( $this, 'email_classes' ) ); |
| 40 | |
| 41 | // Add "transactional" emails. |
| 42 | add_action( 'woocommerce_email_actions', array( $this, 'add_transactional_emails' ) ); |
| 43 | |
| 44 | // Setup styles. |
| 45 | add_filter( 'woocommerce_email_styles', array( $this, 'add_stylesheets' ), 10, 2 ); |
| 46 | |
| 47 | // Preview. |
| 48 | add_filter( 'woocommerce_prepare_email_for_preview', array( $this, 'prepare_email_for_preview' ) ); |
| 49 | add_filter( 'woocommerce_email_preview_email_content_setting_ids', array( $this, 'add_intro_content_to_preview_settings' ), 10, 2 ); |
| 50 | |
| 51 | // Restore customer's context while rendering the emails. |
| 52 | add_action( 'woocommerce_email_stock_notification_product', array( $this, 'maybe_restore_customer_tax_location_data' ), 9 ); |
| 53 | |
| 54 | // Register email templates. |
| 55 | $container = wc_get_container(); |
| 56 | $container->get( EmailTemplatesController::class ); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Registers custom emails classes. |
| 61 | * |
| 62 | * @param array $emails Array of email classes. |
| 63 | * @return array |
| 64 | */ |
| 65 | public function email_classes( $emails ) { |
| 66 | $emails['WC_Email_Customer_Stock_Notification'] = new CustomerStockNotificationEmail(); |
| 67 | $emails['WC_Email_Customer_Stock_Notification_Verify'] = new CustomerStockNotificationVerifyEmail(); |
| 68 | $emails['WC_Email_Customer_Stock_Notification_Verified'] = new CustomerStockNotificationVerifiedEmail(); |
| 69 | |
| 70 | return $emails; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Adds transactional emails. |
| 75 | * |
| 76 | * Stock notifications are sent via a custom AS job. |
| 77 | * Additionally, two transactional emails are dispatched during the signup and verification processes, |
| 78 | * which need to be included in the actions array to support deferred email functionality. |
| 79 | * |
| 80 | * @hook woocommerce_defer_transactional_emails |
| 81 | * |
| 82 | * @param array $actions The list of actions. |
| 83 | * @return array |
| 84 | */ |
| 85 | public function add_transactional_emails( $actions ) { |
| 86 | if ( ! is_array( $actions ) ) { |
| 87 | return $actions; |
| 88 | } |
| 89 | |
| 90 | $actions[] = 'woocommerce_customer_stock_notification_verify'; |
| 91 | $actions[] = 'woocommerce_customer_stock_notification_verified'; |
| 92 | |
| 93 | return $actions; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Restore customer tax location data from notification's metadata |
| 98 | * to display product prices in emails using the customer's tax location, if applicable. |
| 99 | * |
| 100 | * @param Notification $notification The notification object. |
| 101 | * @return void |
| 102 | */ |
| 103 | public function maybe_restore_customer_tax_location_data( $notification ) { |
| 104 | |
| 105 | // No need if stores displaying price excluding tax. |
| 106 | if ( 'incl' !== get_option( 'woocommerce_tax_display_shop' ) ) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Check if for some reason (e.g., 3PD), a WC_Customer is already assigned into the BG process's context. |
| 111 | if ( ! empty( WC()->customer ) ) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | // Get the recorded customer data, if any. |
| 116 | $location = $notification->get_meta( '_customer_location_data' ); |
| 117 | if ( empty( $location ) || ! is_array( $location ) || 4 !== count( $location ) ) { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | // Restore the tax location. |
| 122 | add_filter( |
| 123 | 'woocommerce_get_tax_location', |
| 124 | function () use ( $location ) { |
| 125 | return $location; |
| 126 | } |
| 127 | ); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Prints CSS in the emails. |
| 132 | * |
| 133 | * @param string $css The CSS to print. |
| 134 | * @param WC_Email $email (Optional) The email object. |
| 135 | * @return string |
| 136 | */ |
| 137 | public function add_stylesheets( $css, $email = null ) { |
| 138 | |
| 139 | /** |
| 140 | * `woocommerce_email_stock_notification_emails_to_style` filter. |
| 141 | * |
| 142 | * @since 10.2.0 |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | if ( ( is_null( $email ) || ! in_array( $email->id, (array) apply_filters( 'woocommerce_email_stock_notification_emails_to_style', self::$email_ids ), true ) ) ) { |
| 147 | return $css; |
| 148 | } |
| 149 | |
| 150 | // General text. |
| 151 | $text = get_option( 'woocommerce_email_text_color' ); |
| 152 | |
| 153 | // Primary color. |
| 154 | $base = get_option( 'woocommerce_email_base_color' ); |
| 155 | |
| 156 | /** |
| 157 | * `woocommerce_email_stock_notification_base_text_color` filter. |
| 158 | * |
| 159 | * @since 10.2.0 |
| 160 | * |
| 161 | * @return string |
| 162 | */ |
| 163 | $base_text = (string) apply_filters( 'woocommerce_email_stock_notification_base_text_color', wc_light_or_dark( $base, '#202020', '#ffffff' ), $email ); |
| 164 | |
| 165 | ob_start(); |
| 166 | ?> |
| 167 | #header_wrapper h1 { |
| 168 | line-height: 1em !important; |
| 169 | } |
| 170 | #notification__container { |
| 171 | color: <?php echo esc_attr( $text ); ?> !important; |
| 172 | padding: 20px 20px; |
| 173 | text-align: center; |
| 174 | font-family: "Helvetica Neue", Helvetica, Roboto, Arial, sans-serif; |
| 175 | width: 100%; |
| 176 | } |
| 177 | #notification__into_content { |
| 178 | margin-bottom: 48px; |
| 179 | color: <?php echo esc_attr( $text ); ?> !important; |
| 180 | } |
| 181 | #notification__product__image { |
| 182 | text-align: center; |
| 183 | margin-bottom: 20px; |
| 184 | width: 100%; |
| 185 | } |
| 186 | #notification__product__image img { |
| 187 | margin-right: 0; |
| 188 | width: 220px; |
| 189 | } |
| 190 | #notification__product__title { |
| 191 | font-size: 16px; |
| 192 | font-weight: bold; |
| 193 | line-height: 130%; |
| 194 | margin-bottom: 5px; |
| 195 | color: <?php echo esc_attr( $text ); ?> !important; |
| 196 | } |
| 197 | #notification__product__attributes table { |
| 198 | width: 100%; |
| 199 | padding: 0; |
| 200 | margin: 0; |
| 201 | color: <?php echo esc_attr( $text ); ?> !important; |
| 202 | } |
| 203 | #notification__product__attributes th, |
| 204 | #notification__product__attributes td { |
| 205 | color: <?php echo esc_attr( $text ); ?> !important; |
| 206 | padding: 4px !important; |
| 207 | text-align: center; |
| 208 | } |
| 209 | #notification__product__price { |
| 210 | margin-bottom: 20px; |
| 211 | color: <?php echo esc_attr( $text ); ?> !important; |
| 212 | } |
| 213 | #notification__action_button { |
| 214 | text-decoration: none; |
| 215 | display: inline-block; |
| 216 | background: <?php echo esc_attr( $base ); ?>; |
| 217 | color: <?php echo esc_attr( $base_text ); ?> !important; |
| 218 | border: 10px solid <?php echo esc_attr( $base ); ?>; |
| 219 | } |
| 220 | #notification__verification_expiration { |
| 221 | font-size: 0.8em; |
| 222 | margin-top: 20px; |
| 223 | color: <?php echo esc_attr( $text ); ?>; |
| 224 | } |
| 225 | #notification__footer { |
| 226 | text-align: center; |
| 227 | margin-top: 20px; |
| 228 | color: <?php echo esc_attr( $text ); ?>; |
| 229 | } |
| 230 | #notification__unsubscribe_link { |
| 231 | color: <?php echo esc_attr( $text ); ?>; |
| 232 | } |
| 233 | #notification__product__price .screen-reader-text { |
| 234 | display: none; |
| 235 | } |
| 236 | <?php |
| 237 | $css .= ob_get_clean(); |
| 238 | |
| 239 | return $css; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Register intro_content email fields to be watched by WooCommerce's live email preview. |
| 244 | * |
| 245 | * @param array $setting_ids The email content setting IDs. |
| 246 | * @param string $email_id The email ID. |
| 247 | * @return array |
| 248 | */ |
| 249 | public function add_intro_content_to_preview_settings( $setting_ids, $email_id ) { |
| 250 | |
| 251 | if ( in_array( $email_id, self::$email_ids, true ) ) { |
| 252 | $setting_ids[] = "woocommerce_{$email_id}_intro_content"; |
| 253 | } |
| 254 | |
| 255 | return $setting_ids; |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * Prepares the email for preview. |
| 260 | * |
| 261 | * @param \WC_Email $email The email object being previewed. |
| 262 | * @return \WC_Email |
| 263 | */ |
| 264 | public function prepare_email_for_preview( $email ) { |
| 265 | if ( ! in_array( $email->id, self::$email_ids, true ) ) { |
| 266 | return $email; |
| 267 | } |
| 268 | |
| 269 | $notification = Factory::create_dummy_notification(); |
| 270 | $email->prepare_email( $notification ); |
| 271 | |
| 272 | return $email; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Send a stock notification email. |
| 277 | * |
| 278 | * @param Notification $notification The notification object. |
| 279 | * @return void |
| 280 | */ |
| 281 | public function send_stock_notification_email( Notification $notification ) { |
| 282 | $emails = WC()->mailer()->get_emails(); |
| 283 | $email = $emails['WC_Email_Customer_Stock_Notification'] ?? null; |
| 284 | |
| 285 | if ( $email instanceof CustomerStockNotificationEmail ) { |
| 286 | $email->trigger( $notification ); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Send the double opt-in verification email for a stock notification. |
| 292 | * |
| 293 | * @param Notification $notification The notification object. |
| 294 | * @return void |
| 295 | */ |
| 296 | public function send_verify_email( Notification $notification ) { |
| 297 | $emails = WC()->mailer()->get_emails(); |
| 298 | $email = $emails['WC_Email_Customer_Stock_Notification_Verify'] ?? null; |
| 299 | |
| 300 | if ( $email instanceof CustomerStockNotificationVerifyEmail ) { |
| 301 | $email->trigger( $notification ); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Send the sign-up confirmation email for a stock notification. |
| 307 | * |
| 308 | * @param Notification $notification The notification object. |
| 309 | * @return void |
| 310 | */ |
| 311 | public function send_verified_email( Notification $notification ) { |
| 312 | $emails = WC()->mailer()->get_emails(); |
| 313 | $email = $emails['WC_Email_Customer_Stock_Notification_Verified'] ?? null; |
| 314 | |
| 315 | if ( $email instanceof CustomerStockNotificationVerifiedEmail ) { |
| 316 | $email->trigger( $notification ); |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 |