remove doing_ajax logic from API and created dedicated class for subscription form non ajax submission
This commit is contained in:
@ -24,16 +24,6 @@ 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')
|
||||
);
|
||||
}
|
||||
|
||||
function setupAdmin() {
|
||||
@ -81,14 +71,7 @@ class API {
|
||||
$class = ucfirst($_POST['endpoint']);
|
||||
$endpoint = __NAMESPACE__ . "\\Endpoints\\" . $class;
|
||||
$method = $_POST['method'];
|
||||
|
||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
||||
|
||||
if($doing_ajax) {
|
||||
$data = isset($_POST['data']) ? stripslashes_deep($_POST['data']) : array();
|
||||
} else {
|
||||
$data = $_POST;
|
||||
}
|
||||
$data = isset($_POST['data']) ? stripslashes_deep($_POST['data']) : array();
|
||||
|
||||
if(is_array($data) && !empty($data)) {
|
||||
// filter out reserved keywords from data
|
||||
@ -104,16 +87,12 @@ class API {
|
||||
try {
|
||||
$endpoint = new $endpoint();
|
||||
$response = $endpoint->$method($data);
|
||||
if($doing_ajax) {
|
||||
$response->send();
|
||||
}
|
||||
$response->send();
|
||||
} catch(\Exception $e) {
|
||||
if($doing_ajax) {
|
||||
$error_response = new ErrorResponse(
|
||||
array($e->getCode() => $e->getMessage())
|
||||
);
|
||||
$error_response->send();
|
||||
}
|
||||
$error_response = new ErrorResponse(
|
||||
array($e->getCode() => $e->getMessage())
|
||||
);
|
||||
$error_response->send();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,6 @@ use MailPoet\Models\Segment;
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Models\Form;
|
||||
use MailPoet\Models\StatisticsForms;
|
||||
use MailPoet\Util\Url;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -56,7 +55,6 @@ class Subscribers extends APIEndpoint {
|
||||
}
|
||||
|
||||
function subscribe($data = array()) {
|
||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
||||
$form_id = (isset($data['form_id']) ? (int)$data['form_id'] : false);
|
||||
$form = Form::findOne($form_id);
|
||||
unset($data['form_id']);
|
||||
@ -68,31 +66,16 @@ class Subscribers extends APIEndpoint {
|
||||
unset($data['segments']);
|
||||
|
||||
if(empty($segment_ids)) {
|
||||
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
|
||||
));
|
||||
}
|
||||
return $this->badRequest(array(
|
||||
APIError::BAD_REQUEST => __('Please select a list')
|
||||
));
|
||||
}
|
||||
|
||||
// try to register and subscribe user to segments
|
||||
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||
$errors = $subscriber->getErrors();
|
||||
|
||||
if($errors !== false) {
|
||||
if($doing_ajax) {
|
||||
return $this->badRequest($errors);
|
||||
} else {
|
||||
Url::redirectBack(array(
|
||||
'mailpoet_error' => $form_id,
|
||||
'mailpoet_success' => null
|
||||
));
|
||||
}
|
||||
return $this->badRequest($errors);
|
||||
} else {
|
||||
$meta = array();
|
||||
|
||||
@ -110,22 +93,10 @@ class Subscribers extends APIEndpoint {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
));
|
||||
}
|
||||
}
|
||||
return $this->successResponse(
|
||||
Subscriber::findOne($subscriber->id)->asArray(),
|
||||
$meta
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -91,6 +91,16 @@ class Hooks {
|
||||
'admin_post_nopriv_mailpoet_subscription_update',
|
||||
'\MailPoet\Subscription\Manage::onSave'
|
||||
);
|
||||
|
||||
// Subscription form
|
||||
add_action(
|
||||
'admin_post_mailpoet_subscription_form',
|
||||
'\MailPoet\Subscription\Form::onSubmit'
|
||||
);
|
||||
add_action(
|
||||
'admin_post_nopriv_mailpoet_subscription_form',
|
||||
'\MailPoet\Subscription\Form::onSubmit'
|
||||
);
|
||||
}
|
||||
|
||||
function setupWPUsers() {
|
||||
|
@ -166,34 +166,32 @@ class Subscriber extends Model {
|
||||
$subscriber = self::findOne($subscriber->id);
|
||||
}
|
||||
|
||||
if($subscriber !== false) {
|
||||
// restore trashed subscriber
|
||||
if($subscriber->deleted_at !== null) {
|
||||
$subscriber->setExpr('deleted_at', 'NULL');
|
||||
// restore trashed subscriber
|
||||
if($subscriber->deleted_at !== null) {
|
||||
$subscriber->setExpr('deleted_at', 'NULL');
|
||||
}
|
||||
|
||||
// set status depending on signup confirmation setting
|
||||
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
|
||||
if($signup_confirmation_enabled === true) {
|
||||
$subscriber->set('status', self::STATUS_UNCONFIRMED);
|
||||
} else {
|
||||
$subscriber->set('status', self::STATUS_SUBSCRIBED);
|
||||
}
|
||||
}
|
||||
|
||||
// set status depending on signup confirmation setting
|
||||
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
|
||||
if($signup_confirmation_enabled === true) {
|
||||
$subscriber->set('status', self::STATUS_UNCONFIRMED);
|
||||
} else {
|
||||
$subscriber->set('status', self::STATUS_SUBSCRIBED);
|
||||
}
|
||||
}
|
||||
if($subscriber->save()) {
|
||||
// link subscriber to segments
|
||||
SubscriberSegment::subscribeToSegments($subscriber, $segment_ids);
|
||||
|
||||
if($subscriber->save()) {
|
||||
// link subscriber to segments
|
||||
SubscriberSegment::subscribeToSegments($subscriber, $segment_ids);
|
||||
// signup confirmation
|
||||
$subscriber->sendConfirmationEmail();
|
||||
|
||||
// signup confirmation
|
||||
$subscriber->sendConfirmationEmail();
|
||||
|
||||
// welcome email
|
||||
Scheduler::scheduleSubscriberWelcomeNotification(
|
||||
$subscriber->id,
|
||||
$segment_ids
|
||||
);
|
||||
}
|
||||
// welcome email
|
||||
Scheduler::scheduleSubscriberWelcomeNotification(
|
||||
$subscriber->id,
|
||||
$segment_ids
|
||||
);
|
||||
}
|
||||
|
||||
return $subscriber;
|
||||
|
@ -2,6 +2,7 @@
|
||||
namespace MailPoet\Router\Endpoints;
|
||||
|
||||
use MailPoet\Subscription as UserSubscription;
|
||||
use MailPoet\Util\Url;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
|
69
lib/Subscription/Form.php
Normal file
69
lib/Subscription/Form.php
Normal file
@ -0,0 +1,69 @@
|
||||
<?php
|
||||
namespace MailPoet\Subscription;
|
||||
use \MailPoet\Models\Subscriber;
|
||||
use \MailPoet\Models\StatisticsForms;
|
||||
use \MailPoet\Models\Form as FormModel;
|
||||
use \MailPoet\Util\Url;
|
||||
|
||||
class Form {
|
||||
static function onSubmit() {
|
||||
$reserved_keywords = array(
|
||||
'token',
|
||||
'endpoint',
|
||||
'method',
|
||||
'mailpoet_redirect'
|
||||
);
|
||||
$data = array_diff_key($_POST, array_flip($reserved_keywords));
|
||||
|
||||
$form_id = (isset($data['form_id']) ? (int)$data['form_id'] : false);
|
||||
$form = FormModel::findOne($form_id);
|
||||
unset($data['form_id']);
|
||||
|
||||
$segment_ids = (!empty($data['segments'])
|
||||
? (array)$data['segments']
|
||||
: array()
|
||||
);
|
||||
unset($data['segments']);
|
||||
|
||||
if(empty($segment_ids)) {
|
||||
Url::redirectBack(array(
|
||||
'mailpoet_error' => $form_id,
|
||||
'mailpoet_success' => null
|
||||
));
|
||||
}
|
||||
|
||||
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||
$errors = $subscriber->getErrors();
|
||||
if($errors !== false) {
|
||||
Url::redirectBack(array(
|
||||
'mailpoet_error' => $form_id,
|
||||
'mailpoet_success' => null
|
||||
));
|
||||
} else {
|
||||
$meta = array();
|
||||
|
||||
if($form !== false) {
|
||||
// record form statistics
|
||||
StatisticsForms::record($form->id, $subscriber->id);
|
||||
|
||||
$form = $form->asArray();
|
||||
|
||||
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'];
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($meta['redirect_url'])) {
|
||||
Url::redirectTo($meta['redirect_url']);
|
||||
} else {
|
||||
Url::redirectBack(array(
|
||||
'mailpoet_success' => $form['id'],
|
||||
'mailpoet_error' => null
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@
|
||||
<%= styles | raw %>
|
||||
<form
|
||||
method="post"
|
||||
action="<%= admin_url('admin-post.php?action=mailpoet') | raw %>"
|
||||
action="<%= admin_url('admin-post.php?action=mailpoet_subscription_form') | raw %>"
|
||||
class="mailpoet_form mailpoet_form_<%= form_type %>"
|
||||
novalidate
|
||||
>
|
||||
|
Reference in New Issue
Block a user