| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImportWP\Common\Importer\Mapper; |
| 4 |
|
| 5 |
use ImportWP\Common\Attachment\Attachment; |
| 6 |
use ImportWP\Common\Filesystem\Filesystem; |
| 7 |
use ImportWP\Common\Ftp\Ftp; |
| 8 |
use ImportWP\Common\Importer\Exception\MapperException; |
| 9 |
use ImportWP\Common\Importer\ParsedData; |
| 10 |
use ImportWP\Common\Util\Logger; |
| 11 |
use ImportWP\Container; |
| 12 |
|
| 13 |
class AttachmentMapper extends PostMapper |
| 14 |
{ |
| 15 |
public function exists(ParsedData $data) |
| 16 |
{ |
| 17 |
list($unique_fields, $meta_args, $has_unique_field) = $this->exists_get_identifier($data); |
| 18 |
$unique_field_found = false; |
| 19 |
|
| 20 |
$post_type = 'attachment'; |
| 21 |
$post_status = 'any, trash, future'; |
| 22 |
|
| 23 |
$query_args = array( |
| 24 |
'post_type' => $post_type, |
| 25 |
'post_status' => $post_status, |
| 26 |
'fields' => 'ids', |
| 27 |
'cache_results' => false, |
| 28 |
'update_post_meta_cache' => false, |
| 29 |
'update_post_term_cache' => false, |
| 30 |
'no_found_rows' => true, |
| 31 |
); |
| 32 |
|
| 33 |
foreach ($unique_fields as $field) { |
| 34 |
|
| 35 |
if ($field === 'src') { |
| 36 |
/** |
| 37 |
* @var Attachment $attachment |
| 38 |
*/ |
| 39 |
$attachment = Container::getInstance()->get('attachment'); |
| 40 |
|
| 41 |
$location = $data->getValue('post.file.location', 'post'); |
| 42 |
$download = $data->getValue('post.file.settings._download', 'post'); |
| 43 |
$ftp_path = $data->getValue('post.file.settings._ftp_path', 'post'); |
| 44 |
$remote_url = $data->getValue('post.file.settings._remote_url', 'post'); |
| 45 |
$local_url = $data->getValue('post.file.settings._local_url', 'post'); |
| 46 |
|
| 47 |
if (empty($location)) { |
| 48 |
continue; |
| 49 |
} |
| 50 |
|
| 51 |
$attachment_id = null; |
| 52 |
|
| 53 |
switch ($download) { |
| 54 |
case 'remote': |
| 55 |
$source = $remote_url . $location; |
| 56 |
$attachment_id = $attachment->get_attachment_by_hash($source); |
| 57 |
break; |
| 58 |
case 'ftp': |
| 59 |
$source = $ftp_path . $location; |
| 60 |
$attachment_id = $attachment->get_attachment_by_hash($source); |
| 61 |
break; |
| 62 |
case 'local': |
| 63 |
$source = $local_url . $location; |
| 64 |
$attachment_salt = file_exists($source) ? md5_file($source) : ''; |
| 65 |
$attachment_id = $attachment->get_attachment_by_hash($source, $attachment_salt); |
| 66 |
break; |
| 67 |
case 'media': |
| 68 |
$source = $location; |
| 69 |
$attachment_id = $attachment->attachment_partial_url_to_postid($source); |
| 70 |
break; |
| 71 |
} |
| 72 |
|
| 73 |
$this->set_unique_identifier_settings('src', $source); |
| 74 |
|
| 75 |
if ($attachment_id > 0) { |
| 76 |
$has_unique_field = true; |
| 77 |
$query_args['p'] = $attachment_id; |
| 78 |
break; |
| 79 |
} else { |
| 80 |
return false; |
| 81 |
} |
| 82 |
} else { |
| 83 |
|
| 84 |
// check all groups for a unique value |
| 85 |
$unique_value = $this->find_unique_field_in_data($data, $field); |
| 86 |
|
| 87 |
if (!empty($unique_value)) { |
| 88 |
$has_unique_field = true; |
| 89 |
|
| 90 |
if (in_array($field, $this->_post_fields, true)) { |
| 91 |
|
| 92 |
if (array_key_exists($field, $this->_query_vars)) { |
| 93 |
$query_args[$this->_query_vars[$field]] = $unique_value; |
| 94 |
} else { |
| 95 |
switch ($field) { |
| 96 |
case 'post_title': |
| 97 |
$query_args['title'] = $unique_value; |
| 98 |
break; |
| 99 |
default: |
| 100 |
$query_args[$field] = $unique_value; |
| 101 |
break; |
| 102 |
} |
| 103 |
} |
| 104 |
} else { |
| 105 |
$meta_args[] = array( |
| 106 |
'key' => $field, |
| 107 |
'value' => $unique_value |
| 108 |
); |
| 109 |
} |
| 110 |
$unique_field_found = $field; |
| 111 |
$this->set_unique_identifier_settings($unique_field_found, $unique_value); |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
if (!$has_unique_field) { |
| 118 |
throw new MapperException(__("No Unique fields present.", 'jc-importer')); |
| 119 |
} |
| 120 |
|
| 121 |
if (!empty($meta_args)) { |
| 122 |
$query_args['meta_query'] = $meta_args; |
| 123 |
} |
| 124 |
|
| 125 |
$query_args = apply_filters('iwp/importer/mapper/attachment_exists_query', $query_args); |
| 126 |
Logger::debug("AttachmentMapper::exists -query=" . wp_json_encode($query_args)); |
| 127 |
$query = new \WP_Query($query_args); |
| 128 |
if ($query->post_count > 1) { |
| 129 |
throw new MapperException(sprintf(__("Record is not unique: %s, Matching Ids: (%s).", 'jc-importer'), $unique_field_found, implode(', ', $query->posts))); |
| 130 |
} |
| 131 |
|
| 132 |
if ($query->post_count == 1) { |
| 133 |
$this->ID = $query->posts[0]; |
| 134 |
return $this->ID; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
public function update_post_object($fields, $data) |
| 142 |
{ |
| 143 |
$post = array(); |
| 144 |
$meta = array(); |
| 145 |
|
| 146 |
// we dont import the ID |
| 147 |
if (isset($fields['ID'])) { |
| 148 |
unset($fields['ID']); |
| 149 |
} |
| 150 |
|
| 151 |
$this->sortFields($fields, $post, $meta); |
| 152 |
|
| 153 |
Logger::debug('AttachmentMapper::update_post_object -wp_update_post=' . wp_json_encode($post)); |
| 154 |
|
| 155 |
if (!empty($post)) { |
| 156 |
|
| 157 |
$post['ID'] = $this->ID; |
| 158 |
|
| 159 |
$res = wp_update_post($post); |
| 160 |
if (is_wp_error($res)) { |
| 161 |
throw new MapperException($res->get_error_message()); |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
$this->template->process($this->ID, $data, $this->importer); |
| 166 |
|
| 167 |
$meta = array_merge($meta, $data->getData('custom_fields')); |
| 168 |
Logger::debug('AttachmentMapper::update_post_object -meta=' . wp_json_encode($meta)); |
| 169 |
|
| 170 |
// create post meta |
| 171 |
if ($this->ID && !empty($meta)) { |
| 172 |
foreach ($meta as $key => $value) { |
| 173 |
if (is_array($value)) { |
| 174 |
$this->clear_custom_field($this->ID, $key); |
| 175 |
foreach ($value as $v) { |
| 176 |
$this->add_custom_field($this->ID, $key, $v); |
| 177 |
} |
| 178 |
} else { |
| 179 |
$this->update_custom_field($this->ID, $key, $value); |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
public function insert(ParsedData $data) |
| 186 |
{ |
| 187 |
$fields = $data->getData('default'); |
| 188 |
if (isset($fields['ID'])) { |
| 189 |
unset($fields['ID']); |
| 190 |
} |
| 191 |
|
| 192 |
$this->ID = $attachment_id = $this->download_attachment($data); |
| 193 |
if (!$this->ID) { |
| 194 |
throw new MapperException(__("Unable to download and insert attachment", 'jc-importer')); |
| 195 |
} |
| 196 |
|
| 197 |
$this->update_post_object($fields, $data); |
| 198 |
|
| 199 |
$this->add_version_tag(); |
| 200 |
$this->add_reference_tag($data); |
| 201 |
$this->template->post_process($this->ID, $data); |
| 202 |
|
| 203 |
return $attachment_id; |
| 204 |
} |
| 205 |
|
| 206 |
public function update(ParsedData $data) |
| 207 |
{ |
| 208 |
$attachment_id = $data->getId(); |
| 209 |
|
| 210 |
$fields = $data->getData('default'); |
| 211 |
if (isset($fields['ID'])) { |
| 212 |
unset($fields['ID']); |
| 213 |
} |
| 214 |
|
| 215 |
$this->download_attachment($data); |
| 216 |
|
| 217 |
$this->update_post_object($fields, $data); |
| 218 |
|
| 219 |
$this->add_version_tag(); |
| 220 |
$this->add_reference_tag($data); |
| 221 |
$this->template->post_process($this->ID, $data); |
| 222 |
|
| 223 |
return $attachment_id; |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Download Attachment |
| 228 |
* |
| 229 |
* @param ParsedData $data |
| 230 |
*/ |
| 231 |
public function download_attachment($data) |
| 232 |
{ |
| 233 |
if (!$this->importer->isEnabledField('post.file')) { |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
$is_allowed = $data->permission()->validate(['file' => ''], $data->getMethod(), 'post'); |
| 238 |
if (!isset($is_allowed['file'])) { |
| 239 |
return false; |
| 240 |
} |
| 241 |
|
| 242 |
$attachment_id = intval($data->getId()); |
| 243 |
|
| 244 |
/** |
| 245 |
* @var Attachment $attachment |
| 246 |
*/ |
| 247 |
$attachment = Container::getInstance()->get('attachment'); |
| 248 |
|
| 249 |
$location = $data->getValue('post.file.location', 'post'); |
| 250 |
$download = $data->getValue('post.file.settings._download', 'post'); |
| 251 |
$ftp_path = $data->getValue('post.file.settings._ftp_path', 'post'); |
| 252 |
$remote_url = $data->getValue('post.file.settings._remote_url', 'post'); |
| 253 |
$local_url = $data->getValue('post.file.settings._local_url', 'post'); |
| 254 |
$ftp_host = $data->getValue('post.file.settings._ftp_host', 'post'); |
| 255 |
$ftp_user = $data->getValue('post.file.settings._ftp_user', 'post'); |
| 256 |
$ftp_pass = $data->getValue('post.file.settings._ftp_pass', 'post'); |
| 257 |
|
| 258 |
if (empty($location)) { |
| 259 |
return $attachment_id; |
| 260 |
} |
| 261 |
|
| 262 |
$attachment_salt = ''; |
| 263 |
$result = false; |
| 264 |
|
| 265 |
switch ($download) { |
| 266 |
case 'remote': |
| 267 |
|
| 268 |
$source = $remote_url . $location; |
| 269 |
$attachment_salt = file_exists($source) ? md5_file($source) : ''; |
| 270 |
|
| 271 |
/** |
| 272 |
* @var Filesystem $filesystem |
| 273 |
*/ |
| 274 |
$filesystem = Container::getInstance()->get('filesystem'); |
| 275 |
|
| 276 |
$custom_filename = apply_filters('iwp/attachment/filename', null, $source); |
| 277 |
$result = $filesystem->download_file($source, null, null, $custom_filename); |
| 278 |
|
| 279 |
break; |
| 280 |
case 'ftp': |
| 281 |
|
| 282 |
$source = $ftp_path . $location; |
| 283 |
$attachment_salt = file_exists($source) ? md5_file($source) : ''; |
| 284 |
|
| 285 |
/** |
| 286 |
* @var Ftp $ftp |
| 287 |
*/ |
| 288 |
$ftp = Container::getInstance()->get('ftp'); |
| 289 |
|
| 290 |
$custom_filename = apply_filters('iwp/attachment/filename', null, $source); |
| 291 |
$result = $ftp->download_file($source, $ftp_host, $ftp_user, $ftp_pass, $custom_filename); |
| 292 |
break; |
| 293 |
case 'local': |
| 294 |
|
| 295 |
$source = $local_url . $location; |
| 296 |
$attachment_salt = file_exists($source) ? md5_file($source) : ''; |
| 297 |
|
| 298 |
/** |
| 299 |
* @var Filesystem $filesystem |
| 300 |
*/ |
| 301 |
$filesystem = Container::getInstance()->get('filesystem'); |
| 302 |
|
| 303 |
$custom_filename = apply_filters('iwp/attachment/filename', null, $source); |
| 304 |
$result = $filesystem->copy_file($source, null, $custom_filename); |
| 305 |
break; |
| 306 |
} |
| 307 |
|
| 308 |
if (is_wp_error($result)) { |
| 309 |
throw new MapperException($result->get_error_message()); |
| 310 |
} |
| 311 |
|
| 312 |
if ($result) { |
| 313 |
|
| 314 |
// We have downloaded a file |
| 315 |
if ($attachment_id) { |
| 316 |
|
| 317 |
// updated existing |
| 318 |
$attachment_id = wp_update_post([ |
| 319 |
'ID' => $attachment_id, |
| 320 |
'post_mime_type' => $result['mime'], |
| 321 |
'file' => $result['dest'] |
| 322 |
]); |
| 323 |
} else { |
| 324 |
|
| 325 |
// insert new |
| 326 |
$attachment_id = $attachment->insert_attachment(0, $result['dest'], $result['mime']); |
| 327 |
} |
| 328 |
|
| 329 |
if ($attachment_id) { |
| 330 |
|
| 331 |
// Attachment has been created or updated |
| 332 |
$attachment->generate_image_sizes($attachment_id, $result['dest']); |
| 333 |
$attachment->store_attachment_hash($attachment_id, $source, $attachment_salt); |
| 334 |
} |
| 335 |
|
| 336 |
return $attachment_id; |
| 337 |
} |
| 338 |
|
| 339 |
return false; |
| 340 |
} |
| 341 |
|
| 342 |
public function get_objects_for_removal() |
| 343 |
{ |
| 344 |
if ($this->is_session_tag_enabled()) { |
| 345 |
return $this->get_ids_without_session_tag('pt-' . 'attachment'); |
| 346 |
} else { |
| 347 |
|
| 348 |
$import_ids = apply_filters('iwp/mapper/session_importer_ids', [$this->importer->getId()], $this->importer->getId()); |
| 349 |
if (empty($import_ids)) { |
| 350 |
return false; |
| 351 |
} |
| 352 |
|
| 353 |
$meta_query = []; |
| 354 |
foreach ($import_ids as $import_id) { |
| 355 |
$meta_query[] = array( |
| 356 |
'key' => '_iwp_session_' . $import_id, |
| 357 |
'value' => $this->importer->getStatusId(), |
| 358 |
'compare' => '!=' |
| 359 |
); |
| 360 |
} |
| 361 |
|
| 362 |
if (count($meta_query) > 1) { |
| 363 |
$meta_query['relation'] = 'AND'; |
| 364 |
} |
| 365 |
|
| 366 |
$q = new \WP_Query(array( |
| 367 |
'post_type' => 'attachment', |
| 368 |
'meta_query' => $meta_query, |
| 369 |
'fields' => 'ids', |
| 370 |
'posts_per_page' => -1, |
| 371 |
'cache_results' => false, |
| 372 |
'update_post_meta_cache' => false, |
| 373 |
'post_status' => 'any' |
| 374 |
)); |
| 375 |
|
| 376 |
if ($q->have_posts()) { |
| 377 |
return $q->posts; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
return false; |
| 382 |
} |
| 383 |
|
| 384 |
public function delete($id) |
| 385 |
{ |
| 386 |
$permissions = $this->importer->getPermission('remove'); |
| 387 |
$force = isset($permissions['trash']) ? !$permissions['trash'] : true; // trash = true |
| 388 |
|
| 389 |
if (!$force) { |
| 390 |
|
| 391 |
// set trash flag |
| 392 |
update_post_meta($id, '_iwp_trash_status', get_post_status($id)); |
| 393 |
update_post_meta($id, '_iwp_trash_importer', $this->importer->getId()); |
| 394 |
} |
| 395 |
|
| 396 |
wp_delete_attachment($id, $force); |
| 397 |
|
| 398 |
$this->remove_session_tag($id, 'pt-attachment'); |
| 399 |
} |
| 400 |
|
| 401 |
public function add_version_tag() |
| 402 |
{ |
| 403 |
if ($this->is_session_tag_enabled()) { |
| 404 |
$this->add_session_tag('pt-attachment'); |
| 405 |
} else { |
| 406 |
update_post_meta($this->ID, '_iwp_session_' . $this->importer->getId(), $this->importer->getStatusId()); |
| 407 |
} |
| 408 |
} |
| 409 |
} |
| 410 |
|