| @@ -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' => [] |
| @@ -118,10 +121,21 @@ | ||
| 118 | 121 | $fields['children']['author']['fields'][] = 'display_name'; |
| 119 | 122 | |
| 120 | 123 | // post_meta |
| 121 | 124 | $meta_fields = $wpdb->get_col(sprintf("SELECT DISTINCT meta_key FROM " . $wpdb->postmeta . " WHERE post_id IN (SELECT DISTINCT ID FROM " . $wpdb->posts . " WHERE post_type IN ('%s'))", implode("','", $post_types))); |
| 125 | + $custom_file_fields = apply_filters('iwp/exporter/post_type/custom_file_id_fields', []); | |
| 122 | 126 | foreach ($meta_fields as $field) { |
| 127 | + | |
| 123 | 128 | $fields['children']['custom_fields']['fields'][] = $field; |
| 129 | + | |
| 130 | + if (in_array($field, $custom_file_fields)) { | |
| 131 | + $fields['children']['custom_fields']['fields'][] = $field . '::id'; | |
| 132 | + $fields['children']['custom_fields']['fields'][] = $field . '::url'; | |
| 133 | + $fields['children']['custom_fields']['fields'][] = $field . '::title'; | |
| 134 | + $fields['children']['custom_fields']['fields'][] = $field . '::alt'; | |
| 135 | + $fields['children']['custom_fields']['fields'][] = $field . '::caption'; | |
| 136 | + $fields['children']['custom_fields']['fields'][] = $field . '::description'; | |
| 137 | + } | |
| 124 | 138 | } |
| 125 | 139 | |
| 126 | 140 | // taxonomies |
| 127 | 141 | $taxonomies = get_object_taxonomies($post_types[0], 'objects'); |
| @@ -142,11 +156,15 @@ | ||
| 142 | 156 | ]; |
| 143 | 157 | } |
| 144 | 158 | |
| 145 | 159 | $fields['children']['custom_fields']['fields'] = apply_filters('iwp/exporter/post_type/custom_field_list', $fields['children']['custom_fields']['fields'], $this->post_type); |
| 146 | - $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()); | |
| 147 | 161 | |
| 148 | - return $fields; | |
| 162 | + if (in_array('attachment', $post_types)) { | |
| 163 | + $fields['fields'][] = 'url'; | |
| 164 | + } | |
| 165 | + | |
| 166 | + return $this->parse_fields($fields); | |
| 149 | 167 | } |
| 150 | 168 | |
| 151 | 169 | |
| 152 | 170 | public function have_records($exporter_id) |
| @@ -189,71 +207,83 @@ | ||
| 189 | 207 | { |
| 190 | 208 | return $this->items; |
| 191 | 209 | } |
| 192 | 210 | |
| 193 | - | |
| 194 | - public function setup($i) | |
| 211 | + public function get_record_data($value, $key, $record) | |
| 195 | 212 | { |
| 196 | - $post = get_post($this->items[$i], ARRAY_A); | |
| 197 | - if (!$post) { | |
| 198 | - return false; | |
| 199 | - } | |
| 213 | + switch ($key) { | |
| 214 | + case 'post_thumbnail': | |
| 215 | + $value = wp_get_attachment_url(get_post_thumbnail_id($record['ID'])); | |
| 216 | + break; | |
| 217 | + case 'image': | |
| 200 | 218 | |
| 201 | - $this->record = $post; | |
| 202 | - $this->record['post_thumbnail'] = wp_get_attachment_url(get_post_thumbnail_id($this->record['ID'])); | |
| 203 | - $this->record['custom_fields'] = get_post_meta($post['ID']); | |
| 219 | + $value = [ | |
| 220 | + 'id' => '', | |
| 221 | + 'url' => '', | |
| 222 | + 'title' => '', | |
| 223 | + 'alt' => '', | |
| 224 | + 'caption' => '', | |
| 225 | + 'description' => '' | |
| 226 | + ]; | |
| 204 | 227 | |
| 205 | - $user = get_userdata($post['post_author']); | |
| 206 | - if ($user) { | |
| 207 | - $this->record['author'] = (array)$user->data; | |
| 208 | - } | |
| 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) { | |
| 209 | 232 | |
| 210 | - $thumbnail_id = intval(get_post_thumbnail_id($this->record['ID'])); | |
| 211 | - if ($thumbnail_id > 0) { | |
| 212 | - | |
| 213 | - $attachment = get_post($thumbnail_id, ARRAY_A); | |
| 214 | - $alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true); | |
| 215 | - | |
| 216 | - $this->record['image'] = [ | |
| 217 | - 'id' => $thumbnail_id, | |
| 218 | - 'url' => wp_get_attachment_url($thumbnail_id), | |
| 219 | - 'title' => $attachment['post_title'], | |
| 220 | - 'alt' => $alt, | |
| 221 | - 'caption' => $attachment['post_excerpt'], | |
| 222 | - 'description' => $attachment['post_content'] | |
| 223 | - ]; | |
| 224 | - } else { | |
| 225 | - $this->record['image'] = [ | |
| 226 | - 'id' => '', | |
| 227 | - 'url' => '', | |
| 228 | - 'title' => '', | |
| 229 | - 'alt' => '', | |
| 230 | - 'caption' => '', | |
| 231 | - 'description' => '' | |
| 232 | - ]; | |
| 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; | |
| 233 | 276 | } |
| 234 | 277 | |
| 235 | - $parent = get_post_parent($this->record['ID']); | |
| 236 | - if ($parent) { | |
| 237 | - $this->record['parent'] = [ | |
| 238 | - 'id' => $parent->ID, | |
| 239 | - 'name' => $parent->post_title, | |
| 240 | - 'slug' => $parent->post_name | |
| 241 | - ]; | |
| 242 | - } else { | |
| 243 | - $this->record['parent'] = [ | |
| 244 | - 'id' => '', | |
| 245 | - 'name' => '', | |
| 246 | - 'slug' => '' | |
| 247 | - ]; | |
| 248 | - } | |
| 278 | + if (preg_match('/^tax_(.*?)$/', $key, $tax_match) === 1) { | |
| 279 | + $taxonomy = $tax_match[1]; | |
| 249 | 280 | |
| 250 | - // taxonomies | |
| 251 | - $taxonomies = get_object_taxonomies($this->record['post_type'], 'objects'); | |
| 252 | - foreach (array_keys($taxonomies) as $taxonomy) { | |
| 253 | - $tmp = []; | |
| 281 | + $taxonomies = get_object_taxonomies($record['post_type'], 'objects'); | |
| 282 | + if (!in_array($taxonomy, array_keys($taxonomies))) { | |
| 283 | + return $value; | |
| 284 | + } | |
| 254 | 285 | |
| 255 | - $terms = wp_get_object_terms($post['ID'], $taxonomy); | |
| 256 | 286 | $tmp = [ |
| 257 | 287 | 'id' => [], |
| 258 | 288 | 'slug' => [], |
| 259 | 289 | 'name' => [], |
| @@ -260,8 +290,9 @@ | ||
| 260 | 290 | 'hierarchy::id' => [], |
| 261 | 291 | 'hierarchy::name' => [], |
| 262 | 292 | 'hierarchy::slug' => [], |
| 263 | 293 | ]; |
| 294 | + $terms = wp_get_object_terms($record['ID'], $taxonomy); | |
| 264 | 295 | |
| 265 | 296 | if (!empty($terms)) { |
| 266 | 297 | foreach ($terms as $term) { |
| 267 | 298 | |
| @@ -272,9 +303,8 @@ | ||
| 272 | 303 | $tmp['slug'][] = $term->slug; |
| 273 | 304 | $tmp['name'][] = $term->name; |
| 274 | 305 | |
| 275 | 306 | $ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy'); |
| 276 | - // $ancestor_ids = array_reverse($ancestor_ids); | |
| 277 | 307 | |
| 278 | 308 | $ancestor_tmp = [ |
| 279 | 309 | 'id' => [$term->term_id], |
| 280 | 310 | 'name' => [$term->name], |
| @@ -303,11 +333,24 @@ | ||
| 303 | 333 | $tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']); |
| 304 | 334 | } |
| 305 | 335 | } |
| 306 | 336 | |
| 307 | - $this->record['tax_' . $taxonomy] = $tmp; | |
| 337 | + $value = $tmp; | |
| 308 | 338 | } |
| 309 | 339 | |
| 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; | |
| 349 | + } | |
| 350 | + | |
| 351 | + // Add the base post item | |
| 352 | + $this->record = new ExporterRecord($post, 'post'); | |
| 310 | 353 | $this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type); |
| 311 | 354 | |
| 312 | 355 | return true; |
| 313 | 356 | } |