-
Notifications
You must be signed in to change notification settings - Fork 4
/
rest-prepare-posts.php
55 lines (42 loc) · 1.5 KB
/
rest-prepare-posts.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
<?php
// phpcs:disable
/**
* Plugin Name: REST Prepare Posts
* Description: Modifies the contents of posts in the WP REST API. Adds more detailed taxonomy term information. Also adds Timber Post context. Essentially, this is keeping the REST API in sync with what's added for server side templates.
* Author: NYC Opportunity
*/
// phpcs:enable
use RestPreparePosts\RestPreparePosts as RestPreparePosts;
require_once plugin_dir_path(__FILE__) . '/rest-prepare-posts/RestPreparePosts.php';
// Add custom fields to each post type in our list
add_action('rest_api_init', function() {
$types = ['programs', 'jobs', 'guides', 'employer-programs'];
$taxonomies = get_taxonomies(array(
'_builtin' => false,
'show_in_rest' => true
), 'objects');
foreach ($types as $type) {
$RestPreparePosts = new RestPreparePosts();
/**
* Configure Post Type Preparation
*/
$RestPreparePosts->type = $type;
$RestPreparePosts->taxonomies = $taxonomies;
$RestPreparePosts->timberNamespace = 'WorkingNYC';
add_filter('rest_prepare_' . $type, function($post) use ($RestPreparePosts) {
// /**
// * Add public taxonomy details to the post.
// *
// * @var Array
// */
// $post->data['terms'] = $RestPreparePosts->getTerms($post->data['id']);
/**
* Add public shared Timber context.
*
* @var Array
*/
$post->data['context'] = $RestPreparePosts->getTimberContext($post->data['id']);
return $post;
});
}
});