Code Duplication    Length = 56-56 lines in 3 locations

includes/api/legacy/v1/class-wc-api-resource.php 1 location

@@ 172-227 (lines=56) @@
169
	 * @param object $resource the resource object (e.g WC_Order)
170
	 * @return mixed
171
	 */
172
	public function maybe_add_meta( $data, $resource ) {
173
174
		if ( isset( $this->server->params['GET']['filter']['meta'] ) && 'true' === $this->server->params['GET']['filter']['meta'] && is_object( $resource ) ) {
175
176
			// don't attempt to add meta more than once
177
			if ( preg_grep( '/[a-z]+_meta/', array_keys( $data ) ) )
178
				return $data;
179
180
			// define the top-level property name for the meta
181
			switch ( get_class( $resource ) ) {
182
183
				case 'WC_Order':
184
					$meta_name = 'order_meta';
185
					break;
186
187
				case 'WC_Coupon':
188
					$meta_name = 'coupon_meta';
189
					break;
190
191
				case 'WP_User':
192
					$meta_name = 'customer_meta';
193
					break;
194
195
				default:
196
					$meta_name = 'product_meta';
197
					break;
198
			}
199
200
			if ( is_a( $resource, 'WP_User' ) ) {
201
202
				// customer meta
203
				$meta = (array) get_user_meta( $resource->ID );
204
205
			} elseif ( is_a( $resource, 'WC_Product_Variation' ) ) {
206
207
				// product variation meta
208
				$meta = (array) get_post_meta( $resource->get_variation_id() );
209
210
			} else {
211
212
				// coupon/order/product meta
213
				$meta = (array) get_post_meta( $resource->id );
214
			}
215
216
			foreach( $meta as $meta_key => $meta_value ) {
217
218
				// don't add hidden meta by default
219
				if ( ! is_protected_meta( $meta_key ) ) {
220
					$data[ $meta_name ][ $meta_key ] = maybe_unserialize( $meta_value[0] );
221
				}
222
			}
223
224
		}
225
226
		return $data;
227
	}
228
229
	/**
230
	 * Restrict the fields included in the response if the request specified certain only certain fields should be returned

includes/api/legacy/v2/class-wc-api-resource.php 1 location

@@ 231-286 (lines=56) @@
228
	 * @param object $resource the resource object (e.g WC_Order)
229
	 * @return mixed
230
	 */
231
	public function maybe_add_meta( $data, $resource ) {
232
233
		if ( isset( $this->server->params['GET']['filter']['meta'] ) && 'true' === $this->server->params['GET']['filter']['meta'] && is_object( $resource ) ) {
234
235
			// don't attempt to add meta more than once
236
			if ( preg_grep( '/[a-z]+_meta/', array_keys( $data ) ) )
237
				return $data;
238
239
			// define the top-level property name for the meta
240
			switch ( get_class( $resource ) ) {
241
242
				case 'WC_Order':
243
					$meta_name = 'order_meta';
244
					break;
245
246
				case 'WC_Coupon':
247
					$meta_name = 'coupon_meta';
248
					break;
249
250
				case 'WP_User':
251
					$meta_name = 'customer_meta';
252
					break;
253
254
				default:
255
					$meta_name = 'product_meta';
256
					break;
257
			}
258
259
			if ( is_a( $resource, 'WP_User' ) ) {
260
261
				// customer meta
262
				$meta = (array) get_user_meta( $resource->ID );
263
264
			} elseif ( is_a( $resource, 'WC_Product_Variation' ) ) {
265
266
				// product variation meta
267
				$meta = (array) get_post_meta( $resource->get_variation_id() );
268
269
			} else {
270
271
				// coupon/order/product meta
272
				$meta = (array) get_post_meta( $resource->id );
273
			}
274
275
			foreach( $meta as $meta_key => $meta_value ) {
276
277
				// don't add hidden meta by default
278
				if ( ! is_protected_meta( $meta_key ) ) {
279
					$data[ $meta_name ][ $meta_key ] = maybe_unserialize( $meta_value[0] );
280
				}
281
			}
282
283
		}
284
285
		return $data;
286
	}
287
288
	/**
289
	 * Restrict the fields included in the response if the request specified certain only certain fields should be returned

includes/api/legacy/v3/class-wc-api-resource.php 1 location

@@ 231-286 (lines=56) @@
228
	 * @param object $resource the resource object (e.g WC_Order)
229
	 * @return mixed
230
	 */
231
	public function maybe_add_meta( $data, $resource ) {
232
233
		if ( isset( $this->server->params['GET']['filter']['meta'] ) && 'true' === $this->server->params['GET']['filter']['meta'] && is_object( $resource ) ) {
234
235
			// don't attempt to add meta more than once
236
			if ( preg_grep( '/[a-z]+_meta/', array_keys( $data ) ) )
237
				return $data;
238
239
			// define the top-level property name for the meta
240
			switch ( get_class( $resource ) ) {
241
242
				case 'WC_Order':
243
					$meta_name = 'order_meta';
244
					break;
245
246
				case 'WC_Coupon':
247
					$meta_name = 'coupon_meta';
248
					break;
249
250
				case 'WP_User':
251
					$meta_name = 'customer_meta';
252
					break;
253
254
				default:
255
					$meta_name = 'product_meta';
256
					break;
257
			}
258
259
			if ( is_a( $resource, 'WP_User' ) ) {
260
261
				// customer meta
262
				$meta = (array) get_user_meta( $resource->ID );
263
264
			} elseif ( is_a( $resource, 'WC_Product_Variation' ) ) {
265
266
				// product variation meta
267
				$meta = (array) get_post_meta( $resource->get_variation_id() );
268
269
			} else {
270
271
				// coupon/order/product meta
272
				$meta = (array) get_post_meta( $resource->id );
273
			}
274
275
			foreach( $meta as $meta_key => $meta_value ) {
276
277
				// don't add hidden meta by default
278
				if ( ! is_protected_meta( $meta_key ) ) {
279
					$data[ $meta_name ][ $meta_key ] = maybe_unserialize( $meta_value[0] );
280
				}
281
			}
282
283
		}
284
285
		return $data;
286
	}
287
288
	/**
289
	 * Restrict the fields included in the response if the request specified certain only certain fields should be returned