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  * @class       order_item_meta
  8  * @version     2.0.4
  9  * @package     WooCommerce/Classes
 10  * @author      WooThemes
 11  */
 12 class WC_Order_Item_Meta {
 13 
 14     public $meta;
 15     public $product;
 16 
 17     /**
 18      * Constructor
 19      *
 20      * @access public
 21      * @param string $item_meta (default: '')
 22      * @return void
 23      */
 24     public function __construct( $item_meta = array(), $product = null ) {
 25         $this->meta    = $item_meta;
 26         $this->product = $product;
 27     }
 28 
 29     /**
 30      * Display meta in a formatted list
 31      *
 32      * @access public
 33      * @param bool $flat (default: false)
 34      * @param bool $return (default: false)
 35      * @param string $hideprefix (default: _)
 36      * @return string
 37      */
 38     public function display( $flat = false, $return = false, $hideprefix = '_' ) {
 39 
 40         if ( ! empty( $this->meta ) ) {
 41 
 42             $meta_list = array();
 43 
 44             foreach ( $this->meta as $meta_key => $meta_values ) {
 45 
 46                 if ( empty( $meta_values ) || ( ! empty( $hideprefix ) && substr( $meta_key, 0, 1 ) == $hideprefix ) ) {
 47                     continue;
 48                 }
 49 
 50                 foreach( $meta_values as $meta_value ) {
 51 
 52                     // Skip serialised meta
 53                     if ( is_serialized( $meta_value ) ) {
 54                         continue;
 55                     }
 56 
 57                     $attribute_key = urldecode( str_replace( 'attribute_', '', $meta_key ) );
 58 
 59                     // If this is a term slug, get the term's nice name
 60                     if ( taxonomy_exists( $attribute_key ) ) {
 61                         $term = get_term_by( 'slug', $meta_value, $attribute_key );
 62 
 63                         if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) {
 64                             $meta_value = $term->name;
 65                         }
 66 
 67                     // If we have a product, and its not a term, try to find its non-sanitized name
 68                     } elseif ( $this->product ) {
 69                         $product_attributes = $this->product->get_attributes();
 70 
 71                         if ( isset( $product_attributes[ $attribute_key ] ) ) {
 72                             $meta_key = wc_attribute_label( $product_attributes[ $attribute_key ]['name'] );
 73                         }
 74                     }
 75 
 76                     if ( $flat ) {
 77                         $meta_list[] = wp_kses_post( wc_attribute_label( $attribute_key ) . ': ' . apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) );
 78                     } else {
 79                         $meta_list[] = '
 80                             <dt class="variation-' . sanitize_html_class( sanitize_text_field( $meta_key ) ) . '">' . wp_kses_post( wc_attribute_label( $attribute_key ) ) . ':</dt>
 81                             <dd class="variation-' . sanitize_html_class( sanitize_text_field( $meta_key ) ) . '">' . wp_kses_post( wpautop( apply_filters( 'woocommerce_order_item_display_meta_value', $meta_value ) ) ) . '</dd>
 82                         ';
 83                     }
 84                 }
 85             }
 86 
 87             if ( ! sizeof( $meta_list ) )
 88                 return '';
 89 
 90             $output = $flat ? '' : '<dl class="variation">';
 91 
 92             if ( $flat )
 93                 $output .= implode( ", \n", $meta_list );
 94             else
 95                 $output .= implode( '', $meta_list );
 96 
 97             if ( ! $flat )
 98                 $output .= '</dl>';
 99 
100             if ( $return )
101                 return $output;
102             else
103                 echo $output;
104         }
105 
106         return '';
107     }
108 }
WooCommerce API documentation generated by ApiGen 2.8.0