diff --git a/mailpoet/assets/js/src/common/authorize_sender_email_modal.tsx b/mailpoet/assets/js/src/common/authorize_sender_email_modal.tsx index 27115d28fc..926f26c04c 100644 --- a/mailpoet/assets/js/src/common/authorize_sender_email_modal.tsx +++ b/mailpoet/assets/js/src/common/authorize_sender_email_modal.tsx @@ -128,7 +128,7 @@ function AuthorizeSenderEmailModal({ makeApiRequest(senderEmailAddress) .then((res) => { - const response = Boolean(res?.data); + const response = Boolean(res?.data?.status); setCreateEmailApiResponse(response); setShowLoader(response); if (response) { diff --git a/mailpoet/lib/API/JSON/v1/Settings.php b/mailpoet/lib/API/JSON/v1/Settings.php index ba140d0aab..e18c8959cf 100644 --- a/mailpoet/lib/API/JSON/v1/Settings.php +++ b/mailpoet/lib/API/JSON/v1/Settings.php @@ -219,7 +219,7 @@ class Settings extends APIEndpoint { $e->getMessage() === AuthorizedEmailsController::AUTHORIZED_EMAIL_ERROR_PENDING_CONFIRMATION ) { // return true if the email is already authorized or pending confirmation - $response = true; + $response = ['status' => true]; } else { return $this->badRequest([ APIError::BAD_REQUEST => WPFunctions::get()->__($e->getMessage(), 'mailpoet'), diff --git a/mailpoet/lib/Services/AuthorizedEmailsController.php b/mailpoet/lib/Services/AuthorizedEmailsController.php index 8f2aa8d373..680b3b3c36 100644 --- a/mailpoet/lib/Services/AuthorizedEmailsController.php +++ b/mailpoet/lib/Services/AuthorizedEmailsController.php @@ -64,11 +64,11 @@ class AuthorizedEmailsController { $this->settings->set(self::AUTHORIZED_EMAIL_ADDRESSES_ERROR_SETTING, null); } - public function getAllAuthorizedEmailAddress() { + public function getAllAuthorizedEmailAddress(): array { return $this->bridge->getAuthorizedEmailAddresses(self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_ALL); } - public function createAuthorizedEmailAddress(string $email) { + public function createAuthorizedEmailAddress(string $email): array { $allEmails = $this->getAllAuthorizedEmailAddress(); $authorizedEmails = isset($allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_AUTHORIZED]) ? $allEmails[self::AUTHORIZED_EMAIL_ADDRESSES_API_TYPE_AUTHORIZED] : []; @@ -87,9 +87,8 @@ class AuthorizedEmailsController { $finalData = $this->bridge->createAuthorizedEmailAddress($email); - if (!is_bool($finalData) && is_array($finalData)) { - $errorMessage = isset($finalData['error']) ? $finalData['error'] : ' '; - throw new \InvalidArgumentException($errorMessage); + if ($finalData && isset($finalData['error'])) { + throw new \InvalidArgumentException($finalData['error']); } return $finalData; diff --git a/mailpoet/lib/Services/Bridge.php b/mailpoet/lib/Services/Bridge.php index 2b18b5419f..cdb59ba190 100644 --- a/mailpoet/lib/Services/Bridge.php +++ b/mailpoet/lib/Services/Bridge.php @@ -130,8 +130,6 @@ class Bridge { /** * Create Authorized Email Address - * - * returns true if done or an array of error messages */ public function createAuthorizedEmailAddress(string $emailAdress) { $data = $this diff --git a/mailpoet/lib/Services/Bridge/API.php b/mailpoet/lib/Services/Bridge/API.php index baf563775d..e2fb3861a4 100644 --- a/mailpoet/lib/Services/Bridge/API.php +++ b/mailpoet/lib/Services/Bridge/API.php @@ -183,11 +183,11 @@ class API { /** * Create Authorized Email Address * - * returns true if done or an array of error messages + * returns ['status' => true] if done or an array of error messages ['error' => $errorBody, 'status' => false] * @param string $emailAddress - * @return bool|array + * @return array */ - public function createAuthorizedEmailAddress(string $emailAddress) { + public function createAuthorizedEmailAddress(string $emailAddress): array { $body = ['email' => $emailAddress]; $result = $this->request( $this->urlAuthorizedEmailAddresses, @@ -204,12 +204,15 @@ class API { 'error' => is_wp_error($result) ? $result->get_error_message() : $errorBody, ]; $this->loggerFactory->getLogger(LoggerFactory::TOPIC_BRIDGE)->error('CreateAuthorizedEmailAddress API call failed.', $logData); + $errorResponseData = json_decode($errorBody, true); $fallbackError = sprintf($this->wp->__('An error has happened while performing a request, the server has responded with response code %d'), $code); - return is_array($errorResponseData) ? $errorResponseData : ['error' => $fallbackError]; + + $errorData = is_array($errorResponseData) && isset($errorResponseData['error']) ? $errorResponseData['error'] : $fallbackError; + return ['error' => $errorData, 'status' => false]; } - return $isSuccess; + return ['status' => $isSuccess]; } public function setKey($apiKey) { diff --git a/mailpoet/tests/integration/Services/AuthorizedEmailsControllerTest.php b/mailpoet/tests/integration/Services/AuthorizedEmailsControllerTest.php index 199f6bf679..68b95b639b 100644 --- a/mailpoet/tests/integration/Services/AuthorizedEmailsControllerTest.php +++ b/mailpoet/tests/integration/Services/AuthorizedEmailsControllerTest.php @@ -264,14 +264,15 @@ class AuthorizedEmailsControllerTest extends \MailPoetTest { 'pending' => ['pending@email.com'], 'authorized' => ['authorized@email.com'] ]; + $response = ['status' => true]; $bridgeMock = $this->make(Bridge::class, [ 'getAuthorizedEmailAddresses' => Expected::once($array), - 'createAuthorizedEmailAddress' => Expected::once(true) + 'createAuthorizedEmailAddress' => Expected::once($response) ]); $newslettersRepository = $this->diContainer->get(NewslettersRepository::class); $controller = new AuthorizedEmailsController($this->settings, $bridgeMock, $newslettersRepository); $result = $controller->createAuthorizedEmailAddress('new-authorized@email.com'); - expect($result)->equals(true); + expect($result)->equals($response); } public function testItThrowsAnExceptionForReturnedArrayForCreateNewAuthorizedEmailAddress() {