|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file defines the JSON-LD endpoint. |
|
4
|
|
|
* |
|
5
|
|
|
* @author David Riccitelli <[email protected]> |
|
6
|
|
|
* @package Wordlift\Jsonld |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Wordlift\Jsonld; |
|
10
|
|
|
|
|
11
|
|
|
use Wordlift_Jsonld_Service; |
|
12
|
|
|
use WP_REST_Request; |
|
13
|
|
|
use WP_REST_Response; |
|
14
|
|
|
use WP_REST_Server; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Jsonld_Endpoint |
|
18
|
|
|
* |
|
19
|
|
|
* @package Wordlift\Jsonld |
|
20
|
|
|
*/ |
|
21
|
|
|
class Jsonld_Endpoint { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The {@link Wordlift_Jsonld_Service} instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @var Wordlift_Jsonld_Service The {@link Wordlift_Jsonld_Service} instance. |
|
27
|
|
|
*/ |
|
28
|
|
|
private $jsonld_service; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Wordlift_Entity_Uri_Service |
|
31
|
|
|
*/ |
|
32
|
|
|
private $entity_uri_service; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Jsonld_Endpoint constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param Jsonld_Service $jsonld_service |
|
38
|
|
|
* @param \Wordlift_Entity_Uri_Service $entity_uri_service |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct( $jsonld_service, $entity_uri_service ) { |
|
41
|
|
|
|
|
42
|
|
|
$this->jsonld_service = $jsonld_service; |
|
|
|
|
|
|
43
|
|
|
$this->entity_uri_service = $entity_uri_service; |
|
44
|
|
|
|
|
45
|
|
|
// PHP 5.3 compatibility. |
|
46
|
|
|
$that = $this; |
|
47
|
|
|
add_action( 'rest_api_init', function () use ( $that ) { |
|
48
|
|
|
register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<id>\d+)', array( |
|
49
|
|
|
'methods' => WP_REST_Server::READABLE, |
|
50
|
|
|
'callback' => array( $that, 'jsonld_using_post_id' ), |
|
51
|
|
|
'args' => array( |
|
52
|
|
|
'id' => array( |
|
53
|
|
|
'validate_callback' => function ( $param, $request, $key ) { |
|
|
|
|
|
|
54
|
|
|
return is_numeric( $param ); |
|
55
|
|
|
}, |
|
56
|
|
|
'sanitize_callback' => 'absint', |
|
57
|
|
|
), |
|
58
|
|
|
) |
|
59
|
|
|
) ); |
|
60
|
|
|
|
|
61
|
|
|
register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/http/(?P<item_id>.*)', array( |
|
62
|
|
|
'methods' => 'GET', |
|
63
|
|
|
'callback' => array( $that, 'jsonld_using_item_id' ), |
|
64
|
|
|
) ); |
|
65
|
|
|
|
|
66
|
|
|
register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/post-meta/(?P<meta_key>[^/]+)', array( |
|
67
|
|
|
'methods' => 'GET', |
|
68
|
|
|
'callback' => array( $that, 'jsonld_using_post_meta' ), |
|
69
|
|
|
) ); |
|
70
|
|
|
|
|
71
|
|
|
register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/meta/(?P<meta_key>[^/]+)', array( |
|
72
|
|
|
'methods' => 'GET', |
|
73
|
|
|
'callback' => array( $that, 'jsonld_using_meta' ), |
|
74
|
|
|
) ); |
|
75
|
|
|
|
|
76
|
|
|
register_rest_route( WL_REST_ROUTE_DEFAULT_NAMESPACE, '/jsonld/(?P<post_type>.*)/(?P<post_name>.*)', array( |
|
77
|
|
|
'methods' => 'GET', |
|
78
|
|
|
'callback' => array( $that, 'jsonld_using_get_page_by_path' ), |
|
79
|
|
|
) ); |
|
80
|
|
|
|
|
81
|
|
|
} ); |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Callback for the JSON-LD request. |
|
87
|
|
|
* |
|
88
|
|
|
* @param array $request { |
|
89
|
|
|
* The request array. |
|
90
|
|
|
* |
|
91
|
|
|
* @type int $id The post id. |
|
92
|
|
|
* } |
|
93
|
|
|
* |
|
94
|
|
|
* @return WP_REST_Response |
|
95
|
|
|
* @throws \Exception |
|
96
|
|
|
*/ |
|
97
|
|
|
public function jsonld_using_post_id( $request ) { |
|
98
|
|
|
|
|
99
|
|
|
$post_id = $request['id']; |
|
100
|
|
|
$type = ( 0 === $post_id ) ? Jsonld_Service::TYPE_HOMEPAGE : Jsonld_Service::TYPE_POST; |
|
101
|
|
|
|
|
102
|
|
|
// Send the generated JSON-LD. |
|
103
|
|
|
$data = $this->jsonld_service->get( $type, $post_id ); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
return Jsonld_Response_Helper::to_response( $data ); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Provide a JSON-LD given the itemId. |
|
110
|
|
|
* |
|
111
|
|
|
* @param array $request { |
|
112
|
|
|
* The request array. |
|
113
|
|
|
* |
|
114
|
|
|
* @type string $item_id The entity item id. |
|
115
|
|
|
* } |
|
116
|
|
|
* |
|
117
|
|
|
* @return WP_REST_Response |
|
118
|
|
|
* @throws \Exception |
|
119
|
|
|
*/ |
|
120
|
|
|
public function jsonld_using_item_id( $request ) { |
|
121
|
|
|
|
|
122
|
|
|
$item_id = 'http://' . $request['item_id']; |
|
123
|
|
|
$post = $this->entity_uri_service->get_entity( $item_id ); |
|
124
|
|
|
|
|
125
|
|
|
if ( ! is_a( $post, 'WP_Post' ) ) { |
|
126
|
|
|
return new WP_REST_Response( esc_html( "$item_id not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this->jsonld_using_post_id( array( 'id' => $post->ID, ) ); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function jsonld_using_get_page_by_path( $request ) { |
|
133
|
|
|
|
|
134
|
|
|
$post_name = $request['post_name']; |
|
135
|
|
|
$post_type = $request['post_type']; |
|
136
|
|
|
|
|
137
|
|
|
global $wpdb; |
|
138
|
|
|
|
|
139
|
|
|
$sql = " |
|
140
|
|
|
SELECT ID |
|
141
|
|
|
FROM $wpdb->posts |
|
142
|
|
|
WHERE post_name = %s |
|
143
|
|
|
AND post_type = %s |
|
144
|
|
|
"; |
|
145
|
|
|
|
|
146
|
|
|
$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $post_name, $post_type ) ); |
|
147
|
|
|
|
|
148
|
|
|
if ( is_null( $post_id ) ) { |
|
149
|
|
|
return new WP_REST_Response( esc_html( "$post_name of type $post_type not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @param WP_REST_Request $request |
|
157
|
|
|
* |
|
158
|
|
|
* @return WP_REST_Response |
|
159
|
|
|
* @throws \Exception |
|
160
|
|
|
*/ |
|
161
|
|
|
public function jsonld_using_post_meta( $request ) { |
|
162
|
|
|
|
|
163
|
|
|
$meta_key = $request['meta_key']; |
|
164
|
|
|
$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
165
|
|
|
|
|
166
|
|
|
global $wpdb; |
|
167
|
|
|
|
|
168
|
|
|
$sql = " |
|
169
|
|
|
SELECT post_id AS ID |
|
170
|
|
|
FROM $wpdb->postmeta |
|
171
|
|
|
WHERE meta_key = %s |
|
172
|
|
|
AND meta_value = %s |
|
173
|
|
|
LIMIT 1 |
|
174
|
|
|
"; |
|
175
|
|
|
|
|
176
|
|
|
$post_id = $wpdb->get_var( $wpdb->prepare( $sql, $meta_key, $meta_value ) ); |
|
177
|
|
|
|
|
178
|
|
|
if ( is_null( $post_id ) ) { |
|
179
|
|
|
return new WP_REST_Response( esc_html( "Post with meta key $meta_key and value $meta_value not found." ), 404, array( 'Content-Type' => 'text/html' ) ); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $this->jsonld_using_post_id( array( 'id' => $post_id, ) ); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function jsonld_using_meta( $request ) { |
|
186
|
|
|
|
|
187
|
|
|
global $wpdb; |
|
188
|
|
|
|
|
189
|
|
|
$meta_key = $request['meta_key']; |
|
190
|
|
|
$meta_value = urldecode( current( $request->get_query_params( 'meta_value' ) ) ); |
|
191
|
|
|
|
|
192
|
|
|
$results = $wpdb->get_results( $wpdb->prepare( |
|
193
|
|
|
" |
|
194
|
|
|
SELECT pm.post_id AS id, %s AS type |
|
195
|
|
|
FROM {$wpdb->postmeta} pm |
|
196
|
|
|
INNER JOIN {$wpdb->posts} p |
|
197
|
|
|
ON p.ID = pm.post_id AND p.post_status = 'publish' |
|
198
|
|
|
WHERE pm.meta_key = %s AND pm.meta_value = %s |
|
199
|
|
|
UNION |
|
200
|
|
|
SELECT term_id AS id, %s AS type |
|
201
|
|
|
FROM {$wpdb->termmeta} |
|
202
|
|
|
WHERE meta_key = %s AND meta_value = %s |
|
203
|
|
|
", |
|
204
|
|
|
Jsonld_Service::TYPE_POST, |
|
205
|
|
|
$meta_key, |
|
206
|
|
|
$meta_value, |
|
207
|
|
|
Jsonld_Service::TYPE_TERM, |
|
208
|
|
|
$meta_key, |
|
209
|
|
|
$meta_value |
|
210
|
|
|
) ); |
|
211
|
|
|
|
|
212
|
|
|
$jsonld_service = $this->jsonld_service; |
|
213
|
|
|
|
|
214
|
|
|
$data = array_reduce( $results, function ( $carry, $result ) use ( $jsonld_service ) { |
|
215
|
|
|
$jsonld = $jsonld_service->get( $result->type, $result->id ); |
|
|
|
|
|
|
216
|
|
|
|
|
217
|
|
|
return array_merge( $carry, $jsonld ); |
|
218
|
|
|
}, array() ); |
|
219
|
|
|
|
|
220
|
|
|
return Jsonld_Response_Helper::to_response( $data ); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
} |
|
224
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..