Base_Email.php
2 years ago
WC_Square_Access_Token_Email.php
1 year ago
WC_Square_Gift_Card_Sent.php
1 year ago
WC_Square_Sync_Completed.php
1 year ago
WC_Square_Gift_Card_Sent.php
307 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly. |
| 5 | } |
| 6 | |
| 7 | /** |
| 8 | * Gift card sent to recipient email. |
| 9 | * |
| 10 | * An email sent to the recipient of the gift card. |
| 11 | * |
| 12 | * @class WC_Square_Gift_Card_Sent |
| 13 | * @version 4.2.0 |
| 14 | * @extends WC_Email |
| 15 | */ |
| 16 | class WC_Square_Gift_Card_Sent extends \WC_Email { |
| 17 | /** |
| 18 | * Constructor. |
| 19 | */ |
| 20 | public function __construct() { |
| 21 | $this->id = 'wc_square_gift_card_sent'; |
| 22 | $this->title = __( 'Square Gift Card Sent', 'woocommerce-square' ); |
| 23 | $this->customer_email = true; |
| 24 | $this->description = __( 'This email is sent to the recipient of the Square Gift Card.', 'woocommerce-square' ); |
| 25 | $this->template_html = 'emails/gift-card-sent.php'; |
| 26 | $this->template_plain = 'emails/plain/gift-card-sent.php'; |
| 27 | $this->placeholders = array( |
| 28 | '{square_gift_card_sender_name}' => '', |
| 29 | '{square_gift_card_number}' => '', |
| 30 | '{square_gift_card_balance}' => '', |
| 31 | '{square_gift_card_recipient_name}' => '', |
| 32 | '{square_gift_card_sender_message}' => '', |
| 33 | ); |
| 34 | |
| 35 | // Call parent constructor. |
| 36 | parent::__construct(); |
| 37 | |
| 38 | add_filter( 'woocommerce_email_preview_placeholders', array( $this, 'set_dummy_placeholders' ), 10, 2 ); |
| 39 | add_filter( 'woocommerce_email_preview_dummy_order', array( $this, 'set_dummy_order' ), 10, 2 ); |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Set dummy placeholders for the email. |
| 44 | * |
| 45 | * @since 4.9.0 |
| 46 | * |
| 47 | * @param array $placeholders Placeholders. |
| 48 | * @param string $email_type Email type. |
| 49 | * @return array |
| 50 | */ |
| 51 | public function set_dummy_placeholders( $placeholders, $email_type ) { |
| 52 | if ( 'WC_Square_Gift_Card_Sent' !== $email_type ) { |
| 53 | return $placeholders; |
| 54 | } |
| 55 | |
| 56 | $placeholders['{square_gift_card_sender_name}'] = 'Kelly'; |
| 57 | |
| 58 | return $placeholders; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Set dummy order for the email. |
| 63 | * |
| 64 | * @since 4.9.0 |
| 65 | * |
| 66 | * @param \WC_Order $order The WooCommerce order. |
| 67 | * @param string $email_type Email type. |
| 68 | * @return \WC_Order |
| 69 | */ |
| 70 | public function set_dummy_order( \WC_Order $order, $email_type ) { |
| 71 | if ( 'WC_Square_Gift_Card_Sent' !== $email_type ) { |
| 72 | return $order; |
| 73 | } |
| 74 | |
| 75 | wc_square()->get_gateway()->update_order_meta( $order, 'gift_card_balance', '20' ); |
| 76 | wc_square()->get_gateway()->update_order_meta( $order, 'gift_card_number', '7783320804609121' ); |
| 77 | |
| 78 | $items = $order->get_items(); |
| 79 | |
| 80 | foreach ( $items as $item ) { |
| 81 | $item->update_meta_data( '_square-gift-card-purchase-type', 'new' ); |
| 82 | $item->update_meta_data( 'square-gift-card-sender-name', 'Kelly' ); |
| 83 | $item->update_meta_data( 'square-gift-card-sent-to-message', 'Happy Birthday John!' ); |
| 84 | $item->update_meta_data( 'square-gift-card-send-to-email', 'kelly@example.com' ); |
| 85 | $item->update_meta_data( 'square-gift-card-sent-to-first-name', 'John' ); |
| 86 | $item->save(); |
| 87 | } |
| 88 | |
| 89 | return $order; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get email subject. |
| 94 | * |
| 95 | * @since 4.2.0 |
| 96 | * |
| 97 | * @return string |
| 98 | */ |
| 99 | public function get_default_subject() { |
| 100 | return sprintf( |
| 101 | /* translators: %1$s - Store name */ |
| 102 | __( '{square_gift_card_sender_name} sent you a %1$s Gift Card!', 'woocommerce-square' ), |
| 103 | esc_html( get_bloginfo( 'name' ) ) |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Get email heading. |
| 109 | * |
| 110 | * @since 4.2.0 |
| 111 | * |
| 112 | * @return string |
| 113 | */ |
| 114 | public function get_default_heading() { |
| 115 | return sprintf( |
| 116 | /* translators: %1$s - Store name */ |
| 117 | __( '%1$s Gift Card received!', 'woocommerce-square' ), |
| 118 | esc_html( get_bloginfo( 'name' ) ) |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Trigger for the email. |
| 124 | * |
| 125 | * @since 4.2.0 |
| 126 | * |
| 127 | * @param \WC_Order $order The WooCommerce order. |
| 128 | */ |
| 129 | public function trigger( $order ) { |
| 130 | $this->setup_locale(); |
| 131 | |
| 132 | if ( ! $this->is_enabled() ) { |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | if ( ! $this->does_order_contain_gift_card( $order ) ) { |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | $this->object = wc_get_order( $order ); |
| 141 | $items = $order->get_items(); |
| 142 | |
| 143 | /** @var WC_Order_Item_Product $item */ |
| 144 | foreach ( $items as $item ) { |
| 145 | if ( 'new' !== $item->get_meta( '_square-gift-card-purchase-type' ) ) { |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | $recipient_email = $item->get_meta( 'square-gift-card-send-to-email' ); |
| 150 | |
| 151 | if ( ! $recipient_email ) { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | $this->recipient = $recipient_email; |
| 156 | $this->placeholders['{square_gift_card_sender_name}'] = $item->get_meta( 'square-gift-card-sender-name' ) ? $item->get_meta( 'square-gift-card-sender-name' ) : $order->get_billing_first_name(); |
| 157 | $this->placeholders['{square_gift_card_recipient_name}'] = $item->get_meta( 'square-gift-card-sent-to-first-name' ); |
| 158 | $this->placeholders['{square_gift_card_sender_message}'] = $item->get_meta( 'square-gift-card-sent-to-message' ); |
| 159 | $this->placeholders['{square_gift_card_number}'] = $this->get_gift_card_gan( $order ); |
| 160 | $this->placeholders['{square_gift_card_balance}'] = $this->get_gift_card_amount( $order ); |
| 161 | |
| 162 | $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 163 | } |
| 164 | |
| 165 | $this->restore_locale(); |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns the number of the gift card purchased. |
| 170 | * |
| 171 | * @since 4.2.0 |
| 172 | * |
| 173 | * @param \WC_Order $order The Woo Order associated with the gift card purchase. |
| 174 | * @return string |
| 175 | */ |
| 176 | private function get_gift_card_gan( $order ) { |
| 177 | return wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'gift_card_number' ); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the amount loaded in the purchased gift card. |
| 182 | * |
| 183 | * @since 4.2.0 |
| 184 | * |
| 185 | * @param \WC_Order $order The Woo Order associated with the gift card purchase. |
| 186 | * @return string |
| 187 | */ |
| 188 | private function get_gift_card_amount( $order ) { |
| 189 | return wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'gift_card_balance' ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns the details of the gift card purchased. |
| 194 | * |
| 195 | * @since 4.9.0 |
| 196 | * |
| 197 | * @param \WC_Order $order The Woo Order associated with the gift card purchase. |
| 198 | * @param string $key The key of the gift card detail to retrieve. |
| 199 | * @return string |
| 200 | */ |
| 201 | private function get_gift_card_details( $order, $key = '' ) { |
| 202 | if ( empty( $key ) ) { |
| 203 | return ''; |
| 204 | } |
| 205 | |
| 206 | $items = $order->get_items(); |
| 207 | $result = array(); |
| 208 | |
| 209 | foreach ( $items as $item ) { |
| 210 | if ( 'new' !== $item->get_meta( '_square-gift-card-purchase-type' ) ) { |
| 211 | continue; |
| 212 | } |
| 213 | |
| 214 | $recipient_email = $item->get_meta( 'square-gift-card-send-to-email' ); |
| 215 | |
| 216 | if ( ! $recipient_email ) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | $result['sender_name'] = $item->get_meta( 'square-gift-card-sender-name' ); |
| 221 | $result['sender_message'] = $item->get_meta( 'square-gift-card-sent-to-message' ); |
| 222 | $result['recipient_name'] = $item->get_meta( 'square-gift-card-sent-to-first-name' ); |
| 223 | $result['gift_card_number'] = wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'gift_card_number' ); |
| 224 | $result['gift_card_balance'] = wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'gift_card_balance' ); |
| 225 | } |
| 226 | |
| 227 | return $result[ $key ] ?? ''; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Check if order contains a gift card. |
| 232 | * |
| 233 | * @return boolean |
| 234 | */ |
| 235 | private function does_order_contain_gift_card( $order ) { |
| 236 | return 'yes' === wc_square()->get_gateway( $order->get_payment_method() )->get_order_meta( $order, 'is_gift_card_purchased' ); |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Get content html. |
| 241 | * |
| 242 | * @since 4.2.0 |
| 243 | * |
| 244 | * @return string |
| 245 | */ |
| 246 | public function get_content_html() { |
| 247 | return wc_get_template_html( |
| 248 | $this->template_html, |
| 249 | array( |
| 250 | 'order' => $this->object, |
| 251 | 'gift_card_number' => $this->get_gift_card_details( $this->object, 'gift_card_number' ), |
| 252 | 'gift_card_balance' => $this->get_gift_card_details( $this->object, 'gift_card_balance' ), |
| 253 | 'gift_card_sender_message' => $this->get_gift_card_details( $this->object, 'sender_message' ), |
| 254 | 'gift_card_sender_name' => $this->get_gift_card_details( $this->object, 'sender_name' ), |
| 255 | 'gift_card_recipient_name' => $this->get_gift_card_details( $this->object, 'recipient_name' ), |
| 256 | 'email_heading' => $this->get_heading(), |
| 257 | 'additional_content' => $this->get_additional_content(), |
| 258 | 'sent_to_admin' => false, |
| 259 | 'plain_text' => false, |
| 260 | 'email' => $this, |
| 261 | ) |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Get content plain. |
| 267 | * |
| 268 | * @since 4.2.0 |
| 269 | * |
| 270 | * @return string |
| 271 | */ |
| 272 | public function get_content_plain() { |
| 273 | return wc_get_template_html( |
| 274 | $this->template_plain, |
| 275 | array( |
| 276 | 'order' => $this->object, |
| 277 | 'gift_card_number' => $this->get_gift_card_details( $this->object, 'gift_card_number' ), |
| 278 | 'gift_card_balance' => $this->get_gift_card_details( $this->object, 'gift_card_balance' ), |
| 279 | 'gift_card_sender_message' => $this->get_gift_card_details( $this->object, 'sender_message' ), |
| 280 | 'gift_card_sender_name' => $this->get_gift_card_details( $this->object, 'sender_name' ), |
| 281 | 'gift_card_recipient_name' => $this->get_gift_card_details( $this->object, 'recipient_name' ), |
| 282 | 'email_heading' => $this->get_heading(), |
| 283 | 'additional_content' => $this->get_additional_content(), |
| 284 | 'sent_to_admin' => false, |
| 285 | 'plain_text' => true, |
| 286 | 'email' => $this, |
| 287 | ) |
| 288 | ); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Default content to show below main email content. |
| 293 | * |
| 294 | * @since 4.2.0 |
| 295 | * |
| 296 | * @return string |
| 297 | */ |
| 298 | public function get_default_additional_content() { |
| 299 | return sprintf( |
| 300 | '%1$s <a href="%2$s">%3$s</a>', |
| 301 | esc_html__( 'We look forward to seeing you soon at', 'woocommerce-square' ), |
| 302 | esc_url( get_site_url() ), |
| 303 | esc_url( get_site_url() ) |
| 304 | ); |
| 305 | } |
| 306 | } |
| 307 |