Passed
Pull Request — master (#351)
by Brian
456:50 queued 353:15
created

wpinv_email_renewal_reminders()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 53
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 28
c 0
b 0
f 0
nc 9
nop 0
dl 0
loc 53
rs 8.4444

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Contains functions related to Invoicing plugin.
4
 *
5
 * @since 1.0.0
6
 * @package Invoicing
7
 */
8
9
// MUST have WordPress.
10
if ( !defined( 'WPINC' ) ) {
11
    exit( 'Do NOT access this file directly: ' . basename( __FILE__ ) );
12
}
13
14
function wpinv_init_transactional_emails() {
15
    $email_actions = apply_filters( 'wpinv_email_actions', array(
16
        'wpinv_status_wpi-pending_to_wpi-processing',
17
        'wpinv_status_wpi-pending_to_publish',
18
        'wpinv_status_wpi-pending_to_wpi-cancelled',
19
        'wpinv_status_wpi-pending_to_wpi-failed',
20
        'wpinv_status_wpi-pending_to_wpi-onhold',
21
        'wpinv_status_wpi-failed_to_wpi-processing',
22
        'wpinv_status_wpi-failed_to_publish',
23
        'wpinv_status_wpi-failed_to_wpi-onhold',
24
        'wpinv_status_wpi-onhold_wpi-to_processing',
25
        'wpinv_status_wpi-onhold_to_wpi-cancelled',
26
        'wpinv_status_wpi-onhold_to_wpi-failed',
27
        'wpinv_status_publish_to_wpi-refunded',
28
        'wpinv_status_wpi-processing_to_wpi-refunded',
29
        'wpinv_status_publish',
30
        'wpinv_fully_refunded',
31
        'wpinv_partially_refunded',
32
        'wpinv_new_invoice_note'
33
    ) );
34
35
    foreach ( $email_actions as $action ) {
36
        add_action( $action, 'wpinv_send_transactional_email', 10, 10 );
37
    }
38
}
39
add_action( 'init', 'wpinv_init_transactional_emails' );
40
41
// New invoice email
42
add_action( 'wpinv_status_wpi-pending_to_wpi-processing_notification', 'wpinv_new_invoice_notification' );
43
add_action( 'wpinv_status_wpi-pending_to_publish_notification', 'wpinv_new_invoice_notification' );
44
add_action( 'wpinv_status_wpi-pending_to_wpi-onhold_notification', 'wpinv_new_invoice_notification' );
45
add_action( 'wpinv_status_wpi-failed_to_wpi-processing_notification', 'wpinv_new_invoice_notification' );
46
add_action( 'wpinv_status_wpi-failed_to_publish_notification', 'wpinv_new_invoice_notification' );
47
add_action( 'wpinv_status_wpi-failed_to_wpi-onhold_notification', 'wpinv_new_invoice_notification' );
48
49
// Cancelled invoice email
50
add_action( 'wpinv_status_wpi-pending_to_wpi-cancelled_notification', 'wpinv_cancelled_invoice_notification' );
51
add_action( 'wpinv_status_wpi-onhold_to_wpi-cancelled_notification', 'wpinv_cancelled_invoice_notification' );
52
53
// Failed invoice email
54
add_action( 'wpinv_status_wpi-pending_to_wpi-failed_notification', 'wpinv_failed_invoice_notification' );
55
add_action( 'wpinv_status_wpi-onhold_to_wpi-failed_notification', 'wpinv_failed_invoice_notification' );
56
57
// On hold invoice email
58
add_action( 'wpinv_status_wpi-pending_to_wpi-onhold_notification', 'wpinv_onhold_invoice_notification' );
59
add_action( 'wpinv_status_wpi-failed_to_wpi-onhold_notification', 'wpinv_onhold_invoice_notification' );
60
61
// Processing invoice email
62
add_action( 'wpinv_status_wpi-pending_to_wpi-processing_notification', 'wpinv_processing_invoice_notification' );
63
64
// Paid invoice email
65
add_action( 'wpinv_status_publish_notification', 'wpinv_completed_invoice_notification' );
66
67
// Refunded invoice email
68
add_action( 'wpinv_fully_refunded_notification', 'wpinv_fully_refunded_notification' );
69
add_action( 'wpinv_partially_refunded_notification', 'wpinv_partially_refunded_notification' );
70
add_action( 'wpinv_status_publish_to_wpi-refunded_notification', 'wpinv_fully_refunded_notification' );
71
add_action( 'wpinv_status_wpi-processing_to_wpi-refunded_notification', 'wpinv_fully_refunded_notification' );
72
73
// Invoice note
74
add_action( 'wpinv_new_invoice_note_notification', 'wpinv_new_invoice_note_notification' );
75
76
add_action( 'wpinv_email_header', 'wpinv_email_header' );
77
add_action( 'wpinv_email_footer', 'wpinv_email_footer' );
78
add_action( 'wpinv_email_invoice_details', 'wpinv_email_invoice_details', 10, 3 );
79
add_action( 'wpinv_email_invoice_items', 'wpinv_email_invoice_items', 10, 3 );
80
add_action( 'wpinv_email_billing_details', 'wpinv_email_billing_details', 10, 3 );
81
82
function wpinv_send_transactional_email() {
83
    $args       = func_get_args();
84
    $function   = current_filter() . '_notification';
85
    do_action_ref_array( $function, $args );
86
}
87
88
function wpinv_new_invoice_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

88
function wpinv_new_invoice_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    $email_type = 'new_invoice';
90
    if ( !wpinv_email_is_enabled( $email_type ) ) {
91
        return false;
92
    }
93
94
    $invoice = wpinv_get_invoice( $invoice_id );
95
    if ( empty( $invoice ) ) {
96
        return false;
97
    }
98
99
    if ( !("wpi_invoice" === $invoice->post_type) ) {
100
        return false;
101
    }
102
103
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
104
    if ( !is_email( $recipient ) ) {
0 ignored issues
show
Bug introduced by
It seems like $recipient can also be of type false; however, parameter $email of is_email() 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

104
    if ( !is_email( /** @scrutinizer ignore-type */ $recipient ) ) {
Loading history...
105
        return false;
106
    }
107
108
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type, true );
109
110
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
111
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
112
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
113
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
114
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
115
116
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
117
            'invoice'       => $invoice,
118
            'email_type'    => $email_type,
119
            'email_heading' => $email_heading,
120
            'sent_to_admin' => true,
121
            'plain_text'    => false,
122
            'message_body'  => $message_body,
123
        ) );
124
125
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
126
127
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type, true );
128
129
    return $sent;
130
}
131
132
function wpinv_cancelled_invoice_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

132
function wpinv_cancelled_invoice_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
133
    $email_type = 'cancelled_invoice';
134
    if ( !wpinv_email_is_enabled( $email_type ) ) {
135
        return false;
136
    }
137
138
    $invoice = wpinv_get_invoice( $invoice_id );
139
    if ( empty( $invoice ) ) {
140
        return false;
141
    }
142
143
    if ( !("wpi_invoice" === $invoice->post_type) ) {
144
        return false;
145
    }
146
147
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
148
    if ( !is_email( $recipient ) ) {
0 ignored issues
show
Bug introduced by
It seems like $recipient can also be of type false; however, parameter $email of is_email() 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

148
    if ( !is_email( /** @scrutinizer ignore-type */ $recipient ) ) {
Loading history...
149
        return false;
150
    }
151
152
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type, true );
153
154
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
155
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
156
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
157
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
158
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
159
160
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
161
            'invoice'       => $invoice,
162
            'email_type'    => $email_type,
163
            'email_heading' => $email_heading,
164
            'sent_to_admin' => true,
165
            'plain_text'    => false,
166
            'message_body'  => $message_body,
167
        ) );
168
169
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
170
171
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type, true );
172
173
    return $sent;
174
}
175
176
function wpinv_failed_invoice_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

176
function wpinv_failed_invoice_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
177
    $email_type = 'failed_invoice';
178
    if ( !wpinv_email_is_enabled( $email_type ) ) {
179
        return false;
180
    }
181
    
182
    $invoice = wpinv_get_invoice( $invoice_id );
183
    if ( empty( $invoice ) ) {
184
        return false;
185
    }
186
187
    if ( !("wpi_invoice" === $invoice->post_type) ) {
188
        return false;
189
    }
190
191
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
192
    if ( !is_email( $recipient ) ) {
0 ignored issues
show
Bug introduced by
It seems like $recipient can also be of type false; however, parameter $email of is_email() 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

192
    if ( !is_email( /** @scrutinizer ignore-type */ $recipient ) ) {
Loading history...
193
        return false;
194
    }
195
196
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type, true );
197
198
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
199
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
200
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
201
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
202
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
203
    
204
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
205
            'invoice'       => $invoice,
206
            'email_type'    => $email_type,
207
            'email_heading' => $email_heading,
208
            'sent_to_admin' => true,
209
            'plain_text'    => false,
210
            'message_body'  => $message_body,
211
        ) );
212
213
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
214
215
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type, true );
216
217
    return $sent;
218
}
219
220
function wpinv_onhold_invoice_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

220
function wpinv_onhold_invoice_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
221
    $email_type = 'onhold_invoice';
222
    if ( !wpinv_email_is_enabled( $email_type ) ) {
223
        return false;
224
    }
225
226
    $invoice = wpinv_get_invoice( $invoice_id );
227
    if ( empty( $invoice ) ) {
228
        return false;
229
    }
230
231
    if ( !("wpi_invoice" === $invoice->post_type) ) {
232
        return false;
233
    }
234
235
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
236
    if ( !is_email( $recipient ) ) {
237
        return false;
238
    }
239
240
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
241
242
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
243
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
244
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
245
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
246
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
247
    
248
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
249
            'invoice'       => $invoice,
250
            'email_type'    => $email_type,
251
            'email_heading' => $email_heading,
252
            'sent_to_admin' => false,
253
            'plain_text'    => false,
254
            'message_body'  => $message_body,
255
        ) );
256
    
257
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
258
    
259
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
260
        $recipient  = wpinv_get_admin_email();
261
        $subject    .= ' - ADMIN BCC COPY';
262
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
263
    }
264
265
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
266
267
    return $sent;
268
}
269
270
function wpinv_processing_invoice_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

270
function wpinv_processing_invoice_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
271
    $email_type = 'processing_invoice';
272
    if ( !wpinv_email_is_enabled( $email_type ) ) {
273
        return false;
274
    }
275
276
    $invoice = wpinv_get_invoice( $invoice_id );
277
    if ( empty( $invoice ) ) {
278
        return false;
279
    }
280
281
    if ( !("wpi_invoice" === $invoice->post_type) ) {
282
        return false;
283
    }
284
285
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
286
    if ( !is_email( $recipient ) ) {
287
        return false;
288
    }
289
290
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
291
292
    $search                     = array();
293
    $search['invoice_number']   = '{invoice_number}';
294
    $search['invoice_date']     = '{invoice_date}';
295
    $search['name']             = '{name}';
296
297
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
298
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
299
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
300
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
301
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
302
    
303
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
304
            'invoice'       => $invoice,
305
            'email_type'    => $email_type,
306
            'email_heading' => $email_heading,
307
            'sent_to_admin' => false,
308
            'plain_text'    => false,
309
            'message_body'  => $message_body,
310
        ) );
311
312
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
313
314
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
315
        $recipient  = wpinv_get_admin_email();
316
        $subject    .= ' - ADMIN BCC COPY';
317
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
318
    }
319
320
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
321
322
    return $sent;
323
}
324
325
function wpinv_completed_invoice_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

325
function wpinv_completed_invoice_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
326
    $email_type = 'completed_invoice';
327
    if ( !wpinv_email_is_enabled( $email_type ) ) {
328
        return false;
329
    }
330
331
    $invoice = wpinv_get_invoice( $invoice_id );
332
    if ( empty( $invoice ) ) {
333
        return false;
334
    }
335
336
    if ( !("wpi_invoice" === $invoice->post_type) ) {
337
        return false;
338
    }
339
340
    if($invoice->is_renewal() && wpinv_email_is_enabled( 'completed_invoice_renewal' )){
341
        return false;
342
    }
343
344
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
345
    if ( !is_email( $recipient ) ) {
346
        return false;
347
    }
348
349
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
350
351
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
352
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
353
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
354
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
355
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
356
357
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
358
            'invoice'       => $invoice,
359
            'email_type'    => $email_type,
360
            'email_heading' => $email_heading,
361
            'sent_to_admin' => false,
362
            'plain_text'    => false,
363
            'message_body'  => $message_body,
364
        ) );
365
366
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
367
368
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
369
        $recipient  = wpinv_get_admin_email();
370
        $subject    .= ' - ADMIN BCC COPY';
371
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
372
    }
373
374
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
375
376
    return $sent;
377
}
378
379
function wpinv_fully_refunded_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

379
function wpinv_fully_refunded_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
380
    $email_type = 'refunded_invoice';
381
    if ( !wpinv_email_is_enabled( $email_type ) ) {
382
        return false;
383
    }
384
385
    $invoice = wpinv_get_invoice( $invoice_id );
386
    if ( empty( $invoice ) ) {
387
        return false;
388
    }
389
390
    if ( !("wpi_invoice" === $invoice->post_type) ) {
391
        return false;
392
    }
393
394
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
395
    if ( !is_email( $recipient ) ) {
396
        return false;
397
    }
398
399
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
400
401
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
402
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
403
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
404
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
405
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
406
407
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
408
            'invoice'           => $invoice,
409
            'email_type'        => $email_type,
410
            'email_heading'     => $email_heading,
411
            'sent_to_admin'     => false,
412
            'plain_text'        => false,
413
            'partial_refund'    => false,
414
            'message_body'      => $message_body,
415
        ) );
416
417
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
418
419
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
420
        $recipient  = wpinv_get_admin_email();
421
        $subject    .= ' - ADMIN BCC COPY';
422
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
423
    }
424
425
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
426
427
    return $sent;
428
}
429
430
function wpinv_partially_refunded_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

430
function wpinv_partially_refunded_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
431
    $email_type = 'refunded_invoice';
432
    if ( !wpinv_email_is_enabled( $email_type ) ) {
433
        return false;
434
    }
435
436
    $invoice = wpinv_get_invoice( $invoice_id );
437
    if ( empty( $invoice ) ) {
438
        return false;
439
    }
440
441
    if ( !("wpi_invoice" === $invoice->post_type) ) {
442
        return false;
443
    }
444
445
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
446
    if ( !is_email( $recipient ) ) {
447
        return false;
448
    }
449
450
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
451
452
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
453
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
454
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
455
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
456
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
457
458
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
459
            'invoice'           => $invoice,
460
            'email_type'        => $email_type,
461
            'email_heading'     => $email_heading,
462
            'sent_to_admin'     => false,
463
            'plain_text'        => false,
464
            'partial_refund'    => true,
465
            'message_body'      => $message_body,
466
        ) );
467
468
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
469
470
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
471
        $recipient  = wpinv_get_admin_email();
472
        $subject    .= ' - ADMIN BCC COPY';
473
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
474
    }
475
476
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
477
478
    return $sent;
479
}
480
481
function wpinv_new_invoice_note_notification( $invoice_id, $new_status = '' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $new_status is not used and could be removed. ( Ignorable by Annotation )

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

481
function wpinv_new_invoice_note_notification( $invoice_id, /** @scrutinizer ignore-unused */ $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $invoice_id is not used and could be removed. ( Ignorable by Annotation )

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

481
function wpinv_new_invoice_note_notification( /** @scrutinizer ignore-unused */ $invoice_id, $new_status = '' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
482
}
483
484
function wpinv_user_invoice_notification( $invoice_id ) {
485
    $email_type = 'user_invoice';
486
    if ( !wpinv_email_is_enabled( $email_type ) ) {
487
        return -1;
488
    }
489
490
    $invoice = wpinv_get_invoice( $invoice_id );
491
    if ( empty( $invoice ) ) {
492
        return false;
493
    }
494
495
    if ( !("wpi_invoice" === $invoice->post_type) ) {
496
        return false;
497
    }
498
499
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
500
    if ( !is_email( $recipient ) ) {
501
        return false;
502
    }
503
504
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
505
506
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
507
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
508
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
509
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
510
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
511
    
512
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
513
            'invoice'       => $invoice,
514
            'email_type'    => $email_type,
515
            'email_heading' => $email_heading,
516
            'sent_to_admin' => false,
517
            'plain_text'    => false,
518
            'message_body'  => $message_body,
519
        ) );
520
521
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
522
523
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
524
        $recipient  = wpinv_get_admin_email();
525
        $subject    .= ' - ADMIN BCC COPY';
526
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
527
    }
528
529
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
530
531
    if ( $sent ) {
0 ignored issues
show
introduced by
The condition $sent is always true.
Loading history...
532
        $note = __( 'Invoice has been emailed to the user.', 'invoicing' );
533
    } else {
534
        $note = __( 'Fail to send invoice to the user!', 'invoicing' );
535
    }
536
537
    $invoice->add_note( $note, '', '', true ); // Add system note.
538
539
    return $sent;
540
}
541
542
function wpinv_user_note_notification( $invoice_id, $args = array() ) {
543
    $email_type = 'user_note';
544
    if ( !wpinv_email_is_enabled( $email_type ) ) {
545
        return false;
546
    }
547
548
    $invoice = wpinv_get_invoice( $invoice_id );
549
    if ( empty( $invoice ) ) {
550
        return false;
551
    }
552
553
    $recipient      = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
554
    if ( !is_email( $recipient ) ) {
555
        return false;
556
    }
557
558
    $defaults = array(
559
        'user_note' => ''
560
    );
561
562
    $args = wp_parse_args( $args, $defaults );
563
564
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
565
566
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
567
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
568
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
569
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
570
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
571
572
    $message_body   = str_replace( '{customer_note}', $args['user_note'], $message_body );
573
574
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
575
            'invoice'       => $invoice,
576
            'email_type'    => $email_type,
577
            'email_heading' => $email_heading,
578
            'sent_to_admin' => false,
579
            'plain_text'    => false,
580
            'message_body'  => $message_body,
581
            'customer_note' => $args['user_note']
582
        ) );
583
584
    $content        = wpinv_email_format_text( $content, $invoice );
585
586
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
587
588
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
589
590
    return $sent;
591
}
592
593
function wpinv_mail_get_from_address() {
594
    $from_address = apply_filters( 'wpinv_mail_from_address', wpinv_get_option( 'email_from' ) );
595
    return sanitize_email( $from_address );
0 ignored issues
show
Bug introduced by
It seems like $from_address can also be of type false; however, parameter $email of sanitize_email() 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

595
    return sanitize_email( /** @scrutinizer ignore-type */ $from_address );
Loading history...
596
}
597
598
function wpinv_mail_get_from_name() {
599
    $from_name = apply_filters( 'wpinv_mail_from_name', wpinv_get_option( 'email_from_name' ) );
600
    return wp_specialchars_decode( esc_html( $from_name ), ENT_QUOTES );
0 ignored issues
show
Bug introduced by
It seems like $from_name can also be of type false; however, parameter $text of esc_html() 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

600
    return wp_specialchars_decode( esc_html( /** @scrutinizer ignore-type */ $from_name ), ENT_QUOTES );
Loading history...
601
}
602
603
function wpinv_mail_admin_bcc_active( $mail_type = '' ) {
604
    $active = apply_filters( 'wpinv_mail_admin_bcc_active', wpinv_get_option( 'email_' . $mail_type . '_admin_bcc' ) );
605
    return ( $active ? true : false );
606
}
607
    
608
function wpinv_mail_get_content_type(  $content_type = 'text/html', $email_type = 'html' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $content_type is not used and could be removed. ( Ignorable by Annotation )

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

608
function wpinv_mail_get_content_type(  /** @scrutinizer ignore-unused */ $content_type = 'text/html', $email_type = 'html' ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
609
    $email_type = apply_filters( 'wpinv_mail_content_type', $email_type );
610
611
    switch ( $email_type ) {
612
        case 'html' :
613
            $content_type = 'text/html';
614
            break;
615
        case 'multipart' :
616
            $content_type = 'multipart/alternative';
617
            break;
618
        default :
619
            $content_type = 'text/plain';
620
            break;
621
    }
622
623
    return $content_type;
624
}
625
    
626
function wpinv_mail_send( $to, $subject, $message, $headers, $attachments ) {
627
    add_filter( 'wp_mail_from', 'wpinv_mail_get_from_address' );
628
    add_filter( 'wp_mail_from_name', 'wpinv_mail_get_from_name' );
629
    add_filter( 'wp_mail_content_type', 'wpinv_mail_get_content_type' );
630
631
    $message = wpinv_email_style_body( $message );
632
    $message = apply_filters( 'wpinv_mail_content', $message );
633
634
    $sent  = wp_mail( $to, $subject, $message, $headers, $attachments );
635
636
    if ( !$sent ) {
637
        $log_message = wp_sprintf( __( "\nTime: %s\nTo: %s\nSubject: %s\n", 'invoicing' ), date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ), ( is_array( $to ) ? implode( ', ', $to ) : $to ), $subject );
638
        wpinv_error_log( $log_message, __( "Email from Invoicing plugin failed to send", 'invoicing' ), __FILE__, __LINE__ );
639
    }
640
641
    remove_filter( 'wp_mail_from', 'wpinv_mail_get_from_address' );
642
    remove_filter( 'wp_mail_from_name', 'wpinv_mail_get_from_name' );
643
    remove_filter( 'wp_mail_content_type', 'wpinv_mail_get_content_type' );
644
645
    return $sent;
646
}
647
    
648
function wpinv_get_emails() {
649
    $overdue_days_options       = array();
650
    $overdue_days_options[0]    = __( 'On the Due Date', 'invoicing' );
651
    $overdue_days_options[1]    = __( '1 day after Due Date', 'invoicing' );
652
653
    for ( $i = 2; $i <= 10; $i++ ) {
654
        $overdue_days_options[$i]   = wp_sprintf( __( '%d days after Due Date', 'invoicing' ), $i );
655
    }
656
657
    // Default, built-in gateways
658
    $emails = array(
659
            'new_invoice' => array(
660
            'email_new_invoice_header' => array(
661
                'id'   => 'email_new_invoice_header',
662
                'name' => '<h3>' . __( 'New Invoice', 'invoicing' ) . '</h3>',
663
                'desc' => __( 'New invoice emails are sent to admin when a new invoice is received.', 'invoicing' ),
664
                'type' => 'header',
665
            ),
666
            'email_new_invoice_active' => array(
667
                'id'   => 'email_new_invoice_active',
668
                'name' => __( 'Enable/Disable', 'invoicing' ),
669
                'desc' => __( 'Enable this email notification', 'invoicing' ),
670
                'type' => 'checkbox',
671
                'std'  => 1
672
            ),
673
            'email_new_invoice_subject' => array(
674
                'id'   => 'email_new_invoice_subject',
675
                'name' => __( 'Subject', 'invoicing' ),
676
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
677
                'type' => 'text',
678
                'std'  => __( '[{site_title}] New payment invoice ({invoice_number}) - {invoice_date}', 'invoicing' ),
679
                'size' => 'large'
680
            ),
681
            'email_new_invoice_heading' => array(
682
                'id'   => 'email_new_invoice_heading',
683
                'name' => __( 'Email Heading', 'invoicing' ),
684
                'desc' => __( 'Enter the main heading contained within the email notification for the invoice receipt email.', 'invoicing' ),
685
                'type' => 'text',
686
                'std'  => __( 'New payment invoice', 'invoicing' ),
687
                'size' => 'large'
688
            ),
689
            'email_new_invoice_body' => array(
690
                'id'   => 'email_new_invoice_body',
691
                'name' => __( 'Email Content', 'invoicing' ),
692
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
693
                'type' => 'rich_editor',
694
                'std'  => __( '<p>Hi Admin,</p><p>You have received payment invoice from {name}.</p>', 'invoicing' ),
695
                'class' => 'large',
696
                'size' => '10'
697
            ),
698
        ),
699
        'cancelled_invoice' => array(
700
            'email_cancelled_invoice_header' => array(
701
                'id'   => 'email_cancelled_invoice_header',
702
                'name' => '<h3>' . __( 'Cancelled Invoice', 'invoicing' ) . '</h3>',
703
                'desc' => __( 'Cancelled invoice emails are sent to admin when invoices have been marked cancelled.', 'invoicing' ),
704
                'type' => 'header',
705
            ),
706
            'email_cancelled_invoice_active' => array(
707
                'id'   => 'email_cancelled_invoice_active',
708
                'name' => __( 'Enable/Disable', 'invoicing' ),
709
                'desc' => __( 'Enable this email notification', 'invoicing' ),
710
                'type' => 'checkbox',
711
                'std'  => 1
712
            ),
713
            'email_cancelled_invoice_subject' => array(
714
                'id'   => 'email_cancelled_invoice_subject',
715
                'name' => __( 'Subject', 'invoicing' ),
716
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
717
                'type' => 'text',
718
                'std'  => __( '[{site_title}] Cancelled invoice ({invoice_number})', 'invoicing' ),
719
                'size' => 'large'
720
            ),
721
            'email_cancelled_invoice_heading' => array(
722
                'id'   => 'email_cancelled_invoice_heading',
723
                'name' => __( 'Email Heading', 'invoicing' ),
724
                'desc' => __( 'Enter the main heading contained within the email notification.', 'invoicing' ),
725
                'type' => 'text',
726
                'std'  => __( 'Cancelled invoice', 'invoicing' ),
727
                'size' => 'large'
728
            ),
729
            'email_cancelled_invoice_body' => array(
730
                'id'   => 'email_cancelled_invoice_body',
731
                'name' => __( 'Email Content', 'invoicing' ),
732
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
733
                'type' => 'rich_editor',
734
                'std'  => __( '<p>Hi Admin,</p><p>The invoice #{invoice_number} from {site_title} has been cancelled.</p>', 'invoicing' ),
735
                'class' => 'large',
736
                'size' => '10'
737
            ),
738
        ),
739
        'failed_invoice' => array(
740
            'email_failed_invoice_header' => array(
741
                'id'   => 'email_failed_invoice_header',
742
                'name' => '<h3>' . __( 'Failed Invoice', 'invoicing' ) . '</h3>',
743
                'desc' => __( 'Failed invoice emails are sent to admin when invoices have been marked failed (if they were previously processing or on-hold).', 'invoicing' ),
744
                'type' => 'header',
745
            ),
746
            'email_failed_invoice_active' => array(
747
                'id'   => 'email_failed_invoice_active',
748
                'name' => __( 'Enable/Disable', 'invoicing' ),
749
                'desc' => __( 'Enable this email notification', 'invoicing' ),
750
                'type' => 'checkbox',
751
                'std'  => 1
752
            ),
753
            'email_failed_invoice_subject' => array(
754
                'id'   => 'email_failed_invoice_subject',
755
                'name' => __( 'Subject', 'invoicing' ),
756
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
757
                'type' => 'text',
758
                'std'  => __( '[{site_title}] Failed invoice ({invoice_number})', 'invoicing' ),
759
                'size' => 'large'
760
            ),
761
            'email_failed_invoice_heading' => array(
762
                'id'   => 'email_failed_invoice_heading',
763
                'name' => __( 'Email Heading', 'invoicing' ),
764
                'desc' => __( 'Enter the main heading contained within the email notification.', 'invoicing' ),
765
                'type' => 'text',
766
                'std'  => __( 'Failed invoice', 'invoicing' ),
767
                'size' => 'large'
768
            ),
769
            'email_failed_invoice_body' => array(
770
                'id'   => 'email_failed_invoice_body',
771
                'name' => __( 'Email Content', 'invoicing' ),
772
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
773
                'type' => 'rich_editor',
774
                'std'  => __( '<p>Hi Admin,</p><p>Payment for invoice #{invoice_number} from {site_title} has been failed.</p>', 'invoicing' ),
775
                'class' => 'large',
776
                'size' => '10'
777
            ),
778
        ),
779
        'onhold_invoice' => array(
780
            'email_onhold_invoice_header' => array(
781
                'id'   => 'email_onhold_invoice_header',
782
                'name' => '<h3>' . __( 'On Hold Invoice', 'invoicing' ) . '</h3>',
783
                'desc' => __( 'This is an invoice notification sent to users containing invoice details after an invoice is placed on-hold.', 'invoicing' ),
784
                'type' => 'header',
785
            ),
786
            'email_onhold_invoice_active' => array(
787
                'id'   => 'email_onhold_invoice_active',
788
                'name' => __( 'Enable/Disable', 'invoicing' ),
789
                'desc' => __( 'Enable this email notification', 'invoicing' ),
790
                'type' => 'checkbox',
791
                'std'  => 1
792
            ),
793
            'email_onhold_invoice_subject' => array(
794
                'id'   => 'email_onhold_invoice_subject',
795
                'name' => __( 'Subject', 'invoicing' ),
796
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
797
                'type' => 'text',
798
                'std'  => __( '[{site_title}] Your invoice receipt from {invoice_date}', 'invoicing' ),
799
                'size' => 'large'
800
            ),
801
            'email_onhold_invoice_heading' => array(
802
                'id'   => 'email_onhold_invoice_heading',
803
                'name' => __( 'Email Heading', 'invoicing' ),
804
                'desc' => __( 'Enter the main heading contained within the email notification.', 'invoicing' ),
805
                'type' => 'text',
806
                'std'  => __( 'Thank you for your invoice', 'invoicing' ),
807
                'size' => 'large'
808
            ),
809
            'email_onhold_invoice_admin_bcc' => array(
810
                'id'   => 'email_onhold_invoice_admin_bcc',
811
                'name' => __( 'Enable Admin BCC', 'invoicing' ),
812
                'desc' => __( 'Check if you want to send this notification email to site Admin.', 'invoicing' ),
813
                'type' => 'checkbox',
814
                'std'  => 1
815
            ),
816
            'email_onhold_invoice_body' => array(
817
                'id'   => 'email_onhold_invoice_body',
818
                'name' => __( 'Email Content', 'invoicing' ),
819
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
820
                'type' => 'rich_editor',
821
                'std'  => __( '<p>Hi {name},</p><p>Your invoice is on-hold until we confirm your payment has been received.</p>', 'invoicing' ),
822
                'class' => 'large',
823
                'size' => '10'
824
            ),
825
        ),
826
        'processing_invoice' => array(
827
            'email_processing_invoice_header' => array(
828
                'id'   => 'email_processing_invoice_header',
829
                'name' => '<h3>' . __( 'Processing Invoice', 'invoicing' ) . '</h3>',
830
                'desc' => __( 'This is an invoice notification sent to users containing invoice details after payment.', 'invoicing' ),
831
                'type' => 'header',
832
            ),
833
            'email_processing_invoice_active' => array(
834
                'id'   => 'email_processing_invoice_active',
835
                'name' => __( 'Enable/Disable', 'invoicing' ),
836
                'desc' => __( 'Enable this email notification', 'invoicing' ),
837
                'type' => 'checkbox',
838
                'std'  => 1
839
            ),
840
            'email_processing_invoice_subject' => array(
841
                'id'   => 'email_processing_invoice_subject',
842
                'name' => __( 'Subject', 'invoicing' ),
843
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
844
                'type' => 'text',
845
                'std'  => __( '[{site_title}] Your invoice receipt from {invoice_date}', 'invoicing' ),
846
                'size' => 'large'
847
            ),
848
            'email_processing_invoice_heading' => array(
849
                'id'   => 'email_processing_invoice_heading',
850
                'name' => __( 'Email Heading', 'invoicing' ),
851
                'desc' => __( 'Enter the main heading contained within the email notification for the invoice receipt email.', 'invoicing' ),
852
                'type' => 'text',
853
                'std'  => __( 'Thank you for your invoice', 'invoicing' ),
854
                'size' => 'large'
855
            ),
856
            'email_processing_invoice_admin_bcc' => array(
857
                'id'   => 'email_processing_invoice_admin_bcc',
858
                'name' => __( 'Enable Admin BCC', 'invoicing' ),
859
                'desc' => __( 'Check if you want to send this notification email to site Admin.', 'invoicing' ),
860
                'type' => 'checkbox',
861
                'std'  => 1
862
            ),
863
            'email_processing_invoice_body' => array(
864
                'id'   => 'email_processing_invoice_body',
865
                'name' => __( 'Email Content', 'invoicing' ),
866
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
867
                'type' => 'rich_editor',
868
                'std'  => __( '<p>Hi {name},</p><p>Your invoice has been received at {site_title} and is now being processed.</p>', 'invoicing' ),
869
                'class' => 'large',
870
                'size' => '10'
871
            ),
872
        ),
873
        'completed_invoice' => array(
874
            'email_completed_invoice_header' => array(
875
                'id'   => 'email_completed_invoice_header',
876
                'name' => '<h3>' . __( 'Paid Invoice', 'invoicing' ) . '</h3>',
877
                'desc' => __( 'Invoice paid emails are sent to users when their invoices are marked paid and usually indicate that their payment has been done.', 'invoicing' ),
878
                'type' => 'header',
879
            ),
880
            'email_completed_invoice_active' => array(
881
                'id'   => 'email_completed_invoice_active',
882
                'name' => __( 'Enable/Disable', 'invoicing' ),
883
                'desc' => __( 'Enable this email notification', 'invoicing' ),
884
                'type' => 'checkbox',
885
                'std'  => 1
886
            ),
887
            'email_completed_invoice_renewal_active' => array(
888
                'id'   => 'email_completed_invoice_renewal_active',
889
                'name' => __( 'Enable renewal notification', 'invoicing' ),
890
                'desc' => __( 'Enable renewal invoice email notification. This notification will be sent on renewal.', 'invoicing' ),
891
                'type' => 'checkbox',
892
                'std'  => 0
893
            ),
894
            'email_completed_invoice_subject' => array(
895
                'id'   => 'email_completed_invoice_subject',
896
                'name' => __( 'Subject', 'invoicing' ),
897
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
898
                'type' => 'text',
899
                'std'  => __( '[{site_title}] Your invoice from {invoice_date} has been paid', 'invoicing' ),
900
                'size' => 'large'
901
            ),
902
            'email_completed_invoice_heading' => array(
903
                'id'   => 'email_completed_invoice_heading',
904
                'name' => __( 'Email Heading', 'invoicing' ),
905
                'desc' => __( 'Enter the main heading contained within the email notification for the invoice receipt email.', 'invoicing' ),
906
                'type' => 'text',
907
                'std'  => __( 'Your invoice has been paid', 'invoicing' ),
908
                'size' => 'large'
909
            ),
910
            'email_completed_invoice_admin_bcc' => array(
911
                'id'   => 'email_completed_invoice_admin_bcc',
912
                'name' => __( 'Enable Admin BCC', 'invoicing' ),
913
                'desc' => __( 'Check if you want to send this notification email to site Admin.', 'invoicing' ),
914
                'type' => 'checkbox',
915
            ),
916
            'email_completed_invoice_body' => array(
917
                'id'   => 'email_completed_invoice_body',
918
                'name' => __( 'Email Content', 'invoicing' ),
919
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
920
                'type' => 'rich_editor',
921
                'std'  => __( '<p>Hi {name},</p><p>Your recent invoice on {site_title} has been paid.</p>', 'invoicing' ),
922
                'class' => 'large',
923
                'size' => '10'
924
            ),
925
            'std'  => 1
926
        ),
927
        'refunded_invoice' => array(
928
            'email_refunded_invoice_header' => array(
929
                'id'   => 'email_refunded_invoice_header',
930
                'name' => '<h3>' . __( 'Refunded Invoice', 'invoicing' ) . '</h3>',
931
                'desc' => __( 'Invoice refunded emails are sent to users when their invoices are marked refunded.', 'invoicing' ),
932
                'type' => 'header',
933
            ),
934
            'email_refunded_invoice_active' => array(
935
                'id'   => 'email_refunded_invoice_active',
936
                'name' => __( 'Enable/Disable', 'invoicing' ),
937
                'desc' => __( 'Enable this email notification', 'invoicing' ),
938
                'type' => 'checkbox',
939
                'std'  => 1
940
            ),
941
            'email_refunded_invoice_subject' => array(
942
                'id'   => 'email_refunded_invoice_subject',
943
                'name' => __( 'Subject', 'invoicing' ),
944
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
945
                'type' => 'text',
946
                'std'  => __( '[{site_title}] Your invoice from {invoice_date} has been refunded', 'invoicing' ),
947
                'size' => 'large'
948
            ),
949
            'email_refunded_invoice_heading' => array(
950
                'id'   => 'email_refunded_invoice_heading',
951
                'name' => __( 'Email Heading', 'invoicing' ),
952
                'desc' => __( 'Enter the main heading contained within the email notification.', 'invoicing' ),
953
                'type' => 'text',
954
                'std'  => __( 'Your invoice has been refunded', 'invoicing' ),
955
                'size' => 'large'
956
            ),
957
            'email_refunded_invoice_admin_bcc' => array(
958
                'id'   => 'email_refunded_invoice_admin_bcc',
959
                'name' => __( 'Enable Admin BCC', 'invoicing' ),
960
                'desc' => __( 'Check if you want to send this notification email to site Admin.', 'invoicing' ),
961
                'type' => 'checkbox',
962
                'std'  => 1
963
            ),
964
            'email_refunded_invoice_body' => array(
965
                'id'   => 'email_refunded_invoice_body',
966
                'name' => __( 'Email Content', 'invoicing' ),
967
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
968
                'type' => 'rich_editor',
969
                'std'  => __( '<p>Hi {name},</p><p>Your invoice on {site_title} has been refunded.</p>', 'invoicing' ),
970
                'class' => 'large',
971
                'size' => '10'
972
            ),
973
        ),
974
        'user_invoice' => array(
975
            'email_user_invoice_header' => array(
976
                'id'   => 'email_user_invoice_header',
977
                'name' => '<h3>' . __( 'Customer Invoice', 'invoicing' ) . '</h3>',
978
                'desc' => __( 'Customer invoice emails can be sent to customers containing their invoice information and payment links.', 'invoicing' ),
979
                'type' => 'header',
980
            ),
981
            'email_user_invoice_active' => array(
982
                'id'   => 'email_user_invoice_active',
983
                'name' => __( 'Enable/Disable', 'invoicing' ),
984
                'desc' => __( 'Enable this email notification', 'invoicing' ),
985
                'type' => 'checkbox',
986
                'std'  => 1
987
            ),
988
            'email_user_invoice_subject' => array(
989
                'id'   => 'email_user_invoice_subject',
990
                'name' => __( 'Subject', 'invoicing' ),
991
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
992
                'type' => 'text',
993
                'std'  => __( '[{site_title}] Your invoice from {invoice_date}', 'invoicing' ),
994
                'size' => 'large'
995
            ),
996
            'email_user_invoice_heading' => array(
997
                'id'   => 'email_user_invoice_heading',
998
                'name' => __( 'Email Heading', 'invoicing' ),
999
                'desc' => __( 'Enter the main heading contained within the email notification for the invoice receipt email.', 'invoicing' ),
1000
                'type' => 'text',
1001
                'std'  => __( 'Your invoice {invoice_number} details', 'invoicing' ),
1002
                'size' => 'large'
1003
            ),
1004
            'email_user_invoice_admin_bcc' => array(
1005
                'id'   => 'email_user_invoice_admin_bcc',
1006
                'name' => __( 'Enable Admin BCC', 'invoicing' ),
1007
                'desc' => __( 'Check if you want to send this notification email to site Admin.', 'invoicing' ),
1008
                'type' => 'checkbox',
1009
                'std'  => 1
1010
            ),
1011
            'email_user_invoice_body' => array(
1012
                'id'   => 'email_user_invoice_body',
1013
                'name' => __( 'Email Content', 'invoicing' ),
1014
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
1015
                'type' => 'rich_editor',
1016
                'std'  => __( '<p>Hi {name},</p><p>An invoice has been created for you on {site_title}. To view / pay for this invoice please use the following link: <a class="btn btn-success" href="{invoice_link}">View / Pay</a></p>', 'invoicing' ),
1017
                'class' => 'large',
1018
                'size' => '10'
1019
            ),
1020
        ),
1021
        'user_note' => array(
1022
            'email_user_note_header' => array(
1023
                'id'   => 'email_user_note_header',
1024
                'name' => '<h3>' . __( 'Customer Note', 'invoicing' ) . '</h3>',
1025
                'desc' => __( 'Customer note emails are sent when you add a note to an invoice/quote.', 'invoicing' ),
1026
                'type' => 'header',
1027
            ),
1028
            'email_user_note_active' => array(
1029
                'id'   => 'email_user_note_active',
1030
                'name' => __( 'Enable/Disable', 'invoicing' ),
1031
                'desc' => __( 'Enable this email notification', 'invoicing' ),
1032
                'type' => 'checkbox',
1033
                'std'  => 1
1034
            ),
1035
            'email_user_note_subject' => array(
1036
                'id'   => 'email_user_note_subject',
1037
                'name' => __( 'Subject', 'invoicing' ),
1038
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
1039
                'type' => 'text',
1040
                'std'  => __( '[{site_title}] Note added to your {invoice_label} #{invoice_number} from {invoice_date}', 'invoicing' ),
1041
                'size' => 'large'
1042
            ),
1043
            'email_user_note_heading' => array(
1044
                'id'   => 'email_user_note_heading',
1045
                'name' => __( 'Email Heading', 'invoicing' ),
1046
                'desc' => __( 'Enter the main heading contained within the email notification.', 'invoicing' ),
1047
                'type' => 'text',
1048
                'std'  => __( 'A note has been added to your {invoice_label}', 'invoicing' ),
1049
                'size' => 'large'
1050
            ),
1051
            'email_user_note_body' => array(
1052
                'id'   => 'email_user_note_body',
1053
                'name' => __( 'Email Content', 'invoicing' ),
1054
                'desc' => __( 'The content of the email (wildcards and HTML are allowed).', 'invoicing' ),
1055
                'type' => 'rich_editor',
1056
                'std'  => __( '<p>Hi {name},</p><p>Following note has been added to your {invoice_label}:</p><blockquote class="wpinv-note">{customer_note}</blockquote>', 'invoicing' ),
1057
                'class' => 'large',
1058
                'size' => '10'
1059
            ),
1060
        ),
1061
1062
        'pre_payment' => array(
1063
            'email_pre_payment_header' => array(
1064
                'id'   => 'email_pre_payment_header',
1065
                'name' => '<h3>' . __( 'Renewal Reminder', 'invoicing' ) . '</h3>',
1066
                'desc' => __( 'Renewal reminder emails are sent to user automatically.', 'invoicing' ),
1067
                'type' => 'header',
1068
            ),
1069
            'email_pre_payment_active' => array(
1070
                'id'   => 'email_pre_payment_active',
1071
                'name' => __( 'Enable/Disable', 'invoicing' ),
1072
                'desc' => __( 'Enable this email notification', 'invoicing' ),
1073
                'type' => 'checkbox',
1074
                'std'  => 1
1075
            ),
1076
            'email_pre_payment_reminder_days' => array(
1077
                'id'            => 'email_pre_payment_reminder_days',
1078
                'name'          => __( 'When to Send', 'invoicing' ),
1079
                'desc'          => __( 'Enter a comma separated list of days before renewal when this email should be sent.', 'invoicing' ),
1080
                'default'       => '',
1081
                'type'          => 'text',
1082
                'std'           => '1,5,10',
1083
            ),
1084
            'email_pre_payment_subject' => array(
1085
                'id'   => 'email_pre_payment_subject',
1086
                'name' => __( 'Subject', 'invoicing' ),
1087
                'desc' => __( 'Enter the subject line for the email.', 'invoicing' ),
1088
                'type' => 'text',
1089
                'std'  => __( '[{site_title}] Renewal Reminder', 'invoicing' ),
1090
                'size' => 'large'
1091
            ),
1092
            'email_pre_payment_heading' => array(
1093
                'id'   => 'email_pre_payment_heading',
1094
                'name' => __( 'Email Heading', 'invoicing' ),
1095
                'desc' => __( 'Enter the main heading contained within the email notification.', 'invoicing' ),
1096
                'type' => 'text',
1097
                'std'  => __( 'Upcoming renewal reminder', 'invoicing' ),
1098
                'size' => 'large'
1099
            ),
1100
            'email_pre_payment_admin_bcc' => array(
1101
                'id'   => 'email_pre_payment_admin_bcc',
1102
                'name' => __( 'Enable Admin BCC', 'invoicing' ),
1103
                'desc' => __( 'Check if you want to send this notification email to site Admin.', 'invoicing' ),
1104
                'type' => 'checkbox',
1105
                'std'  => 1
1106
            ),
1107
            'email_pre_payment_body' => array(
1108
                'id'   => 'email_pre_payment_body',
1109
                'name' => __( 'Email Content', 'invoicing' ),
1110
                'desc' => __( 'The content of the email.', 'invoicing' ),
1111
                'type' => 'rich_editor',
1112
                'std'  => __( '<p>Hi {full_name},</p><p>Your subscription for invoice <a href="{invoice_link}">#{invoice_number}</a> will renew on {subscription_renewal_date}.</p>', 'invoicing' ),
1113
                'class' => 'large',
1114
                'size'  => 10,
1115
            ),
1116
        ),
1117
1118
        'overdue' => array(
1119
            'email_overdue_header' => array(
1120
                'id'   => 'email_overdue_header',
1121
                'name' => '<h3>' . __( 'Payment Reminder', 'invoicing' ) . '</h3>',
1122
                'desc' => __( 'Payment reminder emails are sent to user automatically.', 'invoicing' ),
1123
                'type' => 'header',
1124
            ),
1125
            'email_overdue_active' => array(
1126
                'id'   => 'email_overdue_active',
1127
                'name' => __( 'Enable/Disable', 'invoicing' ),
1128
                'desc' => __( 'Enable this email notification', 'invoicing' ),
1129
                'type' => 'checkbox',
1130
                'std'  => 1
1131
            ),
1132
            'email_due_reminder_days' => array(
1133
                'id'        => 'email_due_reminder_days',
1134
                'name'      => __( 'When to Send', 'invoicing' ),
1135
                'desc'      => __( 'Check when you would like payment reminders sent out.', 'invoicing' ),
1136
                'default'   => '',
1137
                'type'      => 'multicheck',
1138
                'options'   => $overdue_days_options,
1139
            ),
1140
            'email_overdue_subject' => array(
1141
                'id'   => 'email_overdue_subject',
1142
                'name' => __( 'Subject', 'invoicing' ),
1143
                'desc' => __( 'Enter the subject line for the invoice receipt email.', 'invoicing' ),
1144
                'type' => 'text',
1145
                'std'  => __( '[{site_title}] Payment Reminder', 'invoicing' ),
1146
                'size' => 'large'
1147
            ),
1148
            'email_overdue_heading' => array(
1149
                'id'   => 'email_overdue_heading',
1150
                'name' => __( 'Email Heading', 'invoicing' ),
1151
                'desc' => __( 'Enter the main heading contained within the email notification.', 'invoicing' ),
1152
                'type' => 'text',
1153
                'std'  => __( 'Payment reminder for your invoice', 'invoicing' ),
1154
                'size' => 'large'
1155
            ),
1156
            'email_overdue_admin_bcc' => array(
1157
                'id'   => 'email_overdue_admin_bcc',
1158
                'name' => __( 'Enable Admin BCC', 'invoicing' ),
1159
                'desc' => __( 'Check if you want to send this notification email to site Admin.', 'invoicing' ),
1160
                'type' => 'checkbox',
1161
                'std'  => 1
1162
            ),
1163
            'email_overdue_body' => array(
1164
                'id'   => 'email_overdue_body',
1165
                'name' => __( 'Email Content', 'invoicing' ),
1166
                'desc' => __( 'The content of the email.', 'invoicing' ),
1167
                'type' => 'rich_editor',
1168
                'std'  => __( '<p>Hi {full_name},</p><p>This is just a friendly reminder that your invoice <a href="{invoice_link}">#{invoice_number}</a> {is_was} due on {invoice_due_date}.</p><p>The total of this invoice is {invoice_total}</p><p>To view / pay now for this invoice please use the following link: <a class="btn btn-success" href="{invoice_link}">View / Pay</a></p>', 'invoicing' ),
1169
                'class' => 'large',
1170
                'size'  => 10,
1171
            ),
1172
        ),
1173
    );
1174
1175
    return apply_filters( 'wpinv_get_emails', $emails );
1176
}
1177
1178
function wpinv_settings_emails( $settings = array() ) {
1179
    $emails = wpinv_get_emails();
1180
1181
    if ( !empty( $emails ) ) {
1182
        foreach ( $emails as $key => $email ) {
1183
            $settings[$key] = $email;
1184
        }
1185
    }
1186
1187
    return apply_filters( 'wpinv_settings_get_emails', $settings );
1188
}
1189
add_filter( 'wpinv_settings_emails', 'wpinv_settings_emails', 10, 1 );
1190
1191
function wpinv_settings_sections_emails( $settings ) {
1192
    $emails = wpinv_get_emails();
1193
1194
    if (!empty($emails)) {
1195
        foreach  ($emails as $key => $email) {
1196
            $settings[$key] = !empty( $email['email_' . $key . '_header']['name'] ) ? strip_tags( $email['email_' . $key . '_header']['name'] ) : $key;
1197
        }
1198
    }
1199
1200
    return $settings;    
1201
}
1202
add_filter( 'wpinv_settings_sections_emails', 'wpinv_settings_sections_emails', 10, 1 );
1203
1204
function wpinv_email_is_enabled( $email_type ) {
1205
    $emails = wpinv_get_emails();
1206
    $enabled = isset( $emails[$email_type] ) && wpinv_get_option( 'email_'. $email_type . '_active', 0 ) ? true : false;
1207
1208
    return apply_filters( 'wpinv_email_is_enabled', $enabled, $email_type );
1209
}
1210
1211
function wpinv_email_get_recipient( $email_type = '', $invoice_id = 0, $invoice = array() ) {
1212
    switch ( $email_type ) {
1213
        case 'new_invoice':
1214
        case 'cancelled_invoice':
1215
        case 'failed_invoice':
1216
            $recipient  = wpinv_get_admin_email();
1217
        break;
1218
        default:
1219
            $invoice    = !empty( $invoice ) && is_object( $invoice ) ? $invoice : ( $invoice_id > 0 ? wpinv_get_invoice( $invoice_id ) : NULL );
1220
            $recipient  = !empty( $invoice ) ? $invoice->get_email() : '';
1221
        break;
1222
    }
1223
1224
    return apply_filters( 'wpinv_email_recipient', $recipient, $email_type, $invoice_id, $invoice );
1225
}
1226
1227
function wpinv_email_get_subject( $email_type = '', $invoice_id = 0, $invoice = array() ) {
1228
    $subject    = wpinv_get_option( 'email_' . $email_type . '_subject' );
1229
    $subject    = __( $subject, 'invoicing' );
0 ignored issues
show
Bug introduced by
It seems like $subject can also be of type false; however, parameter $text of __() 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

1229
    $subject    = __( /** @scrutinizer ignore-type */ $subject, 'invoicing' );
Loading history...
1230
1231
    $subject    = wpinv_email_format_text( $subject, $invoice );
1232
1233
    return apply_filters( 'wpinv_email_subject', $subject, $email_type, $invoice_id, $invoice );
1234
}
1235
1236
function wpinv_email_get_heading( $email_type = '', $invoice_id = 0, $invoice = array() ) {
1237
    $email_heading = wpinv_get_option( 'email_' . $email_type . '_heading' );
1238
    $email_heading = __( $email_heading, 'invoicing' );
0 ignored issues
show
Bug introduced by
It seems like $email_heading can also be of type false; however, parameter $text of __() 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

1238
    $email_heading = __( /** @scrutinizer ignore-type */ $email_heading, 'invoicing' );
Loading history...
1239
1240
    $email_heading = wpinv_email_format_text( $email_heading, $invoice );
1241
1242
    return apply_filters( 'wpinv_email_heading', $email_heading, $email_type, $invoice_id, $invoice );
1243
}
1244
1245
function wpinv_email_get_content( $email_type = '', $invoice_id = 0, $invoice = array() ) {
1246
    $content    = wpinv_get_option( 'email_' . $email_type . '_body' );
1247
    $content    = __( $content, 'invoicing' );
0 ignored issues
show
Bug introduced by
It seems like $content can also be of type false; however, parameter $text of __() 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

1247
    $content    = __( /** @scrutinizer ignore-type */ $content, 'invoicing' );
Loading history...
1248
1249
    $content    = wpinv_email_format_text( $content, $invoice );
1250
1251
    return apply_filters( 'wpinv_email_content', $content, $email_type, $invoice_id, $invoice );
1252
}
1253
1254
function wpinv_email_get_headers( $email_type = '', $invoice_id = 0, $invoice = array() ) {
1255
    $from_name = wpinv_mail_get_from_address();
1256
    $from_email = wpinv_mail_get_from_address();
1257
    
1258
    $invoice    = !empty( $invoice ) && is_object( $invoice ) ? $invoice : ( $invoice_id > 0 ? wpinv_get_invoice( $invoice_id ) : NULL );
1259
    
1260
    $headers    = "From: " . stripslashes_deep( html_entity_decode( $from_name, ENT_COMPAT, 'UTF-8' ) ) . " <$from_email>\r\n";
1261
    $headers    .= "Reply-To: ". $from_email . "\r\n";
1262
    $headers    .= "Content-Type: " . wpinv_mail_get_content_type() . "\r\n";
1263
    
1264
    return apply_filters( 'wpinv_email_headers', $headers, $email_type, $invoice_id, $invoice );
1265
}
1266
1267
function wpinv_email_get_attachments( $email_type = '', $invoice_id = 0, $invoice = array() ) {
1268
    $attachments = array();
1269
    
1270
    return apply_filters( 'wpinv_email_attachments', $attachments, $email_type, $invoice_id, $invoice );
1271
}
1272
1273
function wpinv_email_format_text( $content, $invoice ) {
1274
    $replace_array = array(
1275
        '{site_title}'      => wpinv_get_blogname(),
1276
        '{date}'            => date_i18n( get_option( 'date_format' ), (int) 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_i18n() 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

1276
        '{date}'            => date_i18n( /** @scrutinizer ignore-type */ get_option( 'date_format' ), (int) current_time( 'timestamp' ) ),
Loading history...
1277
    );
1278
    
1279
    if ( !empty( $invoice->ID ) ) {
1280
        $replace_array = array_merge(
1281
            $replace_array, 
1282
            array(
1283
                '{name}'            => $invoice->get_user_full_name(),
1284
                '{full_name}'       => $invoice->get_user_full_name(),
1285
                '{first_name}'      => $invoice->get_first_name(),
1286
                '{last_name}'       => $invoice->get_last_name(),
1287
                '{email}'           => $invoice->get_email(),
1288
                '{invoice_number}'  => $invoice->get_number(),
1289
                '{invoice_total}'   => $invoice->get_total( true ),
1290
                '{invoice_link}'    => $invoice->get_view_url( true ),
1291
                '{invoice_pay_link}'=> $invoice->get_view_url( true ),
1292
                '{invoice_date}'    => $invoice->get_invoice_date( true ),
1293
                '{invoice_due_date}'=> $invoice->get_due_date( true ),
1294
                '{invoice_quote}'   => $invoice->get_invoice_quote_type( $invoice->ID ),
1295
                '{invoice_label}'   => $invoice->get_invoice_quote_type( $invoice->ID ),
1296
                '{is_was}'          => strtotime( $invoice->get_due_date() ) < strtotime( date_i18n( 'Y-m-d' ) ) ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ),
1297
            )
1298
        );
1299
    }
1300
1301
    $replace_array = apply_filters( 'wpinv_email_format_text', $replace_array, $content, $invoice );
1302
1303
    foreach ( $replace_array as $key => $value ) {
1304
        $content = str_replace( $key, $value, $content );
1305
    }
1306
1307
    return apply_filters( 'wpinv_email_content_replace', $content );
1308
}
1309
1310
function wpinv_email_style_body( $content ) {
1311
    // make sure we only inline CSS for html emails
1312
    if ( in_array( wpinv_mail_get_content_type(), array( 'text/html', 'multipart/alternative' ) ) && class_exists( 'DOMDocument' ) ) {
1313
        ob_start();
1314
        wpinv_get_template( 'emails/wpinv-email-styles.php' );
1315
        $css = apply_filters( 'wpinv_email_styles', ob_get_clean() );
1316
1317
        // apply CSS styles inline for picky email clients
1318
        try {
1319
            $emogrifier = new Emogrifier( $content, $css );
1320
            $content    = $emogrifier->emogrify();
1321
        } catch ( Exception $e ) {
1322
            wpinv_error_log( $e->getMessage(), 'emogrifier' );
1323
        }
1324
    }
1325
    return $content;
1326
}
1327
1328
function wpinv_email_header( $email_heading = '', $invoice = array(), $email_type = '', $sent_to_admin = false ) {
1329
    wpinv_get_template( 'emails/wpinv-email-header.php', array( 'email_heading' => $email_heading, 'invoice' => $invoice, 'email_type' => $email_type, 'sent_to_admin' => $sent_to_admin ) );
1330
}
1331
1332
/**
1333
 * Get the email footer.
1334
 */
1335
function wpinv_email_footer( $invoice = array(), $email_type = '', $sent_to_admin = false ) {
1336
    wpinv_get_template( 'emails/wpinv-email-footer.php', array( 'invoice' => $invoice, 'email_type' => $email_type, 'sent_to_admin' => $sent_to_admin ) );
1337
}
1338
1339
function wpinv_email_wrap_message( $message ) {
1340
    // Buffer
1341
    ob_start();
1342
1343
    do_action( 'wpinv_email_header' );
1344
1345
    echo wpautop( wptexturize( $message ) );
1346
1347
    do_action( 'wpinv_email_footer' );
1348
1349
    // Get contents
1350
    $message = ob_get_clean();
1351
1352
    return $message;
1353
}
1354
1355
function wpinv_email_invoice_details( $invoice, $email_type = '', $sent_to_admin = false ) {
1356
    wpinv_get_template( 'emails/wpinv-email-invoice-details.php', array( 'invoice' => $invoice, 'email_type' => $email_type, 'sent_to_admin' => $sent_to_admin ) );
1357
}
1358
1359
function wpinv_email_invoice_items( $invoice, $email_type = '', $sent_to_admin = false ) {
1360
    wpinv_get_template( 'emails/wpinv-email-invoice-items.php', array( 'invoice' => $invoice, 'email_type' => $email_type, 'sent_to_admin' => $sent_to_admin ) );
1361
}
1362
1363
function wpinv_email_billing_details( $invoice, $email_type = '', $sent_to_admin = false ) {
1364
    wpinv_get_template( 'emails/wpinv-email-billing-details.php', array( 'invoice' => $invoice, 'email_type' => $email_type, 'sent_to_admin' => $sent_to_admin ) );
1365
}
1366
1367
function wpinv_send_customer_invoice( $data = array() ) {
1368
    $invoice_id = !empty( $data['invoice_id'] ) ? absint( $data['invoice_id'] ) : NULL;
1369
1370
    if ( empty( $invoice_id ) ) {
1371
        return;
1372
    }
1373
1374
    if ( !wpinv_current_user_can_manage_invoicing() ) {
1375
        wp_die( __( 'You do not have permission to send invoice notification', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
1376
    }
1377
    
1378
    $sent = wpinv_user_invoice_notification( $invoice_id );
1379
1380
    if ( -1 === $sent ) {
1381
        $status = 'email_disabled';
1382
    } elseif ( $sent ) {
1383
        $status = 'email_sent';
1384
    } else {
1385
        $status = 'email_fail';
1386
    }
1387
1388
    $redirect = add_query_arg( array( 'wpinv-message' => $status, 'wpi_action' => false, 'invoice_id' => false ) );
1389
    wp_redirect( $redirect );
1390
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1391
}
1392
add_action( 'wpinv_send_invoice', 'wpinv_send_customer_invoice' );
1393
1394
function wpinv_send_overdue_reminder( $data = array() ) {
1395
    $invoice_id = !empty( $data['invoice_id'] ) ? absint( $data['invoice_id'] ) : NULL;
1396
1397
    if ( empty( $invoice_id ) ) {
1398
        return;
1399
    }
1400
1401
    if ( !wpinv_current_user_can_manage_invoicing() ) {
1402
        wp_die( __( 'You do not have permission to send reminder notification', 'invoicing' ), __( 'Error', 'invoicing' ), array( 'response' => 403 ) );
1403
    }
1404
1405
    $sent = wpinv_send_payment_reminder_notification( $invoice_id );
1406
    
1407
    $status = $sent ? 'email_sent' : 'email_fail';
1408
1409
    $redirect = add_query_arg( array( 'wpinv-message' => $status, 'wpi_action' => false, 'invoice_id' => false ) );
1410
    wp_redirect( $redirect );
1411
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
1412
}
1413
add_action( 'wpinv_send_reminder', 'wpinv_send_overdue_reminder' );
1414
1415
function wpinv_send_customer_note_email( $data ) {
1416
    $invoice_id = !empty( $data['invoice_id'] ) ? absint( $data['invoice_id'] ) : NULL;
1417
1418
    if ( empty( $invoice_id ) ) {
1419
        return;
1420
    }
1421
1422
    $sent = wpinv_user_note_notification( $invoice_id, $data );
0 ignored issues
show
Unused Code introduced by
The assignment to $sent is dead and can be removed.
Loading history...
1423
}
1424
add_action( 'wpinv_new_customer_note', 'wpinv_send_customer_note_email', 10, 1 );
1425
1426
function wpinv_add_notes_to_invoice_email( $invoice, $email_type, $sent_to_admin ) {
0 ignored issues
show
Unused Code introduced by
The parameter $sent_to_admin is not used and could be removed. ( Ignorable by Annotation )

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

1426
function wpinv_add_notes_to_invoice_email( $invoice, $email_type, /** @scrutinizer ignore-unused */ $sent_to_admin ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1427
    if ( !empty( $invoice ) && $email_type == 'user_invoice' && $invoice_notes = wpinv_get_invoice_notes( $invoice->ID, true ) ) {
1428
        $date_format = get_option( 'date_format' );
1429
        $time_format = get_option( 'time_format' );
1430
        ?>
1431
        <div id="wpinv-email-notes">
1432
            <h3 class="wpinv-notes-t"><?php echo apply_filters( 'wpinv_email_invoice_notes_title', __( 'Invoice Notes', 'invoicing' ) ); ?></h3>
1433
            <ol class="wpinv-notes-lists">
1434
        <?php
1435
        foreach ( $invoice_notes as $note ) {
1436
            $note_time = strtotime( $note->comment_date );
1437
            ?>
1438
            <li class="comment wpinv-note">
1439
            <p class="wpinv-note-date meta"><?php printf( __( '%2$s at %3$s', 'invoicing' ), $note->comment_author, date_i18n( $date_format, $note_time ), date_i18n( $time_format, $note_time ), $note_time ); ?></p>
0 ignored issues
show
Bug introduced by
It seems like $date_format can also be of type false; however, parameter $format of date_i18n() 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

1439
            <p class="wpinv-note-date meta"><?php printf( __( '%2$s at %3$s', 'invoicing' ), $note->comment_author, date_i18n( /** @scrutinizer ignore-type */ $date_format, $note_time ), date_i18n( $time_format, $note_time ), $note_time ); ?></p>
Loading history...
1440
            <div class="wpinv-note-desc description"><?php echo wpautop( wptexturize( $note->comment_content ) ); ?></div>
1441
            </li>
1442
            <?php
1443
        }
1444
        ?>  </ol>
1445
        </div>
1446
        <?php
1447
    }
1448
}
1449
add_action( 'wpinv_email_billing_details', 'wpinv_add_notes_to_invoice_email', 10, 3 );
1450
1451
/**
1452
 * Sends renewal reminders.
1453
 */
1454
function wpinv_email_renewal_reminders() {
1455
    global $wpdb;
1456
1457
    if ( ! wpinv_get_option( 'email_pre_payment_active' ) ) {
1458
        return;
1459
    }
1460
1461
    $reminder_days = wpinv_get_option( 'email_pre_payment_reminder_days' );
1462
1463
    if ( empty( $reminder_days ) ) {
1464
        return;
1465
    }
1466
1467
    // How many days before renewal should we send this email?
1468
    $reminder_days = array_unique( array_map( 'absint', array_filter( wpinv_parse_list( $reminder_days ) ) ) );
1469
    if ( empty( $reminder_days ) ) {
1470
        return;
1471
    }
1472
1473
    if ( 1 == count( $reminder_days ) ) {
1474
        $days  = $reminder_days[0];
1475
        $date  = date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) );
1476
        $where = $wpdb->prepare( "DATE(expiration)=%s", $date );
1477
    } else {
1478
        $in    = array();
1479
1480
        foreach ( $reminder_days as $days ) {
1481
            $date  = date( 'Y-m-d', strtotime( "+$days days", current_time( 'timestamp' ) ) );
1482
            $in[]  = $wpdb->prepare( "%s", $date );
1483
        }
1484
1485
        $in    = implode( ',', $in );
1486
        $where = "DATE(expiration) IN ($in)";
1487
    }
1488
1489
    // Fetch subscriptions to send out reminders for.
1490
    $table = $wpdb->prefix . 'wpinv_subscriptions';
1491
1492
    // Fetch invoices.
1493
	$subscriptions  = $wpdb->get_results(
1494
		"SELECT parent_payment_id, product_id, expiration FROM $table
1495
		WHERE
1496
            $where
1497
            AND status IN ( 'trialling', 'active' )");
1498
1499
    foreach ( $subscriptions as $subscription ) {
1500
1501
        // Skip packages.
1502
        if ( get_post_meta( $subscription->product_id, '_wpinv_type', true ) == 'package' ) {
1503
            continue;
1504
        }
1505
1506
        wpinv_send_pre_payment_reminder_notification( $subscription->parent_payment_id, $subscription->expiration );
1507
    }
1508
1509
}
1510
1511
function wpinv_email_payment_reminders() {
1512
    global $wpi_auto_reminder, $wpdb;
1513
1514
    if ( ! wpinv_get_option( 'email_overdue_active' ) ) {
1515
        return;
1516
    }
1517
1518
    if ( $reminder_days = wpinv_get_option( 'email_due_reminder_days' ) ) {
1519
1520
        // Get a list of all reminder dates.
1521
        $reminder_days  = is_array( $reminder_days ) ? array_values( $reminder_days ) : array();
1522
1523
        // Ensure we have integers.
1524
        $reminder_days  = array_unique( array_map( 'absint', $reminder_days ) );
1525
1526
        // Abort if non is selected.
1527
        if ( empty( $reminder_days ) ) {
1528
            return;
1529
        }
1530
1531
        // Fetch the max reminder day.
1532
        $max_date = max( $reminder_days );
1533
1534
        // Todays date.
1535
        $today = date( 'Y-m-d', current_time( 'timestamp' ) );
1536
1537
        if ( empty( $max_date ) ) {
1538
            $where = $wpdb->prepare( "DATE(invoices.due_date)=%s", $today );
1539
        } else if ( 1 == count( $reminder_days ) ) {
1540
            $days  = $reminder_days[0];
1541
            $date  = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) );
1542
            $where = $wpdb->prepare( "DATE(invoices.due_date)=%s", $date );
1543
        } else {
1544
            $in    = array();
1545
1546
            foreach ( $reminder_days as $days ) {
1547
                $date  = date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) );
1548
                $in[]  = $wpdb->prepare( "%s", $date );
1549
            }
1550
1551
            $in    = implode( ',', $in );
1552
            $where = "DATE(invoices.due_date) IN ($in)";
1553
        }
1554
1555
        // Invoices table.
1556
        $table = $wpdb->prefix . 'getpaid_invoices';
1557
1558
        // Fetch invoices.
1559
		$invoices  = $wpdb->get_col(
1560
			"SELECT posts.ID FROM $wpdb->posts as posts
1561
			LEFT JOIN $table as invoices ON invoices.post_id = posts.ID
1562
			WHERE
1563
                $where 
1564
				AND posts.post_type = 'wpi_invoice'
1565
                AND posts.post_status = 'wpi-pending'");
1566
1567
        $wpi_auto_reminder  = true;
1568
1569
        foreach ( $invoices as $invoice ) {
1570
1571
            if ( 'payment_form' != get_post_meta( $invoice, 'wpinv_created_via', true ) ) {
1572
                do_action( 'wpinv_send_payment_reminder_notification', $invoice );
1573
            }
1574
1575
        }
1576
1577
        $wpi_auto_reminder  = false;
1578
    }
1579
}
1580
1581
/**
1582
 * Sends an upcoming renewal notification.
1583
 */
1584
function wpinv_send_pre_payment_reminder_notification( $invoice_id, $renewal_date ) {
1585
1586
    $email_type = 'pre_payment';
1587
    if ( ! wpinv_email_is_enabled( $email_type ) ) {
1588
        return false;
1589
    }
1590
1591
    $invoice    = wpinv_get_invoice( $invoice_id );
1592
    if ( empty( $invoice ) ) {
1593
        return false;
1594
    }
1595
1596
    $recipient  = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
1597
    if ( ! is_email( $recipient ) ) {
1598
        return false;
1599
    }
1600
1601
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
1602
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
1603
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
1604
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
1605
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
1606
1607
    $renewal_date = date_i18n( 'Y-m-d', strtotime( $renewal_date ) );
1608
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
1609
            'invoice'       => $invoice,
1610
            'email_type'    => $email_type,
1611
            'email_heading' => str_replace( '{subscription_renewal_date}', $renewal_date, $email_heading ),
1612
            'sent_to_admin' => false,
1613
            'plain_text'    => false,
1614
            'message_body'  => str_replace( '{subscription_renewal_date}', $renewal_date, $message_body )
1615
        ) );
1616
1617
    $content        = wpinv_email_format_text( $content, $invoice );
1618
1619
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
1620
1621
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
1622
        $recipient  = wpinv_get_admin_email();
1623
        $subject    .= ' - ADMIN BCC COPY';
1624
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
1625
    }
1626
1627
    return $sent;
1628
1629
}
1630
1631
function wpinv_send_payment_reminder_notification( $invoice_id ) {
1632
    $email_type = 'overdue';
1633
    if ( !wpinv_email_is_enabled( $email_type ) ) {
1634
        return false;
1635
    }
1636
1637
    $invoice    = wpinv_get_invoice( $invoice_id );
1638
    if ( empty( $invoice ) ) {
1639
        return false;
1640
    }
1641
1642
    if ( !$invoice->needs_payment() ) {
1643
        return false;
1644
    }
1645
1646
    $recipient  = wpinv_email_get_recipient( $email_type, $invoice_id, $invoice );
1647
    if ( !is_email( $recipient ) ) {
1648
        return false;
1649
    }
1650
1651
    do_action( 'wpinv_pre_send_invoice_notification', $invoice, $email_type );
1652
1653
    $subject        = wpinv_email_get_subject( $email_type, $invoice_id, $invoice );
1654
    $email_heading  = wpinv_email_get_heading( $email_type, $invoice_id, $invoice );
1655
    $headers        = wpinv_email_get_headers( $email_type, $invoice_id, $invoice );
1656
    $message_body   = wpinv_email_get_content( $email_type, $invoice_id, $invoice );
1657
    $attachments    = wpinv_email_get_attachments( $email_type, $invoice_id, $invoice );
1658
1659
    $content        = wpinv_get_template_html( 'emails/wpinv-email-' . $email_type . '.php', array(
1660
            'invoice'       => $invoice,
1661
            'email_type'    => $email_type,
1662
            'email_heading' => $email_heading,
1663
            'sent_to_admin' => false,
1664
            'plain_text'    => false,
1665
            'message_body'  => $message_body
1666
        ) );
1667
1668
    $content        = wpinv_email_format_text( $content, $invoice );
1669
1670
    $sent = wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
1671
    if ( $sent ) {
0 ignored issues
show
introduced by
The condition $sent is always true.
Loading history...
1672
        do_action( 'wpinv_payment_reminder_sent', $invoice_id, $invoice );
1673
    }
1674
1675
    if ( wpinv_mail_admin_bcc_active( $email_type ) ) {
1676
        $recipient  = wpinv_get_admin_email();
1677
        $subject    .= ' - ADMIN BCC COPY';
1678
        wpinv_mail_send( $recipient, $subject, $content, $headers, $attachments );
1679
    }
1680
1681
    do_action( 'wpinv_post_send_invoice_notification', $invoice, $email_type );
1682
1683
    return $sent;
1684
}
1685
add_action( 'wpinv_send_payment_reminder_notification', 'wpinv_send_payment_reminder_notification', 10, 1 );
1686
1687
function wpinv_payment_reminder_sent( $invoice_id, $invoice ) {
1688
    global $wpi_auto_reminder;
1689
1690
    $sent = get_post_meta( $invoice_id, '_wpinv_reminder_sent', true );
1691
1692
    if ( empty( $sent ) ) {
1693
        $sent = array();
1694
    }
1695
    $sent[] = date_i18n( 'Y-m-d' );
1696
1697
    update_post_meta( $invoice_id, '_wpinv_reminder_sent', $sent );
1698
1699
    if ( $wpi_auto_reminder ) { // Auto reminder note.
1700
        $note = __( 'Automated reminder sent to the user.', 'invoicing' );
1701
        $invoice->add_note( $note, false, false, true );
1702
    } else { // Menual reminder note.
1703
        $note = __( 'Manual reminder sent to the user.', 'invoicing' );
1704
        $invoice->add_note( $note );
1705
    }
1706
}
1707
add_action( 'wpinv_payment_reminder_sent', 'wpinv_payment_reminder_sent', 10, 2 );
1708
1709
function wpinv_invoice_notification_set_locale( $invoice, $email_type, $site = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $email_type is not used and could be removed. ( Ignorable by Annotation )

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

1709
function wpinv_invoice_notification_set_locale( $invoice, /** @scrutinizer ignore-unused */ $email_type, $site = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1710
    if ( empty( $invoice ) ) {
1711
        return;
1712
    }
1713
1714
    if ( is_int( $invoice ) ) {
1715
        $invoice = wpinv_get_invoice( $invoice );
1716
    }
1717
1718
    if ( ! empty( $invoice ) && is_object( $invoice ) ) {
1719
        if ( ! $site && function_exists( 'get_user_locale' ) ) {
1720
            $locale = get_user_locale( $invoice->get_user_id() );
1721
        } else {
1722
            $locale = get_locale();
1723
        }
1724
1725
        wpinv_switch_to_locale( $locale );
1726
    }
1727
}
1728
add_action( 'wpinv_pre_send_invoice_notification', 'wpinv_invoice_notification_set_locale', 10, 3 );
1729
1730
function wpinv_invoice_notification_restore_locale( $invoice, $email_type, $site = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $email_type is not used and could be removed. ( Ignorable by Annotation )

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

1730
function wpinv_invoice_notification_restore_locale( $invoice, /** @scrutinizer ignore-unused */ $email_type, $site = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $site is not used and could be removed. ( Ignorable by Annotation )

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

1730
function wpinv_invoice_notification_restore_locale( $invoice, $email_type, /** @scrutinizer ignore-unused */ $site = false ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1731
    if ( empty( $invoice ) ) {
1732
        return;
1733
    }
1734
1735
    wpinv_restore_locale();
1736
}
1737
add_action( 'wpinv_post_send_invoice_notification', 'wpinv_invoice_notification_restore_locale', 10, 3 );
1738