| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\AddonAPI\Importer; |
| 4 |
|
| 5 |
use ImportWP\Common\AddonAPI\Importer\Template\Field; |
| 6 |
|
| 7 |
class FieldData |
| 8 |
{ |
| 9 |
/** |
| 10 |
* @var \ImportWP\Common\AddonAPI\Importer\Template\Field |
| 11 |
*/ |
| 12 |
private $_field; |
| 13 |
|
| 14 |
/** |
| 15 |
* @var \ImportWP\Common\AddonAPI\Importer\ImporterData |
| 16 |
*/ |
| 17 |
private $_addon_data; |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string |
| 21 |
*/ |
| 22 |
private $_data_group; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var string |
| 26 |
*/ |
| 27 |
private $_field_prefix; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param \ImportWP\Common\AddonAPI\Importer\Template\Field $field |
| 31 |
* @param \ImportWP\Common\AddonAPI\Importer\ImporterData $addon_data |
| 32 |
* @param array $args |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function __construct($field, $addon_data, $args = []) |
| 36 |
{ |
| 37 |
$this->_field = $field; |
| 38 |
$this->_addon_data = $addon_data; |
| 39 |
|
| 40 |
if (isset($args['data_group'])) { |
| 41 |
$this->_data_group = $args['data_group']; |
| 42 |
} |
| 43 |
|
| 44 |
if (isset($args['field_prefix'])) { |
| 45 |
$this->_field_prefix = $args['field_prefix']; |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function get_value() |
| 50 |
{ |
| 51 |
// Handle permssions |
| 52 |
$permission_key = $this->_field_prefix . '.' . $this->_field->get_id(); |
| 53 |
$allowed = $this->_addon_data->get_data()->permission()->validate([$permission_key => ''], $this->_addon_data->get_data()->getMethod(), $this->_data_group); |
| 54 |
$is_allowed = isset($allowed[$permission_key]) ? true : false; |
| 55 |
if (!$is_allowed) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
$field_type = $this->_field->get_type(); |
| 60 |
if ($field_type === 'text') { |
| 61 |
|
| 62 |
return $this->_addon_data->get_data()->getValue($this->_field_prefix . '.' . $this->_field->get_id(), $this->_data_group); |
| 63 |
} elseif ($field_type === 'attachment') { |
| 64 |
// TODO: how do we handle attachment fields |
| 65 |
|
| 66 |
$data = $this->_addon_data->get_data()->getData($this->_data_group); |
| 67 |
$attachment_prefix = $this->_field_prefix . '.' . $this->_field->get_id(); |
| 68 |
|
| 69 |
$attachment_keys = [ |
| 70 |
'_meta._title', |
| 71 |
'_meta._alt', |
| 72 |
'_meta._caption', |
| 73 |
'_meta._description', |
| 74 |
'_enable_image_hash', |
| 75 |
'_download', |
| 76 |
'_featured', |
| 77 |
'_remote_url', |
| 78 |
'_ftp_user', |
| 79 |
'_ftp_host', |
| 80 |
'_ftp_pass', |
| 81 |
'_ftp_path', |
| 82 |
'_local_url', |
| 83 |
'_meta._enabled' |
| 84 |
]; |
| 85 |
$attachment_data = [ |
| 86 |
'location' => $data[$attachment_prefix . '.location'], |
| 87 |
]; |
| 88 |
|
| 89 |
foreach ($attachment_keys as $k) { |
| 90 |
if (isset($data[$attachment_prefix . '.settings.' . $k])) { |
| 91 |
$attachment_data[$k] = $data[$attachment_prefix . '.settings.' . $k]; |
| 92 |
} elseif (isset($data[$attachment_prefix . '.' . $k])) { |
| 93 |
$attachment_data[$k] = $data[$attachment_prefix . '.' . $k]; |
| 94 |
} else { |
| 95 |
$attachment_data[$k] = ''; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
return $this->_addon_data->process_attachment($this->_addon_data->get_id(), $attachment_data); |
| 100 |
} |
| 101 |
|
| 102 |
return false; |
| 103 |
} |
| 104 |
} |
| 105 |
|