| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\AddonAPI\Importer\Template; |
| 4 |
|
| 5 |
class Field |
| 6 |
{ |
| 7 |
private $_id; |
| 8 |
private $_name; |
| 9 |
private $_type = 'text'; |
| 10 |
private $_group; |
| 11 |
private $_args = []; |
| 12 |
|
| 13 |
public function __construct($name, $args = []) |
| 14 |
{ |
| 15 |
$this->_id = $args['id'] ?? sanitize_title($name); |
| 16 |
$this->_name = $name; |
| 17 |
|
| 18 |
if (isset($args['type'])) { |
| 19 |
$this->_type = $args['type']; |
| 20 |
} |
| 21 |
|
| 22 |
if (isset($args['group'])) { |
| 23 |
|
| 24 |
if (is_a($args['group'], \ImportWP\Common\AddonAPI\Importer\Template\Panel::class)) { |
| 25 |
$this->_group = $args['group']->get_id(); |
| 26 |
} elseif (is_string($args['group'])) { |
| 27 |
$this->_group = $args['group']; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
$this->_args = $args; |
| 32 |
} |
| 33 |
|
| 34 |
public function get_id() |
| 35 |
{ |
| 36 |
return $this->_id; |
| 37 |
} |
| 38 |
|
| 39 |
public function get_name() |
| 40 |
{ |
| 41 |
return $this->_name; |
| 42 |
} |
| 43 |
|
| 44 |
public function get_type() |
| 45 |
{ |
| 46 |
return $this->_type; |
| 47 |
} |
| 48 |
|
| 49 |
public function get_group() |
| 50 |
{ |
| 51 |
return $this->_group; |
| 52 |
} |
| 53 |
|
| 54 |
public function get_args() |
| 55 |
{ |
| 56 |
return $this->_args; |
| 57 |
} |
| 58 |
|
| 59 |
public function get_arg($key) |
| 60 |
{ |
| 61 |
return isset($this->_args[$key]) ? $this->_args[$key] : null; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* @param \ImportWP\Common\Importer\Template\Template $template |
| 66 |
* @return [] |
| 67 |
*/ |
| 68 |
public function get_template_data($template) |
| 69 |
{ |
| 70 |
switch ($this->_type) { |
| 71 |
case 'text': |
| 72 |
return $template->register_field($this->get_name(), $this->get_id(), $this->get_args()); |
| 73 |
case 'attachment': |
| 74 |
return $template->register_attachment_fields($this->get_name(), $this->get_id(), $this->get_arg('field_label'), $this->get_args()); |
| 75 |
} |
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
|