2013-03-12 10:55:01 +00:00
|
|
|
<?php
|
2015-11-06 09:22:19 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2016-06-21 19:10:09 +00:00
|
|
|
exit;
|
2015-11-06 09:22:19 +00:00
|
|
|
}
|
|
|
|
|
2013-03-12 10:55:01 +00:00
|
|
|
/**
|
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
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @deprecated 3.0.0 wc_display_item_meta function is used instead.
|
2013-03-12 10:55:01 +00:00
|
|
|
* @class order_item_meta
|
2015-05-01 11:42:29 +00:00
|
|
|
* @version 2.4
|
2013-03-12 10:55:01 +00:00
|
|
|
* @package WooCommerce/Classes
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WC_Order_Item_Meta {
|
|
|
|
|
2015-08-10 13:42:10 +00:00
|
|
|
/** @var bool For handling backwards comp */
|
2015-05-01 11:42:29 +00:00
|
|
|
private $legacy = false;
|
|
|
|
|
|
|
|
/** @var Array Order item */
|
|
|
|
private $item = null;
|
|
|
|
|
2015-08-10 13:42:10 +00:00
|
|
|
/** @var Array Post meta data */
|
2015-05-01 11:42:29 +00:00
|
|
|
public $meta = null;
|
|
|
|
|
|
|
|
/** @var Product object */
|
|
|
|
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
|
|
|
*
|
2015-07-16 19:29:01 +00:00
|
|
|
* @param array $item defaults to array()
|
2014-07-28 17:32:38 +00:00
|
|
|
* @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 ) {
|
|
|
|
// Backwards (pre 2.4) compat
|
|
|
|
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
|
|
|
*
|
|
|
|
* @param bool $flat (default: false)
|
|
|
|
* @param bool $return (default: false)
|
|
|
|
* @param string $hideprefix (default: _)
|
2014-10-14 10:54:20 +00:00
|
|
|
* @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 {
|
|
|
|
echo $output;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*
|
|
|
|
* array(
|
|
|
|
* 'pa_size' => array(
|
|
|
|
* 'label' => 'Size',
|
|
|
|
* 'value' => 'Medium',
|
|
|
|
* )
|
|
|
|
* )
|
|
|
|
*
|
2015-05-01 11:42:29 +00:00
|
|
|
* @since 2.4
|
2014-07-28 17:32:38 +00:00
|
|
|
* @param string $hideprefix exclude meta when key is prefixed with this, defaults to `_`
|
|
|
|
* @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 ) {
|
2016-04-19 10:42:44 +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
|
|
|
|
2015-10-29 16:06:21 +00:00
|
|
|
// If this is a term slug, get the term's nice name
|
|
|
|
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 ),
|
2016-04-19 10:42:44 +00:00
|
|
|
'value' => apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ),
|
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.
|
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() ) {
|
2016-11-23 16:15:00 +00:00
|
|
|
wc_deprecated_function( 'get_formatted_legacy', '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 ) {
|
2014-08-05 10:27:18 +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 ) {
|
2014-07-28 17:32:38 +00:00
|
|
|
// Skip serialised meta
|
|
|
|
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
|
|
|
|
2014-07-28 17:32:38 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-06 11:17:07 +00:00
|
|
|
// 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(
|
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
|
|
|
}
|