Completed
Pull Request — develop (#1263)
by Naveen
02:39
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', 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
		$website_jsonld = $jsonld_array[0];
62
		if ( ! is_singular() ) {
63
			return $jsonld_array;
64
		}
65
		$entity_ids               = $this->relation_service->get_objects( $post_id, 'ids', null, 'publish' );
66
		$mentions                 = $this->entity_ids_to_jsonld_references( $entity_ids );
67
		$referenced_entities_data = $this->entity_ids_to_jsonld( $entity_ids );
68
		if ( is_array( $mentions ) && $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...
69
			$jsonld_array[] = $this->get_webpage_schema( $website_jsonld, $post_id, $mentions );
70
		}
71
72
		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...
73
			// Merge the homepage jsonld with annotated entities data.
74
			$jsonld_array = array_merge( $jsonld_array, $referenced_entities_data );
75
		}
76
77
		return $jsonld_array;
78
	}
79
80
81
	/**
82
	 * @param $website_schema array
83
	 * @param $page_id int
84
	 *
85
	 * @return array
86
	 */
87
	public function get_webpage_schema( $website_schema, $page_id, $mentions ) {
88
89
		$webpage = array(
90
			'@context' => 'http://schema.org',
91
			'@type'    => 'WebPage',
92
93
			'@id'             => get_home_url( '', '#webpage' ),
94
			'name'            => get_the_title( $page_id ),
95
			'description'     => get_the_excerpt( $page_id ),
96
			'isPartOf'        => array(
97
				'@id' => $website_schema['@id']
98
			),
99
			'potentialAction' => array(
100
				'@type'  => 'ReadAction',
101
				'target' => array( get_home_url() )
102
			)
103
		);
104
105
		if ( $mentions ) {
106
			$webpage['mentions'] = $mentions;
107
		}
108
109
		return $webpage;
110
111
	}
112
113
114
}