Skip to content

Commit 080b50a

Browse files
authored
Merge pull request #18 from oligirling/add_wordpress_com_support
2 parents 10dc7ea + 023cdd8 commit 080b50a

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,22 @@ Migrate your database to setup the posts, categories, tags & author tables:
3737
$ php artisan migrate
3838
```
3939

40-
Setup the url to your WP blog in your env file:
40+
If you are using wordpress.com to host your blog, set the following env variable to true (default is false). This is done because wordpress.com and wordpress.org (self hosted instances) have different url structure when fetching the posts
41+
``` env
42+
WP_TO_LARAVEL_IS_WORDPRESS_COM=true
43+
```
44+
45+
Next, add your WP blog url to the env file
4146

4247
``` env
4348
WP_TO_LARAVEL_API_URL=https://blog.your-blog-domain.com/
4449
```
4550

51+
or for wordpress.com hosted blogs (change the your-blog part of the url)
52+
``` env
53+
WP_TO_LARAVEL_API_URL=https://public-api.wordpress.com/wp/v2/sites/your-blog.wordpress.com/
54+
```
55+
4656
Finally, we need to configure WP itself. If you're using Wordpress 4.7+, then you're all set - crack on! Otherwise, you'll need to install the WP API plugin to the WP site you wish to sync from:
4757

4858
[Wordpress API](http://v2.wp-api.org/)

config/config.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@
1010
| This is the url of your blog where you've installed the Wordpress API
1111
| plugin.
1212
|
13-
| e.g. http://blog.example.dev/
13+
| e.g. http://blog.example.dev/ or https://coolname.wordpress.com/
1414
*/
1515
'api_url' => env('WP_TO_LARAVEL_API_URL'),
1616

17+
/*
18+
|--------------------------------------------------------------------------
19+
| IS WORDPRESS COM
20+
|--------------------------------------------------------------------------
21+
|
22+
| Is your site hosted on wordpress.com
23+
| There are two types of wordpress, wordpress.com which hosts the blog, or
24+
| wordpress.org which is self hosted
25+
| This matters because they have differnt urls for the API
26+
|
27+
| e.g. true (defaults false)
28+
*/
29+
'is_wordpress_com' => env('WP_TO_LARAVEL_IS_WORDPRESS_COM', false),
1730

1831
/*
1932
|--------------------------------------------------------------------------

src/WordpressToLaravel.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WordpressToLaravel
1818
/**
1919
* @var string
2020
*/
21-
protected $endpoint = 'wp-json/wp/v2/';
21+
protected $hostedEndpoint = 'wp-json/wp/v2/';
2222

2323
/**
2424
* @var FractalManager
@@ -94,6 +94,11 @@ protected function setupModels()
9494
$this->authorModel = $this->config['author_model'] ?? Author::class;
9595
}
9696

97+
protected function isWordpressCom()
98+
{
99+
return $this->config['is_wordpress_com'];
100+
}
101+
97102
protected function setupTransformers()
98103
{
99104
$this->postTransformer = Arr::get($this->config, 'transformers.post') ?? PostTransformer::class;
@@ -202,7 +207,7 @@ protected function makeUrl($postRestBase, $page, $perPage)
202207
return sprintf(
203208
'%s%s%s',
204209
Str::finish($this->config['api_url'], '/'),
205-
$this->endpoint,
210+
$this->isWordpressCom() ? '' : $this->hostedEndpoint,
206211
$queryString
207212
);
208213
}

0 commit comments

Comments
 (0)