| @@ -3,8 +3,9 @@ | ||
| 3 | 3 | namespace ImportWP\Common\Importer\Template; |
| 4 | 4 | |
| 5 | 5 | use ImportWP\Common\Importer\ParsedData; |
| 6 | 6 | use ImportWP\Common\Importer\TemplateInterface; |
| 7 | +use ImportWP\Common\Model\ImporterModel; | |
| 7 | 8 | use ImportWP\EventHandler; |
| 8 | 9 | |
| 9 | 10 | class TermTemplate extends Template implements TemplateInterface |
| 10 | 11 | { |
| @@ -10,8 +11,23 @@ | ||
| 10 | 11 | { |
| 11 | 12 | protected $name = 'Term'; |
| 12 | 13 | protected $mapper = 'term'; |
| 13 | 14 | |
| 15 | + protected $field_map = [ | |
| 16 | + 'term_id' => 'term.term_id', | |
| 17 | + 'name' => 'term.name', | |
| 18 | + 'description' => 'term.description', | |
| 19 | + 'slug' => 'term.slug', | |
| 20 | + 'alias_of' => 'term.alias_of' | |
| 21 | + ]; | |
| 22 | + | |
| 23 | + protected $optional_fields = [ | |
| 24 | + 'term_id', | |
| 25 | + 'description', | |
| 26 | + 'slug', | |
| 27 | + 'alias_of', | |
| 28 | + ]; | |
| 29 | + | |
| 14 | 30 | public function __construct(EventHandler $event_handler) |
| 15 | 31 | { |
| 16 | 32 | parent::__construct($event_handler); |
| 17 | 33 | |
| @@ -39,47 +55,52 @@ | ||
| 39 | 55 | |
| 40 | 56 | public function register() |
| 41 | 57 | { |
| 42 | 58 | return [ |
| 43 | - $this->register_group('Term', 'term', [ | |
| 44 | - $this->register_field('ID', 'term_id', [ | |
| 45 | - 'tooltip' => __('ID is only used to reference existing records', 'importwp') | |
| 59 | + $this->register_group(__('Term', 'jc-importer'), 'term', [ | |
| 60 | + $this->register_field(__('ID', 'jc-importer'), 'term_id', [ | |
| 61 | + 'tooltip' => __('ID is only used to reference existing records', 'jc-importer') | |
| 46 | 62 | ]), |
| 47 | - $this->register_core_field('Name', 'name', [ | |
| 48 | - 'tooltip' => __('The term name', 'importwp') | |
| 63 | + $this->register_core_field(__('Name', 'jc-importer'), 'name', [ | |
| 64 | + 'tooltip' => __('The term name', 'jc-importer') | |
| 49 | 65 | ]), |
| 50 | - $this->register_field('Description', 'description', [ | |
| 51 | - 'tooltip' => __('The term description', 'importwp') | |
| 66 | + $this->register_field(__('Description', 'jc-importer'), 'description', [ | |
| 67 | + 'tooltip' => __('The term description', 'jc-importer') | |
| 52 | 68 | ]), |
| 53 | - $this->register_field('Slug', 'slug', [ | |
| 54 | - 'tooltip' => __('The term slug to use', 'importwp') | |
| 69 | + $this->register_field(__('Slug', 'jc-importer'), 'slug', [ | |
| 70 | + 'tooltip' => __('The term slug to use', 'jc-importer') | |
| 55 | 71 | ]), |
| 56 | - $this->register_group('Parent Settings', '_parent', [ | |
| 57 | - $this->register_field('Parent', 'parent', [ | |
| 72 | + $this->register_group(__('Parent Settings', 'jc-importer'), '_parent', [ | |
| 73 | + $this->register_field(__('Parent', 'jc-importer'), 'parent', [ | |
| 58 | 74 | 'default' => '', |
| 59 | 75 | 'options' => 'callback', |
| 60 | - 'tooltip' => __('The id of the parent term', 'importwp') | |
| 76 | + 'tooltip' => __('The id of the parent term', 'jc-importer') | |
| 61 | 77 | ]), |
| 62 | - $this->register_field('Parent Field Type', '_parent_type', [ | |
| 78 | + $this->register_field(__('Parent Field Type', 'jc-importer'), '_parent_type', [ | |
| 63 | 79 | 'default' => 'id', |
| 64 | 80 | 'options' => [ |
| 65 | - ['value' => 'id', 'label' => 'ID'], | |
| 66 | - ['value' => 'slug', 'label' => 'Slug'], | |
| 67 | - ['value' => 'name', 'label' => 'Name'], | |
| 68 | - ['value' => 'column', 'label' => 'Reference Column'], | |
| 81 | + ['value' => 'id', 'label' => __('ID', 'jc-importer')], | |
| 82 | + ['value' => 'slug', 'label' => __('Slug', 'jc-importer')], | |
| 83 | + ['value' => 'name', 'label' => __('Name', 'jc-importer')], | |
| 84 | + ['value' => 'column', 'label' => __('Reference Column', 'jc-importer')], | |
| 85 | + ['value' => 'custom_field', 'label' => __('Custom Field', 'jc-importer')], | |
| 69 | 86 | ], |
| 70 | 87 | 'type' => 'select', |
| 71 | - 'tooltip' => __('Select how the parent field should be handled', 'importwp') | |
| 88 | + 'tooltip' => __('Select how the parent field should be handled', 'jc-importer') | |
| 72 | 89 | ]), |
| 73 | - $this->register_field('Parent Reference Column', '_parent_ref', [ | |
| 90 | + $this->register_field(__('Parent Reference Column', 'jc-importer'), '_parent_ref', [ | |
| 74 | 91 | 'condition' => ['_parent_type', '==', 'column'], |
| 75 | - 'tooltip' => __('Select the column/node that the parent field is referencing', 'importwp') | |
| 92 | + 'tooltip' => __('Select the column/node that the parent field is referencing', 'jc-importer') | |
| 93 | + ]), | |
| 94 | + $this->register_field(__('Custom Field Key', 'jc-importer'), '_custom_field', [ | |
| 95 | + 'condition' => ['_parent_type', '==', 'custom_field'], | |
| 96 | + 'tooltip' => __('Enter the name of the custom field.', 'jc-importer') | |
| 76 | 97 | ]) |
| 77 | 98 | ]), |
| 78 | - $this->register_field('Alias of', 'alias_of', [ | |
| 79 | - 'tooltip' => __('Slug of the term to make this term an alias of', 'importwp') | |
| 99 | + $this->register_field(__('Alias of', 'jc-importer'), 'alias_of', [ | |
| 100 | + 'tooltip' => __('Slug of the term to make this term an alias of', 'jc-importer') | |
| 80 | 101 | ]), |
| 81 | - ]) | |
| 102 | + ], ['link' => 'https://www.importwp.com/docs/wordpress-taxonomy-importer-template/']) | |
| 82 | 103 | ]; |
| 83 | 104 | } |
| 84 | 105 | |
| 85 | 106 | public function register_settings() |
| @@ -180,12 +201,19 @@ | ||
| 180 | 201 | $parent_id = intval($term_field_map['parent']); |
| 181 | 202 | } elseif ($parent_field_type === 'column') { |
| 182 | 203 | |
| 183 | 204 | // reference column |
| 184 | - $temp_id = $this->get_term_by_cf('term_parent', $term_field_map['parent']); | |
| 205 | + $temp_id = $this->get_term_by_cf('_iwp_ref_term_parent', $term_field_map['parent']); | |
| 185 | 206 | if (intval($temp_id > 0)) { |
| 186 | 207 | $parent_id = intval($temp_id); |
| 187 | 208 | } |
| 209 | + } elseif ($parent_field_type === 'custom_field') { | |
| 210 | + | |
| 211 | + $parent_custom_field = $data->getValue('term._parent._custom_field'); | |
| 212 | + $temp_id = $this->get_term_by_cf($parent_custom_field, $term_field_map['parent']); | |
| 213 | + if (intval($temp_id > 0)) { | |
| 214 | + $parent_id = intval($temp_id); | |
| 215 | + } | |
| 188 | 216 | } |
| 189 | 217 | |
| 190 | 218 | if ($parent_id > 0) { |
| 191 | 219 | $term_field_map['parent'] = $parent_id; |
| @@ -216,9 +244,9 @@ | ||
| 216 | 244 | 'fields' => 'ids', |
| 217 | 245 | 'hide_empty' => false, |
| 218 | 246 | 'meta_query' => [ |
| 219 | 247 | [ |
| 220 | - 'key' => '_iwp_ref_' . $field, | |
| 248 | + 'key' => $field, | |
| 221 | 249 | 'value' => $value, |
| 222 | 250 | 'compare' => '=' |
| 223 | 251 | ] |
| 224 | 252 | ] |
| @@ -254,6 +282,104 @@ | ||
| 254 | 282 | 'label' => $term->name |
| 255 | 283 | ]; |
| 256 | 284 | } |
| 257 | 285 | return $result; |
| 286 | + } | |
| 287 | + | |
| 288 | + /** | |
| 289 | + * Convert fields/headings to data map | |
| 290 | + * | |
| 291 | + * @param mixed $fields | |
| 292 | + * @param ImporterModel $importer | |
| 293 | + * @return array | |
| 294 | + */ | |
| 295 | + public function generate_field_map($fields, $importer) | |
| 296 | + { | |
| 297 | + $result = parent::generate_field_map($fields, $importer); | |
| 298 | + $map = $result['map']; | |
| 299 | + $enabled = $result['enabled']; | |
| 300 | + | |
| 301 | + $parent = []; | |
| 302 | + | |
| 303 | + foreach ($fields as $index => $field) { | |
| 304 | + if (preg_match('/^parent\.(.*?)$/', $field, $matches) === 1) { | |
| 305 | + | |
| 306 | + // Capture parent | |
| 307 | + // parent.id,parent.name,parent.slug | |
| 308 | + if (!isset($parent['map'])) { | |
| 309 | + $parent['map'] = []; | |
| 310 | + } | |
| 311 | + | |
| 312 | + $parent['map'][$matches[1]] = sprintf('{%s}', $index); | |
| 313 | + } elseif (isset($this->field_map[$field])) { | |
| 314 | + | |
| 315 | + // Handle core fields | |
| 316 | + $field_key = $this->field_map[$field]; | |
| 317 | + $map[$field_key] = sprintf('{%s}', $index); | |
| 318 | + | |
| 319 | + if (in_array($field, $this->optional_fields)) { | |
| 320 | + $enabled[] = $field_key; | |
| 321 | + } | |
| 322 | + | |
| 323 | + if (in_array($field, ['role'])) { | |
| 324 | + $map[$field_key . '._enable_text'] = 'yes'; | |
| 325 | + } | |
| 326 | + } | |
| 327 | + } | |
| 328 | + | |
| 329 | + if (!empty($parent)) { | |
| 330 | + | |
| 331 | + // parent.id,parent.name,parent.slug | |
| 332 | + $enabled_key = 'term._parent'; | |
| 333 | + if (isset($parent['map']['name'])) { | |
| 334 | + | |
| 335 | + $enabled[] = $enabled_key; | |
| 336 | + $map['term._parent.parent'] = $parent['map']['name']; | |
| 337 | + $map['term._parent.parent._enable_text'] = 'yes'; | |
| 338 | + $map['term._parent._parent_type'] = 'name'; | |
| 339 | + } elseif (isset($parent['map']['slug'])) { | |
| 340 | + | |
| 341 | + $enabled[] = $enabled_key; | |
| 342 | + $map['term._parent.parent'] = $parent['map']['slug']; | |
| 343 | + $map['term._parent.parent._enable_text'] = 'yes'; | |
| 344 | + $map['term._parent._parent_type'] = 'slug'; | |
| 345 | + } elseif (isset($parent['map']['id'])) { | |
| 346 | + | |
| 347 | + $enabled[] = $enabled_key; | |
| 348 | + $map['term._parent.parent'] = $parent['map']['name']; | |
| 349 | + $map['term._parent.parent._enable_text'] = 'yes'; | |
| 350 | + $map['term._parent._parent_type'] = 'id'; | |
| 351 | + } | |
| 352 | + } | |
| 353 | + | |
| 354 | + return [ | |
| 355 | + 'map' => $map, | |
| 356 | + 'enabled' => $enabled | |
| 357 | + ]; | |
| 358 | + } | |
| 359 | + | |
| 360 | + public function get_permission_fields($importer_model) | |
| 361 | + { | |
| 362 | + $permission_fields = parent::get_permission_fields($importer_model); | |
| 363 | + | |
| 364 | + $permission_fields['core'] = [ | |
| 365 | + 'term_id' => __('ID', 'jc-importer'), | |
| 366 | + 'name' => __('Name', 'jc-importer'), | |
| 367 | + 'description' => __('Description', 'jc-importer'), | |
| 368 | + 'slug' => __('Slug', 'jc-importer'), | |
| 369 | + 'alias_of' => __('Alias Of', 'jc-importer'), | |
| 370 | + 'parent' => __('Parent', 'jc-importer'), | |
| 371 | + ]; | |
| 372 | + | |
| 373 | + return $permission_fields; | |
| 374 | + } | |
| 375 | + | |
| 376 | + public function get_unique_identifier_options($importer_model, $unique_fields = []) | |
| 377 | + { | |
| 378 | + $output = parent::get_unique_identifier_options($importer_model, $unique_fields); | |
| 379 | + | |
| 380 | + return array_merge( | |
| 381 | + $output, | |
| 382 | + $this->get_unique_identifier_options_from_map($importer_model, $unique_fields, $this->field_map, $this->optional_fields) | |
| 383 | + ); | |
| 258 | 384 | } |
| 259 | 385 | } |