PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 1.5.0
Subscriptions for WooCommerce with Stripe Recurring Payments v1.5.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 1.5.0, at includes/Traits/Email.php

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