Completed
Pull Request — master (#2165)
by Justin
05:08
created

purchaselogs.functions.php ➔ wpsc_purchlogs_get_weight()   C

Complexity

Conditions 8
Paths 10

Size

Total Lines 63
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 21
nc 10
nop 2
dl 0
loc 63
rs 6.8825
c 0
b 0
f 0

How to fix   Long Method   

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
global $wpsc_purchlog_statuses;
4
if ( ! isset( $wpsc_purchlog_statuses ) || ! count( $wpsc_purchlog_statuses ) ) {
5
	wpsc_core_load_purchase_log_statuses();
6
}
7
8
function wpsc_instantiate_purchaselogitem() {
9
	global $purchlogitem;
10
	if ( isset( $_REQUEST['purchaselog_id'] ) ) {
11
		$purchlogitem = new wpsc_purchaselogs_items( (int)$_REQUEST['purchaselog_id'] );
12
	}
13
}
14
add_action( 'wpsc_core_included', 'wpsc_instantiate_purchaselogitem' );
15
16
function wpsc_display_purchlog_howtheyfoundus() {
17
	global $purchlogitem;
18
	return esc_attr( $purchlogitem->extrainfo->find_us );
19
}
20
21
function wpsc_display_purchlog_display_howtheyfoundus() {
22
	global $purchlogitem;
23
	return ! empty( $purchlogitem->extrainfo->find_us );
24
}
25
26
function wpsc_check_uniquenames() {
27
	global $wpdb;
28
	$sql = 'SELECT COUNT(`id`) FROM `' . WPSC_TABLE_CHECKOUT_FORMS . '` WHERE unique_name != "" ';
29
	$check_unique_names = $wpdb->get_var( $sql );
30
31
	return $check_unique_names > 0 ? false : true;
32
}
33
34
/**
35
 * Does the purchaselog have tracking information
36
 * @return boolean
37
 */
38
function wpsc_purchlogs_has_tracking() {
39
	global $purchlogitem;
40
	if ( ! empty( $purchlogitem->extrainfo->track_id ) ) {
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return !empty($purchlogi...->extrainfo->track_id);.
Loading history...
41
		return true;
42
	} else {
43
		return false;
44
	}
45
}
46
47
/**
48
 *
49
 * @return string  current tracking id or or empty string if there isn't a tracking id
50
 */
51
function wpsc_purchlogitem_trackid() {
52
	global $purchlogitem;
53
	return esc_attr( empty( $purchlogitem->extrainfo->track_id ) ? '' : $purchlogitem->extrainfo->track_id );
54
}
55
56
/**
57
 * Purchase shipping status
58
 * @return string shipping status or empty string
59
 */
60
function wpsc_purchlogitem_trackstatus() {
61
	global $wpsc_shipping_modules, $purchlogitem;
62
63
	$callable = array( $purchlogitem->extrainfo->shipping_method, 'getStatus' );
64
	$shipping_status_is_callable = is_callable( $callable );
65
66
	if ( $shipping_status_is_callable && ! empty( $purchlogitem->extrainfo->track_id ) ) {
67
		$status = $wpsc_shipping_modules [$purchlogitem->extrainfo->shipping_method]->getStatus( $purchlogitem->extrainfo->track_id );
68
	} else {
69
		$status = '';
70
	}
71
72
	return $status;
73
}
74
75
/**
76
 * Tracking history for purchase
77
 * @return string tracking history or empty string
78
 */
79
function wpsc_purchlogitem_trackhistory() {
80
	global $purchlogitem;
81
82
	if ( ( 'nzpost' == $purchlogitem->extrainfo->shipping_method ) && ! empty( $purchlogitem->extrainfo->track_id ) ) {
83
84
		$output  = '<ul>';
85
		$outputs = array();
86
87
		foreach ( ( array ) $_SESSION ['wpsc_nzpost_parsed'] [0] ['children'] [0] ['children'] [1] ['children'] as $history ) {
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(array)" but found "( array )"
Loading history...
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
88
			$outputs[] = '<li>' . $history ['children'] [0] ['tagData'] . ' : ' . $history ['children'] [1] ['tagData'] . ' </li>';
89
		}
90
91
		$outputs = array_reverse( $outputs );
92
		foreach ( $outputs as $o ) {
93
			$output .= $o;
94
		}
95
96
		$output .= '</ul>';
97
		return $output;
98
	} else {
99
		// TODO: If there isn't one already, we should add a tracking callback to the shipping API
100
		return '';
101
	}
102
}
103
104
105
/**
106
 * Weight of current or specified purchase
107
 *
108
 * @since 3.8.14
109
 *
110
 *
111
 * @param string $id
112
 * @return float $weight in '$out_unit' of shipment
113
 */
114
function wpsc_purchlogs_get_weight( $id = '', $out_unit = 'pound' ) {
115
	global $purchlogitem;
116
	$weight = 0.0;
117
	$items_count = 0;
118
119
	if ( empty( $id ) || ( ! empty( $purchlogitem ) &&  ( $id == $purchlogitem->purchlogid ) ) ) {
120
		$thepurchlogitem = $purchlogitem;
121
	} else {
122
		$thepurchlogitem = new wpsc_purchaselogs_items( $id );
123
	}
124
125
	/**
126
	 * Filter wpsc_purchlogs_before_get_weight
127
	 *
128
	 * Allow the weight to be overridden, can be used to persistantly save weight and recall it rather than recalculate
129
	 *
130
	 * @since 3.8.14
131
	 *
132
	 * @param  float  $weight, purchase calculation will not continue if value is returned
133
	 * @param  string weight unit to use for return value
134
	 * @param  object wpsc_purchaselogs_items purchase log item being used
135
	 * @param  int    purchase log item id
136
	 * @return float  $weight
137
	 */
138
	$weight_override = apply_filters( 'wpsc_purchlogs_before_get_weight', false, $out_unit, $thepurchlogitem, $thepurchlogitem->purchlogid );
139
	if ( $weight_override !== false ) {
140
		return $weight_override;
141
	}
142
143
	// if there isn't a purchase log item we are done
144
	if ( empty( $thepurchlogitem ) ) {
145
		return false;
146
	}
147
148
	foreach ( ( array ) $thepurchlogitem->allcartcontent as $cartitem ) {
0 ignored issues
show
introduced by
Cast statements must not contain whitespace; expected "(array)" but found "( array )"
Loading history...
149
		$product_meta = get_product_meta( $cartitem->prodid, 'product_metadata', true );
150
		if ( ! empty( $product_meta ['weight'] ) ) {
151
152
			$converted_weight = wpsc_convert_weight( $product_meta ['weight'], $product_meta['weight_unit'], $out_unit, true );
153
154
			$weight += $converted_weight * $cartitem->quantity;
155
			$items_count += $cartitem->quantity;
156
		}
157
	}
158
159
	/**
160
	 * Filter wpsc_purchlogs_get_weight
161
	 *
162
	 * Allow the weight to be overridden
163
	 *
164
	 * @since 3.8.14
165
	 *
166
	 * @param  float  $weight                 calculated cart weight
167
	 * @param  object wpsc_purchaselogs_items purchase log item being used
168
	 * @param  int    purchase log item id
169
	 * @param  int    $items_count            how many items are in the cart, useful for
170
	 *                                        cases where packaging weight changes as more items are
171
	 *                                        added
172
	 */
173
	$weight = apply_filters( 'wpsc_purchlogs_get_weight', $weight, $thepurchlogitem, $thepurchlogitem->purchlogid, $items_count );
174
175
	return $weight;
176
}
177
178
/**
179
 * Weight of current or specified purchase formatted as text with units
180
 *
181
 * @since 3.8.14
182
 *
183
 * @param string $id
184
 * @return string $weight in KG and lbs and ounces
185
 */
186
function wpsc_purchlogs_get_weight_text( $id = '' ) {
187
	global $purchlogitem;
188
189
	if ( empty( $id ) ) {
190
		$id = $purchlogitem->purchlogid;
191
	}
192
193
	$weight_in_pounds = wpsc_purchlogs_get_weight( $id, 'pound' );
194
195
	if ( $weight_in_pounds > 0 ) {
196
197
		$pound = floor( $weight_in_pounds );
198
		$ounce = round( ( $weight_in_pounds - $pound ) * 16 );
199
200
		$weight_in_kg = wpsc_purchlogs_get_weight( $id, 'kg' );
201
202
		$weight_string = number_format( $weight_in_kg , 2 ) .' ' .  __( 'KG' , 'wp-e-commerce' ) . ' / ' .  $pound . ' ' .  __( 'LB', 'wp-e-commerce' ) . ' ' . $ounce . ' ' . __( 'OZ', 'wp-e-commerce' );
203
204
	} else {
205
		$weight_string = '';
206
	}
207
208
	/**
209
	 * Filter wpsc_purchlogs_get_weight_text
210
	 *
211
	 * Format weight as text suitable to inform user of purchase shipping weight
212
	 *
213
	 * @since 3.8.14
214
	 *
215
	 * @param  string weight of purchase as text string with both KG and pounds/ounces
216
	 * @param  object wpsc_purchaselogs_items purchase log item being used
217
	 */
218
	return apply_filters( 'wpsc_purchlogs_get_weight_text', $weight_string, $id  );
219
220
}
221
222
function wpsc_purchlogs_has_customfields( $id = '' ) {
223
	global $purchlogitem;
224
	if ( $id == '' ) {
225
		foreach ( (array)$purchlogitem->allcartcontent as $cartitem ) {
226
			if ( $cartitem->files != 'N;' || $cartitem->custom_message != '' ) {
227
				return true;
228
			}
229
		}
230
		return false;
231
	} else {
232
		$purchlogitem = new wpsc_purchaselogs_items( $id );
233
		foreach ( (array)$purchlogitem->allcartcontent as $cartitem ) {
234
			if ( $cartitem->files != 'N;' || $cartitem->custom_message != '' ) {
235
				return true;
236
			}
237
		}
238
		return false;
239
	}
240
	return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
241
}
242
243
function wpsc_trackingid_value() {
244
	global $purchlogs;
245
	return $purchlogs->purchitem->track_id;
246
}
247
248
function wpsc_purchlogs_custommessages() {
249
	global $purchlogitem;
250
	$messages = array();
251
	foreach ( $purchlogitem->allcartcontent as $cartitem ) {
252
		if ( $cartitem->custom_message != '' ) {
253
			$messages[] = array(
254
				'title'   => apply_filters( 'the_title', $cartitem->name ),
255
				'message' => $cartitem->custom_message,
256
			);
257
		}
258
	}
259
	return $messages;
260
}
261
262
function wpsc_purchlogs_customfiles() {
263
	global $purchlogitem;
264
	$files = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
265
	foreach ( $purchlogitem->allcartcontent as $cartitem ) {
266
		if ( $cartitem->files != 'N;' ) {
267
			$file = unserialize( $cartitem->files );
268
269
			if ( $file["mime_type"] == "image/jpeg" || $file["mime_type"] == "image/png" || $file["mime_type"] == "image/gif" ) {
270
				$image = "<a href='" . esc_url ( WPSC_USER_UPLOADS_URL . $file['file_name'] ) . "' >";
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
271
				$image .= "<img width='150' src='".esc_url( WPSC_USER_UPLOADS_URL . $file['file_name'] ). "' alt='' />";
272
				$image .="</a>";
273
				$files[] = $cartitem->name . ' :<br />' . $image;
274
			} else {
275
				$files[] = $cartitem->name . ' :<br />' . esc_url( $file['file_name'] );
276
			}
277
		}
278
	}
279
	return $files;
280
}
281
282
function wpsc_have_purch_items() {
283
	global $purchlogs;
284
	return $purchlogs->have_purch_items();
285
}
286
287
function wpsc_is_checked_status() {
288
	global $purchlogs;
289
290
	return $purchlogs->is_checked_status();
291
}
292
293
function wpsc_have_purchaselog_details() {
294
	global $purchlogitem;
295
	return $purchlogitem->have_purch_item();
296
}
297
298
function wpsc_purchaselog_details_name() {
299
	global $purchlogitem;
300
	return esc_html( apply_filters( 'the_title', $purchlogitem->purchitem->name, $purchlogitem->purchitem->prodid ) );
301
}
302
303
function wpsc_purchaselog_details_id() {
304
	global $purchlogitem;
305
	return $purchlogitem->purchitem->id;
306
}
307
308
function wpsc_purchaselog_product_id() {
309
	global $purchlogitem;
310
	return $purchlogitem->purchitem->prodid;
311
}
312
313
function wpsc_the_purchaselog_item() {
314
	global $purchlogitem;
315
	return $purchlogitem->the_purch_item();
316
}
317
318
function wpsc_purchaselog_details_SKU() {
319
	global $purchlogitem;
320
	$meta_value = wpsc_get_cart_item_meta( $purchlogitem->purchitem->id, 'sku', true );
321
	if ( $meta_value != null ) {
322
		return esc_attr( $meta_value );
323
	} else {
324
		$meta_value = get_product_meta( $purchlogitem->purchitem->prodid, 'sku', true );
325
		if ( $meta_value != null ) {
326
			return esc_attr( $meta_value );
327
		} else {
328
			return __( 'N/A', 'wp-e-commerce' );
329
		}
330
	}
331
}
332
333
function wpsc_purchaselog_details_quantity() {
334
	global $purchlogitem;
335
	return (float) $purchlogitem->purchitem->quantity;
336
}
337
338
function wpsc_purchaselog_details_price() {
339
	global $purchlogitem;
340
	return (float) $purchlogitem->purchitem->price;
341
}
342
343
function wpsc_purchaselog_details_shipping() {
344
	global $purchlogitem;
345
	return (float) $purchlogitem->purchitem->pnp;
346
}
347
348
function wpsc_purchaselog_details_tax() {
349
	global $purchlogitem, $wpsc_cart;
350
351
	return (float) $purchlogitem->purchitem->tax_charged;
352
}
353
354
function wpsc_purchaselog_details_discount() {
355
	global $purchlogitem;
356
	return (float) $purchlogitem->extrainfo->discount_value;
357
}
358
359
function wpsc_purchaselog_details_date() {
360
	global $purchlogitem;
361
	return date_i18n( apply_filters( 'wpsc_single_purchase_log_date_format', get_option( 'date_format' ) ), $purchlogitem->extrainfo->date + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
362
}
363
364
function wpsc_purchaselog_details_date_time() {
365
	global $purchlogitem;
366
	return date_i18n( apply_filters( 'wpsc_single_purchase_log_date_time_format', get_option( 'date_format' ) . ' g:ia' ),   $purchlogitem->extrainfo->date + ( get_option( 'gmt_offset' ) * HOUR_IN_SECONDS ) );
367
}
368
369
function wpsc_purchaselog_details_total() {
370
	global $purchlogitem;
371
	$total = 0;
372
	$total += ( $purchlogitem->purchitem->price * $purchlogitem->purchitem->quantity);
373
	$total += ( $purchlogitem->purchitem->pnp );
374
	$purchlogitem->totalAmount += $total;
375
	return $total;
376
}
377
378
function wpsc_purchaselog_details_purchnumber() {
379
	global $purchlogitem;
380
	return $purchlogitem->extrainfo->id;
381
}
382
383
/**
384
 * Has Discount Data?
385
 */
386
function wpsc_purchlog_has_discount_data() {
387
	global $purchlogitem;
388
	return ! empty( $purchlogitem->extrainfo->discount_data );
389
}
390
391
/**
392
 * Returns Discount Code
393
 */
394
function wpsc_display_purchlog_discount_data( $numeric = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $numeric 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...
395
	global $purchlogitem;
396
	return $purchlogitem->extrainfo->discount_data;
397
}
398
399
/**
400
 * Returns base shipping should make a function to calculate items shipping as well
401
 */
402
function wpsc_display_purchlog_discount( $numeric = false ) {
403
	global $purchlogitem;
404
	return $purchlogitem->log()->discount( $numeric );
405
}
406
407
/**
408
 * Returns base shipping should make a function to calculate items shipping as well
409
 */
410
function wpsc_display_purchlog_shipping( $numeric = false, $include_item = false ) {
411
	global $purchlogitem;
412
	return $purchlogitem->log()->shipping( $numeric, $include_item );
413
}
414
415
/**
416
 * @description: returns taxes as set in purchase log
417
 * @param: numeric - if set will return unformatted price
418
 */
419
function wpec_display_purchlog_taxes( $numeric = false ) {
420
	return wpsc_display_purchlog_taxes( $numeric );
421
}
422
423
/**
424
 * @description: determines whether or not to display the product tax or not
425
 * @return: boolean
426
 */
427
function wpec_display_product_tax() {
428
	global $purchlogitem;
429
	return ($purchlogitem->extrainfo->wpec_taxes_total == 0.00) ? true : false;
430
}
431
432
function wpsc_display_purchlog_taxes( $numeric = false ) {
433
	global $purchlogitem;
434
	return $purchlogitem->log()->taxes( $numeric );
435
}
436
437
function wpsc_display_purchlog_totalprice() {
438
	global $purchlogitem;
439
	return $purchlogitem->log()->total_price();
440
}
441
442
function wpsc_display_purchlog_buyers_name() {
443
	global $purchlogitem;
444
	return esc_html( $purchlogitem->log()->buyers_name() );
445
}
446
447
function wpsc_display_purchlog_buyers_city() {
448
	global $purchlogitem;
449
	return esc_html( $purchlogitem->log()->buyers_city() );
450
}
451
452
function wpsc_display_purchlog_buyers_email() {
453
	global $purchlogitem;
454
	return esc_html( $purchlogitem->log()->buyers_email() );
455
}
456
457
function wpsc_display_purchlog_buyers_address() {
458
	global $purchlogitem;
459
	return esc_html( $purchlogitem->log()->buyers_address() );
460
}
461
462
function wpsc_display_purchlog_buyers_state_and_postcode() {
463
	global $purchlogitem;
464
	return esc_html( $purchlogitem->log()->buyers_state_and_postcode() );
465
}
466
467
function wpsc_display_purchlog_buyers_country() {
468
	global $purchlogitem;
469
	return esc_html( $purchlogitem->log()->buyers_country() );
470
}
471
472
function wpsc_display_purchlog_buyers_phone() {
473
	global $purchlogitem;
474
	return esc_html( $purchlogitem->log()->buyers_phone() );
475
}
476
477
function wpsc_display_purchlog_shipping_name() {
478
	global $purchlogitem;
479
	return esc_html( $purchlogitem->log()->shipping_name() );
480
}
481
482
function wpsc_display_purchlog_shipping_address() {
483
	global $purchlogitem;
484
	return esc_html( $purchlogitem->log()->shipping_address() );
485
}
486
487
function wpsc_display_purchlog_shipping_city() {
488
	global $purchlogitem;
489
	return esc_html( $purchlogitem->log()->shipping_city() );
490
}
491
492
function wpsc_display_purchlog_shipping_state_and_postcode() {
493
	global $purchlogitem;
494
	return esc_html( $purchlogitem->log()->shipping_state_and_postcode() );
495
}
496
497
function wpsc_display_purchlog_shipping_country() {
498
	global $purchlogitem;
499
	return esc_html( $purchlogitem->log()->shipping_country() );
500
}
501
502
function wpsc_display_purchlog_shipping_method() {
503
	global $purchlogitem, $wpsc_shipping_modules;
504
	return esc_html( $purchlogitem->log()->shipping_method() );
505
}
506
507
function wpsc_display_purchlog_shipping_option() {
508
	global $purchlogitem;
509
	return esc_html( $purchlogitem->extrainfo->shipping_option );
510
}
511
512
function wpsc_display_purchlog_paymentmethod() {
513
	global $purchlogitem;
514
	return esc_html( $purchlogitem->log()->payment_method() );
515
}
516
517
function wpsc_purchaselog_order_summary_headers() {
518
	global $purchlogitem;
519
	do_action( 'wpsc_purchaselog_order_summary_headers', $purchlogitem );
520
}
521
522
function wpsc_purchaselog_order_summary() {
523
	global $purchlogitem;
524
	do_action( 'wpsc_purchaselog_order_summary', $purchlogitem );
525
}
526
527
function wpsc_has_purchlog_shipping() {
528
	global $purchlogitem;
529
	return (bool) trim( $purchlogitem->log()->shipping_name() );
530
}
531
532
function wpsc_purchlogs_have_downloads_locked() {
533
	global $purchlogitem;
534
	$ip = $purchlogitem->have_downloads_locked();
535
	if ( $ip == '' ) {
536
		return false;
537
	}
538
539
	return sprintf( __( 'Release downloads locked to this IP address %s', 'wp-e-commerce' ), $ip );
540
}
541
542
/**
543
 * Display Purchase Log Notes
544
 *
545
 * @return  string  Notes.
546
 */
547
function wpsc_display_purchlog_notes() {
548
	global $purchlogitem;
549
	return $purchlogitem->log()->get( 'notes' );
550
}
551
552
// edit purchase log status function
553
function wpsc_purchlog_edit_status( $purchlog_id = '', $purchlog_status = '' ) {
554
	global $wpdb;
555
	if ( empty( $purchlog_id ) && empty( $purchlog_status ) ) {
556
		$purchlog_id = absint( $_POST['id'] );
557
		$purchlog_status = absint( $_POST['new_status'] );
558
	}
559
560
	$purchase_log = new WPSC_Purchase_Log( $purchlog_id );
561
562
   // In the future when everyone is using the 2.0 merchant api,
563
   // we should use the merchant class to update the staus,
564
   // then you can get rid of this hook and have each person overwrite
565
   // the method that updates the status.
566
	do_action( 'wpsc_edit_order_status', array(
567
		'purchlog_id'   => $purchlog_id,
568
		'purchlog_data' => $purchase_log->get_data(),
569
		'new_status'    => $purchlog_status
570
	) );
571
572
	$result = wpsc_update_purchase_log_status( $purchlog_id, $purchlog_status );
573
	wpsc_clear_stock_claims();
574
575
	return $result;
576
}
577