Completed
Pull Request — master (#10259)
by Mike
08:16
created

WC_Meta_Box_Order_Data   B

Complexity

Total Complexity 53

Size/Duplication

Total Lines 444
Duplicated Lines 15.99 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 53
c 1
b 0
f 0
lcom 1
cbo 3
dl 71
loc 444
rs 7.4757

3 Methods

Rating   Name   Duplication   Size   Complexity  
B init_address_fields() 0 94 1
F output() 51 263 42
C save() 20 54 10

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like WC_Meta_Box_Order_Data 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

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 WC_Meta_Box_Order_Data, and based on these observations, apply Extract Interface, too.

1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 20 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Order Data
4
 *
5
 * Functions for displaying the order data meta box.
6
 *
7
 * @author 		WooThemes
8
 * @category 	Admin
9
 * @package 	WooCommerce/Admin/Meta Boxes
10
 * @version     2.2.0
11
 */
12
13
if ( ! defined( 'ABSPATH' ) ) {
14
	exit; // Exit if accessed directly
15
}
16
17
/**
18
 * WC_Meta_Box_Order_Data Class.
19
 */
20
class WC_Meta_Box_Order_Data {
21
22
	/**
23
	 * Billing fields.
24
	 *
25
	 * @var array
26
	 */
27
	protected static $billing_fields = array();
28
29
	/**
30
	 * Shipping fields.
31
	 *
32
	 * @var array
33
	 */
34
	protected static $shipping_fields = array();
35
36
	/**
37
	 * Init billing and shipping fields we display + save.
38
	 */
39
	public static function init_address_fields() {
40
41
		self::$billing_fields = apply_filters( 'woocommerce_admin_billing_fields', array(
42
			'first_name' => array(
43
				'label' => __( 'First Name', 'woocommerce' ),
44
				'show'  => false
45
			),
46
			'last_name' => array(
47
				'label' => __( 'Last Name', 'woocommerce' ),
48
				'show'  => false
49
			),
50
			'company' => array(
51
				'label' => __( 'Company', 'woocommerce' ),
52
				'show'  => false
53
			),
54
			'address_1' => array(
55
				'label' => __( 'Address 1', 'woocommerce' ),
56
				'show'  => false
57
			),
58
			'address_2' => array(
59
				'label' => __( 'Address 2', 'woocommerce' ),
60
				'show'  => false
61
			),
62
			'city' => array(
63
				'label' => __( 'City', 'woocommerce' ),
64
				'show'  => false
65
			),
66
			'postcode' => array(
67
				'label' => __( 'Postcode', 'woocommerce' ),
68
				'show'  => false
69
			),
70
			'country' => array(
71
				'label'   => __( 'Country', 'woocommerce' ),
72
				'show'    => false,
73
				'class'   => 'js_field-country select short',
74
				'type'    => 'select',
75
				'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_allowed_countries()
76
			),
77
			'state' => array(
78
				'label' => __( 'State/County', 'woocommerce' ),
79
				'class'   => 'js_field-state select short',
80
				'show'  => false
81
			),
82
			'email' => array(
83
				'label' => __( 'Email', 'woocommerce' ),
84
			),
85
			'phone' => array(
86
				'label' => __( 'Phone', 'woocommerce' ),
87
			),
88
		) );
89
90
		self::$shipping_fields = apply_filters( 'woocommerce_admin_shipping_fields', array(
91
			'first_name' => array(
92
				'label' => __( 'First Name', 'woocommerce' ),
93
				'show'  => false
94
			),
95
			'last_name' => array(
96
				'label' => __( 'Last Name', 'woocommerce' ),
97
				'show'  => false
98
			),
99
			'company' => array(
100
				'label' => __( 'Company', 'woocommerce' ),
101
				'show'  => false
102
			),
103
			'address_1' => array(
104
				'label' => __( 'Address 1', 'woocommerce' ),
105
				'show'  => false
106
			),
107
			'address_2' => array(
108
				'label' => __( 'Address 2', 'woocommerce' ),
109
				'show'  => false
110
			),
111
			'city' => array(
112
				'label' => __( 'City', 'woocommerce' ),
113
				'show'  => false
114
			),
115
			'postcode' => array(
116
				'label' => __( 'Postcode', 'woocommerce' ),
117
				'show'  => false
118
			),
119
			'country' => array(
120
				'label'   => __( 'Country', 'woocommerce' ),
121
				'show'    => false,
122
				'type'    => 'select',
123
				'class'   => 'js_field-country select short',
124
				'options' => array( '' => __( 'Select a country&hellip;', 'woocommerce' ) ) + WC()->countries->get_shipping_countries()
125
			),
126
			'state' => array(
127
				'label' => __( 'State/County', 'woocommerce' ),
128
				'class'   => 'js_field-state select short',
129
				'show'  => false
130
			),
131
		) );
132
	}
133
134
	/**
135
	 * Output the metabox.
136
	 *
137
	 * @param WP_Post $post
138
	 */
139
	public static function output( $post ) {
140
		global $theorder;
141
142
		if ( ! is_object( $theorder ) ) {
143
			$theorder = wc_get_order( $post->ID );
144
		}
145
146
		$order = $theorder;
147
148
		self::init_address_fields();
149
150
		if ( WC()->payment_gateways() ) {
151
			$payment_gateways = WC()->payment_gateways->payment_gateways();
152
		} else {
153
			$payment_gateways = array();
154
		}
155
156
		$payment_method    = $order->get_payment_method();
157
		$order_type_object = get_post_type_object( $post->post_type );
158
		wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
159
		?>
160
		<style type="text/css">
161
			#post-body-content, #titlediv { display:none }
162
		</style>
163
		<div class="panel-wrap woocommerce">
164
			<input name="post_title" type="hidden" value="<?php echo empty( $post->post_title ) ? __( 'Order', 'woocommerce' ) : esc_attr( $post->post_title ); ?>" />
165
			<input name="post_status" type="hidden" value="<?php echo esc_attr( $post->post_status ); ?>" />
166
			<div id="order_data" class="panel">
167
168
				<h2><?php echo esc_html( sprintf( _x( '%s #%s details', 'Order #123 details', 'woocommerce' ), $order_type_object->labels->singular_name, $order->get_order_number() ) ); ?></h2>
169
				<p class="order_number"><?php
170
171
					if ( $payment_method ) {
172
						printf( __( 'Payment via %s', 'woocommerce' ), ( isset( $payment_gateways[ $payment_method ] ) ? esc_html( $payment_gateways[ $payment_method ]->get_title() ) : esc_html( $payment_method ) ) );
173
174
						if ( $transaction_id = $order->get_transaction_id() ) {
175
								if ( isset( $payment_gateways[ $payment_method ] ) && ( $url = $payment_gateways[ $payment_method ]->get_transaction_url( $order ) ) ) {
176
								echo ' (<a href="' . esc_url( $url ) . '" target="_blank">' . esc_html( $transaction_id ) . '</a>)';
177
							} else {
178
								echo ' (' . esc_html( $transaction_id ) . ')';
179
							}
180
						}
181
						echo '. ';
182
					}
183
184 View Code Duplication
					if ( $ip_address = get_post_meta( $post->ID, '_customer_ip_address', true ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
185
						echo __( 'Customer IP', 'woocommerce' ) . ': ' . esc_html( $ip_address );
186
					}
187
				?></p>
188
189
				<div class="order_data_column_container">
190
					<div class="order_data_column">
191
						<h3><?php _e( 'General Details', 'woocommerce' ); ?></h3>
192
193
						<p class="form-field form-field-wide"><label for="order_date"><?php _e( 'Order date:', 'woocommerce' ) ?></label>
194
							<input type="text" class="date-picker" name="order_date" id="order_date" maxlength="10" value="<?php echo date_i18n( 'Y-m-d', strtotime( $post->post_date ) ); ?>" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />@<input type="text" class="hour" placeholder="<?php esc_attr_e( 'h', 'woocommerce' ) ?>" name="order_date_hour" id="order_date_hour" maxlength="2" size="2" value="<?php echo date_i18n( 'H', strtotime( $post->post_date ) ); ?>" pattern="\-?\d+(\.\d{0,})?" />:<input type="text" class="minute" placeholder="<?php esc_attr_e( 'm', 'woocommerce' ) ?>" name="order_date_minute" id="order_date_minute" maxlength="2" size="2" value="<?php echo date_i18n( 'i', strtotime( $post->post_date ) ); ?>" pattern="\-?\d+(\.\d{0,})?" />
195
						</p>
196
197
						<p class="form-field form-field-wide wc-order-status"><label for="order_status"><?php _e( 'Order status:', 'woocommerce' ) ?> <?php
198
							if ( $order->has_status( 'pending' ) ) {
199
								printf( '<a href="%s">%s &rarr;</a>',
200
									esc_url( $order->get_checkout_payment_url() ),
201
									__( 'Pay', 'woocommerce' )
202
								);
203
							}
204
						?></label>
205
						<select id="order_status" name="order_status" class="wc-enhanced-select">
206
							<?php
207
								$statuses = wc_get_order_statuses();
208
								foreach ( $statuses as $status => $status_name ) {
209
									echo '<option value="' . esc_attr( $status ) . '" ' . selected( $status, 'wc-' . $order->get_status(), false ) . '>' . esc_html( $status_name ) . '</option>';
210
								}
211
							?>
212
						</select></p>
213
214
						<p class="form-field form-field-wide wc-customer-user">
215
							<label for="customer_user"><?php _e( 'Customer:', 'woocommerce' ) ?> <?php
216
								if ( $order->get_customer_id() ) {
217
									$args = array( 'post_status' => 'all',
218
										'post_type'      => 'shop_order',
219
										'_customer_user' => $order->get_customer_id()
220
									);
221
									printf( '<a href="%s">%s &rarr;</a>',
222
										esc_url( add_query_arg( $args, admin_url( 'edit.php' ) ) ),
223
										__( 'View other orders', 'woocommerce' )
224
									);
225
								}
226
							?></label>
227
							<?php
228
							$user_string = '';
229
							$user_id     = '';
230
							if ( $order->get_customer_id() ) {
231
								$user_id     = $order->get_customer_id();
232
								$user        = get_user_by( 'id', $user_id );
233
								$user_string = esc_html( $user->display_name ) . ' (#' . absint( $user->ID ) . ' &ndash; ' . esc_html( $user->user_email ) . ')';
234
							}
235
							?>
236
							<input type="hidden" class="wc-customer-search" id="customer_user" name="customer_user" data-placeholder="<?php esc_attr_e( 'Guest', 'woocommerce' ); ?>" data-selected="<?php echo htmlspecialchars( $user_string ); ?>" value="<?php echo $user_id; ?>" data-allow_clear="true" />
237
						</p>
238
						<?php do_action( 'woocommerce_admin_order_data_after_order_details', $order ); ?>
239
					</div>
240
					<div class="order_data_column">
241
						<h3>
242
							<?php _e( 'Billing Details', 'woocommerce' ); ?>
243
							<a href="#" class="edit_address"><?php _e( 'Edit', 'woocommerce' ); ?></a>
244
							<a href="#" class="tips load_customer_billing" data-tip="<?php esc_attr_e( 'Load billing address', 'woocommerce' ); ?>" style="display:none;"><?php _e( 'Load billing address', 'woocommerce' ); ?></a>
245
						</h3>
246
						<?php
247
							// Display values
248
							echo '<div class="address">';
249
250 View Code Duplication
								if ( $order->get_formatted_billing_address() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
251
									echo '<p><strong>' . __( 'Address', 'woocommerce' ) . ':</strong>' . wp_kses( $order->get_formatted_billing_address(), array( 'br' => array() ) ) . '</p>';
252
								} else {
253
									echo '<p class="none_set"><strong>' . __( 'Address', 'woocommerce' ) . ':</strong> ' . __( 'No billing address set.', 'woocommerce' ) . '</p>';
254
								}
255
256
								foreach ( self::$billing_fields as $key => $field ) {
257
									if ( isset( $field['show'] ) && false === $field['show'] ) {
258
										continue;
259
									}
260
261
									$field_name = 'billing_' . $key;
262
263
									if ( is_callable( array( $order, "get_{$field_name}" ) ) ) {
264
										echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . make_clickable( esc_html( $order->{"get_{$field_name}"}() ) ) . '</p>';
265
									}
266
								}
267
268
							echo '</div>';
269
270
							// Display form
271
							echo '<div class="edit_address">';
272
273 View Code Duplication
							foreach ( self::$billing_fields as $key => $field ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
274
								if ( ! isset( $field['type'] ) ) {
275
									$field['type'] = 'text';
276
								}
277
								if ( ! isset( $field['id'] ) ){
278
									$field['id'] = '_billing_' . $key;
279
								}
280
								switch ( $field['type'] ) {
281
									case 'select' :
282
										woocommerce_wp_select( $field );
283
									break;
284
									default :
285
										woocommerce_wp_text_input( $field );
286
									break;
287
								}
288
							}
289
							?>
290
							<p class="form-field form-field-wide">
291
								<label><?php _e( 'Payment Method:', 'woocommerce' ); ?></label>
292
								<select name="_payment_method" id="_payment_method" class="first">
293
									<option value=""><?php _e( 'N/A', 'woocommerce' ); ?></option>
294
									<?php
295
										$found_method 	= false;
296
297
										foreach ( $payment_gateways as $gateway ) {
298
											if ( $gateway->enabled == "yes" ) {
299
												echo '<option value="' . esc_attr( $gateway->id ) . '" ' . selected( $payment_method, $gateway->id, false ) . '>' . esc_html( $gateway->get_title() ) . '</option>';
300
												if ( $payment_method == $gateway->id ) {
301
													$found_method = true;
302
												}
303
											}
304
										}
305
306 View Code Duplication
										if ( ! $found_method && ! empty( $payment_method ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
307
											echo '<option value="' . esc_attr( $payment_method ) . '" selected="selected">' . __( 'Other', 'woocommerce' ) . '</option>';
308
										} else {
309
											echo '<option value="other">' . __( 'Other', 'woocommerce' ) . '</option>';
310
										}
311
									?>
312
								</select>
313
							</p>
314
							<?php
315
316
							woocommerce_wp_text_input( array( 'id' => '_transaction_id', 'label' => __( 'Transaction ID', 'woocommerce' ) ) );
317
318
							echo '</div>';
319
320
							do_action( 'woocommerce_admin_order_data_after_billing_address', $order );
321
						?>
322
					</div>
323
					<div class="order_data_column">
324
325
						<h3>
326
							<?php _e( 'Shipping Details', 'woocommerce' ); ?>
327
							<a href="#" class="edit_address"><?php _e( 'Edit', 'woocommerce' ); ?></a>
328
							<a href="#" class="tips billing-same-as-shipping" data-tip="<?php esc_attr_e( 'Copy from billing', 'woocommerce' ); ?>" style="display:none;"><?php _e( 'Copy from billing', 'woocommerce' ); ?></a>
329
							<a href="#" class="tips load_customer_shipping" data-tip="<?php esc_attr_e( 'Load shipping address', 'woocommerce' ); ?>" style="display:none;"><?php _e( 'Load shipping address', 'woocommerce' ); ?></a>
330
						</h3>
331
						<?php
332
							// Display values
333
							echo '<div class="address">';
334
335 View Code Duplication
								if ( $order->get_formatted_shipping_address() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
336
									echo '<p><strong>' . __( 'Address', 'woocommerce' ) . ':</strong>' . wp_kses( $order->get_formatted_shipping_address(), array( 'br' => array() ) ) . '</p>';
337
								} else {
338
									echo '<p class="none_set"><strong>' . __( 'Address', 'woocommerce' ) . ':</strong> ' . __( 'No shipping address set.', 'woocommerce' ) . '</p>';
339
								}
340
341
								if ( ! empty( self::$shipping_fields ) ) {
342
									foreach ( self::$shipping_fields as $key => $field ) {
343
										if ( isset( $field['show'] ) && false === $field['show'] ) {
344
											continue;
345
										}
346
347
										$field_name = 'shipping_' . $key;
348
349
										if ( ! empty( $order->$field_name ) ) {
350
											echo '<p><strong>' . esc_html( $field['label'] ) . ':</strong> ' . make_clickable( esc_html( $order->$field_name ) ) . '</p>';
351
										}
352
									}
353
								}
354
355
								if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' == get_option( 'woocommerce_enable_order_comments', 'yes' ) ) && $post->post_excerpt ) {
356
									echo '<p><strong>' . __( 'Customer Provided Note', 'woocommerce' ) . ':</strong> ' . nl2br( esc_html( $post->post_excerpt ) ) . '</p>';
357
								}
358
359
							echo '</div>';
360
361
							// Display form
362
							echo '<div class="edit_address">';
363
364
							if ( ! empty( self::$shipping_fields ) ) {
365 View Code Duplication
								foreach ( self::$shipping_fields as $key => $field ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
									if ( ! isset( $field['type'] ) ) {
367
										$field['type'] = 'text';
368
									}
369
									if ( ! isset( $field['id'] ) ){
370
										$field['id'] = '_shipping_' . $key;
371
									}
372
373
									switch ( $field['type'] ) {
374
										case 'select' :
375
											woocommerce_wp_select( $field );
376
										break;
377
										default :
378
											woocommerce_wp_text_input( $field );
379
										break;
380
									}
381
								}
382
							}
383
384
							if ( apply_filters( 'woocommerce_enable_order_notes_field', 'yes' == get_option( 'woocommerce_enable_order_comments', 'yes' ) ) ) {
385
								?>
386
								<p class="form-field form-field-wide"><label for="excerpt"><?php _e( 'Customer Provided Note', 'woocommerce' ) ?>:</label>
387
								<textarea rows="1" cols="40" name="excerpt" tabindex="6" id="excerpt" placeholder="<?php esc_attr_e( 'Customer\'s notes about the order', 'woocommerce' ); ?>"><?php echo wp_kses_post( $post->post_excerpt ); ?></textarea></p>
388
								<?php
389
							}
390
391
							echo '</div>';
392
393
							do_action( 'woocommerce_admin_order_data_after_shipping_address', $order );
394
						?>
395
					</div>
396
				</div>
397
				<div class="clear"></div>
398
			</div>
399
		</div>
400
		<?php
401
	}
402
403
	/**
404
	 * Save meta box data.
405
	 *
406
	 * @param int $post_id
407
	 * @param WP_Post $post
408
	 */
409
	public static function save( $post_id, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
410
		global $wpdb;
411
412
		self::init_address_fields();
413
414
		// Ensure gateways are loaded in case they need to insert data into the emails
415
		WC()->payment_gateways();
416
		WC()->shipping();
417
		
418
		$order = wc_get_order( $post_id );
419
		$order->set_customer_id( absint( $_POST['customer_user'] ) );
420
		$order->set_transaction_id( wc_clean( $_POST['_transaction_id'] ) );
1 ignored issue
show
Bug introduced by
It seems like wc_clean($_POST['_transaction_id']) targeting wc_clean() can also be of type array; however, WC_Order::set_transaction_id() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
421
		$order->set_status( wc_clean( $_POST['order_status'] ), false, true );
1 ignored issue
show
Bug introduced by
It seems like wc_clean($_POST['order_status']) targeting wc_clean() can also be of type array; however, WC_Order::set_status() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
422
423
		// Billing address fields
424 View Code Duplication
		foreach ( self::$billing_fields as $key => $field ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
425
			if ( ! isset( $field['id'] ) ){
426
				$field['id'] = '_billing_' . $key;
427
			}
428
			if ( is_callable( array( $order, "set{$field['id']}" ) ) ) {
429
				$order->{"set{$field['id']}"}( wc_clean( $_POST[ $field['id'] ] ) );
430
			} else {
431
				$order->add_meta_data( $field['id'], wc_clean( $_POST[ $field['id'] ] ) );
432
			}
433
		}
434
435
		// Shipping address fields
436 View Code Duplication
		foreach ( self::$shipping_fields as $key => $field ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
437
			if ( ! isset( $field['id'] ) ){
438
				$field['id'] = '_shipping_' . $key;
439
			}
440
			if ( is_callable( array( $order, "set{$field['id']}" ) ) ) {
441
				$order->{"set{$field['id']}"}( wc_clean( $_POST[ $field['id'] ] ) );
442
			} else {
443
				$order->add_meta_data( $field['id'], wc_clean( $_POST[ $field['id'] ] ) );
444
			}
445
		}
446
447
		if ( $order->get_payment_method() !== wc_clean( $_POST['_payment_method'] ) ) {
448
			$methods              = WC()->payment_gateways->payment_gateways();
449
			$payment_method       = wc_clean( $_POST['_payment_method'] );
450
			$payment_method_title = isset( $methods[ $payment_method ] ) ? $methods[ $payment_method ]->get_title() : $payment_method;
451
			$order->set_payment_method( $payment_method );
1 ignored issue
show
Bug introduced by
It seems like $payment_method defined by wc_clean($_POST['_payment_method']) on line 449 can also be of type array; however, WC_Order::set_payment_method() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
452
			$order->set_payment_method_title( $payment_method_title );
453
		}
454
455
		if ( empty( $_POST['order_date'] ) ) {
456
			$order->set_date_created( current_time( 'timestamp' ) );
457
		} else {
458
			$order->set_date_created( strtotime( $_POST['order_date'] . ' ' . (int) $_POST['order_date_hour'] . ':' . (int) $_POST['order_date_minute'] . ':00' ) );
459
		}
460
461
		$order->save();
462
	}
463
}
464