Skip to content

Commit f5de5e7

Browse files
authored
Merge pull request #13 from vesper8/master
made compatible with Laravel 5.6, 5.7 and 5.8
2 parents 455ab20 + 3d3d3b2 commit f5de5e7

File tree

5 files changed

+25
-19
lines changed

5 files changed

+25
-19
lines changed

composer.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020
],
2121
"require": {
2222
"php": "^7.1.3",
23-
"cartalyst/tags": "6.0.*",
23+
"cartalyst/tags": "6.0.*|7.0.*|8.0.*",
2424
"guzzlehttp/guzzle": "^6.3",
25-
"illuminate/database": "5.6.*",
26-
"illuminate/support": "5.6.*",
27-
"illuminate/console": "5.6.*",
28-
"illuminate/contracts": "5.6.*",
29-
"illuminate/events": "5.6.*",
25+
"illuminate/database": "5.6.*|5.7.*|5.8.*",
26+
"illuminate/support": "5.6.*|5.7.*|5.8.*",
27+
"illuminate/console": "5.6.*|5.7.*|5.8.*",
28+
"illuminate/contracts": "5.6.*|5.7.*|5.8.*",
29+
"illuminate/events": "5.6.*|5.7.*|5.8.*",
3030
"league/fractal": "^0.17.0"
3131
},
3232
"require-dev": {

migrations/2016_11_18_165313_create_post_table.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ public function up()
1616
Schema::create('posts', function (Blueprint $table) {
1717
$table->increments('id');
1818
$table->integer('wp_id')->unique();
19+
$table->string('type');
1920
$table->string('title');
2021
$table->string('slug');
22+
$table->string('link');
2123
$table->string('featured_image')->nullable();
2224
$table->boolean('sticky')->default(false);
2325
$table->longText('excerpt')->nullable();
2426
$table->longText('content');
25-
$table->string('format');
27+
$table->string('format')->nullable();
2628
$table->string('status');
2729
$table->timestamp('published_at')->nullable();
2830
$table->integer('author_id')->nullable();

src/Commands/Importer.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class Importer extends Command
1616
* @var string
1717
*/
1818
protected $signature = 'wordpress-to-laravel:import
19+
{postRestBase? : The REST API endpoint (defaults to "posts").}
1920
{page? : The page number from WP to import.}
2021
{per-page? : The number of posts per page to fetch.}
2122
{--F|force-all : This option will grab every published post from the WP DB and sync them all (along with the other embedded bits) to your local DB}
@@ -65,6 +66,7 @@ public function __construct(WordpressToLaravel $wordpressToLaravel, Dispatcher $
6566
*/
6667
public function handle()
6768
{
69+
$postRestBase = $this->argument('postRestBase') ?: 'posts';
6870
$page = $this->argument('page') ?: 1;
6971
$perPage = $this->argument('per-page') ?: 5;
7072
$truncate = $this->option('truncate') ?: false;
@@ -75,7 +77,7 @@ public function handle()
7577
$this->registerListeners();
7678

7779
$this->wordpressToLaravel->import(
78-
$page, $perPage, $truncate, $forceAll
80+
$postRestBase, $page, $perPage, $truncate, $forceAll
7981
);
8082

8183
$this->outputCounts();

src/PostTransformer.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@ public function transform($post)
5252
{
5353
return [
5454
'wp_id' => (int)$post->id,
55+
'type' => $post->type,
5556
'title' => $post->title->rendered,
5657
'slug' => $post->slug,
57-
'sticky' => $post->sticky ? 1 : 0,
58-
'excerpt' => $post->excerpt->rendered,
59-
'content' => $post->content->rendered,
60-
'format' => $post->format,
61-
'status' => 'publish',
58+
'link' => $post->link,
59+
'sticky' => $post->sticky ?? 0,
60+
'excerpt' => $post->excerpt->rendered ?? '',
61+
'content' => $post->content->rendered ?? '',
62+
'format' => $post->format ?? null,
63+
'status' => $post->status,
6264
'featured_image' => $this->getFeaturedImage($post),
6365
'published_at' => $this->carbonDate($post->date),
6466
'created_at' => $this->carbonDate($post->date),

src/WordpressToLaravel.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ protected function setupTransformers()
110110
* @param bool $truncate
111111
* @param bool $forceAll
112112
*/
113-
public function import($page = 1, $perPage = 5, $truncate = false, $forceAll = false)
113+
public function import($postRestBase, $page = 1, $perPage = 5, $truncate = false, $forceAll = false)
114114
{
115115
$this->truncate($truncate)
116-
->fetchPosts($page, $perPage, $forceAll)
116+
->fetchPosts($postRestBase, $page, $perPage, $forceAll)
117117
->map(function ($post) {
118118
return $this->transformPost($post);
119119
})
@@ -130,13 +130,13 @@ public function import($page = 1, $perPage = 5, $truncate = false, $forceAll = f
130130
* @param bool $forceAll
131131
* @return Collection
132132
*/
133-
protected function fetchPosts($page, $perPage, $forceAll)
133+
protected function fetchPosts($postRestBase, $page, $perPage, $forceAll)
134134
{
135135
$posts = collect();
136136

137137
while (true) {
138138
$stop = collect(
139-
$this->sendRequest($this->makeUrl($page++, $perPage))
139+
$this->sendRequest($this->makeUrl($postRestBase, $page++, $perPage))
140140
)->map(function ($post) use ($posts) {
141141
$posts->push($post);
142142
})->isEmpty();
@@ -191,10 +191,10 @@ protected function sendRequest($url, $tries = 3)
191191
* @param int $perPage
192192
* @return string
193193
*/
194-
protected function makeUrl($page, $perPage)
194+
protected function makeUrl($postRestBase, $page, $perPage)
195195
{
196196
$queryString = sprintf(
197-
'posts?_embed=true&filter[orderby]=modified&page=%d&per_page=%d',
197+
"{$postRestBase}?_embed=true&filter[orderby]=modified&page=%d&per_page=%d",
198198
$page, $perPage
199199
);
200200

0 commit comments

Comments
 (0)