| @@ -5,8 +5,9 @@ | ||
| 5 | 5 | use ImportWP\Common\Attachment\Attachment; |
| 6 | 6 | use ImportWP\Common\Filesystem\Filesystem; |
| 7 | 7 | use ImportWP\Common\Ftp\Ftp; |
| 8 | 8 | use ImportWP\Common\Importer\File\XMLFile; |
| 9 | +use ImportWP\Common\Importer\Mapper\AbstractMapper; | |
| 9 | 10 | use ImportWP\Common\Importer\ParsedData; |
| 10 | 11 | use ImportWP\Common\Model\ImporterModel; |
| 11 | 12 | use ImportWP\Common\Util\Logger; |
| 12 | 13 | use ImportWP\Container; |
| @@ -47,8 +48,15 @@ | ||
| 47 | 48 | |
| 48 | 49 | protected $default_template_options = []; |
| 49 | 50 | |
| 50 | 51 | /** |
| 52 | + * | |
| 53 | + * List of optional fields that are enabled by default | |
| 54 | + * @var string[] | |
| 55 | + */ | |
| 56 | + protected $default_enabled_fields = []; | |
| 57 | + | |
| 58 | + /** | |
| 51 | 59 | * @var \WP_Error[] $errors |
| 52 | 60 | */ |
| 53 | 61 | protected $errors = []; |
| 54 | 62 | |
| @@ -104,13 +112,28 @@ | ||
| 104 | 112 | $file = new XMLFile($filePath, $config); |
| 105 | 113 | $file->setRecordPath($base_path); |
| 106 | 114 | $nodes = $file->get_node_list(); |
| 107 | 115 | |
| 116 | + // Nodes that appear as a parent of another path have selectable children. | |
| 117 | + $parents = []; | |
| 108 | 118 | foreach ($nodes as $node) { |
| 119 | + $parent = $node; | |
| 120 | + while (($pos = strrpos($parent, '/')) !== false && $pos > 0) { | |
| 121 | + $parent = substr($parent, 0, $pos); | |
| 122 | + $parents[$parent] = true; | |
| 123 | + } | |
| 124 | + } | |
| 125 | + | |
| 126 | + foreach ($nodes as $node) { | |
| 109 | 127 | if (strpos($node, $base_path) !== 0) { |
| 110 | 128 | continue; |
| 111 | 129 | } |
| 112 | 130 | |
| 131 | + // Skip leaf nodes (e.g. /categories/category) — they have no selectable child nodes. | |
| 132 | + if (!isset($parents[$node])) { | |
| 133 | + continue; | |
| 134 | + } | |
| 135 | + | |
| 113 | 136 | $sub_node = substr($node, strlen($base_path)); |
| 114 | 137 | |
| 115 | 138 | $results[] = [ |
| 116 | 139 | 'value' => $sub_node, |
| @@ -196,12 +219,12 @@ | ||
| 196 | 219 | { |
| 197 | 220 | if (isset($args['row_base']) && true === $args['row_base']) { |
| 198 | 221 | |
| 199 | 222 | $fields = array_merge( |
| 200 | - [$this->register_field('Repeater Node', 'row_base', [ | |
| 223 | + [$this->register_field(__('Repeater Node', 'jc-importer'), 'row_base', [ | |
| 201 | 224 | 'type' => 'text', |
| 202 | 225 | 'options' => 'callback', |
| 203 | - 'tooltip' => 'Select the path to the parent node containing values for this record, The preview when selecting data for each record should show a single record. Please note changing this will require updating references in this record.' | |
| 226 | + 'tooltip' => __('Select the path to the parent node containing values for this record, The preview when selecting data for each record should show a single record. Please note changing this will require updating references in this record.', 'jc-importer') | |
| 204 | 227 | ])], |
| 205 | 228 | $fields |
| 206 | 229 | ); |
| 207 | 230 | } |
| @@ -209,15 +232,30 @@ | ||
| 209 | 232 | $tmp = [ |
| 210 | 233 | 'id' => $key, |
| 211 | 234 | 'heading' => $label, |
| 212 | 235 | 'type' => isset($args['type']) ? $args['type'] : 'group', |
| 213 | - 'fields' => $fields | |
| 236 | + 'fields' => $fields, | |
| 214 | 237 | ]; |
| 215 | 238 | |
| 239 | + if (isset($args['link']) && !empty($args['link'])) { | |
| 240 | + $tmp['link'] = $args['link']; | |
| 241 | + } | |
| 242 | + | |
| 216 | 243 | if (isset($args['condition']) && !empty($args['condition'])) { |
| 217 | 244 | $tmp['condition'] = $args['condition']; |
| 218 | 245 | } |
| 219 | 246 | |
| 247 | + // Allow for setting fields to be registered for the group | |
| 248 | + // currently only supporting toggle fields. | |
| 249 | + if (isset($args['settings']) && !empty($args['settings'])) { | |
| 250 | + $tmp['settings'] = $args['settings']; | |
| 251 | + } | |
| 252 | + | |
| 253 | + // Field ids used to build the collapsed repeater row summary in the UI. | |
| 254 | + if (isset($args['row_summary']) && !empty($args['row_summary'])) { | |
| 255 | + $tmp['row_summary'] = array_values((array) $args['row_summary']); | |
| 256 | + } | |
| 257 | + | |
| 220 | 258 | return $tmp; |
| 221 | 259 | } |
| 222 | 260 | |
| 223 | 261 | public function register_core_field($label, $key, $args = []) |
| @@ -250,108 +288,126 @@ | ||
| 250 | 288 | |
| 251 | 289 | return $data; |
| 252 | 290 | } |
| 253 | 291 | |
| 254 | - public function _register_attachment_setting_fields($display_conditions = [], $extra_fields = []) | |
| 292 | + public function _register_attachment_setting_fields($display_conditions = [], $extra_fields = [], $disabled_fields = []) | |
| 255 | 293 | { |
| 256 | - return $this->register_group('Settings', 'settings', array_merge($extra_fields, [ | |
| 257 | 294 | |
| 258 | - $this->register_field('Is Featured?', '_featured', [ | |
| 295 | + $fields = []; | |
| 296 | + | |
| 297 | + if (!in_array('_featured', $disabled_fields)) { | |
| 298 | + $fields[] = $this->register_field(__('Is Featured?', 'jc-importer'), '_featured', [ | |
| 259 | 299 | 'default' => 'no', |
| 260 | 300 | 'options' => [ |
| 261 | - ['value' => 'no', 'label' => 'No'], | |
| 262 | - ['value' => 'yes', 'label' => 'Yes'], | |
| 301 | + ['value' => 'no', 'label' => __('No', 'jc-importer')], | |
| 302 | + ['value' => 'yes', 'label' => __('Yes', 'jc-importer')], | |
| 263 | 303 | ], |
| 264 | - 'tooltip' => __('Is the attachment the featured image for the current post.', 'importwp') | |
| 265 | - ]), | |
| 266 | - $this->register_field('Download', '_download', [ | |
| 304 | + 'tooltip' => __('Is the attachment the featured image for the current post.', 'jc-importer') | |
| 305 | + ]); | |
| 306 | + } | |
| 307 | + | |
| 308 | + $fields = array_merge($fields, [ | |
| 309 | + $this->register_field(__('Download', 'jc-importer'), '_download', [ | |
| 267 | 310 | 'default' => 'remote', |
| 268 | 311 | 'options' => [ |
| 269 | - ['value' => 'remote', 'label' => 'Remote URL'], | |
| 270 | - ['value' => 'ftp', 'label' => 'FTP'], | |
| 271 | - ['value' => 'local', 'label' => 'Local Filesystem'], | |
| 272 | - ['value' => 'media', 'label' => 'Media Library'], | |
| 312 | + ['value' => 'remote', 'label' => __('Remote URL', 'jc-importer')], | |
| 313 | + ['value' => 'ftp', 'label' => __('FTP', 'jc-importer')], | |
| 314 | + ['value' => 'local', 'label' => __('Local Filesystem', 'jc-importer')], | |
| 315 | + ['value' => 'media', 'label' => __('Media Library', 'jc-importer')], | |
| 273 | 316 | ], |
| 274 | - 'tooltip' => __('Select how the attachment is being downloaded.', 'importwp') | |
| 317 | + 'tooltip' => __('Select how the attachment is being downloaded.', 'jc-importer') | |
| 275 | 318 | ]), |
| 276 | - $this->register_field('Host', '_ftp_host', [ | |
| 319 | + $this->register_field(__('Host', 'jc-importer'), '_ftp_host', [ | |
| 277 | 320 | 'condition' => ['_download', '==', 'ftp'], |
| 278 | - 'tooltip' => __('Enter the FTP hostname', 'importwp') | |
| 321 | + 'tooltip' => __('Enter the FTP hostname', 'jc-importer') | |
| 279 | 322 | ]), |
| 280 | - $this->register_field('Username', '_ftp_user', [ | |
| 323 | + $this->register_field(__('Username', 'jc-importer'), '_ftp_user', [ | |
| 281 | 324 | 'condition' => ['_download', '==', 'ftp'], |
| 282 | - 'tooltip' => __('Enter the FTP username', 'importwp') | |
| 325 | + 'tooltip' => __('Enter the FTP username', 'jc-importer') | |
| 283 | 326 | ]), |
| 284 | - $this->register_field('Password', '_ftp_pass', [ | |
| 327 | + $this->register_field(__('Password', 'jc-importer'), '_ftp_pass', [ | |
| 285 | 328 | 'condition' => ['_download', '==', 'ftp'], |
| 286 | - 'tooltip' => __('Enter the FTP password', 'importwp') | |
| 329 | + 'tooltip' => __('Enter the FTP password', 'jc-importer') | |
| 287 | 330 | ]), |
| 288 | - $this->register_field('Path', '_ftp_path', [ | |
| 331 | + $this->register_field(__('Path', 'jc-importer'), '_ftp_path', [ | |
| 289 | 332 | 'condition' => ['_download', '==', 'ftp'], |
| 290 | - 'tooltip' => __('Enter the FTP base path, this is prefixed onto the Location field, leave empty to be ignore', 'importwp') | |
| 333 | + 'tooltip' => __('Enter the FTP base path, this is prefixed onto the Location field, leave empty to be ignore', 'jc-importer') | |
| 291 | 334 | ]), |
| 292 | - $this->register_field('Base URL', '_remote_url', [ | |
| 335 | + $this->register_field(__('Base URL', 'jc-importer'), '_remote_url', [ | |
| 293 | 336 | 'condition' => ['_download', '==', 'remote'], |
| 294 | - 'tooltip' => __('Enter the base url, this is prefixed onto the Location field, leave empty to be ignore', 'importwp') | |
| 337 | + 'tooltip' => __('Enter the base url, this is prefixed onto the Location field, leave empty to be ignore', 'jc-importer') | |
| 295 | 338 | ]), |
| 296 | - $this->register_field('Base URL', '_local_url', [ | |
| 339 | + $this->register_field(__('Base URL', 'jc-importer'), '_local_url', [ | |
| 297 | 340 | 'condition' => ['_download', '==', 'local'], |
| 298 | - 'tooltip' => __('Enter the base path from this servers root file system, this is prefixed onto the Location field, leave empty to be ignore', 'importwp') | |
| 341 | + 'tooltip' => __('Enter the base path from this servers root file system, this is prefixed onto the Location field, leave empty to be ignore', 'jc-importer') | |
| 299 | 342 | ]), |
| 300 | 343 | |
| 301 | - $this->register_field('Permissions', '_enable_image_hash', [ | |
| 344 | + $this->register_field(__('Permissions', 'jc-importer'), '_enable_image_hash', [ | |
| 302 | 345 | 'condition' => ['_download', '!=', 'media'], |
| 303 | 346 | 'default' => 'yes', |
| 304 | 347 | 'options' => [ |
| 305 | - ['value' => 'no', 'label' => 'Always download new files'], | |
| 306 | - ['value' => 'yes', 'label' => 'Search media library before downloading new files'], | |
| 348 | + ['value' => 'no', 'label' => __('Always download new files', 'jc-importer')], | |
| 349 | + ['value' => 'yes', 'label' => __('Search media library before downloading new files', 'jc-importer')], | |
| 307 | 350 | ], |
| 308 | - 'tooltip' => __('Enable to stop duplicate images by searching the media library before downloading new files.', 'importwp') | |
| 351 | + 'tooltip' => __('Enable to stop duplicate images by searching the media library before downloading new files.', 'jc-importer') | |
| 309 | 352 | ]), |
| 310 | - $this->register_field('Delimiter', '_delimiter', [ | |
| 353 | + $this->register_field(__('Delimiter', 'jc-importer'), '_delimiter', [ | |
| 311 | 354 | 'type' => 'text', |
| 312 | - 'tooltip' => 'A single character used to seperate attachments when listing multiple, Leave empty to use default: ,' | |
| 355 | + 'tooltip' => sprintf(__('A single character used to seperate attachments when listing multiple, Leave empty to use the default: %s', 'jc-importer'), ',') | |
| 313 | 356 | ]), |
| 314 | - $this->register_group('Attachment Meta', '_meta', [ | |
| 315 | - $this->register_field('Enable Meta', '_enabled', [ | |
| 357 | + | |
| 358 | + ]); | |
| 359 | + | |
| 360 | + if (!in_array('_meta', $disabled_fields)) { | |
| 361 | + $fields[] = $this->register_group(__('Attachment Meta', 'jc-importer'), '_meta', [ | |
| 362 | + $this->register_field(__('Enable Meta', 'jc-importer'), '_enabled', [ | |
| 316 | 363 | 'default' => 'no', |
| 317 | 364 | 'options' => [ |
| 318 | - ['value' => 'no', 'label' => 'No'], | |
| 319 | - ['value' => 'yes', 'label' => 'Yes'], | |
| 365 | + ['value' => 'no', 'label' => __('No', 'jc-importer')], | |
| 366 | + ['value' => 'yes', 'label' => __('Yes', 'jc-importer')], | |
| 320 | 367 | ], |
| 321 | 368 | 'type' => 'select', |
| 322 | - 'tooltip' => __('Enable/Disable the fields to import attachment meta data.', 'importwp') | |
| 369 | + 'tooltip' => __('Enable/Disable the fields to import attachment meta data.', 'jc-importer') | |
| 323 | 370 | ]), |
| 324 | - $this->register_field('Alt Text', '_alt', [ | |
| 371 | + $this->register_field(__('Alt Text', 'jc-importer'), '_alt', [ | |
| 325 | 372 | 'condition' => ['_enabled', '==', 'yes'], |
| 326 | - 'tooltip' => __('Image attachment alt text.', 'importwp'), | |
| 373 | + 'tooltip' => __('Image attachment alt text.', 'jc-importer'), | |
| 327 | 374 | ]), |
| 328 | - $this->register_field('Title Text', '_title', [ | |
| 375 | + $this->register_field(__('Title Text', 'jc-importer'), '_title', [ | |
| 329 | 376 | 'condition' => ['_enabled', '==', 'yes'], |
| 330 | - 'tooltip' => __('Attachments title text.', 'importwp') | |
| 377 | + 'tooltip' => __('Attachments title text.', 'jc-importer') | |
| 331 | 378 | ]), |
| 332 | - $this->register_field('Caption Text', '_caption', [ | |
| 379 | + $this->register_field(__('Caption Text', 'jc-importer'), '_caption', [ | |
| 333 | 380 | 'condition' => ['_enabled', '==', 'yes'], |
| 334 | - 'tooltip' => __('Image attachments caption text.', 'importwp') | |
| 381 | + 'tooltip' => __('Image attachments caption text.', 'jc-importer') | |
| 335 | 382 | ]), |
| 336 | - $this->register_field('Description Text', '_description', [ | |
| 383 | + $this->register_field(__('Description Text', 'jc-importer'), '_description', [ | |
| 337 | 384 | 'condition' => ['_enabled', '==', 'yes'], |
| 338 | - 'tooltip' => __('Attachments description text.', 'importwp') | |
| 385 | + 'tooltip' => __('Attachments description text.', 'jc-importer') | |
| 339 | 386 | ]) |
| 340 | - ]), | |
| 341 | - ]), ['type' => 'settings', 'condition' => $display_conditions]); | |
| 387 | + ]); | |
| 388 | + } | |
| 389 | + | |
| 390 | + return $this->register_group(__('Settings', 'jc-importer'), 'settings', array_merge($extra_fields, $fields), ['type' => 'settings', 'condition' => $display_conditions]); | |
| 342 | 391 | } |
| 343 | 392 | |
| 344 | - public function register_attachment_fields($label = 'Attachments', $name = 'attachments', $field_label = 'Location', $group_args = null) | |
| 393 | + public function register_attachment_fields($label = 'Images & Attachments', $name = 'attachments', $field_label = 'Location', $group_args = null, $attachment_args = []) | |
| 345 | 394 | { |
| 346 | 395 | if (is_null($group_args)) { |
| 347 | - $group_args = ['type' => 'repeatable', 'row_base' => true]; | |
| 396 | + $group_args = ['type' => 'repeatable', 'row_base' => true, 'row_summary' => ['location'], 'link' => 'https://www.importwp.com/docs/how-to-import-wordpress-attachments-onto-a-post-type/']; | |
| 397 | + } else if (!isset($group_args['row_summary'])) { | |
| 398 | + $group_args['row_summary'] = ['location']; | |
| 348 | 399 | } |
| 400 | + | |
| 401 | + $display_conditions = isset($attachment_args['conditions']) ? $attachment_args['conditions'] : []; | |
| 402 | + $extra_fields = isset($attachment_args['extra_fields']) ? $attachment_args['extra_fields'] : []; | |
| 403 | + $disabled_fields = isset($attachment_args['disabled_fields']) ? $attachment_args['disabled_fields'] : []; | |
| 404 | + | |
| 349 | 405 | return $this->register_group($label, $name, [ |
| 350 | 406 | $this->register_field($field_label, 'location', [ |
| 351 | - 'tooltip' => __('The source location of the file being attached.', 'importwp') | |
| 407 | + 'tooltip' => __('The source location of the file being attached.', 'jc-importer') | |
| 352 | 408 | ]), |
| 353 | - $this->_register_attachment_setting_fields() | |
| 409 | + $this->_register_attachment_setting_fields($display_conditions, $extra_fields, $disabled_fields) | |
| 354 | 410 | ], $group_args); |
| 355 | 411 | } |
| 356 | 412 | |
| 357 | 413 | public function get_field_options($field_name, $id) |
| @@ -453,8 +509,20 @@ | ||
| 453 | 509 | $field_groups['default']['fields'][$field_id] = $field_value; |
| 454 | 510 | } |
| 455 | 511 | } |
| 456 | 512 | |
| 513 | + if ($this->importer->has_custom_unique_identifier()) { | |
| 514 | + | |
| 515 | + $ref = $this->importer->getSetting('unique_identifier_ref'); | |
| 516 | + | |
| 517 | + $field_groups['iwp'] = [ | |
| 518 | + 'id' => 'iwp', | |
| 519 | + 'fields' => [ | |
| 520 | + $this->importer->get_iwp_reference_meta_key() => (!is_null($ref) ? $ref : '') | |
| 521 | + ] | |
| 522 | + ]; | |
| 523 | + } | |
| 524 | + | |
| 457 | 525 | return $field_groups; |
| 458 | 526 | } |
| 459 | 527 | |
| 460 | 528 | /** |
| @@ -496,19 +564,21 @@ | ||
| 496 | 564 | { |
| 497 | 565 | // Allows virtual groups to be registered |
| 498 | 566 | $this->groups = $this->event_handler->run('template.pre_process_groups', [$this->groups, $data, $this]); |
| 499 | 567 | |
| 500 | - $map = $data->getData('default'); | |
| 501 | - foreach ($this->groups as $group) { | |
| 502 | - $group_map = []; | |
| 568 | + if (is_array($this->groups) && !empty($this->groups)) { | |
| 569 | + $map = $data->getData('default'); | |
| 570 | + foreach ($this->groups as $group) { | |
| 571 | + $group_map = []; | |
| 503 | 572 | |
| 504 | - foreach ($map as $field_key => $fields_map) { | |
| 505 | - if (preg_match('/^' . $group . '\.(.*?)$/', $field_key) === 1) { | |
| 506 | - $group_map[$field_key] = $fields_map; | |
| 573 | + foreach ($map as $field_key => $fields_map) { | |
| 574 | + if (preg_match('/^' . $group . '\.(.*?)$/', $field_key) === 1) { | |
| 575 | + $group_map[$field_key] = $fields_map; | |
| 576 | + } | |
| 507 | 577 | } |
| 578 | + | |
| 579 | + $data->replace($group_map, $group); | |
| 508 | 580 | } |
| 509 | - | |
| 510 | - $data->replace($group_map, $group); | |
| 511 | 581 | } |
| 512 | 582 | |
| 513 | 583 | return $data; |
| 514 | 584 | } |
| @@ -634,16 +704,97 @@ | ||
| 634 | 704 | if (empty($source)) { |
| 635 | 705 | continue 2; |
| 636 | 706 | } |
| 637 | 707 | |
| 638 | - $custom_filename = apply_filters('iwp/attachment/filename', null, $source); | |
| 639 | 708 | |
| 709 | + $attachment_salt = file_exists($source) ? md5_file($source) : ''; | |
| 640 | 710 | $attachment_id = 0; |
| 641 | 711 | if ($attachment_enable_image_hash == 'yes') { |
| 642 | 712 | $attachment_id = $attachment->get_attachment_by_hash($source); |
| 643 | 713 | } |
| 644 | 714 | |
| 715 | + // check to see if remote url matches file existing in media library | |
| 645 | 716 | if ($attachment_id <= 0) { |
| 717 | + $dir = wp_get_upload_dir(); | |
| 718 | + if (str_starts_with($source, $dir['baseurl'] . '/')) { | |
| 719 | + $attachment_id = attachment_url_to_postid($source); | |
| 720 | + } | |
| 721 | + } | |
| 722 | + | |
| 723 | + if ($attachment_id <= 0) { | |
| 724 | + | |
| 725 | + $main_zip_file = false; | |
| 726 | + if ($base_url === 'iwp_zip') { | |
| 727 | + $main_zip = get_post_meta($this->importer->getId(), '_iwp_zip', true); | |
| 728 | + if ($main_zip && file_exists($main_zip)) { | |
| 729 | + $base_url .= '://' . $main_zip; | |
| 730 | + $main_zip_file = $main_zip; | |
| 731 | + } | |
| 732 | + } | |
| 733 | + | |
| 734 | + $zip = $this->get_zip_source($base_url, $filesystem); | |
| 735 | + if ($zip !== false) { | |
| 736 | + | |
| 737 | + if (!$main_zip_file && isset($zip['src'])) { | |
| 738 | + | |
| 739 | + /** | |
| 740 | + * @var \ImportWP\Common\Http\Http $http | |
| 741 | + */ | |
| 742 | + $http = Container::getInstance()->get('http'); | |
| 743 | + $result = $http->download_file($zip['src'], $zip['name']); | |
| 744 | + if (is_wp_error($result)) { | |
| 745 | + break; | |
| 746 | + } | |
| 747 | + | |
| 748 | + if (!$result) { | |
| 749 | + $result = new \WP_Error('IWP_HTTP_2', __('Error downloading zip file', 'jc-importer')); | |
| 750 | + break; | |
| 751 | + } | |
| 752 | + } | |
| 753 | + | |
| 754 | + $result = $this->get_file_from_zip($main_zip_file ? $main_zip_file : $zip['name'], $location, $filesystem); | |
| 755 | + if (!$result) { | |
| 756 | + continue 2; | |
| 757 | + } | |
| 758 | + | |
| 759 | + break; | |
| 760 | + } | |
| 761 | + } | |
| 762 | + | |
| 763 | + if (apply_filters('iwp/template/process_attachment/enable_file_size_hash', false) === true) { | |
| 764 | + | |
| 765 | + // check to see if the remote url image is the same size as the one on disk. | |
| 766 | + if ($attachment_id > 0 && $attachment_enable_image_hash == 'yes') { | |
| 767 | + | |
| 768 | + $existing_file = get_attached_file($attachment_id, true); | |
| 769 | + | |
| 770 | + // Remove -scaled from the file name if it exists | |
| 771 | + if ($existing_file && file_exists($existing_file)) { | |
| 772 | + $existing_file = str_replace('-scaled.', '.', $existing_file); | |
| 773 | + } | |
| 774 | + | |
| 775 | + if ($existing_file && file_exists($existing_file)) { | |
| 776 | + $head = wp_remote_head($source); | |
| 777 | + if (!is_wp_error($head)) { | |
| 778 | + | |
| 779 | + // get file size from the header | |
| 780 | + $header_key = apply_filters('iwp/template/process_attachment/remote_file_size_header_key', 'content-length'); | |
| 781 | + $size = wp_remote_retrieve_header($head, $header_key); | |
| 782 | + $existing_file_size = filesize($existing_file); | |
| 783 | + | |
| 784 | + if ($size != $existing_file_size) { | |
| 785 | + | |
| 786 | + // append the size to the attachment salt | |
| 787 | + $attachment_salt .= $size; | |
| 788 | + $attachment_id = $attachment->get_attachment_by_hash($source, $attachment_salt); | |
| 789 | + } | |
| 790 | + } | |
| 791 | + } | |
| 792 | + } | |
| 793 | + } | |
| 794 | + | |
| 795 | + $custom_filename = apply_filters('iwp/attachment/filename', null, $source); | |
| 796 | + if ($attachment_id <= 0) { | |
| 646 | 797 | Logger::write(__CLASS__ . '::process__attachments -remote=' . $source . ' -filename=' . $custom_filename); |
| 647 | 798 | $result = $filesystem->download_file($source, null, null, $custom_filename); |
| 648 | 799 | } |
| 649 | 800 | break; |
| @@ -686,16 +837,46 @@ | ||
| 686 | 837 | if (empty($source)) { |
| 687 | 838 | continue 2; |
| 688 | 839 | } |
| 689 | 840 | |
| 690 | - $custom_filename = apply_filters('iwp/attachment/filename', null, $source); | |
| 691 | - | |
| 841 | + $attachment_salt = file_exists($source) ? md5_file($source) : ''; | |
| 692 | 842 | $attachment_id = 0; |
| 693 | 843 | if ($attachment_enable_image_hash == 'yes') { |
| 694 | - $attachment_id = $attachment->get_attachment_by_hash($source); | |
| 844 | + $attachment_id = $attachment->get_attachment_by_hash($source, $attachment_salt); | |
| 695 | 845 | } |
| 696 | 846 | |
| 697 | 847 | if ($attachment_id <= 0) { |
| 848 | + | |
| 849 | + $main_zip_file = false; | |
| 850 | + if ($base_url === 'iwp_zip') { | |
| 851 | + $main_zip = get_post_meta($this->importer->getId(), '_iwp_zip', true); | |
| 852 | + if ($main_zip && file_exists($main_zip)) { | |
| 853 | + $base_url .= '://' . $main_zip; | |
| 854 | + $main_zip_file = $main_zip; | |
| 855 | + } | |
| 856 | + } | |
| 857 | + | |
| 858 | + $zip = $this->get_zip_source($base_url, $filesystem); | |
| 859 | + if ($zip !== false) { | |
| 860 | + | |
| 861 | + if (!$main_zip_file && isset($zip['src'])) { | |
| 862 | + $result = $ftp->download_file($zip['src'], $ftp_host, $ftp_user, $ftp_pass, $zip['name']); | |
| 863 | + if (is_wp_error($result)) { | |
| 864 | + break; | |
| 865 | + } | |
| 866 | + } | |
| 867 | + | |
| 868 | + $result = $this->get_file_from_zip($main_zip_file ? $main_zip_file : $zip['name'], $location, $filesystem); | |
| 869 | + if (!$result) { | |
| 870 | + continue 2; | |
| 871 | + } | |
| 872 | + | |
| 873 | + break; | |
| 874 | + } | |
| 875 | + } | |
| 876 | + | |
| 877 | + $custom_filename = apply_filters('iwp/attachment/filename', null, $source); | |
| 878 | + if ($attachment_id <= 0) { | |
| 698 | 879 | Logger::write(__CLASS__ . '::process__attachments -ftp=' . $source . ' -filename=' . $custom_filename); |
| 699 | 880 | $result = $ftp->download_file($source, $ftp_host, $ftp_user, $ftp_pass, $custom_filename); |
| 700 | 881 | } |
| 701 | 882 | break; |
| @@ -714,15 +895,46 @@ | ||
| 714 | 895 | if (empty($source)) { |
| 715 | 896 | continue 2; |
| 716 | 897 | } |
| 717 | 898 | |
| 718 | - $custom_filename = apply_filters('iwp/attachment/filename', null, $source); | |
| 719 | 899 | $attachment_salt = file_exists($source) ? md5_file($source) : ''; |
| 720 | 900 | $attachment_id = 0; |
| 721 | 901 | if ($attachment_enable_image_hash == 'yes') { |
| 722 | 902 | $attachment_id = $attachment->get_attachment_by_hash($source, $attachment_salt); |
| 723 | 903 | } |
| 904 | + | |
| 724 | 905 | if ($attachment_id <= 0) { |
| 906 | + | |
| 907 | + $main_zip_file = false; | |
| 908 | + if ($base_url === 'iwp_zip') { | |
| 909 | + $main_zip = get_post_meta($this->importer->getId(), '_iwp_zip', true); | |
| 910 | + if ($main_zip && file_exists($main_zip)) { | |
| 911 | + $base_url .= '://' . $main_zip; | |
| 912 | + $main_zip_file = $main_zip; | |
| 913 | + } | |
| 914 | + } | |
| 915 | + | |
| 916 | + $zip = $this->get_zip_source($base_url, $filesystem); | |
| 917 | + if ($zip !== false) { | |
| 918 | + | |
| 919 | + if (!$main_zip_file && isset($zip['src'])) { | |
| 920 | + $result = $filesystem->copy($zip['src'], $zip['name']); | |
| 921 | + if (is_wp_error($result)) { | |
| 922 | + break; | |
| 923 | + } | |
| 924 | + } | |
| 925 | + | |
| 926 | + $result = $this->get_file_from_zip($main_zip_file ? $main_zip_file : $zip['name'], $location, $filesystem); | |
| 927 | + if (!$result) { | |
| 928 | + continue 2; | |
| 929 | + } | |
| 930 | + | |
| 931 | + break; | |
| 932 | + } | |
| 933 | + } | |
| 934 | + | |
| 935 | + $custom_filename = apply_filters('iwp/attachment/filename', null, $source); | |
| 936 | + if ($attachment_id <= 0) { | |
| 725 | 937 | Logger::write(__CLASS__ . '::process__attachments -local=' . $source . ' -filename=' . $custom_filename); |
| 726 | 938 | $result = $filesystem->copy_file($source, null, $custom_filename); |
| 727 | 939 | } |
| 728 | 940 | break; |
| @@ -766,8 +978,32 @@ | ||
| 766 | 978 | $attachment_args['caption'] = isset($attachment_captions[$location_counter]) ? $attachment_captions[$location_counter] : null; |
| 767 | 979 | $attachment_args['description'] = isset($attachment_descriptions[$location_counter]) ? $attachment_descriptions[$location_counter] : null;; |
| 768 | 980 | } |
| 769 | 981 | |
| 982 | + // resize attachment | |
| 983 | + if ($crop_details = apply_filters('iwp/importer/template/process_attachment/resize', '__return_false')) { | |
| 984 | + | |
| 985 | + if (is_array($crop_details) && count($crop_details) == 3 && file_exists($result['dest'])) { | |
| 986 | + | |
| 987 | + list($max_w, $max_h, $crop) = $crop_details; | |
| 988 | + | |
| 989 | + if (!is_null($max_w)) { | |
| 990 | + $max_w = absint($max_w); | |
| 991 | + } | |
| 992 | + if (!is_null($max_h)) { | |
| 993 | + $max_h = absint($max_h); | |
| 994 | + } | |
| 995 | + | |
| 996 | + $editor = wp_get_image_editor($result['dest']); | |
| 997 | + if (!is_wp_error($editor)) { | |
| 998 | + $editor->resize($max_w, $max_h, (bool)$crop); | |
| 999 | + $editor->save($result['dest']); | |
| 1000 | + } else { | |
| 1001 | + Logger::write(__CLASS__ . '::process__attachments -resize -error=' . $editor->get_error_message()); | |
| 1002 | + } | |
| 1003 | + } | |
| 1004 | + } | |
| 1005 | + | |
| 770 | 1006 | $attachment_id = $attachment->insert_attachment($post_id, $result['dest'], $result['mime'], $attachment_args); |
| 771 | 1007 | if (is_wp_error($attachment_id)) { |
| 772 | 1008 | Logger::write(__CLASS__ . '::process__attachments -error=' . $attachment_id->get_error_message()); |
| 773 | 1009 | continue; |
| @@ -821,8 +1057,47 @@ | ||
| 821 | 1057 | return $attachment_ids; |
| 822 | 1058 | } |
| 823 | 1059 | |
| 824 | 1060 | /** |
| 1061 | + * Remove custom field meta fields from the provided array based on importer permissions. | |
| 1062 | + * | |
| 1063 | + * @param string[string] $custom_fields | |
| 1064 | + * @param ParsedData $data | |
| 1065 | + * @param string $group_name | |
| 1066 | + * @param string $permission_key | |
| 1067 | + * @param string $prefix | |
| 1068 | + * @return string[string] | |
| 1069 | + */ | |
| 1070 | + public function process_attachment_meta_permissions($custom_fields, $data, $group_name, $permission_key, $prefix) | |
| 1071 | + { | |
| 1072 | + $allowed = $data->permission()->validate([ | |
| 1073 | + $permission_key . '._alt' => '', | |
| 1074 | + $permission_key . '._title' => '', | |
| 1075 | + $permission_key . '._caption' => '', | |
| 1076 | + $permission_key . '._description' => '' | |
| 1077 | + ], $data->getMethod(), $group_name); | |
| 1078 | + | |
| 1079 | + if (!isset($allowed[$permission_key . '._alt'])) { | |
| 1080 | + // remove alt | |
| 1081 | + unset($custom_fields[$prefix . 'settings._meta._alt']); | |
| 1082 | + } | |
| 1083 | + if (!isset($allowed[$permission_key . '._title'])) { | |
| 1084 | + // remove title | |
| 1085 | + unset($custom_fields[$prefix . 'settings._meta._title']); | |
| 1086 | + } | |
| 1087 | + if (!isset($allowed[$permission_key . '._caption'])) { | |
| 1088 | + // remove caption | |
| 1089 | + unset($custom_fields[$prefix . 'settings._meta._caption']); | |
| 1090 | + } | |
| 1091 | + if (!isset($allowed[$permission_key . '._description'])) { | |
| 1092 | + // remove description | |
| 1093 | + unset($custom_fields[$prefix . 'settings._meta._description']); | |
| 1094 | + } | |
| 1095 | + | |
| 1096 | + return $custom_fields; | |
| 1097 | + } | |
| 1098 | + | |
| 1099 | + /** | |
| 825 | 1100 | * Add error message |
| 826 | 1101 | * |
| 827 | 1102 | * @param string|\WP_Error $error |
| 828 | 1103 | * @return void |
| @@ -845,6 +1120,115 @@ | ||
| 845 | 1120 | */ |
| 846 | 1121 | public function generate_field_map($fields, $importer) |
| 847 | 1122 | { |
| 848 | 1123 | return ['enabled' => [], 'map' => []]; |
| 1124 | + } | |
| 1125 | + | |
| 1126 | + public function get_zip_source($base_url, $filesystem) | |
| 1127 | + { | |
| 1128 | + $zip_parts = []; | |
| 1129 | + if (preg_match('/^iwp_zip:\/\/(.*?)$/', $base_url, $zip_parts) === 1) { | |
| 1130 | + | |
| 1131 | + $iwp_tmp = $filesystem->get_temp_directory(); | |
| 1132 | + | |
| 1133 | + $iwp_tmp .= DIRECTORY_SEPARATOR . 'zip'; | |
| 1134 | + if (!is_dir($iwp_tmp)) { | |
| 1135 | + mkdir($iwp_tmp); | |
| 1136 | + } | |
| 1137 | + | |
| 1138 | + $hash = md5($base_url); | |
| 1139 | + $iwp_tmp .= DIRECTORY_SEPARATOR . $hash . '.zip'; | |
| 1140 | + if (!file_exists($iwp_tmp)) { | |
| 1141 | + return ['src' => $zip_parts[1], 'name' => $iwp_tmp]; | |
| 1142 | + } | |
| 1143 | + | |
| 1144 | + return ['name' => $iwp_tmp]; | |
| 1145 | + } | |
| 1146 | + | |
| 1147 | + return false; | |
| 1148 | + } | |
| 1149 | + | |
| 1150 | + public function get_file_from_zip($zip_path, $filename, $filesystem) | |
| 1151 | + { | |
| 1152 | + $zip = new \ZipArchive(); | |
| 1153 | + if ($zip->open($zip_path) === true) { | |
| 1154 | + $output_data = $zip->getFromName($filename); | |
| 1155 | + if ($output_data === false) { | |
| 1156 | + return false; | |
| 1157 | + } | |
| 1158 | + | |
| 1159 | + return $filesystem->string_to_file($output_data, $filename); | |
| 1160 | + } | |
| 1161 | + | |
| 1162 | + return false; | |
| 1163 | + } | |
| 1164 | + | |
| 1165 | + public function try_use_main_zip($base_url, $location) | |
| 1166 | + { | |
| 1167 | + if ($base_url === 'iwp_zip') { | |
| 1168 | + | |
| 1169 | + $main_zip = get_post_meta($this->importer->getId(), '_iwp_zip', true); | |
| 1170 | + $source = $main_zip . $location; | |
| 1171 | + $base_url .= $main_zip; | |
| 1172 | + } | |
| 1173 | + } | |
| 1174 | + | |
| 1175 | + public function get_permission_fields($importer_model) | |
| 1176 | + { | |
| 1177 | + return []; | |
| 1178 | + } | |
| 1179 | + | |
| 1180 | + public function register_settings() {} | |
| 1181 | + | |
| 1182 | + public function register_options() | |
| 1183 | + { | |
| 1184 | + return []; | |
| 1185 | + } | |
| 1186 | + | |
| 1187 | + public function get_default_enabled_fields() | |
| 1188 | + { | |
| 1189 | + return $this->default_enabled_fields; | |
| 1190 | + } | |
| 1191 | + | |
| 1192 | + | |
| 1193 | + public function get_unique_identifier_options($importer_model, $unique_fields = []) | |
| 1194 | + { | |
| 1195 | + $output = []; | |
| 1196 | + return $output; | |
| 1197 | + } | |
| 1198 | + | |
| 1199 | + public function get_unique_identifier_options_from_map($importer_model, $unique_fields, $field_map, $optional_fields) | |
| 1200 | + { | |
| 1201 | + $output = []; | |
| 1202 | + $mapped_data = $importer_model->getMap(); | |
| 1203 | + | |
| 1204 | + foreach ($field_map as $field_id => $field_map_key) { | |
| 1205 | + | |
| 1206 | + if (isset($output[$field_id])) { | |
| 1207 | + continue; | |
| 1208 | + } | |
| 1209 | + | |
| 1210 | + $output[$field_id] = [ | |
| 1211 | + 'value' => $field_id, | |
| 1212 | + 'label' => $field_id, | |
| 1213 | + 'uid' => false, | |
| 1214 | + 'active' => false, | |
| 1215 | + ]; | |
| 1216 | + | |
| 1217 | + if (in_array($field_id, $unique_fields)) { | |
| 1218 | + $output[$field_id]['uid'] = true; | |
| 1219 | + } | |
| 1220 | + | |
| 1221 | + if (!isset($mapped_data[$field_map_key]) || empty($mapped_data[$field_map_key])) { | |
| 1222 | + continue; | |
| 1223 | + } | |
| 1224 | + | |
| 1225 | + if (in_array($field_id, $optional_fields) && true !== $importer_model->isEnabledField($field_map_key)) { | |
| 1226 | + continue; | |
| 1227 | + } | |
| 1228 | + | |
| 1229 | + $output[$field_id]['active'] = true; | |
| 1230 | + } | |
| 1231 | + | |
| 1232 | + return $output; | |
| 849 | 1233 | } |
| 850 | 1234 | } |