Completed
Pull Request — develop (#1347)
by David
04:57 queued 01:46
created

Offer_Structured_Data::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Shipping_Data;
4
5
class Offer_Structured_Data {
6
7
	/**
8
	 * @var Shipping_Zones
9
	 */
10
	private $shipping_zones;
11
12
	/**
13
	 * Offer_Structured_Data constructor.
14
	 *
15
	 * @param Shipping_Zones $shipping_zones
16
	 */
17
	public function __construct( $shipping_zones ) {
18
19
		$this->shipping_zones = $shipping_zones;
20
21
		add_filter( 'wl_entity_jsonld', array( $this, 'entity_jsonld' ), 10, 2 );
22
23
	}
24
25
	public function entity_jsonld( $jsonld, $post_id ) {
26
27
		// Bail out if it's not a Product or the offers property isn't set.
28
		if ( ! in_array( 'Product', (array) $jsonld['@type'] ) || ! isset( $jsonld['offers'] ) ) {
29
			return $jsonld;
30
		}
31
32 View Code Duplication
		if ( ! is_array( $jsonld['offers'] ) || ! is_numeric( key( $jsonld['offers'] ) ) ) {
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...
33
			$jsonld['offers'] = array( $jsonld['offers'] );
34
		}
35
36
		$product = new Product( $post_id );
37
38
		foreach ( $jsonld['offers'] as &$offer ) {
39
			$this->shipping_zones->add_available_delivery_method( $offer );
40
			$this->shipping_zones->add_offer_shipping_details( $offer, $product );
41
		}
42
43
		return $jsonld;
44
	}
45
46
}
47