Passed
Push — master ( 618fc1...d0e164 )
by Brian
05:18
created
includes/class-getpaid-subscription-notification-emails.php 1 patch
Indentation   +257 added lines, -257 removed lines patch added patch discarded remove patch
@@ -13,300 +13,300 @@
 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_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();
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 40
 
41 41
     }
42 42
 
43 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_type, $hook );
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}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ),
108
-			'{subscription_initial_amount}'   => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ),
109
-			'{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ),
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
-		if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) {
162
-			return;
163
-		}
164
-
165
-		do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email );
166
-
167
-		$recipients  = $this->get_recipients( $invoice );
168
-		$mailer      = new GetPaid_Notification_Email_Sender();
169
-		$merge_tags  = $email->get_merge_tags();
170
-		$content     = $email->get_content( $merge_tags, $extra_args );
171
-		$subject     = $email->add_merge_tags( $email->get_subject(), $merge_tags );
172
-		$attachments = $email->get_attachments();
173
-
174
-		$result = $mailer->send(
175
-			apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ),
176
-			$subject,
177
-			$content,
178
-			$attachments
179
-		);
180
-
181
-		// Maybe send a copy to the admin.
182
-		if ( $email->include_admin_bcc() ) {
183
-			$mailer->send(
184
-				wpinv_get_admin_email(),
185
-				$subject . __( ' - ADMIN BCC COPY', 'invoicing' ),
186
-				$content,
187
-				$attachments
188
-			);
189
-		}
190
-
191
-		if ( $result ) {
192
-			$invoice->add_system_note(
193
-				sprintf(
194
-					__( 'Successfully sent %s notification email to %s.', 'invoicing' ),
195
-					sanitize_key( $type ),
196
-					$email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
197
-				)
198
-			);
199
-		} else {
200
-			$invoice->add_system_note(
201
-				sprintf(
202
-					__( 'Failed sending %s notification email to %s.', 'invoicing' ),
203
-					sanitize_key( $type ),
204
-					$email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
205
-				)
206
-			);	
207
-		}
208
-
209
-		do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email );
210
-
211
-	}
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_type, $hook );
63
+
64
+        }
65
+
66
+    }
212 67
 
213 68
     /**
214
-	 * Sends a new trial notification.
215
-	 *
216
-	 * @param WPInv_Subscription $subscription
217
-	 */
218
-	public function subscription_trial( $subscription ) {
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 ) {
219 75
 
220
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
221
-		$this->send_email( $subscription, $email, __FUNCTION__ );
76
+        if ( is_a( $object, 'WPInv_Subscription' ) ) {
77
+            $merge_tags = array_merge(
78
+                $merge_tags,
79
+                $this->get_subscription_merge_tags( $object )
80
+            );
81
+        }
222 82
 
223
-	}
83
+        return $merge_tags;
224 84
 
225
-	/**
226
-	 * Sends a cancelled subscription notification.
227
-	 *
228
-	 * @param WPInv_Subscription $subscription
229
-	 */
230
-	public function subscription_cancelled( $subscription ) {
85
+    }
231 86
 
232
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
233
-		$this->send_email( $subscription, $email, __FUNCTION__ );
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}' => sanitize_text_field( wpinv_price( $subscription->get_recurring_amount(), $invoice->get_currency() ) ),
108
+            '{subscription_initial_amount}'   => sanitize_text_field( wpinv_price( $subscription->get_initial_amount(), $invoice->get_currency() ) ),
109
+            '{subscription_recurring_period}' => getpaid_get_subscription_period_label( $subscription->get_period(), $subscription->get_frequency(), '' ),
110
+            '{subscription_bill_times}'       => $subscription->get_bill_times(),
111
+            '{subscription_url}'              => esc_url( $subscription->get_view_url() ),
112
+        );
234 113
 
235
-	}
114
+    }
236 115
 
237
-	/**
238
-	 * Sends a subscription expired notification.
239
-	 *
240
-	 * @param WPInv_Subscription $subscription
241
-	 */
242
-	public function subscription_expired( $subscription ) {
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
+    }
243 125
 
244
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
245
-		$this->send_email( $subscription, $email, __FUNCTION__ );
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() );
246 134
 
247
-	}
135
+        $cc = $invoice->get_email_cc();
248 136
 
249
-	/**
250
-	 * Sends a completed subscription notification.
251
-	 *
252
-	 * @param WPInv_Subscription $subscription
253
-	 */
254
-	public function subscription_complete( $subscription ) {
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
+        }
255 141
 
256
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
257
-		$this->send_email( $subscription, $email, __FUNCTION__ );
142
+        return $recipients;
143
+    }
258 144
 
259
-	}
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
+        if ( apply_filters( 'getpaid_skip_subscription_email', false, $type, $subscription ) ) {
162
+            return;
163
+        }
164
+
165
+        do_action( 'getpaid_before_send_subscription_notification', $type, $subscription, $email );
166
+
167
+        $recipients  = $this->get_recipients( $invoice );
168
+        $mailer      = new GetPaid_Notification_Email_Sender();
169
+        $merge_tags  = $email->get_merge_tags();
170
+        $content     = $email->get_content( $merge_tags, $extra_args );
171
+        $subject     = $email->add_merge_tags( $email->get_subject(), $merge_tags );
172
+        $attachments = $email->get_attachments();
173
+
174
+        $result = $mailer->send(
175
+            apply_filters( 'getpaid_subscription_email_recipients', wpinv_parse_list( $recipients ), $email ),
176
+            $subject,
177
+            $content,
178
+            $attachments
179
+        );
180
+
181
+        // Maybe send a copy to the admin.
182
+        if ( $email->include_admin_bcc() ) {
183
+            $mailer->send(
184
+                wpinv_get_admin_email(),
185
+                $subject . __( ' - ADMIN BCC COPY', 'invoicing' ),
186
+                $content,
187
+                $attachments
188
+            );
189
+        }
190
+
191
+        if ( $result ) {
192
+            $invoice->add_system_note(
193
+                sprintf(
194
+                    __( 'Successfully sent %s notification email to %s.', 'invoicing' ),
195
+                    sanitize_key( $type ),
196
+                    $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
197
+                )
198
+            );
199
+        } else {
200
+            $invoice->add_system_note(
201
+                sprintf(
202
+                    __( 'Failed sending %s notification email to %s.', 'invoicing' ),
203
+                    sanitize_key( $type ),
204
+                    $email->is_admin_email() ? __( 'admin' ) : __( 'the customer' )
205
+                )
206
+            );	
207
+        }
208
+
209
+        do_action( 'getpaid_after_send_subscription_notification', $type, $subscription, $email );
260 210
 
261
-	/**
262
-	 * Sends a subscription renewal reminder notification.
263
-	 *
264
-	 */
265
-	public function renewal_reminder() {
211
+    }
266 212
 
267
-		$email = new GetPaid_Notification_Email( __FUNCTION__ );
213
+    /**
214
+     * Sends a new trial notification.
215
+     *
216
+     * @param WPInv_Subscription $subscription
217
+     */
218
+    public function subscription_trial( $subscription ) {
268 219
 
269
-		// Fetch reminder days.
270
-		$reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) );
220
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
221
+        $this->send_email( $subscription, $email, __FUNCTION__ );
271 222
 
272
-		// Abort if non is set.
273
-		if ( empty( $reminder_days ) ) {
274
-			return;
275
-		}
223
+    }
276 224
 
277
-		// Fetch matching subscriptions.
225
+    /**
226
+     * Sends a cancelled subscription notification.
227
+     *
228
+     * @param WPInv_Subscription $subscription
229
+     */
230
+    public function subscription_cancelled( $subscription ) {
231
+
232
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
233
+        $this->send_email( $subscription, $email, __FUNCTION__ );
234
+
235
+    }
236
+
237
+    /**
238
+     * Sends a subscription expired notification.
239
+     *
240
+     * @param WPInv_Subscription $subscription
241
+     */
242
+    public function subscription_expired( $subscription ) {
243
+
244
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
245
+        $this->send_email( $subscription, $email, __FUNCTION__ );
246
+
247
+    }
248
+
249
+    /**
250
+     * Sends a completed subscription notification.
251
+     *
252
+     * @param WPInv_Subscription $subscription
253
+     */
254
+    public function subscription_complete( $subscription ) {
255
+
256
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $subscription );
257
+        $this->send_email( $subscription, $email, __FUNCTION__ );
258
+
259
+    }
260
+
261
+    /**
262
+     * Sends a subscription renewal reminder notification.
263
+     *
264
+     */
265
+    public function renewal_reminder() {
266
+
267
+        $email = new GetPaid_Notification_Email( __FUNCTION__ );
268
+
269
+        // Fetch reminder days.
270
+        $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) );
271
+
272
+        // Abort if non is set.
273
+        if ( empty( $reminder_days ) ) {
274
+            return;
275
+        }
276
+
277
+        // Fetch matching subscriptions.
278 278
         $args  = array(
279 279
             'number'             => -1,
280
-			'count_total'        => false,
281
-			'status'             => 'trialling active',
280
+            'count_total'        => false,
281
+            'status'             => 'trialling active',
282 282
             'date_expires_query' => array(
283
-				'relation'  => 'OR'
283
+                'relation'  => 'OR'
284 284
             ),
285
-		);
285
+        );
286 286
 
287
-		foreach ( $reminder_days as $days ) {
288
-			$date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) );
287
+        foreach ( $reminder_days as $days ) {
288
+            $date = date_parse( date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) ) );
289 289
 
290
-			$args['date_expires_query'][] = array(
291
-				'year'  => $date['year'],
292
-				'month' => $date['month'],
293
-				'day'   => $date['day'],
294
-			);
290
+            $args['date_expires_query'][] = array(
291
+                'year'  => $date['year'],
292
+                'month' => $date['month'],
293
+                'day'   => $date['day'],
294
+            );
295 295
 
296
-		}
296
+        }
297 297
 
298
-		$subscriptions = new GetPaid_Subscriptions_Query( $args );
298
+        $subscriptions = new GetPaid_Subscriptions_Query( $args );
299 299
 
300 300
         foreach ( $subscriptions as $subscription ) {
301 301
 
302
-			// Skip packages.
303
-			if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) {
304
-				$email->object = $subscription;
305
-            	$this->send_email( $subscription, $email, __FUNCTION__ );
306
-			}
302
+            // Skip packages.
303
+            if ( apply_filters( 'getpaid_send_subscription_renewal_reminder_email', true ) ) {
304
+                $email->object = $subscription;
305
+                $this->send_email( $subscription, $email, __FUNCTION__ );
306
+            }
307 307
 
308
-		}
308
+        }
309 309
 
310
-	}
310
+    }
311 311
 
312 312
 }
Please login to merge, or discard this patch.