2013-03-12 10:55:01 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Order Item Meta
|
2013-03-12 10:55:01 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* A Simple class for managing order item meta so plugins add it in the correct format.
|
2013-03-12 10:55:01 +00:00
|
|
|
*
|
2018-03-21 03:43:54 +00:00
|
|
|
* @package WooCommerce/Classes
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 wc_display_item_meta function is used instead.
|
2018-03-21 03:43:54 +00:00
|
|
|
* @version 2.4
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Order item meta class.
|
2013-03-12 10:55:01 +00:00
|
|
|
*/
|
|
|
|
class WC_Order_Item_Meta {
|
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
/**
|
|
|
|
* For handling backwards compatibility.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2015-05-01 11:42:29 +00:00
|
|
|
private $legacy = false;
|
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
/**
|
|
|
|
* Order item
|
|
|
|
*
|
|
|
|
* @var array|null
|
|
|
|
*/
|
|
|
|
private $item = null;
|
2015-05-01 11:42:29 +00:00
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
/**
|
|
|
|
* Post meta data
|
|
|
|
*
|
|
|
|
* @var array|null
|
|
|
|
*/
|
|
|
|
public $meta = null;
|
2015-05-01 11:42:29 +00:00
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
/**
|
|
|
|
* Product object.
|
|
|
|
*
|
|
|
|
* @var WC_Product|null
|
|
|
|
*/
|
2015-05-01 11:42:29 +00:00
|
|
|
public $product = null;
|
2013-03-12 10:55:01 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Constructor.
|
2013-03-12 10:55:01 +00:00
|
|
|
*
|
2018-03-21 03:43:54 +00:00
|
|
|
* @param array $item defaults to array().
|
|
|
|
* @param \WC_Product $product defaults to null.
|
2013-03-12 10:55:01 +00:00
|
|
|
*/
|
2015-05-01 11:42:29 +00:00
|
|
|
public function __construct( $item = array(), $product = null ) {
|
2017-04-24 23:26:43 +00:00
|
|
|
wc_deprecated_function( 'WC_Order_Item_Meta::__construct', '3.1', 'WC_Order_Item_Product' );
|
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
// Backwards (pre 2.4) compatibility.
|
2015-05-01 11:42:29 +00:00
|
|
|
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'] );
|
2014-02-24 15:14:32 +00:00
|
|
|
$this->product = $product;
|
2013-03-12 10:55:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Display meta in a formatted list.
|
2013-03-12 10:55:01 +00:00
|
|
|
*
|
2018-03-21 03:43:54 +00:00
|
|
|
* @param bool $flat Flat (default: false).
|
|
|
|
* @param bool $return Return (default: false).
|
|
|
|
* @param string $hideprefix Hide prefix (default: _).
|
|
|
|
* @param string $delimiter Delimiter used to separate items when $flat is true.
|
2015-04-01 13:33:56 +00:00
|
|
|
* @return string|void
|
2013-03-12 10:55:01 +00:00
|
|
|
*/
|
2014-10-14 10:54:20 +00:00
|
|
|
public function display( $flat = false, $return = false, $hideprefix = '_', $delimiter = ", \n" ) {
|
2015-05-01 11:42:29 +00:00
|
|
|
$output = '';
|
2014-07-28 17:32:38 +00:00
|
|
|
$formatted_meta = $this->get_formatted( $hideprefix );
|
|
|
|
|
|
|
|
if ( ! empty( $formatted_meta ) ) {
|
2013-03-12 10:55:01 +00:00
|
|
|
$meta_list = array();
|
|
|
|
|
2015-05-01 11:42:29 +00:00
|
|
|
foreach ( $formatted_meta as $meta ) {
|
2014-07-28 17:32:38 +00:00
|
|
|
if ( $flat ) {
|
|
|
|
$meta_list[] = wp_kses_post( $meta['label'] . ': ' . $meta['value'] );
|
|
|
|
} else {
|
|
|
|
$meta_list[] = '
|
2015-05-01 11:42:29 +00:00
|
|
|
<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>
|
|
|
|
';
|
2014-04-23 15:23:59 +00:00
|
|
|
}
|
2014-07-28 17:32:38 +00:00
|
|
|
}
|
2013-03-12 10:55:01 +00:00
|
|
|
|
2014-07-28 17:32:38 +00:00
|
|
|
if ( ! empty( $meta_list ) ) {
|
|
|
|
if ( $flat ) {
|
2014-10-14 10:54:20 +00:00
|
|
|
$output .= implode( $delimiter, $meta_list );
|
2014-07-28 17:32:38 +00:00
|
|
|
} else {
|
2014-10-14 10:54:20 +00:00
|
|
|
$output .= '<dl class="variation">' . implode( '', $meta_list ) . '</dl>';
|
2014-07-28 17:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 15:23:59 +00:00
|
|
|
|
2015-06-15 22:16:44 +00:00
|
|
|
$output = apply_filters( 'woocommerce_order_items_meta_display', $output, $this );
|
|
|
|
|
2014-07-28 17:32:38 +00:00
|
|
|
if ( $return ) {
|
|
|
|
return $output;
|
|
|
|
} else {
|
2018-03-21 03:43:54 +00:00
|
|
|
echo $output; // WPCS: XSS ok.
|
2014-07-28 17:32:38 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-16 14:50:09 +00:00
|
|
|
|
2014-07-28 17:32:38 +00:00
|
|
|
/**
|
2015-05-01 11:42:29 +00:00
|
|
|
* Return an array of formatted item meta in format e.g.
|
2014-07-28 17:32:38 +00:00
|
|
|
*
|
2018-03-21 03:43:54 +00:00
|
|
|
* Returns: array(
|
2014-07-28 17:32:38 +00:00
|
|
|
* 'pa_size' => array(
|
|
|
|
* 'label' => 'Size',
|
|
|
|
* 'value' => 'Medium',
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
2015-05-01 11:42:29 +00:00
|
|
|
* @since 2.4
|
2018-03-21 03:43:54 +00:00
|
|
|
* @param string $hideprefix exclude meta when key is prefixed with this, defaults to '_'.
|
2014-07-28 17:32:38 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_formatted( $hideprefix = '_' ) {
|
2015-05-01 11:42:29 +00:00
|
|
|
if ( $this->legacy ) {
|
|
|
|
return $this->get_formatted_legacy( $hideprefix );
|
2014-07-28 17:32:38 +00:00
|
|
|
}
|
2014-02-28 15:42:12 +00:00
|
|
|
|
2014-07-28 17:32:38 +00:00
|
|
|
$formatted_meta = array();
|
2013-03-12 10:55:01 +00:00
|
|
|
|
2015-10-29 16:06:21 +00:00
|
|
|
if ( ! empty( $this->item['item_meta_array'] ) ) {
|
|
|
|
foreach ( $this->item['item_meta_array'] as $meta_id => $meta ) {
|
2018-03-21 03:43:54 +00:00
|
|
|
if ( '' === $meta->value || is_serialized( $meta->value ) || ( ! empty( $hideprefix ) && substr( $meta->key, 0, 1 ) === $hideprefix ) ) {
|
2015-10-29 16:06:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2014-07-28 17:32:38 +00:00
|
|
|
|
2015-10-29 16:06:21 +00:00
|
|
|
$attribute_key = urldecode( str_replace( 'attribute_', '', $meta->key ) );
|
2016-04-19 10:42:44 +00:00
|
|
|
$meta_value = $meta->value;
|
2015-05-01 11:42:29 +00:00
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
// If this is a term slug, get the term's nice name.
|
2015-10-29 16:06:21 +00:00
|
|
|
if ( taxonomy_exists( $attribute_key ) ) {
|
2016-04-19 10:42:44 +00:00
|
|
|
$term = get_term_by( 'slug', $meta_value, $attribute_key );
|
|
|
|
|
2015-10-29 16:06:21 +00:00
|
|
|
if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) {
|
2016-04-19 10:42:44 +00:00
|
|
|
$meta_value = $term->name;
|
2015-10-29 16:06:21 +00:00
|
|
|
}
|
2015-05-01 11:42:29 +00:00
|
|
|
}
|
|
|
|
|
2015-10-29 16:06:21 +00:00
|
|
|
$formatted_meta[ $meta_id ] = array(
|
|
|
|
'key' => $meta->key,
|
|
|
|
'label' => wc_attribute_label( $attribute_key, $this->product ),
|
2017-06-10 15:05:02 +00:00
|
|
|
'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value, $meta, $this->item ),
|
2015-10-29 16:06:21 +00:00
|
|
|
);
|
|
|
|
}
|
2015-05-01 11:42:29 +00:00
|
|
|
}
|
|
|
|
|
2015-06-15 22:16:44 +00:00
|
|
|
return apply_filters( 'woocommerce_order_items_meta_get_formatted', $formatted_meta, $this );
|
2015-05-01 11:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an array of formatted item meta in format e.g.
|
2015-11-03 13:31:20 +00:00
|
|
|
* Handles @deprecated args.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2018-03-21 03:43:54 +00:00
|
|
|
* @param string $hideprefix Hide prefix.
|
2017-05-15 11:50:52 +00:00
|
|
|
*
|
2015-05-01 11:42:29 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-07-15 02:15:31 +00:00
|
|
|
public function get_formatted_legacy( $hideprefix = '_' ) {
|
2015-08-11 11:09:54 +00:00
|
|
|
if ( ! is_ajax() ) {
|
2017-06-05 09:43:13 +00:00
|
|
|
wc_deprecated_argument( 'WC_Order_Item_Meta::get_formatted', '2.4', 'Item Meta Data is being called with legacy arguments' );
|
2015-08-11 11:09:54 +00:00
|
|
|
}
|
2015-05-01 11:42:29 +00:00
|
|
|
|
|
|
|
$formatted_meta = array();
|
|
|
|
|
|
|
|
foreach ( $this->meta as $meta_key => $meta_values ) {
|
2018-03-21 03:43:54 +00:00
|
|
|
if ( empty( $meta_values ) || ( ! empty( $hideprefix ) && substr( $meta_key, 0, 1 ) === $hideprefix ) ) {
|
2014-07-28 17:32:38 +00:00
|
|
|
continue;
|
2013-03-12 10:55:01 +00:00
|
|
|
}
|
2014-08-05 10:28:27 +00:00
|
|
|
foreach ( (array) $meta_values as $meta_value ) {
|
2018-03-21 03:43:54 +00:00
|
|
|
// Skip serialised meta.
|
2014-07-28 17:32:38 +00:00
|
|
|
if ( is_serialized( $meta_value ) ) {
|
|
|
|
continue;
|
|
|
|
}
|
2013-05-28 14:58:40 +00:00
|
|
|
|
2014-07-28 17:32:38 +00:00
|
|
|
$attribute_key = urldecode( str_replace( 'attribute_', '', $meta_key ) );
|
2013-03-12 10:55:01 +00:00
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
// If this is a term slug, get the term's nice name.
|
2014-07-28 17:32:38 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-21 03:43:54 +00:00
|
|
|
// Unique key required.
|
2014-10-06 11:17:07 +00:00
|
|
|
$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(
|
2015-05-01 11:42:29 +00:00
|
|
|
'key' => $meta_key,
|
|
|
|
'label' => wc_attribute_label( $attribute_key, $this->product ),
|
|
|
|
'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ),
|
2014-07-28 17:32:38 +00:00
|
|
|
);
|
|
|
|
}
|
2013-03-12 10:55:01 +00:00
|
|
|
}
|
2014-05-11 13:37:27 +00:00
|
|
|
|
2014-07-28 17:32:38 +00:00
|
|
|
return $formatted_meta;
|
2013-03-12 10:55:01 +00:00
|
|
|
}
|
2014-07-28 17:32:38 +00:00
|
|
|
}
|