| 1 |
<?php |
| 2 |
|
| 3 |
namespace Better_Payment\Lite\WooCommerce\Emails; |
| 4 |
|
| 5 |
use Better_Payment\Lite\WooCommerce\Emails; |
| 6 |
use Better_Payment\Lite\WooCommerce\Subscriptions; |
| 7 |
|
| 8 |
/** |
| 9 |
* Exit if accessed directly |
| 10 |
*/ |
| 11 |
if ( ! defined( 'ABSPATH' ) ) { |
| 12 |
exit; |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Shared base for the Better Payment subscription lifecycle emails |
| 17 |
* — template plumbing, the notification-hook trigger, and the template |
| 18 |
* data both emails render. |
| 19 |
* |
| 20 |
* Subclasses set $id / $title / $description and the default |
| 21 |
* subject/heading copy, then call this constructor. Everything else — |
| 22 |
* enable toggle, subject, heading, additional content, HTML/plain type — |
| 23 |
* is the stock WC_Email settings UI under WooCommerce → Settings → Emails. |
| 24 |
* |
| 25 |
* This class extends WC_Email, so it must ONLY be loaded from the |
| 26 |
* `woocommerce_email_classes` filter (see Emails::register_email_classes()); |
| 27 |
* the rest of the module never references it. |
| 28 |
* |
| 29 |
* @since 2.4.0 |
| 30 |
*/ |
| 31 |
abstract class SubscriptionEmail extends \WC_Email { |
| 32 |
|
| 33 |
/** |
| 34 |
* Bind template paths and the notification hook. Subclasses have |
| 35 |
* already set $this->id when this runs. |
| 36 |
*/ |
| 37 |
public function __construct() { |
| 38 |
$this->customer_email = true; |
| 39 |
$this->template_base = Emails::template_base(); |
| 40 |
$this->template_html = Emails::template_html( $this->id ); |
| 41 |
$this->template_plain = Emails::template_plain( $this->id ); |
| 42 |
$this->placeholders = array( |
| 43 |
'{order_number}' => '', |
| 44 |
); |
| 45 |
|
| 46 |
add_action( $this->notification_hook(), array( $this, 'trigger' ) ); |
| 47 |
|
| 48 |
parent::__construct(); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* The notification hook Emails::notify() fires for this email. |
| 53 |
* |
| 54 |
* @return string |
| 55 |
*/ |
| 56 |
abstract protected function notification_hook(); |
| 57 |
|
| 58 |
/** |
| 59 |
* Send the email for a subscription order. The lifecycle guards |
| 60 |
* (delayed send, status re-check) already ran in the Emails registrar — |
| 61 |
* this only checks the WC-level toggles (enabled + recipient). |
| 62 |
* |
| 63 |
* @param mixed $order_id Parent (subscription) order id. |
| 64 |
* @return void |
| 65 |
*/ |
| 66 |
public function trigger( $order_id ) { |
| 67 |
$this->setup_locale(); |
| 68 |
|
| 69 |
$order = function_exists( 'wc_get_order' ) ? wc_get_order( absint( $order_id ) ) : false; |
| 70 |
|
| 71 |
if ( $order instanceof \WC_Order ) { |
| 72 |
$this->object = $order; |
| 73 |
$this->recipient = $order->get_billing_email(); |
| 74 |
$this->placeholders['{order_number}'] = $order->get_order_number(); |
| 75 |
} |
| 76 |
|
| 77 |
if ( $this->object instanceof \WC_Order && $this->is_enabled() && $this->get_recipient() ) { |
| 78 |
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); |
| 79 |
} |
| 80 |
|
| 81 |
$this->restore_locale(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get the HTML email content. |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
public function get_content_html() { |
| 90 |
return wc_get_template_html( $this->template_html, $this->template_args( false ), 'better-payment', $this->template_base ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get the plain-text email content. |
| 95 |
* |
| 96 |
* @return string |
| 97 |
*/ |
| 98 |
public function get_content_plain() { |
| 99 |
return wc_get_template_html( $this->template_plain, $this->template_args( true ), 'better-payment', $this->template_base ); |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* The data both templates receive. Schedule + item names come from the |
| 104 |
* parent order's activation snapshot, so a later product edit never |
| 105 |
* changes what the email reports. |
| 106 |
* |
| 107 |
* @param bool $plain_text Rendering the plain-text variant. |
| 108 |
* @return array |
| 109 |
*/ |
| 110 |
protected function template_args( $plain_text ) { |
| 111 |
$order = $this->object; |
| 112 |
|
| 113 |
$item_names = array(); |
| 114 |
foreach ( Subscriptions::order_subscription_items( $order ) as $item ) { |
| 115 |
$item_names[] = $item->get_name(); |
| 116 |
} |
| 117 |
|
| 118 |
$schedule = ''; |
| 119 |
if ( $order instanceof \WC_Order ) { |
| 120 |
$interval = (int) $order->get_meta( Subscriptions::INTERVAL_META ); |
| 121 |
if ( $interval > 0 ) { |
| 122 |
$schedule = Subscriptions::describe_schedule( $interval, $order->get_meta( Subscriptions::PERIOD_META ) ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return array( |
| 127 |
'order' => $order, |
| 128 |
'item_names' => $item_names, |
| 129 |
'schedule' => $schedule, |
| 130 |
'view_url' => Emails::subscription_view_url( $order ), |
| 131 |
'email_heading' => $this->get_heading(), |
| 132 |
'additional_content' => $this->get_additional_content(), |
| 133 |
'sent_to_admin' => false, |
| 134 |
'plain_text' => (bool) $plain_text, |
| 135 |
'email' => $this, |
| 136 |
); |
| 137 |
} |
| 138 |
} |
| 139 |
|