Access_Token_Email.php
2 years ago
Base_Email.php
2 years ago
Gift_Card_Sent.php
2 years ago
Sync_Completed.php
2 years ago
Gift_Card_Sent.php
226 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WooCommerce\Square\Emails; |
| 4 | |
| 5 | if ( ! defined( 'ABSPATH' ) ) { |
| 6 | exit; // Exit if accessed directly. |
| 7 | } |
| 8 | |
| 9 | /** |
| 10 | * Gift card sent to recipient email. |
| 11 | * |
| 12 | * An email sent to the recipient of the gift card. |
| 13 | * |
| 14 | * @class Gift_Card_Sent |
| 15 | * @version 4.2.0 |
| 16 | * @extends WC_Email |
| 17 | */ |
| 18 | class Gift_Card_Sent extends \WC_Email { |
| 19 | /** |
| 20 | * Convenience object to retrieve email data |
| 21 | * related to gift card. |
| 22 | * |
| 23 | * @var null|object |
| 24 | */ |
| 25 | public $gift_card_email_data = null; |
| 26 | |
| 27 | /** |
| 28 | * Constructor. |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | $this->id = 'wc_square_gift_card_sent'; |
| 32 | $this->title = __( 'Square Gift Card sent', 'woocommerce-square' ); |
| 33 | $this->customer_email = true; |
| 34 | $this->description = __( 'This email is sent to the recipient of the Square Gift Card.', 'woocommerce-square' ); |
| 35 | $this->template_html = 'emails/gift-card-sent.php'; |
| 36 | $this->template_plain = 'emails/plain/gift-card-sent.php'; |
| 37 | $this->placeholders = array( |
| 38 | '{square_gift_card_sender_name}' => '', |
| 39 | '{square_gift_card_number}' => '', |
| 40 | '{square_gift_card_balance}' => '', |
| 41 | '{square_gift_card_recipient_name}' => '', |
| 42 | '{square_gift_card_sender_message}' => '', |
| 43 | ); |
| 44 | |
| 45 | $this->gift_card_email_data = new \stdClass(); |
| 46 | |
| 47 | // Call parent constructor. |
| 48 | parent::__construct(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get email subject. |
| 53 | * |
| 54 | * @since 4.2.0 |
| 55 | * |
| 56 | * @return string |
| 57 | */ |
| 58 | public function get_default_subject() { |
| 59 | return sprintf( |
| 60 | /* translators: %1$s - Store name */ |
| 61 | __( '{square_gift_card_sender_name} sent you a %1$s Gift Card!', 'woocommerce-square' ), |
| 62 | esc_html( get_bloginfo( 'name' ) ) |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Get email heading. |
| 68 | * |
| 69 | * @since 4.2.0 |
| 70 | * |
| 71 | * @return string |
| 72 | */ |
| 73 | public function get_default_heading() { |
| 74 | return sprintf( |
| 75 | /* translators: %1$s - Store name */ |
| 76 | __( '%1$s Gift Card received!', 'woocommerce-square' ), |
| 77 | esc_html( get_bloginfo( 'name' ) ) |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Trigger for the email. |
| 83 | * |
| 84 | * @since 4.2.0 |
| 85 | * |
| 86 | * @param \WC_Order $order The WooCommerce order. |
| 87 | */ |
| 88 | public function trigger( $order ) { |
| 89 | $this->setup_locale(); |
| 90 | |
| 91 | if ( ! $this->is_enabled() ) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | if ( ! $this->does_order_contain_gift_card( $order ) ) { |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | $this->object = wc_get_order( $order ); |
| 100 | $items = $order->get_items(); |
| 101 | |
| 102 | /** @var WC_Order_Item_Product $item */ |
| 103 | foreach ( $items as $item ) { |
| 104 | if ( 'new' !== $item->get_meta( '_square-gift-card-purchase-type' ) ) { |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | $recipient_email = $item->get_meta( 'square-gift-card-send-to-email' ); |
| 109 | |
| 110 | if ( ! $recipient_email ) { |
| 111 | continue; |
| 112 | } |
| 113 | |
| 114 | $this->gift_card_email_data->sender_name = $item->get_meta( 'square-gift-card-sender-name' ); |
| 115 | $this->gift_card_email_data->recipient_name = $item->get_meta( 'square-gift-card-sent-to-first-name' ); |
| 116 | $this->gift_card_email_data->sender_message = $item->get_meta( 'square-gift-card-sent-to-message' ); |
| 117 | |
| 118 | $this->recipient = $recipient_email; |
| 119 | $this->placeholders['{square_gift_card_sender_name}'] = $this->gift_card_email_data->sender_name ? $this->gift_card_email_data->sender_name : $order->get_billing_first_name(); |
| 120 | $this->placeholders['{square_gift_card_recipient_name}'] = $this->gift_card_email_data->recipient_name; |
| 121 | $this->placeholders['{square_gift_card_sender_message}'] = $this->gift_card_email_data->sender_message; |
| 122 | $this->placeholders['{square_gift_card_number}'] = $this->get_gift_card_gan( $order ); |
| 123 | $this->placeholders['{square_gift_card_balance}'] = $this->get_gift_card_amount( $order ); |
| 124 | |
| 125 | $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 126 | } |
| 127 | |
| 128 | $this->restore_locale(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the number of the gift card purchased. |
| 133 | * |
| 134 | * @since 4.2.0 |
| 135 | * |
| 136 | * @param \WC_Order $order The Woo Order associated with the gift card purchase. |
| 137 | * @return string |
| 138 | */ |
| 139 | private function get_gift_card_gan( $order ) { |
| 140 | return wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'gift_card_number' ); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Returns the amount loaded in the purchased gift card. |
| 145 | * |
| 146 | * @since 4.2.0 |
| 147 | * |
| 148 | * @param \WC_Order $order The Woo Order associated with the gift card purchase. |
| 149 | * @return string |
| 150 | */ |
| 151 | private function get_gift_card_amount( $order ) { |
| 152 | return wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'gift_card_balance' ); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Says if a Gift card was purchased in the order. |
| 157 | * |
| 158 | * @return boolean |
| 159 | */ |
| 160 | private function does_order_contain_gift_card( $order ) { |
| 161 | return 'yes' === wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'is_gift_card_purchased' ); |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Get content html. |
| 166 | * |
| 167 | * @since 4.2.0 |
| 168 | * |
| 169 | * @return string |
| 170 | */ |
| 171 | public function get_content_html() { |
| 172 | return wc_get_template_html( |
| 173 | $this->template_html, |
| 174 | array( |
| 175 | 'order' => $this->object, |
| 176 | 'gift_card_number' => $this->get_gift_card_gan( $this->object ), |
| 177 | 'gift_card_balance' => $this->get_gift_card_amount( $this->object ), |
| 178 | 'email_heading' => $this->get_heading(), |
| 179 | 'additional_content' => $this->get_additional_content(), |
| 180 | 'sent_to_admin' => false, |
| 181 | 'plain_text' => false, |
| 182 | 'email' => $this, |
| 183 | ) |
| 184 | ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Get content plain. |
| 189 | * |
| 190 | * @since 4.2.0 |
| 191 | * |
| 192 | * @return string |
| 193 | */ |
| 194 | public function get_content_plain() { |
| 195 | return wc_get_template_html( |
| 196 | $this->template_plain, |
| 197 | array( |
| 198 | 'order' => $this->object, |
| 199 | 'gift_card_number' => $this->get_gift_card_gan( $this->object ), |
| 200 | 'gift_card_balance' => $this->get_gift_card_amount( $this->object ), |
| 201 | 'email_heading' => $this->get_heading(), |
| 202 | 'additional_content' => $this->get_additional_content(), |
| 203 | 'sent_to_admin' => false, |
| 204 | 'plain_text' => true, |
| 205 | 'email' => $this, |
| 206 | ) |
| 207 | ); |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Default content to show below main email content. |
| 212 | * |
| 213 | * @since 4.2.0 |
| 214 | * |
| 215 | * @return string |
| 216 | */ |
| 217 | public function get_default_additional_content() { |
| 218 | return sprintf( |
| 219 | '%1$s <a href="%2$s">%3$s</a>', |
| 220 | esc_html__( 'We look forward to seeing you soon at', 'woocommerce-square' ), |
| 221 | esc_url( get_site_url() ), |
| 222 | esc_url( get_site_url() ) |
| 223 | ); |
| 224 | } |
| 225 | } |
| 226 |