Created
January 15, 2021 12:32
-
-
Save vmitchell85/b27ebcbf6489f87e5c49b277a43918c3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Console\Commands; | |
use Corcel\Model\Page as WpPage; | |
use Illuminate\Console\Command; | |
use Illuminate\Support\Facades\Auth; | |
use Illuminate\Support\Str; | |
use Statamic\Entries\Entry; | |
use Statamic\Facades\Collection; | |
use Statamic\Facades\Site; | |
use Statamic\Facades\User; | |
use Statamic\Http\Resources\API\EntryResource; | |
class ImportWP extends Command | |
{ | |
/** | |
* The name and signature of the console command. | |
* | |
* @var string | |
*/ | |
protected $signature = 'import:wordpress'; | |
/** | |
* The console command description. | |
* | |
* @var string | |
*/ | |
protected $description = 'Command description'; | |
/** | |
* Create a new command instance. | |
* | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
parent::__construct(); | |
} | |
/** | |
* Execute the console command. | |
* | |
* @return int | |
*/ | |
public function handle() | |
{ | |
WpPage::where('post_parent', '') | |
->where('post_status', 'publish') | |
->get()->each(function ($page) { | |
$parentEntry = $this->makeEntry($page); | |
$this->recursive($parentEntry, $page); | |
}); | |
} | |
public function cleanContent($content) | |
{ | |
// make urls relative | |
$content = (string) Str::of($content) | |
->replace('http://www.example.com/', '/'); | |
return $content; | |
} | |
public function recursive($parentEntry, $page) | |
{ | |
WpPage::where('post_parent', $page->ID) | |
->where('post_status', 'publish') | |
->get()->each(function ($newPage) use ($parentEntry) { | |
$newParent = $this->makeEntry($newPage, $parentEntry->id()); | |
$this->recursive($newParent, $newPage); | |
}); | |
} | |
public function makeEntry(WpPage $page, $parent_id = null) | |
{ | |
$collection = Collection::findByHandle('pages'); | |
$data = [ | |
'title' => $page->title !== '' ? $page->title : 'MISSING TITLE', | |
'slug' => $page->post_name !== '' ? $page->post_name : Str::random(), | |
'content' => $this->cleanContent($page->post_content), | |
]; | |
$blueprint = 'pages'; | |
$site = Site::all()->first(); | |
$blueprint = $collection->entryBlueprint($blueprint); | |
$fields = $blueprint->fields()->addValues($data); | |
$fields->validate(Entry::createRules($collection, $site)); | |
$values = $fields->process()->values()->except(['slug', 'date', 'blueprint']); | |
$entry = Entry::make() | |
->collection($collection) | |
->blueprint($blueprint) | |
->locale('default') | |
->published(true) | |
->slug($page->post_name) | |
->data($values); | |
// if ($collection->dated()) { | |
// $entry->date($this->formatDateForSaving($request->date)); | |
// } | |
if (($structure = $collection->structure()) && !$collection->orderable()) { | |
$tree = $structure->in('default'); | |
$parent = $parent_id ?? null; | |
$entry->afterSave(function ($entry) use ($parent, $tree) { | |
$tree->appendTo($parent, $entry)->save(); | |
}); | |
} | |
$user = User::all()->first(); | |
Auth::login($user); | |
if ($entry->revisionsEnabled()) { | |
$entry->store([ | |
'message' => 'testing', | |
'user' => $user, | |
]); | |
} else { | |
$entry->updateLastModified($user)->save(); | |
} | |
return new EntryResource($entry); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment