Skip to content

Instantly share code, notes, and snippets.

@chrispian
Last active September 27, 2024 18:34
Show Gist options
  • Select an option

  • Save chrispian/c393c42003c952d59013013a7e2ad219 to your computer and use it in GitHub Desktop.

Select an option

Save chrispian/c393c42003c952d59013013a7e2ad219 to your computer and use it in GitHub Desktop.

Revisions

  1. chrispian renamed this gist Sep 27, 2024. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. chrispian created this gist Sep 27, 2024.
    116 changes: 116 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,116 @@
    <?php
    /*
    * NOTES:
    *
    * - This was designed for my personal use and could be more robust.
    * - Make sure to update the tables you want to import into.
    * - Make sure to update the website url you want to import from.
    * - Customize as needed. I had very few posts and no media.
    *
    */


    namespace App\Console\Commands;

    use App\Models\Post;
    use App\Models\PostCategory;
    use Carbon\Carbon;
    use Illuminate\Console\Command;
    use Illuminate\Support\Facades\Http;
    use Illuminate\Support\Str;
    use League\HTMLToMarkdown\HtmlConverter;
    use Spatie\Tags\Tag;

    class GetPosts extends Command
    {
    protected $signature = 'wp:getposts';
    protected $description = 'Fetch posts from WordPress and insert them into the local database';

    public function __construct()
    {
    parent::__construct();
    }

    public function handle()
    {
    $converter = new HtmlConverter();
    $page = 1;

    do {
    // Construct the dynamic API URL based on the current page
    $wpApiUrl = "https://YOURDOMAIN.com/wp-json/wp/v2/posts?page={$page}";
    $response = Http::get($wpApiUrl);

    if ($response->successful()) {
    $posts = $response->json();

    // If no posts are returned, stop the loop
    if (empty($posts)) {
    break;
    }

    foreach ($posts as $wpPost) {
    // Convert HTML to Markdown
    $title = html_entity_decode($wpPost['title']['rendered']);
    $summary = $converter->convert(html_entity_decode($wpPost['excerpt']['rendered']));
    $content = $converter->convert(html_entity_decode($wpPost['content']['rendered']));

    $post = Post::create([
    'sort_order' => null,
    'author_id' => 1,
    'title' => $title,
    'slug' => $wpPost['slug'],
    'cover_image' => $wpPost['featured_media'], // Assuming featured_media URL is available
    'summary' => $summary,
    'content' => $content,
    'status' => 'Published',
    'project_id' => null,
    'series_id' => null,
    'related_posts' => null,
    'wordpress_slug' => $wpPost['slug'],
    'wordpress_id' => $wpPost['id'],
    'created_at' => Carbon::parse($wpPost['date'])->setTimezone('UTC'),
    'updated_at' => Carbon::parse($wpPost['modified'])->setTimezone('UTC'),

    ]);

    // Handle Tags
    if (!empty($wpPost['tags'])) {
    $tags = [];
    foreach ($wpPost['tags'] as $wpTagId) {
    $wpTag = Http::get("https://YOURDOMAIN.com/wp-json/wp/v2/tags/{$wpTagId}")->json();
    $tag = Tag::firstOrCreate(['wordpress_id' => $wpTag['id']], [
    'name' => $wpTag['name'],
    ]);
    $tags[] = $tag->id;
    }
    $post->tags()->sync($tags);
    }

    // Handle Categories
    if (!empty($wpPost['categories'])) {
    $categories = [];
    foreach ($wpPost['categories'] as $wpCategoryId) {
    $wpCategory = Http::get("https://YOURDOMAIN.com/wp-json/wp/v2/categories/{$wpCategoryId}")->json();
    $category = PostCategory::firstOrCreate(['id' => $wpCategory['id']], [
    'title' => $wpCategory['name'],
    'slug' => Str::slug($wpCategory['name']),
    ]);
    $categories[] = $category->id;
    }
    $post->categories()->sync($categories);
    }
    }

    $this->info("Posts from page {$page} imported successfully.");
    } else {
    $this->error("Failed to fetch posts from page {$page}.");
    break;
    }

    $page++; // Move to the next page
    } while (true);

    $this->info('All posts imported successfully.');
    }
    }