Completed
Push — develop ( 88c487...db0b89 )
by Remco
05:00
created

FormProcessor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Form Processor
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2018 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Pay\Forms
9
 */
10
11
namespace Pronamic\WordPress\Pay\Forms;
12
13
use Pronamic\WordPress\Pay\Plugin;
14
use WP_Error;
15
use WP_User;
16
17
/**
18
 * Form Processor
19
 *
20
 * @author Remco Tolsma
21
 * @version 3.7.0
22
 * @since 3.7.0
23
 */
24
class FormProcessor {
25
	/**
26
	 * Plugin.
27
	 *
28
	 * @var Plugin
29
	 */
30
	private $plugin;
31
32
	/**
33
	 * Constructs and initalize an form processor object.
34
	 *
35
	 * @param Plugin $plugin Plugin.
36
	 */
37
	public function __construct( $plugin ) {
38
		$this->plugin = $plugin;
39
40
		// Actions.
41
		add_action( 'init', array( $this, 'init' ) );
42
	}
43
44
	/**
45
	 * Initialize.
46
	 */
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 );
0 ignored issues
show
Bug introduced by
It seems like $config_id can also be of type false; however, parameter $config_id of Pronamic\WordPress\Pay\Plugin::start() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

84
		$payment = Plugin::start( /** @scrutinizer ignore-type */ $config_id, $gateway, $data );
Loading history...
85
86
		$error = $gateway->get_error();
87
88
		if ( $error instanceof WP_Error ) {
0 ignored issues
show
introduced by
$error is always a sub-type of WP_Error.
Loading history...
89
			Plugin::render_errors( $error );
0 ignored issues
show
Bug introduced by
$error of type WP_Error is incompatible with the type array expected by parameter $errors of Pronamic\WordPress\Pay\Plugin::render_errors(). ( Ignorable by Annotation )

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

89
			Plugin::render_errors( /** @scrutinizer ignore-type */ $error );
Loading history...
90
91
			exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

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

Loading history...
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;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

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

Loading history...
132
	}
133
134
	/**
135
	 * Validate.
136
	 *
137
	 * @return boolean True if valid, false otherwise.
138
	 */
139
	private function validate() {
140
		global $pronamic_pay_errors;
141
142
		// First Name.
143
		$first_name = filter_input( INPUT_POST, 'pronamic_pay_first_name', FILTER_SANITIZE_STRING );
144
145
		if ( empty( $first_name ) ) {
146
			$pronamic_pay_errors['first_name'] = __( 'Please enter your first name', 'pronamic_ideal' );
147
		}
148
149
		// E-mail.
150
		$email = filter_input( INPUT_POST, 'pronamic_pay_email', FILTER_VALIDATE_EMAIL );
151
152
		if ( empty( $email ) ) {
153
			$pronamic_pay_errors['email'] = __( 'Please enter a valid email address', 'pronamic_ideal' );
154
		}
155
156
		return empty( $pronamic_pay_errors );
157
	}
158
}
159