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