Completed
Pull Request — develop (#1263)
by Naveen
02:56
created

Jsonld_Homepage::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
1
<?php
2
3
namespace Wordlift\Jsonld;
4
5
/**
6
 * @since 3.27.9
7
 * @author Naveen Muthusamy <[email protected]>
8
 */
9
class Jsonld_Homepage {
10
	/**
11
	 * @var \Wordlift_Relation_Service
12
	 */
13
	private $relation_service;
14
	/**
15
	 * @var \Wordlift_Entity_Service
16
	 */
17
	private $entity_service;
18
	/**
19
	 * @var \Wordlift_Entity_Post_To_Jsonld_Converter
20
	 */
21
	private $entity_post_to_jsonld_service;
22
23
	/**
24
	 * Jsonld_Homepage constructor.
25
	 *
26
	 * @param $relation_service \Wordlift_Relation_Service
27
	 * @param $entity_service \Wordlift_Entity_Service
28
	 * @param $entity_post_to_jsonld_service \Wordlift_Entity_Post_To_Jsonld_Converter
29
	 */
30
	public function __construct( $relation_service, $entity_service, $entity_post_to_jsonld_service ) {
31
32
		$this->relation_service = $relation_service;
33
34
		$this->entity_service = $entity_service;
35
36
		$this->entity_post_to_jsonld_service = $entity_post_to_jsonld_service;
37
38
		add_filter( 'wl_website_jsonld_array', array( $this, 'add_webpage_and_mentions' ), 10, 2 );
39
40
	}
41
42
43
	private function entity_ids_to_jsonld_references( $entity_ids ) {
44
		$that = $this;
45
46
		return array_map( function ( $entity_id ) use ( $that ) {
47
			return array( '@id' => $that->entity_service->get_uri( $entity_id ) );
48
		}, $entity_ids );
49
	}
50
51
	private function entity_ids_to_jsonld( $entity_ids ) {
52
		$that = $this;
53
54
		return array_map( function ( $entity_id ) use ( $that ) {
55
			return $that->entity_post_to_jsonld_service->convert( $entity_id );
56
		}, $entity_ids );
57
	}
58
59
60
	public function add_webpage_and_mentions( $jsonld_array, $post_id ) {
61
62
		$website_jsonld           = $jsonld_array[0];
63
		$mentions                 = array();
64
		$referenced_entities_data = array();
65
66
//		if ( get_post_type( $post_id ) === 'entity' ) {
67
//			$jsonld['@type'] = 'WebPage';
68
//
69
//			$jsonld['mainEntity'] = array(
70
//				'@id' => \Wordlift_Entity_Service::get_instance()->get_uri( $post_id )
71
//		);
72
//
73
//			return $jsonld;
74
//		}
75
76
77
		if ( is_singular() ) {
78
			$entity_ids               = $this->relation_service->get_objects( $post_id, 'ids', null, 'publish' );
79
			$mentions                 = $this->entity_ids_to_jsonld_references( $entity_ids );
80
			$referenced_entities_data = $this->entity_ids_to_jsonld( $entity_ids );
81
		}
82
83
84
		$webpage_schema = $this->get_webpage_schema( $website_jsonld, $post_id, $mentions );
85
86
		if ( $referenced_entities_data ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $referenced_entities_data 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...
87
			// Merge the homepage jsonld with annotated entities data.
88
			$jsonld_array = array_merge( $jsonld_array, array_merge( array( $webpage_schema ), $referenced_entities_data ) );
89
		}
90
91
		return $jsonld_array;
92
	}
93
94
95
	/**
96
	 * @param $website_schema array
97
	 * @param $page_id int
98
	 *
99
	 * @return array
100
	 */
101
	public function get_webpage_schema( $website_schema, $page_id, $mentions ) {
102
103
		$webpage = array(
104
			'@context' => 'http://schema.org',
105
			'@type'    => 'WebPage',
106
107
			'@id'             => get_home_url( '', '#webpage' ),
108
			'name'            => get_the_title( $page_id ),
109
			'description'     => get_the_excerpt( $page_id ),
110
			'isPartOf'        => array(
111
				'@id' => $website_schema['@id']
112
			),
113
			'potentialAction' => array(
114
				'@type'  => 'ReadAction',
115
				'target' => array( get_home_url() )
116
			)
117
		);
118
119
		if ( $mentions ) {
120
			$webpage['mentions'] = $mentions;
121
		}
122
123
		return $webpage;
124
125
	}
126
127
128
}