Closes issue 480
This commit is contained in:
@ -151,6 +151,10 @@ const FormList = React.createClass({
|
||||
return segment.name;
|
||||
}).join(', ');
|
||||
|
||||
if (form.settings.segments_selected_by === 'user') {
|
||||
segments = MailPoet.I18n.t('userChoice') + ' ' + segments;
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<td className={ row_classes }>
|
||||
|
@ -44,29 +44,36 @@ function(
|
||||
endpoint: 'subscribers',
|
||||
action: 'subscribe',
|
||||
data: data
|
||||
}).fail(function(response) {
|
||||
form.find('.mailpoet_validate_error').html(
|
||||
response.errors.map(function(error) {
|
||||
return error.message;
|
||||
}).join('<br />')
|
||||
).show();
|
||||
}).done(function(response) {
|
||||
if(response.result === false) {
|
||||
form.find('.mailpoet_validate_error').html(
|
||||
response.errors.join('<br />')
|
||||
).show();
|
||||
// successfully subscribed
|
||||
if (
|
||||
response.meta !== undefined
|
||||
&& response.meta.redirect_url !== undefined
|
||||
) {
|
||||
// go to page
|
||||
window.location.href = response.meta.redirect_url;
|
||||
} else {
|
||||
// successfully subscribed
|
||||
if(response.page !== undefined) {
|
||||
// go to page
|
||||
window.location.href = response.page;
|
||||
} else {
|
||||
// display success message
|
||||
form.find('.mailpoet_validate_success').show();
|
||||
}
|
||||
|
||||
// reset form
|
||||
form.trigger('reset');
|
||||
// reset validation
|
||||
parsley.reset();
|
||||
// display success message
|
||||
form.find('.mailpoet_validate_success').show();
|
||||
}
|
||||
|
||||
// reset form
|
||||
form.trigger('reset');
|
||||
// reset validation
|
||||
parsley.reset();
|
||||
|
||||
// resize iframe
|
||||
if(window.frameElement !== null) {
|
||||
if (
|
||||
window.frameElement !== null
|
||||
&& MailPoet !== undefined
|
||||
&& MailPoet['Iframe']
|
||||
) {
|
||||
MailPoet.Iframe.autoSize(window.frameElement);
|
||||
}
|
||||
});
|
||||
|
@ -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
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -22,7 +22,6 @@ class Checkbox extends Base {
|
||||
|
||||
foreach($options as $option) {
|
||||
$html .= '<label class="mailpoet_checkbox_label">';
|
||||
$html .= '<input type="hidden" name="'.$field_name.'" value="0" />';
|
||||
$html .= '<input type="checkbox" class="mailpoet_checkbox" ';
|
||||
|
||||
$html .= 'name="'.$field_name.'" ';
|
||||
|
@ -24,13 +24,14 @@ class Export {
|
||||
return join(' ', array(
|
||||
'<iframe',
|
||||
'width="100%"',
|
||||
'height="100%"',
|
||||
'scrolling="no"',
|
||||
'frameborder="0"',
|
||||
'src="'.$iframe_url.'"',
|
||||
'class="mailpoet_form_iframe"',
|
||||
'vspace="0"',
|
||||
'tabindex="0"',
|
||||
'onload="MailPoet.Iframe.autoSize(this);"',
|
||||
'onload="if(window[\'MailPoet\']) MailPoet.Iframe.autoSize(this);"',
|
||||
'marginwidth="0"',
|
||||
'marginheight="0"',
|
||||
'hspace="0"',
|
||||
|
@ -8,6 +8,7 @@ use \MailPoet\Models\Subscriber;
|
||||
use \MailPoet\Form\Renderer as FormRenderer;
|
||||
use \MailPoet\Form\Util;
|
||||
use \MailPoet\Util\Security;
|
||||
use \MailPoet\Util\Url;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
@ -148,12 +149,17 @@ class Widget extends \WP_Widget {
|
||||
'after_title' => (!empty($after_title) ? $after_title : '')
|
||||
);
|
||||
|
||||
// check if the form was successfully submitted via POST (non ajax)
|
||||
// (POST) non ajax success/error variables
|
||||
$data['success'] = (
|
||||
(isset($_GET['mailpoet_success']))
|
||||
&&
|
||||
((int)$_GET['mailpoet_success'] === (int)$form['id'])
|
||||
);
|
||||
$data['error'] = (
|
||||
(isset($_GET['mailpoet_error']))
|
||||
&&
|
||||
((int)$_GET['mailpoet_error'] === (int)$form['id'])
|
||||
);
|
||||
|
||||
// generate security token
|
||||
$data['token'] = Security::generateToken();
|
||||
|
@ -73,72 +73,69 @@ class Subscriber extends Model {
|
||||
}
|
||||
|
||||
function sendConfirmationEmail() {
|
||||
if($this->status === self::STATUS_UNCONFIRMED) {
|
||||
$signup_confirmation = Setting::getValue('signup_confirmation');
|
||||
$signup_confirmation = Setting::getValue('signup_confirmation');
|
||||
|
||||
$segments = $this->segments()->findMany();
|
||||
$segment_names = array_map(function($segment) {
|
||||
return $segment->name;
|
||||
}, $segments);
|
||||
$segments = $this->segments()->findMany();
|
||||
$segment_names = array_map(function($segment) {
|
||||
return $segment->name;
|
||||
}, $segments);
|
||||
|
||||
$body = nl2br($signup_confirmation['body']);
|
||||
$body = nl2br($signup_confirmation['body']);
|
||||
|
||||
// replace list of segments shortcode
|
||||
$body = str_replace(
|
||||
'[lists_to_confirm]',
|
||||
'<strong>'.join(', ', $segment_names).'</strong>',
|
||||
$body
|
||||
);
|
||||
// replace list of segments shortcode
|
||||
$body = str_replace(
|
||||
'[lists_to_confirm]',
|
||||
'<strong>'.join(', ', $segment_names).'</strong>',
|
||||
$body
|
||||
);
|
||||
|
||||
// replace activation link
|
||||
$body = str_replace(
|
||||
array(
|
||||
'[activation_link]',
|
||||
'[/activation_link]'
|
||||
),
|
||||
array(
|
||||
'<a href="'.esc_attr(Subscription\Url::getConfirmationUrl($this)).'">',
|
||||
'</a>'
|
||||
),
|
||||
$body
|
||||
);
|
||||
// replace activation link
|
||||
$body = str_replace(
|
||||
array(
|
||||
'[activation_link]',
|
||||
'[/activation_link]'
|
||||
),
|
||||
array(
|
||||
'<a href="'.esc_attr(Subscription\Url::getConfirmationUrl($this)).'">',
|
||||
'</a>'
|
||||
),
|
||||
$body
|
||||
);
|
||||
|
||||
// build email data
|
||||
$email = array(
|
||||
'subject' => $signup_confirmation['subject'],
|
||||
'body' => array(
|
||||
'html' => $body,
|
||||
'text' => $body
|
||||
)
|
||||
);
|
||||
// build email data
|
||||
$email = array(
|
||||
'subject' => $signup_confirmation['subject'],
|
||||
'body' => array(
|
||||
'html' => $body,
|
||||
'text' => $body
|
||||
)
|
||||
);
|
||||
|
||||
// convert subscriber to array
|
||||
$subscriber = $this->asArray();
|
||||
// convert subscriber to array
|
||||
$subscriber = $this->asArray();
|
||||
|
||||
// set from
|
||||
$from = (
|
||||
!empty($signup_confirmation['from'])
|
||||
&& !empty($signup_confirmation['from']['address'])
|
||||
) ? $signup_confirmation['from']
|
||||
: false;
|
||||
// set from
|
||||
$from = (
|
||||
!empty($signup_confirmation['from'])
|
||||
&& !empty($signup_confirmation['from']['address'])
|
||||
) ? $signup_confirmation['from']
|
||||
: false;
|
||||
|
||||
// set reply to
|
||||
$reply_to = (
|
||||
!empty($signup_confirmation['reply_to'])
|
||||
&& !empty($signup_confirmation['reply_to']['address'])
|
||||
) ? $signup_confirmation['reply_to']
|
||||
: false;
|
||||
// set reply to
|
||||
$reply_to = (
|
||||
!empty($signup_confirmation['reply_to'])
|
||||
&& !empty($signup_confirmation['reply_to']['address'])
|
||||
) ? $signup_confirmation['reply_to']
|
||||
: false;
|
||||
|
||||
// send email
|
||||
try {
|
||||
$mailer = new Mailer(false, $from, $reply_to);
|
||||
return $mailer->send($email, $subscriber);
|
||||
} catch(\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
// send email
|
||||
try {
|
||||
$mailer = new Mailer(false, $from, $reply_to);
|
||||
return $mailer->send($email, $subscriber);
|
||||
} catch(\Exception $e) {
|
||||
$this->setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static function generateToken($email = null) {
|
||||
@ -153,27 +150,30 @@ class Subscriber extends Model {
|
||||
}
|
||||
|
||||
static function subscribe($subscriber_data = array(), $segment_ids = array()) {
|
||||
if(empty($subscriber_data) or empty($segment_ids)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$signup_confirmation_enabled = (bool)Setting::getValue(
|
||||
'signup_confirmation.enabled'
|
||||
);
|
||||
|
||||
$subscriber = self::createOrUpdate($subscriber_data);
|
||||
$errors = $subscriber->getErrors();
|
||||
$subscriber = self::findOne($subscriber_data['email']);
|
||||
|
||||
if($subscriber === false) {
|
||||
// create new subscriber
|
||||
$subscriber = self::createOrUpdate($subscriber_data);
|
||||
if($subscriber->getErrors() !== false) {
|
||||
return $subscriber;
|
||||
}
|
||||
|
||||
if($errors === false && $subscriber->id > 0) {
|
||||
$subscriber = self::findOne($subscriber->id);
|
||||
}
|
||||
|
||||
// restore deleted subscriber
|
||||
if($subscriber !== false) {
|
||||
// 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) {
|
||||
// auto subscribe when signup confirmation is disabled
|
||||
if($signup_confirmation_enabled === true) {
|
||||
$subscriber->set('status', self::STATUS_UNCONFIRMED);
|
||||
} else {
|
||||
@ -186,9 +186,7 @@ class Subscriber extends Model {
|
||||
SubscriberSegment::subscribeToSegments($subscriber, $segment_ids);
|
||||
|
||||
// signup confirmation
|
||||
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
|
||||
$subscriber->sendConfirmationEmail();
|
||||
}
|
||||
$subscriber->sendConfirmationEmail();
|
||||
|
||||
// welcome email
|
||||
Scheduler::scheduleSubscriberWelcomeNotification(
|
||||
|
@ -274,18 +274,17 @@
|
||||
// form editor: default fields
|
||||
var template = Handlebars.compile($('#form_template_fields').html());
|
||||
|
||||
var data = {
|
||||
fields: mailpoet_default_fields
|
||||
};
|
||||
|
||||
return MailPoet.Ajax.post({
|
||||
endpoint: 'customFields',
|
||||
action: 'getAll',
|
||||
}).done(function(response) {
|
||||
data.fields = $.merge(response.data, data.fields);
|
||||
|
||||
// render toolbar
|
||||
jQuery('#mailpoet_toolbar_fields').html(template(data));
|
||||
jQuery('#mailpoet_toolbar_fields').html(template({
|
||||
fields: $.merge(
|
||||
$.merge([], mailpoet_default_fields),
|
||||
response.data
|
||||
)
|
||||
}));
|
||||
|
||||
setTimeout(function() {
|
||||
WysijaForm.init();
|
||||
|
@ -20,25 +20,6 @@
|
||||
<%= form | raw %>
|
||||
<script type="text/javascript">
|
||||
var MailPoetForm = <%= json_encode(mailpoet_form) %>;
|
||||
function autoSize() {
|
||||
var iframe = window.frameElement;
|
||||
var height = document.body.scrollHeight;
|
||||
parent.MailPoet.Iframe.setSize(iframe, height);
|
||||
}
|
||||
|
||||
jQuery(function($) {
|
||||
$(function() {
|
||||
autoSize();
|
||||
});
|
||||
});
|
||||
|
||||
jQuery('form').on('submit', function() {
|
||||
setTimeout(function() {
|
||||
// make sure we resize the iframe
|
||||
autoSize();
|
||||
}.bind(this), 1);
|
||||
return true;
|
||||
}.bind(this));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
@ -30,11 +30,14 @@
|
||||
<% if not(success) %>
|
||||
style="display:none;"
|
||||
<% endif %>
|
||||
>
|
||||
<%= form.settings.success_message %>
|
||||
</p>
|
||||
<p class="mailpoet_validate_error" style="display:none;">
|
||||
<!-- errors will be displayed here -->
|
||||
><%= form.settings.success_message %></p>
|
||||
<p class="mailpoet_validate_error"
|
||||
<% if not(error) %>
|
||||
style="display:none;"
|
||||
<% endif %>
|
||||
><% if (error) %>
|
||||
<%= __("An error occurred, make sure you have filled all the required fields.") %>
|
||||
<% endif %>
|
||||
</p>
|
||||
</div>
|
||||
</form>
|
||||
|
@ -41,6 +41,7 @@
|
||||
|
||||
'formName': __('Name'),
|
||||
'segments': __('Lists'),
|
||||
'userChoice': __('User choice:'),
|
||||
'signups': __('Signups'),
|
||||
'createdOn': __('Created On'),
|
||||
'oneFormTrashed': __('1 form was moved to the trash'),
|
||||
|
Reference in New Issue
Block a user