class-wc-order-item-meta.php 5.8 KB

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