Skip to content
This repository was archived by the owner on Jan 6, 2025. It is now read-only.

Commit 88e5289

Browse files
authored
Merge pull request #13 from kevmarsden/update-taxonomy-import
Update term/taxonomy import
2 parents 4513709 + 4cc9824 commit 88e5289

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

src/Helpers/Migrator.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,7 @@ private function createTaxonomies()
9898
$taxonomy = Taxonomy::findByHandle($taxonomy_slug);
9999

100100
if (! $taxonomy) {
101-
$taxonomy = Taxonomy::make($taxonomy_slug);
102-
}
103-
104-
foreach ($taxonomy_data as $key => $value) {
105-
$taxonomy->set($key, $value);
101+
$taxonomy = Taxonomy::make($taxonomy_slug)->title($taxonomy_data['title']);
106102
}
107103

108104
$taxonomy->save();
@@ -118,20 +114,18 @@ private function createTaxonomyTerms()
118114
{
119115
foreach (Arr::get($this->migration, 'terms', []) as $taxonomy_slug => $terms) {
120116
foreach ($terms as $term_slug => $term_data) {
117+
121118
// Skip if this term was not checked in the summary.
122119
if (! $this->summary['taxonomies'][$taxonomy_slug]['terms'][$term_slug]['_checked']) {
123120
continue;
124121
}
125122

126-
$term = Term::findBySlug($term_slug, $taxonomy_slug);
127-
128-
if (! $term) {
129-
$term = Term::make($term_slug)->taxonomy($taxonomy_slug);
123+
// Skip term creation if this term already exists.
124+
if ($this->summary['taxonomies'][$taxonomy_slug]['terms'][$term_slug]['exists'] === true) {
125+
continue;
130126
}
131127

132-
foreach ($term_data as $key => $value) {
133-
$term->set($key, $value);
134-
}
128+
$term = Term::make()->taxonomy($taxonomy_slug)->slug($term_slug)->set('title', $term_data['title']);
135129

136130
$term->save();
137131
}

src/Helpers/WpImporter.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Exception;
66
use Statamic\Facades\Entry;
77
use Statamic\Facades\Term;
8+
use Statamic\Facades\Taxonomy;
89
use Statamic\Support\Arr;
910

1011
class WpImporter
@@ -59,9 +60,12 @@ public function summary($prepared)
5960
$taxonomy_terms = [];
6061

6162
foreach ($terms as $slug => $term) {
63+
64+
$exists = Taxonomy::find($slug) ? true : false;
65+
6266
$taxonomy_terms[$slug] = [
6367
'slug' => $slug,
64-
'exists' => (bool) Term::query()->where('taxonomy', $taxonomy)->where('slug', $slug)->first(),
68+
'exists' => $exists,
6569
'_checked' => true,
6670
];
6771
}

0 commit comments

Comments
 (0)