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