Completed
Push — master ( d786b7...986814 )
by Justin
06:40 queued 01:07
created

WPSC_Checkout_Form_Data   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 244
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 244
rs 10
c 0
b 0
f 0
wmc 27
lcom 1
cbo 3

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fetch() 0 36 5
A get_raw_data() 0 3 1
A prepare_get() 0 3 1
A prepare_get_data() 0 3 1
D get_gateway_data() 0 44 9
A set() 0 11 2
A save() 0 12 1
B save_form() 0 46 6
1
<?php
2
/**
3
 * The WP eCommerce Checkout form data Class
4
 *
5
 * @package wp-e-commerce
6
 * @since 3.8
7
 */
8
9
class WPSC_Checkout_Form_Data extends WPSC_Query_Base {
10
	private $raw_data	   = array();
11
	private $gateway_data   = array();
12
	private $submitted_data = array();
13
	private $log_id;
14
15
	/**
16
	 * An array of arrays of cache keys. Allows versioning the cached values,
17
	 * and busting cache for a group if needed (by incrementing the version).
18
	 *
19
	 * @var array
20
	 */
21
	protected $group_ids = array(
22
		'raw_data' => array(
23
			'group'	 => 'wpsc_checkout_form_raw_data',
24
			'version' => 1,
25
		),
26
		'gateway_data' => array(
27
			'group'	 => 'wpsc_checkout_form_gateway_data',
28
			'version' => 0,
29
		),
30
	);
31
32
	public function __construct( $log_id ) {
33
		$this->log_id = absint( $log_id );
34
		$this->fetch();
35
	}
36
37
	/**
38
	 * Fetches the actual $data array.
39
	 *
40
	 * @access protected
41
	 * @since 4.0
42
	 *
43
	 * @return void
44
	 */
45
	protected function fetch() {
46
		if ( $this->fetched ) {
47
			return;
48
		}
49
50
		global $wpdb;
51
52
		if ( ! $this->raw_data = $this->cache_get( $this->log_id, 'raw_data' ) ) {
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cache_get($this->log_id, 'raw_data') of type boolean is incompatible with the declared type array of property $raw_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53
			$sql = "
54
				SELECT c.id, c.name, c.type, c.mandatory, c.unique_name, c.checkout_set as form_group, s.id as data_id, s.value
55
				FROM " . WPSC_TABLE_SUBMITTED_FORM_DATA . " AS s
56
				INNER JOIN " . WPSC_TABLE_CHECKOUT_FORMS . " AS c
57
					ON c.id = s.form_id
58
				WHERE s.log_id = %d AND active = '1'
59
			";
60
61
			$sql = $wpdb->prepare( $sql, $this->log_id );
62
			$this->raw_data = $wpdb->get_results( $sql );
63
			$this->exists   = ! empty( $this->raw_data );
0 ignored issues
show
Documentation Bug introduced by
The property $exists was declared of type string, but !empty($this->raw_data) is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
64
65
			// Set the cache for raw checkout for data
66
			$this->cache_set( $this->log_id, $this->raw_data, 'raw_data' );
67
		}
68
69
		// At the moment, only core fields have unique_name. In the future, all fields will have
70
		// a unique name rather than just IDs.
71
		foreach ( $this->raw_data as $field ) {
72
			if ( ! empty( $field->unique_name ) ) {
73
				$this->data[ $field->unique_name ] = $field->value;
74
			}
75
		}
76
77
		do_action( 'wpsc_checkout_form_data_fetched', $this );
78
79
		$this->fetched = true;
0 ignored issues
show
Documentation Bug introduced by
The property $fetched was declared of type string, but true is of type boolean. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
80
	}
81
82
	public function get_raw_data() {
83
		return $this->raw_data;
84
	}
85
86
	/**
87
	 * Prepares the return value for get() (apply_filters, etc).
88
	 *
89
	 * @access protected
90
	 * @since  4.0
91
	 *
92
	 * @param  mixed  $value Value fetched
93
	 * @param  string $key   Key for $data.
94
	 *
95
	 * @return mixed
96
	 */
97
	protected function prepare_get( $value, $key ) {
98
		return apply_filters( 'wpsc_checkout_form_data_get_property', $value, $key, $this );
99
	}
100
101
	/**
102
	 * Prepares the return value for get_data() (apply_filters, etc).
103
	 *
104
	 * @access protected
105
	 * @since  4.0
106
	 *
107
	 * @return mixed
108
	 */
109
	protected function prepare_get_data() {
110
		return apply_filters( 'wpsc_checkout_form_get_data', $this->data, $this->log_id, $this );
111
	}
112
113
	public function get_gateway_data() {
114
		if ( ! $this->gateway_data = $this->cache_get( $this->log_id, 'gateway_data' ) ) {
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->cache_get($this->log_id, 'gateway_data') of type boolean is incompatible with the declared type array of property $gateway_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
115
			$map = array(
116
				'firstname' => 'first_name',
117
				'lastname'  => 'last_name',
118
				'address'   => 'street',
119
				'city'	  => 'city',
120
				'state'	 => 'state',
121
				'country'   => 'country',
122
				'postcode'  => 'zip',
123
				'phone'	 => 'phone',
124
			);
125
126
			foreach ( array( 'shipping', 'billing' ) as $type ) {
127
128
				$data_key = "{$type}_address";
129
				$this->gateway_data[ $data_key ] = array();
130
131
				foreach ( $map as $key => $new_key ) {
132
					$key = $type . $key;
133
134
					if ( isset( $this->data[ $key ] ) ) {
135
						$value = $this->data [$key ];
136
137
						if ( $new_key == 'state' && is_numeric( $value ) ) {
138
							$value = wpsc_get_state_by_id( $value, 'code' );
139
						}
140
141
						$this->gateway_data[ $data_key ][ $new_key ] = $value;
142
					}
143
				}
144
145
				$name  = isset( $this->gateway_data[ $data_key ]['first_name'] ) ? $this->gateway_data[ $data_key ]['first_name'] . ' ' : '';
146
				$name .= isset( $this->gateway_data[ $data_key ]['last_name']  ) ? $this->gateway_data[ $data_key ]['last_name']		: '';
147
148
				$this->gateway_data[ $data_key ]['name'] = trim( $name );
149
			}
150
151
			// Sets the cache for checkout form gateway data
152
			$this->cache_set( $this->log_id, $this->gateway_data, 'gateway_data' );
153
		}
154
155
		return apply_filters( 'wpsc_checkout_form_gateway_data', $this->gateway_data, $this->log_id );
156
	}
157
158
	/**
159
	 * Set specific database fields.
160
	 *
161
	 * @param string|int $key   Expects either form ID or unique name.
162
	 * @param string	 $value Value to be set for field.
163
	 *
164
	 * @since  4.0
165
	 * @return WPSC_Checkout_Form_Data Current instance of form data.
166
	 */
167
	public function set( $key, $value = '' ) {
168
169
		if ( ! is_numeric( $key ) ) {
170
			$checkout_form = WPSC_Checkout_Form::get();
171
			$key = $checkout_form->get_field_id_by_unique_name( $key );
172
		}
173
174
		$this->submitted_data[ $key ] = $value;
175
176
		return $this;
177
	}
178
179
	/**
180
	 * Used in conjunction with set() method, saves individual checkout form fields to database.
181
	 *
182
	 * @since  4.0
183
	 * @return void
184
	 */
185
	public function save() {
186
187
		$log	= new WPSC_Purchase_Log( $this->log_id );
188
		$form   = WPSC_Checkout_Form::get();
189
		$fields = $form->get_fields();
190
191
		$original_data = wp_list_pluck( $this->get_raw_data(), 'value', 'id' );
192
193
		$this->submitted_data = array_replace( $original_data, $this->submitted_data );
194
195
		return self::save_form( $log, $fields, $this->submitted_data );
196
	}
197
198
	/**
199
	 * Save Submitted Form Fields to the wpsc_submited_form_data table.
200
	 *
201
	 * @param WPSC_Purchase_Log $purchase_log
202
	 * @param array $fields
203
	 * @return void
204
	 */
205
	public static function save_form( $purchase_log, $fields, $data = array() ) {
206
		global $wpdb;
207
208
		$log_id = $purchase_log->get( 'id' );
209
210
		// delete previous field values
211
		$sql = $wpdb->prepare( "DELETE FROM " . WPSC_TABLE_SUBMITTED_FORM_DATA . " WHERE log_id = %d", $log_id );
212
		$wpdb->query( $sql );
213
214
		if ( empty( $data ) && isset( $_POST['wpsc_checkout_details'] ) ) {
215
			$data = $_POST['wpsc_checkout_details'];
216
		}
217
218
		$customer_details = array();
219
220
		foreach ( $fields as $field ) {
221
222
			if ( $field->type == 'heading' ) {
223
				continue;
224
			}
225
226
			$value = '';
227
228
			if ( isset( $data[ $field->id ] ) ) {
229
				$value = wp_unslash( $data[ $field->id ] );
230
			}
231
232
			$customer_details[ $field->id ] = $value;
233
234
			$wpdb->insert(
235
				WPSC_TABLE_SUBMITTED_FORM_DATA,
236
				array(
237
					'log_id'  => $log_id,
238
					'form_id' => $field->id,
239
					'value'   => $value,
240
				),
241
				array(
242
					'%d',
243
					'%d',
244
					'%s',
245
				)
246
			);
247
		}
248
249
		wpsc_save_customer_details( $customer_details );
250
	}
251
252
}
253