diff --git a/app/config/providers.php b/app/config/providers.php index 65e2127b3e..a377cae6f0 100644 --- a/app/config/providers.php +++ b/app/config/providers.php @@ -224,5 +224,14 @@ return [ // Ordered by ABC. 'form' => false, 'beta' => false, 'mock' => true, + ], + 'wordpress' => [ + 'name' => 'WordPress', + 'developers' => 'https://developer.wordpress.com/docs/oauth2/', + 'icon' => 'icon-wordpress', + 'enabled' => true, + 'form' => false, + 'beta' => false, + 'mock' => false ] ]; diff --git a/public/images/oauth2/wordpress.png b/public/images/oauth2/wordpress.png new file mode 100644 index 0000000000..f1d664ada1 Binary files /dev/null and b/public/images/oauth2/wordpress.png differ diff --git a/src/Appwrite/Auth/OAuth2/WordPress.php b/src/Appwrite/Auth/OAuth2/WordPress.php new file mode 100644 index 0000000000..a83953405e --- /dev/null +++ b/src/Appwrite/Auth/OAuth2/WordPress.php @@ -0,0 +1,136 @@ + $this->appID, + 'redirect_uri' => $this->callback, + 'response_type' => 'code', + 'scope' => $this->getScopes(), + 'state' => \json_encode($this->state) + ]); + } + + /** + * @param string $code + * + * @return string + */ + public function getAccessToken(string $code):string + { + $accessToken = $this->request( + 'POST', + 'https://public-api.wordpress.com/oauth2/token', + [], + \http_build_query([ + 'client_id' => $this->appID, + 'redirect_uri' => $this->callback, + 'client_secret' => $this->appSecret, + 'grant_type' => 'authorization_code', + 'code' => $code + ]) + ); + + $accessToken = \json_decode($accessToken, true); + + if (isset($accessToken['access_token'])) { + return $accessToken['access_token']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserID(string $accessToken):string + { + $user = $this->getUser($accessToken); + + if (isset($user['ID'])) { + return $user['ID']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserEmail(string $accessToken):string + { + $user = $this->getUser($accessToken); + + if (isset($user['email']) && $user['verified']) { + return $user['email']; + } + + return ''; + } + + /** + * @param $accessToken + * + * @return string + */ + public function getUserName(string $accessToken):string + { + $user = $this->getUser($accessToken); + + if (isset($user['username'])) { + return $user['username']; + } + + return ''; + } + + /** + * @param string $accessToken + * + * @return array + */ + protected function getUser(string $accessToken) + { + if (empty($this->user)) { + $this->user = \json_decode($this->request('GET', 'https://public-api.wordpress.com/rest/v1/me', ['Authorization: Bearer '.$accessToken]), true); + } + + return $this->user; + } +}