updated mailer endpoint

This commit is contained in:
Jonathan Labreuille
2016-08-08 17:41:30 +02:00
parent d7c5c8c3e7
commit 4950e47297
4 changed files with 29 additions and 35 deletions

View File

@ -1,11 +1,12 @@
<?php <?php
namespace MailPoet\API\Endpoints; namespace MailPoet\API\Endpoints;
use MailPoet\API\Endpoint as APIEndpoint;
use MailPoet\API\Error as APIError;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class Mailer { class Mailer extends APIEndpoint {
function send($data) { function send($data = array()) {
$response = array();
try { try {
$mailer = new \MailPoet\Mailer\Mailer( $mailer = new \MailPoet\Mailer\Mailer(
(isset($data['mailer'])) ? $data['mailer'] : false, (isset($data['mailer'])) ? $data['mailer'] : false,
@ -14,10 +15,17 @@ class Mailer {
); );
$result = $mailer->send($data['newsletter'], $data['subscriber']); $result = $mailer->send($data['newsletter'], $data['subscriber']);
} catch(\Exception $e) { } catch(\Exception $e) {
$result = false; return $this->errorResponse(array(
$response['errors'] = array($e->getMessage()); $e->getCode() => $e->getMessage()
));
}
if($result === false) {
return $this->errorResponse(array(
APIError::BAD_REQUEST => __("The email could not be sent. Please check your settings.")
));
} else {
return $this->successResponse(null);
} }
$response['result'] = ($result) ? true : false;
return $response;
} }
} }

View File

@ -27,7 +27,7 @@ abstract class Response {
if(!empty($this->meta)) { if(!empty($this->meta)) {
$response['meta'] = $this->meta; $response['meta'] = $this->meta;
} }
if(!empty($data)) { if($data !== null) {
$response = array_merge($response, $data); $response = array_merge($response, $data);
} }

View File

@ -6,12 +6,14 @@ if(!defined('ABSPATH')) exit;
class SuccessResponse extends Response { class SuccessResponse extends Response {
public $data; public $data;
function __construct($data = array(), $meta = array(), $status = self::STATUS_OK) { function __construct($data = null, $meta = null, $status = self::STATUS_OK) {
parent::__construct($status, $meta); parent::__construct($status, $meta);
$this->data = $data; $this->data = $data;
} }
function getData() { function getData() {
if($this->data === null) return null;
return array( return array(
'data' => $this->data 'data' => $this->data
); );

View File

@ -699,36 +699,20 @@
email: recipient email: recipient
} }
} }
}).done(function(response) { }).always(function() {
MailPoet.Modal.loading(false); MailPoet.Modal.loading(false);
if(response.result === true) { }).done(function(response) {
MailPoet.Notice.success( MailPoet.Notice.success(
"<%= __('The email has been sent! Check your inbox.') %>", "<%= __('The email has been sent! Check your inbox.') %>",
{ scroll: true }
);
}).fail(function(response) {
if (response.errors.length > 0) {
MailPoet.Notice.error(
response.errors.map(function(error) { return error.message; }),
{ scroll: true } { scroll: true }
); );
} else {
if (response.errors) {
MailPoet.Notice.error(
"<%= __('The email could not be sent: ') %> " + response.errors,
{ scroll: true }
);
}
else {
if(mailer.method === 'PHPMail') {
MailPoet.Notice.error(
"<%= __('The email could not be sent. Please contact your host to fix this issue.') %>",
{ scroll: true, static: true }
);
} else {
MailPoet.Notice.error(
"<%= __('The email could not be sent. Please check your settings.') %>",
{ scroll: true }
);
}
}
} }
}).error(function(response) {
MailPoet.Modal.loading(false);
}); });
}); });