Passed
Pull Request — master (#408)
by Brian
05:34
created

GetPaid_Invoice_Notification_Emails::new_invoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Contains the notification emails management class.
4
 *
5
 */
6
7
use function SimplePay\Core\Payments\Payment_Confirmation\get_content;
0 ignored issues
show
introduced by
The function SimplePay\Core\Payments\...onfirmation\get_content was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * This class handles invoice notificaiton emails.
13
 *
14
 */
15
class GetPaid_Invoice_Notification_Emails {
16
17
    /**
18
	 * The array of invoice email actions.
19
	 * 
20
	 * @param array
21
	 */
22
	public $invoice_actions;
23
24
    /**
25
	 * Class constructor
26
     * 
27
	 */
28
	public function __construct() {
29
30
		$this->invoice_actions = apply_filters(
31
			'getpaid_notification_email_invoice_triggers',
32
			array(
33
				'getpaid_new_invoice'                   => 'new_invoice',
34
				'getpaid_invoice_status_wpi-cancelled'  => 'cancelled_invoice',
35
				'getpaid_invoice_status_wpi-failed'     => 'failed_invoice',
36
				'getpaid_invoice_status_wpi-onhold'     => 'onhold_invoice',
37
				'getpaid_invoice_status_wpi-processing' => 'processing_invoice',
38
				'getpaid_invoice_status_publish'        => 'completed_invoice',
39
				'getpaid_invoice_status_wpi-renewal'    => 'completed_invoice',
40
				'getpaid_invoice_status_wpi-refunded'   => 'refunded_invoice',
41
				'getpaid_new_invoice'                   => 'user_invoice',
42
				'getpaid_new_customer_note'             => 'user_note',
43
				'getpaid_subscriptions_daily_cron'      => 'overdue',
44
				
45
			)
46
		);
47
		
48
    }
49
    
50
    /**
51
	 * Registers email hooks.
52
	 */
53
	public function init_hooks() {
54
55
		add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 3 );
56
		add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 );
57
		foreach ( $this->invoice_actions as $hook => $email_type ) {
58
59
			$email = new GetPaid_Notification_Email( $email_type );
60
61
			if ( $email->is_active() && method_exists( $this, $email_type ) ) {
62
				add_action( $hook, array( $this, $email_type ), 10, 2 );
63
			} else {
64
				do_action( 'getpaid_hook_invoice_notification_email_invoice_trigger', $email );
65
			}
66
67
		}
68
69
	}
70
71
	/**
72
	 * Filters invoice merge tags.
73
	 * 
74
	 * @param array $merge_tags
75
	 * @param string $email_type
76
	 * @param mixed|WPInv_Invoice|WPInv_Subscription $object
77
	 */
78
	public function invoice_merge_tags( $merge_tags, $email_type, $object ) {
79
80
		if ( is_a( $object, 'WPInv_Invoice' ) ) {
81
			$merge_tags = array_merge(
82
				$merge_tags,
83
				$this->get_invoice_merge_tags( $object )
84
			);
85
		}
86
87
		if ( is_a( $object, 'WPInv_Subscription' ) ) {
88
			$merge_tags = array_merge(
89
				$merge_tags,
90
				$this->get_invoice_merge_tags( $object->get_parent_payment() )
91
			);
92
		}
93
94
		return apply_filters( 'getpaid_invoice_notification_merge_tags', $merge_tags, $object, $email_type, $this );
95
96
	}
97
98
	/**
99
	 * Generates invoice merge tags.
100
	 * 
101
	 * @param WPInv_Invoice $invoice
102
	 * @return array
103
	 */
104
	public function get_invoice_merge_tags( $invoice ) {
105
106
		// Abort if it does not exist.
107
		if ( ! $invoice->get_id() ) {
108
			return array();
109
		}
110
111
		return array(
112
			'{name}'                => sanitize_text_field( $invoice->get_user_full_name() ),
113
			'{full_name}'           => sanitize_text_field( $invoice->get_user_full_name() ),
114
			'{first_name}'          => sanitize_text_field( $invoice->get_first_name() ),
115
			'{last_name}'           => sanitize_text_field( $invoice->get_last_name() ),
116
			'{email}'               => sanitize_email( $invoice->get_email() ),
117
			'{invoice_number}'      => sanitize_text_field( $invoice->get_number() ),
118
			'{invoice_total}'       => wpinv_price( wpinv_format_amount( $invoice->get_total() ) ),
119
			'{invoice_link}'        => esc_url( $invoice->get_view_url() ),
120
			'{invoice_pay_link}'    => esc_url( $invoice->get_checkout_payment_url() ),
121
			'{invoice_receipt_link}'=> esc_url( $invoice->get_receipt_url() ),
122
			'{invoice_date}'        => date( get_option( 'date_format' ), strtotime( $invoice->get_date_created(), current_time( 'timestamp' ) ) ),
0 ignored issues
show
Bug introduced by
It seems like get_option('date_format') can also be of type false; however, parameter $format of date() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

122
			'{invoice_date}'        => date( /** @scrutinizer ignore-type */ get_option( 'date_format' ), strtotime( $invoice->get_date_created(), current_time( 'timestamp' ) ) ),
Loading history...
123
			'{invoice_due_date}'    => date( get_option( 'date_format' ), strtotime( $invoice->get_due_date(), current_time( 'timestamp' ) ) ),
124
			'{invoice_quote}'       => sanitize_text_field( $invoice->get_type() ),
125
			'{invoice_label}'       => sanitize_text_field( ucfirst( $invoice->get_type() ) ),
126
			'{invoice_description}' => wp_kses_post( $invoice->get_description() ),
127
			'{subscription_name}'   => wp_kses_post( $invoice->get_subscription_name() ),
128
			'{is_was}'              => strtotime( $invoice->get_due_date() ) < current_time( 'timestamp' ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ),
129
		);
130
131
	}
132
133
	/**
134
	 * Helper function to send an email.
135
	 * 
136
	 * @param WPInv_Invoice $invoice
137
	 * @param GetPaid_Notification_Email $email
138
	 * @param string $type
139
	 * @param string|array $recipients
140
	 * @param array $extra_args Extra template args.
141
	 */
142
	public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) {
143
144
		do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email );
145
146
		$mailer     = new GetPaid_Notification_Email_Sender();
147
		$merge_tags = $email->get_merge_tags();
148
149
		$result = $mailer->send(
150
			apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ),
151
			$email->add_merge_tags( $email->get_subject(), $merge_tags ),
152
			$email->get_content( $merge_tags, $extra_args ),
153
			$email->get_attachments()
154
		);
155
156
		// Maybe send a copy to the admin.
157
		if ( $email->include_admin_bcc() ) {
158
			$mailer->send(
159
				wpinv_get_admin_email(),
160
				$email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ),
161
				$email->get_content( $merge_tags ),
162
				$email->get_attachments()
163
			);
164
		}
165
166
		if ( ! $result ) {
167
			$invoice->add_note( sprintf( __( 'Failed sending %s notification email.', 'invoicing' ), sanitize_key( $type ) ), false, false, true );
168
		}
169
170
		do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email );
171
172
	}
173
174
	/**
175
	 * Also send emails to any cc users.
176
	 * 
177
	 * @param array $recipients
178
	 * @param GetPaid_Notification_Email $email
179
	 */
180
	public function filter_email_recipients( $recipients, $email ) {
181
182
		if ( ! $email->is_admin_email() ) {
183
			$cc = $email->object->get_email_cc();
0 ignored issues
show
Bug introduced by
The method get_email_cc() does not exist on WPInv_Item. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
			/** @scrutinizer ignore-call */ 
184
   $cc = $email->object->get_email_cc();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method get_email_cc() does not exist on WPInv_Subscription. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

183
			/** @scrutinizer ignore-call */ 
184
   $cc = $email->object->get_email_cc();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
184
185
			if ( ! empty( $cc ) ) {
186
				$cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) );
187
				$recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) );
188
			}
189
190
		}
191
192
		return $recipients;
193
194
	}
195
196
    /**
197
	 * Sends a new invoice notification.
198
	 * 
199
	 * @param WPInv_Invoice $invoice
200
	 */
201
	public function new_invoice( $invoice ) {
202
203
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
204
		$recipient = wpinv_get_admin_email();
205
206
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
207
208
	}
209
210
	/**
211
	 * Sends a cancelled invoice notification.
212
	 * 
213
	 * @param WPInv_Invoice $invoice
214
	 */
215
	public function cancelled_invoice( $invoice ) {
216
217
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
218
		$recipient = wpinv_get_admin_email();
219
220
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
221
222
	}
223
224
	/**
225
	 * Sends a failed invoice notification.
226
	 * 
227
	 * @param WPInv_Invoice $invoice
228
	 */
229
	public function failed_invoice( $invoice ) {
230
231
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
232
		$recipient = wpinv_get_admin_email();
233
234
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
235
236
	}
237
238
	/**
239
	 * Sends a notification whenever an invoice is put on hold.
240
	 * 
241
	 * @param WPInv_Invoice $invoice
242
	 */
243
	public function onhold_invoice( $invoice ) {
244
245
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
246
		$recipient = $invoice->get_email();
247
248
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
249
250
	}
251
252
	/**
253
	 * Sends a notification whenever an invoice is marked as processing payment.
254
	 * 
255
	 * @param WPInv_Invoice $invoice
256
	 */
257
	public function processing_invoice( $invoice ) {
258
259
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
260
		$recipient = $invoice->get_email();
261
262
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
263
264
	}
265
266
	/**
267
	 * Sends a notification whenever an invoice is paid.
268
	 * 
269
	 * @param WPInv_Invoice $invoice
270
	 */
271
	public function completed_invoice( $invoice ) {
272
273
		// (Maybe) abort if it is a renewal invoice.
274
		if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) {
275
			return;
276
		}
277
278
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
279
		$recipient = $invoice->get_email();
280
281
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
282
283
	}
284
285
	/**
286
	 * Sends a notification whenever an invoice is refunded.
287
	 * 
288
	 * @param WPInv_Invoice $invoice
289
	 */
290
	public function refunded_invoice( $invoice ) {
291
292
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
293
		$recipient = $invoice->get_email();
294
295
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
296
297
	}
298
299
	/**
300
	 * Notifies a user about new invoices
301
	 * 
302
	 * @param WPInv_Invoice $invoice
303
	 */
304
	public function user_invoice( $invoice ) {
305
306
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
307
		$recipient = $invoice->get_email();
308
309
		$this->send_email( $invoice, $email, __METHOD__, $recipient );
310
311
	}
312
313
	/**
314
	 * Notifies admin about new invoice notes
315
	 * 
316
	 * @param WPInv_Invoice $invoice
317
	 * @param string $note
318
	 */
319
	public function user_note( $invoice, $note ) {
320
321
		$email     = new GetPaid_Notification_Email( __METHOD__, $invoice );
322
		$recipient = $invoice->get_email();
323
324
		$this->send_email( $invoice, $email, __METHOD__, $recipient, array( 'customer_note' => $note ) );
325
326
	}
327
328
}
329