/includes/class-wc-order-item-meta.php

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