From d2743b5e23d580ae25af3bc200594a6e8c55a526 Mon Sep 17 00:00:00 2001 From: Alberto Parziale Date: Tue, 29 Oct 2019 12:07:32 +0100 Subject: [PATCH] remove duplicated files --- Aeria/Field/BaseField.php | 130 ------------------------------- Aeria/Field/GalleryField.php | 53 ------------- Aeria/Field/PictureField.php | 33 -------- Aeria/Field/RepeaterField.php | 97 ----------------------- Aeria/Field/SectionsField.php | 142 ---------------------------------- Aeria/Field/SelectField.php | 25 ------ Aeria/Field/SwitchField.php | 28 ------- 7 files changed, 508 deletions(-) delete mode 100644 Aeria/Field/BaseField.php delete mode 100644 Aeria/Field/GalleryField.php delete mode 100644 Aeria/Field/PictureField.php delete mode 100644 Aeria/Field/RepeaterField.php delete mode 100644 Aeria/Field/SectionsField.php delete mode 100644 Aeria/Field/SelectField.php delete mode 100644 Aeria/Field/SwitchField.php diff --git a/Aeria/Field/BaseField.php b/Aeria/Field/BaseField.php deleted file mode 100644 index 38dd368..0000000 --- a/Aeria/Field/BaseField.php +++ /dev/null @@ -1,130 +0,0 @@ -parentKey = $parentKey; - $this->config = $config; - $this->id = isset($config['id']) - ? $config['id'] - : null; - $this->index = $index; - $this->key = $this->getKey(); - $this->sections = $sections; - } - - public function shouldBeChildOf(Node $possibleParent) - { - if ($possibleParent->isMultipleField) { - if (preg_match('/^'.$possibleParent->getKey().'.{1,}/', $this->getKey())){ - return true; - } - else - return false; - } - else if (get_class($possibleParent)=="RootNode") // Check if possibleParent is root - return true; - else - return false; - } - - public function getKey() { - return $this->parentKey - . (!is_null($this->index) ? '-'.$this->index : '') - . (!is_null($this->id) ? '-'.$this->id : ''); - } - - public function get(array $savedFields) { - if (!isset($savedFields[$this->key])) - { - return null; - } - - if (is_array($savedFields[$this->key])) { - return $savedFields[$this->key][0]; - } else { - return $savedFields[$this->key]; - } - } - - public function getAdmin(array $savedFields, array $errors) { - - if (isset($errors[$this->key])) { - $result = [ - 'value' => $errors[$this->key]["value"], - 'error' => $errors[$this->key]["message"] - ]; - } else { - $result = [ - 'value' => $this->get($savedFields), - ]; - } - return array_merge( - $this->config, - $result - ); - } - - public function set($context_ID, $context_type, array $savedFields, $validator_service, $query_service) - { - $value = isset($_POST[$this->key]) ? $_POST[$this->key] : null; - $old = isset($savedFields[$this->key][0]) ? $savedFields[$this->key][0] : ''; - - if ($value == $old) return ["value" => $value]; - - if (is_null($value) || $value == '') { - $this->deleteField($context_ID, $context_type, $query_service); - return ["value" => $value]; - } else { - $validators=(isset($this->config["validators"])) ? $this->config["validators"] : ""; - $error=$validator_service->validate($value, $validators); - - if(!$error["status"]){ - $this->saveField($context_ID, $context_type, $value, $old); - return ["value" => $value]; - } else { - FieldError::make($context_ID) - ->addError($this->key, $error); - return $error; - } - } - } - - private function saveField($context_ID, $context_type, $value, $old){ - switch ($context_type) { - case 'options': - update_option($this->key, $value); - break; - case 'meta': - update_post_meta($context_ID, $this->key, $value, $old); - break; - default: - throw new Exception("Node context is not valid."); - break; - } - } - - private function deleteField($context_ID, $context_type, $query_service){ - switch ($context_type) { - case 'options': - $query_service->deleteOption($this->key); - break; - case 'meta': - $query_service->deleteMeta($context_ID, $this->key); - break; - - default: - throw new Exception("Node context is not valid."); - break; - } - } -} diff --git a/Aeria/Field/GalleryField.php b/Aeria/Field/GalleryField.php deleted file mode 100644 index 88ea8e6..0000000 --- a/Aeria/Field/GalleryField.php +++ /dev/null @@ -1,53 +0,0 @@ -key, ['id' => 'picture'], $this->sections, $i - ))->get($savedFields); - } - return $children; - } - - public function getAdmin(array $savedFields, array $errors) { - $stored_value = parent::get($savedFields); - $result = []; - $result['value'] = (int)$stored_value; - $result['children'] = []; - - for ($i = 0; $i < $result['value']; ++$i) { - $result['children'][] = (new PictureField( - $this->key, ['id' => 'picture'], $this->sections, $i - ))->getAdmin($savedFields, $errors); - } - return array_merge( - $this->config, - $result - ); - } - - public function set($context_ID, $context_type, array $savedFields, $validator_service, $query_service) { - $stored_values = (int)parent::set($context_ID, $context_type, $savedFields, $validator_service, $query_service)["value"]; - if(!$stored_values) { - return; - } - for ($i = 0; $i < $stored_values; ++$i) { - (new PictureField( - $this->key, ['id' => 'picture'], $this->sections, $i - ))->set($context_ID, $context_type, $savedFields, $validator_service, $query_service); - } - } -} diff --git a/Aeria/Field/PictureField.php b/Aeria/Field/PictureField.php deleted file mode 100644 index 9e347a3..0000000 --- a/Aeria/Field/PictureField.php +++ /dev/null @@ -1,33 +0,0 @@ - $thumb, - 'full' => $full - ]; - } - - - public function getAdmin(array $savedFields, array $errors) { - $stored_value = parent::get($savedFields); - $result = []; - $result['value'] = (int)$stored_value; - $result['url'] = wp_get_attachment_image_src($result['value'])[0]; - - return array_merge( - $this->config, - $result - ); - } -} diff --git a/Aeria/Field/RepeaterField.php b/Aeria/Field/RepeaterField.php deleted file mode 100644 index 8cb42ce..0000000 --- a/Aeria/Field/RepeaterField.php +++ /dev/null @@ -1,97 +0,0 @@ -config['fields']; - - for ($i = 0; $i < $stored_value; ++$i) { - $child = new \StdClass(); - for ($j = 0; $j < count($fields); ++$j) { - $field_config = $fields[$j]; - - $child->{$field_config['id']} = FieldNodeFactory::make( - $this->key, $field_config, $this->sections, $i - )->get($metas); - } - - if(count($fields) === 1){ - $children[] = $child->{$fields[0]['id']}; - }else{ - $children[] = $child; - } - } - - return $children; - } - - public function getAdmin(array $metas, array $errors) { - $stored_value = (int)parent::get($metas); - $children = []; - - $fields = $this->config['fields']; - - for ($i = 0; $i < $stored_value; ++$i) { - $child = []; - for ($j = 0; $j < count($fields); ++$j) { - $field_config = $fields[$j]; - - $child[] = FieldNodeFactory::make( - $this->key, $field_config, $this->sections, $i - )->getAdmin($metas, $errors); - } - $children[] = $child; - } - - return array_merge( - $this->config, - [ - "value" => $stored_value, - "children" => $children - ] - ); - } - - - public function set($context_ID, $context_type, array $metas, $validator_service, $query_service) { - $stored_values = (int)parent::set($context_ID, $context_type, $metas, $validator_service, $query_service)["value"]; - if(!$stored_values) { - return; - } - - $fields = $this->config['fields']; - - for ($i = 0; $i < $stored_values; ++$i) { - for ($j = 0; $j < count($fields); ++$j) { - FieldNodeFactory::make( - $this->key, $fields[$j], $this->sections, $i - )->set($context_ID, $context_type, $metas, $validator_service, $query_service); - } - } - $this->deleteOrphanMeta($this->key,$metas); - } - - private function deleteOrphanMeta ($parentKey, $metas) - { - $oldFields=static::pregGrepKeys("/^".$parentKey."/", $metas); - $newFields=static::pregGrepKeys("/^".$parentKey."/", $_POST); - $deletableFields = array_diff_key($oldFields, $newFields); - foreach ($deletableFields as $deletableKey => $deletableField) { - delete_post_meta($_POST['post_ID'], $deletableKey); - } - } - - private static function pregGrepKeys($pattern, $input) { - return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input)))); - } -} diff --git a/Aeria/Field/SectionsField.php b/Aeria/Field/SectionsField.php deleted file mode 100644 index cc7820c..0000000 --- a/Aeria/Field/SectionsField.php +++ /dev/null @@ -1,142 +0,0 @@ -sections[$type]; - } - - private function getTitle($index){ - // TODO: decide if headerTitle is needed inside the theme - return new BaseField( - $this->key, - ["id" => 'headerTitle', "validators" =>"" ], - $this->sections, - $index - ); - } - - private function getDraftMode($index){ - return new SwitchField( - $this->key, - ["id" => 'draft', "validators" => "" ], - $this->sections, - $index - ); - } - - public function get(array $metas) { - $types = parent::get($metas); - if(is_null($types) || $types == ''){ - return []; - } - - $types = explode(',', $types); - $children = []; - - foreach ($types as $i => $type) { - $section_config = $this->getSectionConfig($type); - $field_result = new \StdClass(); - $field_result->type = $type; - $field_result->data = new \StdClass(); - - foreach ($section_config['fields'] as $field_index => $field_config) { - $field_result->data->{$field_config['id']} = FieldNodeFactory::make( - $this->key, $field_config, $this->sections, $i - )->get($metas); - } - - $isDraft = $this->getDraftMode($i)->get($metas); - if(!$isDraft){ - $children[] = $field_result; - } - } - - return $children; - } - - public function getAdmin(array $metas, array $errors) { - $stored_value = parent::get($metas); - $stored_value = (bool)$stored_value ? explode(',', $stored_value) : []; - - $children = []; - - foreach ($stored_value as $type_index => $type) { - $section_config = $this->getSectionConfig($type); - - $fields = []; - - foreach ($section_config['fields'] as $field_index => $field_config) { - $fields[] = array_merge( - $field_config, - FieldNodeFactory::make( - $this->key, $field_config, $this->sections, $type_index - )->getAdmin($metas, $errors) - ); - } - - $children[] = array_merge( - $section_config, - [ - 'title' => $this->getTitle($type_index)->get($metas), - 'isDraft' => $this->getDraftMode($type_index)->get($metas), - 'fields' => $fields - ] - ); - } - return array_merge( - $this->config, - [ - "value" => $stored_value, - "children" => $children - ] - ); - } - - public function set($context_ID, $context_type, array $metas, $validator_service, $query_service) { - $stored_value = parent::set($context_ID, $context_type, $metas, $validator_service, $query_service)["value"]; - $stored_value = (bool)$stored_value ? explode(',', $stored_value) : []; - - foreach ($stored_value as $type_index => $type) { - $section_config = $this->getSectionConfig($type); - - // save title - $this->getTitle($type_index)->set($context_ID, $context_type, $metas, $validator_service, $query_service); - - // save status - $this->getDraftMode($type_index)->set($context_ID, $context_type, $metas, $validator_service, $query_service); - - // save children - foreach ($section_config['fields'] as $field_index => $field_config) { - FieldNodeFactory::make( - $this->key, $field_config, $this->sections, $type_index - )->set($context_ID, $context_type, $metas, $validator_service, $query_service, $query_service); - } - // remove orphans - $this->deleteOrphanMeta($this->key.'-'.$type_index, $metas); - } - } - - private function deleteOrphanMeta($parentKey, $metas) - { - $oldFields=static::pregGrepKeys("/^".$parentKey."/", $metas); - $newFields=static::pregGrepKeys("/^".$parentKey."/", $_POST); - $deletableFields = array_diff_key($oldFields, $newFields); - foreach ($deletableFields as $deletableKey => $deletableField){ - delete_post_meta($_POST['post_ID'], $deletableKey); - } - } - - private static function pregGrepKeys($pattern, $input) { - return array_intersect_key($input, array_flip(preg_grep($pattern, array_keys($input)))); - } -} diff --git a/Aeria/Field/SelectField.php b/Aeria/Field/SelectField.php deleted file mode 100644 index cc02345..0000000 --- a/Aeria/Field/SelectField.php +++ /dev/null @@ -1,25 +0,0 @@ -config['multiple']) && $this->config['multiple']){ - $values = explode(',', $values); - } - return $values; - } - - public function getAdmin(array $savedFields, array $errors) { - $savedValues = parent::getAdmin($savedFields, $errors); - - return array_merge( - $this->config, - $savedValues - ); - } -} diff --git a/Aeria/Field/SwitchField.php b/Aeria/Field/SwitchField.php deleted file mode 100644 index 073113e..0000000 --- a/Aeria/Field/SwitchField.php +++ /dev/null @@ -1,28 +0,0 @@ -key]) || !filter_var($_POST[$this->key], FILTER_VALIDATE_BOOLEAN)) { - $_POST[$this->key] = 'false'; - } else { - $_POST[$this->key] = 'true'; - } - parent::set($context_ID, $context_type, $savedFields, $validator_service, $query_service); - } -}