| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\Mapper; |
| 4 |
|
| 5 |
abstract class AbstractMapper |
| 6 |
{ |
| 7 |
protected $filters; |
| 8 |
protected $record = []; |
| 9 |
public $records = []; |
| 10 |
public $items = []; |
| 11 |
|
| 12 |
public function get_value($column, $template_data = null) |
| 13 |
{ |
| 14 |
if (is_null($template_data)) { |
| 15 |
$template_data = $this->record(); |
| 16 |
} |
| 17 |
|
| 18 |
$parts = explode('.', $column); |
| 19 |
if (count($parts) == 2) { |
| 20 |
|
| 21 |
// handle looped data |
| 22 |
if (isset($template_data[$parts[0]], $template_data[$parts[0]][0], $template_data[$parts[0]][0][$parts[1]])) { |
| 23 |
return array_reduce($template_data[$parts[0]], function ($carry, $item) use ($parts) { |
| 24 |
if (isset($item[$parts[1]])) { |
| 25 |
$carry[] = $item[$parts[1]]; |
| 26 |
} |
| 27 |
return $carry; |
| 28 |
}, []); |
| 29 |
} |
| 30 |
|
| 31 |
// handled grouped data or single |
| 32 |
return isset($template_data[$parts[0]], $template_data[$parts[0]][$parts[1]]) ? $template_data[$parts[0]][$parts[1]] : ''; |
| 33 |
} |
| 34 |
|
| 35 |
return isset($template_data[$column]) ? $template_data[$column] : ''; |
| 36 |
} |
| 37 |
|
| 38 |
public function set_filters($filters = []) |
| 39 |
{ |
| 40 |
$this->filters = $filters; |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Filter record |
| 45 |
* |
| 46 |
* @param array $post |
| 47 |
* @return boolean |
| 48 |
*/ |
| 49 |
public function filter() |
| 50 |
{ |
| 51 |
$result = false; |
| 52 |
|
| 53 |
if (empty($this->filters)) { |
| 54 |
return $result; |
| 55 |
} |
| 56 |
|
| 57 |
foreach ($this->filters as $group) { |
| 58 |
|
| 59 |
$result = true; |
| 60 |
|
| 61 |
if (empty($group)) { |
| 62 |
continue; |
| 63 |
} |
| 64 |
|
| 65 |
foreach ($group as $row) { |
| 66 |
|
| 67 |
$left = $this->get_value($row['left']); |
| 68 |
$right = $row['right']; |
| 69 |
$right_parts = array_map('trim', explode(',', $right)); |
| 70 |
|
| 71 |
switch ($row['condition']) { |
| 72 |
case 'equal': |
| 73 |
if ($left != $right) { |
| 74 |
$result = false; |
| 75 |
} |
| 76 |
break; |
| 77 |
case 'contains': |
| 78 |
if (stripos($left, $right) === false) { |
| 79 |
$result = false; |
| 80 |
} |
| 81 |
break; |
| 82 |
case 'in': |
| 83 |
if (!in_array($left, $right_parts)) { |
| 84 |
$result = false; |
| 85 |
} |
| 86 |
break; |
| 87 |
case 'not-equal': |
| 88 |
if ($left == $right) { |
| 89 |
$result = false; |
| 90 |
} |
| 91 |
break; |
| 92 |
case 'not-contains': |
| 93 |
if (stripos($left, $right) !== false) { |
| 94 |
$result = false; |
| 95 |
} |
| 96 |
break; |
| 97 |
case 'not-in': |
| 98 |
if (in_array($left, $right_parts)) { |
| 99 |
$result = false; |
| 100 |
} |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
if ($result) { |
| 106 |
return true; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
return $result; |
| 112 |
} |
| 113 |
|
| 114 |
public function record($index = 0) |
| 115 |
{ |
| 116 |
if (isset($this->records[$index])) { |
| 117 |
return $this->records[$index]; |
| 118 |
} |
| 119 |
|
| 120 |
return $this->record; |
| 121 |
} |
| 122 |
|
| 123 |
public function get_records() |
| 124 |
{ |
| 125 |
return $this->items; |
| 126 |
} |
| 127 |
|
| 128 |
public function set_records($records) |
| 129 |
{ |
| 130 |
$this->items = $records; |
| 131 |
} |
| 132 |
|
| 133 |
public function modify_custom_field_data($record, $type) |
| 134 |
{ |
| 135 |
$custom_file_fields = apply_filters('iwp/exporter/' . $type . '/custom_file_id_fields', []); |
| 136 |
if (!empty($custom_file_fields)) { |
| 137 |
foreach ($custom_file_fields as $custom_field) { |
| 138 |
|
| 139 |
if (!isset($record['custom_fields'][$custom_field]) && !empty($record['custom_fields'][$custom_field])) { |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
$data = [ |
| 144 |
'id' => [], |
| 145 |
'url' => [], |
| 146 |
'title' => [], |
| 147 |
'alt' => [], |
| 148 |
'caption' => [], |
| 149 |
'description' => [], |
| 150 |
]; |
| 151 |
|
| 152 |
foreach ($record['custom_fields'][$custom_field] as $custom_field_value) { |
| 153 |
|
| 154 |
$custom_field_data = maybe_unserialize($custom_field_value); |
| 155 |
if (is_string($custom_field_data)) { |
| 156 |
$custom_field_data = explode(',', $custom_field_data); |
| 157 |
} |
| 158 |
|
| 159 |
if (empty($custom_field_data)) { |
| 160 |
continue; |
| 161 |
} |
| 162 |
|
| 163 |
foreach ($custom_field_data as $possible_attachment_id) { |
| 164 |
|
| 165 |
$attachment_id = intval($possible_attachment_id); |
| 166 |
if ($attachment_id === 0) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
$attachment = get_post($attachment_id, ARRAY_A); |
| 171 |
$alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true); |
| 172 |
|
| 173 |
$data['id'][] = $attachment_id; |
| 174 |
$data['url'][] = wp_get_attachment_url($attachment_id); |
| 175 |
$data['title'][] = $attachment['post_title']; |
| 176 |
$data['alt'][] = $alt; |
| 177 |
$data['caption'][] = $attachment['post_excerpt']; |
| 178 |
$data['description'][] = $attachment['post_content']; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
$record['custom_fields'][$custom_field . '::id'] = $data['id']; |
| 183 |
$record['custom_fields'][$custom_field . '::url'] = $data['url']; |
| 184 |
$record['custom_fields'][$custom_field . '::title'] = $data['title']; |
| 185 |
$record['custom_fields'][$custom_field . '::alt'] = $data['alt']; |
| 186 |
$record['custom_fields'][$custom_field . '::caption'] = $data['caption']; |
| 187 |
$record['custom_fields'][$custom_field . '::description'] = $data['description']; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return $record; |
| 192 |
} |
| 193 |
} |
| 194 |
|