Skip to content

Instantly share code, notes, and snippets.

@lokmanm
Last active December 22, 2021 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lokmanm/d1f4038d3cb14bab817ea9e704eb6ab7 to your computer and use it in GitHub Desktop.
Save lokmanm/d1f4038d3cb14bab817ea9e704eb6ab7 to your computer and use it in GitHub Desktop.
Import Wordpress posts to Statamic
<?php
namespace App\Console\Commands;
use Corcel\Model\Post;
use Corcel\Model\Category;
use Corcel\Model\Taxonomy;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Statamic\Facades\Entry;
class ImportWP extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'import:wp';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Import posts from WordPress';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
$posts = Post::type('post')->orderBy('post_date', 'desc')->published()->limit(1)->get();
$bar = $this->output->createProgressBar(count($posts));
$bar->start();
foreach ($posts as $post) {
$content = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $post->post_content);
$content = preg_replace('~(?:\[/?).*?"]~s', '', $content);
$content = preg_replace('(\\[([^[]|)*])', '', $content );
$content = preg_replace('/\[(.*?)\]/', '', $content );
$featured_image = Str::of($post->thumbnail->size('invalid_size'))->ltrim('https://f2n2.mk/wp-content/');
$entry = Entry::make()
->collection('posts')
->slug($post->post_name)
->date($post->post_date)
->data([
'title' => $post->title,
'image' => (string)$featured_image,
'content' => (new \HtmlToProseMirror\Renderer)->render($content),
]);
$entry->save();
$bar->advance();
}
$bar->finish();
return 'done';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment