1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Order Item Meta |
9
|
|
|
* |
10
|
|
|
* A Simple class for managing order item meta so plugins add it in the correct format. |
11
|
|
|
* |
12
|
|
|
* @class order_item_meta |
13
|
|
|
* @version 2.4 |
14
|
|
|
* @package WooCommerce/Classes |
15
|
|
|
* @author WooThemes |
16
|
|
|
*/ |
17
|
|
|
class WC_Order_Item_Meta { |
18
|
|
|
|
19
|
|
|
/** @var bool For handling backwards comp */ |
20
|
|
|
private $legacy = false; |
21
|
|
|
|
22
|
|
|
/** @var Array Order item */ |
23
|
|
|
private $item = null; |
24
|
|
|
|
25
|
|
|
/** @var Array Post meta data */ |
26
|
|
|
public $meta = null; |
27
|
|
|
|
28
|
|
|
/** @var Product object */ |
29
|
|
|
public $product = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param array $item defaults to array() |
35
|
|
|
* @param \WC_Product $product defaults to null |
36
|
|
|
* @return \WC_Order_Item_Meta instance |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public function __construct( $item = array(), $product = null ) { |
39
|
|
|
// Backwards (pre 2.4) compat |
40
|
|
|
if ( ! isset( $item['item_meta'] ) ) { |
41
|
|
|
$this->legacy = true; |
42
|
|
|
$this->meta = array_filter( (array) $item ); |
43
|
|
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->item = $item; |
47
|
|
|
$this->meta = array_filter( (array) $item['item_meta'] ); |
48
|
|
|
$this->product = $product; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Display meta in a formatted list. |
53
|
|
|
* |
54
|
|
|
* @param bool $flat (default: false) |
55
|
|
|
* @param bool $return (default: false) |
56
|
|
|
* @param string $hideprefix (default: _) |
57
|
|
|
* @param string $delimiter Delimiter used to separate items when $flat is true |
58
|
|
|
* @return string|void |
59
|
|
|
*/ |
60
|
|
|
public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) { |
61
|
|
|
$output = ''; |
62
|
|
|
$formatted_meta = $this->get_formatted( $hideprefix ); |
63
|
|
|
|
64
|
|
|
if ( ! empty( $formatted_meta ) ) { |
65
|
|
|
$meta_list = array(); |
66
|
|
|
|
67
|
|
|
foreach ( $formatted_meta as $meta ) { |
68
|
|
|
if ( $flat ) { |
69
|
|
|
$meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] ); |
70
|
|
|
} else { |
71
|
|
|
$meta_list[] = ' |
72
|
|
|
<dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( $meta['label'] ) . ':</dt> |
73
|
|
|
<dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta['key'] ) ) . '">' . wp_kses_post( wpautop( make_clickable( $meta['value'] ) ) ) . '</dd> |
74
|
|
|
'; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if ( ! empty( $meta_list ) ) { |
79
|
|
|
if ( $flat ) { |
80
|
|
|
$output .= implode( $delimiter, $meta_list ); |
81
|
|
|
} else { |
82
|
|
|
$output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>'; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this ); |
88
|
|
|
|
89
|
|
|
if ( $return ) { |
90
|
|
|
return $output; |
91
|
|
|
} else { |
92
|
|
|
echo $output; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return an array of formatted item meta in format e.g. |
98
|
|
|
* |
99
|
|
|
* array( |
100
|
|
|
* 'pa_size' => array( |
101
|
|
|
* 'label' => 'Size', |
102
|
|
|
* 'value' => 'Medium', |
103
|
|
|
* ) |
104
|
|
|
* ) |
105
|
|
|
* |
106
|
|
|
* @since 2.4 |
107
|
|
|
* @param string $hideprefix exclude meta when key is prefixed with this, defaults to `_` |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function get_formatted( $hideprefix = '_' ) { |
111
|
|
|
if ( $this->legacy ) { |
112
|
|
|
return $this->get_formatted_legacy( $hideprefix ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$formatted_meta = array(); |
116
|
|
|
|
117
|
|
|
if ( ! empty( $this->item['item_meta_array'] ) ) { |
118
|
|
|
foreach ( $this->item['item_meta_array'] as $meta_id => $meta ) { |
119
|
|
|
if ( "" === $meta->value || is_serialized( $meta->value ) || ( ! empty( $hideprefix ) && substr( $meta->key, 0, 1 ) === $hideprefix ) ) { |
120
|
|
|
continue; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$attribute_key = urldecode( str_replace( 'attribute_', '', $meta->key ) ); |
124
|
|
|
$meta_value = $meta->value; |
125
|
|
|
|
126
|
|
|
// If this is a term slug, get the term's nice name |
127
|
|
View Code Duplication |
if ( taxonomy_exists( $attribute_key ) ) { |
|
|
|
|
128
|
|
|
$term = get_term_by( 'slug', $meta_value, $attribute_key ); |
129
|
|
|
|
130
|
|
|
if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) { |
131
|
|
|
$meta_value = $term->name; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
$formatted_meta[ $meta_id ] = array( |
136
|
|
|
'key' => $meta->key, |
137
|
|
|
'label' => wc_attribute_label( $attribute_key, $this->product ), |
138
|
|
|
'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ), |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
return apply_filters( 'woocommerce_order_items_meta_get_formatted', $formatted_meta, $this ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Return an array of formatted item meta in format e.g. |
148
|
|
|
* Handles @deprecated args. |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function get_formatted_legacy( $hideprefix = '_' ) { |
152
|
|
|
if ( ! is_ajax() ) { |
153
|
|
|
_deprecated_function( 'get_formatted_legacy', '2.4', 'Item Meta Data is being called with legacy arguments' ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$formatted_meta = array(); |
157
|
|
|
|
158
|
|
|
foreach ( $this->meta as $meta_key => $meta_values ) { |
159
|
|
|
if ( empty( $meta_values ) || ( ! empty( $hideprefix ) && substr( $meta_key, 0, 1 ) == $hideprefix ) ) { |
160
|
|
|
continue; |
161
|
|
|
} |
162
|
|
|
foreach ( (array) $meta_values as $meta_value ) { |
163
|
|
|
// Skip serialised meta |
164
|
|
|
if ( is_serialized( $meta_value ) ) { |
165
|
|
|
continue; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$attribute_key = urldecode( str_replace( 'attribute_', '', $meta_key ) ); |
169
|
|
|
|
170
|
|
|
// If this is a term slug, get the term's nice name |
171
|
|
View Code Duplication |
if ( taxonomy_exists( $attribute_key ) ) { |
|
|
|
|
172
|
|
|
$term = get_term_by( 'slug', $meta_value, $attribute_key ); |
173
|
|
|
if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) { |
174
|
|
|
$meta_value = $term->name; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
// Unique key required |
179
|
|
|
$formatted_meta_key = $meta_key; |
180
|
|
|
$loop = 0; |
181
|
|
|
while ( isset( $formatted_meta[ $formatted_meta_key ] ) ) { |
182
|
|
|
$loop ++; |
183
|
|
|
$formatted_meta_key = $meta_key . '-' . $loop; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$formatted_meta[ $formatted_meta_key ] = array( |
187
|
|
|
'key' => $meta_key, |
188
|
|
|
'label' => wc_attribute_label( $attribute_key, $this->product ), |
189
|
|
|
'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ), |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $formatted_meta; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.