Skip to content
This repository has been archived by the owner on Jan 26, 2021. It is now read-only.

Commit

Permalink
feat(api-fields): change whitelist for blacklist
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Sep 24, 2018
1 parent 867d759 commit a1d0811
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions admin/wp-pwa-admin-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@
<div class="message-header">
<nav class="level">
<div class="level-left">
<strong>Whitelist WP API fields</strong>
<strong>Filter WP API fields</strong>
</div>
<div class="level-right">
<a href="#" class="close-api-fields" style="color:inherit"><i class="fa fa-times-circle" aria-hidden="true"></i></a>
Expand All @@ -436,7 +436,7 @@
?>
<textarea id="api-fields" class="textarea"><?php echo $api_fields_output; ?></textarea>
</p>
<p><em>* Delete all fields to deactivate the whitelisting.</em></p>
<p><em>* Use dot notation for nested fields (i.e: title.rendered).</em></p>
<br>
<p>
<a href="#" id="save-api-fields"class="button button-lg">Save</a>
Expand Down
20 changes: 16 additions & 4 deletions libs/class-rest-api-filter-fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ public function filter_magic( $response, $post, $request ){
$filters = isset($settings['wp_pwa_api_fields']) ? $settings['wp_pwa_api_fields'] : array();

if(sizeof($filters) !== 0){

$filters[] = '_embedded';
// Create a new array
$filtered_data = array();

Expand Down Expand Up @@ -126,7 +124,7 @@ public function filter_magic( $response, $post, $request ){
// Foreach property inside the data, check if the key is in the filter.
foreach ($data as $key => $value) {
// If the key is in the $filters array, add it to the $filtered_data
if (in_array($key, $singleFilters)) {
if (!in_array($key, $singleFilters)) {
$filtered_data[$key] = $value;
}
}
Expand All @@ -137,7 +135,7 @@ public function filter_magic( $response, $post, $request ){
foreach ($childFilters as $childFilter) {
$val = $this->array_path_value($data,$childFilter);
if($val != null){
$this->set_array_path_value($filtered_data,$childFilter,$val);
$this->unset_array_path_value($filtered_data,$childFilter,$val);
}
}

Expand Down Expand Up @@ -165,6 +163,20 @@ function childValueFilterArray($var){
return (strpos($var,'.') !=false);
}

function unset_array_path_value(&$array, $path) {
$path = explode('.', $path);
$temp = & $array;

foreach($path as $key) {
if(isset($temp[$key])){
if(!($key == end($path))) $temp = &$temp[$key];
} else {
return false; //invalid path
}
}
unset($temp[end($path)]);
}

// found on http://codeaid.net/php/get-values-of-multi-dimensional-arrays-using-xpath-notation
function array_path_value(array $array, $path, $default = null)
{
Expand Down

0 comments on commit a1d0811

Please sign in to comment.