PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.4.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.4.0
2.4.0 2.3.0 2.2.0 2.1.0 2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 All 65 releases
subscription / includes / Traits / Email.php

Email.php in Subscriptions for WooCommerce with Stripe Recurring Payments 2.4.0, at includes/Traits/Email.php

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