Changeset 1571403
- Timestamp:
- 01/09/2017 08:06:04 PM (9 years ago)
- Location:
- rest-api-filter-fields/trunk
- Files:
-
- 2 added
- 2 edited
-
includes (added)
-
includes/class-rest-api-filter-fields.php (added)
-
readme.txt (modified) (3 diffs)
-
rest-api-filter-fields.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
rest-api-filter-fields/trunk/readme.txt
r1567961 r1571403 3 3 Donate link: https://svrooij.nl/buy-me-a-beer 4 4 Tags: json, rest, api, rest-api 5 Requires at least: 4. 35 Requires at least: 4.4 6 6 Tested up to: 4.7 7 Stable tag: 1.0. 47 Stable tag: 1.0.5 8 8 License: MIT 9 9 License URI: https://raw.githubusercontent.com/svrooij/rest-api-filter-fields/master/LICENSE … … 71 71 Installing this plugin is really easy. 72 72 73 1. Upload `rest-api-filter-fields.php` to the `/wp-content/plugins/` directory 74 2. Activate the plugin through the 'Plugins' menu in WordPress 75 76 You can also download it through the build-in plugin manager. 77 Then it will be installed in `/wp-content/plugins/rest-api-filter-fields/`. 73 Just search the plugin directory for `rest api filter fields` and press install. 74 Or download it right from [Github](https://github.com/svrooij/rest-api-filter-fields/releases) and copy the `rest-api-filter-fields` directory in the archive to `wp-content/plugins/`. 78 75 79 76 == Frequently Asked Questions == … … 101 98 == Changelog == 102 99 100 = 1.0.5 = 101 * Support for embedded fields (when you include the '_embed' GET parameter!). 102 * The `_links` field doesn't get stripped anymore. 103 * Taking the first element of an collection with `first`, like `_embedded.author.first.name`. 104 * Moved all the logic to a separate class, so it won't intervene the Wordpress core. 105 103 106 = 1.0.4 = 104 107 * Updated readme. -
rest-api-filter-fields/trunk/rest-api-filter-fields.php
r1567961 r1571403 11 11 * Plugin URI: https://github.com/svrooij/rest-api-filter-fields 12 12 * Description: Enables you to filter the fields returned by the api. 13 * Version: 1.0. 413 * Version: 1.0.5 14 14 * Author: Stephan van Rooij 15 15 * Author URI: https://svrooij.nl … … 18 18 */ 19 19 20 add_action('rest_api_init','rest_api_filter_fields_init',20); 21 /** 22 * Register the fields functionality for all posts. 23 * Because of the 12 you can also use the filter functionality for custom posts 24 */ 25 function rest_api_filter_fields_init(){ 26 27 // Get all public post types, default includes 'post','page','attachment' and custom types added before 'init', 20 28 $post_types = get_post_types(array('public' => true), 'objects'); 29 30 foreach ($post_types as $post_type) { 31 32 //Test if this posttype should be shown in the rest api. 33 $show_in_rest = ( isset( $post_type->show_in_rest ) && $post_type->show_in_rest ) ? true : false; 34 if($show_in_rest) { 35 36 // We need the postname to enable the filter. 37 $post_type_name = $post_type->name; 38 39 //die($post_type_name); 40 41 // Add de filter. The api uses eg. 'rest_prepare_post' with 3 parameters. 42 add_filter('rest_prepare_'.$post_type_name,'rest_api_filter_fields_magic',20,3); 43 } 44 45 } 46 47 // Also enable filtering 'categories', 'comments', 'taxonomies' and 'terms' 48 add_filter('rest_prepare_comment','rest_api_filter_fields_magic',20,3); 49 add_filter('rest_prepare_taxonomy','rest_api_filter_fields_magic',20,3); 50 add_filter('rest_prepare_term','rest_api_filter_fields_magic',20,3); 51 add_filter('rest_prepare_category','rest_api_filter_fields_magic',20,3); 20 // Only include the file if we actually have the WP_REST_Controller class. 21 if(class_exists( 'WP_REST_Controller' )){ 22 require_once('includes/class-rest-api-filter-fields.php'); 52 23 } 53 54 55 /**56 * This is where the magic happends.57 *58 * @return object (Either the original or the object with the fields filtered)59 */60 function rest_api_filter_fields_magic( $data, $post, $request ){61 // Get the parameter from the WP_REST_Request62 // This supports headers, GET/POST variables.63 // and returns 'null' when not exists64 $fields = $request->get_param('fields');65 if($fields){66 67 // Create a new array68 $filtered_data = array();69 70 // The original data is in $data object in the property data71 $data = $data->data;72 73 // Explode the $fields parameter to an array.74 $filters = explode(',',$fields);75 76 // If the filter is empty return the original.77 if(empty($filters) || count($filters) == 0)78 return $data;79 80 $singleFilters = array_filter($filters,'singleValueFilterArray');81 //$filtered_data['singleFilters'] = $singleFilters;82 83 84 // Foreach property inside the data, check if the key is in the filter.85 foreach ($data as $key => $value) {86 // If the key is in the $filters array, add it to the $filtered_data87 if (in_array($key, $singleFilters)) {88 $filtered_data[$key] = $value;89 }90 }91 92 $childFilters = array_filter($filters,'childValueFilterArray');93 //$filtered_data['childFilters'] = $childFilters;94 95 foreach ($childFilters as $childFilter) {96 $val = array_path_value($data,$childFilter);97 if($val != null){98 set_array_path_value($filtered_data,$childFilter,$val);99 }100 }101 102 }103 104 // return the filtered_data if it is set and got fields.105 return (isset($filtered_data) && count($filtered_data) > 0) ? $filtered_data : $data;106 107 }108 109 // Function to filter the fields array110 function singleValueFilterArray($var){111 return (strpos($var,'.') ===false);112 }113 114 // Function to filter the fields array115 function childValueFilterArray($var){116 return (strpos($var,'.') !=false);117 }118 119 // found on http://codeaid.net/php/get-values-of-multi-dimensional-arrays-using-xpath-notation120 function array_path_value(array $array, $path, $default = null)121 {122 // specify the delimiter123 $delimiter = '.';124 125 // fail if the path is empty126 if (empty($path)) {127 throw new Exception('Path cannot be empty');128 }129 130 // remove all leading and trailing slashes131 $path = trim($path, $delimiter);132 133 // use current array as the initial value134 $value = $array;135 136 // extract parts of the path137 $parts = explode($delimiter, $path);138 139 // loop through each part and extract its value140 foreach ($parts as $part) {141 if (isset($value[$part])) {142 // replace current value with the child143 $value = $value[$part];144 } else {145 // key doesn't exist, fail146 return $default;147 }148 }149 150 return $value;151 }152 153 // Function found on http://codeaid.net/php/set-value-of-an-array-using-xpath-notation154 function set_array_path_value(array &$array, $path, $value)155 {156 // fail if the path is empty157 if (empty($path)) {158 throw new Exception('Path cannot be empty');159 }160 161 // fail if path is not a string162 if (!is_string($path)) {163 throw new Exception('Path must be a string');164 }165 166 // specify the delimiter167 $delimiter = '.';168 169 // remove all leading and trailing slashes170 $path = trim($path, $delimiter);171 172 // split the path in into separate parts173 $parts = explode($delimiter, $path);174 175 // initially point to the root of the array176 $pointer =& $array;177 178 // loop through each part and ensure that the cell is there179 foreach ($parts as $part) {180 // fail if the part is empty181 if (empty($part)) {182 throw new Exception('Invalid path specified: ' . $path);183 }184 185 // create the cell if it doesn't exist186 if (!isset($pointer[$part])) {187 $pointer[$part] = array();188 }189 190 // redirect the pointer to the new cell191 $pointer =& $pointer[$part];192 }193 194 // set value of the target cell195 $pointer = $value;196 }197 198 ?>
Note: See TracChangeset
for help on using the changeset viewer.