PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.14.23
Import WP – CSV & XML Import Export for WordPress v2.14.23
2.15.1 2.15.0 2.14.24 2.14.23 2.7.0 2.7.1 2.7.10 2.7.11 2.7.12 2.7.13 2.7.14 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 2.8.0 2.8.1 2.8.2 2.8.3 2.9.0 2.9.1 All 144 releases
jc-importer / class / Common / Exporter / Mapper / AbstractMapper.php

AbstractMapper.php in Import WP – CSV & XML Import Export for WordPress 2.14.23, at class/Common/Exporter/Mapper/AbstractMapper.php

216 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // TODO: template_data should be class with arrayaccess to load data on demand.
15
16 if (is_null($template_data)) {
17 $template_data = $this->record();
18 }
19
20 $parts = explode('.', $column);
21 if (count($parts) == 2) {
22
23 // handle looped data
24 if (isset($template_data[$parts[0]], $template_data[$parts[0]][0], $template_data[$parts[0]][0][$parts[1]])) {
25 return array_reduce($template_data[$parts[0]], function ($carry, $item) use ($parts) {
26 if (isset($item[$parts[1]])) {
27 $carry[] = $item[$parts[1]];
28 }
29 return $carry;
30 }, []);
31 }
32
33 // handled grouped data or single
34 return isset($template_data[$parts[0]], $template_data[$parts[0]][$parts[1]]) ? $template_data[$parts[0]][$parts[1]] : '';
35 }
36
37 return isset($template_data[$column]) ? $template_data[$column] : '';
38 }
39
40 public function set_filters($filters = [])
41 {
42 $this->filters = $filters;
43 }
44
45 /**
46 * Filter record
47 *
48 * @param array $post
49 * @return boolean
50 */
51 public function filter()
52 {
53 $result = false;
54
55 if (empty($this->filters)) {
56 return $result;
57 }
58
59 foreach ($this->filters as $group) {
60
61 $result = true;
62
63 if (empty($group)) {
64 continue;
65 }
66
67 foreach ($group as $row) {
68
69 $left = $this->get_value($row['left']);
70
71 // left should always be a string
72 if (is_array($left)) {
73 $left = implode(',', $left);
74 }
75
76 $right = $row['right'];
77 $right_parts = array_map('trim', explode(',', $right));
78
79 switch ($row['condition']) {
80 case 'equal':
81 if ($left != $right) {
82 $result = false;
83 }
84 break;
85 case 'contains':
86 if (stripos($left, $right) === false) {
87 $result = false;
88 }
89 break;
90 case 'in':
91 if (!in_array($left, $right_parts)) {
92 $result = false;
93 }
94 break;
95 case 'not-equal':
96 if ($left == $right) {
97 $result = false;
98 }
99 break;
100 case 'not-contains':
101 if (stripos($left, $right) !== false) {
102 $result = false;
103 }
104 break;
105 case 'not-in':
106 if (in_array($left, $right_parts)) {
107 $result = false;
108 }
109 break;
110 }
111 }
112
113 if ($result) {
114 return true;
115 }
116 }
117
118
119 return $result;
120 }
121
122 public function record($index = 0)
123 {
124 if (isset($this->records[$index])) {
125 return $this->records[$index];
126 }
127
128 return $this->record;
129 }
130
131 public function get_records()
132 {
133 return $this->items;
134 }
135
136 public function set_records($records)
137 {
138 $this->items = $records;
139 }
140
141 public function modify_custom_field_data($custom_fields, $type)
142 {
143 $custom_file_fields = apply_filters('iwp/exporter/' . $type . '/custom_file_id_fields', []);
144 if (!empty($custom_file_fields)) {
145 foreach ($custom_file_fields as $custom_field) {
146
147 if (!isset($custom_fields[$custom_field]) && !empty($custom_fields[$custom_field])) {
148 continue;
149 }
150
151 $data = [
152 'id' => [],
153 'url' => [],
154 'title' => [],
155 'alt' => [],
156 'caption' => [],
157 'description' => [],
158 ];
159
160 foreach ($custom_fields[$custom_field] as $custom_field_value) {
161
162 $custom_field_data = maybe_unserialize($custom_field_value);
163 if (is_string($custom_field_data)) {
164 $custom_field_data = explode(',', $custom_field_data);
165 }
166
167 if (empty($custom_field_data)) {
168 continue;
169 }
170
171 foreach ($custom_field_data as $possible_attachment_id) {
172
173 $attachment_id = intval($possible_attachment_id);
174 if ($attachment_id === 0) {
175 continue;
176 }
177
178 $attachment = get_post($attachment_id, ARRAY_A);
179 $alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
180
181 $data['id'][] = $attachment_id;
182 $data['url'][] = wp_get_attachment_url($attachment_id);
183 $data['title'][] = $attachment['post_title'];
184 $data['alt'][] = $alt;
185 $data['caption'][] = $attachment['post_excerpt'];
186 $data['description'][] = $attachment['post_content'];
187 }
188 }
189
190 $custom_fields[$custom_field . '::id'] = $data['id'];
191 $custom_fields[$custom_field . '::url'] = $data['url'];
192 $custom_fields[$custom_field . '::title'] = $data['title'];
193 $custom_fields[$custom_field . '::alt'] = $data['alt'];
194 $custom_fields[$custom_field . '::caption'] = $data['caption'];
195 $custom_fields[$custom_field . '::description'] = $data['description'];
196 }
197 }
198
199 return $custom_fields;
200 }
201
202 public function parse_fields($fields)
203 {
204 $fields['fields'] = isset($fields['fields']) ? array_values($fields['fields']) : [];
205
206 if (isset($fields['children']) && !empty($fields['fields'])) {
207
208 foreach ($fields['children'] as $child_id => $child_data) {
209 $fields['children'][$child_id] = $this->parse_fields($child_data);
210 }
211 }
212
213 return $fields;
214 }
215 }
216