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