Completed
Push — develop ( bfde1a...07de1c )
by David
03:08
created

Offer_Structured_Data   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A entity_jsonld() 0 17 6
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
		if ( ! is_array( $jsonld['offers'] ) || ! is_numeric( key( $jsonld['offers'] ) ) ) {
33
			$jsonld['offers'] = array( $jsonld['offers'] );
34
		}
35
36
		foreach ( $jsonld['offers'] as &$offer ) {
37
			$this->shipping_zones->add_available_delivery_method( $offer );
38
		}
39
40
		return $jsonld;
41
	}
42
43
}
44