| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Exporter\Mapper; |
| 4 |
|
| 5 |
use ImportWP\Common\Exporter\MapperInterface; |
| 6 |
|
| 7 |
class PostMapper extends AbstractMapper implements MapperInterface |
| 8 |
{ |
| 9 |
|
| 10 |
/** |
| 11 |
* @var WP_Query |
| 12 |
*/ |
| 13 |
protected $query; |
| 14 |
protected $post_type; |
| 15 |
|
| 16 |
public function __construct($post_type = 'post') |
| 17 |
{ |
| 18 |
$this->post_type = $post_type; |
| 19 |
} |
| 20 |
|
| 21 |
public function get_core_fields() |
| 22 |
{ |
| 23 |
return array( |
| 24 |
'ID', |
| 25 |
'post_author', |
| 26 |
'post_date', |
| 27 |
'post_date_gmt', |
| 28 |
'post_content', |
| 29 |
'post_title', |
| 30 |
'post_excerpt', |
| 31 |
'post_status', |
| 32 |
'comment_status', |
| 33 |
'ping_status', |
| 34 |
'post_password', |
| 35 |
'post_name', |
| 36 |
'to_ping', |
| 37 |
'pinged', |
| 38 |
'post_modified', |
| 39 |
'post_modified_gmt', |
| 40 |
'post_content_filtered', |
| 41 |
'post_parent', |
| 42 |
'guid', |
| 43 |
'menu_order', |
| 44 |
'post_type', |
| 45 |
'post_mime_type', |
| 46 |
'comment_count', |
| 47 |
); |
| 48 |
} |
| 49 |
|
| 50 |
public function get_post_types_array() |
| 51 |
{ |
| 52 |
$post_types = is_string($this->post_type) ? explode(',', $this->post_type) : (array)$this->post_type; |
| 53 |
$post_types = array_values(array_filter(array_map('trim', $post_types))); |
| 54 |
return $post_types; |
| 55 |
} |
| 56 |
|
| 57 |
public function get_fields() |
| 58 |
{ |
| 59 |
/** |
| 60 |
* @var \WPDB |
| 61 |
*/ |
| 62 |
global $wpdb; |
| 63 |
|
| 64 |
$fields = [ |
| 65 |
'key' => 'main', |
| 66 |
'label' => 'Post', |
| 67 |
'loop' => true, |
| 68 |
'fields' => [], |
| 69 |
'children' => [ |
| 70 |
'author' => [ |
| 71 |
'key' => 'author', |
| 72 |
'label' => 'Author', |
| 73 |
'loop' => false, |
| 74 |
'fields' => [], |
| 75 |
'children' => [] |
| 76 |
], |
| 77 |
'image' => [ |
| 78 |
'key' => 'image', |
| 79 |
'label' => 'Featured Image', |
| 80 |
'loop' => false, |
| 81 |
'fields' => ['id', 'url', 'title', 'alt', 'caption', 'description'], |
| 82 |
'children' => [] |
| 83 |
], |
| 84 |
'parent' => [ |
| 85 |
'key' => 'parent', |
| 86 |
'label' => 'Parent', |
| 87 |
'loop' => false, |
| 88 |
'fields' => ['id', 'name', 'slug'], |
| 89 |
'children' => [] |
| 90 |
], |
| 91 |
'custom_fields' => [ |
| 92 |
'key' => 'custom_fields', |
| 93 |
'label' => 'Custom Fields', |
| 94 |
'loop' => true, |
| 95 |
'loop_fields' => ['meta_key', 'meta_value'], |
| 96 |
'fields' => [], |
| 97 |
'children' => [] |
| 98 |
] |
| 99 |
] |
| 100 |
|
| 101 |
]; |
| 102 |
|
| 103 |
$fields['fields'] = $this->get_core_fields(); |
| 104 |
|
| 105 |
$post_types = $this->get_post_types_array(); |
| 106 |
|
| 107 |
// add post_thumbnail |
| 108 |
if (post_type_supports($post_types[0], 'thumbnail')) { |
| 109 |
$fields['fields'][] = 'post_thumbnail'; |
| 110 |
} |
| 111 |
|
| 112 |
// post author |
| 113 |
$fields['children']['author']['fields'][] = 'ID'; |
| 114 |
$fields['children']['author']['fields'][] = 'user_login'; |
| 115 |
$fields['children']['author']['fields'][] = 'user_nicename'; |
| 116 |
$fields['children']['author']['fields'][] = 'user_email'; |
| 117 |
$fields['children']['author']['fields'][] = 'user_url'; |
| 118 |
$fields['children']['author']['fields'][] = 'display_name'; |
| 119 |
|
| 120 |
// post_meta |
| 121 |
$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))); |
| 122 |
$custom_file_fields = apply_filters('iwp/exporter/post_type/custom_file_id_fields', []); |
| 123 |
foreach ($meta_fields as $field) { |
| 124 |
|
| 125 |
$fields['children']['custom_fields']['fields'][] = $field; |
| 126 |
|
| 127 |
if (in_array($field, $custom_file_fields)) { |
| 128 |
$fields['children']['custom_fields']['fields'][] = $field . '::id'; |
| 129 |
$fields['children']['custom_fields']['fields'][] = $field . '::url'; |
| 130 |
$fields['children']['custom_fields']['fields'][] = $field . '::title'; |
| 131 |
$fields['children']['custom_fields']['fields'][] = $field . '::alt'; |
| 132 |
$fields['children']['custom_fields']['fields'][] = $field . '::caption'; |
| 133 |
$fields['children']['custom_fields']['fields'][] = $field . '::description'; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// taxonomies |
| 138 |
$taxonomies = get_object_taxonomies($post_types[0], 'objects'); |
| 139 |
foreach ($taxonomies as $tax) { |
| 140 |
$fields['children']['tax_' . $tax->name] = [ |
| 141 |
'key' => 'tax_' . $tax->name, |
| 142 |
'label' => $tax->label, |
| 143 |
'loop' => true, |
| 144 |
'loop_fields' => ['id', 'name', 'slug'], |
| 145 |
'fields' => [ |
| 146 |
'name', |
| 147 |
'slug', |
| 148 |
'id', |
| 149 |
'hierarchy::id', |
| 150 |
'hierarchy::name', |
| 151 |
'hierarchy::slug', |
| 152 |
] |
| 153 |
]; |
| 154 |
} |
| 155 |
|
| 156 |
$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); |
| 158 |
|
| 159 |
if (in_array('attachment', $post_types)) { |
| 160 |
$fields['fields'][] = 'url'; |
| 161 |
} |
| 162 |
|
| 163 |
return $fields; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
public function have_records($exporter_id) |
| 168 |
{ |
| 169 |
$post_types = $this->get_post_types_array(); |
| 170 |
|
| 171 |
$query_args = []; |
| 172 |
$query_args = apply_filters('iwp/exporter/post_query', $query_args); |
| 173 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/post_query', $exporter_id), $query_args); |
| 174 |
|
| 175 |
foreach ($post_types as $post_type) { |
| 176 |
$query_args = apply_filters(sprintf('iwp/exporter/post_query/%s', $post_type), $query_args); |
| 177 |
$query_args = apply_filters(sprintf('iwp/exporter/%d/post_query/%s', $exporter_id, $post_type), $query_args); |
| 178 |
} |
| 179 |
|
| 180 |
$query_args = wp_parse_args($query_args, [ |
| 181 |
'post_type' => $post_types, |
| 182 |
'posts_per_page' => -1, |
| 183 |
'post_status' => 'any, trash, future', |
| 184 |
'fields' => 'ids', |
| 185 |
'cache_results' => false, |
| 186 |
'update_post_meta_cache' => false, |
| 187 |
'update_post_term_cache' => false, |
| 188 |
'no_found_rows' => true, |
| 189 |
'order' => 'ASC' |
| 190 |
]); |
| 191 |
|
| 192 |
$this->query = new \WP_Query($query_args); |
| 193 |
$this->items = $this->query->posts; |
| 194 |
|
| 195 |
return $this->found_records() > 0; |
| 196 |
} |
| 197 |
|
| 198 |
public function found_records() |
| 199 |
{ |
| 200 |
return count($this->items); |
| 201 |
} |
| 202 |
|
| 203 |
public function get_records() |
| 204 |
{ |
| 205 |
return $this->items; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
public function setup($i) |
| 210 |
{ |
| 211 |
$post = get_post($this->items[$i], ARRAY_A); |
| 212 |
if (!$post) { |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 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 |
|
| 220 |
$this->record = $this->modify_custom_field_data($this->record, 'post_type'); |
| 221 |
|
| 222 |
$user = get_userdata($post['post_author']); |
| 223 |
if ($user) { |
| 224 |
$this->record['author'] = (array)$user->data; |
| 225 |
} |
| 226 |
|
| 227 |
$thumbnail_id = intval(get_post_thumbnail_id($this->record['ID'])); |
| 228 |
if ($thumbnail_id > 0) { |
| 229 |
|
| 230 |
$attachment = get_post($thumbnail_id, ARRAY_A); |
| 231 |
$alt = get_post_meta($thumbnail_id, '_wp_attachment_image_alt', true); |
| 232 |
|
| 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 |
$tmp = [ |
| 274 |
'id' => [], |
| 275 |
'slug' => [], |
| 276 |
'name' => [], |
| 277 |
'hierarchy::id' => [], |
| 278 |
'hierarchy::name' => [], |
| 279 |
'hierarchy::slug' => [], |
| 280 |
]; |
| 281 |
|
| 282 |
if (!empty($terms)) { |
| 283 |
foreach ($terms as $term) { |
| 284 |
|
| 285 |
/** |
| 286 |
* @var \WP_Term $term |
| 287 |
*/ |
| 288 |
$tmp['id'][] = $term->term_id; |
| 289 |
$tmp['slug'][] = $term->slug; |
| 290 |
$tmp['name'][] = $term->name; |
| 291 |
|
| 292 |
$ancestor_ids = get_ancestors($term->term_id, $taxonomy, 'taxonomy'); |
| 293 |
// $ancestor_ids = array_reverse($ancestor_ids); |
| 294 |
|
| 295 |
$ancestor_tmp = [ |
| 296 |
'id' => [$term->term_id], |
| 297 |
'name' => [$term->name], |
| 298 |
'slug' => [$term->slug] |
| 299 |
]; |
| 300 |
|
| 301 |
foreach ($ancestor_ids as $ancestor_id) { |
| 302 |
|
| 303 |
$ancestor = get_term($ancestor_id, $taxonomy); |
| 304 |
if (is_wp_error($ancestor)) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
|
| 308 |
$ancestor_tmp['id'][] = $ancestor->term_id; |
| 309 |
$ancestor_tmp['name'][] = $ancestor->name; |
| 310 |
$ancestor_tmp['slug'][] = $ancestor->slug; |
| 311 |
} |
| 312 |
|
| 313 |
$ancestor_tmp['id'] = array_reverse($ancestor_tmp['id']); |
| 314 |
$tmp['hierarchy::id'][] = implode(' > ', $ancestor_tmp['id']); |
| 315 |
|
| 316 |
$ancestor_tmp['name'] = array_reverse($ancestor_tmp['name']); |
| 317 |
$tmp['hierarchy::name'][] = implode(' > ', $ancestor_tmp['name']); |
| 318 |
|
| 319 |
$ancestor_tmp['slug'] = array_reverse($ancestor_tmp['slug']); |
| 320 |
$tmp['hierarchy::slug'][] = implode(' > ', $ancestor_tmp['slug']); |
| 321 |
} |
| 322 |
} |
| 323 |
|
| 324 |
$this->record['tax_' . $taxonomy] = $tmp; |
| 325 |
} |
| 326 |
|
| 327 |
if ($post['post_type'] === 'attachment') { |
| 328 |
$this->record['url'] = wp_get_attachment_url($post['ID']); |
| 329 |
} |
| 330 |
|
| 331 |
$this->record = apply_filters('iwp/exporter/post_type/setup_data', $this->record, $this->post_type); |
| 332 |
|
| 333 |
return true; |
| 334 |
} |
| 335 |
} |
| 336 |
|