Skip to content

Commit

Permalink
add filter on transform config and update ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Parziale committed Mar 27, 2020
1 parent ac98775 commit e90704c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 72 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.1.7';
const VERSION = '3.1.8';

/**
* Constructs the Aeria container.
Expand Down
3 changes: 0 additions & 3 deletions Aeria/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,6 @@ public function getValidationStructure(): array
'capability' => $this->makeRegExValidator(
'/^[a-z0-9_-]+$/'
),
'parent' => $this->makeRegExValidator(
'/^[a-z0-9_-]+$/'
),
'fields' => function ($value) {
return [
'result' => is_array($value),
Expand Down
22 changes: 12 additions & 10 deletions Aeria/Field/Fields/BaseField.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public function shouldBeChildOf(Node $possible_parent)
public function getKey()
{
return $this->parent_key
.(!is_null($this->index) ? '-'.$this->index : '')
.(!is_null($this->id) ? '-'.$this->id : '');
.(!is_null($this->index) ? '-'.$this->index : '')
.(!is_null($this->id) ? '-'.$this->id : '');
}

/**
Expand Down Expand Up @@ -142,23 +142,25 @@ public function getAdmin(array $saved_fields, array $errors)
{
if (isset($errors[$this->key])) {
$result = [
'value' => $errors[$this->key]['value'],
'error' => $errors[$this->key]['message'],
'value' => $errors[$this->key]['value'],
'error' => $errors[$this->key]['message'],
];
} else {
$result = [
'value' => $this->get($saved_fields, true),
'value' => $this->get($saved_fields, true),
];
}

if (is_null($result['value'])) {
return $this->config;
}

return array_merge(
$config = array_merge(
$this->config,
$result
);

$config = apply_filters('aeria_get_admin_base', $config);
$config = apply_filters('aeria_get_admin_'.$this->id, $config);
$config = apply_filters('aeria_get_admin_'.$this->key, $config);

return $config;
}

/**
Expand Down
30 changes: 18 additions & 12 deletions Aeria/Field/Fields/SectionsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ public function getAdmin(array $metas, array $errors)
$children[] = array_merge(
$section_config,
[
'title' => $this->getTitle($type_index)->get($metas),
'isDraft' => $this->getDraftMode($type_index)->get($metas),
'accordionState' => $this->getAccordionStatus($type_index)->get($metas),
'fields' => $fields,
'title' => $this->getTitle($type_index)->get($metas),
'isDraft' => $this->getDraftMode($type_index)->get($metas),
'accordionState' => $this->getAccordionStatus($type_index)->get($metas),
'fields' => $fields,
]
);
}
Expand All @@ -186,8 +186,8 @@ public function getAdmin(array $metas, array $errors)
return array_merge(
$this->config,
[
'value' => $stored_value,
'children' => $children,
'value' => $stored_value,
'children' => $children,
]
);
}
Expand Down Expand Up @@ -226,26 +226,32 @@ public function set($context_ID, $context_type, array $metas, array $new_values,
)->set($context_ID, $context_type, $metas, $new_values, $validator_service, $query_service, $query_service);
}
// remove orphans
$this->deleteOrphanMeta($this->key.'-'.$type_index, $metas, $new_values);
$this->deleteOrphanMeta($this->key.'-'.$type_index, $metas, $new_values, $context_type);
}
}

/**
* Deletes the metas that lose a parent :(.
*
* @param string $parent_key the parent's key
* @param array $metas the saved fields
* @param array $new_values the values we're saving
* @param string $parent_key the parent's key
* @param array $metas the saved fields
* @param array $new_values the values we're saving
* @param string $context_type the context type. Right now, options|meta
*
* @since Method available since Release 3.0.0
*/
private function deleteOrphanMeta($parent_key, $metas, $new_values)
private function deleteOrphanMeta($parent_key, $metas, $new_values, $context_type)
{
$oldFields = static::pregGrepKeys('/^'.$parent_key.'/', $metas);
$newFields = static::pregGrepKeys('/^'.$parent_key.'/', $new_values);
$deletableFields = array_diff_key($oldFields, $newFields);

foreach ($deletableFields as $deletableKey => $deletableField) {
delete_post_meta($new_values['post_ID'], $deletableKey);
if ($context_type === 'options') {
delete_option($deletableKey);
} else {
delete_post_meta($new_values['post_ID'], $deletableKey);
}
}
}

Expand Down
20 changes: 0 additions & 20 deletions Aeria/Field/Fields/SelectField.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,4 @@ public function get(array $saved_fields, bool $skip_filter = false)

return $values;
}

/**
* Gets the field's value and its errors.
*
* @param array $saved_fields the FieldGroup's saved fields
* @param array $errors the saving errors
*
* @return array the field's config, hydrated with values and errors
*
* @since Method available since Release 3.0.0
*/
public function getAdmin(array $saved_fields, array $errors)
{
$savedValues = parent::getAdmin($saved_fields, $errors, true);

return array_merge(
$this->config,
$savedValues
);
}
}
18 changes: 14 additions & 4 deletions Aeria/Kernel/Tasks/CreateConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function do(array $args)
{
$args['config'] = $this->manipulateConfig($args['config']);
$args['config'] = $this->checkSectionIds($args['config']);
$args['container']->make('config')->merge($args['config']);
$args['config'] = apply_filters('aeria_transform_config', $args['config']);
$args['container']->make('config')->load($args['config']);

return $args;
}
Expand All @@ -39,6 +40,7 @@ private function checkSectionIds($tree)
{
foreach ($tree as $key => $value) {
$tree[$key]['id'] = $key;
$tree[$key] = apply_filters('aeria_transform_section_'.$tree[$key]['id'], $tree[$key]);
}

return $tree;
Expand All @@ -47,9 +49,6 @@ private function checkSectionIds($tree)
private function manipulateConfig($tree)
{
foreach ($tree as $key => $value) {
if (is_array($tree[$key])) {
$tree[$key] = $this->manipulateConfig($tree[$key]);
}
if ($key === 'section') {
$tree[$key] = $this->checkSectionIds($tree[$key]);
}
Expand All @@ -58,6 +57,9 @@ private function manipulateConfig($tree)
$tree[$key][$fieldKey] = $this->getRealFields($field_config);
}
}
if (is_array($tree[$key])) {
$tree[$key] = $this->manipulateConfig($tree[$key]);
}
}

return $tree;
Expand All @@ -71,6 +73,11 @@ private function getRealFields($field_config)
$field_config['fields'] = $this->getRealFields($field_config['fields']);
}

if (isset($field_config['id'])) {
$field_config = apply_filters('aeria_before_transform_field_base', $field_config);
$field_config = apply_filters('aeria_before_transform_field_'.$field_config['id'], $field_config);
}

if (!isset($field_config['type'])) {
return $field_config;
}
Expand All @@ -87,6 +94,9 @@ private function getRealFields($field_config)
? $field_type_class::transformConfig($field_config)
: $field_config;

$new_field_config = apply_filters('aeria_after_transform_field_base', $new_field_config);
$new_field_config = apply_filters('aeria_after_transform_field_'.$new_field_config['id'], $new_field_config);

return $new_field_config;
}
}
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.1.7
* Version: 3.1.8
* Author: Caffeina
* Author URI: https://caffeina.com
* Text Domain: aeria
Expand Down
16 changes: 8 additions & 8 deletions assets/js/aeria.js

Large diffs are not rendered by default.

19 changes: 9 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "aeria",
"version": "3.1.7",
"version": "3.1.8",
"description": "Aeria",
"scripts": {
"dev": "webpack --watch --mode development",
Expand Down Expand Up @@ -38,8 +38,8 @@
"webpack-cli": "^3.3.11"
},
"dependencies": {
"@aeria/core": "0.0.11",
"@aeria/uikit": "0.0.13",
"@aeria/core": "0.0.13",
"@aeria/uikit": "0.0.15",
"lodash.throttle": "^4.1.1",
"polished": "^3.5.1",
"react": "^16.13.1",
Expand Down

0 comments on commit e90704c

Please sign in to comment.