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