From 09803197245bdd3907ae4efbef2d71d66b5fe686 Mon Sep 17 00:00:00 2001 From: Andrea Fiadone Date: Thu, 2 Apr 2020 20:49:20 +0200 Subject: [PATCH] [+] Query refactored and PostTypes and Terms fields made async --- Aeria/Aeria.php | 2 +- Aeria/Field/Fields/PostTypesField.php | 14 +--- Aeria/Field/Fields/SelectField.php | 12 +--- Aeria/Field/Fields/TermsField.php | 26 +------ Aeria/Query/Query.php | 100 ++++++++++++++++++-------- Aeria/helpers.php | 59 ++++++++++++++- aeria.php | 2 +- package.json | 2 +- 8 files changed, 141 insertions(+), 76 deletions(-) diff --git a/Aeria/Aeria.php b/Aeria/Aeria.php index 532e91b..a7d25ce 100755 --- a/Aeria/Aeria.php +++ b/Aeria/Aeria.php @@ -31,7 +31,7 @@ */ class Aeria extends Container { - const VERSION = '3.2.0'; + const VERSION = '3.2.1'; /** * Constructs the Aeria container. diff --git a/Aeria/Field/Fields/PostTypesField.php b/Aeria/Field/Fields/PostTypesField.php index 91996dd..8bf4553 100644 --- a/Aeria/Field/Fields/PostTypesField.php +++ b/Aeria/Field/Fields/PostTypesField.php @@ -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); } diff --git a/Aeria/Field/Fields/SelectField.php b/Aeria/Field/Fields/SelectField.php index 5dcae1f..5c732e1 100644 --- a/Aeria/Field/Fields/SelectField.php +++ b/Aeria/Field/Fields/SelectField.php @@ -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); diff --git a/Aeria/Field/Fields/TermsField.php b/Aeria/Field/Fields/TermsField.php index a5059d7..0476d48 100644 --- a/Aeria/Field/Fields/TermsField.php +++ b/Aeria/Field/Fields/TermsField.php @@ -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); } diff --git a/Aeria/Query/Query.php b/Aeria/Query/Query.php index fc05412..244e88f 100644 --- a/Aeria/Query/Query.php +++ b/Aeria/Query/Query.php @@ -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; @@ -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; } /** @@ -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 []; @@ -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; } /** diff --git a/Aeria/helpers.php b/Aeria/helpers.php index 9c053ec..269ed29 100755 --- a/Aeria/helpers.php +++ b/Aeria/helpers.php @@ -58,7 +58,7 @@ function ($value) { */ function dd(...$args) { - dump(...$args); + var_dump(...$args); die(); } } @@ -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']); + } + )); + } +} diff --git a/aeria.php b/aeria.php index 2ab7eec..9e0e977 100755 --- a/aeria.php +++ b/aeria.php @@ -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 diff --git a/package.json b/package.json index 9db9c3e..ab01913 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "aeria", - "version": "3.2.0", + "version": "3.2.1", "description": "Aeria", "scripts": { "dev": "webpack --watch --mode development",