Total Complexity | 69 |
Total Lines | 480 |
Duplicated Lines | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 0 |
Complex classes like GetPaid_Checkout often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GetPaid_Checkout, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class GetPaid_Checkout { |
||
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 ) { |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * Processes the checkout. |
||
31 | * |
||
32 | */ |
||
33 | public function process_checkout() { |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Validates the submission. |
||
61 | * |
||
62 | */ |
||
63 | protected function validate_submission() { |
||
97 | } |
||
98 | |||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Retrieves submission items. |
||
103 | * |
||
104 | * @return GetPaid_Form_Item[] |
||
105 | */ |
||
106 | protected function get_submission_items() { |
||
107 | |||
108 | $items = $this->payment_form_submission->get_items(); |
||
109 | |||
110 | // Ensure that we have items. |
||
111 | if ( empty( $items ) && ! $this->payment_form_submission->has_fees() ) { |
||
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() { |
||
140 | } |
||
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 ) { |
||
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 | $user = get_current_user_id(); |
||
194 | |||
195 | if ( empty( $user ) ) { |
||
196 | $user = get_user_by( 'email', $submission->get_billing_email() ); |
||
197 | } |
||
198 | |||
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 ) ); |
||
202 | $user = wpinv_create_user( $submission->get_billing_email(), $name ); |
||
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 | } |
||
210 | |||
211 | if ( is_wp_error( $user ) ) { |
||
212 | wp_send_json_error( $user->get_error_message() ); |
||
213 | } |
||
214 | |||
215 | if ( is_numeric( $user ) ) { |
||
216 | return $user; |
||
217 | } |
||
218 | |||
219 | return $user->ID; |
||
220 | |||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Prepares submission data for saving to the database. |
||
225 | * |
||
226 | * @return array |
||
227 | */ |
||
228 | public function prepare_submission_data_for_saving() { |
||
229 | |||
230 | $submission = $this->payment_form_submission; |
||
231 | |||
232 | // Prepared submission details. |
||
233 | $prepared = array( |
||
234 | 'all' => array(), |
||
235 | 'meta' => array(), |
||
236 | ); |
||
237 | |||
238 | // Raw submission details. |
||
239 | $data = $submission->get_data(); |
||
240 | |||
241 | // Loop through the submitted details. |
||
242 | foreach ( $submission->get_payment_form()->get_elements() as $field ) { |
||
243 | |||
244 | // Skip premade fields. |
||
245 | if ( ! empty( $field['premade'] ) ) { |
||
246 | continue; |
||
247 | } |
||
248 | |||
249 | // Ensure address is provided. |
||
250 | if ( 'address' === $field['type'] ) { |
||
251 | $address_type = isset( $field['address_type'] ) && 'shipping' === $field['address_type'] ? 'shipping' : 'billing'; |
||
252 | |||
253 | foreach ( $field['fields'] as $address_field ) { |
||
254 | |||
255 | if ( ! empty( $address_field['visible'] ) && ! empty( $address_field['required'] ) && '' === trim( $_POST[ $address_type ][ $address_field['name'] ] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
||
256 | wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
||
257 | } |
||
258 | } |
||
259 | } |
||
260 | |||
261 | // If it is required and not set, abort. |
||
262 | if ( ! $submission->is_required_field_set( $field ) ) { |
||
263 | wp_send_json_error( __( 'Please fill all required fields.', 'invoicing' ) ); |
||
264 | } |
||
265 | |||
266 | // Handle misc fields. |
||
267 | if ( isset( $data[ $field['id'] ] ) ) { |
||
268 | |||
269 | // Uploads. |
||
270 | if ( 'file_upload' === $field['type'] ) { |
||
271 | $max_file_num = empty( $field['max_file_num'] ) ? 1 : absint( $field['max_file_num'] ); |
||
272 | |||
273 | if ( count( $data[ $field['id'] ] ) > $max_file_num ) { |
||
274 | wp_send_json_error( __( 'Maximum number of allowed files exceeded.', 'invoicing' ) ); |
||
275 | } |
||
276 | |||
277 | $value = array(); |
||
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 | } |
||
286 | |||
287 | $value = implode( ' | ', $value ); |
||
288 | |||
289 | } elseif ( 'checkbox' === $field['type'] ) { |
||
290 | $value = ! empty( $data[ $field['id'] ] ) ? __( 'Yes', 'invoicing' ) : __( 'No', 'invoicing' ); |
||
291 | } else { |
||
292 | $value = wp_kses_post( $data[ $field['id'] ] ); |
||
293 | } |
||
294 | |||
295 | $label = $field['id']; |
||
296 | |||
297 | if ( isset( $field['label'] ) ) { |
||
298 | $label = $field['label']; |
||
299 | } |
||
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 ); |
||
305 | |||
306 | } |
||
307 | } |
||
308 | |||
309 | return $prepared; |
||
310 | |||
311 | } |
||
312 | |||
313 | /** |
||
314 | * Retrieves address details. |
||
315 | * |
||
316 | * @return array |
||
317 | * @param WPInv_Invoice $invoice |
||
318 | * @param string $type |
||
319 | */ |
||
320 | public function prepare_address_details( $invoice, $type = 'billing' ) { |
||
321 | |||
322 | $data = $this->payment_form_submission->get_data(); |
||
323 | $type = sanitize_key( $type ); |
||
324 | $address = array(); |
||
325 | $prepared = array(); |
||
326 | |||
327 | if ( ! empty( $data[ $type ] ) ) { |
||
328 | $address = $data[ $type ]; |
||
329 | } |
||
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 | } |
||
338 | |||
339 | // Filter address details. |
||
340 | $prepared = apply_filters( "getpaid_checkout_{$type}_address", $prepared, $this->payment_form_submission, $invoice ); |
||
341 | |||
342 | // Remove non-whitelisted values. |
||
343 | return array_filter( $prepared, 'getpaid_is_address_field_whitelisted', ARRAY_FILTER_USE_KEY ); |
||
344 | |||
345 | } |
||
346 | |||
347 | /** |
||
348 | * Prepares the billing details. |
||
349 | * |
||
350 | * @return array |
||
351 | * @param WPInv_Invoice $invoice |
||
352 | */ |
||
353 | protected function prepare_billing_info( &$invoice ) { |
||
354 | |||
355 | $billing_address = $this->prepare_address_details( $invoice, 'billing' ); |
||
356 | |||
357 | // Update the invoice with the billing details. |
||
358 | $invoice->set_props( $billing_address ); |
||
359 | |||
360 | } |
||
361 | |||
362 | /** |
||
363 | * Prepares the shipping details. |
||
364 | * |
||
365 | * @return array |
||
366 | * @param WPInv_Invoice $invoice |
||
367 | */ |
||
368 | protected function prepare_shipping_info( $invoice ) { |
||
377 | |||
378 | } |
||
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 ) { |
||
388 | |||
389 | // Ensure the invoice exists. |
||
390 | if ( ! $invoice->exists() ) { |
||
391 | wp_send_json_error( __( 'An error occured while saving your invoice. Please try again.', 'invoicing' ) ); |
||
392 | } |
||
393 | |||
394 | // Save payment form data. |
||
395 | $prepared_payment_form_data = apply_filters( 'getpaid_prepared_payment_form_data', $prepared_payment_form_data, $invoice ); |
||
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 ) ) { |
||
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 | } |
||
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 | } |
||
407 | } |
||
408 | |||
409 | // Save payment form data. |
||
410 | $shipping = apply_filters( 'getpaid_checkout_shipping_details', $shipping, $this->payment_form_submission ); |
||
411 | if ( ! empty( $shipping ) ) { |
||
412 | update_post_meta( $invoice->get_id(), 'shipping_address', $shipping ); |
||
413 | } |
||
414 | |||
415 | // Backwards compatibility. |
||
416 | add_filter( 'wp_redirect', array( $this, 'send_redirect_response' ) ); |
||
417 | |||
418 | try { |
||
419 | $this->process_payment( $invoice ); |
||
420 | } catch ( Exception $e ) { |
||
421 | wpinv_set_error( 'payment_error', $e->getMessage() ); |
||
422 | } |
||
423 | |||
424 | // If we are here, there was an error. |
||
425 | wpinv_send_back_to_checkout( $invoice ); |
||
426 | |||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Processes the actual payment. |
||
431 | * |
||
432 | * @param WPInv_Invoice $invoice |
||
433 | */ |
||
434 | protected function process_payment( $invoice ) { |
||
469 | |||
470 | } |
||
471 | |||
472 | /** |
||
473 | * Marks the invoice as paid in case the checkout is free. |
||
474 | * |
||
475 | * @param WPInv_Invoice $invoice |
||
476 | */ |
||
477 | protected function process_free_payment( $invoice ) { |
||
478 | |||
479 | $invoice->set_gateway( 'none' ); |
||
480 | $invoice->add_note( __( "This is a free invoice and won't be sent to the payment gateway", 'invoicing' ), false, false, true ); |
||
481 | $invoice->mark_paid(); |
||
482 | wpinv_send_to_success_page( array( 'invoice_key' => $invoice->get_key() ) ); |
||
483 | |||
484 | } |
||
485 | |||
486 | /** |
||
487 | * Sends a redrect response to payment details. |
||
488 | * |
||
489 | */ |
||
490 | public function send_redirect_response( $url ) { |
||
493 | } |
||
494 | |||
495 | } |
||
496 |
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.