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

Commit

Permalink
Merge pull request #13 from kevmarsden/update-taxonomy-import
Browse files Browse the repository at this point in the history
Update term/taxonomy import
  • Loading branch information
jackmcdade authored Oct 17, 2024
2 parents 4513709 + 4cc9824 commit 88e5289
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
18 changes: 6 additions & 12 deletions src/Helpers/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,7 @@ private function createTaxonomies()
$taxonomy = Taxonomy::findByHandle($taxonomy_slug);

if (! $taxonomy) {
$taxonomy = Taxonomy::make($taxonomy_slug);
}

foreach ($taxonomy_data as $key => $value) {
$taxonomy->set($key, $value);
$taxonomy = Taxonomy::make($taxonomy_slug)->title($taxonomy_data['title']);
}

$taxonomy->save();
Expand All @@ -118,20 +114,18 @@ private function createTaxonomyTerms()
{
foreach (Arr::get($this->migration, 'terms', []) as $taxonomy_slug => $terms) {
foreach ($terms as $term_slug => $term_data) {

// Skip if this term was not checked in the summary.
if (! $this->summary['taxonomies'][$taxonomy_slug]['terms'][$term_slug]['_checked']) {
continue;
}

$term = Term::findBySlug($term_slug, $taxonomy_slug);

if (! $term) {
$term = Term::make($term_slug)->taxonomy($taxonomy_slug);
// Skip term creation if this term already exists.
if ($this->summary['taxonomies'][$taxonomy_slug]['terms'][$term_slug]['exists'] === true) {
continue;
}

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

$term->save();
}
Expand Down
6 changes: 5 additions & 1 deletion src/Helpers/WpImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Statamic\Facades\Entry;
use Statamic\Facades\Term;
use Statamic\Facades\Taxonomy;
use Statamic\Support\Arr;

class WpImporter
Expand Down Expand Up @@ -59,9 +60,12 @@ public function summary($prepared)
$taxonomy_terms = [];

foreach ($terms as $slug => $term) {

$exists = Taxonomy::find($slug) ? true : false;

$taxonomy_terms[$slug] = [
'slug' => $slug,
'exists' => (bool) Term::query()->where('taxonomy', $taxonomy)->where('slug', $slug)->first(),
'exists' => $exists,
'_checked' => true,
];
}
Expand Down

0 comments on commit 88e5289

Please sign in to comment.