form subscription only using Router now
This commit is contained in:
@@ -22,7 +22,7 @@ function(
|
|||||||
|
|
||||||
form.parsley().on('form:validated', function(parsley) {
|
form.parsley().on('form:validated', function(parsley) {
|
||||||
// clear messages
|
// clear messages
|
||||||
form.find('.mailpoet_message').html('');
|
//form.find('.mailpoet_message').hide();
|
||||||
|
|
||||||
// resize iframe
|
// resize iframe
|
||||||
if(window.frameElement !== null) {
|
if(window.frameElement !== null) {
|
||||||
@@ -59,13 +59,9 @@ function(
|
|||||||
if(response.page !== undefined) {
|
if(response.page !== undefined) {
|
||||||
// go to page
|
// go to page
|
||||||
window.location.href = response.page;
|
window.location.href = response.page;
|
||||||
} else if(response.message !== undefined) {
|
} else {
|
||||||
// display success message
|
// display success message
|
||||||
form
|
form.find('.mailpoet_validate_success').show();
|
||||||
.find('.mailpoet_message')
|
|
||||||
.html('<p class="mailpoet_validate_success">'+
|
|
||||||
response.message+
|
|
||||||
'</p>');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// reset form
|
// reset form
|
||||||
|
@@ -9,14 +9,13 @@ class Hooks {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
$this->setupSubscribe();
|
|
||||||
$this->setupWPUsers();
|
$this->setupWPUsers();
|
||||||
$this->setupImageSize();
|
$this->setupImageSize();
|
||||||
$this->setupListing();
|
$this->setupListing();
|
||||||
$this->setupManageSubscription();
|
$this->setupSubscriptionEvents();
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupSubscribe() {
|
function setupSubscriptionEvents() {
|
||||||
$subscribe = Setting::getValue('subscribe', array());
|
$subscribe = Setting::getValue('subscribe', array());
|
||||||
// Subscribe in comments
|
// Subscribe in comments
|
||||||
if(
|
if(
|
||||||
@@ -98,7 +97,7 @@ class Hooks {
|
|||||||
add_action(
|
add_action(
|
||||||
'profile_update',
|
'profile_update',
|
||||||
'\MailPoet\Segments\WP::synchronizeUser',
|
'\MailPoet\Segments\WP::synchronizeUser',
|
||||||
1,2
|
1, 2
|
||||||
);
|
);
|
||||||
add_action(
|
add_action(
|
||||||
'delete_user',
|
'delete_user',
|
||||||
@@ -140,18 +139,6 @@ class Hooks {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupManageSubscription() {
|
|
||||||
// handle subscription form submission
|
|
||||||
add_action(
|
|
||||||
'admin_post_mailpoet_subscription_update',
|
|
||||||
'\MailPoet\Subscription\Manage::onSave'
|
|
||||||
);
|
|
||||||
add_action(
|
|
||||||
'admin_post_nopriv_mailpoet_subscription_update',
|
|
||||||
'\MailPoet\Subscription\Manage::onSave'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function setScreenOption($status, $option, $value) {
|
function setScreenOption($status, $option, $value) {
|
||||||
if(preg_match('/^mailpoet_(.*)_per_page$/', $option)) {
|
if(preg_match('/^mailpoet_(.*)_per_page$/', $option)) {
|
||||||
return $value;
|
return $value;
|
||||||
|
@@ -7,33 +7,12 @@ use \MailPoet\Models\Setting;
|
|||||||
use \MailPoet\Models\Subscriber;
|
use \MailPoet\Models\Subscriber;
|
||||||
use \MailPoet\Form\Renderer as FormRenderer;
|
use \MailPoet\Form\Renderer as FormRenderer;
|
||||||
use \MailPoet\Form\Util;
|
use \MailPoet\Form\Util;
|
||||||
|
use \MailPoet\Util\Security;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class Widget extends \WP_Widget {
|
class Widget extends \WP_Widget {
|
||||||
function __construct () {
|
function __construct () {
|
||||||
// add_action(
|
|
||||||
// 'wp_ajax_mailpoet_form_subscribe',
|
|
||||||
// array($this, 'subscribe')
|
|
||||||
// );
|
|
||||||
// add_action(
|
|
||||||
// 'wp_ajax_nopriv_mailpoet_form_subscribe',
|
|
||||||
// array($this, 'subscribe')
|
|
||||||
// );
|
|
||||||
// add_action(
|
|
||||||
// 'admin_post_nopriv_mailpoet_form_subscribe',
|
|
||||||
// array($this, 'subscribe')
|
|
||||||
// );
|
|
||||||
// add_action(
|
|
||||||
// 'admin_post_mailpoet_form_subscribe',
|
|
||||||
// array($this, 'subscribe')
|
|
||||||
// );
|
|
||||||
|
|
||||||
// add_action(
|
|
||||||
// 'init',
|
|
||||||
// array($this, 'subscribe')
|
|
||||||
// );
|
|
||||||
|
|
||||||
return parent::__construct(
|
return parent::__construct(
|
||||||
'mailpoet_form',
|
'mailpoet_form',
|
||||||
__('MailPoet Form'),
|
__('MailPoet Form'),
|
||||||
@@ -169,21 +148,15 @@ class Widget extends \WP_Widget {
|
|||||||
'after_title' => (!empty($after_title) ? $after_title : '')
|
'after_title' => (!empty($after_title) ? $after_title : '')
|
||||||
);
|
);
|
||||||
|
|
||||||
// if(isset($_GET['mailpoet_form']) && (int)$_GET['mailpoet_form'] === $form['id']) {
|
// check if the form was submitted
|
||||||
// // form messages (success / error)
|
$data['is_submitted'] = (
|
||||||
// $output .= '<div class="mailpoet_message">';
|
(isset($_GET['mailpoet_form']))
|
||||||
// // success message
|
&&
|
||||||
// if(isset($_GET['mailpoet_success'])) {
|
((int)$_GET['mailpoet_form'] === (int)$form['id'])
|
||||||
// $output .= '<p class="mailpoet_validate_success">'.strip_tags(urldecode($_GET['mailpoet_success']), '<a><strong><em><br><p>').'</p>';
|
);
|
||||||
// }
|
|
||||||
// // error message
|
// generate security token
|
||||||
// if(isset($_GET['mailpoet_error'])) {
|
$data['token'] = Security::generateToken();
|
||||||
// $output .= '<p class="mailpoet_validate_error">'.strip_tags(urldecode($_GET['mailpoet_error']), '<a><strong><em><br><p>').'</p>';
|
|
||||||
// }
|
|
||||||
// $output .= '</div>';
|
|
||||||
// } else {
|
|
||||||
// $output .= '<div class="mailpoet_message"></div>';
|
|
||||||
// }
|
|
||||||
|
|
||||||
// render form
|
// render form
|
||||||
$renderer = new Renderer();
|
$renderer = new Renderer();
|
||||||
|
@@ -9,18 +9,28 @@ class Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
|
// security token
|
||||||
add_action(
|
add_action(
|
||||||
'admin_head',
|
'admin_head',
|
||||||
array($this, 'setToken')
|
array($this, 'setToken')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Admin API (Ajax only)
|
||||||
add_action(
|
add_action(
|
||||||
'wp_ajax_mailpoet',
|
'wp_ajax_mailpoet',
|
||||||
array($this, 'setupAdmin')
|
array($this, 'setupAdmin')
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Public API (Ajax)
|
||||||
add_action(
|
add_action(
|
||||||
'wp_ajax_nopriv_mailpoet',
|
'wp_ajax_nopriv_mailpoet',
|
||||||
array($this, 'setupPublic')
|
array($this, 'setupPublic')
|
||||||
);
|
);
|
||||||
|
// Public API (Post)
|
||||||
|
add_action(
|
||||||
|
'admin_post_nopriv_mailpoet',
|
||||||
|
array($this, 'setupPublic')
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function setupAdmin() {
|
function setupAdmin() {
|
||||||
@@ -38,7 +48,23 @@ class Router {
|
|||||||
$class = ucfirst($_POST['endpoint']);
|
$class = ucfirst($_POST['endpoint']);
|
||||||
$endpoint = __NAMESPACE__ . "\\" . $class;
|
$endpoint = __NAMESPACE__ . "\\" . $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;
|
||||||
|
}
|
||||||
|
|
||||||
|
// filter out reserved keywords from data
|
||||||
|
$reserved_keywords = array(
|
||||||
|
'token',
|
||||||
|
'endpoint',
|
||||||
|
'method',
|
||||||
|
'mailpoet_redirect'
|
||||||
|
);
|
||||||
|
$data = array_diff_key($data, array_flip($reserved_keywords));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$endpoint = new $endpoint();
|
$endpoint = new $endpoint();
|
||||||
@@ -62,6 +88,12 @@ class Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function verifyToken() {
|
function verifyToken() {
|
||||||
if(!wp_verify_nonce($_POST['token'], 'mailpoet_token')) { die(); }
|
if(
|
||||||
|
empty($_POST['token'])
|
||||||
|
||
|
||||||
|
!wp_verify_nonce($_POST['token'], 'mailpoet_token')
|
||||||
|
) {
|
||||||
|
die();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,6 +8,7 @@ use MailPoet\Models\SubscriberCustomField;
|
|||||||
use MailPoet\Models\Segment;
|
use MailPoet\Models\Segment;
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Models\Form;
|
use MailPoet\Models\Form;
|
||||||
|
use MailPoet\Util\Url;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
@@ -88,41 +89,17 @@ class Subscribers {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
$subscriber = Subscriber::subscribe($data, $segment_ids);
|
||||||
if($subscriber->getErrors() !== false) {
|
$errors = $subscriber->getErrors();
|
||||||
return array(
|
$result = ($errors === false && $subscriber->id() > 0);
|
||||||
'result' => false,
|
|
||||||
'errors' => $subscriber->getErrors()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// get success message to display after subscription
|
// get success message to display after subscription
|
||||||
$form_settings = (
|
$form_settings = (
|
||||||
isset($form->settings)
|
isset($form->settings)
|
||||||
? unserialize($form->settings) : null
|
? unserialize($form->settings)
|
||||||
|
: null
|
||||||
);
|
);
|
||||||
|
|
||||||
if($form_settings !== null) {
|
if($form_settings !== null) {
|
||||||
$message = $form_settings['success_message'];
|
|
||||||
|
|
||||||
// url params for non ajax requests
|
|
||||||
if($doing_ajax === false) {
|
|
||||||
// get referer
|
|
||||||
$referer = (wp_get_referer() !== false)
|
|
||||||
? wp_get_referer() : $_SERVER['HTTP_REFERER'];
|
|
||||||
|
|
||||||
// redirection parameters
|
|
||||||
$params = array(
|
|
||||||
'mailpoet_form' => $form->id()
|
|
||||||
);
|
|
||||||
|
|
||||||
// handle success/error messages
|
|
||||||
if($result === false) {
|
|
||||||
$params['mailpoet_error'] = urlencode($message);
|
|
||||||
} else {
|
|
||||||
$params['mailpoet_success'] = urlencode($message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch($form_settings['on_success']) {
|
switch($form_settings['on_success']) {
|
||||||
case 'page':
|
case 'page':
|
||||||
// response depending on context
|
// response depending on context
|
||||||
@@ -130,11 +107,15 @@ class Subscribers {
|
|||||||
return array(
|
return array(
|
||||||
'result' => $result,
|
'result' => $result,
|
||||||
'page' => get_permalink($form_settings['success_page']),
|
'page' => get_permalink($form_settings['success_page']),
|
||||||
'message' => $message
|
'errors' => $errors
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
$redirect_to = ($result === false) ? $referer : get_permalink($form_settings['success_page']);
|
// handle success/error messages
|
||||||
wp_redirect(add_query_arg($params, $redirect_to));
|
if($result === false) {
|
||||||
|
Url::redirectBack();
|
||||||
|
} else {
|
||||||
|
Url::redirectTo(get_permalink($form_settings['success_page']));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
@@ -144,11 +125,10 @@ class Subscribers {
|
|||||||
if($doing_ajax === true) {
|
if($doing_ajax === true) {
|
||||||
return array(
|
return array(
|
||||||
'result' => true,
|
'result' => true,
|
||||||
'message' => $message
|
'errors' => $errors
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// redirect to previous page
|
Url::redirectBack();
|
||||||
wp_redirect(add_query_arg($params, $referer));
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Subscription;
|
namespace MailPoet\Subscription;
|
||||||
use \MailPoet\Models\Setting;
|
|
||||||
use \MailPoet\Models\Subscriber;
|
use \MailPoet\Models\Subscriber;
|
||||||
use \MailPoet\Models\SubscriberSegment;
|
|
||||||
use \MailPoet\Util\Url;
|
use \MailPoet\Util\Url;
|
||||||
|
|
||||||
class Manage {
|
class Manage {
|
||||||
|
@@ -14,28 +14,24 @@ class Url {
|
|||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
static function redirectBack() {
|
static function redirectBack($params = array()) {
|
||||||
// check mailpoet_redirect parameter
|
// check mailpoet_redirect parameter
|
||||||
$referer = (isset($_POST['mailpoet_redirect'])
|
$referer = (isset($_POST['mailpoet_redirect'])
|
||||||
? $_POST['mailpoet_redirect']
|
? $_POST['mailpoet_redirect']
|
||||||
: null
|
: wp_get_referer()
|
||||||
);
|
);
|
||||||
|
|
||||||
// fallback: http referer
|
|
||||||
if($referer === null) {
|
|
||||||
if(!empty($_SERVER['HTTP_REFERER'])) {
|
|
||||||
$referer = $_SERVER['HTTP_REFERER'];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// fallback: home_url
|
// fallback: home_url
|
||||||
if($referer === null) {
|
if(!$referer) {
|
||||||
$referer = home_url();
|
$referer = home_url();
|
||||||
}
|
}
|
||||||
|
|
||||||
if($referer !== null) {
|
// append extra params to url
|
||||||
self::redirectTo($referer);
|
if(!empty($params)) {
|
||||||
|
$referer = add_query_arg($params, $referer);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self::redirectTo($referer);
|
||||||
exit();
|
exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,13 +9,14 @@
|
|||||||
<%= 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_form_subscribe') | raw %>"
|
|
||||||
#>
|
|
||||||
class="mailpoet_form mailpoet_form_<%= form_type %>"
|
class="mailpoet_form mailpoet_form_<%= form_type %>"
|
||||||
novalidate
|
novalidate
|
||||||
>
|
>
|
||||||
<input type="hidden" name="form_id" value="<%= form.id %>" />
|
<input type="hidden" name="form_id" value="<%= form.id %>" />
|
||||||
|
<input type="hidden" name="token" value="<%= token %>" />
|
||||||
|
<input type="hidden" name="endpoint" value="subscribers" />
|
||||||
|
<input type="hidden" name="method" value="subscribe" />
|
||||||
|
|
||||||
<% if not(form.settings.segments_selected_by == 'user') %>
|
<% if not(form.settings.segments_selected_by == 'user') %>
|
||||||
<% for segment in form.settings.segments %>
|
<% for segment in form.settings.segments %>
|
||||||
@@ -23,8 +24,15 @@
|
|||||||
<% endfor %>
|
<% endfor %>
|
||||||
<% endif %>
|
<% endif %>
|
||||||
<%= html | raw %>
|
<%= html | raw %>
|
||||||
|
<div
|
||||||
|
class="mailpoet_message"
|
||||||
|
>
|
||||||
|
|
||||||
<div class="mailpoet_message"></div>
|
<p class="mailpoet_validate_success" style="display:none;">
|
||||||
|
<%= form.settings.success_message %>
|
||||||
|
</p>
|
||||||
|
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
<%= after_widget | raw %>
|
<%= after_widget | raw %>
|
||||||
|
Reference in New Issue
Block a user