Changeset 1539854
- Timestamp:
- 11/24/2016 12:31:12 PM (9 years ago)
- Location:
- rest-api-filter-fields/trunk
- Files:
-
- 2 edited
-
readme.txt (modified) (6 diffs)
-
rest-api-filter-fields.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
rest-api-filter-fields/trunk/readme.txt
r1538710 r1539854 5 5 Requires at least: 4.3 6 6 Tested up to: 4.6.1 7 Stable tag: 1.0. 27 Stable tag: 1.0.3 8 8 License: MIT 9 9 License URI: https://raw.githubusercontent.com/svrooij/rest-api-filter-fields/master/LICENSE 10 10 11 Filter the properties returned by the wordpress rest api V211 Filter the properties returned by the Wordpress rest api V2 12 12 13 13 == Description == … … 18 18 If you only want titles and links of some articles it doesn't make sense to return the content or the excerpt. 19 19 20 This isn't limited to posts, it also works on custom posttypes, pages, terms, taxonomies and comments.20 This isn't limited to posts, it also works on custom posttypes, categories, pages, terms, taxonomies and comments. 21 21 22 22 Instead of returning: … … 51 51 } 52 52 53 It can return (with ``fields=id,title ,link`` as GET parameter)53 It can return (with ``fields=id,title.rendered,link`` as GET parameter) 54 54 55 55 { … … 65 65 1. If you specify fields so it wouldn't return data the default response is send back to the client. 66 66 2. (for developers) something wrong with this plugin? [Github](https://github.com/svrooij/rest-api-filter-fields/) 67 67 3. If you like the plugin (buy me a beer)[https://svrooij.nl/buy-me-a-beer/] 68 68 69 69 == Installation == … … 84 84 When you want to add [featured_images](https://github.com/svrooij/rest-api-filter-fields/issues/5), I recommend using [better-rest-api-featured-images](https://wordpress.org/plugins/better-rest-api-featured-images/) 85 85 86 = How about nested propterties? = 87 88 You can filter on nested properties with a '.' like 'title.rendered'. Not sure if this also works on arrays. (Existing issues)[https://github.com/svrooij/rest-api-filter-fields/issues] or (Submit issue)[https://github.com/svrooij/rest-api-filter-fields/issues/new] 89 86 90 = Does this also work for my custom posttype? = 87 91 … … 97 101 == Changelog == 98 102 103 = 1.0.3 = 104 * Filter on nested fields [Issue #1](https://github.com/svrooij/rest-api-filter-fields/issues/1) implemented. Please test and leave a response [here](https://github.com/svrooij/rest-api-filter-fields/issues/1). 105 99 106 = 1.0.2 = 100 107 * Added filtering for categories [Issue #4](https://github.com/svrooij/rest-api-filter-fields/issues/4) -
rest-api-filter-fields/trunk/rest-api-filter-fields.php
r1538710 r1539854 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. 213 * Version: 1.0.3 14 14 * Author: Stephan van Rooij 15 15 * Author URI: https://svrooij.nl … … 68 68 $filtered_data = array(); 69 69 70 // The original data is in $data object in the property data 71 $data = $data->data; 72 70 73 // Explode the $fields parameter to an array. 71 $filter = explode(',',$fields);74 $filters = explode(',',$fields); 72 75 73 76 // If the filter is empty return the original. 74 if(empty($filter ) || count($filter) == 0)77 if(empty($filters) || count($filters) == 0) 75 78 return $data; 76 79 80 $singleFilters = array_filter($filters,'singleValueFilterArray'); 81 //$filtered_data['singleFilters'] = $singleFilters; 77 82 78 // The original data is in $data object in the property data 83 79 84 // Foreach property inside the data, check if the key is in the filter. 80 foreach ($data ->dataas $key => $value) {81 // If the key is in the $filter array, add it to the $filtered_data82 if (in_array($key, $ filter)) {85 foreach ($data as $key => $value) { 86 // If the key is in the $filters array, add it to the $filtered_data 87 if (in_array($key, $singleFilters)) { 83 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); 84 99 } 85 100 } … … 92 107 } 93 108 109 // Function to filter the fields array 110 function singleValueFilterArray($var){ 111 return (strpos($var,'.') ===false); 112 } 113 114 // Function to filter the fields array 115 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-notation 120 function array_path_value(array $array, $path, $default = null) 121 { 122 // specify the delimiter 123 $delimiter = '.'; 124 125 // fail if the path is empty 126 if (empty($path)) { 127 throw new Exception('Path cannot be empty'); 128 } 129 130 // remove all leading and trailing slashes 131 $path = trim($path, $delimiter); 132 133 // use current array as the initial value 134 $value = $array; 135 136 // extract parts of the path 137 $parts = explode($delimiter, $path); 138 139 // loop through each part and extract its value 140 foreach ($parts as $part) { 141 if (isset($value[$part])) { 142 // replace current value with the child 143 $value = $value[$part]; 144 } else { 145 // key doesn't exist, fail 146 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-notation 154 function set_array_path_value(array &$array, $path, $value) 155 { 156 // fail if the path is empty 157 if (empty($path)) { 158 throw new Exception('Path cannot be empty'); 159 } 160 161 // fail if path is not a string 162 if (!is_string($path)) { 163 throw new Exception('Path must be a string'); 164 } 165 166 // specify the delimiter 167 $delimiter = '.'; 168 169 // remove all leading and trailing slashes 170 $path = trim($path, $delimiter); 171 172 // split the path in into separate parts 173 $parts = explode($delimiter, $path); 174 175 // initially point to the root of the array 176 $pointer =& $array; 177 178 // loop through each part and ensure that the cell is there 179 foreach ($parts as $part) { 180 // fail if the part is empty 181 if (empty($part)) { 182 throw new Exception('Invalid path specified: ' . $path); 183 } 184 185 // create the cell if it doesn't exist 186 if (!isset($pointer[$part])) { 187 $pointer[$part] = array(); 188 } 189 190 // redirect the pointer to the new cell 191 $pointer =& $pointer[$part]; 192 } 193 194 // set value of the target cell 195 $pointer = $value; 196 } 197 94 198 ?>
Note: See TracChangeset
for help on using the changeset viewer.