Closes issue 480

This commit is contained in:
Jonathan Labreuille
2016-09-21 11:53:27 +02:00
parent 0ba48234de
commit 6091751a4b
14 changed files with 185 additions and 192 deletions

View File

@ -151,6 +151,10 @@ const FormList = React.createClass({
return segment.name; return segment.name;
}).join(', '); }).join(', ');
if (form.settings.segments_selected_by === 'user') {
segments = MailPoet.I18n.t('userChoice') + ' ' + segments;
}
return ( return (
<div> <div>
<td className={ row_classes }> <td className={ row_classes }>

View File

@ -44,16 +44,20 @@ function(
endpoint: 'subscribers', endpoint: 'subscribers',
action: 'subscribe', action: 'subscribe',
data: data data: data
}).done(function(response) { }).fail(function(response) {
if(response.result === false) {
form.find('.mailpoet_validate_error').html( form.find('.mailpoet_validate_error').html(
response.errors.join('<br />') response.errors.map(function(error) {
return error.message;
}).join('<br />')
).show(); ).show();
} else { }).done(function(response) {
// successfully subscribed // successfully subscribed
if(response.page !== undefined) { if (
response.meta !== undefined
&& response.meta.redirect_url !== undefined
) {
// go to page // go to page
window.location.href = response.page; window.location.href = response.meta.redirect_url;
} else { } else {
// display success message // display success message
form.find('.mailpoet_validate_success').show(); form.find('.mailpoet_validate_success').show();
@ -63,10 +67,13 @@ function(
form.trigger('reset'); form.trigger('reset');
// reset validation // reset validation
parsley.reset(); parsley.reset();
}
// resize iframe // resize iframe
if(window.frameElement !== null) { if (
window.frameElement !== null
&& MailPoet !== undefined
&& MailPoet['Iframe']
) {
MailPoet.Iframe.autoSize(window.frameElement); MailPoet.Iframe.autoSize(window.frameElement);
} }
}); });

View File

@ -6,9 +6,6 @@ use \MailPoet\Util\Security;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
class API { class API {
function __construct() {
}
function init() { function init() {
// security token // security token
add_action( add_action(
@ -27,7 +24,12 @@ class API {
'wp_ajax_nopriv_mailpoet', 'wp_ajax_nopriv_mailpoet',
array($this, 'setupPublic') array($this, 'setupPublic')
); );
// Public API (Post) // Public API (Post)
add_action(
'admin_post_mailpoet',
array($this, 'setupPublic')
);
add_action( add_action(
'admin_post_nopriv_mailpoet', 'admin_post_nopriv_mailpoet',
array($this, 'setupPublic') array($this, 'setupPublic')
@ -102,14 +104,18 @@ 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();
} }
} }
}
function setToken() { function setToken() {
$global = '<script type="text/javascript">'; $global = '<script type="text/javascript">';

View File

@ -8,7 +8,7 @@ if(!defined('ABSPATH')) exit;
class CustomFields extends APIEndpoint { class CustomFields extends APIEndpoint {
function getAll() { function getAll() {
$collection = CustomField::findMany(); $collection = CustomField::orderByAsc('created_at')->findMany();
$custom_fields = array_map(function($custom_field) { $custom_fields = array_map(function($custom_field) {
return $custom_field->asArray(); return $custom_field->asArray();
}, $collection); }, $collection);

View File

@ -156,7 +156,6 @@ class Forms extends APIEndpoint {
// check if the user gets to pick his own lists // check if the user gets to pick his own lists
// or if it's selected by the admin // or if it's selected by the admin
$has_segment_selection = false; $has_segment_selection = false;
foreach($body as $i => $block) { foreach($body as $i => $block) {
if($block['type'] === 'segment') { if($block['type'] === 'segment') {
$has_segment_selection = true; $has_segment_selection = true;
@ -164,7 +163,7 @@ class Forms extends APIEndpoint {
$list_selection = array_filter( $list_selection = array_filter(
array_map(function($segment) { array_map(function($segment) {
return (isset($segment['id']) return (isset($segment['id'])
? (int)$segment['id'] ? $segment['id']
: null : null
); );
}, $block['params']['values']) }, $block['params']['values'])
@ -177,6 +176,7 @@ class Forms extends APIEndpoint {
// check list selection // check list selection
if($has_segment_selection === true) { if($has_segment_selection === true) {
$settings['segments_selected_by'] = 'user'; $settings['segments_selected_by'] = 'user';
$settings['segments'] = $list_selection;
} else { } else {
$settings['segments_selected_by'] = 'admin'; $settings['segments_selected_by'] = 'admin';
} }

View File

@ -57,13 +57,9 @@ class Subscribers extends APIEndpoint {
function subscribe($data = array()) { function subscribe($data = array()) {
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX); $doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
$errors = array(); $form_id = (isset($data['form_id']) ? (int)$data['form_id'] : false);
$form = Form::findOne($form_id);
$form = Form::findOne($data['form_id']);
unset($data['form_id']); unset($data['form_id']);
if($form === false || !$form->id()) {
$errors[] = __('This form does not exist');
}
$segment_ids = (!empty($data['segments']) $segment_ids = (!empty($data['segments'])
? (array)$data['segments'] ? (array)$data['segments']
@ -72,72 +68,64 @@ class Subscribers extends APIEndpoint {
unset($data['segments']); unset($data['segments']);
if(empty($segment_ids)) { if(empty($segment_ids)) {
$errors[] = __('Please select a list'); if($doing_ajax) {
} return $this->badRequest(array(
APIError::BAD_REQUEST => __('Please select a list')
if(!empty($errors)) { ));
return array( } else {
'result' => false, Url::redirectBack(array(
'errors' => $errors '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();
$result = ($errors === false && $subscriber->id() > 0);
if($errors !== false) {
if($doing_ajax) {
return $this->badRequest($errors);
} else {
Url::redirectBack(array(
'mailpoet_error' => $form_id,
'mailpoet_success' => null
));
}
} else {
$meta = array();
if($form !== false) {
// record form statistics // record form statistics
if($result === true && $form !== false && $form->id > 0) {
StatisticsForms::record($form->id, $subscriber->id); 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'];
}
} }
// get success message to display after subscription
$form_settings = (
isset($form->settings)
? unserialize($form->settings)
: null
);
if($form_settings !== null) { if($doing_ajax) {
switch($form_settings['on_success']) { return $this->successResponse(
case 'page': Subscriber::findOne($subscriber->id)->asArray(),
$success_page_url = get_permalink($form_settings['success_page']); $meta
// response depending on context
if($doing_ajax === true) {
return array(
'result' => $result,
'page' => $success_page_url,
'errors' => $errors
); );
} else { } else {
if($result === false) { if(isset($meta['redirect_url'])) {
Url::redirectBack(); Url::redirectTo($meta['redirect_url']);
} else { } else {
Url::redirectTo($success_page_url); Url::redirectBack(array(
'mailpoet_success' => $form['id'],
'mailpoet_error' => null
));
} }
} }
break;
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;
}
} }
} }

View File

@ -22,7 +22,6 @@ class Checkbox extends Base {
foreach($options as $option) { foreach($options as $option) {
$html .= '<label class="mailpoet_checkbox_label">'; $html .= '<label class="mailpoet_checkbox_label">';
$html .= '<input type="hidden" name="'.$field_name.'" value="0" />';
$html .= '<input type="checkbox" class="mailpoet_checkbox" '; $html .= '<input type="checkbox" class="mailpoet_checkbox" ';
$html .= 'name="'.$field_name.'" '; $html .= 'name="'.$field_name.'" ';

View File

@ -24,13 +24,14 @@ class Export {
return join(' ', array( return join(' ', array(
'<iframe', '<iframe',
'width="100%"', 'width="100%"',
'height="100%"',
'scrolling="no"', 'scrolling="no"',
'frameborder="0"', 'frameborder="0"',
'src="'.$iframe_url.'"', 'src="'.$iframe_url.'"',
'class="mailpoet_form_iframe"', 'class="mailpoet_form_iframe"',
'vspace="0"', 'vspace="0"',
'tabindex="0"', 'tabindex="0"',
'onload="MailPoet.Iframe.autoSize(this);"', 'onload="if(window[\'MailPoet\']) MailPoet.Iframe.autoSize(this);"',
'marginwidth="0"', 'marginwidth="0"',
'marginheight="0"', 'marginheight="0"',
'hspace="0"', 'hspace="0"',

View File

@ -8,6 +8,7 @@ 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; use \MailPoet\Util\Security;
use \MailPoet\Util\Url;
if(!defined('ABSPATH')) exit; if(!defined('ABSPATH')) exit;
@ -148,12 +149,17 @@ class Widget extends \WP_Widget {
'after_title' => (!empty($after_title) ? $after_title : '') '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'] = ( $data['success'] = (
(isset($_GET['mailpoet_success'])) (isset($_GET['mailpoet_success']))
&& &&
((int)$_GET['mailpoet_success'] === (int)$form['id']) ((int)$_GET['mailpoet_success'] === (int)$form['id'])
); );
$data['error'] = (
(isset($_GET['mailpoet_error']))
&&
((int)$_GET['mailpoet_error'] === (int)$form['id'])
);
// generate security token // generate security token
$data['token'] = Security::generateToken(); $data['token'] = Security::generateToken();

View File

@ -73,7 +73,6 @@ class Subscriber extends Model {
} }
function sendConfirmationEmail() { function sendConfirmationEmail() {
if($this->status === self::STATUS_UNCONFIRMED) {
$signup_confirmation = Setting::getValue('signup_confirmation'); $signup_confirmation = Setting::getValue('signup_confirmation');
$segments = $this->segments()->findMany(); $segments = $this->segments()->findMany();
@ -138,8 +137,6 @@ class Subscriber extends Model {
return false; return false;
} }
} }
return false;
}
static function generateToken($email = null) { static function generateToken($email = null) {
if($email !== null) { if($email !== null) {
@ -153,27 +150,30 @@ class Subscriber extends Model {
} }
static function subscribe($subscriber_data = array(), $segment_ids = array()) { 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 = (bool)Setting::getValue(
'signup_confirmation.enabled' 'signup_confirmation.enabled'
); );
$subscriber = self::findOne($subscriber_data['email']);
if($subscriber === false) {
// create new subscriber
$subscriber = self::createOrUpdate($subscriber_data); $subscriber = self::createOrUpdate($subscriber_data);
$errors = $subscriber->getErrors(); if($subscriber->getErrors() !== false) {
return $subscriber;
}
if($errors === false && $subscriber->id > 0) {
$subscriber = self::findOne($subscriber->id); $subscriber = self::findOne($subscriber->id);
}
// restore deleted subscriber if($subscriber !== false) {
// 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($subscriber->status !== self::STATUS_SUBSCRIBED) {
// auto subscribe when signup confirmation is disabled
if($signup_confirmation_enabled === true) { if($signup_confirmation_enabled === true) {
$subscriber->set('status', self::STATUS_UNCONFIRMED); $subscriber->set('status', self::STATUS_UNCONFIRMED);
} else { } else {
@ -186,9 +186,7 @@ class Subscriber extends Model {
SubscriberSegment::subscribeToSegments($subscriber, $segment_ids); SubscriberSegment::subscribeToSegments($subscriber, $segment_ids);
// signup confirmation // signup confirmation
if($subscriber->status !== self::STATUS_SUBSCRIBED) {
$subscriber->sendConfirmationEmail(); $subscriber->sendConfirmationEmail();
}
// welcome email // welcome email
Scheduler::scheduleSubscriberWelcomeNotification( Scheduler::scheduleSubscriberWelcomeNotification(

View File

@ -274,18 +274,17 @@
// form editor: default fields // form editor: default fields
var template = Handlebars.compile($('#form_template_fields').html()); var template = Handlebars.compile($('#form_template_fields').html());
var data = {
fields: mailpoet_default_fields
};
return MailPoet.Ajax.post({ return MailPoet.Ajax.post({
endpoint: 'customFields', endpoint: 'customFields',
action: 'getAll', action: 'getAll',
}).done(function(response) { }).done(function(response) {
data.fields = $.merge(response.data, data.fields);
// render toolbar // 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() { setTimeout(function() {
WysijaForm.init(); WysijaForm.init();

View File

@ -20,25 +20,6 @@
<%= form | raw %> <%= form | raw %>
<script type="text/javascript"> <script type="text/javascript">
var MailPoetForm = <%= json_encode(mailpoet_form) %>; 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> </script>
</body> </body>
</html> </html>

View File

@ -30,11 +30,14 @@
<% if not(success) %> <% if not(success) %>
style="display:none;" style="display:none;"
<% endif %> <% endif %>
> ><%= form.settings.success_message %></p>
<%= form.settings.success_message %> <p class="mailpoet_validate_error"
</p> <% if not(error) %>
<p class="mailpoet_validate_error" style="display:none;"> style="display:none;"
<!-- errors will be displayed here --> <% endif %>
><% if (error) %>
<%= __("An error occurred, make sure you have filled all the required fields.") %>
<% endif %>
</p> </p>
</div> </div>
</form> </form>

View File

@ -41,6 +41,7 @@
'formName': __('Name'), 'formName': __('Name'),
'segments': __('Lists'), 'segments': __('Lists'),
'userChoice': __('User choice:'),
'signups': __('Signups'), 'signups': __('Signups'),
'createdOn': __('Created On'), 'createdOn': __('Created On'),
'oneFormTrashed': __('1 form was moved to the trash'), 'oneFormTrashed': __('1 form was moved to the trash'),