|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Define the Wordlift_Jsonld_Service class to support JSON-LD. |
|
4
|
|
|
* |
|
5
|
|
|
* @since 3.8.0 |
|
6
|
|
|
* @package Wordlift |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This class exports an entity using JSON-LD. |
|
11
|
|
|
* |
|
12
|
|
|
* @since 3.8.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
class Wordlift_Jsonld_Service { |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* A {@link Wordlift_Entity_Service} instance. |
|
18
|
|
|
* |
|
19
|
|
|
* @since 3.8.0 |
|
20
|
|
|
* @access private |
|
21
|
|
|
* @var Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
22
|
|
|
*/ |
|
23
|
|
|
private $entity_service; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* A {@link Wordlift_Entity_To_Jsonld_Converter} instance. |
|
27
|
|
|
* |
|
28
|
|
|
* @since 3.8.0 |
|
29
|
|
|
* @access private |
|
30
|
|
|
* @var \Wordlift_Entity_Post_To_Jsonld_Converter A {@link Wordlift_Entity_To_Jsonld_Converter} instance. |
|
31
|
|
|
*/ |
|
32
|
|
|
private $entity_to_jsonld_converter; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create a JSON-LD service. |
|
36
|
|
|
* |
|
37
|
|
|
* @since 3.8.0 |
|
38
|
|
|
* |
|
39
|
|
|
* @param \Wordlift_Entity_Service $entity_service A {@link Wordlift_Entity_Service} instance. |
|
40
|
|
|
* @param \Wordlift_Entity_Post_To_Jsonld_Converter $entity_to_jsonld_converter A {@link Wordlift_Entity_Post_To_Jsonld_Converter} instance. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( $entity_service, $entity_to_jsonld_converter ) { |
|
43
|
|
|
|
|
44
|
|
|
$this->entity_service = $entity_service; |
|
45
|
|
|
$this->entity_to_jsonld_converter = $entity_to_jsonld_converter; |
|
46
|
|
|
|
|
47
|
|
|
add_action( 'wp_footer', array( $this, 'wp_footer' ), PHP_INT_MAX ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Hook to WP's wp_footer action and load the JSON-LD data. |
|
52
|
|
|
* |
|
53
|
|
|
* @since 3.8.0 |
|
54
|
|
|
*/ |
|
55
|
|
|
public function wp_footer() { |
|
56
|
|
|
|
|
57
|
|
|
// We only care about singular pages. |
|
58
|
|
|
if ( ! is_singular() ) { |
|
59
|
|
|
return; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
// Get the entities related to the current post (and that are published). |
|
63
|
|
|
$post_id = get_the_ID(); |
|
64
|
|
|
$posts = $this->entity_service->is_entity( $post_id ) |
|
65
|
|
|
? array( get_the_ID() ) |
|
66
|
|
|
: array_unique( wl_core_get_related_entity_ids( $post_id, array( |
|
67
|
|
|
'status' => 'publish', |
|
68
|
|
|
) ) ); |
|
69
|
|
|
|
|
70
|
|
|
// Build the URL to load the JSON-LD asynchronously. |
|
71
|
|
|
$url = admin_url( 'admin-ajax.php?action=wl_jsonld' ); |
|
72
|
|
|
$entity_service = $this->entity_service; |
|
73
|
|
|
$data = implode( '&', array_map( function ( $item ) use ( $entity_service ) { |
|
74
|
|
|
return 'uri[]=' . rawurldecode( $entity_service->get_uri( $item ) ); |
|
75
|
|
|
}, $posts ) ); |
|
76
|
|
|
|
|
77
|
|
|
// Print the Javascript code. |
|
78
|
|
|
echo <<<EOF |
|
79
|
|
|
<script type="text/javascript"><!-- |
|
80
|
|
|
(function($) { $( window ).on( 'load', function() { $.post('$url','$data').done(function(data) { |
|
81
|
|
|
$('head').append( '<script type="application/ld+json">'+JSON.stringify(data)+'</s' + 'cript>' ); |
|
82
|
|
|
}); }); })(jQuery); |
|
83
|
|
|
// --></script> |
|
84
|
|
|
EOF; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Process calls to the AJAX 'wl_jsonld' endpoint. |
|
89
|
|
|
* |
|
90
|
|
|
* @since 3.8.0 |
|
91
|
|
|
*/ |
|
92
|
|
|
public function get() { |
|
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
// If no URI has been provided return an empty array. |
|
95
|
|
|
if ( ! isset( $_REQUEST['uri'] ) ) { |
|
96
|
|
|
wp_send_json( array() ); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Get an array of URIs to parse. |
|
100
|
|
|
$uris = is_array( $_REQUEST['uri'] ) ? $_REQUEST['uri'] : array( $_REQUEST['uri'] ); |
|
101
|
|
|
|
|
102
|
|
|
// An array of references which is captured when converting an URI to a |
|
103
|
|
|
// json which we gather to further expand our json-ld. |
|
104
|
|
|
$references = array(); |
|
105
|
|
|
|
|
106
|
|
|
// Set a reference to the entity_to_jsonld_converter to use in the closures. |
|
107
|
|
|
$entity_to_jsonld_converter = $this->entity_to_jsonld_converter; |
|
108
|
|
|
|
|
109
|
|
|
// Convert each URI to a JSON-LD array, while gathering referenced entities. |
|
110
|
|
|
// in the references array. |
|
111
|
|
|
$jsonld = array_merge( |
|
112
|
|
|
array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
113
|
|
|
|
|
114
|
|
|
return $entity_to_jsonld_converter->convert( $item, $references ); |
|
115
|
|
|
}, $uris ), |
|
116
|
|
|
// Convert each URI in the references array to JSON-LD. We don't output |
|
117
|
|
|
// entities already output above (hence the array_diff). |
|
118
|
|
|
array_map( function ( $item ) use ( $entity_to_jsonld_converter, &$references ) { |
|
119
|
|
|
|
|
120
|
|
|
return $entity_to_jsonld_converter->convert( $item, $references ); |
|
121
|
|
|
}, array_diff( $references, $uris ) ) |
|
122
|
|
|
); |
|
123
|
|
|
|
|
124
|
|
|
// Finally send the JSON-LD. |
|
125
|
|
|
wp_send_json( $jsonld ); |
|
126
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
} |
|
130
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: