Completed
Push — master ( 09e43c...a8e479 )
by Mike
11:28
created

wc-admin-functions.php ➔ wc_save_order_items()   D

Complexity

Conditions 28
Paths 4

Size

Total Lines 80
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 28
eloc 45
c 2
b 0
f 1
nc 4
nop 2
dl 0
loc 80
rs 4.9612

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * WooCommerce Admin Functions
4
 *
5
 * @author   WooThemes
6
 * @category Core
7
 * @package  WooCommerce/Admin/Functions
8
 * @version  2.4.0
9
 */
10
11
if ( ! defined( 'ABSPATH' ) ) {
12
	exit; // Exit if accessed directly
13
}
14
15
/**
16
 * Get all WooCommerce screen ids.
17
 *
18
 * @return array
19
 */
20
function wc_get_screen_ids() {
21
22
	$wc_screen_id = sanitize_title( __( 'WooCommerce', 'woocommerce' ) );
23
	$screen_ids   = array(
24
		'toplevel_page_' . $wc_screen_id,
25
		$wc_screen_id . '_page_wc-reports',
26
		$wc_screen_id . '_page_wc-shipping',
27
		$wc_screen_id . '_page_wc-settings',
28
		$wc_screen_id . '_page_wc-status',
29
		$wc_screen_id . '_page_wc-addons',
30
		'toplevel_page_wc-reports',
31
		'product_page_product_attributes',
32
		'edit-product',
33
		'product',
34
		'edit-shop_coupon',
35
		'shop_coupon',
36
		'edit-product_cat',
37
		'edit-product_tag',
38
		'profile',
39
		'user-edit'
40
	);
41
42
	foreach ( wc_get_order_types() as $type ) {
43
		$screen_ids[] = $type;
44
		$screen_ids[] = 'edit-' . $type;
45
	}
46
47
	return apply_filters( 'woocommerce_screen_ids', $screen_ids );
48
}
49
50
/**
51
 * Create a page and store the ID in an option.
52
 *
53
 * @param mixed $slug Slug for the new page
54
 * @param string $option Option name to store the page's ID
55
 * @param string $page_title (default: '') Title for the new page
56
 * @param string $page_content (default: '') Content for the new page
57
 * @param int $post_parent (default: 0) Parent for the new page
58
 * @return int page ID
59
 */
60
function wc_create_page( $slug, $option = '', $page_title = '', $page_content = '', $post_parent = 0 ) {
61
	global $wpdb;
62
63
	$option_value     = get_option( $option );
64
65
	if ( $option_value > 0 && ( $page_object = get_post( $option_value ) ) ) {
66
		if ( 'page' === $page_object->post_type && ! in_array( $page_object->post_status, array( 'pending', 'trash', 'future', 'auto-draft' ) ) ) {
67
			// Valid page is already in place
68
			return $page_object->ID;
69
		}
70
	}
71
72 View Code Duplication
	if ( strlen( $page_content ) > 0 ) {
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...
73
		// Search for an existing page with the specified page content (typically a shortcode)
74
		$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' ) AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
75
	} else {
76
		// Search for an existing page with the specified page slug
77
		$valid_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status NOT IN ( 'pending', 'trash', 'future', 'auto-draft' )  AND post_name = %s LIMIT 1;", $slug ) );
78
	}
79
80
	$valid_page_found = apply_filters( 'woocommerce_create_page_id', $valid_page_found, $slug, $page_content );
81
82
	if ( $valid_page_found ) {
83
		if ( $option ) {
84
			update_option( $option, $valid_page_found );
85
		}
86
		return $valid_page_found;
87
	}
88
89
	// Search for a matching valid trashed page
90 View Code Duplication
	if ( strlen( $page_content ) > 0 ) {
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...
91
		// Search for an existing page with the specified page content (typically a shortcode)
92
		$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_content LIKE %s LIMIT 1;", "%{$page_content}%" ) );
93
	} else {
94
		// Search for an existing page with the specified page slug
95
		$trashed_page_found = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type='page' AND post_status = 'trash' AND post_name = %s LIMIT 1;", $slug ) );
96
	}
97
98
	if ( $trashed_page_found ) {
99
		$page_id   = $trashed_page_found;
100
		$page_data = array(
101
			'ID'             => $page_id,
102
			'post_status'    => 'publish',
103
		);
104
	 	wp_update_post( $page_data );
105
	} else {
106
		$page_data = array(
107
			'post_status'    => 'publish',
108
			'post_type'      => 'page',
109
			'post_author'    => 1,
110
			'post_name'      => $slug,
111
			'post_title'     => $page_title,
112
			'post_content'   => $page_content,
113
			'post_parent'    => $post_parent,
114
			'comment_status' => 'closed'
115
		);
116
		$page_id = wp_insert_post( $page_data );
117
	}
118
119
	if ( $option ) {
120
		update_option( $option, $page_id );
121
	}
122
123
	return $page_id;
124
}
125
126
/**
127
 * Output admin fields.
128
 *
129
 * Loops though the woocommerce options array and outputs each field.
130
 *
131
 * @param array $options Opens array to output
132
 */
133
function woocommerce_admin_fields( $options ) {
134
135
	if ( ! class_exists( 'WC_Admin_Settings' ) ) {
136
		include( dirname( __FILE__ ) . '/class-wc-admin-settings.php' );
137
	}
138
139
	WC_Admin_Settings::output_fields( $options );
140
}
141
142
/**
143
 * Update all settings which are passed.
144
 *
145
 * @param array $options
146
 * @param array $data
147
 */
148
function woocommerce_update_options( $options, $data = null ) {
149
150
	if ( ! class_exists( 'WC_Admin_Settings' ) ) {
151
		include( dirname( __FILE__ ) . '/class-wc-admin-settings.php' );
152
	}
153
154
	WC_Admin_Settings::save_fields( $options, $data );
155
}
156
157
/**
158
 * Get a setting from the settings API.
159
 *
160
 * @param mixed $option_name
161
 * @param mixed $default
162
 * @return string
163
 */
164
function woocommerce_settings_get_option( $option_name, $default = '' ) {
165
166
	if ( ! class_exists( 'WC_Admin_Settings' ) ) {
167
		include( dirname( __FILE__ ) . '/class-wc-admin-settings.php' );
168
	}
169
170
	return WC_Admin_Settings::get_option( $option_name, $default );
171
}
172
173
/**
174
 * Save order items. Uses the CRUD.
175
 *
176
 * @since 2.2
177
 * @param int $order_id Order ID
178
 * @param array $items Order items to save
179
 */
180
function wc_save_order_items( $order_id, $items ) {
181
	// Allow other plugins to check change in order items before they are saved
182
	do_action( 'woocommerce_before_save_order_items', $order_id, $items );
183
184
	$order = wc_get_order( $order_id );
185
186
	// Line items and fees
187
	if ( isset( $items['order_item_id'] ) ) {
188
		foreach ( $items['order_item_id'] as $item_id ) {
189
			if ( ! $item = $order->get_item( absint( $item_id ) ) ) {
190
				continue;
191
			}
192
193
			if ( isset( $items['order_item_name'][ $item_id ] ) ) {
194
				$item->set_name( wc_clean( wp_unslash( $items['order_item_name'][ $item_id ] ) ) );
195
			}
196
197
			if ( isset( $items['order_item_qty'][ $item_id ] ) && is_callable( array( $item, 'set_quantity' ) ) ) {
198
				$item->set_quantity( $items['order_item_qty'][ $item_id ] );
199
			}
200
201
			if ( isset( $items['order_item_tax_class'][ $item_id ] ) ) {
202
				$item->set_tax_class( wc_clean( $items['order_item_tax_class'][ $item_id ] ) );
203
			}
204
205
			if ( isset( $items['meta_key'][ $item_id ], $items['meta_value'][ $item_id ] ) ) {
206
				foreach ( $items['meta_key'][ $item_id ] as $meta_id => $meta_key ) {
207
					$meta_value = isset( $items['meta_value'][ $item_id ][ $meta_id ] ) ? $items['meta_value'][ $item_id ][ $meta_id ] : '';
208
209
					if ( strstr( $meta_id, 'new-' ) ) {
210
						if ( $meta_key === '' && $meta_value === '' ) {
211
							continue;
212
						}
213
						$item->add_meta_data( $meta_key, $meta_value, false );
214
					} elseif ( $meta_key === '' && $meta_value === '' ) {
215
						$item->delete_meta_data_by_mid( $meta_id );
216
					} else {
217
						$item->update_meta_data( $meta_key, $meta_value, $meta_id );
218
					}
219
				}
220
			}
221
222
			$line_tax          = isset( $items['line_tax'][ $item_id ] ) ? $items['line_tax'][ $item_id ]: array();
223
			$line_subtotal_tax = isset( $items['line_subtotal_tax'][ $item_id ] ) ? $items['line_subtotal_tax'][ $item_id ]: $line_tax;
224
			$item->set_total( isset( $items['line_total'][ $item_id ] ) ? $items['line_total'][ $item_id ] : 0 );
225
			$item->set_total_tax( array_sum( $line_tax ) );
226
227
			if ( is_callable( array( $item, 'set_subtotal' ) ) ) {
228
				$item->set_subtotal( isset( $items['line_subtotal'][ $item_id ] ) ? $items['line_subtotal'][ $item_id ] : $item->get_total() );
229
				$item->set_subtotal_tax( array_sum( $line_subtotal_tax ) );
230
			}
231
232
			$item->set_taxes( array( 'total' => $line_tax, 'subtotal' => $line_subtotal_tax ) );
233
			$item->save();
234
		}
235
	}
236
237
	// Shipping Rows
238
	if ( isset( $items['shipping_method_id'] ) ) {
239
		foreach ( $items['shipping_method_id'] as $item_id ) {
240
			if ( ! $item = $order->get_item( absint( $item_id ) ) ) {
241
				continue;
242
			}
243
			$item->set_method_id( isset( $items['shipping_method'][ $item_id ] ) ? wc_clean( $items['shipping_method'][ $item_id ] ) : '' );
244
			$item->set_method_title( isset( $items['shipping_method_title'][ $item_id ] ) ? wc_clean( wp_unslash( $items['shipping_method_title'][ $item_id ] ) ) : '' );
245
			$item->set_total( isset( $items['shipping_cost'][ $item_id ] ) ? $items['shipping_cost'][ $item_id ] : '' );
246
			$item->set_taxes( array( 'total' => isset( $items['shipping_taxes'][ $item_id ] ) ? $items['shipping_taxes'][ $item_id ] : array() ) );
247
			$item->save();
248
		}
249
	}
250
251
	// Updates tax totals
252
	$order->update_taxes();
253
254
	// Calc totals - this also triggers save
255
	$order->calculate_totals( false );
256
257
	// Inform other plugins that the items have been saved
258
	do_action( 'woocommerce_saved_order_items', $order_id, $items );
259
}
260