<?php
 namespace Krombox\OAuth2\Client\Provider; use League\OAuth2\Client\Provider\AbstractProvider; use League\OAuth2\Client\Provider\Exception\IdentityProviderException; use League\OAuth2\Client\Token\AccessToken; use League\OAuth2\Client\Tool\BearerAuthorizationTrait; use Psr\Http\Message\ResponseInterface; class WordPress extends AbstractProvider { use BearerAuthorizationTrait; private $urlAuthorize; private $urlAccessToken; private $urlResourceOwnerDetails; private $domain; public function __construct(array $options = [], array $collaborators = []) { $this->assertRequiredOptions($options); $possible = $this->getConfigurableOptions(); $configured = array_intersect_key($options, array_flip($possible)); foreach ($configured as $key => $value) { $this->$key = $value; } $options = array_diff_key($options, $configured); parent::__construct($options, $collaborators); } protected function getConfigurableOptions() { return array_merge($this->getRequiredOptions(), [ 'urlAuthorize', 'urlAccessToken', 'urlResourceOwnerDetails', ]); } protected function getRequiredOptions() { return [ 'domain' ]; } protected function assertRequiredOptions(array $options) { $missing = array_diff_key(array_flip($this->getRequiredOptions()), $options); if (!empty($missing)) { throw new \Exception( 'Required options not defined: ' . implode(', ', array_keys($missing)) ); } } public function getBaseAuthorizationUrl() { return $this->urlAuthorize ? $this->urlAuthorize : $this->domain . '/oauth/authorize'; } public function getBaseAccessTokenUrl(array $params) { return $this->urlAccessToken ? $this->urlAccessToken : $this->domain . '/oauth/token'; } public function getResourceOwnerDetailsUrl(AccessToken $token) { return $this->urlResourceOwnerDetails ? $this->urlResourceOwnerDetails : $this->domain . '/wp-json/wp/v2/users/me?context=edit'; } protected function getDefaultScopes() { return []; } protected function checkResponse(ResponseInterface $response, $data) { if (!empty($data['error'])) { $message = $data['error'].': '.$data['error_description']; throw new IdentityProviderException($message, $response->getStatusCode(), $data); } } protected function createResourceOwner(array $response, AccessToken $token) { return new WordPressResourceOwner($response); } } 