forked from svrooij/rest-api-filter-fields
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest-api-filter-fields.php
More file actions
93 lines (73 loc) · 3 KB
/
rest-api-filter-fields.php
File metadata and controls
93 lines (73 loc) · 3 KB
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
<?php
/**
* WP REST API - filter fields
*
* @package REST_Api_Filter_Fields
* @author Stephan van Rooij <github@svrooij.nl>
* @license MIT
*
* @wordpress-plugin
* Plugin Name: WP REST API - filter fields
* Plugin URI: https://github.com/svrooij/rest-api-filter-fields
* Description: Enables you to filter the fields returned by the api.
* Version: 1.0.1
* Author: Stephan van Rooij
* Author URI: https://svrooij.nl
* License: MIT
* License URI: https://raw.githubusercontent.com/svrooij/rest-api-filter-fields/master/LICENSE
*/
add_action('rest_api_init','rest_api_filter_fields_init',20);
/**
* Register the fields functionality for all posts.
* Because of the 12 you can also use the filter functionality for custom posts
*/
function rest_api_filter_fields_init(){
// Get all public post types, default includes 'post','page','attachment' and custom types added before 'init', 20
$post_types = get_post_types(array('public' => true), 'objects');
foreach ($post_types as $post_type) {
//Test if this posttype should be shown in the rest api.
$show_in_rest = ( isset( $post_type->show_in_rest ) && $post_type->show_in_rest ) ? true : false;
if($show_in_rest) {
// We need the postname to enable the filter.
$post_type_name = $post_type->name;
//die($post_type_name);
// Add de filter. The api uses eg. 'rest_prepare_post' with 3 parameters.
add_filter('rest_prepare_'.$post_type_name,'rest_api_filter_fields_magic',20,3);
}
}
// Also enable filtering 'comments', 'taxonomies' and 'terms'
add_filter('rest_prepare_comment','rest_api_filter_fields_magic',20,3);
add_filter('rest_prepare_taxonomy','rest_api_filter_fields_magic',20,3);
add_filter('rest_prepare_term','rest_api_filter_fields_magic',20,3);
}
/**
* This is where the magic happends.
*
* @return object (Either the original or the object with the fields filtered)
*/
function rest_api_filter_fields_magic( $data, $post, $request ){
// Get the parameter from the WP_REST_Request
// This supports headers, GET/POST variables.
// and returns 'null' when not exists
$fields = $request->get_param('fields');
if($fields){
// Create a new array
$filtered_data = array();
// Explode the $fields parameter to an array.
$filter = explode(',',$fields);
// If the filter is empty return the original.
if(empty($filter) || count($filter) == 0)
return $data;
// The original data is in $data object in the property data
// Foreach property inside the data, check if the key is in the filter.
foreach ($data->data as $key => $value) {
// If the key is in the $filter array, add it to the $filtered_data
if (in_array($key, $filter)) {
$filtered_data[$key] = $value;
}
}
}
// return the filtered_data if it is set and got fields.
return (isset($filtered_data) && count($filtered_data) > 0) ? $filtered_data : $data;
}
?>