| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | if ( ! defined( 'ABSPATH' ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 	exit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * Line Item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * A class which represents an item within an order or cart. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Uses ArrayAccess to be BW compatible with previously public properties. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @version     2.7.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @since       2.7.0 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package     WooCommerce/Classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @author      WooThemes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | class WC_Item extends WC_Data implements ArrayAccess { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	 * Constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	 * @param int|object|array $read ID to load from the DB (optional) or already queried data. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 23 |  | View Code Duplication | 	public function __construct( $read = 0 ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 		parent::__construct( $read ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 		if ( $read instanceof WC_Item ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 			if ( $this->is_type( $read->get_type() ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 				$this->set_props( $read->get_data() ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 		} elseif ( is_array( $read ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 			$this->set_props( $read ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 			$this->read( $read ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * Type checking | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 * @param  string|array  $Type | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	 * @return boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	public function is_type( $type ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		return is_array( $type ) ? in_array( $this->get_type(), $type ) : $type === $this->get_type(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * Get item type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	public function get_type() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 		return ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	|-------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 	| Array Access Methods | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 	|-------------------------------------------------------------------------- | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	| For backwards compat with legacy arrays. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	| | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	*/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * offsetSet for ArrayAccess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 * @param string $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 	 * @param mixed $value | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 67 |  |  | 	 */ | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 68 |  | View Code Duplication | 	public function offsetSet( $offset, $value ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  | 		if ( 'item_meta_array' === $offset ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  | 			foreach ( $value as $meta_id => $meta ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  | 				$this->update_meta_data( $meta->key, $meta->value, $meta_id ); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  | 			} | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  | 			return; | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  | 		if ( array_key_exists( $offset, $this->data ) ) { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  | 			$this->data[ $offset ] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  | 		$this->update_meta_data( '_' . $offset, $value ); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 	 * offsetUnset for ArrayAccess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	 * @param string $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 87 |  | View Code Duplication | 	public function offsetUnset( $offset ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 		if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 			$this->meta_data = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 			return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		if ( array_key_exists( $offset, $this->data ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			unset( $this->data[ $offset ] ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		$this->delete_meta_data( '_' . $offset ); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 	 * offsetExists for ArrayAccess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	 * @param string $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	public function offsetExists( $offset ) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 106 |  | View Code Duplication | 		if ( 'item_meta_array' === $offset || 'item_meta' === $offset || array_key_exists( $offset, $this->data ) ) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		return array_key_exists( '_' . $offset, wp_list_pluck( $this->meta_data, 'value', 'key' ) ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 	 * Get legacy item_meta_array. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 	 * @return arrau | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 	private function get_item_meta_array() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		$return = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 		foreach ( $this->meta_data as $meta ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 			$return[ $meta->id ] = $meta; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 		return $return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * offsetGet for ArrayAccess. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * Item meta was expanded in previous versions, with prefixes removed. This maintains support. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @param string $offset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	public function offsetGet( $offset ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$meta_values = wp_list_pluck( $this->meta_data, 'value', 'key' ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		if ( 'item_meta_array' === $offset ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			return $this->get_item_meta_array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		} elseif ( 'item_meta' === $offset ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 			return $meta_values; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		} elseif ( array_key_exists( $offset, $this->data ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			return $this->data[ $offset ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		} elseif ( array_key_exists( '_' . $offset, $meta_values ) ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 			return $meta_values[ '_' . $offset ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 145 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 146 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.