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',
|
'wp_ajax_nopriv_mailpoet',
|
||||||
array($this, 'setupPublic')
|
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() {
|
function setupAdmin() {
|
||||||
@ -81,14 +71,7 @@ class API {
|
|||||||
$class = ucfirst($_POST['endpoint']);
|
$class = ucfirst($_POST['endpoint']);
|
||||||
$endpoint = __NAMESPACE__ . "\\Endpoints\\" . $class;
|
$endpoint = __NAMESPACE__ . "\\Endpoints\\" . $class;
|
||||||
$method = $_POST['method'];
|
$method = $_POST['method'];
|
||||||
|
$data = isset($_POST['data']) ? stripslashes_deep($_POST['data']) : array();
|
||||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
|
||||||
|
|
||||||
if($doing_ajax) {
|
|
||||||
$data = isset($_POST['data']) ? stripslashes_deep($_POST['data']) : array();
|
|
||||||
} else {
|
|
||||||
$data = $_POST;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(is_array($data) && !empty($data)) {
|
if(is_array($data) && !empty($data)) {
|
||||||
// filter out reserved keywords from data
|
// filter out reserved keywords from data
|
||||||
@ -104,16 +87,12 @@ class API {
|
|||||||
try {
|
try {
|
||||||
$endpoint = new $endpoint();
|
$endpoint = new $endpoint();
|
||||||
$response = $endpoint->$method($data);
|
$response = $endpoint->$method($data);
|
||||||
if($doing_ajax) {
|
$response->send();
|
||||||
$response->send();
|
|
||||||
}
|
|
||||||
} catch(\Exception $e) {
|
} catch(\Exception $e) {
|
||||||
if($doing_ajax) {
|
$error_response = new ErrorResponse(
|
||||||
$error_response = new ErrorResponse(
|
array($e->getCode() => $e->getMessage())
|
||||||
array($e->getCode() => $e->getMessage())
|
);
|
||||||
);
|
$error_response->send();
|
||||||
$error_response->send();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,7 +11,6 @@ use MailPoet\Models\Segment;
|
|||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Models\Form;
|
use MailPoet\Models\Form;
|
||||||
use MailPoet\Models\StatisticsForms;
|
use MailPoet\Models\StatisticsForms;
|
||||||
use MailPoet\Util\Url;
|
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@ -56,7 +55,6 @@ class Subscribers extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function subscribe($data = array()) {
|
function subscribe($data = array()) {
|
||||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
|
||||||
$form_id = (isset($data['form_id']) ? (int)$data['form_id'] : false);
|
$form_id = (isset($data['form_id']) ? (int)$data['form_id'] : false);
|
||||||
$form = Form::findOne($form_id);
|
$form = Form::findOne($form_id);
|
||||||
unset($data['form_id']);
|
unset($data['form_id']);
|
||||||
@ -68,31 +66,16 @@ class Subscribers extends APIEndpoint {
|
|||||||
unset($data['segments']);
|
unset($data['segments']);
|
||||||
|
|
||||||
if(empty($segment_ids)) {
|
if(empty($segment_ids)) {
|
||||||
if($doing_ajax) {
|
return $this->badRequest(array(
|
||||||
return $this->badRequest(array(
|
APIError::BAD_REQUEST => __('Please select a list')
|
||||||
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);
|
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||||
$errors = $subscriber->getErrors();
|
$errors = $subscriber->getErrors();
|
||||||
|
|
||||||
if($errors !== false) {
|
if($errors !== false) {
|
||||||
if($doing_ajax) {
|
return $this->badRequest($errors);
|
||||||
return $this->badRequest($errors);
|
|
||||||
} else {
|
|
||||||
Url::redirectBack(array(
|
|
||||||
'mailpoet_error' => $form_id,
|
|
||||||
'mailpoet_success' => null
|
|
||||||
));
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$meta = array();
|
$meta = array();
|
||||||
|
|
||||||
@ -110,22 +93,10 @@ class Subscribers extends APIEndpoint {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $this->successResponse(
|
||||||
if($doing_ajax) {
|
Subscriber::findOne($subscriber->id)->asArray(),
|
||||||
return $this->successResponse(
|
$meta
|
||||||
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
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,6 +91,16 @@ class Hooks {
|
|||||||
'admin_post_nopriv_mailpoet_subscription_update',
|
'admin_post_nopriv_mailpoet_subscription_update',
|
||||||
'\MailPoet\Subscription\Manage::onSave'
|
'\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() {
|
function setupWPUsers() {
|
||||||
|
@ -166,34 +166,32 @@ class Subscriber extends Model {
|
|||||||
$subscriber = self::findOne($subscriber->id);
|
$subscriber = self::findOne($subscriber->id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($subscriber !== false) {
|
// restore trashed subscriber
|
||||||
// restore trashed subscriber
|
if($subscriber->deleted_at !== null) {
|
||||||
if($subscriber->deleted_at !== null) {
|
$subscriber->setExpr('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->save()) {
|
||||||
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
|
// link subscriber to segments
|
||||||
if($signup_confirmation_enabled === true) {
|
SubscriberSegment::subscribeToSegments($subscriber, $segment_ids);
|
||||||
$subscriber->set('status', self::STATUS_UNCONFIRMED);
|
|
||||||
} else {
|
|
||||||
$subscriber->set('status', self::STATUS_SUBSCRIBED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if($subscriber->save()) {
|
// signup confirmation
|
||||||
// link subscriber to segments
|
$subscriber->sendConfirmationEmail();
|
||||||
SubscriberSegment::subscribeToSegments($subscriber, $segment_ids);
|
|
||||||
|
|
||||||
// signup confirmation
|
// welcome email
|
||||||
$subscriber->sendConfirmationEmail();
|
Scheduler::scheduleSubscriberWelcomeNotification(
|
||||||
|
$subscriber->id,
|
||||||
// welcome email
|
$segment_ids
|
||||||
Scheduler::scheduleSubscriberWelcomeNotification(
|
);
|
||||||
$subscriber->id,
|
|
||||||
$segment_ids
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $subscriber;
|
return $subscriber;
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
namespace MailPoet\Router\Endpoints;
|
namespace MailPoet\Router\Endpoints;
|
||||||
|
|
||||||
use MailPoet\Subscription as UserSubscription;
|
use MailPoet\Subscription as UserSubscription;
|
||||||
|
use MailPoet\Util\Url;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
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 %>
|
<%= styles | raw %>
|
||||||
<form
|
<form
|
||||||
method="post"
|
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 %>"
|
class="mailpoet_form mailpoet_form_<%= form_type %>"
|
||||||
novalidate
|
novalidate
|
||||||
>
|
>
|
||||||
|
Reference in New Issue
Block a user