Closes issue 480
This commit is contained in:
@ -6,9 +6,6 @@ use \MailPoet\Util\Security;
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class API {
|
||||
function __construct() {
|
||||
}
|
||||
|
||||
function init() {
|
||||
// security token
|
||||
add_action(
|
||||
@ -27,7 +24,12 @@ class API {
|
||||
'wp_ajax_nopriv_mailpoet',
|
||||
array($this, 'setupPublic')
|
||||
);
|
||||
|
||||
// Public API (Post)
|
||||
add_action(
|
||||
'admin_post_mailpoet',
|
||||
array($this, 'setupPublic')
|
||||
);
|
||||
add_action(
|
||||
'admin_post_nopriv_mailpoet',
|
||||
array($this, 'setupPublic')
|
||||
@ -102,12 +104,16 @@ class API {
|
||||
try {
|
||||
$endpoint = new $endpoint();
|
||||
$response = $endpoint->$method($data);
|
||||
$response->send();
|
||||
if($doing_ajax) {
|
||||
$response->send();
|
||||
}
|
||||
} catch(\Exception $e) {
|
||||
$error_response = new ErrorResponse(
|
||||
array($e->getCode() => $e->getMessage())
|
||||
);
|
||||
$error_response->send();
|
||||
if($doing_ajax) {
|
||||
$error_response = new ErrorResponse(
|
||||
array($e->getCode() => $e->getMessage())
|
||||
);
|
||||
$error_response->send();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@ if(!defined('ABSPATH')) exit;
|
||||
|
||||
class CustomFields extends APIEndpoint {
|
||||
function getAll() {
|
||||
$collection = CustomField::findMany();
|
||||
$collection = CustomField::orderByAsc('created_at')->findMany();
|
||||
$custom_fields = array_map(function($custom_field) {
|
||||
return $custom_field->asArray();
|
||||
}, $collection);
|
||||
|
@ -156,7 +156,6 @@ class Forms extends APIEndpoint {
|
||||
// check if the user gets to pick his own lists
|
||||
// or if it's selected by the admin
|
||||
$has_segment_selection = false;
|
||||
|
||||
foreach($body as $i => $block) {
|
||||
if($block['type'] === 'segment') {
|
||||
$has_segment_selection = true;
|
||||
@ -164,7 +163,7 @@ class Forms extends APIEndpoint {
|
||||
$list_selection = array_filter(
|
||||
array_map(function($segment) {
|
||||
return (isset($segment['id'])
|
||||
? (int)$segment['id']
|
||||
? $segment['id']
|
||||
: null
|
||||
);
|
||||
}, $block['params']['values'])
|
||||
@ -177,6 +176,7 @@ class Forms extends APIEndpoint {
|
||||
// check list selection
|
||||
if($has_segment_selection === true) {
|
||||
$settings['segments_selected_by'] = 'user';
|
||||
$settings['segments'] = $list_selection;
|
||||
} else {
|
||||
$settings['segments_selected_by'] = 'admin';
|
||||
}
|
||||
|
@ -57,13 +57,9 @@ class Subscribers extends APIEndpoint {
|
||||
|
||||
function subscribe($data = array()) {
|
||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
||||
$errors = array();
|
||||
|
||||
$form = Form::findOne($data['form_id']);
|
||||
$form_id = (isset($data['form_id']) ? (int)$data['form_id'] : false);
|
||||
$form = Form::findOne($form_id);
|
||||
unset($data['form_id']);
|
||||
if($form === false || !$form->id()) {
|
||||
$errors[] = __('This form does not exist');
|
||||
}
|
||||
|
||||
$segment_ids = (!empty($data['segments'])
|
||||
? (array)$data['segments']
|
||||
@ -72,71 +68,63 @@ class Subscribers extends APIEndpoint {
|
||||
unset($data['segments']);
|
||||
|
||||
if(empty($segment_ids)) {
|
||||
$errors[] = __('Please select a list');
|
||||
}
|
||||
|
||||
if(!empty($errors)) {
|
||||
return array(
|
||||
'result' => false,
|
||||
'errors' => $errors
|
||||
);
|
||||
if($doing_ajax) {
|
||||
return $this->badRequest(array(
|
||||
APIError::BAD_REQUEST => __('Please select a list')
|
||||
));
|
||||
} else {
|
||||
Url::redirectBack(array(
|
||||
'mailpoet_error' => $form_id,
|
||||
'mailpoet_success' => null
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// try to register and subscribe user to segments
|
||||
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||
$errors = $subscriber->getErrors();
|
||||
$result = ($errors === false && $subscriber->id() > 0);
|
||||
|
||||
// record form statistics
|
||||
if($result === true && $form !== false && $form->id > 0) {
|
||||
StatisticsForms::record($form->id, $subscriber->id);
|
||||
}
|
||||
if($errors !== false) {
|
||||
if($doing_ajax) {
|
||||
return $this->badRequest($errors);
|
||||
} else {
|
||||
Url::redirectBack(array(
|
||||
'mailpoet_error' => $form_id,
|
||||
'mailpoet_success' => null
|
||||
));
|
||||
}
|
||||
} else {
|
||||
$meta = array();
|
||||
|
||||
// get success message to display after subscription
|
||||
$form_settings = (
|
||||
isset($form->settings)
|
||||
? unserialize($form->settings)
|
||||
: null
|
||||
);
|
||||
if($form !== false) {
|
||||
// record form statistics
|
||||
StatisticsForms::record($form->id, $subscriber->id);
|
||||
|
||||
if($form_settings !== null) {
|
||||
switch($form_settings['on_success']) {
|
||||
case 'page':
|
||||
$success_page_url = get_permalink($form_settings['success_page']);
|
||||
$form = $form->asArray();
|
||||
|
||||
// response depending on context
|
||||
if($doing_ajax === true) {
|
||||
return array(
|
||||
'result' => $result,
|
||||
'page' => $success_page_url,
|
||||
'errors' => $errors
|
||||
);
|
||||
} else {
|
||||
if($result === false) {
|
||||
Url::redirectBack();
|
||||
} else {
|
||||
Url::redirectTo($success_page_url);
|
||||
}
|
||||
}
|
||||
break;
|
||||
if($form['settings']['on_success'] === 'page') {
|
||||
// redirect to a page on a success, pass the page url in the meta
|
||||
$meta['redirect_url'] = get_permalink($form['settings']['success_page']);
|
||||
} else if($form['settings']['on_success'] === 'url') {
|
||||
$meta['redirect_url'] = $form['settings']['success_url'];
|
||||
}
|
||||
}
|
||||
|
||||
case 'message':
|
||||
default:
|
||||
// response depending on context
|
||||
if($doing_ajax === true) {
|
||||
return array(
|
||||
'result' => $result,
|
||||
'errors' => $errors
|
||||
);
|
||||
} else {
|
||||
$params = (
|
||||
($result === true)
|
||||
? array('mailpoet_success' => $form->id)
|
||||
: array()
|
||||
);
|
||||
|
||||
Url::redirectBack($params);
|
||||
}
|
||||
break;
|
||||
if($doing_ajax) {
|
||||
return $this->successResponse(
|
||||
Subscriber::findOne($subscriber->id)->asArray(),
|
||||
$meta
|
||||
);
|
||||
} else {
|
||||
if(isset($meta['redirect_url'])) {
|
||||
Url::redirectTo($meta['redirect_url']);
|
||||
} else {
|
||||
Url::redirectBack(array(
|
||||
'mailpoet_success' => $form['id'],
|
||||
'mailpoet_error' => null
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user