| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\Template; |
| 4 |
|
| 5 |
use ImportWP\Common\Importer\ParsedData; |
| 6 |
use ImportWP\Common\Importer\TemplateInterface; |
| 7 |
use ImportWP\EventHandler; |
| 8 |
|
| 9 |
class AttachmentTemplate extends Template implements TemplateInterface |
| 10 |
{ |
| 11 |
protected $name = 'Attachment'; |
| 12 |
protected $mapper = 'attachment'; |
| 13 |
private $virtual_fields = []; |
| 14 |
|
| 15 |
protected $field_map = [ |
| 16 |
'ID' => 'post.ID', |
| 17 |
'post_title' => 'post.post_title', |
| 18 |
'post_content' => 'post.post_content', |
| 19 |
'post_excerpt' => 'post.post_excerpt', |
| 20 |
'post_name' => 'post.post_name', |
| 21 |
'post_status' => 'post.post_status', |
| 22 |
'menu_order' => 'post.menu_order', |
| 23 |
'post_password' => 'post.post_password', |
| 24 |
'post_date' => 'post.post_date', |
| 25 |
'comment_status' => 'post.comment_status', |
| 26 |
'ping_status' => 'post.ping_status', |
| 27 |
'post_parent' => 'post._parent.parent', |
| 28 |
'_wp_attachment_image_alt' => 'post._wp_attachment_image_alt', |
| 29 |
]; |
| 30 |
|
| 31 |
protected $optional_fields = []; |
| 32 |
|
| 33 |
public function __construct(EventHandler $event_handler) |
| 34 |
{ |
| 35 |
parent::__construct($event_handler); |
| 36 |
|
| 37 |
$this->groups[] = 'post'; |
| 38 |
$this->default_template_options['unique_field'] = ['ID', 'post_name', 'src']; |
| 39 |
$this->default_template_options['post_type'] = 'attachment'; |
| 40 |
|
| 41 |
$this->field_options = array_merge($this->field_options, [ |
| 42 |
'post._parent.parent' => [$this, 'get_post_parent_options'], |
| 43 |
]); |
| 44 |
|
| 45 |
$this->optional_fields = array_keys($this->field_map); |
| 46 |
} |
| 47 |
|
| 48 |
public function register() |
| 49 |
{ |
| 50 |
return [ |
| 51 |
$this->register_group('Attachment', 'post', [ |
| 52 |
$this->register_field('ID', 'ID', [ |
| 53 |
'tooltip' => __('ID is only used to reference existing records', 'importwp') |
| 54 |
]), |
| 55 |
$this->register_field('Title', 'post_title', [ |
| 56 |
'tooltip' => __('Title of the post.', 'importwp') |
| 57 |
]), |
| 58 |
$this->register_field('Description', 'post_content', [ |
| 59 |
'tooltip' => __('Main WYSIWYG editor content of the post.', 'importwp') |
| 60 |
]), |
| 61 |
$this->register_field('Caption', 'post_excerpt', [ |
| 62 |
'tooltip' => __('A custom short extract for the post.', 'importwp') |
| 63 |
]), |
| 64 |
$this->register_field('Alt Text', '_wp_attachment_image_alt', [ |
| 65 |
'tooltip' => __('The slug is the user friendly and URL valid name of the post.', 'importwp') |
| 66 |
]), |
| 67 |
$this->register_field('Slug', 'post_name', [ |
| 68 |
'tooltip' => __('The slug is the user friendly and URL valid name of the post.', 'importwp') |
| 69 |
]), |
| 70 |
$this->register_attachment_fields('File', 'file', 'Media File', [], ['disabled_fields' => ['_meta', '_featured']]), |
| 71 |
$this->register_field('Date', 'post_date', [ |
| 72 |
'tooltip' => __('The date of the post , enter in the format "YYYY-MM-DD HH:ii:ss"', 'importwp') |
| 73 |
]), |
| 74 |
$this->register_group('Parent Settings', '_parent', [ |
| 75 |
$this->register_field('Parent', 'parent', [ |
| 76 |
'default' => '', |
| 77 |
'options' => 'callback', |
| 78 |
'tooltip' => __('Set this for the post it belongs to', 'importwp') |
| 79 |
]), |
| 80 |
$this->register_field('Parent Field Type', '_parent_type', [ |
| 81 |
'default' => 'id', |
| 82 |
'options' => [ |
| 83 |
['value' => 'id', 'label' => 'ID'], |
| 84 |
['value' => 'slug', 'label' => 'Slug'], |
| 85 |
['value' => 'name', 'label' => 'Name'], |
| 86 |
['value' => 'column', 'label' => 'Reference Column'], |
| 87 |
], |
| 88 |
'type' => 'select', |
| 89 |
'tooltip' => __('Select how the parent field should be handled', 'importwp') |
| 90 |
]), |
| 91 |
$this->register_field('Parent Reference Column', '_parent_ref', [ |
| 92 |
'condition' => ['_parent_type', '==', 'column'], |
| 93 |
'tooltip' => __('Select the column/node that the parent field is referencing', 'importwp') |
| 94 |
]) |
| 95 |
]), |
| 96 |
$this->register_field('Order', 'menu_order', [ |
| 97 |
'tooltip' => __('The order the post should be displayed in', 'importwp') |
| 98 |
]), |
| 99 |
$this->register_group('Author Settings', '_author', [ |
| 100 |
$this->register_field('Author', 'post_author', [ |
| 101 |
'tooltip' => __('The user of who added this post', 'importwp') |
| 102 |
]), |
| 103 |
$this->register_field('Author Field Type', '_author_type', [ |
| 104 |
'default' => 'id', |
| 105 |
'options' => [ |
| 106 |
['value' => 'id', 'label' => 'ID'], |
| 107 |
['value' => 'login', 'label' => 'Login'], |
| 108 |
['value' => 'email', 'label' => 'Email'], |
| 109 |
], |
| 110 |
'tooltip' => __('Select how the author field should be handled', 'importwp') |
| 111 |
]) |
| 112 |
]), |
| 113 |
$this->register_field('Password', 'post_password', [ |
| 114 |
'tooltip' => __('The password to access the post', 'importwp') |
| 115 |
]), |
| 116 |
$this->register_field('Allow Comments', 'comment_status', [ |
| 117 |
'options' => [ |
| 118 |
['value' => '0', 'label' => 'Disabled'], |
| 119 |
['value' => '1', 'label' => 'Enabled'] |
| 120 |
], |
| 121 |
'default' => '0', |
| 122 |
'tooltip' => __('Whether the post can accept comments', 'importwp') |
| 123 |
]), |
| 124 |
$this->register_field('Allow Pingbacks', 'ping_status', [ |
| 125 |
'options' => [ |
| 126 |
['value' => 'closed', 'label' => 'Closed'], |
| 127 |
['value' => 'open', 'label' => 'Open'] |
| 128 |
], |
| 129 |
'default' => 'closed', |
| 130 |
'tooltip' => __('Whether the post can accept pings', 'importwp') |
| 131 |
]) |
| 132 |
]), |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
public function register_settings() |
| 137 |
{ |
| 138 |
return []; |
| 139 |
} |
| 140 |
|
| 141 |
public function register_options() |
| 142 |
{ |
| 143 |
return []; |
| 144 |
} |
| 145 |
|
| 146 |
public function pre_process(ParsedData $data) |
| 147 |
{ |
| 148 |
$data = parent::pre_process($data); |
| 149 |
|
| 150 |
$field_map = []; |
| 151 |
$fields = [ |
| 152 |
'ID', |
| 153 |
'post_title', |
| 154 |
'post_name', |
| 155 |
'post_content', |
| 156 |
'post_excerpt', |
| 157 |
'_wp_attachment_image_alt', |
| 158 |
'post_date', |
| 159 |
'menu_order', |
| 160 |
'post_author', |
| 161 |
'post_password', |
| 162 |
'comment_status', |
| 163 |
'ping_status', |
| 164 |
]; |
| 165 |
|
| 166 |
foreach ($fields as $field) { |
| 167 |
|
| 168 |
$field_id = sprintf('post.%s', $field); |
| 169 |
$value = $data->getValue($field_id, 'post'); |
| 170 |
if ($value !== false && $this->importer->isEnabledField($field_id)) { |
| 171 |
$field_map[$field] = $value; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
$file_location = $data->getValue('post.file.location'); |
| 176 |
if ($file_location !== false && $this->importer->isEnabledField('post.file')) { |
| 177 |
|
| 178 |
// Added so it can be used as a unique identifier |
| 179 |
// But not in default so it isnt automatically inserted. |
| 180 |
$data->add(['src' => $file_location], 'post'); |
| 181 |
} |
| 182 |
|
| 183 |
if (!empty($this->virtual_fields)) { |
| 184 |
foreach ($this->virtual_fields as $virtual_field) { |
| 185 |
$value = $data->getValue($virtual_field); |
| 186 |
if (false !== $value) { |
| 187 |
$post_field_map[$virtual_field] = $value; |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
if ($this->importer->isEnabledField('post._author')) { |
| 193 |
$post_author = $data->getValue('post._author.post_author', 'post'); |
| 194 |
$post_author_type = $data->getValue('post._author._author_type', 'post'); |
| 195 |
|
| 196 |
$user_id = 0; |
| 197 |
|
| 198 |
if ($post_author_type === 'id') { |
| 199 |
|
| 200 |
$user = get_user_by('ID', $post_author); |
| 201 |
if ($user) { |
| 202 |
$user_id = intval($user->ID); |
| 203 |
} |
| 204 |
} elseif ($post_author_type === 'login') { |
| 205 |
|
| 206 |
$user = get_user_by('login', $post_author); |
| 207 |
if ($user) { |
| 208 |
$user_id = intval($user->ID); |
| 209 |
} |
| 210 |
} elseif ($post_author_type === 'email') { |
| 211 |
|
| 212 |
$user = get_user_by('email', $post_author); |
| 213 |
if ($user) { |
| 214 |
$user_id = intval($user->ID); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
$field_map['post_author'] = $user_id > 0 ? $user_id : ''; |
| 219 |
} |
| 220 |
|
| 221 |
// post_parent is different |
| 222 |
$post_parent = $data->getValue('post._parent.parent', 'post'); |
| 223 |
if ($this->importer->isEnabledField('post._parent') && $post_parent !== false) { |
| 224 |
|
| 225 |
$field_map['post_parent'] = $post_parent; |
| 226 |
|
| 227 |
$parent_id = 0; |
| 228 |
$parent_field_type = $data->getValue('post._parent._parent_type'); |
| 229 |
|
| 230 |
if ($parent_field_type === 'name' || $parent_field_type === 'slug') { |
| 231 |
|
| 232 |
// name or slug |
| 233 |
$page = get_posts(array('name' => sanitize_title($field_map['post_parent']), 'post_type' => 'any')); |
| 234 |
if ($page) { |
| 235 |
$parent_id = intval($page[0]->ID); |
| 236 |
} |
| 237 |
} elseif ($parent_field_type === 'id') { |
| 238 |
|
| 239 |
// ID |
| 240 |
$parent_id = intval($field_map['post_parent']); |
| 241 |
} elseif ($parent_field_type === 'column') { |
| 242 |
|
| 243 |
// reference column |
| 244 |
$temp_id = $this->get_post_by_cf('post_parent', $field_map['post_parent']); |
| 245 |
if (intval($temp_id > 0)) { |
| 246 |
$parent_id = intval($temp_id); |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
if ($parent_id > 0) { |
| 251 |
$field_map['post_parent'] = $parent_id; |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
$data->replace($field_map, 'default'); |
| 256 |
|
| 257 |
return $data; |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Alter fields before they are parsed |
| 262 |
* |
| 263 |
* @param array $fields |
| 264 |
* @return array |
| 265 |
*/ |
| 266 |
public function field_map($fields) |
| 267 |
{ |
| 268 |
if ($this->importer->isEnabledField('post._parent') && $fields['post._parent._parent_type'] === 'column' && !empty($fields["post._parent._parent_ref"])) { |
| 269 |
$fields['_iwp_ref_post_parent'] = $fields["post._parent._parent_ref"]; |
| 270 |
$this->virtual_fields[] = '_iwp_ref_post_parent'; |
| 271 |
} |
| 272 |
return $fields; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Get list of posts |
| 277 |
* |
| 278 |
* @param ImporterModel $importer_model |
| 279 |
* |
| 280 |
* @return array |
| 281 |
*/ |
| 282 |
public function get_post_parent_options($importer_model) |
| 283 |
{ |
| 284 |
$query = new \WP_Query(array( |
| 285 |
'post_type' => 'any', |
| 286 |
'posts_per_page' => -1, |
| 287 |
'cache_results' => false, |
| 288 |
'update_post_meta_cache' => false, |
| 289 |
)); |
| 290 |
|
| 291 |
if (is_wp_error($query)) { |
| 292 |
return $query; |
| 293 |
} |
| 294 |
|
| 295 |
$result = []; |
| 296 |
foreach ($query->posts as $post) { |
| 297 |
$result[] = [ |
| 298 |
'value' => '' . $post->ID, |
| 299 |
'label' => $post->post_title |
| 300 |
]; |
| 301 |
} |
| 302 |
|
| 303 |
return $result; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Find existing post/page/custom by reference field |
| 308 |
* |
| 309 |
* @param string $field Reference Field Name |
| 310 |
* @param string $value Value to check against reference |
| 311 |
* |
| 312 |
* @return bool |
| 313 |
*/ |
| 314 |
public function get_post_by_cf($field, $value) |
| 315 |
{ |
| 316 |
|
| 317 |
$query = new \WP_Query(array( |
| 318 |
'post_type' => 'any', |
| 319 |
'posts_per_page' => 1, |
| 320 |
'fields' => 'ids', |
| 321 |
'cache_results' => false, |
| 322 |
'update_post_meta_cache' => false, |
| 323 |
'meta_query' => array( |
| 324 |
array( |
| 325 |
'key' => '_iwp_ref_' . $field, |
| 326 |
'value' => $value |
| 327 |
) |
| 328 |
), |
| 329 |
'post_status' => 'any' |
| 330 |
)); |
| 331 |
if ($query->have_posts()) { |
| 332 |
return $query->posts[0]; |
| 333 |
} |
| 334 |
return false; |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* Convert fields/headings to data map |
| 339 |
* |
| 340 |
* @param mixed $fields |
| 341 |
* @param ImporterModel $importer |
| 342 |
* @return array |
| 343 |
*/ |
| 344 |
public function generate_field_map($fields, $importer) |
| 345 |
{ |
| 346 |
$result = parent::generate_field_map($fields, $importer); |
| 347 |
$map = $result['map']; |
| 348 |
$enabled = $result['enabled']; |
| 349 |
|
| 350 |
$taxonomies = []; |
| 351 |
$attachments = []; |
| 352 |
$author = []; |
| 353 |
$parent = []; |
| 354 |
|
| 355 |
foreach ($fields as $index => $field) { |
| 356 |
|
| 357 |
if (preg_match('/^author\.(.*?)$/', $field, $matches) === 1) { |
| 358 |
|
| 359 |
// Capture author. |
| 360 |
// author.ID, author.user_login,author.user_nicename,author.user_email,author.user_url,author.display_name |
| 361 |
if (!isset($author['map'])) { |
| 362 |
$author['map'] = []; |
| 363 |
} |
| 364 |
|
| 365 |
$author['map'][$matches[1]] = sprintf('{%d}', $index); |
| 366 |
} elseif (preg_match('/^parent\.(.*?)$/', $field, $matches) === 1) { |
| 367 |
|
| 368 |
// Capture parent |
| 369 |
// parent.id,parent.name,parent.slug |
| 370 |
if (!isset($parent['map'])) { |
| 371 |
$parent['map'] = []; |
| 372 |
} |
| 373 |
|
| 374 |
$parent['map'][$matches[1]] = sprintf('{%d}', $index); |
| 375 |
} elseif (isset($this->field_map[$field])) { |
| 376 |
|
| 377 |
// Handle core fields |
| 378 |
$field_key = $this->field_map[$field]; |
| 379 |
$map[$field_key] = sprintf('{%d}', $index); |
| 380 |
|
| 381 |
if (in_array($field, $this->optional_fields)) { |
| 382 |
$enabled[] = $field_key; |
| 383 |
} |
| 384 |
|
| 385 |
if (in_array($field, ['post_status', 'comment_status', 'ping_status'])) { |
| 386 |
$map[$field_key . '._enable_text'] = 'yes'; |
| 387 |
} |
| 388 |
} elseif ($field === 'custom_fields._wp_attachment_image_alt') { |
| 389 |
|
| 390 |
|
| 391 |
$field_key = $this->field_map[$field]; |
| 392 |
$map[$field_key] = sprintf('{%d}', $index); |
| 393 |
$enabled[] = $field_key; |
| 394 |
|
| 395 |
add_filter('iwp/importer/generate_field_map/custom_fields', function ($custom_fields) { |
| 396 |
|
| 397 |
if (isset($custom_fields['_wp_attachment_image_alt'])) { |
| 398 |
unset($custom_fields['_wp_attachment_image_alt']); |
| 399 |
} |
| 400 |
|
| 401 |
return $custom_fields; |
| 402 |
}); |
| 403 |
} elseif ($field === 'url') { |
| 404 |
|
| 405 |
|
| 406 |
$map['post.file.location'] = sprintf('{%d}', $index); |
| 407 |
$enabled[] = 'post.file'; |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
$author = apply_filters('iwp/importer/generate_field_map/author', $author, $fields, $importer); |
| 412 |
$parent = apply_filters('iwp/importer/generate_field_map/parent', $parent, $fields, $importer); |
| 413 |
|
| 414 |
if (!empty($author)) { |
| 415 |
|
| 416 |
// TODO: enable field? this is a seperate field list |
| 417 |
$enabled_key = 'post._author'; |
| 418 |
if (isset($author['map']['user_email'])) { |
| 419 |
|
| 420 |
$enabled[] = $enabled_key; |
| 421 |
$map['post._author.post_author'] = $author['map']['user_email']; |
| 422 |
$map['post._author._author_type'] = 'email'; |
| 423 |
} elseif (isset($author['map']['user_login'])) { |
| 424 |
|
| 425 |
$enabled[] = $enabled_key; |
| 426 |
$map['post._author.post_author'] = $author['map']['user_login']; |
| 427 |
$map['post._author._author_type'] = 'login'; |
| 428 |
} elseif (isset($author['map']['ID'])) { |
| 429 |
|
| 430 |
$enabled[] = $enabled_key; |
| 431 |
$map['post._author.post_author'] = $author['map']['ID']; |
| 432 |
$map['post._author._author_type'] = 'id'; |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
if (!empty($parent)) { |
| 437 |
|
| 438 |
// parent.id,parent.name,parent.slug |
| 439 |
$enabled_key = 'post._parent'; |
| 440 |
if (isset($parent['map']['name'])) { |
| 441 |
|
| 442 |
$enabled[] = $enabled_key; |
| 443 |
$map['post._parent.parent'] = $parent['map']['name']; |
| 444 |
$map['post._parent.parent._enable_text'] = 'yes'; |
| 445 |
$map['post._parent._parent_type'] = 'name'; |
| 446 |
} elseif (isset($parent['map']['slug'])) { |
| 447 |
|
| 448 |
$enabled[] = $enabled_key; |
| 449 |
$map['post._parent.parent'] = $parent['map']['slug']; |
| 450 |
$map['post._parent.parent._enable_text'] = 'yes'; |
| 451 |
$map['post._parent._parent_type'] = 'slug'; |
| 452 |
} elseif (isset($parent['map']['id'])) { |
| 453 |
|
| 454 |
$enabled[] = $enabled_key; |
| 455 |
$map['post._parent.parent'] = $parent['map']['name']; |
| 456 |
$map['post._parent.parent._enable_text'] = 'yes'; |
| 457 |
$map['post._parent._parent_type'] = 'id'; |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
return [ |
| 462 |
'map' => $map, |
| 463 |
'enabled' => $enabled |
| 464 |
]; |
| 465 |
} |
| 466 |
|
| 467 |
public function get_permission_fields($importer_model) |
| 468 |
{ |
| 469 |
$permission_fields = parent::get_permission_fields($importer_model); |
| 470 |
|
| 471 |
$permission_fields['core'] = [ |
| 472 |
'ID' => 'ID', |
| 473 |
'post_title' => 'Title', |
| 474 |
'file' => 'Attachment File', |
| 475 |
'post_content' => 'Description', |
| 476 |
'post_excerpt' => 'Caption', |
| 477 |
'post_name' => 'Slug', |
| 478 |
'post_status' => 'Post Status', |
| 479 |
'menu_order' => 'Order', |
| 480 |
'post_password' => 'Password', |
| 481 |
'post_date' => 'Date', |
| 482 |
'comment_status' => 'Allow Comments', |
| 483 |
'ping_status' => 'Allow Pingbacks', |
| 484 |
'post_parent' => 'Parent', |
| 485 |
'post_author' => 'Author', |
| 486 |
'_wp_attachment_image_alt' => 'Alt Text', |
| 487 |
]; |
| 488 |
|
| 489 |
return $permission_fields; |
| 490 |
} |
| 491 |
} |
| 492 |
|