|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Taxonomy Term JSON-LD Adapter. |
|
4
|
|
|
* |
|
5
|
|
|
* The {@link Wordlift_Term_JsonLd_Adapter} intercepts calls to terms' pages and loads the related JSON-LD in page. |
|
6
|
|
|
* |
|
7
|
|
|
* @since 3.20.0 |
|
8
|
|
|
* @package Wordlift |
|
9
|
|
|
* @subpackage Wordlift/public |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the {@link Wordlift_Term_JsonLd_Adapter} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.20.0 |
|
16
|
|
|
*/ |
|
17
|
|
|
class Wordlift_Term_JsonLd_Adapter { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The {@link Wordlift_Entity_Uri_Service} instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @since 3.20.0 |
|
23
|
|
|
* @access private |
|
24
|
|
|
* @var \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
25
|
|
|
*/ |
|
26
|
|
|
private $entity_uri_service; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* The {@link Wordlift_Jsonld_Service} instance. |
|
30
|
|
|
* |
|
31
|
|
|
* @since 3.20.0 |
|
32
|
|
|
* @access private |
|
33
|
|
|
* @var \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
34
|
|
|
*/ |
|
35
|
|
|
private $jsonld_service; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Wordlift_Term_JsonLd_Adapter constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @since 3.20.0 |
|
41
|
|
|
* |
|
42
|
|
|
* @param \Wordlift_Entity_Uri_Service $entity_uri_service The {@link Wordlift_Entity_Uri_Service} instance. |
|
43
|
|
|
* @param \Wordlift_Jsonld_Service $jsonld_service The {@link Wordlift_Jsonld_Service} instance. |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct( $entity_uri_service, $jsonld_service ) { |
|
46
|
|
|
|
|
47
|
|
|
add_action( 'wp_head', array( $this, 'wp_head' ) ); |
|
48
|
|
|
|
|
49
|
|
|
$this->entity_uri_service = $entity_uri_service; |
|
50
|
|
|
$this->jsonld_service = $jsonld_service; |
|
51
|
|
|
|
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Hook to `wp_head` to print the JSON-LD. |
|
56
|
|
|
* |
|
57
|
|
|
* @since 3.20.0 |
|
58
|
|
|
*/ |
|
59
|
|
|
public function wp_head() { |
|
60
|
|
|
|
|
61
|
|
|
// Bail out if it's not a category page. |
|
62
|
|
|
if ( ! is_tax() && ! is_category() ) { |
|
63
|
|
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$term = get_queried_object(); |
|
67
|
|
|
$term_id = $term->term_id; |
|
68
|
|
|
|
|
69
|
|
|
// The `_wl_entity_id` are URIs. |
|
70
|
|
|
$entity_ids = get_term_meta( $term_id, '_wl_entity_id' ); |
|
71
|
|
|
$entity_uri_service = $this->entity_uri_service; |
|
72
|
|
|
|
|
73
|
|
|
$local_entity_ids = array_filter( $entity_ids, function ( $uri ) use ( $entity_uri_service ) { |
|
74
|
|
|
return $entity_uri_service->is_internal( $uri ); |
|
75
|
|
|
} ); |
|
76
|
|
|
|
|
77
|
|
|
// Bail out if there are no entities. |
|
78
|
|
|
if ( empty( $local_entity_ids ) ) { |
|
79
|
|
|
return; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$post = $this->entity_uri_service->get_entity( $local_entity_ids[0] ); |
|
83
|
|
|
$jsonld = $this->jsonld_service->get_jsonld( false, $post->ID ); |
|
84
|
|
|
// Reset the `url` to the term page. |
|
85
|
|
|
$jsonld[0]['url'] = get_term_link( $term_id ); |
|
86
|
|
|
$jsonld_string = wp_json_encode( $jsonld ); |
|
87
|
|
|
|
|
88
|
|
|
echo "<script type=\"application/ld+json\">$jsonld_string</script>"; |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|