Skip to content

Commit

Permalink
Enable virtual fields in sections - Version bump to 3.0.14
Browse files Browse the repository at this point in the history
  • Loading branch information
Alberto Parziale committed Oct 29, 2019
1 parent d2743b5 commit 489fead
Show file tree
Hide file tree
Showing 13 changed files with 243 additions and 152 deletions.
2 changes: 1 addition & 1 deletion Aeria/Aeria.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/
class Aeria extends Container
{
public const VERSION = '3.0.13';
public const VERSION = '3.0.14';
/**
* Constructs the Aeria container
*
Expand Down
26 changes: 19 additions & 7 deletions Aeria/Field/Fields/BaseField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

/**
* BaseField is the class that represents every Aeria field
*
*
* @category Field
* @package Aeria
* @author Andrea Longo <andrea.longo@caffeina.com>
Expand All @@ -20,6 +20,18 @@
class BaseField extends Node implements FieldInterface
{
public $is_multiple_field = false;

/**
* Transform the config array; note that this does not operate on
* `$this->config`: this way it can be called from outside
*
* @param array $config the field's config
*
* @return array the transformed config
*/
public static function transformConfig(array $config) {
return $config;
}
/**
* Constructs the field
*
Expand All @@ -35,7 +47,7 @@ class BaseField extends Node implements FieldInterface
*/
public function __construct($parent_key, $config, $sections, $index = null) {
$this->parent_key = $parent_key;
$this->config = $config;
$this->config = static::transformConfig($config);
$this->id = isset($config['id'])
? $config['id']
: null;
Expand Down Expand Up @@ -75,7 +87,7 @@ public function shouldBeChildOf(Node $possible_parent)
* @access public
* @since Method available since Release 3.0.0
*/
public function getKey()
public function getKey()
{
return $this->parent_key
. (!is_null($this->index) ? '-'.$this->index : '')
Expand Down Expand Up @@ -117,7 +129,7 @@ public function get(array $saved_fields, bool $skip_filter = false) {
* @access public
* @since Method available since Release 3.0.0
*/
public function getAdmin(array $saved_fields, array $errors)
public function getAdmin(array $saved_fields, array $errors)
{
if (isset($errors[$this->key])) {
$result = [
Expand Down Expand Up @@ -148,7 +160,7 @@ public function getAdmin(array $saved_fields, array $errors)
* @param array $new_values the values we're saving
* @param Validator $validator_service Aeria's validator service
* @param Query $query_service Aeria's query service
*
*
* @return array the results of the saving
*
* @access public
Expand Down Expand Up @@ -184,7 +196,7 @@ public function set($context_ID, $context_type, array $saved_fields, array $new_
* @param string $context_type the context type. Right now, options|meta
* @param mixed $value the new value
* @param mixed $old the old value
*
*
* @return void
* @throws Exception if the node context is invalid
*
Expand All @@ -211,7 +223,7 @@ private function saveField($context_ID, $context_type, $value, $old)
* @param int $context_ID the context ID. For posts, post's ID
* @param string $context_type the context type. Right now, options|meta
* @param Query $query_service Aeria's query service
*
*
* @return void
* @throws Exception if the node context is invalid
*
Expand Down
7 changes: 4 additions & 3 deletions Aeria/Field/Fields/DateRangeField.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Aeria\Field\Interfaces\FieldInterface;
/**
* DateRange is the class that represents a Date Range field
*
*
* @category Field
* @package Aeria
* @author Simone Montali <simone.montali@caffeina.com>
Expand All @@ -31,8 +31,9 @@ public function get(array $metas, bool $skip_filter = false)
$from = (new BaseField($this->key, ['id' => 'from'], $this->sections))->get($metas);
$to = (new BaseField($this->key, ['id' => 'to'], $this->sections))->get($metas);
$values = [$from,$to];
if(!$skip_filter)
if(!$skip_filter) {
$values = apply_filters("aeria_get_daterange", $values, $this->config);
}
return $values;
}
/**
Expand All @@ -59,7 +60,7 @@ public function getAdmin(array $metas, array $errors)
* @param array $new_values the values we're saving
* @param Validator $validator_service Aeria's validator service
* @param Query $query_service Aeria's query service
*
*
* @return void
*
* @access public
Expand Down
51 changes: 30 additions & 21 deletions Aeria/Field/Fields/PostTypesField.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,35 @@ class PostTypesField extends SelectField
{

protected $original_config;
/**
* Transform the config array; note that this does not operate on
* `$this->config`: this way it can be called from outside
*
* @param array $config the field's config
*
* @return array the transformed config
*/
public static function transformConfig(array $config) {
$config['type'] = 'select';

$config['options'] = [
array( 'label' => 'page', 'value' => 'page'),
array( 'label' => 'post', 'value' => 'post'),
];

$post_types = get_post_types(array(
'public' => true,
'_builtin' => false
));

$config['options'] = array_merge(
$config['options'],
array_map( function ($post_type) {
return array( 'label' => $post_type, 'value' => $post_type);
}, array_values($post_types) )
);
return parent::transformConfig($config);
}
/**
* Constructs the field
*
Expand All @@ -31,27 +60,7 @@ class PostTypesField extends SelectField
* @since Method available since Release 3.0.7
*/
public function __construct($parent_key, $config, $sections, $index = null) {
parent::__construct($parent_key, $config, $sections, $index);

$this->original_config = json_decode(json_encode($config));

$this->config['type'] = 'select';

$this->config['options'] = [
array( 'label' => 'page', 'value' => 'page'),
array( 'label' => 'post', 'value' => 'post'),
];

$post_types = get_post_types(array(
'public' => true,
'_builtin' => false
));

$this->config['options'] = array_merge(
$this->config['options'],
array_map( function ($post_type) {
return array( 'label' => $post_type, 'value' => $post_type);
}, array_values($post_types) )
);
parent::__construct($parent_key, $config, $sections, $index);
}
}
23 changes: 16 additions & 7 deletions Aeria/Field/Fields/RelationField.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Aeria\Field\Interfaces\FieldInterface;
/**
* Relation is the class that represents a relationship field
*
*
* @category Field
* @package Aeria
* @author Andrea Longo <andrea.longo@caffeina.com>
Expand All @@ -17,6 +17,20 @@ class RelationField extends SelectField
{

protected $original_config;
/**
* Transform the config array; note that this does not operate on
* `$this->config`: this way it can be called from outside
*
* @param array $config the field's config
*
* @return array the transformed config
*/
public static function transformConfig(array $config) {
$config['type'] = 'select';
$config['ajax'] = $config['relation'];
unset($config['relation']);
return parent::transformConfig($config);
}
/**
* Constructs the field
*
Expand All @@ -31,13 +45,8 @@ class RelationField extends SelectField
* @since Method available since Release 3.0.0
*/
public function __construct($parent_key, $config, $sections, $index = null) {
parent::__construct($parent_key, $config, $sections, $index);

$this->original_config = json_decode(json_encode($config));

$this->config['type'] = 'select';
$this->config['ajax'] = $this->config['relation'];
unset($this->config['relation']);
parent::__construct($parent_key, $config, $sections, $index);
}
/**
* Gets the field's value
Expand Down
13 changes: 8 additions & 5 deletions Aeria/Field/Fields/SectionsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Aeria\Field\Exceptions\NonExistentConfigException;

/**
* Sections is the class that represents a section field.
* Sections is the class that represents a section field
*
* @category Field
*
Expand All @@ -32,9 +32,9 @@ class SectionsField extends BaseField
private function getSectionConfig($type)
{
if (isset($this->sections[$type])) {
return $this->sections[$type];
return $this->sections[$type];
} else {
throw new NonExistentConfigException();
throw new NonExistentConfigException();
}
}

Expand Down Expand Up @@ -132,10 +132,9 @@ public function get(array $metas, bool $skip_filter = false)
$children[] = $field_result;
}
}
if (!$skip_filter) {
if(!$skip_filter) {
$children = apply_filters('aeria_get_sections', $children, $this->config);
}

return $children;
}

Expand Down Expand Up @@ -202,6 +201,8 @@ public function getAdmin(array $metas, array $errors)
* @param Validator $validator_service Aeria's validator service
* @param Query $query_service Aeria's query service
*
* @return void
*
* @since Method available since Release 3.0.0
*/
public function set($context_ID, $context_type, array $metas, array $new_values, $validator_service, $query_service)
Expand Down Expand Up @@ -237,6 +238,8 @@ public function set($context_ID, $context_type, array $metas, array $new_values,
* @param array $metas the saved fields
* @param array $new_values the values we're saving
*
* @return void
*
* @since Method available since Release 3.0.0
*/
private function deleteOrphanMeta($parent_key, $metas, $new_values)
Expand Down
53 changes: 32 additions & 21 deletions Aeria/Field/Fields/TermsField.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,38 @@
class TermsField extends SelectField
{
protected $original_config;
/**
* Transform the config array; note that this does not operate on
* `$this->config`: this way it can be called from outside
*
* @param array $config the field's config
*
* @return array the transformed config
*/
public static function transformConfig(array $config) {
$config['type'] = 'select';

$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'] = array_map(
function ($term) {
return array(
'label' => $term->name,
'value' => $term->term_id,
);
},
array_values($terms)
);

unset($config['taxonomy']);
return parent::transformConfig($config);
}
/**
* Constructs the field.
*
Expand All @@ -28,27 +59,7 @@ class TermsField extends SelectField
*/
public function __construct($parent_key, $config, $sections, $index = null)
{
parent::__construct($parent_key, $config, $sections, $index);

$this->original_config = json_decode(json_encode($config));

$this->config['type'] = 'select';

$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,
));

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

unset($this->config['taxonomy']);
parent::__construct($parent_key, $config, $sections, $index);
}
}
Loading

0 comments on commit 489fead

Please sign in to comment.