Completed
Pull Request — master (#10259)
by Mike
08:16
created

WC_Order_Item::save_meta_data()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 24
loc 24
rs 8.6845
cc 4
eloc 15
nc 6
nop 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 17 and the first side effect is on line 3.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
if ( ! defined( 'ABSPATH' ) ) {
3
	exit;
4
}
5
6
/**
7
 * Order Item
8
 *
9
 * A class which represents an item within an order and handles CRUD.
10
 * Uses ArrayAccess to be BW compatible with WC_Orders::get_items().
11
 *
12
 * @version     2.6.0
13
 * @since       2.6.0
14
 * @package     WooCommerce/Classes
15
 * @author      WooThemes
16
 */
17
class WC_Order_Item implements ArrayAccess, WC_Data {
18
19
	/**
20
	 * Data array, with defaults.
21
	 * @since 2.6.0
22
	 * @var array
23
	 */
24
	protected $_data = array(
25
		'order_id'      => 0,
26
		'order_item_id' => 0,
27
		'name'          => '',
28
		'type'          => '',
29
	);
30
31
	/**
32
	 * Stores additonal meta data.
33
	 * @var array
34
	 */
35
	protected $_meta_data = array();
36
37
	/**
38
	 * Constructor.
39
	 * @param int|object|array $order_item ID to load from the DB (optional) or already queried data.
0 ignored issues
show
Bug introduced by
There is no parameter named $order_item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
40
	 */
41
	public function __construct( $item = 0 ) {
42
		if ( $item instanceof WC_Order_Item ) {
43
			if ( $this->is_type( $item->get_type() ) ) {
44
				$this->set_all( $item->get_data() );
45
			}
46
		} elseif ( is_array( $item ) ) {
47
			$this->set_all( $item );
48
		} else {
49
			$this->read( $item );
50
		}
51
	}
52
53
	/**
54
	 * Set all data based on input array.
55
	 * @param array $data
56
	 * @access private
57
	 */
58
	public function set_all( $data ) {
59
		foreach ( $data as $key => $value ) {
60
			if ( is_callable( array( $this, "set_$key" ) ) ) {
61
				$this->{"set_$key"}( $value );
62
			} elseif ( 'meta_data' !== $key ) {
63
				$this->_data[ $key ] = $value;
64
			} else {
65
				foreach ( $value as $meta_id => $meta ) {
66
					$this->_meta_data[ $meta_id ] = $meta;
67
				}
68
			}
69
		}
70
	}
71
72
	/**
73
	 * Change data to JSON format.
74
	 * @return string Data in JSON format.
75
	 */
76
	public function __toString() {
77
		return json_encode( $this->get_data() );
78
	}
79
80
	/**
81
	 * Type checking
82
	 * @param  string|array  $Type
0 ignored issues
show
Bug introduced by
There is no parameter named $Type. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
83
	 * @return boolean
84
	 */
85
	public function is_type( $type ) {
86
		return is_array( $type ) ? in_array( $this->get_type(), $type ) : $type === $this->get_type();
87
	}
88
89
	/**
90
	 * Get qty.
91
	 * @return int
92
	 */
93
	public function get_qty() {
94
		return 1;
95
	}
96
97
	/*
98
	|--------------------------------------------------------------------------
99
	| Getters
100
	|--------------------------------------------------------------------------
101
	*/
102
103
	/**
104
	 * Get all class data in array format.
105
	 * @since 2.6.0
106
	 * @return array
107
	 */
108
	public function get_data() {
109
		return array_merge( $this->_data, array( 'meta_data' => $this->get_meta_data() ) );
110
	}
111
112
	/**
113
	 * Get order item ID.
114
	 * @return int
115
	 */
116
	public function get_id() {
117
		return $this->get_order_item_id();
118
	}
119
120
	/**
121
	 * Get order ID this meta belongs to.
122
	 * @return int
123
	 */
124
	public function get_order_id() {
125
		return absint( $this->_data['order_id'] );
126
	}
127
128
	/**
129
	 * Get order item ID this meta belongs to.
130
	 * @return int
131
	 */
132
	public function get_order_item_id() {
133
		return absint( $this->_data['order_item_id'] );
134
	}
135
136
	/**
137
	 * Get order item name.
138
	 * @return string
139
	 */
140
	public function get_name() {
141
		return $this->_data['name'];
142
	}
143
144
	/**
145
	 * Get order item type.
146
	 * @return string
147
	 */
148
	public function get_type() {
149
		return $this->_data['type'];
150
	}
151
152
	/*
153
	|--------------------------------------------------------------------------
154
	| Setters
155
	|--------------------------------------------------------------------------
156
	*/
157
158
	/**
159
	 * Set ID
160
	 * @param int $value
161
	 */
162
	public function set_id( $value ) {
163
		$this->set_order_item_id( $value );
164
	}
165
166
	/**
167
	 * Set order ID.
168
	 * @param int $value
169
	 */
170
	public function set_order_id( $value ) {
171
		$this->_data['order_id'] = absint( $value );
172
	}
173
174
	/**
175
	 * Set order item ID.
176
	 * @param int $value
177
	 */
178
	public function set_order_item_id( $value ) {
179
		$this->_data['order_item_id'] = absint( $value );
180
	}
181
182
	/**
183
	 * Set order item name.
184
	 * @param string $value
185
	 */
186
	public function set_name( $value ) {
187
		$this->_data['name'] = wc_clean( $value );
188
	}
189
190
	/**
191
	 * Set order item type.
192
	 * @param string $value
193
	 */
194
	public function set_type( $value ) {
195
		$this->_data['type'] = wc_clean( $value );
196
197
	}
198
199
	/*
200
	|--------------------------------------------------------------------------
201
	| CRUD methods
202
	|--------------------------------------------------------------------------
203
	|
204
	| Methods which create, read, update and delete data from the database.
205
	|
206
	*/
207
208
	/**
209
	 * Insert data into the database.
210
	 * @since 2.6.0
211
	 */
212 View Code Duplication
	public function create() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
213
		global $wpdb;
214
215
		$wpdb->insert( $wpdb->prefix . 'woocommerce_order_items', array(
216
			'order_item_name' => $this->get_name(),
217
			'order_item_type' => $this->get_type(),
218
			'order_id'        => $this->get_order_id()
219
		) );
220
		$this->set_id( $wpdb->insert_id );
221
222
		do_action( 'woocommerce_new_order_item', $this->get_id(), $this, $this->get_order_id() );
223
	}
224
225
	/**
226
	 * Update data in the database.
227
	 * @since 2.6.0
228
	 */
229 View Code Duplication
	public function update() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
230
		global $wpdb;
231
232
		$wpdb->update( $wpdb->prefix . 'woocommerce_order_items', array(
233
			'order_item_name' => $this->get_name(),
234
			'order_item_type' => $this->get_type(),
235
			'order_id'        => $this->get_order_id()
236
		), array( 'order_item_id' => $this->get_id() ) );
237
238
		do_action( 'woocommerce_update_order_item', $this->get_id(), $this, $this->get_order_id() );
239
	}
240
241
	/**
242
	 * Read from the database.
243
	 * @since 2.6.0
244
	 * @param int|object $item ID of object to read, or already queried object.
245
	 */
246
	public function read( $item ) {
247
		global $wpdb;
248
249
		if ( is_numeric( $item ) && ! empty( $item ) ) {
250
			$data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item ) );
251
		} elseif ( ! empty( $item->order_item_id ) ) {
252
			$data = $item;
253
		} else {
254
			$data = false;
255
		}
256
257
		if ( $data ) {
258
			$this->set_order_id( $data->order_id );
259
			$this->set_id( $data->order_item_id );
260
			$this->set_name( $data->order_item_name );
261
			$this->set_type( $data->order_item_type );
262
			$this->read_meta_data();
263
		}
264
	}
265
266
	/**
267
	 * Save data to the database.
268
	 * @since 2.6.0
269
	 * @return int Item ID
270
	 */
271
	public function save() {
272
		if ( ! $this->get_id() ) {
273
			$this->create();
274
		} else {
275
			$this->update();
276
		}
277
		$this->save_meta_data();
278
279
		return $this->get_id();
280
	}
281
282
	/**
283
	 * Delete data from the database.
284
	 * @since 2.6.0
285
	 */
286
	public function delete() {
287
		if ( $this->get_id() ) {
288
			global $wpdb;
289
			do_action( 'woocommerce_before_delete_order_item', $this->get_id() );
290
			$wpdb->delete( $wpdb->prefix . 'woocommerce_order_items', array( 'order_item_id' => $this->get_id() ) );
291
			$wpdb->delete( $wpdb->prefix . 'woocommerce_order_itemmeta', array( 'order_item_id' => $this->get_id() ) );
292
			do_action( 'woocommerce_delete_order_item', $this->get_id() );
293
		}
294
	}
295
296
	/*
297
	|--------------------------------------------------------------------------
298
	| Meta Data Handling
299
	|--------------------------------------------------------------------------
300
	*/
301
302
	/**
303
	 * Get All Meta Data
304
	 * @return array
305
	 */
306
	public function get_meta_data() {
307
		return $this->_meta_data;
308
	}
309
310
	/**
311
	 * Expands things like term slugs before return.
312
	 * @param string $hideprefix (default: _)
313
	 * @return array
314
	 */
315
	public function get_formatted_meta_data( $hideprefix = '_' ) {
316
		$formatted_meta = array();
317
		$meta_data      = $this->get_meta_data();
318
319
		foreach ( $meta_data as $meta_id => $meta ) {
320 View Code Duplication
			if ( "" === $meta->value || is_serialized( $meta->value ) || ( ! empty( $hideprefix ) && substr( $meta->key, 0, 1 ) === $hideprefix ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
321
				continue;
322
			}
323
324
			$attribute_key = urldecode( str_replace( 'attribute_', '', $meta->key ) );
325
			$display_key   = wc_attribute_label( $attribute_key, is_callable( array( $this, 'get_product' ) ) ? $this->get_product() : false );
0 ignored issues
show
Bug introduced by
It seems like you code against a specific sub-type and not the parent class WC_Order_Item as the method get_product() does only exist in the following sub-classes of WC_Order_Item: WC_Order_Item_Product. Maybe you want to instanceof check for one of these explicitly?

Let’s take a look at an example:

abstract class User
{
    /** @return string */
    abstract public function getPassword();
}

class MyUser extends User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the parent class:

    abstract class User
    {
        /** @return string */
        abstract public function getPassword();
    
        /** @return string */
        abstract public function getDisplayName();
    }
    
Loading history...
326
			$display_value = $meta->value;
327
328 View Code Duplication
			if ( taxonomy_exists( $attribute_key ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
329
				$term = get_term_by( 'slug', $meta->value, $attribute_key );
330
				if ( ! is_wp_error( $term ) && is_object( $term ) && $term->name ) {
331
					$display_value = $term->name;
332
				}
333
			}
334
335
			$formatted_meta[ $meta_id ] = (object) array(
336
				'key'           => $meta->key,
337
				'value'         => $meta->key,
338
				'display_key'   => apply_filters( 'woocommerce_order_item_display_meta_key', $display_key ),
339
				'display_value' => apply_filters( 'woocommerce_order_item_display_meta_value', $display_value ),
340
			);
341
		}
342
343
		return $formatted_meta;
344
	}
345
346
	/**
347
	 * Internal meta keys we don't want exposed as part of meta_data.
348
	 * @return array()
0 ignored issues
show
Documentation introduced by
The doc-type array() could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
349
	 */
350
	protected function get_internal_meta_keys() {
351
		return array();
352
	}
353
354
	/**
355
	 * Get Meta Data by Key
356
	 * @param string $key
357
	 * @param bool $single return first found meta with key, or all with $key
358
	 * @return mixed
359
	 */
360 View Code Duplication
	public function get_meta( $key = '', $single = true ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
361
		$meta_ids = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key );
362
		$value    = '';
363
364
		if ( $meta_ids ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $meta_ids of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
365
			if ( $single ) {
366
				$value   = $this->_meta_data[ current( $meta_ids ) ]->value;
367
			} else {
368
				$value = array_intersect_key( $this->_meta_data, $meta_ids );
369
			}
370
		}
371
372
		return $value;
373
	}
374
375
	/**
376
	 * Set all meta data from array.
377
	 * @param array $data Key/Value pairs
378
	 */
379 View Code Duplication
	public function set_meta_data( $data ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
380
		if ( ! empty( $data ) && is_array( $data ) ) {
381
			foreach ( $data as $meta_id => $meta ) {
382
				$meta = (array) $meta;
383
				if ( isset( $meta['key'], $meta['value'] ) ) {
384
					$this->_meta_data[ $meta_id ] = (object) array(
385
						'key'   => $meta['key'],
386
						'value' => $meta['value']
387
					);
388
				}
389
			}
390
		}
391
	}
392
393
	/**
394
	 * Add meta data.
395
	 * @param array $data Key/Value pairs
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
396
	 */
397 View Code Duplication
	public function add_meta_data( $key, $value, $unique = false ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
398
		if ( $unique ) {
399
			$meta_ids = array_keys( wp_list_pluck( $this->_meta_data, 'key' ), $key );
400
			$this->_meta_data = array_diff_key( $this->_meta_data, array_fill_keys( $meta_ids, '' ) );
401
		}
402
		$this->_meta_data[ 'new-' . sizeof( $this->_meta_data ) ] = (object) array(
403
			'key'   => $key,
404
			'value' => $value
405
		);
406
	}
407
408
	/**
409
	 * Update meta data by key or ID, if provided.
410
	 * @param  string $key
411
	 * @param  string $value
412
	 * @param  int $meta_id
413
	 */
414 View Code Duplication
	public function update_meta_data( $key, $value, $meta_id = '' ) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
415
		if ( $meta_id && isset( $this->_meta_data[ $meta_id ] ) ) {
416
			$this->_meta_data[ $meta_id ] = (object) array(
417
				'key'   => $key,
418
				'value' => $value
419
			);
420
		} else {
421
			$this->add_meta_data( $key, $value, true );
422
		}
423
	}
424
425
	/**
426
	 * Read Meta Data from the database. Ignore any internal properties.
427
	 */
428 View Code Duplication
	protected function read_meta_data() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
429
		$this->_meta_data = array();
430
431
		if ( ! $this->get_id() ) {
432
			return;
433
		}
434
435
		$cache_key   = WC_Cache_Helper::get_cache_prefix( 'order_itemmeta' ) . $this->get_id();
436
		$cached_meta = wp_cache_get( $cache_key, 'order_itemmeta' );
437
438
		if ( false !== $cached_meta ) {
439
			$this->_meta_data = $cached_meta;
440
		} else {
441
			global $wpdb;
442
443
			$raw_meta_data = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d ORDER BY meta_id", $this->get_id() ) );
444
445
			foreach ( $raw_meta_data as $meta ) {
446
				if ( in_array( $meta->meta_key, $this->get_internal_meta_keys() ) ) {
447
					continue;
448
				}
449
				$this->_meta_data[ $meta->meta_id ] = (object) array( 'key' => $meta->meta_key, 'value' => $meta->meta_value );
450
			}
451
452
			wp_cache_set( $cache_key, $this->_meta_data, 'order_itemmeta' );
453
		}
454
	}
455
456
	/**
457
	 * Update Meta Data in the database.
458
	 */
459 View Code Duplication
	protected function save_meta_data() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
460
		global $wpdb;
461
		$all_meta_ids = array_map( 'absint', $wpdb->get_col( $wpdb->prepare( "SELECT meta_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE order_item_id = %d", $this->get_id() ) . " AND meta_key NOT IN ('" . implode( "','", array_map( 'esc_sql', $this->get_internal_meta_keys() ) ) . "');" ) );
462
		$set_meta_ids = array();
463
464
		foreach ( $this->_meta_data as $meta_id => $meta ) {
465
			if ( 'new' === substr( $meta_id, 0, 3 ) ) {
466
				$set_meta_ids[] = add_metadata( 'order_item', $this->get_id(), $meta->key, $meta->value, false );
467
			} else {
468
				update_metadata_by_mid( 'order_item', $meta_id, $meta->value, $meta->key );
469
				$set_meta_ids[] = absint( $meta_id );
470
			}
471
		}
472
473
		// Delete no longer set meta data
474
		$delete_meta_ids = array_diff( $all_meta_ids, $set_meta_ids );
475
476
		foreach ( $delete_meta_ids as $meta_id ) {
477
			delete_metadata_by_mid( 'order_item', $meta_id );
478
		}
479
480
		WC_Cache_Helper::incr_cache_prefix( 'order_itemmeta' );
481
		$this->read_meta_data();
482
	}
483
484
	/*
485
	|--------------------------------------------------------------------------
486
	| Array Access Methods
487
	|--------------------------------------------------------------------------
488
	|
489
	| For backwards compat with legacy arrays.
490
	|
491
	*/
492
493
	/**
494
	 * offsetSet for ArrayAccess
495
	 * @param string $offset
496
	 * @param mixed $value
497
	 */
498
	public function offsetSet( $offset, $value ) {
499
		if ( 'item_meta_array' === $offset ) {
500
			$this->_meta_data = $value;
0 ignored issues
show
Documentation Bug introduced by
It seems like $value of type * is incompatible with the declared type array of property $_meta_data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
501
			return;
502
		}
503
		$this->_data[ $offset ] = $value;
504
	}
505
506
	/**
507
	 * offsetUnset for ArrayAccess
508
	 * @param string $offset
509
	 */
510
	public function offsetUnset( $offset ) {
511
		if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) {
512
			$this->_meta_data = array();
513
			return;
514
		}
515
		unset( $this->_data[ $offset ] );
516
	}
517
518
	/**
519
	 * offsetExists for ArrayAccess
520
	 * @param string $offset
521
	 * @return bool
522
	 */
523
	public function offsetExists( $offset ) {
524
		if ( 'item_meta_array' === $offset || 'item_meta' === $offset ) {
525
			return true;
526
		}
527
		return isset( $this->_data[ $offset ] );
528
	}
529
530
	/**
531
	 * offsetGet for ArrayAccess
532
	 * @param string $offset
533
	 * @return mixed
534
	 */
535
	public function offsetGet( $offset ) {
536
		if ( 'item_meta_array' === $offset ) {
537
			return $this->_meta_data;
538
		} elseif ( 'item_meta' === $offset ) {
539
			$meta_values = wp_list_pluck( $this->_meta_data, 'value', 'key' );
540
			return $meta_values;
541
		}
542
		return isset( $this->_data[ $offset ] ) ? $this->_data[ $offset ] : null;
543
	}
544
}
545