CustomerStockNotificationEmail.php
4 weeks ago
CustomerStockNotificationVerifiedEmail.php
4 weeks ago
CustomerStockNotificationVerifyEmail.php
4 weeks ago
EmailActionController.php
4 weeks ago
EmailManager.php
4 weeks ago
EmailTemplatesController.php
10 months ago
EmailTemplatesController.php
141 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 | |
| 9 | /** |
| 10 | * Email templates controller. |
| 11 | */ |
| 12 | class EmailTemplatesController { |
| 13 | |
| 14 | /** |
| 15 | * Initialize the class. |
| 16 | * |
| 17 | * @internal |
| 18 | * |
| 19 | * @return void |
| 20 | */ |
| 21 | final public function init() { |
| 22 | add_action( 'init', array( $this, 'register_template_hooks' ) ); |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Add template hooks. |
| 27 | * |
| 28 | * @internal |
| 29 | */ |
| 30 | public function register_template_hooks() { |
| 31 | add_action( 'woocommerce_email_stock_notification_product', array( $this, 'email_product_image' ), 10, 3 ); |
| 32 | add_action( 'woocommerce_email_stock_notification_product', array( $this, 'email_product_title' ), 20, 3 ); |
| 33 | add_action( 'woocommerce_email_stock_notification_product', array( $this, 'email_product_attributes' ), 30, 3 ); |
| 34 | add_action( 'woocommerce_email_stock_notification_product', array( $this, 'email_product_price' ), 40, 3 ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Email product image. |
| 39 | * |
| 40 | * @param WC_Product $product The product object. |
| 41 | * @param Notification $notification The notification object. |
| 42 | * @param bool $plain_text Whether the email is plain text. |
| 43 | */ |
| 44 | public function email_product_image( $product, $notification, $plain_text = false ) { |
| 45 | if ( $plain_text ) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | $image = wp_get_attachment_image_src( $product->get_image_id(), 'woocommerce_thumbnail' ); |
| 50 | $image_src = is_array( $image ) && isset( $image[0] ) ? $image[0] : ''; |
| 51 | |
| 52 | ob_start(); |
| 53 | if ( $image_src ) { ?> |
| 54 | <div id="notification__product__image"> |
| 55 | <img src="<?php echo esc_attr( $image_src ); ?>" alt="<?php echo esc_attr( $product->get_title() ); ?>" width="220"/> |
| 56 | </div> |
| 57 | <?php |
| 58 | } |
| 59 | $html = ob_get_clean(); |
| 60 | echo wp_kses_post( $html ); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Email product title. |
| 65 | * |
| 66 | * @param WC_Product $product The product object. |
| 67 | * @param Notification $notification The notification object. |
| 68 | * @param bool $plain_text Whether the email is plain text. |
| 69 | */ |
| 70 | public function email_product_title( $product, $notification, $plain_text = false ) { |
| 71 | if ( $plain_text ) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | ob_start(); |
| 76 | ?> |
| 77 | <div id="notification__product__title"><?php echo esc_html( $product->get_name() ); ?></div> |
| 78 | <?php |
| 79 | $html = ob_get_clean(); |
| 80 | echo wp_kses_post( $html ); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Email product attributes. |
| 85 | * |
| 86 | * @param WC_Product $product The product object. |
| 87 | * @param Notification $notification The notification object. |
| 88 | * @param bool $plain_text Whether the email is plain text. |
| 89 | */ |
| 90 | public function email_product_attributes( $product, $notification, $plain_text = false ) { |
| 91 | if ( $plain_text ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $formatted_variation_list = $notification->get_product_formatted_variation_list( false ); |
| 96 | if ( empty( $formatted_variation_list ) ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Convert list to HTML table for better rendering. |
| 101 | $formatted_variation_list = strtr( |
| 102 | $formatted_variation_list, |
| 103 | array( |
| 104 | '<dl' => '<table', |
| 105 | '<dd' => '<tr><th', |
| 106 | '<dt' => '<tr><td', |
| 107 | 'dl>' => 'table>', |
| 108 | 'dd>' => 'th></tr>', |
| 109 | 'dt>' => 'td></tr>', |
| 110 | ) |
| 111 | ); |
| 112 | |
| 113 | ob_start(); |
| 114 | ?> |
| 115 | <div id="notification__product__attributes"><?php echo wp_kses_post( $formatted_variation_list ); ?></div> |
| 116 | <?php |
| 117 | $html = ob_get_clean(); |
| 118 | echo wp_kses_post( $html ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Email product price. |
| 123 | * |
| 124 | * @param WC_Product $product The product object. |
| 125 | * @param Notification $notification The notification object. |
| 126 | * @param bool $plain_text Whether the email is plain text. |
| 127 | */ |
| 128 | public function email_product_price( $product, $notification, $plain_text = false ) { |
| 129 | if ( $plain_text ) { |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | ob_start(); |
| 134 | ?> |
| 135 | <div id="notification__product__price"><?php echo wp_kses_post( $product->get_price_html() ); ?></div> |
| 136 | <?php |
| 137 | $html = ob_get_clean(); |
| 138 | echo wp_kses_post( $html ); |
| 139 | } |
| 140 | } |
| 141 |