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

Commit

Permalink
Added config file
Browse files Browse the repository at this point in the history
Added checkboxes to check/uncheck all
  • Loading branch information
jezzdk committed Sep 14, 2021
1 parent b104d0e commit 3c63dfc
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 69 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,32 @@ The summary will show you if anything was already found in your installation. If

Done :)

## Config

The content of the config file looks like this:

```
<?php
return [
/*
* Enable downloading of featured image. The default is 'true'.
*/
'download_images' => true,
/*
* Filter out meta data keys prefixed with '_'. The default is 'true'.
*/
'exclude_underscore_data' => true,
];
```

You can publish it with the command:

`php artisan vendor:publish --tag=statamic-wp-import`

## Known issues

You might get timeout errors if you're importing large datasets and/or many images. In that case you might want to tweak the timeouts on your server or run the import locally.
15 changes: 15 additions & 0 deletions config/statamic-wp-import.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

return [

/*
* Enable downloading of featured image. The default is 'true'.
*/
'download_images' => true,

/*
* Filter out meta data keys prefixed with '_'. The default is 'true'.
*/
'exclude_underscore_data' => true,

];
2 changes: 1 addition & 1 deletion dist/js/addon.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions resources/js/components/Importer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,18 @@ export default {
});
},
uncheckAll: function(items) {
_.each(items, (item) => {
item._checked = false;
});
},
checkAll: function(items) {
_.each(items, (item) => {
item._checked = true;
});
},
size: function (obj) {
return _.size(obj);
},
Expand Down
105 changes: 57 additions & 48 deletions resources/views/summary.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<div class="form-group">
<div class="py-1.5 px-2 text-sm w-full rounded-md bg-yellow border border-yellow-dark mb-3" role="alert" v-if="hasDuplicates(summary.pages)">
Duplicate items found
<span class="text-xs ml-2">(<a @click.prevent="uncheckDuplicates(summary.pages)" href="#" class="text-blue hover:underline">Uncheck duplicates</a>)</span>
<span class="text-xs ml-1">(<a @click.prevent="uncheckDuplicates(summary.pages)" href="#" class="text-blue hover:underline">Uncheck duplicates</a>)</span>
</div>

<label>Entries</label>
Expand All @@ -30,21 +30,24 @@
<a @click="showAllPages = false" v-else>Hide</a>
</p>

<table class="w-full mt-2 mb-4" v-if="showAllPages">
<thead>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1"></th>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1">URL</th>
</thead>
<tbody class="border-t border-grey-60">
<tr v-for="(page, i) in summary.pages">
<td class="px-2 py-1 w-4" :class="{ 'bg-yellow': page.exists }">
<input type="checkbox" v-model="page._checked" id="page-@{{ i }}" />
<label for="page-@{{ i }}"></label>
</td>
<td class="px-2 py-1 text-xs" :class="{ 'bg-yellow': page.exists }">@{{ page.url }}</td>
</tr>
</tbody>
</table>
<div v-show="showAllPages">
<a @click.prevent="uncheckAll(summary.pages)" href="#" class="text-xs text-blue hover:underline">Uncheck all</a> / <a @click.prevent="checkAll(summary.pages)" href="#" class="text-xs text-blue hover:underline">Check all</a>
<table class="w-full mt-2 mb-4">
<thead>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1"></th>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1">URL</th>
</thead>
<tbody class="border-t border-grey-60">
<tr v-for="(page, i) in summary.pages">
<td class="px-2 py-1 w-4" :class="{ 'bg-yellow': page.exists }">
<input type="checkbox" v-model="page._checked" id="page-@{{ i }}" />
<label for="page-@{{ i }}"></label>
</td>
<td class="px-2 py-1 text-xs" :class="{ 'bg-yellow': page.exists }">@{{ page.url }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

Expand All @@ -57,7 +60,7 @@
<div class="form-group">
<div class="py-1.5 px-2 text-sm w-full rounded-md bg-yellow border border-yellow-dark mb-3" role="alert" v-if="hasDuplicates(collection.entries)">
Duplicate items found
<span class="text-xs ml-2">(<a @click.prevent="uncheckDuplicates(collection.entries)" href="#" class="text-blue hover:underline">Uncheck duplicates</a>)</span>
<span class="text-xs ml-1">(<a @click.prevent="uncheckDuplicates(collection.entries)" href="#" class="text-blue hover:underline">Uncheck duplicates</a>)</span>
</div>

<label>Entries</label>
Expand All @@ -67,21 +70,24 @@
<a href="#" @click.prevent="hideCollection(collectionName)" v-else>Hide</a>
</p>

<table class="w-full mt-2 mb-4" v-show="shouldShowCollection(collectionName)">
<thead>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1"></th>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1">Slug</th>
</thead>
<tbody class="border-t border-grey-60">
<tr v-for="(entry, slug) in collection.entries">
<td class="px-2 py-1 w-4" :class="{ 'bg-yellow': entry.exists }">
<input type="checkbox" v-model="entry._checked" id="c-@{{ collectionName }}-@{{ slug }}" />
<label for="c-@{{ collectionName }}-@{{ slug }}"></label>
</td>
<td class="px-2 py-1 text-xs" :class="{ 'bg-yellow': entry.exists }">@{{ entry.slug }}</td>
</tr>
</tbody>
</table>
<div v-show="shouldShowCollection(collectionName)">
<a @click.prevent="uncheckAll(collection.entries)" href="#" class="text-xs text-blue hover:underline">Uncheck all</a> / <a @click.prevent="checkAll(collection.entries)" href="#" class="text-xs text-blue hover:underline">Check all</a>
<table class="w-full mt-2 mb-4">
<thead>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1"></th>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1">Slug</th>
</thead>
<tbody class="border-t border-grey-60">
<tr v-for="(entry, slug) in collection.entries">
<td class="px-2 py-1 w-4" :class="{ 'bg-yellow': entry.exists }">
<input type="checkbox" v-model="entry._checked" id="c-@{{ collectionName }}-@{{ slug }}" />
<label for="c-@{{ collectionName }}-@{{ slug }}"></label>
</td>
<td class="px-2 py-1 text-xs" :class="{ 'bg-yellow': entry.exists }">@{{ entry.slug }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

Expand All @@ -94,7 +100,7 @@
<div class="form-group">
<div class="py-1.5 px-2 text-sm w-full rounded-md bg-yellow border border-yellow-dark mb-3" role="alert" v-if="hasDuplicates(taxonomy.terms)">
Duplicate items found
<span class="text-xs ml-2">(<a @click.prevent="uncheckDuplicates(taxonomy.terms)" href="#" class="text-blue hover:underline">Uncheck duplicates</a>)</span>
<span class="text-xs ml-1">(<a @click.prevent="uncheckDuplicates(taxonomy.terms)" href="#" class="text-blue hover:underline">Uncheck duplicates</a>)</span>
</div>
<label>Terms</label>
<p>
Expand All @@ -103,21 +109,24 @@
<a href="#" @click.prevent="hideTaxonomy(taxonomyName)" v-else>Hide</a>
</p>

<table class="w-full mt-2 mb-4" v-if="shouldShowTaxonomy(taxonomyName)">
<thead>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1 w-4"></th>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1">Slug</th>
</thead>
<tbody class="border-t border-grey-60">
<tr v-for="(term, slug) in taxonomy.terms">
<td class="px-2 py-1 w-4" :class="{ 'bg-yellow': term.exists }">
<input type="checkbox" v-model="term._checked" id="t-@{{ taxonomyName }}-@{{ slug }}" />
<label for="t-@{{ taxonomyName }}-@{{ slug }}"></label>
</td>
<td class="px-2 py-1 text-xs" :class="{ 'bg-yellow': term.exists }">@{{ term.slug }}</td>
</tr>
</tbody>
</table>
<div v-show="shouldShowTaxonomy(taxonomyName)">
<a @click.prevent="uncheckAll(taxonomy.terms)" href="#" class="text-xs text-blue hover:underline">Uncheck all</a> / <a @click.prevent="checkAll(taxonomy.terms)" href="#" class="text-xs text-blue hover:underline">Check all</a>
<table class="w-full mt-2 mb-4">
<thead>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1 w-4"></th>
<th class="text-left uppercase text-grey-60 text-xs px-2 py-1">Slug</th>
</thead>
<tbody class="border-t border-grey-60">
<tr v-for="(term, slug) in taxonomy.terms">
<td class="px-2 py-1 w-4" :class="{ 'bg-yellow': term.exists }">
<input type="checkbox" v-model="term._checked" id="t-@{{ taxonomyName }}-@{{ slug }}" />
<label for="t-@{{ taxonomyName }}-@{{ slug }}"></label>
</td>
<td class="px-2 py-1 text-xs" :class="{ 'bg-yellow': term.exists }">@{{ term.slug }}</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>

Expand Down
47 changes: 28 additions & 19 deletions src/Helpers/Migrator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Jezzdk\StatamicWpImport\Helpers;

use Exception;
use Illuminate\Support\Facades\Storage;
use Statamic\Assets\Asset;
use Statamic\Facades\AssetContainer;
Expand Down Expand Up @@ -179,10 +180,12 @@ private function createEntries()
'slug' => $slug
]));

$asset = $this->downloadAsset($meta['data']['featured_image_url'] ?? '', $collection);
if (config('statamic-wp-import.download_images')) {
$asset = $this->downloadAsset($meta['data']['featured_image_url'] ?? '', $collection);

if ($asset) {
$entry->set('featured_image', $asset->path());
if ($asset) {
$entry->set('featured_image', $asset->path());
}
}

$entry->save();
Expand Down Expand Up @@ -216,10 +219,12 @@ private function createPages()
'slug' => $slug
]));

$asset = $this->downloadAsset($meta['data']['featured_image_url'] ?? '', 'pages');
if (config('statamic-wp-import.download_images')) {
$asset = $this->downloadAsset($meta['data']['featured_image_url'] ?? '', 'pages');

if ($asset) {
$page->set('image', $asset->path());
if ($asset) {
$page->set('featured_image', $asset->path());
}
}

$page->save();
Expand All @@ -238,23 +243,27 @@ private function downloadAsset(string $url = null, string $container): Asset|boo
return false;
}

$image = file_get_contents($url);
$originalImageName = basename($url);
try {
$image = @file_get_contents($url);
$originalImageName = basename($url);

Storage::put($tempFile = 'temp', $image);
Storage::put($tempFile = 'temp', $image);

$assetContainer = AssetContainer::findByHandle('assets');
$assetContainer = AssetContainer::findByHandle('assets');

$asset = $assetContainer->makeAsset("imports/{$container}/{$originalImageName}")
->upload(
new UploadedFile(
Storage::path($tempFile),
$originalImageName,
)
);
$asset = $assetContainer->makeAsset("imports/{$container}/{$originalImageName}")
->upload(
new UploadedFile(
Storage::path($tempFile),
$originalImageName,
)
);

$asset->save();
$asset->save();

return $asset;
return $asset;
} catch (Exception $e) {
return false;
}
}
}
4 changes: 3 additions & 1 deletion src/Helpers/Preparer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public function prepare(array $data)
$this->createTaxonomies();
$this->createCollections();

$this->filterMetaData();
if (config('statamic-wp-import.exclude_underscore_data')) {
$this->filterMetaData();
}

return $this->migration;
}
Expand Down

0 comments on commit 3c63dfc

Please sign in to comment.