PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.0.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.0.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 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Traits / Email.php

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

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