From 3d97101093945078b3525467560669f02d080ccf Mon Sep 17 00:00:00 2001 From: Alberto Parziale Date: Mon, 14 Oct 2019 09:23:03 +0000 Subject: [PATCH] Update option page generation & add a new api route /terms - Version bump to 3.0.6 --- Aeria/Aeria.php | 2 +- .../KernelServiceProvider.php | 4 +- Aeria/Kernel/Tasks/CreateAdminScripts.php | 42 ++++++++++++++++ Aeria/Kernel/Tasks/CreateOptions.php | 6 +-- Aeria/Kernel/Tasks/CreateRouter.php | 11 +++- Aeria/OptionsPage/OptionsPage.php | 42 ++++++++-------- Aeria/Query/Query.php | 50 ++++++++++++++++--- aeria.php | 2 +- 8 files changed, 125 insertions(+), 34 deletions(-) create mode 100644 Aeria/Kernel/Tasks/CreateAdminScripts.php diff --git a/Aeria/Aeria.php b/Aeria/Aeria.php index 1190972..67152ef 100755 --- a/Aeria/Aeria.php +++ b/Aeria/Aeria.php @@ -33,7 +33,7 @@ */ class Aeria extends Container { - public const VERSION = '3.0.5'; + public const VERSION = '3.0.6'; /** * Constructs the Aeria container * diff --git a/Aeria/Kernel/ServiceProviders/KernelServiceProvider.php b/Aeria/Kernel/ServiceProviders/KernelServiceProvider.php index e972d98..cc5fe1a 100755 --- a/Aeria/Kernel/ServiceProviders/KernelServiceProvider.php +++ b/Aeria/Kernel/ServiceProviders/KernelServiceProvider.php @@ -10,6 +10,7 @@ use Aeria\Taxonomy\Taxonomy; use Aeria\Kernel\Loader; use Aeria\Kernel\Tasks\{ + CreateAdminScripts, CreateControllers, CreateField, CreateMeta, @@ -23,7 +24,7 @@ /** * KernelServiceProvider is in charge of registering the Kernel to the container - * + * * @category Kernel * @package Aeria * @author Jacopo Martinelli @@ -63,6 +64,7 @@ public function boot(Container $container): bool $kernel = $container->make('kernel'); $config = $container->make('config'); Loader::loadConfig($config, $container); + $kernel->register(new CreateAdminScripts()); $kernel->register(new CreateControllers()); $kernel->register(new CreateField()); $kernel->register(new CreateMeta()); diff --git a/Aeria/Kernel/Tasks/CreateAdminScripts.php b/Aeria/Kernel/Tasks/CreateAdminScripts.php new file mode 100644 index 0000000..d22f4a8 --- /dev/null +++ b/Aeria/Kernel/Tasks/CreateAdminScripts.php @@ -0,0 +1,42 @@ + + * @license https://github.com/caffeinalab/aeria/blob/master/LICENSE MIT license + * @link https://github.com/caffeinalab/aeria + */ +class CreateAdminScripts extends Task +{ + public $priority = 6; + public $admin_only = true; + /** + * The main task method. It registers the option pages. + * + * @param array $args the arguments to be passed to the Task + * + * @return void + * + * @access public + * @since Method available since Release 3.0.0 + */ + public function do(array $args) + { + add_action( + 'admin_head', + function () use ($args) { + global $_wp_admin_css_colors; + $admin_colors = $_wp_admin_css_colors; + $aeria_colors = $admin_colors[get_user_option('admin_color')]->colors; + $args['service']['render_engine']->render('color_encoder_template', ['colors' => $aeria_colors]); + } + ); + } +} diff --git a/Aeria/Kernel/Tasks/CreateOptions.php b/Aeria/Kernel/Tasks/CreateOptions.php index c15a8f0..2cf345a 100644 --- a/Aeria/Kernel/Tasks/CreateOptions.php +++ b/Aeria/Kernel/Tasks/CreateOptions.php @@ -6,7 +6,7 @@ /** * This task is in charge of creating options pages. - * + * * @category Kernel * @package Aeria * @author Simone Montali @@ -32,7 +32,7 @@ public function do(array $args) $default_icon_data = file_get_contents(dirname(__DIR__, 2).'/aeria.svg'); $default_icon = 'data:image/svg+xml;base64,'.base64_encode($default_icon_data); if (isset($args['config']['aeria']['options'])) { - $section_config = isset($args['config']['aeria']['section']) ? $args['config']['aeria']['section'] : null; + $section_config = isset($args['config']['aeria']['section']) ? $args['config']['aeria']['section'] : []; foreach ($args['config']['aeria']['options'] as $name => $data) { $config =[]; @@ -65,4 +65,4 @@ function () use ($args) { ); } -} \ No newline at end of file +} diff --git a/Aeria/Kernel/Tasks/CreateRouter.php b/Aeria/Kernel/Tasks/CreateRouter.php index 50c4d4b..14f1677 100644 --- a/Aeria/Kernel/Tasks/CreateRouter.php +++ b/Aeria/Kernel/Tasks/CreateRouter.php @@ -7,7 +7,7 @@ /** * This task is in charge of creating the router and its routes. - * + * * @category Kernel * @package Aeria * @author Simone Montali @@ -55,6 +55,13 @@ public function do(array $args) return $args['service']['query']->getTaxonomies($wp_req->get_params()); } ); + $args['service']['router']->get( + '/terms', function ($request) use ($args) { + $wp_req = $request->wp_request; + + return $args['service']['query']->getTerms($wp_req->get_params()); + } + ); $args['service']['router']->get( "/validate-by-id", function ($request) use ($args) { $wp_req = $request->wp_request; @@ -74,4 +81,4 @@ public function do(array $args) } $args['service']['router']->boot(); } -} \ No newline at end of file +} diff --git a/Aeria/OptionsPage/OptionsPage.php b/Aeria/OptionsPage/OptionsPage.php index 9396935..c6d342d 100644 --- a/Aeria/OptionsPage/OptionsPage.php +++ b/Aeria/OptionsPage/OptionsPage.php @@ -6,8 +6,8 @@ use Aeria\Field\FieldError; use Aeria\RenderEngine\RenderEngine; /** - * OptionsPage is in charge of generating pages in WP's options - * + * OptionsPage is in charge of generating pages in WP's options + * * @category Options * @package Aeria * @author Simone Montali @@ -56,7 +56,7 @@ public function register($option_page) &&isset($option_page["validator_service"]) &&isset($option_page["query_service"]) ) - $this->optionPages[]=$option_page; + $this->option_pages[]=$option_page; return null; } /** @@ -72,10 +72,11 @@ public function register($option_page) */ public function boot($aeria_config, $render_service) { + $editor_enabled = false; $default_icon_data = file_get_contents(dirname(__DIR__).'/aeria.svg'); $default_icon = 'data:image/svg+xml;base64,'.base64_encode($default_icon_data); // Registering aeria's editor page - if (empty($GLOBALS['admin_page_hooks']["aeria_options"])) { + if ($editor_enabled) { add_menu_page( "Aeria editor", "Aeria", @@ -83,24 +84,25 @@ public function boot($aeria_config, $render_service) "aeria_options", '', $default_icon - ); + ); + add_submenu_page( + "aeria_options", + "Editor", + "Editor", + "manage_options", + "aeria_options", + function () use ($aeria_config, $render_service) { + static::renderHTML( + "aeria_editor", + $aeria_config, + $render_service + ); + } + ); } - add_submenu_page( - "aeria_options", - "Editor", - "Editor", - "manage_options", - "aeria_options", - function () use ($aeria_config, $render_service) { - static::renderHTML( - "aeria_editor", - $aeria_config, - $render_service - ); - } - ); + // Registering other option pages - foreach ($this->optionPages as $singleOptionPage) { + foreach ($this->option_pages as $singleOptionPage) { // Check if nav menu parent exists if (empty($GLOBALS['admin_page_hooks'][$singleOptionPage["parent"]])) { add_menu_page( diff --git a/Aeria/Query/Query.php b/Aeria/Query/Query.php index b78a5ed..d814d08 100644 --- a/Aeria/Query/Query.php +++ b/Aeria/Query/Query.php @@ -54,7 +54,7 @@ public function deleteOption($key) /** * Gets the saved post types * - * @param array $parameters the additional query parameters - check + * @param array $parameters the additional query parameters - check * WP codex to know more * * @return array of the post types @@ -83,7 +83,7 @@ public function getPostTypes($parameters) /** * Gets the saved taxonomies * - * @param array $parameters the additional query parameters - check + * @param array $parameters the additional query parameters - check * WP codex to know more * * @return array of the taxonomies @@ -101,15 +101,53 @@ public function getTaxonomies($parameters) foreach ($taxonomies as $index => $taxonomy) { if ((preg_match('/'.$searchField.'/', $taxonomy->name)&&((in_array($postType, $taxonomy->object_type)) || $postType == "") || $searchField=="")) { $response[$index]["label"] = $taxonomy->labels->name; - $response[$index]["value"] = $taxonomy->name; + $response[$index]["value"] = $taxonomy->name; } } return $response; } + + /** + * Gets the saved terms. + * + * @param array $parameters the additional query parameters - check + * WP codex to know more + * + * @return array of terms + * + * @since Method available since Release 3.0.0 + */ + public function getTerms($parameters) + { + $searchField = (isset($parameters['s'])) ? $parameters['s'] : ''; + $sender = (isset($parameters['sender'])) ? $parameters['sender'] : null; + $taxonomy = (isset($parameters['taxonomy'])) ? $parameters['taxonomy'] : 'category'; + $hide_empty = (isset($parameters['hide_empty'])) ? $parameters['hide_empty'] : true; + $terms = get_terms(array( + 'search' => $searchField, + 'taxonomy' => $taxonomy, + 'hide_empty' => $hide_empty, + )); + + switch ($sender) { + case 'SelectOptions': + return array_map(function ($term) { + return array( + 'value' => $term->term_id, + 'label' => $term->name, + ); + }, array_values($terms)); + break; + default: + return $terms; + break; + } + } + /** * Gets the requested posts * - * @param array $parameters the additional query parameters - check + * @param array $parameters the additional query parameters - check * WP codex to know more * * @return array of the requested posts @@ -171,7 +209,7 @@ public function getPosts($parameters) /** * Registers a new query * - * @param array $parameters the additional query parameters - check + * @param array $parameters the additional query parameters - check * WP codex to know more * * @return array of the requested posts @@ -185,7 +223,7 @@ public function register($name, $querier) } /** * Overrides php __call. If the function is present in the class prototypes, it - * gets called. + * gets called. * * @param string $name the function name * @param array $args the arguments to be passed to the function diff --git a/aeria.php b/aeria.php index 78b3db3..2d87737 100755 --- a/aeria.php +++ b/aeria.php @@ -11,7 +11,7 @@ * Plugin Name: Aeria * Plugin URI: https://github.com/caffeinalab/aeria * Description: Aeria is a modular, lightweight, fast WordPress Application development kit. - * Version: 3.0.5 + * Version: 3.0.6 * Author: Caffeina * Author URI: https://caffeina.com * Text Domain: aeria