|
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 ) { |
|
|
|
|
|
|
81
|
|
|
$jsonld['mentions'] = $mentions; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
if ( $referenced_entities_data ) { |
|
|
|
|
|
|
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
|
|
|
} |
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.