| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace ImportWP\Common\Exporter\Mapper; |
| 4 | 4 | |
| 5 | +use ImportWP\Common\Exporter\ExporterRecord; | |
| 5 | 6 | use ImportWP\Common\Exporter\MapperInterface; |
| 6 | 7 | |
| 7 | 8 | class PostMapper extends AbstractMapper implements MapperInterface |
| 8 | 9 | { |
| @@ -15,8 +16,10 @@ | ||
| 15 | 16 | |
| 16 | 17 | public function __construct($post_type = 'post') |
| 17 | 18 | { |
| 18 | 19 | $this->post_type = $post_type; |
| 20 | + | |
| 21 | + add_filter('iwp/exporter_record/post', [$this, 'get_record_data'], 10, 3); | |
| 19 | 22 | } |
| 20 | 23 | |
| 21 | 24 | public function get_core_fields() |
| 22 | 25 | { |
| @@ -62,15 +65,15 @@ | ||
| 62 | 65 | global $wpdb; |
| 63 | 66 | |
| 64 | 67 | $fields = [ |
| 65 | 68 | 'key' => 'main', |
| 66 | - 'label' => 'Post', | |
| 69 | + 'label' => __('Post', 'jc-importer'), | |
| 67 | 70 | 'loop' => true, |
| 68 | 71 | 'fields' => [], |
| 69 | 72 | 'children' => [ |
| 70 | 73 | 'author' => [ |
| 71 | 74 | 'key' => 'author', |
| 72 | - 'label' => 'Author', | |
| 75 | + 'label' => __('Author', 'jc-importer'), | |
| 73 | 76 | 'loop' => false, |
| 74 | 77 | 'fields' => [], |
| 75 | 78 | 'children' => [] |
| 76 | 79 | ], |
| @@ -75,9 +78,9 @@ | ||
| 75 | 78 | 'children' => [] |
| 76 | 79 | ], |
| 77 | 80 | 'image' => [ |
| 78 | 81 | 'key' => 'image', |
| 79 | - 'label' => 'Featured Image', | |
| 82 | + 'label' => __('Featured Image', 'jc-importer'), | |
| 80 | 83 | 'loop' => false, |
| 81 | 84 | 'fields' => ['id', 'url', 'title', 'alt', 'caption', 'description'], |
| 82 | 85 | 'children' => [] |
| 83 | 86 | ], |
| @@ -82,9 +85,9 @@ | ||
| 82 | 85 | 'children' => [] |
| 83 | 86 | ], |
| 84 | 87 | 'parent' => [ |
| 85 | 88 | 'key' => 'parent', |
| 86 | - 'label' => 'Parent', | |
| 89 | + 'label' => __('Parent', 'jc-importer'), | |
| 87 | 90 | 'loop' => false, |
| 88 | 91 | 'fields' => ['id', 'name', 'slug'], |
| 89 | 92 | 'children' => [] |
| 90 | 93 | ], |
| @@ -89,9 +92,9 @@ | ||
| 89 | 92 | 'children' => [] |
| 90 | 93 | ], |
| 91 | 94 | 'custom_fields' => [ |
| 92 | 95 | 'key' => 'custom_fields', |
| 93 | - 'label' => 'Custom Fields', | |
| 96 | + 'label' => __('Custom Fields', 'jc-importer'), | |
| 94 | 97 | 'loop' => true, |
| 95 | 98 | 'loop_fields' => ['meta_key', 'meta_value'], |
| 96 | 99 | 'fields' => [], |
| 97 | 100 | 'children' => [] |
| @@ -153,15 +156,15 @@ | ||
| 153 | 156 | ]; |
| 154 | 157 | } |
| 155 | 158 | |
| 156 | 159 | $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/post_type/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type); |
| 157 | - $fields = apply_filters('iwp/exporter/post_type/fields', $fields, $this->post_type); | |
| 160 | + $fields = apply_filters('iwp/exporter/post_type/fields', $fields, $this->get_post_types_array()); | |
| 158 | 161 | |
| 159 | 162 | if (in_array('attachment', $post_types)) { |
| 160 | 163 | $fields['fields'][] = 'url'; |
| 161 | 164 | } |
| 162 | 165 | |
| 163 | - return $fields; | |
| 166 | + return $this->parse_fields($fields); | |
| 164 | 167 | } |
| 165 | 168 | |
| 166 | 169 | |
| 167 | 170 | public function have_records($exporter_id) |
| @@ -204,73 +207,83 @@ | ||
| 204 | 207 | { |
| 205 | 208 | return $this->items; |
| 206 | 209 | } |
| 207 | 210 | |
| 208 | - | |
| 209 | - public function setup($i) | |
| 211 | + public function get_record_data($value, $key, $record) | |
| 210 | 212 | { |
| 211 | - $post = get_post($this->items[$i], ARRAY_A); | |
| 212 | - if (!$post) { | |
| 213 | - return false; | |
| 214 | - } | |
| 213 | + switch ($key) { | |
| 214 | + case 'post_thumbnail': | |
| 215 | + $value = wp_get_attachment_url(get_post_thumbnail_id($record['ID'])); | |
| 216 | + break; | |
| 217 | + case 'image': | |
| 215 | 218 | |
| 216 | - $this->record = $post; | |
| 217 | - $this->record['post_thumbnail'] = wp_get_attachment_url(get_post_thumbnail_id($this->record['ID'])); | |
| 218 | - $this->record['custom_fields'] = get_post_meta($post['ID']); | |
| 219 | + $value = [ | |
| 220 | + 'id' => '', | |
| 221 | + 'url' => '', | |
| 222 | + 'title' => '', | |
| 223 | + 'alt' => '', | |
| 224 | + 'caption' => '', | |
| 225 | + 'description' => '' | |
| 226 | + ]; | |
| 219 | 227 | |
| 220 | - $this->record = $this->modify_custom_field_data($this->record, 'post_type'); | |
| 228 | + $thumbnail_id = intval(get_post_thumbnail_id($record['ID'])); | |
| 229 | + if ($thumbnail_id > 0) { | |
| 230 | + $attachment = get_post($thumbnail_id, ARRAY_A); | |
| 231 | + if ($attachment) { | |
| 221 | 232 | |
| 222 | - $user = get_userdata($post['post_author']); | |
| 223 | - if ($user) { | |
| 224 | - $this->record['author'] = (array)$user->data; | |
| 233 | + $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true); | |
| 234 | + $value = [ | |
| 235 | + 'id' => $thumbnail_id, | |
| 236 | + 'url' => wp_get_attachment_url($thumbnail_id), | |
| 237 | + 'title' => $attachment['post_title'], | |
| 238 | + 'alt' => $alt, | |
| 239 | + 'caption' => $attachment['post_excerpt'], | |
| 240 | + 'description' => $attachment['post_content'] | |
| 241 | + ]; | |
| 242 | + } | |
| 243 | + } | |
| 244 | + break; | |
| 245 | + case 'custom_fields': | |
| 246 | + $value = get_post_meta($record['ID']); | |
| 247 | + $value = $this->modify_custom_field_data($value, 'post_type'); | |
| 248 | + break; | |
| 249 | + case 'author': | |
| 250 | + $user = get_userdata($record['post_author']); | |
| 251 | + if ($user) { | |
| 252 | + $value = (array)$user->data; | |
| 253 | + } | |
| 254 | + break; | |
| 255 | + case 'parent': | |
| 256 | + $parent = get_post_parent($record['ID']); | |
| 257 | + if ($parent) { | |
| 258 | + $value = [ | |
| 259 | + 'id' => $parent->ID, | |
| 260 | + 'name' => $parent->post_title, | |
| 261 | + 'slug' => $parent->post_name | |
| 262 | + ]; | |
| 263 | + } else { | |
| 264 | + $value = [ | |
| 265 | + 'id' => '', | |
| 266 | + 'name' => '', | |
| 267 | + 'slug' => '' | |
| 268 | + ]; | |
| 269 | + } | |
| 270 | + break; | |
| 271 | + case 'url': | |
| 272 | + if ($record['post_type'] === 'attachment') { | |
| 273 | + $value = wp_get_attachment_url($record['ID']); | |
| 274 | + } | |
| 275 | + break; | |
| 225 | 276 | } |
| 226 | 277 | |
| 227 | - $thumbnail_id = intval(get_post_thumbnail_id($this->record['ID'])); | |
| 228 | - if ($thumbnail_id > 0) { | |
| 278 | + if (preg_match('/^tax_(.*?)$/', $key, $tax_match) === 1) { | |
| 279 | + $taxonomy = $tax_match[1]; | |
| 229 | 280 | |
| 230 | - $attachment = get_post($thumbnail_id, ARRAY_A); | |
| 231 | - $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true); | |
| 281 | + $taxonomies = get_object_taxonomies($record['post_type'], 'objects'); | |
| 282 | + if (!in_array($taxonomy, array_keys($taxonomies))) { | |
| 283 | + return $value; | |
| 284 | + } | |
| 232 | 285 | |
| 233 | - $this->record['image'] = [ | |
| 234 | - 'id' => $thumbnail_id, | |
| 235 | - 'url' => wp_get_attachment_url($thumbnail_id), | |
| 236 | - 'title' => $attachment['post_title'], | |
| 237 | - 'alt' => $alt, | |
| 238 | - 'caption' => $attachment['post_excerpt'], | |
| 239 | - 'description' => $attachment['post_content'] | |
| 240 | - ]; | |
| 241 | - } else { | |
| 242 | - $this->record['image'] = [ | |
| 243 | - 'id' => '', | |
| 244 | - 'url' => '', | |
| 245 | - 'title' => '', | |
| 246 | - 'alt' => '', | |
| 247 | - 'caption' => '', | |
| 248 | - 'description' => '' | |
| 249 | - ]; | |
| 250 | - } | |
| 251 | - | |
| 252 | - $parent = get_post_parent($this->record['ID']); | |
| 253 | - if ($parent) { | |
| 254 | - $this->record['parent'] = [ | |
| 255 | - 'id' => $parent->ID, | |
| 256 | - 'name' => $parent->post_title, | |
| 257 | - 'slug' => $parent->post_name | |
| 258 | - ]; | |
| 259 | - } else { | |
| 260 | - $this->record['parent'] = [ | |
| 261 | - 'id' => '', | |
| 262 | - 'name' => '', | |
| 263 | - 'slug' => '' | |
| 264 | - ]; | |
| 265 | - } | |
| 266 | - | |
| 267 | - // taxonomies | |
| 268 | - $taxonomies = get_object_taxonomies($this->record['post_type'], 'objects'); | |
| 269 | - foreach (array_keys($taxonomies) as $taxonomy) { | |
| 270 | - $tmp = []; | |
| 271 | - | |
| 272 | - $terms = wp_get_object_terms($post['ID'], $taxonomy); | |
| 273 | 286 | $tmp = [ |
| 274 | 287 | 'id' => [], |
| 275 | 288 | 'slug' => [], |
| 276 | 289 | 'name' => [], |
| @@ -277,8 +290,9 @@ | ||
| 277 | 290 | 'hierarchy::id' => [], |
| 278 | 291 | 'hierarchy::name' => [], |
| 279 | 292 | 'hierarchy::slug' => [], |
| 280 | 293 | ]; |
| 294 | + $terms = wp_get_object_terms($record['ID'], $taxonomy); | |
| 281 | 295 | |
| 282 | 296 | if (!empty($terms)) { |
| 283 | 297 | foreach ($terms as $term) { |
| 284 | 298 | |
| @@ -289,9 +303,8 @@ | ||
| 289 | 303 | $tmp['slug'][] = $term->slug; |
| 290 | 304 | $tmp['name'][] = $term->name; |
| 291 | 305 | |
| 292 | 306 | $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy'); |
| 293 | - // $ancestor_ids = array_reverse($ancestor_ids); | |
| 294 | 307 | |
| 295 | 308 | $ancestor_tmp = [ |
| 296 | 309 | 'id' => [$term->term_id], |
| 297 | 310 | 'name' => [$term->name], |
| @@ -320,15 +333,24 @@ | ||
| 320 | 333 | $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']); |
| 321 | 334 | } |
| 322 | 335 | } |
| 323 | 336 | |
| 324 | - $this->record['tax_' . $taxonomy] = $tmp; | |
| 337 | + $value = $tmp; | |
| 325 | 338 | } |
| 326 | 339 | |
| 327 | - if ($post['post_type'] === 'attachment') { | |
| 328 | - $this->record['url'] = wp_get_attachment_url($post['ID']); | |
| 340 | + return $value; | |
| 341 | + } | |
| 342 | + | |
| 343 | + | |
| 344 | + public function setup($i) | |
| 345 | + { | |
| 346 | + $post = get_post($this->items[$i], ARRAY_A); | |
| 347 | + if (!$post) { | |
| 348 | + return false; | |
| 329 | 349 | } |
| 330 | 350 | |
| 351 | + // Add the base post item | |
| 352 | + $this->record = new ExporterRecord($post, 'post'); | |
| 331 | 353 | $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type); |
| 332 | 354 | |
| 333 | 355 | return true; |
| 334 | 356 | } |