| 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 hiqdev\yii2\modules\pages\Module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use Yii; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use hiqdev\yii2\modules\pages\interfaces\PageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use hiqdev\yii2\modules\pages\interfaces\StorageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use hiqdev\yii2\modules\pages\models\PagesList; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use hiqdev\yii2\modules\pages\models\HtmlPage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use Vnn\WpApiClient\Auth\WpBasicAuth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use Vnn\WpApiClient\Http\GuzzleAdapter; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use Vnn\WpApiClient\WpClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use GuzzleHttp\Client as GuzzleClient; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use yii\base\BaseObject; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use yii\di\Instance; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use yii\helpers\Url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * Class WordPressApi provides interaction with WordPress REST Api. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  * The configuration in di-container should be like this: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  * 'WordpressApi' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  *      'class'     => full name of this class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  *      'url'       => url to deployed wordpress, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  *      'login'     => admin login, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  *      'password'  => admin password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  *      'login' and 'password' fields aren't necessary for GET requests. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  *  ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  * ``` | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  * @package hiqdev\yii2\modules\pages\storage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | class WordPressApi extends BaseObject implements StorageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     /** @var WpClient */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private $client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     private $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private $login; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     private $password; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public Module $pages; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function getPage(string $pageName): ?PageInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         $language = Yii::$app->language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $pageData = $this->getClient()->posts()->get(null, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'slug' => $pageName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if (empty($pageData)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $pageData = $pageData[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $translatedPage = $pageData['translation'][$language]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         if ($translatedPage && $language !== $pageData['lang']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             Yii::$app->response->redirect(Url::to(sprintf('/%s/%s', $this->pages->id, $translatedPage))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         if (!$translatedPage && $language !== $pageData['lang']) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |             $canonical = Url::to(sprintf('/%s/%s/%s', $pageData['lang'], $this->pages->id, $pageData['slug']), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         return Yii::createObject([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'class' => HtmlPage::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             'title' => $pageData['title']['rendered'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             'text' => $pageData['content']['rendered'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             'keywords' => $pageData['seo']['keywords'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             'description' => $pageData['seo']['description'], | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |             'canonical' => $canonical ?? null, | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |     public function getList(string $listName = null): ?PagesList | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |         $listData = $this->getClient()->posts()->get(null, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |             'lang' => Yii::$app->language, | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         if (empty($listData)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |             return null; | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |         $pages = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |         foreach ($listData as $pageData) { | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |             $pages[] = Yii::createObject([ | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |                 'class' => HtmlPage::class, | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |                 'title' => $pageData['title']['rendered'], | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |                 'text' => $pageData['excerpt']['rendered'], | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |                 'slug' => $pageData['slug'], | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |                 'featuredImageUrl' => $pageData['featured_image_url'], | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |                 'url' => Url::to(sprintf('/%s/%s', $this->pages->id, $pageData['slug'])), | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |             ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return Yii::createObject(PagesList::class, [$pages]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     private function getClient(): WpClient | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         if (!$this->client) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $this->client = Yii::$container->get(WpClient::class, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |                 Yii::$container->get(GuzzleAdapter::class, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |                     Instance::of(GuzzleClient::class), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |                 ]), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 $this->url, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $this->client->setCredentials(Yii::$container->get(WpBasicAuth::class, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 $this->login, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $this->password, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         return $this->client; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * @param string $url | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     public function setUrl(string $url): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |         $this->url = $url; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      * @param string $login | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     public function setLogin(string $login): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         $this->login = $login; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param string $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     public function setPassword(string $password): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         $this->password = $password; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 158 |  |  |  |