| | |
| | | 'sponsor', |
| | | 'containsInPlace', |
| | | 'containsPlace', |
| | | 'openingHours' |
| | | 'openingHours', |
| | | 'id', |
| | | 'ignore', |
| | | ]; |
| | | |
| | | protected array $hints = [ |
| | |
| | | protected function setChecks():void |
| | | { |
| | | $checks = [ |
| | | 'website' |
| | | 'website', |
| | | 'organization' |
| | | ]; |
| | | $this->checks = array_merge($checks, Registrar::getRegistered()); |
| | | } |
| | |
| | | } |
| | | public function renderFieldsFor(string $class, array $stored):void |
| | | { |
| | | $fields = $this->getFieldsFor($class); |
| | | $fields = $this->getFieldsForClass($class); |
| | | $instance = new $class(); |
| | | foreach ($fields as $property => $value) { |
| | | $this->renderProperty($property, $stored[$property]??null, $instance); |
| | | } |
| | | } |
| | | |
| | | public function getFieldsFor(string $class):array |
| | | public function getFieldsForClass(string $class):array |
| | | { |
| | | if (!class_exists($class)) { |
| | | error_log('Class not found: '.$class); |
| | |
| | | } |
| | | |
| | | |
| | | |
| | | $_POST['type'] = $type; |
| | | $result = $this->saveFields($action, $type, $_POST); |
| | | |
| | | |
| | |
| | | |
| | | public function saveFields(string $action, string $class, array $data):array |
| | | { |
| | | $action = strtolower($action); |
| | | if (!in_array($action, $this->checks)) { |
| | | error_log('[SEOAdmin]Action is not allowed: '.$action); |
| | | return [ |
| | |
| | | ]; |
| | | } |
| | | |
| | | $allowed = $this->getFieldsFor($class); |
| | | if (empty($allowed)) { |
| | | return [ |
| | | 'jvb_notice' => 'error', |
| | | 'jvb_message' => 'Could not get fields from class' |
| | | ]; |
| | | } |
| | | |
| | | $checked = array_filter($data, function ($item) use ($allowed) { |
| | | return array_key_exists($item, $allowed); |
| | | }, ARRAY_FILTER_USE_KEY); |
| | | |
| | | $stored = get_option(BASE.ucfirst($action).'Schema', []); |
| | | $updates = []; |
| | | foreach ($checked as $property => $value) { |
| | | $sanitized = Sanitizer::sanitize($value, $this->buildConfig($property)); |
| | | if (!array_key_exists($property, $stored) || $stored[$property] !== $sanitized) |
| | | $updates[$property] = $sanitized; |
| | | } |
| | | if (!empty($updates)) { |
| | | $history = get_option(BASE.ucfirst($action).'SchemaHistory', []); |
| | | array_unshift($history, $stored); |
| | | if (count($history) > 5){ |
| | | array_pop($history); |
| | | } |
| | | update_option(BASE.ucfirst($action).'SchemaHistory', $history); |
| | | |
| | | $update = array_merge($stored, $updates); |
| | | update_option(BASE.ucfirst($action).'Schema', $update); |
| | | } |
| | | return [ |
| | | $success = JVB()->schemaHelper()->updateSchema($action, $data); |
| | | return $success ? [ |
| | | 'jvb_notice' => 'success', |
| | | 'jvb_message' => 'Saved changes successfully' |
| | | ] : [ |
| | | 'jvb_notice' => 'error', |
| | | 'jvb_message' => 'Something went wrong...' |
| | | ]; |
| | | } |
| | | } |