Passed
Push — master ( ece56d...190d73 )
by Brian
09:21 queued 04:12
created

output()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 31
rs 9.52
cc 4
nc 4
nop 1
1
<?php
2
/**
3
 * Invoice Shipping Address
4
 *
5
 * Display the invoice shipping address meta box.
6
 *
7
 */
8
9
if ( ! defined( 'ABSPATH' ) ) {
10
	exit; // Exit if accessed directly
11
}
12
13
/**
14
 * GetPaid_Meta_Box_Invoice_Shipping_Address Class.
15
 */
16
class GetPaid_Meta_Box_Invoice_Shipping_Address {
17
18
	/**
19
	 * Output the metabox.
20
	 *
21
	 * @param WP_Post $post
22
	 */
23
	public static function output( $post ) {
24
25
		// Retrieve shipping address.
26
		$shipping_address = get_post_meta( $post->ID, 'shipping_address', true );
27
28
		// Abort if it is invalid.
29
		if ( ! is_array( $shipping_address ) ) {
30
			return;
31
		}
32
33
		?>
34
35
		<div class="bsui">
36
37
			<?php foreach ( getpaid_user_address_fields() as $key => $label ) : ?>
38
39
					<?php if ( ! empty( $shipping_address[ $key ] ) ) : ?>
40
41
						<div class="form-group form-row">
42
							<div class="col">
43
								<span style="font-weight: 600"><?php echo sanitize_text_field( $label ); ?>:</span>
44
							</div>
45
							<div class="col">
46
								<?php echo self::prepare_for_display( $shipping_address, $key ); ?>
47
							</div>
48
						</div>
49
50
					<?php endif; ?>
51
52
			<?php endforeach; ?>
53
54
		</div>
55
56
		<?php
57
58
	}
59
60
	/**
61
	 * Prepares a value.
62
	 *
63
	 * @param array $address
64
	 * @param string $key
65
	 * @return string
66
	 */
67
	public static function prepare_for_display( $address, $key ) {
68
69
		// Prepare the value.
70
		$value = $address[ $key ];
71
72
		if ( $key == 'country' ) {
73
			$value = wpinv_country_name( $value );
74
		}
75
76
		if ( $key == 'state' ) {
77
			$country = isset( $address[ 'country' ] ) ? $address[ 'country' ] : wpinv_get_default_country();
78
			$value = wpinv_state_name( $value, $country );
79
		}
80
81
		return sanitize_text_field( $value );
82
83
	}
84
85
}
86