<?php
|
namespace JVBase\registrar\fields;
|
|
use JVBase\meta\MetaTypeManager;
|
|
if (!defined('ABSPATH')) {
|
exit;
|
}
|
|
class Field {
|
protected string $name; // field name. Will be prefixed with BASE
|
protected string $type; // field type
|
protected string $label; // define its label
|
protected mixed $default; // default value
|
protected string $description;
|
protected string $hint;
|
protected bool $required = false; // whether it is required
|
protected bool $hidden = false; // whether to show in the editor
|
protected bool $quickEdit = true; // whether to show in quick edit table
|
protected bool $quill; // whether to use quill
|
protected int $maxLength; // of characters
|
/**
|
* @var ?bool For timeline post types. Indicates whether all posts get this field, or just the parent
|
*/
|
protected ?bool $for_all = null;
|
/**
|
* @var ?string Rather than manually defining sections, one can define which section a field belongs to here
|
*/
|
protected ?string $section = null;
|
|
public function __construct(string $name, array $config) {
|
$this->name = $name;
|
$required = ['type', 'label'];
|
foreach ($required as $r) {
|
if (!array_key_exists($r, $config)) {
|
error_log('[JVBase\registrar\Field] Missing required '.$r.' for field');
|
return;
|
}
|
}
|
foreach ($config as $key => $value) {
|
if (property_exists($this, $key)) {
|
$method = 'set' . ucfirst($key);
|
$this->$method($value);
|
} else {
|
error_log('Instance: '.print_r($this, true));
|
error_log('[JVBase\registrar\Field] Invalid key for '.$name.': '.$key);
|
}
|
}
|
}
|
|
public function setDescription(string $description):void
|
{
|
$this->description = $description;
|
}
|
public function getDescription():string
|
{
|
return $this->description;
|
}
|
public function setHint(string $hint):void
|
{
|
$this->hint = $hint;
|
}
|
public function getHint():string
|
{
|
return $this->hint;
|
}
|
|
protected function setType(string $type):void{
|
$allowed = array_keys(MetaTypeManager::getTypes());
|
if (!in_array($type, $allowed)) {
|
error_log('[JVBase\registrar\Field] Invalid type attempted '.$type);
|
return;
|
}
|
$this->type = $type;
|
}
|
|
protected function setLabel(string $label):void{
|
$this->label = $label;
|
}
|
protected function setRequired(bool $required):void{
|
$this->required = $required;
|
}
|
protected function setHidden(bool $hidden):void{
|
$this->hidden = $hidden;
|
}
|
protected function setQuickEdit(bool $quickEdit):void{
|
$this->quickEdit = $quickEdit;
|
}
|
protected function setDefault(mixed $default):void
|
{
|
$this->default = $default;
|
}
|
|
protected function setQuill(bool $quill):void
|
{
|
$this->quill = $quill;
|
}
|
|
public function setForAll(bool $set):void
|
{
|
$this->for_all = $set;
|
}
|
public function getForAll():?bool
|
{
|
return $this->for_all??null;
|
}
|
public function setSection(string $section):void
|
{
|
$this->section = $section;
|
}
|
public function getSection():?string
|
{
|
return $this->section??null;
|
}
|
|
protected function setMaxLength(int $maxLength):void
|
{
|
$this->maxLength = $maxLength;
|
}
|
public function getConfig():array{
|
$config = get_object_vars($this);
|
|
$config = array_map(function ($item) {
|
if (is_a($item, Field::class)) {
|
return $item->getConfig();
|
} else if (is_array($item)) {
|
$temp = [];
|
foreach ($item as $v) {
|
if (is_a($v, Field::class)) {
|
$temp[] = $v->getConfig();
|
} else {
|
$temp[] = $v;
|
}
|
}
|
return $temp;
|
} else {
|
return $item;
|
}
|
}, $config);
|
|
return $config;
|
}
|
}
|