Passed
Push — master ( 17a77b...f99e01 )
by Brian
05:28
created
includes/payments/class-getpaid-checkout.php 1 patch
Indentation   +301 added lines, -301 removed lines patch added patch discarded remove patch
@@ -12,184 +12,184 @@  discard block
 block discarded – undo
12 12
  */
13 13
 class GetPaid_Checkout {
14 14
 
15
-	/**
16
-	 * @var GetPaid_Payment_Form_Submission
17
-	 */
18
-	protected $payment_form_submission;
19
-
20
-	/**
21
-	 * Class constructor.
22
-	 *
23
-	 * @param GetPaid_Payment_Form_Submission $submission
24
-	 */
25
-	public function __construct( $submission ) {
26
-		$this->payment_form_submission = $submission;
27
-	}
28
-
29
-	/**
30
-	 * Processes the checkout.
31
-	 *
32
-	 */
33
-	public function process_checkout() {
34
-
35
-		// Validate the submission.
36
-		$this->validate_submission();
37
-
38
-		// Prepare the invoice.
39
-		$items      = $this->get_submission_items();
40
-		$invoice    = $this->get_submission_invoice();
41
-		$invoice    = $this->process_submission_invoice( $invoice, $items );
42
-		$prepared   = $this->prepare_submission_data_for_saving();
43
-
44
-		$this->prepare_billing_info( $invoice );
45
-
46
-		$shipping   = $this->prepare_shipping_info( $invoice );
47
-
48
-		// Save the invoice.
49
-		$invoice->set_is_viewed( true );
50
-		$invoice->recalculate_total();
15
+    /**
16
+     * @var GetPaid_Payment_Form_Submission
17
+     */
18
+    protected $payment_form_submission;
19
+
20
+    /**
21
+     * Class constructor.
22
+     *
23
+     * @param GetPaid_Payment_Form_Submission $submission
24
+     */
25
+    public function __construct( $submission ) {
26
+        $this->payment_form_submission = $submission;
27
+    }
28
+
29
+    /**
30
+     * Processes the checkout.
31
+     *
32
+     */
33
+    public function process_checkout() {
34
+
35
+        // Validate the submission.
36
+        $this->validate_submission();
37
+
38
+        // Prepare the invoice.
39
+        $items      = $this->get_submission_items();
40
+        $invoice    = $this->get_submission_invoice();
41
+        $invoice    = $this->process_submission_invoice( $invoice, $items );
42
+        $prepared   = $this->prepare_submission_data_for_saving();
43
+
44
+        $this->prepare_billing_info( $invoice );
45
+
46
+        $shipping   = $this->prepare_shipping_info( $invoice );
47
+
48
+        // Save the invoice.
49
+        $invoice->set_is_viewed( true );
50
+        $invoice->recalculate_total();
51 51
         $invoice->save();
52 52
 
53
-		do_action( 'getpaid_checkout_invoice_updated', $invoice );
53
+        do_action( 'getpaid_checkout_invoice_updated', $invoice );
54 54
 
55
-		// Send to the gateway.
56
-		$this->post_process_submission( $invoice, $prepared, $shipping );
57
-	}
55
+        // Send to the gateway.
56
+        $this->post_process_submission( $invoice, $prepared, $shipping );
57
+    }
58 58
 
59
-	/**
60
-	 * Validates the submission.
61
-	 *
62
-	 */
63
-	protected function validate_submission() {
59
+    /**
60
+     * Validates the submission.
61
+     *
62
+     */
63
+    protected function validate_submission() {
64 64
 
65
-		$submission = $this->payment_form_submission;
66
-		$data       = $submission->get_data();
65
+        $submission = $this->payment_form_submission;
66
+        $data       = $submission->get_data();
67 67
 
68
-		// Do we have an error?
68
+        // Do we have an error?
69 69
         if ( ! empty( $submission->last_error ) ) {
70
-			wp_send_json_error( $submission->last_error );
70
+            wp_send_json_error( $submission->last_error );
71 71
         }
72 72
 
73
-		// We need a billing email.
73
+        // We need a billing email.
74 74
         if ( ! $submission->has_billing_email() ) {
75 75
             wp_send_json_error( __( 'Provide a valid billing email.', 'invoicing' ) );
76
-		}
76
+        }
77 77
 
78
-		// Non-recurring gateways should not be allowed to process recurring invoices.
79
-		if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) {
80
-			wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) );
81
-		}
78
+        // Non-recurring gateways should not be allowed to process recurring invoices.
79
+        if ( $submission->should_collect_payment_details() && $submission->has_recurring && ! wpinv_gateway_support_subscription( $data['wpi-gateway'] ) ) {
80
+            wp_send_json_error( __( 'The selected payment gateway does not support subscription payments.', 'invoicing' ) );
81
+        }
82 82
 
83
-		// Ensure the gateway is active.
84
-		if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) {
85
-			wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) );
86
-		}
83
+        // Ensure the gateway is active.
84
+        if ( $submission->should_collect_payment_details() && ! wpinv_is_gateway_active( $data['wpi-gateway'] ) ) {
85
+            wp_send_json_error( __( 'The selected payment gateway is not active', 'invoicing' ) );
86
+        }
87 87
 
88
-		// Clear any existing errors.
89
-		wpinv_clear_errors();
88
+        // Clear any existing errors.
89
+        wpinv_clear_errors();
90 90
 
91
-		// Allow themes and plugins to hook to errors
92
-		do_action( 'getpaid_checkout_error_checks', $submission );
91
+        // Allow themes and plugins to hook to errors
92
+        do_action( 'getpaid_checkout_error_checks', $submission );
93 93
 
94
-		// Do we have any errors?
94
+        // Do we have any errors?
95 95
         if ( wpinv_get_errors() ) {
96 96
             wp_send_json_error( getpaid_get_errors_html() );
97
-		}
97
+        }
98 98
 
99
-	}
99
+    }
100 100
 
101
-	/**
102
-	 * Retrieves submission items.
103
-	 *
104
-	 * @return GetPaid_Form_Item[]
105
-	 */
106
-	protected function get_submission_items() {
101
+    /**
102
+     * Retrieves submission items.
103
+     *
104
+     * @return GetPaid_Form_Item[]
105
+     */
106
+    protected function get_submission_items() {
107 107
 
108
-		$items = $this->payment_form_submission->get_items();
108
+        $items = $this->payment_form_submission->get_items();
109 109
 
110 110
         // Ensure that we have items.
111 111
         if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) {
112 112
             wp_send_json_error( __( 'Please provide at least one item or amount.', 'invoicing' ) );
113
-		}
114
-
115
-		return $items;
116
-	}
117
-
118
-	/**
119
-	 * Retrieves submission invoice.
120
-	 *
121
-	 * @return WPInv_Invoice
122
-	 */
123
-	protected function get_submission_invoice() {
124
-		$submission = $this->payment_form_submission;
125
-
126
-		if ( ! $submission->has_invoice() ) {
127
-			$invoice = new WPInv_Invoice();
128
-			$invoice->set_created_via( 'payment_form' );
129
-			return $invoice;
130 113
         }
131 114
 
132
-		$invoice = $submission->get_invoice();
115
+        return $items;
116
+    }
117
+
118
+    /**
119
+     * Retrieves submission invoice.
120
+     *
121
+     * @return WPInv_Invoice
122
+     */
123
+    protected function get_submission_invoice() {
124
+        $submission = $this->payment_form_submission;
125
+
126
+        if ( ! $submission->has_invoice() ) {
127
+            $invoice = new WPInv_Invoice();
128
+            $invoice->set_created_via( 'payment_form' );
129
+            return $invoice;
130
+        }
131
+
132
+        $invoice = $submission->get_invoice();
133 133
 
134
-		// Make sure that it is neither paid or refunded.
135
-		if ( $invoice->is_paid() || $invoice->is_refunded() ) {
136
-			wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) );
137
-		}
134
+        // Make sure that it is neither paid or refunded.
135
+        if ( $invoice->is_paid() || $invoice->is_refunded() ) {
136
+            wp_send_json_error( __( 'This invoice has already been paid for.', 'invoicing' ) );
137
+        }
138 138
 
139
-		return $invoice;
140
-	}
139
+        return $invoice;
140
+    }
141 141
 
142
-	/**
143
-	 * Processes the submission invoice.
144
-	 *
145
-	 * @param WPInv_Invoice $invoice
146
-	 * @param GetPaid_Form_Item[] $items
147
-	 * @return WPInv_Invoice
148
-	 */
149
-	protected function process_submission_invoice( $invoice, $items ) {
142
+    /**
143
+     * Processes the submission invoice.
144
+     *
145
+     * @param WPInv_Invoice $invoice
146
+     * @param GetPaid_Form_Item[] $items
147
+     * @return WPInv_Invoice
148
+     */
149
+    protected function process_submission_invoice( $invoice, $items ) {
150 150
 
151
-		$submission = $this->payment_form_submission;
151
+        $submission = $this->payment_form_submission;
152 152
 
153
-		// Set-up the invoice details.
154
-		$invoice->set_email( sanitize_email( $submission->get_billing_email() ) );
155
-		$invoice->set_user_id( $this->get_submission_customer() );
156
-		$invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) );
153
+        // Set-up the invoice details.
154
+        $invoice->set_email( sanitize_email( $submission->get_billing_email() ) );
155
+        $invoice->set_user_id( $this->get_submission_customer() );
156
+        $invoice->set_payment_form( absint( $submission->get_payment_form()->get_id() ) );
157 157
         $invoice->set_items( $items );
158 158
         $invoice->set_fees( $submission->get_fees() );
159 159
         $invoice->set_taxes( $submission->get_taxes() );
160
-		$invoice->set_discounts( $submission->get_discounts() );
161
-		$invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) );
162
-		$invoice->set_currency( $submission->get_currency() );
160
+        $invoice->set_discounts( $submission->get_discounts() );
161
+        $invoice->set_gateway( $submission->get_field( 'wpi-gateway' ) );
162
+        $invoice->set_currency( $submission->get_currency() );
163 163
 
164
-		if ( $submission->has_shipping() ) {
165
-			$invoice->set_shipping( $submission->get_shipping() );
166
-		}
164
+        if ( $submission->has_shipping() ) {
165
+            $invoice->set_shipping( $submission->get_shipping() );
166
+        }
167 167
 
168
-		$address_confirmed = $submission->get_field( 'confirm-address' );
169
-		$invoice->set_address_confirmed( ! empty( $address_confirmed ) );
168
+        $address_confirmed = $submission->get_field( 'confirm-address' );
169
+        $invoice->set_address_confirmed( ! empty( $address_confirmed ) );
170 170
 
171
-		if ( $submission->has_discount_code() ) {
171
+        if ( $submission->has_discount_code() ) {
172 172
             $invoice->set_discount_code( $submission->get_discount_code() );
173
-		}
174
-
175
-		getpaid_maybe_add_default_address( $invoice );
176
-		return $invoice;
177
-	}
178
-
179
-	/**
180
-	 * Retrieves the submission's customer.
181
-	 *
182
-	 * @return int The customer id.
183
-	 */
184
-	protected function get_submission_customer() {
185
-		$submission = $this->payment_form_submission;
186
-
187
-		// If this is an existing invoice...
188
-		if ( $submission->has_invoice() ) {
189
-			return $submission->get_invoice()->get_user_id();
190
-		}
191
-
192
-		// (Maybe) create the user.
173
+        }
174
+
175
+        getpaid_maybe_add_default_address( $invoice );
176
+        return $invoice;
177
+    }
178
+
179
+    /**
180
+     * Retrieves the submission's customer.
181
+     *
182
+     * @return int The customer id.
183
+     */
184
+    protected function get_submission_customer() {
185
+        $submission = $this->payment_form_submission;
186
+
187
+        // If this is an existing invoice...
188
+        if ( $submission->has_invoice() ) {
189
+            return $submission->get_invoice()->get_user_id();
190
+        }
191
+
192
+        // (Maybe) create the user.
193 193
         $user = get_current_user_id();
194 194
 
195 195
         if ( empty( $user ) ) {
@@ -197,16 +197,16 @@  discard block
 block discarded – undo
197 197
         }
198 198
 
199 199
         if ( empty( $user ) ) {
200
-			$name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) );
201
-			$name = implode( '', array_filter( $name ) );
200
+            $name = array( $submission->get_field( 'wpinv_first_name', 'billing' ), $submission->get_field( 'wpinv_last_name', 'billing' ) );
201
+            $name = implode( '', array_filter( $name ) );
202 202
             $user = wpinv_create_user( $submission->get_billing_email(), $name );
203 203
 
204
-			// (Maybe) send new user notification.
205
-			$should_send_notification = wpinv_get_option( 'disable_new_user_emails' );
206
-			if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) {
207
-				wp_send_new_user_notifications( $user, 'user' );
208
-			}
209
-		}
204
+            // (Maybe) send new user notification.
205
+            $should_send_notification = wpinv_get_option( 'disable_new_user_emails' );
206
+            if ( ! empty( $user ) && is_numeric( $user ) && apply_filters( 'getpaid_send_new_user_notification', empty( $should_send_notification ), $user ) ) {
207
+                wp_send_new_user_notifications( $user, 'user' );
208
+            }
209
+        }
210 210
 
211 211
         if ( is_wp_error( $user ) ) {
212 212
             wp_send_json_error( $user->get_error_message() );
@@ -214,47 +214,47 @@  discard block
 block discarded – undo
214 214
 
215 215
         if ( is_numeric( $user ) ) {
216 216
             return $user;
217
-		}
217
+        }
218 218
 
219
-		return $user->ID;
219
+        return $user->ID;
220 220
 
221
-	}
221
+    }
222 222
 
223
-	/**
223
+    /**
224 224
      * Prepares submission data for saving to the database.
225 225
      *
226
-	 * @return array
226
+     * @return array
227 227
      */
228 228
     public function prepare_submission_data_for_saving() {
229 229
 
230
-		$submission = $this->payment_form_submission;
230
+        $submission = $this->payment_form_submission;
231 231
 
232
-		// Prepared submission details.
232
+        // Prepared submission details.
233 233
         $prepared = array(
234
-			'all'  => array(),
235
-			'meta' => array(),
236
-		);
234
+            'all'  => array(),
235
+            'meta' => array(),
236
+        );
237 237
 
238 238
         // Raw submission details.
239
-		$data     = $submission->get_data();
239
+        $data     = $submission->get_data();
240 240
 
241
-		// Loop through the submitted details.
241
+        // Loop through the submitted details.
242 242
         foreach ( $submission->get_payment_form()->get_elements() as $field ) {
243 243
 
244
-			// Skip premade fields.
244
+            // Skip premade fields.
245 245
             if ( ! empty( $field['premade'] ) ) {
246 246
                 continue;
247 247
             }
248 248
 
249
-			// Ensure address is provided.
250
-			if ( $field['type'] == 'address' ) {
249
+            // Ensure address is provided.
250
+            if ( $field['type'] == 'address' ) {
251 251
                 $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing';
252 252
 
253
-				foreach ( $field['fields'] as $address_field ) {
253
+                foreach ( $field['fields'] as $address_field ) {
254 254
 
255
-					if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) {
256
-						wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) );
257
-					}
255
+                    if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) {
256
+                        wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) );
257
+                    }
258 258
 }
259 259
 }
260 260
 
@@ -266,31 +266,31 @@  discard block
 block discarded – undo
266 266
             // Handle misc fields.
267 267
             if ( isset( $data[ $field['id'] ] ) ) {
268 268
 
269
-				// Uploads.
270
-				if ( $field['type'] == 'file_upload' ) {
271
-					$max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] );
269
+                // Uploads.
270
+                if ( $field['type'] == 'file_upload' ) {
271
+                    $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] );
272 272
 
273
-					if ( count( $data[ $field['id'] ] ) > $max_file_num ) {
274
-						wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) );
275
-					}
273
+                    if ( count( $data[ $field['id'] ] ) > $max_file_num ) {
274
+                        wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) );
275
+                    }
276 276
 
277
-					$value = array();
277
+                    $value = array();
278 278
 
279
-					foreach ( $data[ $field['id'] ] as $url => $name ) {
280
-						$value[] = sprintf(
281
-							'<a href="%s" target="_blank">%s</a>',
282
-							esc_url_raw( $url ),
283
-							esc_html( $name )
284
-						);
285
-					}
279
+                    foreach ( $data[ $field['id'] ] as $url => $name ) {
280
+                        $value[] = sprintf(
281
+                            '<a href="%s" target="_blank">%s</a>',
282
+                            esc_url_raw( $url ),
283
+                            esc_html( $name )
284
+                        );
285
+                    }
286 286
 
287
-					$value = implode( ' | ', $value );
287
+                    $value = implode( ' | ', $value );
288 288
 
289
-				} elseif ( $field['type'] == 'checkbox' ) {
290
-					$value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' );
291
-				} else {
292
-					$value = wp_kses_post( $data[ $field['id'] ] );
293
-				}
289
+                } elseif ( $field['type'] == 'checkbox' ) {
290
+                    $value = isset( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' );
291
+                } else {
292
+                    $value = wp_kses_post( $data[ $field['id'] ] );
293
+                }
294 294
 
295 295
                 $label = $field['id'];
296 296
 
@@ -298,188 +298,188 @@  discard block
 block discarded – undo
298 298
                     $label = $field['label'];
299 299
                 }
300 300
 
301
-				if ( ! empty( $field['add_meta'] ) ) {
302
-					$prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value );
303
-				}
304
-				$prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value );
301
+                if ( ! empty( $field['add_meta'] ) ) {
302
+                    $prepared['meta'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value );
303
+                }
304
+                $prepared['all'][ wpinv_clean( $label ) ] = wp_kses_post_deep( $value );
305 305
 
306 306
             }
307 307
 }
308 308
 
309
-		return $prepared;
309
+        return $prepared;
310 310
 
311
-	}
311
+    }
312 312
 
313
-	/**
313
+    /**
314 314
      * Retrieves address details.
315 315
      *
316
-	 * @return array
317
-	 * @param WPInv_Invoice $invoice
318
-	 * @param string $type
316
+     * @return array
317
+     * @param WPInv_Invoice $invoice
318
+     * @param string $type
319 319
      */
320 320
     public function prepare_address_details( $invoice, $type = 'billing' ) {
321 321
 
322
-		$data     = $this->payment_form_submission->get_data();
323
-		$type     = sanitize_key( $type );
324
-		$address  = array();
325
-		$prepared = array();
322
+        $data     = $this->payment_form_submission->get_data();
323
+        $type     = sanitize_key( $type );
324
+        $address  = array();
325
+        $prepared = array();
326 326
 
327
-		if ( ! empty( $data[ $type ] ) ) {
328
-			$address = $data[ $type ];
329
-		}
327
+        if ( ! empty( $data[ $type ] ) ) {
328
+            $address = $data[ $type ];
329
+        }
330 330
 
331
-		// Clean address details.
332
-		foreach ( $address as $key => $value ) {
333
-			$key             = sanitize_key( $key );
334
-			$key             = str_replace( 'wpinv_', '', $key );
335
-			$value           = wpinv_clean( $value );
336
-			$prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice );
337
-		}
331
+        // Clean address details.
332
+        foreach ( $address as $key => $value ) {
333
+            $key             = sanitize_key( $key );
334
+            $key             = str_replace( 'wpinv_', '', $key );
335
+            $value           = wpinv_clean( $value );
336
+            $prepared[ $key ] = apply_filters( "getpaid_checkout_{$type}_address_$key", $value, $this->payment_form_submission, $invoice );
337
+        }
338 338
 
339
-		// Filter address details.
340
-		$prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice );
339
+        // Filter address details.
340
+        $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice );
341 341
 
342
-		// Remove non-whitelisted values.
343
-		return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY );
342
+        // Remove non-whitelisted values.
343
+        return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY );
344 344
 
345
-	}
345
+    }
346 346
 
347
-	/**
347
+    /**
348 348
      * Prepares the billing details.
349 349
      *
350
-	 * @return array
351
-	 * @param WPInv_Invoice $invoice
350
+     * @return array
351
+     * @param WPInv_Invoice $invoice
352 352
      */
353 353
     protected function prepare_billing_info( &$invoice ) {
354 354
 
355
-		$billing_address = $this->prepare_address_details( $invoice, 'billing' );
355
+        $billing_address = $this->prepare_address_details( $invoice, 'billing' );
356 356
 
357
-		// Update the invoice with the billing details.
358
-		$invoice->set_props( $billing_address );
357
+        // Update the invoice with the billing details.
358
+        $invoice->set_props( $billing_address );
359 359
 
360
-	}
360
+    }
361 361
 
362
-	/**
362
+    /**
363 363
      * Prepares the shipping details.
364 364
      *
365
-	 * @return array
366
-	 * @param WPInv_Invoice $invoice
365
+     * @return array
366
+     * @param WPInv_Invoice $invoice
367 367
      */
368 368
     protected function prepare_shipping_info( $invoice ) {
369 369
 
370
-		$data = $this->payment_form_submission->get_data();
370
+        $data = $this->payment_form_submission->get_data();
371 371
 
372
-		if ( empty( $data['same-shipping-address'] ) ) {
373
-			return $this->prepare_address_details( $invoice, 'shipping' );
374
-		}
372
+        if ( empty( $data['same-shipping-address'] ) ) {
373
+            return $this->prepare_address_details( $invoice, 'shipping' );
374
+        }
375 375
 
376
-		return $this->prepare_address_details( $invoice, 'billing' );
376
+        return $this->prepare_address_details( $invoice, 'billing' );
377 377
 
378
-	}
378
+    }
379 379
 
380
-	/**
381
-	 * Confirms the submission is valid and send users to the gateway.
382
-	 *
383
-	 * @param WPInv_Invoice $invoice
384
-	 * @param array $prepared_payment_form_data
385
-	 * @param array $shipping
386
-	 */
387
-	protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) {
380
+    /**
381
+     * Confirms the submission is valid and send users to the gateway.
382
+     *
383
+     * @param WPInv_Invoice $invoice
384
+     * @param array $prepared_payment_form_data
385
+     * @param array $shipping
386
+     */
387
+    protected function post_process_submission( $invoice, $prepared_payment_form_data, $shipping ) {
388 388
 
389
-		// Ensure the invoice exists.
389
+        // Ensure the invoice exists.
390 390
         if ( ! $invoice->exists() ) {
391 391
             wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) );
392 392
         }
393 393
 
394
-		// Save payment form data.
395
-		$prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice );
394
+        // Save payment form data.
395
+        $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice );
396 396
         delete_post_meta( $invoice->get_id(), 'payment_form_data' );
397
-		delete_post_meta( $invoice->get_id(), 'additional_meta_data' );
398
-		if ( ! empty( $prepared_payment_form_data ) ) {
397
+        delete_post_meta( $invoice->get_id(), 'additional_meta_data' );
398
+        if ( ! empty( $prepared_payment_form_data ) ) {
399 399
 
400
-			if ( ! empty( $prepared_payment_form_data['all'] ) ) {
401
-				update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] );
402
-			}
400
+            if ( ! empty( $prepared_payment_form_data['all'] ) ) {
401
+                update_post_meta( $invoice->get_id(), 'payment_form_data', $prepared_payment_form_data['all'] );
402
+            }
403 403
 
404
-			if ( ! empty( $prepared_payment_form_data['meta'] ) ) {
405
-				update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] );
406
-			}
404
+            if ( ! empty( $prepared_payment_form_data['meta'] ) ) {
405
+                update_post_meta( $invoice->get_id(), 'additional_meta_data', $prepared_payment_form_data['meta'] );
406
+            }
407 407
 }
408 408
 
409
-		// Save payment form data.
410
-		$shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission );
409
+        // Save payment form data.
410
+        $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission );
411 411
         if ( ! empty( $shipping ) ) {
412 412
             update_post_meta( $invoice->get_id(), 'shipping_address', $shipping );
413
-		}
413
+        }
414 414
 
415
-		// Backwards compatibility.
415
+        // Backwards compatibility.
416 416
         add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) );
417 417
 
418
-		$this->process_payment( $invoice );
418
+        $this->process_payment( $invoice );
419 419
 
420 420
         // If we are here, there was an error.
421
-		wpinv_send_back_to_checkout( $invoice );
421
+        wpinv_send_back_to_checkout( $invoice );
422 422
 
423
-	}
423
+    }
424 424
 
425
-	/**
426
-	 * Processes the actual payment.
427
-	 *
428
-	 * @param WPInv_Invoice $invoice
429
-	 */
430
-	protected function process_payment( $invoice ) {
425
+    /**
426
+     * Processes the actual payment.
427
+     *
428
+     * @param WPInv_Invoice $invoice
429
+     */
430
+    protected function process_payment( $invoice ) {
431 431
 
432
-		// Clear any checkout errors.
433
-		wpinv_clear_errors();
432
+        // Clear any checkout errors.
433
+        wpinv_clear_errors();
434 434
 
435
-		// No need to send free invoices to the gateway.
436
-		if ( $invoice->is_free() ) {
437
-			$this->process_free_payment( $invoice );
438
-		}
435
+        // No need to send free invoices to the gateway.
436
+        if ( $invoice->is_free() ) {
437
+            $this->process_free_payment( $invoice );
438
+        }
439 439
 
440
-		$submission = $this->payment_form_submission;
440
+        $submission = $this->payment_form_submission;
441 441
 
442
-		// Fires before sending to the gateway.
443
-		do_action( 'getpaid_checkout_before_gateway', $invoice, $submission );
442
+        // Fires before sending to the gateway.
443
+        do_action( 'getpaid_checkout_before_gateway', $invoice, $submission );
444 444
 
445
-		// Allow the sumission data to be modified before it is sent to the gateway.
446
-		$submission_data    = $submission->get_data();
447
-		$submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice );
448
-		$submission_data    = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice );
445
+        // Allow the sumission data to be modified before it is sent to the gateway.
446
+        $submission_data    = $submission->get_data();
447
+        $submission_gateway = apply_filters( 'getpaid_gateway_submission_gateway', $invoice->get_gateway(), $submission, $invoice );
448
+        $submission_data    = apply_filters( 'getpaid_gateway_submission_data', $submission_data, $submission, $invoice );
449 449
 
450
-		// Validate the currency.
451
-		if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) {
452
-			wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) );
453
-		}
450
+        // Validate the currency.
451
+        if ( ! apply_filters( "getpaid_gateway_{$submission_gateway}_is_valid_for_currency", true, $invoice->get_currency() ) ) {
452
+            wpinv_set_error( 'invalid_currency', __( 'The chosen payment gateway does not support this currency', 'invoicing' ) );
453
+        }
454 454
 
455
-		// Check to see if we have any errors.
456
-		if ( wpinv_get_errors() ) {
457
-			wpinv_send_back_to_checkout( $invoice );
458
-		}
455
+        // Check to see if we have any errors.
456
+        if ( wpinv_get_errors() ) {
457
+            wpinv_send_back_to_checkout( $invoice );
458
+        }
459 459
 
460
-		// Send info to the gateway for payment processing
461
-		do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission );
460
+        // Send info to the gateway for payment processing
461
+        do_action( "getpaid_gateway_$submission_gateway", $invoice, $submission_data, $submission );
462 462
 
463
-		// Backwards compatibility.
464
-		wpinv_send_to_gateway( $submission_gateway, $invoice );
463
+        // Backwards compatibility.
464
+        wpinv_send_to_gateway( $submission_gateway, $invoice );
465 465
 
466
-	}
466
+    }
467 467
 
468
-	/**
469
-	 * Marks the invoice as paid in case the checkout is free.
470
-	 *
471
-	 * @param WPInv_Invoice $invoice
472
-	 */
473
-	protected function process_free_payment( $invoice ) {
468
+    /**
469
+     * Marks the invoice as paid in case the checkout is free.
470
+     *
471
+     * @param WPInv_Invoice $invoice
472
+     */
473
+    protected function process_free_payment( $invoice ) {
474 474
 
475
-		$invoice->set_gateway( 'none' );
476
-		$invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true );
477
-		$invoice->mark_paid();
478
-		wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
475
+        $invoice->set_gateway( 'none' );
476
+        $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true );
477
+        $invoice->mark_paid();
478
+        wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) );
479 479
 
480
-	}
480
+    }
481 481
 
482
-	/**
482
+    /**
483 483
      * Sends a redrect response to payment details.
484 484
      *
485 485
      */
Please login to merge, or discard this patch.
includes/class-getpaid-notification-email-sender.php 1 patch
Indentation   +134 added lines, -134 removed lines patch added patch discarded remove patch
@@ -13,17 +13,17 @@  discard block
 block discarded – undo
13 13
 class GetPaid_Notification_Email_Sender {
14 14
 
15 15
     /**
16
-	 * Whether or not we should inline CSS into the email.
17
-	 */
18
-	public $inline_css = true;
16
+     * Whether or not we should inline CSS into the email.
17
+     */
18
+    public $inline_css = true;
19 19
 
20 20
     /**
21
-	 * The wp_mail() data.
22
-	 */
21
+     * The wp_mail() data.
22
+     */
23 23
     public $wp_mail_data = null;
24 24
 
25 25
     /**
26
-	 * Sends a new email.
26
+     * Sends a new email.
27 27
      *
28 28
      * @param string|array $to The recipients email or an array of recipient emails.
29 29
      * @param string $subject The email's subject.
@@ -31,49 +31,49 @@  discard block
 block discarded – undo
31 31
      * @param array $attachments The email attachments.
32 32
      *
33 33
      * @return bool
34
-	 */
35
-	public function send( $to, $subject, $email, $attachments = array() ) {
34
+     */
35
+    public function send( $to, $subject, $email, $attachments = array() ) {
36 36
 
37
-		/*
37
+        /*
38 38
 		 * Allow to filter data on per-email basis.
39 39
 		 */
40
-		$data = apply_filters(
41
-			'getpaid_email_data',
42
-			array(
43
-				'to'          => array_filter( array_unique( wpinv_parse_list( $to ) ) ),
44
-				'subject'     => htmlspecialchars_decode( wp_strip_all_tags( $subject ), ENT_QUOTES ),
45
-				'email'       => apply_filters( 'wpinv_mail_content', $email ),
46
-				'headers'     => $this->get_headers(),
47
-				'attachments' => $attachments,
48
-			),
49
-			$this
50
-		);
40
+        $data = apply_filters(
41
+            'getpaid_email_data',
42
+            array(
43
+                'to'          => array_filter( array_unique( wpinv_parse_list( $to ) ) ),
44
+                'subject'     => htmlspecialchars_decode( wp_strip_all_tags( $subject ), ENT_QUOTES ),
45
+                'email'       => apply_filters( 'wpinv_mail_content', $email ),
46
+                'headers'     => $this->get_headers(),
47
+                'attachments' => $attachments,
48
+            ),
49
+            $this
50
+        );
51 51
 
52 52
         // Remove slashes.
53 53
         $data               = (array) wp_unslash( $data );
54 54
 
55 55
         // Cache it.
56
-		$this->wp_mail_data = $data;
56
+        $this->wp_mail_data = $data;
57 57
 
58
-		// Attach our own hooks.
59
-		$this->before_sending();
58
+        // Attach our own hooks.
59
+        $this->before_sending();
60 60
 
61 61
         $result = false;
62 62
 
63 63
         foreach ( $this->wp_mail_data['to'] as $to ) {
64
-			$result = $this->_send( $to, $data );
64
+            $result = $this->_send( $to, $data );
65 65
         }
66 66
 
67
-		// Remove our hooks.
68
-		$this->after_sending();
67
+        // Remove our hooks.
68
+        $this->after_sending();
69 69
 
70
-		$this->wp_mail_data = null;
70
+        $this->wp_mail_data = null;
71 71
 
72
-		return $result;
73
-	}
72
+        return $result;
73
+    }
74 74
 
75
-	/**
76
-	 * Does the actual sending.
75
+    /**
76
+     * Does the actual sending.
77 77
      *
78 78
      * @param string $to The recipient's email.
79 79
      * @param array $data The email's data.
@@ -81,81 +81,81 @@  discard block
 block discarded – undo
81 81
      * @param array $attachments The email attachments.
82 82
      *
83 83
      * @return bool
84
-	 */
85
-	protected function _send( $to, $data ) {
86
-
87
-		// Prepare the sending function.
88
-		$sending_function = apply_filters( 'getpaid_email_email_sending_function', 'wp_mail' );
89
-
90
-		// Send the actual email.
91
-		$result = call_user_func(
92
-			$sending_function,
93
-			$to,
94
-			html_entity_decode( $data['subject'], ENT_QUOTES, get_bloginfo( 'charset' ) ),
95
-			$data['email'],
96
-			$data['headers'],
97
-			$data['attachments']
98
-		);
99
-
100
-		if ( ! $result ) {
101
-			$log_message = wp_sprintf( __( "\nTime: %1\$s\nTo: %2\$s\nSubject: %3\$s\n", 'invoicing' ), date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ), $to, $data['subject'] );
102
-			wpinv_error_log( $log_message, __( 'Email from Invoicing plugin failed to send', 'invoicing' ), __FILE__, __LINE__ );
103
-		}
104
-
105
-		return $result;
106
-	}
84
+     */
85
+    protected function _send( $to, $data ) {
86
+
87
+        // Prepare the sending function.
88
+        $sending_function = apply_filters( 'getpaid_email_email_sending_function', 'wp_mail' );
89
+
90
+        // Send the actual email.
91
+        $result = call_user_func(
92
+            $sending_function,
93
+            $to,
94
+            html_entity_decode( $data['subject'], ENT_QUOTES, get_bloginfo( 'charset' ) ),
95
+            $data['email'],
96
+            $data['headers'],
97
+            $data['attachments']
98
+        );
99
+
100
+        if ( ! $result ) {
101
+            $log_message = wp_sprintf( __( "\nTime: %1\$s\nTo: %2\$s\nSubject: %3\$s\n", 'invoicing' ), date_i18n( 'F j Y H:i:s', current_time( 'timestamp' ) ), $to, $data['subject'] );
102
+            wpinv_error_log( $log_message, __( 'Email from Invoicing plugin failed to send', 'invoicing' ), __FILE__, __LINE__ );
103
+        }
104
+
105
+        return $result;
106
+    }
107 107
 
108 108
     /**
109
-	 * Retrieves email headers.
110
-	 */
111
-	public function get_headers() {
109
+     * Retrieves email headers.
110
+     */
111
+    public function get_headers() {
112 112
 
113
-		$name       = $this->get_from_name();
114
-		$reply_to   = $this->get_reply_to();
115
-		$headers    = array( "Reply-To:$name <$reply_to>" );
113
+        $name       = $this->get_from_name();
114
+        $reply_to   = $this->get_reply_to();
115
+        $headers    = array( "Reply-To:$name <$reply_to>" );
116 116
 
117
-		return apply_filters( 'getpaid_email_headers', $headers, $this );
117
+        return apply_filters( 'getpaid_email_headers', $headers, $this );
118 118
 
119
-	}
119
+    }
120 120
 
121 121
     /**
122
-	 * Fires before an email is sent
123
-	 *
124
-	 * @since 1.0.0
125
-	 */
126
-	public function before_sending() {
122
+     * Fires before an email is sent
123
+     *
124
+     * @since 1.0.0
125
+     */
126
+    public function before_sending() {
127 127
 
128 128
         do_action( 'getpaid_before_send_email', $this );
129
-		add_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 );
130
-		add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 );
131
-		add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 );
132
-		add_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000 );
129
+        add_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 );
130
+        add_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 );
131
+        add_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 );
132
+        add_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000 );
133 133
 
134
-	}
134
+    }
135 135
 
136 136
     /**
137
-	 * Returns the from name.
138
-	 */
139
-	public function get_from_name() {
137
+     * Returns the from name.
138
+     */
139
+    public function get_from_name() {
140 140
 
141 141
         $from_name = wpinv_get_option( 'email_from_name', get_bloginfo( 'name' ) );
142 142
 
143
-		if ( empty( $from_name ) ) {
144
-			$from_name = get_bloginfo( 'name' );
143
+        if ( empty( $from_name ) ) {
144
+            $from_name = get_bloginfo( 'name' );
145 145
         }
146 146
 
147
-		return wp_specialchars_decode( $from_name, ENT_QUOTES );
147
+        return wp_specialchars_decode( $from_name, ENT_QUOTES );
148 148
     }
149 149
 
150 150
     /**
151
-	 * Returns the from email.
152
-	 */
153
-	public function get_from_address() {
151
+     * Returns the from email.
152
+     */
153
+    public function get_from_address() {
154 154
 
155 155
         $from_address = wpinv_get_option( 'email_from', $this->default_from_address() );
156 156
 
157
-		if ( ! is_email( $from_address ) ) {
158
-			$from_address = $this->default_from_address();
157
+        if ( ! is_email( $from_address ) ) {
158
+            $from_address = $this->default_from_address();
159 159
         }
160 160
 
161 161
         return $from_address;
@@ -163,75 +163,75 @@  discard block
 block discarded – undo
163 163
     }
164 164
 
165 165
     /**
166
-	 * The default emails from address.
167
-	 *
168
-	 * Defaults to wordpress@$sitename
169
-	 * Some hosts will block outgoing mail from this address if it doesn't exist,
170
-	 * but there's no easy alternative. Defaulting to admin_email might appear to be
171
-	 * another option, but some hosts may refuse to relay mail from an unknown domain.
172
-	 *
173
-	 */
174
-	public function default_from_address() {
175
-
176
-		// Get the site domain and get rid of www.
177
-		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
178
-		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
179
-			$sitename = substr( $sitename, 4 );
180
-		}
181
-
182
-		$from_email = 'wordpress@' . $sitename;
183
-
184
-		return apply_filters( 'getpaid_default_from_address', $from_email );
166
+     * The default emails from address.
167
+     *
168
+     * Defaults to wordpress@$sitename
169
+     * Some hosts will block outgoing mail from this address if it doesn't exist,
170
+     * but there's no easy alternative. Defaulting to admin_email might appear to be
171
+     * another option, but some hosts may refuse to relay mail from an unknown domain.
172
+     *
173
+     */
174
+    public function default_from_address() {
175
+
176
+        // Get the site domain and get rid of www.
177
+        $sitename = strtolower( $_SERVER['SERVER_NAME'] );
178
+        if ( substr( $sitename, 0, 4 ) == 'www.' ) {
179
+            $sitename = substr( $sitename, 4 );
180
+        }
181
+
182
+        $from_email = 'wordpress@' . $sitename;
183
+
184
+        return apply_filters( 'getpaid_default_from_address', $from_email );
185 185
 
186 186
     }
187 187
 
188 188
     /**
189
-	 * Get the email reply-to.
190
-	 *
191
-	 *
192
-	 * @return string The email reply-to address.
193
-	 */
194
-	public function get_reply_to() {
189
+     * Get the email reply-to.
190
+     *
191
+     *
192
+     * @return string The email reply-to address.
193
+     */
194
+    public function get_reply_to() {
195 195
 
196
-		$reply_to = current( wpinv_parse_list( wpinv_get_admin_email() ) );
196
+        $reply_to = current( wpinv_parse_list( wpinv_get_admin_email() ) );
197 197
 
198
-		if ( ! is_email( $reply_to ) ) {
199
-			$reply_to = get_option( 'admin_email' );
200
-		}
198
+        if ( ! is_email( $reply_to ) ) {
199
+            $reply_to = get_option( 'admin_email' );
200
+        }
201 201
 
202
-		return $reply_to;
202
+        return $reply_to;
203 203
     }
204 204
 
205 205
     /**
206
-	 * Get the email content type.
207
-	 *
208
-	 */
209
-	public function get_content_type() {
210
-		return apply_filters( 'getpaid_email_content_type', 'text/html', $this );
206
+     * Get the email content type.
207
+     *
208
+     */
209
+    public function get_content_type() {
210
+        return apply_filters( 'getpaid_email_content_type', 'text/html', $this );
211 211
     }
212 212
 
213 213
     /**
214
-	 * Ensures that our email messages are not messed up by template plugins.
215
-	 *
216
-	 * @return array wp_mail_data.
217
-	 */
218
-	public function ensure_email_content( $args ) {
219
-		$args['message'] = $this->wp_mail_data['email'];
220
-		return $args;
214
+     * Ensures that our email messages are not messed up by template plugins.
215
+     *
216
+     * @return array wp_mail_data.
217
+     */
218
+    public function ensure_email_content( $args ) {
219
+        $args['message'] = $this->wp_mail_data['email'];
220
+        return $args;
221 221
     }
222 222
 
223 223
     /**
224
-	 * A little house keeping after an email is sent.
225
-	 *
226
- 	 */
227
-	public function after_sending() {
224
+     * A little house keeping after an email is sent.
225
+     *
226
+     */
227
+    public function after_sending() {
228 228
 
229 229
         do_action( 'getpaid_after_send_email', $this->wp_mail_data );
230
-		remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 );
231
-		remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 );
232
-		remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 );
233
-		remove_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000 );
230
+        remove_filter( 'wp_mail_from', array( $this, 'get_from_address' ), 1000 );
231
+        remove_filter( 'wp_mail_from_name', array( $this, 'get_from_name' ), 1000 );
232
+        remove_filter( 'wp_mail_content_type', array( $this, 'get_content_type' ), 1000 );
233
+        remove_filter( 'wp_mail', array( $this, 'ensure_email_content' ), 1000 );
234 234
 
235
-	}
235
+    }
236 236
 
237 237
 }
Please login to merge, or discard this patch.