PluginProbe
Import WP – CSV & XML Import Export for WordPress / 2.8.0
Import WP – CSV & XML Import Export for WordPress v2.8.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 / Importer / Mapper / TermMapper.php

TermMapper.php in Import WP – CSV & XML Import Export for WordPress 2.8.0, at class/Common/Importer/Mapper/TermMapper.php

260 lines 7.9 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\Importer\Mapper;
4
5 use ImportWP\Common\Importer\Exception\MapperException;
6 use ImportWP\Common\Importer\MapperInterface;
7 use ImportWP\Common\Importer\ParsedData;
8
9 class TermMapper extends AbstractMapper implements MapperInterface
10 {
11 protected $_term_fields = array(
12 'term_id',
13 'alias_of',
14 'description',
15 'parent',
16 'slug',
17 'name'
18 );
19
20 public function setup()
21 {
22 }
23
24 public function teardown()
25 {
26 }
27
28 public function exists(ParsedData $data)
29 {
30 $fields = $data->getData('default');
31 $term = !empty($fields['name']) ? $fields['name'] : false;
32 $slug = !empty($fields['slug']) ? $fields['slug'] : false;
33 $term_id = !empty($fields['term_id']) ? $fields['term_id'] : false;
34 $taxonomy = $this->importer->getSetting('taxonomy');
35
36 // escape if required fields are not entered
37 if (false === $taxonomy || (false === $term && false === $slug && false === $term_id)) {
38 return false;
39 }
40
41 if (!empty($term_id)) {
42 $term = get_term_by('id', $term_id, $taxonomy);
43 } else if (!empty($slug) && 'yes' !== $data->getValue('slug', '_generated')) {
44 // use slug if its not been generated from name
45 $term = get_term_by('slug', $slug, $taxonomy);
46 } else if (!empty($term)) {
47 $term = get_term_by('name', $term, $taxonomy);
48 }
49 if ($term && isset($term->term_id)) {
50 $this->ID = $term->term_id;
51 return true;
52 }
53 return false;
54 }
55
56 public function insert(ParsedData $data)
57 {
58 $fields = $data->getData('default');
59
60 $this->ID = false;
61 $args = array();
62 $custom_fields = array();
63
64 // term_id Cant be inserted or updated, it is just used for reference
65 if (isset($fields['term_id'])) {
66 unset($fields['term_id']);
67 }
68
69 // escape if required fields are not entered
70 if (!isset($fields['name']) || empty($fields['name'])) {
71 throw new MapperException('Term name is missing.');
72 }
73
74 $term = !empty($fields['name']) ? $fields['name'] : false;
75 $taxonomy = $this->importer->getSetting('taxonomy');
76
77 if ($term && $taxonomy) {
78
79 unset($fields['name']);
80 unset($fields['taxonomy']);
81 foreach ($fields as $key => $value) {
82 //
83 if (empty($value)) {
84 continue;
85 }
86 if (in_array($key, $this->_term_fields)) {
87 $args[$key] = $value;
88 } else {
89 $custom_fields[$key] = $value;
90 }
91 }
92
93 // returns array('term_id' => #, 'taxonomy_id' => #)
94 $insert = wp_insert_term($term, $taxonomy, $args);
95 if (is_wp_error($insert)) {
96 throw new MapperException($insert->get_error_message());
97 }
98
99 $this->ID = $insert['term_id'];
100
101 // TODO: Do we merge in the custom fields, or do we process that in post_process
102 $this->template->process($this->ID, $data, $this->importer);
103
104 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
105
106 if (!is_wp_error($this->ID) && intval($this->ID) > 0 && !empty($custom_fields)) {
107 foreach ($custom_fields as $key => $value) {
108 if (is_array($value)) {
109 $this->clear_custom_field($this->ID, $key);
110 foreach ($value as $v) {
111 $this->add_custom_field($this->ID, $key, $v);
112 }
113 } else {
114 $this->update_custom_field($this->ID, $key, $value);
115 }
116 }
117 }
118 }
119
120 $this->add_version_tag();
121 $this->template->post_process($this->ID, $data);
122
123 return $this->ID;
124 }
125
126 public function update(ParsedData $data)
127 {
128 $fields = $data->getData('default');
129 $args = array();
130 $custom_fields = array();
131
132 // term_id Cant be inserted or updated, it is just used for reference
133 if (isset($fields['term_id'])) {
134 unset($fields['term_id']);
135 }
136
137 $taxonomy = $this->importer->getSetting('taxonomy');
138 foreach ($fields as $key => $value) {
139 //
140 if (empty($value)) {
141 continue;
142 }
143 if (in_array($key, $this->_term_fields)) {
144 $args[$key] = $value;
145 } else {
146 $custom_fields[$key] = $value;
147 }
148 }
149
150 // returns array('term_id' => #, 'taxonomy_id' => #)
151 $result = wp_update_term($this->ID, $taxonomy, $args);
152 if (is_wp_error($result)) {
153 throw new MapperException($result->get_error_message());
154 }
155
156 // TODO: Do we merge in the custom fields, or do we process that in post_process
157 $this->template->process($this->ID, $data, $this->importer);
158
159 // merge meta group
160 $custom_fields = array_merge($custom_fields, $data->getData('custom_fields'));
161
162 if (!empty($custom_fields)) {
163 foreach ($custom_fields as $key => $value) {
164 if (is_array($value)) {
165 $this->clear_custom_field($this->ID, $key);
166 foreach ($value as $v) {
167 $this->add_custom_field($this->ID, $key, $v);
168 }
169 } else {
170 $this->update_custom_field($this->ID, $key, $value);
171 }
172 }
173 }
174
175 $this->add_version_tag();
176 $this->template->post_process($this->ID, $data);
177
178 return $this->ID;
179 }
180
181 public function add_version_tag()
182 {
183 if ($this->is_session_tag_enabled()) {
184 $this->add_session_tag('t-' . $this->importer->getSetting('taxonomy'));
185 } else {
186 update_term_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId());
187 }
188 }
189
190 public function get_objects_for_removal()
191 {
192 if ($this->is_session_tag_enabled()) {
193 return $this->get_ids_without_session_tag('t-' . $this->importer->getSetting('taxonomy'));
194 } else {
195 $taxonomy = $this->importer->getSetting('taxonomy');
196 $terms = get_terms([
197 'taxonomy' => $taxonomy,
198 'fields' => 'ids',
199 'hide_empty' => false,
200 'meta_query' => [
201 [
202 'key' => '_iwp_session_' . $this->importer->getId(),
203 'value' => $this->importer->getStatusId(),
204 'compare' => '!='
205 ]
206 ]
207 ]);
208
209 if (!is_wp_error($terms)) {
210 return $terms;
211 }
212 }
213
214 return false;
215 }
216
217 public function delete($id)
218 {
219 wp_delete_term($id, $this->importer->getSetting('taxonomy'));
220
221 $this->remove_session_tag($id, 't-' . $this->importer->getSetting('taxonomy'));
222 }
223
224 /**
225 * Clear all post meta before adding custom field
226 */
227 public function clear_custom_field($term_id, $key)
228 {
229 delete_term_meta($term_id, $key);
230 }
231
232 /**
233 * Add custom field, allow for multiple records using the same key
234 *
235 * @param int $term_id
236 * @param string $key
237 * @param string $value
238 * @return void
239 */
240 public function add_custom_field($term_id, $key, $value)
241 {
242 // Stop double serialization
243 if (is_serialized($value)) {
244 $value = unserialize($value);
245 }
246
247 add_term_meta($term_id, $key, $value);
248 }
249
250 public function update_custom_field($id, $key, $value)
251 {
252 // Stop double serialization
253 if (is_serialized($value)) {
254 $value = unserialize($value);
255 }
256
257 update_term_meta($id, $key, $value);
258 }
259 }
260