1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Yii2 Pages Module |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/hiqdev/yii2-module-pages |
6
|
|
|
* @package yii2-module-pages |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
* @copyright Copyright (c) 2016-2017, HiQDev (http://hiqdev.com/) |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace hiqdev\yii2\modules\pages\storage; |
12
|
|
|
|
13
|
|
|
use Yii; |
14
|
|
|
use hiqdev\yii2\modules\pages\models\AbstractPage; |
15
|
|
|
use hiqdev\yii2\modules\pages\models\RenderedPage; |
16
|
|
|
use Vnn\WpApiClient\Auth\WpBasicAuth; |
17
|
|
|
use Vnn\WpApiClient\Http\GuzzleAdapter; |
18
|
|
|
use Vnn\WpApiClient\WpClient; |
19
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
20
|
|
|
use yii\base\BaseObject; |
21
|
|
|
use yii\helpers\Url; |
22
|
|
|
|
23
|
|
|
class WordPressApi extends BaseObject implements StorageInterface |
24
|
|
|
{ |
25
|
|
|
/** @var WpClient */ |
26
|
|
|
private $client; |
27
|
|
|
|
28
|
|
|
/** @var string */ |
29
|
|
|
private $url; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $login; |
33
|
|
|
|
34
|
|
|
/** @var string */ |
35
|
|
|
private $password; |
36
|
|
|
|
37
|
|
|
public function getPage(string $pageName): ?AbstractPage |
38
|
|
|
{ |
39
|
|
|
$language = \Yii::$app->language; |
40
|
|
|
$pageData = $this->getClient()->posts()->get(null, [ |
41
|
|
|
'slug' => $pageName, |
42
|
|
|
]); |
43
|
|
|
|
44
|
|
|
if (empty($pageData)) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$pageData = $pageData[0]; |
49
|
|
|
$translatedPage = $pageData['translation'][$language]; |
50
|
|
|
if ($translatedPage && $language !== $pageData['lang']) { |
51
|
|
|
Yii::$app->response->redirect(Url::to('/pages/' . $translatedPage)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return Yii::createObject([ |
55
|
|
|
'class' => RenderedPage::class, |
56
|
|
|
'title' => $pageData['title']['rendered'], |
57
|
|
|
'text' => $pageData['content']['rendered'], |
58
|
|
|
'keywords' => $pageData['seo']['keywords'], |
59
|
|
|
'description' => $pageData['seo']['description'], |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function getList(): ?AbstractPage |
64
|
|
|
{ |
65
|
|
|
$listData = $this->getClient()->posts()->get(null, [ |
66
|
|
|
'lang' => \Yii::$app->language, |
67
|
|
|
]); |
68
|
|
|
|
69
|
|
|
if (empty($listData)) { |
70
|
|
|
return null; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$text = ''; |
74
|
|
|
foreach ($listData as $pageData) { |
75
|
|
|
$text .= (Yii::createObject([ |
76
|
|
|
'class' => RenderedPage::class, |
77
|
|
|
'title' => $pageData['title']['rendered'], |
78
|
|
|
'text' => $pageData['excerpt']['rendered'], |
79
|
|
|
'slug' => $pageData['slug'], |
80
|
|
|
'featuredImageUrl' => $pageData['featured_image_url'], |
81
|
|
|
]))->renderMiniature(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return Yii::createObject([ |
85
|
|
|
'class' => RenderedPage::class, |
86
|
|
|
'title' => 'List of Pages', |
87
|
|
|
'text' => $text, |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getClient(): WpClient |
92
|
|
|
{ |
93
|
|
|
if (!$this->client) { |
94
|
|
|
$this->client = Yii::$container->get(WpClient::class, [ |
95
|
|
|
Yii::$container->get(GuzzleAdapter::class, [ |
96
|
|
|
Yii::$container->get(GuzzleClient::class) |
97
|
|
|
]), |
98
|
|
|
$this->url |
99
|
|
|
]); |
100
|
|
|
|
101
|
|
|
$this->client->setCredentials(Yii::$container->get(WpBasicAuth::class, [ |
102
|
|
|
$this->login, |
103
|
|
|
$this->password |
104
|
|
|
])); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $this->client; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $url |
112
|
|
|
*/ |
113
|
|
|
public function setUrl(string $url): void |
114
|
|
|
{ |
115
|
|
|
$this->url = $url; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $login |
120
|
|
|
*/ |
121
|
|
|
public function setLogin(string $login): void |
122
|
|
|
{ |
123
|
|
|
$this->login = $login; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $password |
128
|
|
|
*/ |
129
|
|
|
public function setPassword(string $password): void |
130
|
|
|
{ |
131
|
|
|
$this->password = $password; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|