Completed
Pull Request — develop (#1263)
by Naveen
03:13
created

Jsonld_Homepage::entity_ids_to_jsonld()   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\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( $this, 'add_mentions_if_singular' ), 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_mentions_if_singular( $jsonld, $post_id ) {
61
62
		$mentions                 = array();
63
		$referenced_entities_data = array();
64
65
66
		if ( get_post_type( $post_id ) === 'entity' ) {
67
			$jsonld['mainEntityOfPage'] = get_permalink( $post_id );
68
69
			return $jsonld;
70
		}
71
72
73
		if ( is_singular() ) {
74
			$jsonld['@type']          = 'WebPage';
75
			$entity_ids               = $this->relation_service->get_objects( $post_id, 'ids', null, 'publish' );
76
			$mentions                 = $this->entity_ids_to_jsonld_references( $entity_ids );
77
			$referenced_entities_data = $this->entity_ids_to_jsonld( $entity_ids );
78
		}
79
80
		if ( $mentions ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $mentions 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...
81
			$jsonld['mentions'] = $mentions;
82
		}
83
84
		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...
85
			$jsonld = array( $jsonld );
86
			// Merge the homepage jsonld with annotated entities data.
87
			$jsonld = array_merge( $jsonld, $referenced_entities_data );
88
		}
89
90
		return $jsonld;
91
	}
92
93
}