-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-api.php
257 lines (223 loc) · 7.87 KB
/
class-api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
/**
* API
*
* The LDP API itself
*
* @package WPLDP
* @version 1.0.0
* @author Benoit Alessandroni
* @license https://www.gnu.org/licenses/gpl-2.0.txt GNU/GPLv2
* @since 2.0.0
*/
namespace WpLdp;
if ( ! class_exists( '\WpLdp\Api' ) ) {
/**
* Handles everything related to our custom LDP API.
*
* @category Class
* @package WPLDP
* @author Benoit Alessandroni
* @license https://www.gnu.org/licenses/gpl-2.0.txt GNU/GPLv2
*/
class Api {
/**
* The base url of the API.
*/
const LDP_API_URL = 'api/ldp/v1/';
/**
* Class default constructor
*
* @return {Api} Instance of the Api Class
*/
public function __construct() {
add_filter( 'rest_url_prefix', array( $this, 'define_api_slug' ) );
add_action( 'rest_api_init', function() {
register_rest_route( 'ldp/v1', '/schema/', array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_api_definition' ),
), true );
register_rest_route( 'ldp/v1', '/(?P<ldp_container>[a-zA-Z0-9-]+)/(?P<ldp_resource>[a-zA-Z0-9-]+)', array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array( $this, 'get_resource' ),
) );
});
}
/**
* Gets the general schema this site LPD API.
*
* @param \WP_REST_Request $request The current HTTP request object.
* @param \WP_REST_Response $response The current HTTP response object.
* @return \WP_REST_Response $response The current HTTP response object.
*/
public function get_api_definition( \WP_REST_Request $request, \WP_REST_Response $response = null ) {
header( 'Content-Type: application/ld+json' );
header( 'Access-Control-Allow-Origin: *' );
$query = new \WP_Query(
array(
'post_type' => 'ldp_resource',
'posts_per_page' => -1,
)
);
$array = [];
$posts = $query->get_posts();
$result = array(
'@context' => get_option( 'ldp_context', 'http://lov.okfn.org/dataset/lov/context' ),
'@graph' => array(
array(
'@id' => rtrim( get_rest_url(), '/' ) . $request->get_route() . '/',
'@type' => 'http://www.w3.org/ns/ldp#BasicContainer',
'http://www.w3.org/ns/ldp#contains' => array(),
),
)
);
foreach ( $posts as $post ) {
$values = get_the_terms( $post->ID, 'ldp_container' );
if ( ! empty( $values ) ) {
if ( empty( $values[0] ) ) {
$value = reset( $values );
} else {
$value = $values[0];
}
$term_meta = get_option( "ldp_container_$value->term_id" );
$rdf_type = isset( $term_meta['ldp_rdf_type'] ) ? $term_meta['ldp_rdf_type'] : null;
if ( null !== $rdf_type ) {
if ( array_key_exists( $rdf_type,$array ) ) {
$array[ $rdf_type ]['value']++;
} else {
$array[ $rdf_type ]['value'] = 1;
$array[ $rdf_type ]['id'] = strtolower( explode( ':', $rdf_type )[1] );
}
}
}
}
foreach ( $array as $key => $value ) {
$current_container_entry = array();
$current_container_entry['@id'] = site_url( '/' ) . \WpLdp\Api::LDP_API_URL . $value['id'] . '/';
$current_container_entry['@type'] = 'http://www.w3.org/ns/ldp#BasicContainer';
$current_container_entry['@count'] = $value['value'];
$result['@graph'][0]['http://www.w3.org/ns/ldp#contains'][] = $current_container_entry;
}
return rest_ensure_response( $result );
}
/**
* Gets the details of the current ldp resource.
*
* @param \WP_REST_Request $request The current HTTP request object.
* @param \WP_REST_Response $response The current HTTP response object.
* @return \WP_REST_Response $response The current HTTP response object.
*/
public function get_resource( \WP_REST_Request $request, \WP_REST_Response $response = null ) {
$params = $request->get_params();
$ldp_container = $params['ldp_container'];
$ldp_resource_slug = $params['ldp_resource'];
$headers = $request->get_headers();
if ( isset( $headers['accept'] )
&& strstr( $headers['accept'][0], 'text/html' ) !== false ) {
header( 'Location: ' . site_url( '/' ) . Wpldp::FRONT_PAGE_URL . '#' . get_rest_url() . 'ldp/v1/' . $ldp_container . '/' . $ldp_resource_slug . '/' );
exit;
}
header( 'Content-Type: application/ld+json' );
header( 'Access-Control-Allow-Origin: *' );
$query = new \WP_Query(
array(
'name' => $ldp_resource_slug,
'post_type' => 'ldp_resource',
)
);
$post = $query->get_posts();
if ( ! empty( $post ) && is_array( $post ) ) {
$post = $post[0];
} else {
return null;
}
// Getting general information about the container associated with the current resource.
$fields = \WpLdp\Utils::get_resource_fields_list( $post->ID );
$terms = wp_get_post_terms( $post->ID, 'ldp_container' );
if ( ! empty( $terms ) && is_array( $terms ) ) {
$term_id = $terms[0]->term_id;
$term_meta = get_option( "ldp_container_$term_id" );
$rdf_type = isset( $term_meta['ldp_rdf_type'] ) ? $term_meta['ldp_rdf_type'] : null;
}
$result = array(
'@context' => get_option( 'ldp_context', 'http://lov.okfn.org/dataset/lov/context' ),
'@graph' => array( array() ),
);
$referer = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null;
// Handling special case of editing trhough the WordPress admin backend.
$custom_fields_keys = get_post_custom_keys( $post->ID );
foreach ( $fields as $field ) {
$field_name = \WpLdp\Utils::get_field_name( $field );
if ( isset( $field_name ) ) {
if ( ! isset( $field->multiple ) || ! $field->multiple ) {
$field_value = get_post_custom_values( $field_name, $post->ID )[0];
$result['@graph'][0][ $field_name ] = ! empty( $field_value ) ? $field_value : null;
} else {
$result['@graph'][0][ $field_name ] = array();
$field_values = get_post_custom_values( $field_name, $post->ID )[0];
if ( ! empty( $field_values ) ) {
$field_values = unserialize( $field_values );
foreach ( $field_values as $value ) {
$multiple_field_entry = array(
'@id' => ! empty( $value ) ? $value : null,
);
$result['@graph'][0][ $field_name ][] = $multiple_field_entry;
}
}
}
}
}
// Get user to retrieve associated posts !
$user_login = null;
foreach ( $fields as $field ) {
$field_name = \WpLdp\Utils::get_field_name( $field );
if ( isset( $field_name ) && 'foaf:nick' === $field_name ) {
$user_login = get_post_custom_values( $field_name, $post->ID )[0];
}
}
if ( ! empty( $user_login ) ) {
$user = get_user_by( 'login', $user_login );
if ( $user ) {
$loop = new \WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 12,
'orderby' => 'menu_order',
'author' => $user->data->ID,
'post_status' => 'any',
) );
$posts = $loop->get_posts();
if ( ! empty( $posts ) ) {
$result['@graph'][0]['posts'] = array( array() );
foreach ( $posts as $post ) {
$current_post_entry = array();
$current_post_entry['@id'] = get_permalink( $post->ID );
$current_post_entry['dc:title'] = $post->post_title;
$post_content = ( ! empty( $post->post_content ) && false !== $post->post_content ) ? substr( $post->post_content, 0, 150 ) : null;
if ( ! empty( $post->post_content ) ) {
$current_post_entry['sioc:blogPost'] = $post_content;
}
$result['@graph'][0]['posts'][] = $current_post_entry;
}
}
}
}
if ( ! empty( $rdf_type ) ) {
$result['@graph'][0]['@type'] = $rdf_type;
}
$result['@graph'][0]['@id'] = rtrim( get_rest_url(), '/' ) . $request->get_route() . '/';
return rest_ensure_response( $result );
}
/**
* Filters the current site API slug.
*
* @param {string} $slug The current site API slug.
* @return {string} $slug The current site API slug.
*/
public function define_api_slug( $slug ) {
return 'api';
}
}
$wpldp_api = new Api();
} else {
exit( 'Class Api already exists' );
}