Plugin Directory

Changeset 1571403


Ignore:
Timestamp:
01/09/2017 08:06:04 PM (9 years ago)
Author:
svrooij
Message:

Committing 1.0.5 to trunk

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

Legend:

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

    r1567961 r1571403  
    33Donate link: https://svrooij.nl/buy-me-a-beer
    44Tags: json, rest, api, rest-api
    5 Requires at least: 4.3
     5Requires at least: 4.4
    66Tested up to: 4.7
    7 Stable tag: 1.0.4
     7Stable tag: 1.0.5
    88License: MIT
    99License URI: https://raw.githubusercontent.com/svrooij/rest-api-filter-fields/master/LICENSE
     
    7171Installing this plugin is really easy.
    7272
    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/`.
     73Just search the plugin directory for `rest api filter fields` and press install.
     74Or 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/`.
    7875
    7976== Frequently Asked Questions ==
     
    10198== Changelog ==
    10299
     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
    103106= 1.0.4 =
    104107* Updated readme.
  • rest-api-filter-fields/trunk/rest-api-filter-fields.php

    r1567961 r1571403  
    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.4
     13 * Version:             1.0.5
    1414 * Author:              Stephan van Rooij
    1515 * Author URI:          https://svrooij.nl
     
    1818 */
    1919
    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.
     21if(class_exists( 'WP_REST_Controller' )){
     22  require_once('includes/class-rest-api-filter-fields.php');
    5223}
    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_Request
    62   // This supports headers, GET/POST variables.
    63   // and returns 'null' when not exists
    64   $fields = $request->get_param('fields');
    65   if($fields){
    66 
    67     // Create a new array
    68     $filtered_data = array();
    69 
    70     // The original data is in $data object in the property data
    71     $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_data
    87       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 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 
    198  ?>
Note: See TracChangeset for help on using the changeset viewer.