| Conditions | 7 |
| Paths | 7 |
| Total Lines | 85 |
| Code Lines | 42 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 47 | public function init() { |
||
| 48 | global $pronamic_pay_errors; |
||
| 49 | |||
| 50 | $pronamic_pay_errors = array(); |
||
| 51 | |||
| 52 | // Nonce. |
||
| 53 | if ( ! filter_has_var( INPUT_POST, 'pronamic_pay_nonce' ) ) { |
||
| 54 | return; |
||
| 55 | } |
||
| 56 | |||
| 57 | $nonce = filter_input( INPUT_POST, 'pronamic_pay_nonce', FILTER_SANITIZE_STRING ); |
||
| 58 | |||
| 59 | if ( ! wp_verify_nonce( $nonce, 'pronamic_pay' ) ) { |
||
| 60 | return; |
||
| 61 | } |
||
| 62 | |||
| 63 | // Validate. |
||
| 64 | $valid = $this->validate(); |
||
| 65 | |||
| 66 | if ( ! $valid ) { |
||
| 67 | return; |
||
| 68 | } |
||
| 69 | |||
| 70 | // Gateway. |
||
| 71 | $id = filter_input( INPUT_POST, 'pronamic_pay_form_id', FILTER_VALIDATE_INT ); |
||
| 72 | |||
| 73 | $config_id = get_post_meta( $id, '_pronamic_payment_form_config_id', true ); |
||
| 74 | |||
| 75 | $gateway = Plugin::get_gateway( $config_id ); |
||
| 76 | |||
| 77 | if ( ! $gateway ) { |
||
| 78 | return; |
||
| 79 | } |
||
| 80 | |||
| 81 | // Data. |
||
| 82 | $data = new PaymentFormData(); |
||
| 83 | |||
| 84 | $payment = Plugin::start( $config_id, $gateway, $data ); |
||
|
|
|||
| 85 | |||
| 86 | $error = $gateway->get_error(); |
||
| 87 | |||
| 88 | if ( $error instanceof WP_Error ) { |
||
| 89 | Plugin::render_errors( $error ); |
||
| 90 | |||
| 91 | exit; |
||
| 92 | } |
||
| 93 | |||
| 94 | // @see https://github.com/WordImpress/Give/blob/1.1/includes/payments/functions.php#L172-L178. |
||
| 95 | // @see https://github.com/woothemes/woocommerce/blob/2.4.3/includes/wc-user-functions.php#L36-L118. |
||
| 96 | $first_name = filter_input( INPUT_POST, 'pronamic_pay_first_name', FILTER_SANITIZE_STRING ); |
||
| 97 | $last_name = filter_input( INPUT_POST, 'pronamic_pay_last_name', FILTER_SANITIZE_STRING ); |
||
| 98 | $email = filter_input( INPUT_POST, 'pronamic_pay_email', FILTER_VALIDATE_EMAIL ); |
||
| 99 | |||
| 100 | $user = get_user_by( 'email', $email ); |
||
| 101 | |||
| 102 | if ( ! $user ) { |
||
| 103 | // Make a random string for password. |
||
| 104 | $password = wp_generate_password( 10 ); |
||
| 105 | |||
| 106 | // Make a user with the username as the email. |
||
| 107 | $user_id = wp_insert_user( |
||
| 108 | array( |
||
| 109 | 'user_login' => $email, |
||
| 110 | 'user_pass' => $password, |
||
| 111 | 'user_email' => $email, |
||
| 112 | 'role' => 'payer', |
||
| 113 | 'first_name' => $first_name, |
||
| 114 | 'last_name' => $last_name, |
||
| 115 | ) |
||
| 116 | ); |
||
| 117 | |||
| 118 | // User. |
||
| 119 | $user = new WP_User( $user_id ); |
||
| 120 | } |
||
| 121 | |||
| 122 | wp_update_post( |
||
| 123 | array( |
||
| 124 | 'ID' => $payment->post->ID, |
||
| 125 | 'post_author' => $user->ID, |
||
| 126 | ) |
||
| 127 | ); |
||
| 128 | |||
| 129 | $gateway->redirect( $payment ); |
||
| 130 | |||
| 131 | exit; |
||
| 132 | } |
||
| 159 |