Passed
Push — master ( 73f2f1...e7f679 )
by Brian
04:31
created
includes/wpinv-address-functions.php 1 patch
Indentation   +87 added lines, -87 removed lines patch added patch discarded remove patch
@@ -13,9 +13,9 @@  discard block
 block discarded – undo
13 13
 
14 14
 
15 15
 function wpinv_get_default_country() {
16
-	$country = wpinv_get_option( 'default_country', 'UK' );
16
+    $country = wpinv_get_option( 'default_country', 'UK' );
17 17
 
18
-	return apply_filters( 'wpinv_default_country', $country );
18
+    return apply_filters( 'wpinv_default_country', $country );
19 19
 }
20 20
 
21 21
 /**
@@ -36,7 +36,7 @@  discard block
 block discarded – undo
36 36
  */
37 37
 function wpinv_sanitize_country( $country ) {
38 38
 
39
-	// Enure the country is specified
39
+    // Enure the country is specified
40 40
     if ( empty( $country ) ) {
41 41
         $country = wpinv_get_default_country();
42 42
     }
@@ -66,9 +66,9 @@  discard block
 block discarded – undo
66 66
 }
67 67
 
68 68
 function wpinv_get_default_state() {
69
-	$state = wpinv_get_option( 'default_state', '' );
69
+    $state = wpinv_get_option( 'default_state', '' );
70 70
 
71
-	return apply_filters( 'wpinv_default_state', $state );
71
+    return apply_filters( 'wpinv_default_state', $state );
72 72
 }
73 73
 
74 74
 function wpinv_state_name( $state_code = '', $country_code = '' ) {
@@ -299,11 +299,11 @@  discard block
 block discarded – undo
299 299
 
300 300
     $country = wpinv_sanitize_country( $country );
301 301
 
302
-	foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) {
303
-		if ( false !== array_search( $country, $countries, true ) ) {
304
-			return $continent_code;
305
-		}
306
-	}
302
+    foreach ( wpinv_get_continents( 'countries' ) as $continent_code => $countries ) {
303
+        if ( false !== array_search( $country, $countries, true ) ) {
304
+            return $continent_code;
305
+        }
306
+    }
307 307
 
308 308
     return '';
309 309
 
@@ -595,31 +595,31 @@  discard block
 block discarded – undo
595 595
 }
596 596
 
597 597
 function wpinv_get_states_field() {
598
-	if ( empty( $_POST['country'] ) ) {
599
-		$_POST['country'] = wpinv_get_default_country();
600
-	}
601
-	$states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) );
598
+    if ( empty( $_POST['country'] ) ) {
599
+        $_POST['country'] = wpinv_get_default_country();
600
+    }
601
+    $states = wpinv_get_country_states( sanitize_text_field( $_POST['country'] ) );
602 602
 
603
-	if ( ! empty( $states ) ) {
604
-		$sanitized_field_name = sanitize_text_field( $_POST['field_name'] );
603
+    if ( ! empty( $states ) ) {
604
+        $sanitized_field_name = sanitize_text_field( $_POST['field_name'] );
605 605
 
606 606
         $class  = isset( $_POST['class'] ) ? esc_attr( sanitize_text_field( $_POST['class'] ) ) : '';
607 607
         $class .= " $sanitized_field_name getpaid_js_field-state custom-select wpinv-select wpi_select2";
608 608
 
609 609
         $args  = array(
610
-			'name'             => $sanitized_field_name,
611
-			'id'               => $sanitized_field_name,
612
-			'class'            => implode( ' ', array_unique( explode( ' ', $class ) ) ),
613
-			'options'          => array_merge( array( '' => '' ), $states ),
614
-			'show_option_all'  => false,
615
-			'show_option_none' => false,
616
-		);
617
-
618
-		wpinv_html_select( $args );
619
-
620
-	} else {
621
-		echo 'nostates';
622
-	}
610
+            'name'             => $sanitized_field_name,
611
+            'id'               => $sanitized_field_name,
612
+            'class'            => implode( ' ', array_unique( explode( ' ', $class ) ) ),
613
+            'options'          => array_merge( array( '' => '' ), $states ),
614
+            'show_option_all'  => false,
615
+            'show_option_none' => false,
616
+        );
617
+
618
+        wpinv_html_select( $args );
619
+
620
+    } else {
621
+        echo 'nostates';
622
+    }
623 623
 
624 624
 }
625 625
 
@@ -638,47 +638,47 @@  discard block
 block discarded – undo
638 638
  */
639 639
 function wpinv_get_address_formats() {
640 640
 
641
-		return apply_filters(
641
+        return apply_filters(
642 642
             'wpinv_localisation_address_formats',
643
-			array(
644
-				'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}",
645
-				'AU'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}",
646
-				'AT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
647
-				'BE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
648
-				'CA'      => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}}  {{zip}}\n{{country}}",
649
-				'CH'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
650
-				'CL'      => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}",
651
-				'CN'      => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}",
652
-				'CZ'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
653
-				'DE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
654
-				'EE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
655
-				'FI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
656
-				'DK'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
657
-				'FR'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}",
658
-				'HK'      => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}",
659
-				'HU'      => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}",
660
-				'IN'      => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}",
661
-				'IS'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
662
-				'IT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}",
663
-				'JP'      => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}",
664
-				'TW'      => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}",
665
-				'LI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
666
-				'NL'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
667
-				'NZ'      => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}",
668
-				'NO'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
669
-				'PL'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
670
-				'PT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
671
-				'SK'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
672
-				'RS'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
673
-				'SI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
674
-				'ES'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}",
675
-				'SE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
676
-				'TR'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}",
677
-				'UG'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}",
678
-				'US'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}",
679
-				'VN'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}",
680
-			)
681
-		);
643
+            array(
644
+                'default' => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}\n{{zip}}\n{{country}}",
645
+                'AU'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}} {{zip}}\n{{country}}",
646
+                'AT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
647
+                'BE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
648
+                'CA'      => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{state_code}}  {{zip}}\n{{country}}",
649
+                'CH'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
650
+                'CL'      => "{{company}}\n{{name}}\n{{address}}\n{{state}}\n{{zip}} {{city}}\n{{country}}",
651
+                'CN'      => "{{country}} {{zip}}\n{{state}}, {{city}}, {{address}}\n{{company}}\n{{name}}",
652
+                'CZ'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
653
+                'DE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
654
+                'EE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
655
+                'FI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
656
+                'DK'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
657
+                'FR'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city_upper}}\n{{country}}",
658
+                'HK'      => "{{company}}\n{{first_name}} {{last_name_upper}}\n{{address}}\n{{city_upper}}\n{{state_upper}}\n{{country}}",
659
+                'HU'      => "{{name}}\n{{company}}\n{{city}}\n{{address}}\n{{zip}}\n{{country}}",
660
+                'IN'      => "{{company}}\n{{name}}\n{{address}}\n{{city}} {{zip}}\n{{state}}, {{country}}",
661
+                'IS'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
662
+                'IT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}}\n{{city}}\n{{state_upper}}\n{{country}}",
663
+                'JP'      => "{{zip}}\n{{state}} {{city}} {{address}}\n{{company}}\n{{last_name}} {{first_name}}\n{{country}}",
664
+                'TW'      => "{{company}}\n{{last_name}} {{first_name}}\n{{address}}\n{{state}}, {{city}} {{zip}}\n{{country}}",
665
+                'LI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
666
+                'NL'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
667
+                'NZ'      => "{{name}}\n{{company}}\n{{address}}\n{{city}} {{zip}}\n{{country}}",
668
+                'NO'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
669
+                'PL'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
670
+                'PT'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
671
+                'SK'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
672
+                'RS'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
673
+                'SI'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
674
+                'ES'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}}\n{{state}}\n{{country}}",
675
+                'SE'      => "{{company}}\n{{name}}\n{{address}}\n{{zip}} {{city}}\n{{country}}",
676
+                'TR'      => "{{name}}\n{{company}}\n{{address}}\n{{zip}} {{city}} {{state}}\n{{country}}",
677
+                'UG'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{state}}, {{country}}",
678
+                'US'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}, {{state_code}} {{zip}}\n{{country}}",
679
+                'VN'      => "{{name}}\n{{company}}\n{{address}}\n{{city}}\n{{country}}",
680
+            )
681
+        );
682 682
 }
683 683
 
684 684
 /**
@@ -695,21 +695,21 @@  discard block
 block discarded – undo
695 695
     }
696 696
 
697 697
     // Get all formats.
698
-	$formats = wpinv_get_address_formats();
698
+    $formats = wpinv_get_address_formats();
699 699
 
700
-	// Get format for the specified country.
701
-	$format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
700
+    // Get format for the specified country.
701
+    $format = ( $country && isset( $formats[ $country ] ) ) ? $formats[ $country ] : $formats['default'];
702 702
 
703 703
     /**
704
-	 * Filters the address format to use on Invoices.
704
+     * Filters the address format to use on Invoices.
705 705
      *
706 706
      * New lines will be replaced by a `br` element. Double new lines will be replaced by a paragraph. HTML tags are allowed.
707
-	 *
708
-	 * @since 1.0.13
709
-	 *
710
-	 * @param string $format  The address format to use.
707
+     *
708
+     * @since 1.0.13
709
+     *
710
+     * @param string $format  The address format to use.
711 711
      * @param string $country The country who's address format is being retrieved.
712
-	 */
712
+     */
713 713
     return apply_filters( 'wpinv_get_full_address_format', $format, $country );
714 714
 }
715 715
 
@@ -730,8 +730,8 @@  discard block
 block discarded – undo
730 730
         'country'    => '',
731 731
         'zip'        => '',
732 732
         'first_name' => '',
733
-		'last_name'  => '',
734
-		'company'    => '',
733
+        'last_name'  => '',
734
+        'company'    => '',
735 735
     );
736 736
 
737 737
     $args    = map_deep( wp_parse_args( array_filter( $billing_details ), $default_args ), 'trim' );
@@ -752,14 +752,14 @@  discard block
 block discarded – undo
752 752
     $args['country_code'] = $country;
753 753
 
754 754
     /**
755
-	 * Filters the address format replacements to use on Invoices.
755
+     * Filters the address format replacements to use on Invoices.
756
+     *
757
+     *
758
+     * @since 1.0.13
756 759
      *
757
-	 *
758
-	 * @since 1.0.13
759
-	 *
760
-	 * @param array $replacements  The address replacements to use.
760
+     * @param array $replacements  The address replacements to use.
761 761
      * @param array $billing_details  The billing details to use.
762
-	 */
762
+     */
763 763
     $replacements = apply_filters( 'wpinv_get_invoice_address_replacements', $args, $billing_details );
764 764
 
765 765
     $return = array();
@@ -782,5 +782,5 @@  discard block
 block discarded – undo
782 782
  * @return string
783 783
  */
784 784
 function wpinv_trim_formatted_address_line( $line ) {
785
-	return trim( $line, ', ' );
785
+    return trim( $line, ', ' );
786 786
 }
Please login to merge, or discard this patch.
includes/class-getpaid-invoice-notification-emails.php 1 patch
Indentation   +470 added lines, -470 removed lines patch added patch discarded remove patch
@@ -12,493 +12,493 @@
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Invoice_Notification_Emails {
14 14
 
15
-	/**
16
-	 * The array of invoice email actions.
17
-	 *
18
-	 * @param array
19
-	 */
20
-	public $invoice_actions;
21
-
22
-	/**
23
-	 * Class constructor
24
-	 *
25
-	 */
26
-	public function __construct() {
27
-
28
-		$this->invoice_actions = apply_filters(
29
-			'getpaid_notification_email_invoice_triggers',
30
-			array(
31
-				'getpaid_new_invoice'                   => array( 'new_invoice', 'user_invoice' ),
32
-				'getpaid_invoice_status_wpi-cancelled'  => 'cancelled_invoice',
33
-				'getpaid_invoice_status_wpi-failed'     => 'failed_invoice',
34
-				'getpaid_invoice_status_wpi-onhold'     => 'onhold_invoice',
35
-				'getpaid_invoice_status_wpi-processing' => 'processing_invoice',
36
-				'getpaid_invoice_status_publish'        => 'completed_invoice',
37
-				'getpaid_invoice_status_wpi-renewal'    => 'completed_invoice',
38
-				'getpaid_invoice_status_wpi-refunded'   => 'refunded_invoice',
39
-				'getpaid_new_customer_note'             => 'user_note',
40
-				'getpaid_daily_maintenance'             => 'overdue',
41
-			)
42
-		);
43
-
44
-		$this->init_hooks();
45
-
46
-	}
47
-
48
-	/**
49
-	 * Registers email hooks.
50
-	 */
51
-	public function init_hooks() {
52
-
53
-		add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 );
54
-		add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 );
55
-
56
-		foreach ( $this->invoice_actions as $hook => $email_type ) {
57
-			$this->init_email_type_hook( $hook, $email_type );
58
-		}
59
-	}
60
-
61
-	/**
62
-	 * Registers an email hook for an invoice action.
63
-	 *
64
-	 * @param string $hook
65
-	 * @param string|array $email_type
66
-	 */
67
-	public function init_email_type_hook( $hook, $email_type ) {
68
-
69
-		$email_type = wpinv_parse_list( $email_type );
70
-
71
-		foreach ( $email_type as $type ) {
72
-
73
-			$email = new GetPaid_Notification_Email( $type );
74
-
75
-			// Abort if it is not active.
76
-			if ( ! $email->is_active() ) {
77
-				continue;
78
-			}
79
-
80
-			if ( method_exists( $this, $type ) ) {
81
-				add_action( $hook, array( $this, $type ), 100, 2 );
82
-				continue;
83
-			}
84
-
85
-			do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook );
86
-		}
87
-
88
-	}
89
-
90
-	/**
91
-	 * Filters invoice merge tags.
92
-	 *
93
-	 * @param array $merge_tags
94
-	 * @param mixed|WPInv_Invoice|WPInv_Subscription $object
95
-	 */
96
-	public function invoice_merge_tags( $merge_tags, $object ) {
97
-
98
-		if ( is_a( $object, 'WPInv_Invoice' ) ) {
99
-			return array_merge(
100
-				$merge_tags,
101
-				$this->get_invoice_merge_tags( $object )
102
-			);
103
-		}
104
-
105
-		if ( is_a( $object, 'WPInv_Subscription' ) ) {
106
-			return array_merge(
107
-				$merge_tags,
108
-				$this->get_invoice_merge_tags( $object->get_parent_payment() )
109
-			);
110
-		}
111
-
112
-		return $merge_tags;
113
-
114
-	}
115
-
116
-	/**
117
-	 * Generates invoice merge tags.
118
-	 *
119
-	 * @param WPInv_Invoice $invoice
120
-	 * @return array
121
-	 */
122
-	public function get_invoice_merge_tags( $invoice ) {
123
-
124
-		// Abort if it does not exist.
125
-		if ( ! $invoice->get_id() ) {
126
-			return array();
127
-		}
128
-
129
-		$due_date   = $invoice->get_due_date();
130
-		$due_date   = empty( $due_date ) ? time() + MINUTE_IN_SECONDS : strtotime( $due_date ) + ( (int) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
131
-		$merge_tags = array(
132
-			'{name}'                 => sanitize_text_field( $invoice->get_user_full_name() ),
133
-			'{full_name}'            => sanitize_text_field( $invoice->get_user_full_name() ),
134
-			'{first_name}'           => sanitize_text_field( $invoice->get_first_name() ),
135
-			'{last_name}'            => sanitize_text_field( $invoice->get_last_name() ),
136
-			'{email}'                => sanitize_email( $invoice->get_email() ),
137
-			'{invoice_number}'       => sanitize_text_field( $invoice->get_number() ),
138
-			'{invoice_currency}'     => sanitize_text_field( $invoice->get_currency() ),
139
-			'{invoice_total}'        => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ),
140
-			'{invoice_link}'         => esc_url( $invoice->get_view_url() ),
141
-			'{invoice_pay_link}'     => esc_url( $invoice->get_checkout_payment_url() ),
142
-			'{invoice_receipt_link}' => esc_url( $invoice->get_receipt_url() ),
143
-			'{invoice_date}'         => getpaid_format_date_value( $invoice->get_date_created() ),
144
-			'{invoice_due_date}'     => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ),
145
-			'{invoice_quote}'        => sanitize_text_field( strtolower( $invoice->get_label() ) ),
146
-			'{invoice_label}'        => sanitize_text_field( ucfirst( $invoice->get_label() ) ),
147
-			'{invoice_description}'  => wp_kses_post( $invoice->get_description() ),
148
-			'{subscription_name}'    => wp_kses_post( $invoice->get_subscription_name() ),
149
-			'{is_was}'               => $due_date < time() ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ),
150
-		);
151
-
152
-		$payment_form_data = $invoice->get_meta( 'payment_form_data', true );
153
-
154
-		if ( is_array( $payment_form_data ) ) {
155
-
156
-			foreach ( $payment_form_data as $label => $value ) {
157
-
158
-				$label = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $label ) );
159
-				$value = is_array( $value ) ? implode( ', ', $value ) : $value;
160
-
161
-				if ( is_scalar( $value ) ) {
162
-					$merge_tags[ "{{$label}}" ] = wp_kses_post( $value );
163
-				}
164
-			}
165
-		}
166
-
167
-		return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice );
168
-	}
169
-
170
-	/**
171
-	 * Helper function to send an email.
172
-	 *
173
-	 * @param WPInv_Invoice $invoice
174
-	 * @param GetPaid_Notification_Email $email
175
-	 * @param string $type
176
-	 * @param string|array $recipients
177
-	 * @param array $extra_args Extra template args.
178
-	 */
179
-	public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) {
180
-
181
-		do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email );
182
-
183
-		$skip = $invoice->is_free() && wpinv_get_option( 'skip_email_free_invoice' );
184
-		if ( apply_filters( 'getpaid_skip_invoice_email', $skip, $type, $invoice ) ) {
185
-			return;
186
-		}
187
-
188
-		$mailer     = new GetPaid_Notification_Email_Sender();
189
-		$merge_tags = $email->get_merge_tags();
190
-
191
-		$result = $mailer->send(
192
-			apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ),
193
-			$email->add_merge_tags( $email->get_subject(), $merge_tags ),
194
-			$email->get_content( $merge_tags, $extra_args ),
195
-			$email->get_attachments()
196
-		);
197
-
198
-		// Maybe send a copy to the admin.
199
-		if ( $email->include_admin_bcc() ) {
200
-			$mailer->send(
201
-				wpinv_get_admin_email(),
202
-				$email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ),
203
-				$email->get_content( $merge_tags ),
204
-				$email->get_attachments()
205
-			);
206
-		}
207
-
208
-		if ( $result ) {
209
-			$invoice->add_system_note(
210
-				sprintf(
211
-					// translators: %1 is the email type, %2 is the invoice recipient.
212
-					__( 'Successfully sent %1$s notification email to %2$s.', 'invoicing' ),
213
-					sanitize_key( $type ),
214
-					$email->is_admin_email() ? __( 'admin', 'invoicing' ) : __( 'the customer', 'invoicing' )
215
-				)
216
-			);
217
-		} else {
218
-			$invoice->add_system_note(
219
-				sprintf(
220
-					// translators: %1 is the email type, %2 is the invoice recipient.
221
-					__( 'Failed sending %1$s notification email to %2$s.', 'invoicing' ),
222
-					sanitize_key( $type ),
223
-					$email->is_admin_email() ? __( 'admin', 'invoicing' ) : __( 'the customer', 'invoicing' )
224
-				)
225
-			);
226
-		}
227
-
228
-		do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email );
229
-
230
-		return $result;
231
-	}
232
-
233
-	/**
234
-	 * Also send emails to any cc users.
235
-	 *
236
-	 * @param array $recipients
237
-	 * @param GetPaid_Notification_Email $email
238
-	 */
239
-	public function filter_email_recipients( $recipients, $email ) {
240
-
241
-		if ( ! $email->is_admin_email() ) {
242
-			$cc   = $email->object->get_email_cc();
243
-			$cc_2 = get_user_meta( $email->object->get_user_id(), '_wpinv_email_cc', true );
244
-
245
-			if ( ! empty( $cc ) ) {
246
-				$cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) );
247
-				$recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) );
248
-			}
249
-
250
-			if ( ! empty( $cc_2 ) ) {
251
-				$cc_2 = array_map( 'sanitize_email', wpinv_parse_list( $cc_2 ) );
252
-				$recipients = array_filter( array_unique( array_merge( $recipients, $cc_2 ) ) );
253
-			}
254
-		}
255
-
256
-		return $recipients;
257
-
258
-	}
259
-
260
-	/**
261
-	 * Sends a new invoice notification.
262
-	 *
263
-	 * @param WPInv_Invoice $invoice
264
-	 */
265
-	public function new_invoice( $invoice ) {
266
-
267
-		// Only send this email for invoices created via the admin page.
268
-		if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_paid() || $this->is_payment_form_invoice( $invoice->get_id() ) ) {
269
-			return;
270
-		}
271
-
272
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
273
-		$recipient = wpinv_get_admin_email();
274
-
275
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
276
-
277
-	}
278
-
279
-	/**
280
-	 * Sends a cancelled invoice notification.
281
-	 *
282
-	 * @param WPInv_Invoice $invoice
283
-	 */
284
-	public function cancelled_invoice( $invoice ) {
285
-
286
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
287
-		$recipient = $invoice->get_email();
288
-
289
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
290
-	}
291
-
292
-	/**
293
-	 * Sends a failed invoice notification.
294
-	 *
295
-	 * @param WPInv_Invoice $invoice
296
-	 */
297
-	public function failed_invoice( $invoice ) {
298
-
299
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
300
-		$recipient = wpinv_get_admin_email();
301
-
302
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
303
-
304
-	}
305
-
306
-	/**
307
-	 * Sends a notification whenever an invoice is put on hold.
308
-	 *
309
-	 * @param WPInv_Invoice $invoice
310
-	 */
311
-	public function onhold_invoice( $invoice ) {
312
-
313
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
314
-		$recipient = $invoice->get_email();
315
-
316
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
317
-
318
-	}
319
-
320
-	/**
321
-	 * Sends a notification whenever an invoice is marked as processing payment.
322
-	 *
323
-	 * @param WPInv_Invoice $invoice
324
-	 */
325
-	public function processing_invoice( $invoice ) {
326
-
327
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
328
-		$recipient = $invoice->get_email();
329
-
330
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
331
-
332
-	}
333
-
334
-	/**
335
-	 * Sends a notification whenever an invoice is paid.
336
-	 *
337
-	 * @param WPInv_Invoice $invoice
338
-	 */
339
-	public function completed_invoice( $invoice ) {
340
-
341
-		// (Maybe) abort if it is a renewal invoice.
342
-		if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) {
343
-			return;
344
-		}
345
-
346
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
347
-		$recipient = $invoice->get_email();
348
-
349
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
350
-
351
-	}
15
+    /**
16
+     * The array of invoice email actions.
17
+     *
18
+     * @param array
19
+     */
20
+    public $invoice_actions;
21
+
22
+    /**
23
+     * Class constructor
24
+     *
25
+     */
26
+    public function __construct() {
27
+
28
+        $this->invoice_actions = apply_filters(
29
+            'getpaid_notification_email_invoice_triggers',
30
+            array(
31
+                'getpaid_new_invoice'                   => array( 'new_invoice', 'user_invoice' ),
32
+                'getpaid_invoice_status_wpi-cancelled'  => 'cancelled_invoice',
33
+                'getpaid_invoice_status_wpi-failed'     => 'failed_invoice',
34
+                'getpaid_invoice_status_wpi-onhold'     => 'onhold_invoice',
35
+                'getpaid_invoice_status_wpi-processing' => 'processing_invoice',
36
+                'getpaid_invoice_status_publish'        => 'completed_invoice',
37
+                'getpaid_invoice_status_wpi-renewal'    => 'completed_invoice',
38
+                'getpaid_invoice_status_wpi-refunded'   => 'refunded_invoice',
39
+                'getpaid_new_customer_note'             => 'user_note',
40
+                'getpaid_daily_maintenance'             => 'overdue',
41
+            )
42
+        );
43
+
44
+        $this->init_hooks();
45
+
46
+    }
47
+
48
+    /**
49
+     * Registers email hooks.
50
+     */
51
+    public function init_hooks() {
52
+
53
+        add_filter( 'getpaid_get_email_merge_tags', array( $this, 'invoice_merge_tags' ), 10, 2 );
54
+        add_filter( 'getpaid_invoice_email_recipients', array( $this, 'filter_email_recipients' ), 10, 2 );
55
+
56
+        foreach ( $this->invoice_actions as $hook => $email_type ) {
57
+            $this->init_email_type_hook( $hook, $email_type );
58
+        }
59
+    }
60
+
61
+    /**
62
+     * Registers an email hook for an invoice action.
63
+     *
64
+     * @param string $hook
65
+     * @param string|array $email_type
66
+     */
67
+    public function init_email_type_hook( $hook, $email_type ) {
68
+
69
+        $email_type = wpinv_parse_list( $email_type );
70
+
71
+        foreach ( $email_type as $type ) {
72
+
73
+            $email = new GetPaid_Notification_Email( $type );
74
+
75
+            // Abort if it is not active.
76
+            if ( ! $email->is_active() ) {
77
+                continue;
78
+            }
79
+
80
+            if ( method_exists( $this, $type ) ) {
81
+                add_action( $hook, array( $this, $type ), 100, 2 );
82
+                continue;
83
+            }
84
+
85
+            do_action( 'getpaid_invoice_init_email_type_hook', $type, $hook );
86
+        }
87
+
88
+    }
89
+
90
+    /**
91
+     * Filters invoice merge tags.
92
+     *
93
+     * @param array $merge_tags
94
+     * @param mixed|WPInv_Invoice|WPInv_Subscription $object
95
+     */
96
+    public function invoice_merge_tags( $merge_tags, $object ) {
97
+
98
+        if ( is_a( $object, 'WPInv_Invoice' ) ) {
99
+            return array_merge(
100
+                $merge_tags,
101
+                $this->get_invoice_merge_tags( $object )
102
+            );
103
+        }
104
+
105
+        if ( is_a( $object, 'WPInv_Subscription' ) ) {
106
+            return array_merge(
107
+                $merge_tags,
108
+                $this->get_invoice_merge_tags( $object->get_parent_payment() )
109
+            );
110
+        }
111
+
112
+        return $merge_tags;
113
+
114
+    }
115
+
116
+    /**
117
+     * Generates invoice merge tags.
118
+     *
119
+     * @param WPInv_Invoice $invoice
120
+     * @return array
121
+     */
122
+    public function get_invoice_merge_tags( $invoice ) {
123
+
124
+        // Abort if it does not exist.
125
+        if ( ! $invoice->get_id() ) {
126
+            return array();
127
+        }
128
+
129
+        $due_date   = $invoice->get_due_date();
130
+        $due_date   = empty( $due_date ) ? time() + MINUTE_IN_SECONDS : strtotime( $due_date ) + ( (int) get_option( 'gmt_offset' ) * HOUR_IN_SECONDS );
131
+        $merge_tags = array(
132
+            '{name}'                 => sanitize_text_field( $invoice->get_user_full_name() ),
133
+            '{full_name}'            => sanitize_text_field( $invoice->get_user_full_name() ),
134
+            '{first_name}'           => sanitize_text_field( $invoice->get_first_name() ),
135
+            '{last_name}'            => sanitize_text_field( $invoice->get_last_name() ),
136
+            '{email}'                => sanitize_email( $invoice->get_email() ),
137
+            '{invoice_number}'       => sanitize_text_field( $invoice->get_number() ),
138
+            '{invoice_currency}'     => sanitize_text_field( $invoice->get_currency() ),
139
+            '{invoice_total}'        => sanitize_text_field( wpinv_price( $invoice->get_total(), $invoice->get_currency() ) ),
140
+            '{invoice_link}'         => esc_url( $invoice->get_view_url() ),
141
+            '{invoice_pay_link}'     => esc_url( $invoice->get_checkout_payment_url() ),
142
+            '{invoice_receipt_link}' => esc_url( $invoice->get_receipt_url() ),
143
+            '{invoice_date}'         => getpaid_format_date_value( $invoice->get_date_created() ),
144
+            '{invoice_due_date}'     => getpaid_format_date_value( $invoice->get_due_date(), __( 'on receipt', 'invoicing' ) ),
145
+            '{invoice_quote}'        => sanitize_text_field( strtolower( $invoice->get_label() ) ),
146
+            '{invoice_label}'        => sanitize_text_field( ucfirst( $invoice->get_label() ) ),
147
+            '{invoice_description}'  => wp_kses_post( $invoice->get_description() ),
148
+            '{subscription_name}'    => wp_kses_post( $invoice->get_subscription_name() ),
149
+            '{is_was}'               => $due_date < time() ? __( 'was', 'invoicing' ) : __( 'is', 'invoicing' ),
150
+        );
151
+
152
+        $payment_form_data = $invoice->get_meta( 'payment_form_data', true );
153
+
154
+        if ( is_array( $payment_form_data ) ) {
155
+
156
+            foreach ( $payment_form_data as $label => $value ) {
157
+
158
+                $label = preg_replace( '/[^a-z0-9]+/', '_', strtolower( $label ) );
159
+                $value = is_array( $value ) ? implode( ', ', $value ) : $value;
160
+
161
+                if ( is_scalar( $value ) ) {
162
+                    $merge_tags[ "{{$label}}" ] = wp_kses_post( $value );
163
+                }
164
+            }
165
+        }
166
+
167
+        return apply_filters( 'getpaid_invoice_email_merge_tags', $merge_tags, $invoice );
168
+    }
352 169
 
353
-	/**
354
-	 * Sends a notification whenever an invoice is refunded.
355
-	 *
356
-	 * @param WPInv_Invoice $invoice
357
-	 */
358
-	public function refunded_invoice( $invoice ) {
359
-
360
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
361
-		$recipient = $invoice->get_email();
362
-
363
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
364
-
365
-	}
170
+    /**
171
+     * Helper function to send an email.
172
+     *
173
+     * @param WPInv_Invoice $invoice
174
+     * @param GetPaid_Notification_Email $email
175
+     * @param string $type
176
+     * @param string|array $recipients
177
+     * @param array $extra_args Extra template args.
178
+     */
179
+    public function send_email( $invoice, $email, $type, $recipients, $extra_args = array() ) {
366 180
 
367
-	/**
368
-	 * Notifies a user about new invoices
369
-	 *
370
-	 * @param WPInv_Invoice $invoice
371
-	 * @param bool $force
372
-	 */
373
-	public function user_invoice( $invoice, $force = false ) {
181
+        do_action( 'getpaid_before_send_invoice_notification', $type, $invoice, $email );
374 182
 
375
-		if ( ! $force && ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) {
376
-			return;
377
-		}
378
-
379
-		// Only send this email for invoices created via the admin page.
380
-		if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $invoice->is_paid() ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) {
381
-			return;
382
-		}
383
-
384
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
385
-		$recipient = $invoice->get_email();
386
-
387
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
388
-
389
-	}
390
-
391
-	/**
392
-	 * Checks if an invoice is a payment form invoice.
393
-	 *
394
-	 * @param int $invoice
395
-	 * @return bool
396
-	 */
397
-	public function is_payment_form_invoice( $invoice ) {
398
-		$created_via             = get_post_meta( $invoice, 'wpinv_created_via', true );
399
-		$is_payment_form_invoice = 'payment_form' === $created_via || 'geodirectory' === $created_via;
400
-		$is_payment_form_invoice = apply_filters( 'getpaid_invoice_notifications_is_payment_form_invoice', $is_payment_form_invoice, $invoice );
401
-		return empty( $_GET['getpaid-admin-action'] ) && $is_payment_form_invoice;
402
-	}
403
-
404
-	/**
405
-	 * Notifies admin about new invoice notes
406
-	 *
407
-	 * @param WPInv_Invoice $invoice
408
-	 * @param string $note
409
-	 */
410
-	public function user_note( $invoice, $note ) {
411
-
412
-		$email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
413
-		$recipient = $invoice->get_email();
414
-
415
-		return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) );
416
-
417
-	}
418
-
419
-	/**
420
-	 * (Force) Sends overdue notices.
421
-	 *
422
-	 * @param WPInv_Invoice $invoice
423
-	 */
424
-	public function force_send_overdue_notice( $invoice ) {
425
-		$email = new GetPaid_Notification_Email( 'overdue', $invoice );
426
-		return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() );
427
-	}
428
-
429
-	/**
430
-	 * Sends overdue notices.
431
-	 *
432
-	 * @TODO: Create an invoices query class.
433
-	 */
434
-	public function overdue() {
435
-		global $wpdb;
436
-
437
-		$email = new GetPaid_Notification_Email( __FUNCTION__ );
438
-
439
-		// Fetch reminder days.
440
-		$reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) );
441
-
442
-		// Abort if non is set.
443
-		if ( empty( $reminder_days ) ) {
444
-			return;
445
-		}
446
-
447
-		// Retrieve date query.
448
-		$date_query = $this->get_date_query( $reminder_days );
449
-
450
-		// Invoices table.
451
-		$table = $wpdb->prefix . 'getpaid_invoices';
452
-
453
-		// Fetch invoices.
454
-		$invoices  = $wpdb->get_col(
455
-			"SELECT posts.ID FROM $wpdb->posts as posts
183
+        $skip = $invoice->is_free() && wpinv_get_option( 'skip_email_free_invoice' );
184
+        if ( apply_filters( 'getpaid_skip_invoice_email', $skip, $type, $invoice ) ) {
185
+            return;
186
+        }
187
+
188
+        $mailer     = new GetPaid_Notification_Email_Sender();
189
+        $merge_tags = $email->get_merge_tags();
190
+
191
+        $result = $mailer->send(
192
+            apply_filters( 'getpaid_invoice_email_recipients', wpinv_parse_list( $recipients ), $email ),
193
+            $email->add_merge_tags( $email->get_subject(), $merge_tags ),
194
+            $email->get_content( $merge_tags, $extra_args ),
195
+            $email->get_attachments()
196
+        );
197
+
198
+        // Maybe send a copy to the admin.
199
+        if ( $email->include_admin_bcc() ) {
200
+            $mailer->send(
201
+                wpinv_get_admin_email(),
202
+                $email->add_merge_tags( $email->get_subject() . __( ' - ADMIN BCC COPY', 'invoicing' ), $merge_tags ),
203
+                $email->get_content( $merge_tags ),
204
+                $email->get_attachments()
205
+            );
206
+        }
207
+
208
+        if ( $result ) {
209
+            $invoice->add_system_note(
210
+                sprintf(
211
+                    // translators: %1 is the email type, %2 is the invoice recipient.
212
+                    __( 'Successfully sent %1$s notification email to %2$s.', 'invoicing' ),
213
+                    sanitize_key( $type ),
214
+                    $email->is_admin_email() ? __( 'admin', 'invoicing' ) : __( 'the customer', 'invoicing' )
215
+                )
216
+            );
217
+        } else {
218
+            $invoice->add_system_note(
219
+                sprintf(
220
+                    // translators: %1 is the email type, %2 is the invoice recipient.
221
+                    __( 'Failed sending %1$s notification email to %2$s.', 'invoicing' ),
222
+                    sanitize_key( $type ),
223
+                    $email->is_admin_email() ? __( 'admin', 'invoicing' ) : __( 'the customer', 'invoicing' )
224
+                )
225
+            );
226
+        }
227
+
228
+        do_action( 'getpaid_after_send_invoice_notification', $type, $invoice, $email );
229
+
230
+        return $result;
231
+    }
232
+
233
+    /**
234
+     * Also send emails to any cc users.
235
+     *
236
+     * @param array $recipients
237
+     * @param GetPaid_Notification_Email $email
238
+     */
239
+    public function filter_email_recipients( $recipients, $email ) {
240
+
241
+        if ( ! $email->is_admin_email() ) {
242
+            $cc   = $email->object->get_email_cc();
243
+            $cc_2 = get_user_meta( $email->object->get_user_id(), '_wpinv_email_cc', true );
244
+
245
+            if ( ! empty( $cc ) ) {
246
+                $cc = array_map( 'sanitize_email', wpinv_parse_list( $cc ) );
247
+                $recipients = array_filter( array_unique( array_merge( $recipients, $cc ) ) );
248
+            }
249
+
250
+            if ( ! empty( $cc_2 ) ) {
251
+                $cc_2 = array_map( 'sanitize_email', wpinv_parse_list( $cc_2 ) );
252
+                $recipients = array_filter( array_unique( array_merge( $recipients, $cc_2 ) ) );
253
+            }
254
+        }
255
+
256
+        return $recipients;
257
+
258
+    }
259
+
260
+    /**
261
+     * Sends a new invoice notification.
262
+     *
263
+     * @param WPInv_Invoice $invoice
264
+     */
265
+    public function new_invoice( $invoice ) {
266
+
267
+        // Only send this email for invoices created via the admin page.
268
+        if ( ! $invoice->is_type( 'invoice' ) || $invoice->is_paid() || $this->is_payment_form_invoice( $invoice->get_id() ) ) {
269
+            return;
270
+        }
271
+
272
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
273
+        $recipient = wpinv_get_admin_email();
274
+
275
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
276
+
277
+    }
278
+
279
+    /**
280
+     * Sends a cancelled invoice notification.
281
+     *
282
+     * @param WPInv_Invoice $invoice
283
+     */
284
+    public function cancelled_invoice( $invoice ) {
285
+
286
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
287
+        $recipient = $invoice->get_email();
288
+
289
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
290
+    }
291
+
292
+    /**
293
+     * Sends a failed invoice notification.
294
+     *
295
+     * @param WPInv_Invoice $invoice
296
+     */
297
+    public function failed_invoice( $invoice ) {
298
+
299
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
300
+        $recipient = wpinv_get_admin_email();
301
+
302
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
303
+
304
+    }
305
+
306
+    /**
307
+     * Sends a notification whenever an invoice is put on hold.
308
+     *
309
+     * @param WPInv_Invoice $invoice
310
+     */
311
+    public function onhold_invoice( $invoice ) {
312
+
313
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
314
+        $recipient = $invoice->get_email();
315
+
316
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
317
+
318
+    }
319
+
320
+    /**
321
+     * Sends a notification whenever an invoice is marked as processing payment.
322
+     *
323
+     * @param WPInv_Invoice $invoice
324
+     */
325
+    public function processing_invoice( $invoice ) {
326
+
327
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
328
+        $recipient = $invoice->get_email();
329
+
330
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
331
+
332
+    }
333
+
334
+    /**
335
+     * Sends a notification whenever an invoice is paid.
336
+     *
337
+     * @param WPInv_Invoice $invoice
338
+     */
339
+    public function completed_invoice( $invoice ) {
340
+
341
+        // (Maybe) abort if it is a renewal invoice.
342
+        if ( $invoice->is_renewal() && ! wpinv_get_option( 'email_completed_invoice_renewal_active', false ) ) {
343
+            return;
344
+        }
345
+
346
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
347
+        $recipient = $invoice->get_email();
348
+
349
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
350
+
351
+    }
352
+
353
+    /**
354
+     * Sends a notification whenever an invoice is refunded.
355
+     *
356
+     * @param WPInv_Invoice $invoice
357
+     */
358
+    public function refunded_invoice( $invoice ) {
359
+
360
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
361
+        $recipient = $invoice->get_email();
362
+
363
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
364
+
365
+    }
366
+
367
+    /**
368
+     * Notifies a user about new invoices
369
+     *
370
+     * @param WPInv_Invoice $invoice
371
+     * @param bool $force
372
+     */
373
+    public function user_invoice( $invoice, $force = false ) {
374
+
375
+        if ( ! $force && ! empty( $GLOBALS['wpinv_skip_invoice_notification'] ) ) {
376
+            return;
377
+        }
378
+
379
+        // Only send this email for invoices created via the admin page.
380
+        if ( ! $invoice->is_type( 'invoice' ) || ( empty( $force ) && $invoice->is_paid() ) || ( empty( $force ) && $this->is_payment_form_invoice( $invoice->get_id() ) ) ) {
381
+            return;
382
+        }
383
+
384
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
385
+        $recipient = $invoice->get_email();
386
+
387
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient );
388
+
389
+    }
390
+
391
+    /**
392
+     * Checks if an invoice is a payment form invoice.
393
+     *
394
+     * @param int $invoice
395
+     * @return bool
396
+     */
397
+    public function is_payment_form_invoice( $invoice ) {
398
+        $created_via             = get_post_meta( $invoice, 'wpinv_created_via', true );
399
+        $is_payment_form_invoice = 'payment_form' === $created_via || 'geodirectory' === $created_via;
400
+        $is_payment_form_invoice = apply_filters( 'getpaid_invoice_notifications_is_payment_form_invoice', $is_payment_form_invoice, $invoice );
401
+        return empty( $_GET['getpaid-admin-action'] ) && $is_payment_form_invoice;
402
+    }
403
+
404
+    /**
405
+     * Notifies admin about new invoice notes
406
+     *
407
+     * @param WPInv_Invoice $invoice
408
+     * @param string $note
409
+     */
410
+    public function user_note( $invoice, $note ) {
411
+
412
+        $email     = new GetPaid_Notification_Email( __FUNCTION__, $invoice );
413
+        $recipient = $invoice->get_email();
414
+
415
+        return $this->send_email( $invoice, $email, __FUNCTION__, $recipient, array( 'customer_note' => $note ) );
416
+
417
+    }
418
+
419
+    /**
420
+     * (Force) Sends overdue notices.
421
+     *
422
+     * @param WPInv_Invoice $invoice
423
+     */
424
+    public function force_send_overdue_notice( $invoice ) {
425
+        $email = new GetPaid_Notification_Email( 'overdue', $invoice );
426
+        return $this->send_email( $invoice, $email, 'overdue', $invoice->get_email() );
427
+    }
428
+
429
+    /**
430
+     * Sends overdue notices.
431
+     *
432
+     * @TODO: Create an invoices query class.
433
+     */
434
+    public function overdue() {
435
+        global $wpdb;
436
+
437
+        $email = new GetPaid_Notification_Email( __FUNCTION__ );
438
+
439
+        // Fetch reminder days.
440
+        $reminder_days = array_unique( wp_parse_id_list( $email->get_option( 'days' ) ) );
441
+
442
+        // Abort if non is set.
443
+        if ( empty( $reminder_days ) ) {
444
+            return;
445
+        }
446
+
447
+        // Retrieve date query.
448
+        $date_query = $this->get_date_query( $reminder_days );
449
+
450
+        // Invoices table.
451
+        $table = $wpdb->prefix . 'getpaid_invoices';
452
+
453
+        // Fetch invoices.
454
+        $invoices  = $wpdb->get_col(
455
+            "SELECT posts.ID FROM $wpdb->posts as posts
456 456
 			LEFT JOIN $table as invoices ON invoices.post_id = posts.ID
457 457
 			WHERE posts.post_type = 'wpi_invoice' AND posts.post_status = 'wpi-pending' $date_query"
458 458
         );
459 459
 
460
-		foreach ( $invoices as $invoice ) {
460
+        foreach ( $invoices as $invoice ) {
461 461
 
462
-			// Only send this email for invoices created via the admin page.
463
-			if ( ! $this->is_payment_form_invoice( $invoice ) ) {
464
-				$invoice       = new WPInv_Invoice( $invoice );
465
-				$email->object = $invoice;
462
+            // Only send this email for invoices created via the admin page.
463
+            if ( ! $this->is_payment_form_invoice( $invoice ) ) {
464
+                $invoice       = new WPInv_Invoice( $invoice );
465
+                $email->object = $invoice;
466 466
 
467
-				if ( $invoice->needs_payment() && ! $invoice->is_renewal() ) {
468
-					$this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() );
469
-				}
470
-			}
471
-		}
467
+                if ( $invoice->needs_payment() && ! $invoice->is_renewal() ) {
468
+                    $this->send_email( $invoice, $email, __FUNCTION__, $invoice->get_email() );
469
+                }
470
+            }
471
+        }
472 472
 
473
-	}
473
+    }
474 474
 
475
-	/**
476
-	 * Calculates the date query for an invoices query
477
-	 *
478
-	 * @param array $reminder_days
479
-	 * @return string
480
-	 */
481
-	public function get_date_query( $reminder_days ) {
475
+    /**
476
+     * Calculates the date query for an invoices query
477
+     *
478
+     * @param array $reminder_days
479
+     * @return string
480
+     */
481
+    public function get_date_query( $reminder_days ) {
482 482
 
483
-		$date_query = array(
484
-			'relation' => 'OR',
485
-		);
483
+        $date_query = array(
484
+            'relation' => 'OR',
485
+        );
486 486
 
487
-		foreach ( $reminder_days as $days ) {
488
-			$date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) );
487
+        foreach ( $reminder_days as $days ) {
488
+            $date = date_parse( date( 'Y-m-d', strtotime( "-$days days", current_time( 'timestamp' ) ) ) );
489 489
 
490
-			$date_query[] = array(
491
-				'year'  => $date['year'],
492
-				'month' => $date['month'],
493
-				'day'   => $date['day'],
494
-			);
490
+            $date_query[] = array(
491
+                'year'  => $date['year'],
492
+                'month' => $date['month'],
493
+                'day'   => $date['day'],
494
+            );
495 495
 
496
-		}
496
+        }
497 497
 
498
-		$date_query = new WP_Date_Query( $date_query, 'invoices.due_date' );
498
+        $date_query = new WP_Date_Query( $date_query, 'invoices.due_date' );
499 499
 
500
-		return $date_query->get_sql();
500
+        return $date_query->get_sql();
501 501
 
502
-	}
502
+    }
503 503
 
504 504
 }
Please login to merge, or discard this patch.