| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\AddonAPI\Importer; |
| 4 |
|
| 5 |
class CustomFieldsData |
| 6 |
{ |
| 7 |
/** |
| 8 |
* @var \ImportWP\Common\AddonAPI\Importer\Template\CustomFields |
| 9 |
*/ |
| 10 |
private $_custom_fields; |
| 11 |
|
| 12 |
/** |
| 13 |
* @var \ImportWP\Common\AddonAPI\Importer\ImporterData |
| 14 |
*/ |
| 15 |
private $_addon_data; |
| 16 |
|
| 17 |
private $_data_cache = null; |
| 18 |
|
| 19 |
/** |
| 20 |
* @param \ImportWP\Common\AddonAPI\Importer\Template\CustomFields $panel |
| 21 |
* @param \ImportWP\Common\AddonAPI\Importer\ImporterData $addon_data |
| 22 |
* @return void |
| 23 |
*/ |
| 24 |
public function __construct($custom_fields, $addon_data) |
| 25 |
{ |
| 26 |
$this->_custom_fields = $custom_fields; |
| 27 |
$this->_addon_data = $addon_data; |
| 28 |
} |
| 29 |
|
| 30 |
public function get_value($field_id) |
| 31 |
{ |
| 32 |
if (is_null($this->_data_cache)) { |
| 33 |
|
| 34 |
$this->_data_cache = []; |
| 35 |
|
| 36 |
$custom_field_data = $this->_addon_data->get_data()->getData('custom_fields'); |
| 37 |
$max_rows = intval($custom_field_data['custom_fields._index']); |
| 38 |
|
| 39 |
for ($i = 0; $i < $max_rows; $i++) { |
| 40 |
|
| 41 |
// skip if field is not part of these custom fields |
| 42 |
if (strpos($custom_field_data['custom_fields.' . $i . '.key'], $this->_custom_fields->get_prefix() . '::') !== 0) { |
| 43 |
continue; |
| 44 |
} |
| 45 |
|
| 46 |
// TODO: check to see if field is registered, and if so capture data and process |
| 47 |
|
| 48 |
$current_field = false; |
| 49 |
foreach ($this->_custom_fields->get_fields() as $custom_field_id => $custom_field) { |
| 50 |
if ($custom_field_data['custom_fields.' . $i . '.key'] === $this->_custom_fields->get_prefix() . '::' . $custom_field['type'] . '::' . $custom_field_id) { |
| 51 |
$current_field = $custom_field; |
| 52 |
break; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
if (!$current_field) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
// capture custom field settings |
| 61 |
$this->_data_cache[$current_field['id']] = [ |
| 62 |
'field' => $current_field, |
| 63 |
'settings' => [] |
| 64 |
]; |
| 65 |
foreach ($custom_field_data as $k => $v) { |
| 66 |
|
| 67 |
$cf_prefix = 'custom_fields.' . $i . '.'; |
| 68 |
$key_length = strlen($cf_prefix); |
| 69 |
if (strpos($k, $cf_prefix) !== 0) { |
| 70 |
continue; |
| 71 |
} |
| 72 |
|
| 73 |
$this->_data_cache[$current_field['id']]['settings'][substr($k, $key_length)] = $v; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
if (!isset($this->_data_cache[$field_id])) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
// TODO: process custom field |
| 83 |
$custom_field = $this->_data_cache[$field_id]['field']; |
| 84 |
$settings = $this->_data_cache[$field_id]['settings']; |
| 85 |
|
| 86 |
// check permissions |
| 87 |
$permission_key = 'custom_fields' . '.' . apply_filters('iwp/custom_field_key', $settings['key']); |
| 88 |
$allowed = $this->_addon_data->get_data()->permission()->validate([$permission_key => ''], $this->_addon_data->get_data()->getMethod(), 'custom_fields'); |
| 89 |
$is_allowed = isset($allowed[$permission_key]) ? true : false; |
| 90 |
if (!$is_allowed) { |
| 91 |
return false; |
| 92 |
} |
| 93 |
|
| 94 |
$value = $settings['value']; |
| 95 |
$value = apply_filters('iwp/template/process_field', $value, $field_id, iwp()->importer); |
| 96 |
|
| 97 |
switch ($custom_field['type']) { |
| 98 |
case 'attachment': |
| 99 |
$settings['location'] = $value; |
| 100 |
return $this->_addon_data->process_attachment($this->_addon_data->get_id(), $settings); |
| 101 |
case 'text': |
| 102 |
default: |
| 103 |
return $value; |
| 104 |
} |
| 105 |
|
| 106 |
return false; |
| 107 |
} |
| 108 |
|
| 109 |
public function log($field_id) |
| 110 |
{ |
| 111 |
$this->_addon_data->log($field_id, $this->_custom_fields->get_prefix()); |
| 112 |
} |
| 113 |
} |
| 114 |
|