Plugin Directory

Changeset 1539854


Ignore:
Timestamp:
11/24/2016 12:31:12 PM (9 years ago)
Author:
svrooij
Message:

Committing 1.0.3 to trunk

Location:
rest-api-filter-fields/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • rest-api-filter-fields/trunk/readme.txt

    r1538710 r1539854  
    55Requires at least: 4.3
    66Tested up to: 4.6.1
    7 Stable tag: 1.0.2
     7Stable tag: 1.0.3
    88License: MIT
    99License URI: https://raw.githubusercontent.com/svrooij/rest-api-filter-fields/master/LICENSE
    1010
    11 Filter the properties returned by the wordpress rest api V2
     11Filter the properties returned by the Wordpress rest api V2
    1212
    1313== Description ==
     
    1818If you only want titles and links of some articles it doesn't make sense to return the content or the excerpt.
    1919
    20 This isn't limited to posts, it also works on custom posttypes, pages, terms, taxonomies and comments.
     20This isn't limited to posts, it also works on custom posttypes, categories, pages, terms, taxonomies and comments.
    2121
    2222Instead of returning:
     
    5151    }
    5252
    53 It can return (with ``fields=id,title,link`` as GET parameter)
     53It can return (with ``fields=id,title.rendered,link`` as GET parameter)
    5454
    5555    {
     
    65651. If you specify fields so it wouldn't return data the default response is send back to the client.
    66662. (for developers) something wrong with this plugin? [Github](https://github.com/svrooij/rest-api-filter-fields/)
    67 
     673. If you like the plugin (buy me a beer)[https://svrooij.nl/buy-me-a-beer/]
    6868
    6969== Installation ==
     
    8484When 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/)
    8585
     86= How about nested propterties? =
     87
     88You 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
    8690= Does this also work for my custom posttype? =
    8791
     
    97101== Changelog ==
    98102
     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
    99106= 1.0.2 =
    100107* 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  
    1111 * Plugin URI:          https://github.com/svrooij/rest-api-filter-fields
    1212 * Description:         Enables you to filter the fields returned by the api.
    13  * Version:             1.0.2
     13 * Version:             1.0.3
    1414 * Author:              Stephan van Rooij
    1515 * Author URI:          https://svrooij.nl
     
    6868    $filtered_data = array();
    6969
     70    // The original data is in $data object in the property data
     71    $data = $data->data;
     72
    7073    // Explode the $fields parameter to an array.
    71     $filter = explode(',',$fields);
     74    $filters = explode(',',$fields);
    7275
    7376    // If the filter is empty return the original.
    74     if(empty($filter) || count($filter) == 0)
     77    if(empty($filters) || count($filters) == 0)
    7578      return $data;
    7679
     80    $singleFilters = array_filter($filters,'singleValueFilterArray');
     81    //$filtered_data['singleFilters'] = $singleFilters;
    7782
    78     // The original data is in $data object in the property data
     83
    7984    // Foreach property inside the data, check if the key is in the filter.
    80     foreach ($data->data as $key => $value) {
    81       // If the key is in the $filter array, add it to the $filtered_data
    82       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)) {
    8388        $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);
    8499      }
    85100    }
     
    92107}
    93108
     109// Function to filter the fields array
     110function singleValueFilterArray($var){
     111  return (strpos($var,'.') ===false);
     112}
     113
     114// Function to filter the fields array
     115function 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
     120function 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
     154function 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
    94198 ?>
Note: See TracChangeset for help on using the changeset viewer.