Skip to content

Commit

Permalink
[+] Query refactored and PostTypes and Terms fields made async
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Fiadone committed Apr 2, 2020
1 parent e952db1 commit 0980319
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Aeria/Aeria.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/
class Aeria extends Container
{
const VERSION = '3.2.0';
const VERSION = '3.2.1';

/**
* Constructs the Aeria container.
Expand Down
14 changes: 3 additions & 11 deletions Aeria/Field/Fields/PostTypesField.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,10 @@ class PostTypesField extends SelectField
public static function transformConfig(array $config)
{
$config['type'] = 'select';
$filter = (isset($config['filter'])) ? $config['filter'] : [];
$config['ajax'] = array_merge($filter, ['endpoint' => '/wp-json/aeria/post-types']);

$post_types = get_post_types(
['public' => true],
'objects'
);

$config['options'] = array_map(function ($post_type) {
return [
'value' => $post_type->name,
'label' => $post_type->label,
];
}, array_values($post_types));
unset($config['filter']);

return parent::transformConfig($config);
}
Expand Down
12 changes: 2 additions & 10 deletions Aeria/Field/Fields/SelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,8 @@ class SelectField extends BaseField
*/
public static function transformConfig(array $config)
{
if ((isset($config['exclude']) || isset($config['include'])) && isset($config['options'])) {
$options = array_filter(
$config['options'],
function ($option) use ($config) {
return isset($config['exclude'])
? !in_array($option['value'], $config['exclude'])
: in_array($option['value'], $config['include']);
}
);
$config['options'] = array_values($options);
if (isset($config['options'])) {
$config['options'] = aeria_objects_filter($config['options'], $config);
}

return parent::transformConfig($config);
Expand Down
26 changes: 3 additions & 23 deletions Aeria/Field/Fields/TermsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,10 @@ class TermsField extends SelectField
public static function transformConfig(array $config)
{
$config['type'] = 'select';
$filter = (isset($config['filter'])) ? $config['filter'] : [];
$config['ajax'] = array_merge($filter, ['endpoint' => '/wp-json/aeria/terms']);

$taxonomy = (isset($config['taxonomy'])) ? $config['taxonomy'] : 'category';
$hide_empty = (isset($config['hide_empty'])) ? $config['hide_empty'] : true;

$terms = get_terms(array(
'taxonomy' => $taxonomy,
'hide_empty' => $hide_empty,
));

$config['options'] = [];

if (!empty($terms) && !is_wp_error($terms)) {
$config['options'] = array_map(
function ($term) {
return array(
'label' => $term->name,
'value' => $term->term_id,
);
},
array_values($terms)
);
}

unset($config['taxonomy']);
unset($config['filter']);

return parent::transformConfig($config);
}
Expand Down
100 changes: 72 additions & 28 deletions Aeria/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,32 @@ public function deleteOption($key)
*/
public function getPostTypes($parameters)
{
$searchField = (isset($parameters['s'])) ? $parameters['s'] : null;
$public = (isset($parameters['public'])) ? $parameters['public'] : false;
$searchField = (isset($parameters['s'])) ? $parameters['s'] : '';
$public = (isset($parameters['public'])) ? $parameters['public'] : true;
$sender = (isset($parameters['sender'])) ? $parameters['sender'] : null;

$args = [
'public' => $public,
'query_var' => $searchField,
];
$types = get_post_types($args, 'object');
$response = [];
foreach ($types as $index => $post_type) {
$response[$index]['label'] = $post_type->labels->name;
$response[$index]['value'] = $post_type->name;
$types = array_reduce(get_post_types(['public' => $public], 'object'), function ($carry, $type) {
if ((empty($searchField) || preg_match('/'.$searchField.'/', $type->name))) {
$carry[] = json_decode(json_encode($type), true);
}

return $carry;
}, []);

$types = aeria_objects_filter($types, $parameters, 'name');

switch ($sender) {
case 'SelectOptions':
return array_map(function ($type) {
return array(
'value' => $type['name'],
'label' => $type['labels']['name'],
);
}, $types);
break;
default:
return $types;
break;
}

return $response;
Expand All @@ -88,16 +101,35 @@ public function getTaxonomies($parameters)
$searchField = (isset($parameters['s'])) ? $parameters['s'] : '';
$sender = (isset($parameters['sender'])) ? $parameters['sender'] : null;
$post_type = (isset($parameters['post_type'])) ? $parameters['post_type'] : '';
$taxonomies = get_taxonomies([], 'objects');
$response = [];
foreach ($taxonomies as $index => $taxonomy) {
$taxonomies = [];

foreach (get_taxonomies([], 'objects') as $index => $taxonomy) {
if ((empty($searchField) || preg_match('/'.$searchField.'/', $taxonomy->name)) && (!empty($post_type) && in_array($post_type, $taxonomy->object_type))) {
$response[$index]['label'] = $taxonomy->labels->name;
$response[$index]['value'] = $taxonomy->name;
$taxonomies[] = json_decode(json_encode($taxonomy), true);
}
}

return $response;
$taxonomies = aeria_objects_filter($taxonomies, $parameters, 'name');

switch ($sender) {
case 'SelectOptions':
$taxonomies = array_map(function ($taxonomy) {
return [
'value' => $taxonomy['name'],
'label' => $taxonomy['labels']['name'],
];
}, $taxonomies);
break;
case 'Names':
$taxonomies = array_map(function ($taxonomy) {
return $taxonomy['name'];
}, $taxonomies);
break;
default:
break;
}

return $taxonomies;
}

/**
Expand All @@ -115,7 +147,7 @@ public function getTerms($parameters)
$searchField = (isset($parameters['s'])) ? $parameters['s'] : '';
$sender = (isset($parameters['sender'])) ? $parameters['sender'] : null;
$post_type = (isset($parameters['post_type'])) ? $parameters['post_type'] : 'post';
$default_taxonomies = array_keys($this->getTaxonomies(['post_type' => $post_type]));
$default_taxonomies = $this->getTaxonomies(['post_type' => $post_type, 'sender' => 'Names']);

if (!isset($parameters['taxonomy']) && empty($default_taxonomies)) {
return [];
Expand All @@ -124,25 +156,37 @@ public function getTerms($parameters)
$taxonomies = (isset($parameters['taxonomy'])) ? $parameters['taxonomy'] : $default_taxonomies;
$hide_empty = (isset($parameters['hide_empty'])) ? filter_var($parameters['hide_empty'], FILTER_VALIDATE_BOOLEAN) : true;

$terms = get_terms(array(
$terms = get_terms([
'search' => $searchField,
'taxonomy' => $taxonomies,
'hide_empty' => $hide_empty,
));
]);

if (is_wp_error($terms)) {
return [];
}

$terms = array_map(function ($term) {
return json_decode(json_encode($term), true);
}, $terms);

$terms = aeria_objects_filter($terms, $parameters, 'name');
$terms = aeria_objects_filter($terms, $parameters, 'slug');

switch ($sender) {
case 'SelectOptions':
return array_map(function ($term) {
return array(
'value' => $term->term_id,
'label' => $term->name,
);
}, array_values($terms));
$terms = array_map(function ($term) {
return [
'value' => $term['term_id'],
'label' => $term['name'],
];
}, $terms);
break;
default:
return $terms;
break;
}
}

return $terms;
}

/**
Expand Down
59 changes: 58 additions & 1 deletion Aeria/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function ($value) {
*/
function dd(...$args)
{
dump(...$args);
var_dump(...$args);
die();
}
}
Expand Down Expand Up @@ -286,3 +286,60 @@ function array_flat(array $to_be_normalized, $times = -1)
return $pivot;
}
}

if (!function_exists('aeria_array_filter')) {
/**
* Filters a data array.
*
* @param array $data the data to be filtered
* @param array $config the filter configuration
*
* @return array the filtered data
*
* @since Method available since Release 3.2.1
*/
function aeria_array_filter($data = [], $config = [])
{
if (empty($data) || (!isset($config['exclude']) && !isset($config['include']))) {
return $data;
}

return array_filter(
$data,
function ($value) use ($config) {
return isset($config['exclude'])
? !in_array($value, $config['exclude'])
: in_array($value, $config['include']);
}
);
}
}

if (!function_exists('aeria_object_filter')) {
/**
* Filters a select options array.
*
* @param array $objects the select options
* @param array $config the filter configuration
* @param string $key the filter configuration
*
* @return array the filtered select options
*
* @since Method available since Release 3.2.1
*/
function aeria_objects_filter($objects = [], $config = [], $key = 'value')
{
if (empty($objects) || (!isset($config['exclude']) && !isset($config['include']))) {
return $objects;
}

return array_values(array_filter(
$objects,
function ($object) use ($config, $key) {
return isset($config['exclude'])
? !in_array($object[$key], $config['exclude'])
: in_array($object[$key], $config['include']);
}
));
}
}
2 changes: 1 addition & 1 deletion aeria.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Plugin Name: Aeria
* Plugin URI: https://github.com/caffeinalab/aeria
* Description: Aeria is a modular, lightweight, fast WordPress Application development kit.
* Version: 3.2.0
* Version: 3.2.1
* Author: Caffeina
* Author URI: https://caffeina.com
* Text Domain: aeria
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aeria",
"version": "3.2.0",
"version": "3.2.1",
"description": "Aeria",
"scripts": {
"dev": "webpack --watch --mode development",
Expand Down

0 comments on commit 0980319

Please sign in to comment.