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

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jackmcdade committed Oct 15, 2024
1 parent c1523a8 commit deafa1b
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 70 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ Note: This addon works in conjunction with a [Wordpress plugin](https://github.c

## Note

This is a rough addon the Rad Pack inherited from @jezzdk. We're open to PRs and collaboration, though the Statamic Core Team is working on a different approach that might replace this in the near(ish) future.
This is a rough addon the Rad Pack inherited from [@jezzdk](https://github.com/jezzdk). It's based on the v2 method of generating a JSON file with a separate WP plugin, which isn't ideal, but should still be functional.

We're open to PRs and collaboration, though the Statamic Core Team is working on a different approach that will likely replace this in the near future.


## Features
Expand Down Expand Up @@ -51,12 +53,12 @@ You can use the events above to do your own downloading of images and what not.
You can search for this addon in the `Tools > Addons` section of the Statamic control panel and click **install**, or run the following command from your project root:

``` bash
composer require jezzdk/statamic-wp-import
composer require RadPack/statamic-wp-import
```

## How to Use

First of all, you must export your data using the [Export to Statamic Wordpress Plugin](https://github.com/jezzdk/wordpress-to-statamic-exporter). Check anything you wish to export, but have the notes above in mind.
First of all, you must export your data using the [Export to Statamic Wordpress Plugin](https://github.com/RadPack/wordpress-to-statamic-exporter). Check anything you wish to export, but have the notes above in mind.

Go to the `Tools > WP Import` section and upload the json file.

Expand Down Expand Up @@ -105,7 +107,7 @@ return [

You can publish it with the command:

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

## Known issues

Expand Down
9 changes: 3 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
{
"name": "jezzdk/statamic-wp-import",
"name": "statamic-rad-pack/wp-import",
"autoload": {
"psr-4": {
"Jezzdk\\StatamicWpImport\\": "src"
"RadPack\\StatamicWpImport\\": "src"
}
},
"authors": [
{
"name": "Jess Stopa"
}
],
"support": {
"email": "[email protected]"
},
"extra": {
"statamic": {
"name": "Statamic WP Import",
"description": "Import Wordpress content into Statamic"
},
"laravel": {
"providers": [
"Jezzdk\\StatamicWpImport\\ServiceProvider"
"RadPack\\StatamicWpImport\\ServiceProvider"
]
}
}
Expand Down
23 changes: 14 additions & 9 deletions resources/views/upload.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@

@section('content')

<header class="mb-3"><h1>WP Import</h1></header>
<header class="mb-6">
<h1>WP Import</h1>
</header>

<div class="card p-2 content">
<div class="card p-4 content">
<form action="{{ cp_route('wp-import.upload') }}" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<h2 class="font-bold">JSON file</h2>
<p class="text-grey text-sm my-1">Upload the JSON file you have exported with the official <a href="https://github.com/statamic/wordpress-to-statamic-exporter" target="_blank">WordPress plugin</a>.</p>
<div class="flex justify-between items-center">
<div class="pr-4">
<input type="file" class="form-control" name="file" />
</div>
<h2 class="font-bold">Upload</h2>
<p class="text-grey text-sm my-1">Upload the JSON file you have exported with <a href="https://github.com/jezzdk/wordpress-to-statamic-exporter" target="_blank">this WordPress plugin</a>.</p>
<div class="flex space-x-3 items-center mt-3">
<input type="file" class="border rounded px-3 py-2 text-sm w-full" name="file" />
<div>
<button type="submit" class="btn btn-primary">Import</button>
</div>
</div>
</form>
</div>

@endsection
@include('statamic::partials.docs-callout', [
'topic' => __('this addon'),
'url' => 'https://statamic.com/addons/rad-pack/wp-import'
])

@endsection
2 changes: 1 addition & 1 deletion routes/cp.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Illuminate\Support\Facades\Route;
use Jezzdk\StatamicWpImport\Http\Controllers\ImportController;
use RadPack\StatamicWpImport\Http\Controllers\ImportController;

Route::group(['prefix' => 'wp-import'], function () {
Route::get('/', [ImportController::class, 'index'])->name('wp-import.index');
Expand Down
39 changes: 17 additions & 22 deletions src/Helpers/Migrator.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Jezzdk\StatamicWpImport\Helpers;
namespace RadPack\StatamicWpImport\Helpers;

use Exception;
use Illuminate\Support\Facades\Http;
Expand All @@ -10,8 +10,8 @@
use Statamic\Facades\Collection;
use Statamic\Facades\Entry;
use Statamic\Facades\Stache;
use Statamic\Facades\Term;
use Statamic\Facades\Taxonomy;
use Statamic\Facades\Term;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class Migrator
Expand All @@ -32,9 +32,6 @@ class Migrator

/**
* Perform the migration
*
* @param $migration
* @param $summary
*/
public function migrate($migration, $summary)
{
Expand All @@ -55,7 +52,7 @@ public function migrate($migration, $summary)
/**
* Prepare the migration
*
* @param array $migration
* @param array $migration
* @return array
*/
private function prepareMigration($migration)
Expand All @@ -72,8 +69,8 @@ private function prepareMigration($migration)
/**
* Sort an array by folder depth (amount of slashes)
*
* @param array $arr An array with paths for keys
* @return array The sorted array
* @param array $arr An array with paths for keys
* @return array The sorted array
*/
private function sortDeepest($arr)
{
Expand All @@ -99,7 +96,7 @@ private function createTaxonomies()
foreach (array_get($this->migration, 'taxonomies', []) as $taxonomy_slug => $taxonomy_data) {
$taxonomy = Taxonomy::findByHandle($taxonomy_slug);

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

Expand All @@ -121,13 +118,13 @@ private function createTaxonomyTerms()
foreach (array_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']) {
if (! $this->summary['taxonomies'][$taxonomy_slug]['terms'][$term_slug]['_checked']) {
continue;
}

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

if (!$term) {
if (! $term) {
$term = Term::make($term_slug)->taxonomy($taxonomy_slug);
}

Expand All @@ -150,7 +147,7 @@ private function createCollections()
foreach (array_get($this->migration, 'collections', []) as $handle => $data) {
$collection = Collection::findByHandle($handle);

if (!$collection) {
if (! $collection) {
$collection = Collection::make($handle);
}

Expand All @@ -172,13 +169,13 @@ private function createEntries()
foreach ($this->migration['entries'] as $collection => $entries) {
foreach ($entries as $slug => $meta) {
// Skip if this entry was not checked in the summary.
if (!$this->summary['collections'][$collection]['entries'][$slug]['_checked']) {
if (! $this->summary['collections'][$collection]['entries'][$slug]['_checked']) {
continue;
}

$entry = Entry::query()->where('collection', $collection)->where('slug', $slug)->first();

if (!$entry) {
if (! $entry) {
$entry = Entry::make()->collection($collection)->slug($slug);
}

Expand Down Expand Up @@ -212,7 +209,7 @@ private function createPages()
{
foreach ($this->migration['pages'] as $url => $meta) {
// Skip if this page was not checked in the summary.
if (!$this->summary['pages'][$url]['_checked']) {
if (! $this->summary['pages'][$url]['_checked']) {
continue;
}

Expand All @@ -221,7 +218,7 @@ private function createPages()

$page = Entry::query()->where('collection', 'pages')->where('slug', $slug)->first();

if (!$page) {
if (! $page) {
$page = Entry::make()->collection('pages')->slug($slug);
}

Expand All @@ -245,13 +242,10 @@ private function createPages()

/**
* Create an asset from a URL
*
* @param string|null $url
* @return Asset|bool
*/
private function downloadAsset(string $url = null, string $collection, string $slug): Asset|bool
private function downloadAsset(?string $url, string $collection, string $slug): Asset|bool
{
if (!$url) {
if (! $url) {
return false;
}

Expand Down Expand Up @@ -285,7 +279,8 @@ private function downloadAsset(string $url = null, string $collection, string $s

return $asset;
} catch (Exception $e) {
logger('Image download failed: ' . $e->getMessage());
logger('Image download failed: '.$e->getMessage());

return false;
}
}
Expand Down
27 changes: 14 additions & 13 deletions src/Helpers/Preparer.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<?php

namespace Jezzdk\StatamicWpImport\Helpers;
namespace RadPack\StatamicWpImport\Helpers;

use Statamic\Facades\URL;
use Statamic\Support\Str;

class Preparer
{
private $data;

private $migration = [];

public function prepare(array $data)
{
$this->data = $data;

if (!isset($this->data['pages']) || !is_array($this->data['pages'])) {
if (! isset($this->data['pages']) || ! is_array($this->data['pages'])) {
$this->data['pages'] = [];
}

Expand Down Expand Up @@ -53,14 +54,14 @@ protected function filterMetaData()

private function createTaxonomies()
{
if (!isset($this->data['taxonomies'])) {
if (! isset($this->data['taxonomies'])) {
return;
}

foreach ($this->data['taxonomies'] as $taxonomy_name => $terms) {
$this->migration['taxonomies']->put($taxonomy_name, [
'title' => Str::title($taxonomy_name),
'route' => '/' . $taxonomy_name . '/{slug}'
'route' => '/'.$taxonomy_name.'/{slug}',
]);

$this->migration['terms']->put($taxonomy_name, collect());
Expand All @@ -80,7 +81,7 @@ private function createTaxonomies()

private function createCollections()
{
if (!isset($this->data['collections'])) {
if (! isset($this->data['collections'])) {
return;
}

Expand All @@ -93,13 +94,13 @@ private function createCollections()
/**
* Create a collection
*
* @param string $collection
* @param string $collection
* @param array $entries
* @return void
*/
private function createCollection($collection, $entries)
{
$route = '/' . $collection . '/{slug}';
$route = '/'.$collection.'/{slug}';

$collection = str_replace('/', '-', $collection);

Expand All @@ -116,7 +117,7 @@ private function createCollection($collection, $entries)

$this->migration['collections']->put($collection, [
'order' => $type,
'route' => $route
'route' => $route,
]);

$this->migration['entries']->put($collection, collect());
Expand All @@ -125,7 +126,7 @@ private function createCollection($collection, $entries)
/**
* Create the entries in a collection
*
* @param string $collection
* @param string $collection
* @param array $entries
* @return void
*/
Expand All @@ -141,13 +142,13 @@ private function createEntries($collection, $entries)
/**
* Replace slugs in taxonomy fields with their IDs
*
* @param array $data The array of data to modify
* @return array The modified array
* @param array $data The array of data to modify
* @return array The modified array
*/
private function replaceTaxonomies($data)
{
foreach ($data as $field_name => &$value) {
if (!$this->isTaxonomyField($field_name)) {
if (! $this->isTaxonomyField($field_name)) {
continue;
}

Expand Down Expand Up @@ -175,7 +176,7 @@ private function replaceTaxonomies($data)
* Is a given $key a taxonomy field name?
*
* @param string $key
* @return boolean
* @return bool
*/
private function isTaxonomyField($key)
{
Expand Down
Loading

0 comments on commit deafa1b

Please sign in to comment.