Completed
Push — master ( e1fcb8...b01a52 )
by Brian
26s queued 17s
created

init_hooks()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 17
rs 9.9666
cc 4
nc 4
nop 0
1
<?php
2
/**
3
 * Contains the subscriptions notification emails management class.
4
 *
5
 */
6
7
defined( 'ABSPATH' ) || exit;
8
9
/**
10
 * This class handles subscription notificaiton emails.
11
 *
12
 */
13
class GetPaid_Subscription_Notification_Emails {
14
15
    /**
16
	 * The array of subscription email actions.
17
	 *
18
	 * @param array
19
	 */
20
	public $subscription_actions;
21
22
    /**
23
	 * Class constructor
24
     *
25
	 */
26
	public function __construct() {
27
28
		$this->subscription_actions = apply_filters(
29
			'getpaid_notification_email_subscription_triggers',
30
			array(
31
				'getpaid_subscription_trialling' => 'subscription_trial',
32
				'getpaid_subscription_cancelled' => 'subscription_cancelled',
33
				'getpaid_subscription_expired'   => 'subscription_expired',
34
				'getpaid_subscription_completed' => 'subscription_complete',
35
				'getpaid_daily_maintenance'      => 'renewal_reminder',
36
			)
37
		);
38
39
		$this->init_hooks();
40
41
    }
42
43
    /**
44
	 * Registers email hooks.
45
	 */
46
	public function init_hooks() {
47
48
		add_filter( 'getpaid_get_email_merge_tags', array( $this, 'subscription_merge_tags' ), 10, 2 );
49
		foreach ( $this->subscription_actions as $hook => $email_type ) {
50
51
			$email = new GetPaid_Notification_Email( $email_type );
52
53
			if ( ! $email->is_active() ) {
54
				continue;
55
			}
56
57
			if ( method_exists( $this, $email_type ) ) {
58
				add_action( $hook, array( $this, $email_type ), 100, 2 );
59
				continue;
60
			}
61
62
			do_action( 'getpaid_subscription_notification_email_register_hook', $email );
63
64
		}
65
66
	}
67
68
	/**
69
	 * Filters subscription merge tags.
70
	 *
71
	 * @param array $merge_tags
72
	 * @param mixed|WPInv_Invoice|WPInv_Subscription $object
73
	 */
74
	public function subscription_merge_tags( $merge_tags, $object ) {
75
76
		if ( is_a( $object, 'WPInv_Subscription' ) ) {
77
			$merge_tags = array_merge(
78
				$merge_tags,
79
				$this->get_subscription_merge_tags( $object )
80
			);
81
		}
82
83
		return $merge_tags;
84
85
	}
86
87
	/**
88
	 * Generates subscription merge tags.
89
	 *
90
	 * @param WPInv_Subscription $subscription
91
	 * @return array
92
	 */
93
	public function get_subscription_merge_tags( $subscription ) {
94
95
		// Abort if it does not exist.
96
		if ( ! $subscription->get_id() ) {
97
			return array();
98
		}
99
100
		$invoice    = $subscription->get_parent_invoice();
101
		return array(
102
			'{subscription_renewal_date}'     => getpaid_format_date_value( $subscription->get_next_renewal_date(), __( 'Never', 'invoicing' ) ),
103
			'{subscription_created}'          => getpaid_format_date_value( $subscription->get_date_created() ),
104
			'{subscription_status}'           => sanitize_text_field( $subscription->get_status_label() ),
105
			'{subscription_profile_id}'       => sanitize_text_field( $subscription->get_profile_id() ),
106
			'{subscription_id}'               => absint( $subscription->get_id() ),
107
			'{subscription_recurring_amount}' => wpinv_price( wpinv_format_amount( $subscription->get_recurring_amount() ), $invoice->get_currency() ),
108
			'{subscription_initial_amount}'   => wpinv_price( wpinv_format_amount( $subscription->get_initial_amount() ), $invoice->get_currency() ),
109
			'{subscription_recurring_period}' => strtolower( sanitize_text_field( WPInv_Subscriptions::wpinv_get_pretty_subscription_frequency( $subscription->get_period(), $subscription->get_frequency(), true ) ) ),
110
			'{subscription_bill_times}'       => $subscription->get_bill_times(),
111
			'{subscription_url}'              => esc_url( $subscription->get_view_url() ),
112
		);
113
114
	}
115
116
	/**
117
	 * Checks if we should send a notification for a subscription.
118
	 *
119
	 * @param WPInv_Invoice $invoice
120
	 * @return bool
121
	 */
122
	public function should_send_notification( $invoice ) {
123
		return 0 != $invoice->get_id();
124
	}
125
126
	/**
127
	 * Returns notification recipients.
128
	 *
129
	 * @param WPInv_Invoice $invoice
130
	 * @return array
131
	 */
132
	public function get_recipients( $invoice ) {
133
		$recipients = array( $invoice->get_email() );
134
135
		$cc = $invoice->get_email_cc();
136
137
		if ( ! empty( $cc ) ) {
138
			$cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) );
139
			$recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) );
140
		}
141
142
		return $recipients;
143
	}
144
145
	/**
146
	 * Helper function to send an email.
147
	 *
148
	 * @param WPInv_Subscription $subscription
149
	 * @param GetPaid_Notification_Email $email
150
	 * @param string $type
151
	 * @param array $extra_args Extra template args.
152
	 */
153
	public function send_email( $subscription, $email, $type, $extra_args = array() ) {
154
155
		// Abort in case the parent invoice does not exist.
156
		$invoice = $subscription->get_parent_invoice();
157
		if ( ! $this->should_send_notification( $invoice ) ) {
158
			return;
159
		}
160
161
		do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email );
162
163
		$recipients  = $this->get_recipients( $invoice );
164
		$mailer      = new GetPaid_Notification_Email_Sender();
165
		$merge_tags  = $email->get_merge_tags();
166
		$content     = $email->get_content( $merge_tags, $extra_args );
167
		$subject     = $email->add_merge_tags( $email->get_subject(), $merge_tags );
168
		$attachments = $email->get_attachments();
169
170
		$result = $mailer->send(
171
			apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ),
172
			$subject,
173
			$content,
174
			$attachments
175
		);
176
177
		// Maybe send a copy to the admin.
178
		if ( $email->include_admin_bcc() ) {
179
			$mailer->send(
180
				wpinv_get_admin_email(),
181
				$subject . __( ' - ADMIN BCC COPY', 'invoicing' ),
182
				$content,
183
				$attachments
184
			);
185
		}
186
187
		if ( ! $result ) {
188
			$subscription->get_parent_invoice()->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true );
189
		}
190
191
		do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email );
192
193
	}
194
195
    /**
196
	 * Sends a new trial notification.
197
	 *
198
	 * @param WPInv_Subscription $subscription
199
	 */
200
	public function subscription_trial( $subscription ) {
201
202
		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
203
		$this->send_email( $subscription, $email, __FUNCTION__ );
204
205
	}
206
207
	/**
208
	 * Sends a cancelled subscription notification.
209
	 *
210
	 * @param WPInv_Subscription $subscription
211
	 */
212
	public function subscription_cancelled( $subscription ) {
213
214
		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
215
		$this->send_email( $subscription, $email, __FUNCTION__ );
216
217
	}
218
219
	/**
220
	 * Sends a subscription expired notification.
221
	 *
222
	 * @param WPInv_Subscription $subscription
223
	 */
224
	public function subscription_expired( $subscription ) {
225
226
		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
227
		$this->send_email( $subscription, $email, __FUNCTION__ );
228
229
	}
230
231
	/**
232
	 * Sends a completed subscription notification.
233
	 *
234
	 * @param WPInv_Subscription $subscription
235
	 */
236
	public function subscription_complete( $subscription ) {
237
238
		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
239
		$this->send_email( $subscription, $email, __FUNCTION__ );
240
241
	}
242
243
	/**
244
	 * Sends a subscription renewal reminder notification.
245
	 *
246
	 */
247
	public function renewal_reminder() {
248
249
		$email = new GetPaid_Notification_Email( __FUNCTION__ );
250
251
		// Fetch reminder days.
252
		$reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) );
253
254
		// Abort if non is set.
255
		if ( empty( $reminder_days ) ) {
256
			return;
257
		}
258
259
		// Fetch matching subscriptions.
260
        $args  = array(
261
            'number'             => -1,
262
			'count_total'        => false,
263
			'status'             => 'trialling active',
264
            'date_expires_query' => array(
265
				'relation'  => 'OR'
266
            ),
267
		);
268
269
		foreach ( $reminder_days as $days ) {
270
			$date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) );
271
272
			$args['date_expires_query'][] = array(
273
				'year'  => $date['year'],
274
				'month' => $date['month'],
275
				'day'   => $date['day'],
276
			);
277
278
		}
279
280
		$subscriptions = new GetPaid_Subscriptions_Query( $args );
281
282
        foreach ( $subscriptions as $subscription ) {
283
284
			// Skip packages.
285
			if ( get_post_meta( $subscription->get_product_id(), '_wpinv_type', true ) != 'package' ) {
286
				$email->object = $subscription;
287
            	$this->send_email( $subscription, $email, __FUNCTION__ );
288
			}
289
290
		}
291
292
	}
293
294
}
295