| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Traits; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
use SpringDevs\Subscription\Illuminate\Helper; |
| 7 |
use SpringDevs\Subscription\Illuminate\Subscription\Subscription; |
| 8 |
|
| 9 |
trait Email { |
| 10 |
/** |
| 11 |
* Template list with emails. |
| 12 |
* |
| 13 |
* @var array[] |
| 14 |
*/ |
| 15 |
private $templates = array( |
| 16 |
'subscrpt_status_changed_admin_email' => array( |
| 17 |
'html' => 'emails/status-changed-admin-html.php', |
| 18 |
'plain' => 'emails/plains/status-changed-admin-plain.php', |
| 19 |
), |
| 20 |
'subscrpt_subscription_expired_email' => array( |
| 21 |
'html' => 'emails/subscription-expired-html.php', |
| 22 |
'plain' => 'emails/plains/subscription-expired-plain.php', |
| 23 |
), |
| 24 |
'subscrpt_subscription_cancelled_email' => array( |
| 25 |
'html' => 'emails/subscription-cancelled-html.php', |
| 26 |
'plain' => 'emails/plains/subscription-cancelled-plain.php', |
| 27 |
), |
| 28 |
'subscrpt_renew_reminder' => array( |
| 29 |
'html' => 'emails/renew-reminder-html.php', |
| 30 |
'plain' => 'emails/plains/renew-reminder-plain.php', |
| 31 |
), |
| 32 |
); |
| 33 |
/** |
| 34 |
* Template path. |
| 35 |
* |
| 36 |
* @var string |
| 37 |
*/ |
| 38 |
public $template_base; |
| 39 |
|
| 40 |
/** |
| 41 |
* Plain text template path. |
| 42 |
* |
| 43 |
* @var string |
| 44 |
*/ |
| 45 |
public $template_plain; |
| 46 |
|
| 47 |
/** |
| 48 |
* HTML template path. |
| 49 |
* |
| 50 |
* @var string |
| 51 |
*/ |
| 52 |
public $template_html; |
| 53 |
|
| 54 |
/** |
| 55 |
* Subscription id. |
| 56 |
* |
| 57 |
* @var int |
| 58 |
*/ |
| 59 |
public $subscription_id; |
| 60 |
|
| 61 |
/** |
| 62 |
* Product Name ( Order Item ). |
| 63 |
* |
| 64 |
* @var string |
| 65 |
*/ |
| 66 |
public $product_name; |
| 67 |
|
| 68 |
/** |
| 69 |
* Subscription's quantity. |
| 70 |
* |
| 71 |
* @var int |
| 72 |
*/ |
| 73 |
public $qty; |
| 74 |
|
| 75 |
/** |
| 76 |
* Formatted recurring amount. |
| 77 |
* |
| 78 |
* @var string |
| 79 |
*/ |
| 80 |
public $amount; |
| 81 |
|
| 82 |
/** |
| 83 |
* Contain extra data to pass template. |
| 84 |
* |
| 85 |
* @var array |
| 86 |
*/ |
| 87 |
public $extra = array(); |
| 88 |
|
| 89 |
/** |
| 90 |
* Get email heading. |
| 91 |
* |
| 92 |
* @return string |
| 93 |
* @since 3.1.0 |
| 94 |
*/ |
| 95 |
public function get_default_heading(): string { |
| 96 |
return __( 'Subscription: #{subscription_id}', 'subscription' ); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the email content in HTML format. |
| 101 |
* |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
public function get_content_html(): string { |
| 105 |
return $this->render_template( $this->template_html ); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Get the email content in Plain Format. |
| 110 |
* |
| 111 |
* @return string |
| 112 |
*/ |
| 113 |
public function get_content_plain(): string { |
| 114 |
return $this->render_template( $this->template_plain ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Render template for email. |
| 119 |
* |
| 120 |
* @param string $path Template Path. |
| 121 |
* |
| 122 |
* @return string |
| 123 |
*/ |
| 124 |
public function render_template( string $path ): string { |
| 125 |
// Build view subscription url & set it to mail extra data. |
| 126 |
$view_subs_endpoint = Subscription::get_user_endpoint( 'view_subs' ); |
| 127 |
$view_subscription_url = wc_get_endpoint_url( $view_subs_endpoint, $this->subscription_id, wc_get_page_permalink( 'myaccount' ) ); |
| 128 |
|
| 129 |
return wc_get_template_html( |
| 130 |
$path, |
| 131 |
array_merge( |
| 132 |
array( |
| 133 |
'id' => $this->subscription_id, |
| 134 |
'email_heading' => $this->get_heading(), |
| 135 |
'product_name' => $this->product_name, |
| 136 |
'qty' => $this->qty, |
| 137 |
'amount' => $this->amount, |
| 138 |
'view_subscription_url' => $view_subscription_url, |
| 139 |
), |
| 140 |
$this->extra |
| 141 |
), |
| 142 |
'subscription', |
| 143 |
$this->template_base |
| 144 |
); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Set template for mail. |
| 149 |
* |
| 150 |
* @param string $id id of email. |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function set_template( string $id ) { |
| 154 |
$this->template_base = SUBSCRPT_TEMPLATES; |
| 155 |
if ( isset( $this->templates[ $id ] ) ) { |
| 156 |
$this->template_html = $this->templates[ $id ]['html']; |
| 157 |
$this->template_plain = $this->templates[ $id ]['plain']; |
| 158 |
add_filter( 'woocommerce_template_directory', array( $this, 'filter_woocommerce_template_directory' ), 10, 2 ); |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Set data for subscription table. |
| 164 |
* |
| 165 |
* @return void |
| 166 |
*/ |
| 167 |
public function set_table_data() { |
| 168 |
$order_item_id = get_post_meta( $this->subscription_id, '_subscrpt_order_item_id', true ); |
| 169 |
try { |
| 170 |
$order_id = wc_get_order_id_by_order_item_id( $order_item_id ); |
| 171 |
} catch ( Exception $e ) { |
| 172 |
return; |
| 173 |
} |
| 174 |
$order = wc_get_order( $order_id ); |
| 175 |
if ( ! $order ) { |
| 176 |
return; |
| 177 |
} |
| 178 |
|
| 179 |
$order_item = $order->get_item( $order_item_id ); |
| 180 |
|
| 181 |
$this->product_name = $order_item->get_name(); |
| 182 |
$this->qty = $order_item->get_quantity(); |
| 183 |
$this->amount = Helper::format_price_with_order_item( get_post_meta( $this->subscription_id, '_subscrpt_price', true ), $order_item->get_id() ); |
| 184 |
} |
| 185 |
|
| 186 |
/** |
| 187 |
* Filter WooCommerce template directory. |
| 188 |
* |
| 189 |
* @param string $directory Directory name. |
| 190 |
* @param string $template Template path. |
| 191 |
* @return string |
| 192 |
*/ |
| 193 |
public function filter_woocommerce_template_directory( string $directory, string $template ): string { |
| 194 |
$email_templates = array( $this->template_plain, $this->template_html ); |
| 195 |
if ( in_array( $template, $email_templates, true ) ) { |
| 196 |
return 'subscription'; |
| 197 |
} |
| 198 |
|
| 199 |
return $directory; |
| 200 |
} |
| 201 |
} |
| 202 |
|