PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.9.0
Import WP – CSV & XML Import Export for WordPress v2.9.0
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.9.0, at class/Common/Exporter/Mapper/AbstractMapper.php

196 lines 6.0 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 $right = $row['right'];
71 $right_parts = array_map('trim', explode(',', $right));
72
73 switch ($row['condition']) {
74 case 'equal':
75 if ($left != $right) {
76 $result = false;
77 }
78 break;
79 case 'contains':
80 if (stripos($left, $right) === false) {
81 $result = false;
82 }
83 break;
84 case 'in':
85 if (!in_array($left, $right_parts)) {
86 $result = false;
87 }
88 break;
89 case 'not-equal':
90 if ($left == $right) {
91 $result = false;
92 }
93 break;
94 case 'not-contains':
95 if (stripos($left, $right) !== false) {
96 $result = false;
97 }
98 break;
99 case 'not-in':
100 if (in_array($left, $right_parts)) {
101 $result = false;
102 }
103 break;
104 }
105 }
106
107 if ($result) {
108 return true;
109 }
110 }
111
112
113 return $result;
114 }
115
116 public function record($index = 0)
117 {
118 if (isset($this->records[$index])) {
119 return $this->records[$index];
120 }
121
122 return $this->record;
123 }
124
125 public function get_records()
126 {
127 return $this->items;
128 }
129
130 public function set_records($records)
131 {
132 $this->items = $records;
133 }
134
135 public function modify_custom_field_data($custom_fields, $type)
136 {
137 $custom_file_fields = apply_filters('iwp/exporter/' . $type . '/custom_file_id_fields', []);
138 if (!empty($custom_file_fields)) {
139 foreach ($custom_file_fields as $custom_field) {
140
141 if (!isset($custom_fields[$custom_field]) && !empty($custom_fields[$custom_field])) {
142 continue;
143 }
144
145 $data = [
146 'id' => [],
147 'url' => [],
148 'title' => [],
149 'alt' => [],
150 'caption' => [],
151 'description' => [],
152 ];
153
154 foreach ($custom_fields[$custom_field] as $custom_field_value) {
155
156 $custom_field_data = maybe_unserialize($custom_field_value);
157 if (is_string($custom_field_data)) {
158 $custom_field_data = explode(',', $custom_field_data);
159 }
160
161 if (empty($custom_field_data)) {
162 continue;
163 }
164
165 foreach ($custom_field_data as $possible_attachment_id) {
166
167 $attachment_id = intval($possible_attachment_id);
168 if ($attachment_id === 0) {
169 continue;
170 }
171
172 $attachment = get_post($attachment_id, ARRAY_A);
173 $alt = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
174
175 $data['id'][] = $attachment_id;
176 $data['url'][] = wp_get_attachment_url($attachment_id);
177 $data['title'][] = $attachment['post_title'];
178 $data['alt'][] = $alt;
179 $data['caption'][] = $attachment['post_excerpt'];
180 $data['description'][] = $attachment['post_content'];
181 }
182 }
183
184 $custom_fields[$custom_field . '::id'] = $data['id'];
185 $custom_fields[$custom_field . '::url'] = $data['url'];
186 $custom_fields[$custom_field . '::title'] = $data['title'];
187 $custom_fields[$custom_field . '::alt'] = $data['alt'];
188 $custom_fields[$custom_field . '::caption'] = $data['caption'];
189 $custom_fields[$custom_field . '::description'] = $data['description'];
190 }
191 }
192
193 return $custom_fields;
194 }
195 }
196