-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSON-API.php
53 lines (52 loc) · 1.5 KB
/
JSON-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
<?php
add_action( 'rest_api_init', function () {
register_rest_route( 'charta-vitae', '/projects/all',
array( 'methods' => 'GET', 'callback' => 'cv_get_project_data' )
);
} );
function cv_get_project_data(WP_REST_Request $request){
# get all cv_projects and their properties
$posts = get_posts(array( 'post_type'=>'cv_project', 'numberposts'=>-1 ));
$data = array( 'projects'=>[], 'links'=>[] );
foreach( $posts as $post){
$proj = [
'id'=> $post->ID,
'title'=>$post->post_title,
'url'=>get_permalink($post->ID),
'tags'=>[]
];
# set dates if they exist
if(($start = get_post_meta($post->ID, "start", true)) != '' ){
$proj['start'] = $start;
}
if(($end = get_post_meta($post->ID, "end", true)) != '' ){
$proj['end'] = $end;
}
# set tags if they exist
foreach( wp_get_post_terms($post->ID,'cv_tag') as $tag){
$proj['tags'][] = $tag->slug;
}
$data['projects'][] = $proj;
# add a link for causal relationships if any
$caused = get_post_meta($post->ID,'caused');
if( count($caused) > 0 ){
# add a link for each caused project
foreach($caused as $idString){
$data['links'][] = [
'source'=>$post->ID, 'target'=>(int)$idString, 'type'=>'causal'
];
}
}
# add a link for a parent relationship if any
if( $post->post_parent != 0 ){
$data['links'][] = [
'source'=>$post->ID, 'target'=>$post->post_parent,
'type'=>'constitutive'
];
}
$data['tags'] = get_terms(['taxonomy'=>'cv_tag']);
shuffle($data['tags']);
}
return $data;
}
?>