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,29 +44,36 @@ function(
endpoint: 'subscribers', endpoint: 'subscribers',
action: 'subscribe', action: 'subscribe',
data: data 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) { }).done(function(response) {
if(response.result === false) { // successfully subscribed
form.find('.mailpoet_validate_error').html( if (
response.errors.join('<br />') response.meta !== undefined
).show(); && response.meta.redirect_url !== undefined
) {
// go to page
window.location.href = response.meta.redirect_url;
} else { } else {
// successfully subscribed // display success message
if(response.page !== undefined) { form.find('.mailpoet_validate_success').show();
// 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();
} }
// reset form
form.trigger('reset');
// reset validation
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,12 +104,16 @@ class API {
try { try {
$endpoint = new $endpoint(); $endpoint = new $endpoint();
$response = $endpoint->$method($data); $response = $endpoint->$method($data);
$response->send(); if($doing_ajax) {
$response->send();
}
} catch(\Exception $e) { } catch(\Exception $e) {
$error_response = new ErrorResponse( if($doing_ajax) {
array($e->getCode() => $e->getMessage()) $error_response = new ErrorResponse(
); array($e->getCode() => $e->getMessage())
$error_response->send(); );
$error_response->send();
}
} }
} }

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,71 +68,63 @@ 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);
// record form statistics if($errors !== false) {
if($result === true && $form !== false && $form->id > 0) { if($doing_ajax) {
StatisticsForms::record($form->id, $subscriber->id); 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 if($form !== false) {
$form_settings = ( // record form statistics
isset($form->settings) StatisticsForms::record($form->id, $subscriber->id);
? unserialize($form->settings)
: null
);
if($form_settings !== null) { $form = $form->asArray();
switch($form_settings['on_success']) {
case 'page':
$success_page_url = get_permalink($form_settings['success_page']);
// response depending on context if($form['settings']['on_success'] === 'page') {
if($doing_ajax === true) { // redirect to a page on a success, pass the page url in the meta
return array( $meta['redirect_url'] = get_permalink($form['settings']['success_page']);
'result' => $result, } else if($form['settings']['on_success'] === 'url') {
'page' => $success_page_url, $meta['redirect_url'] = $form['settings']['success_url'];
'errors' => $errors }
); }
} else {
if($result === false) {
Url::redirectBack();
} else {
Url::redirectTo($success_page_url);
}
}
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); if($doing_ajax) {
} return $this->successResponse(
break; 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
));
}
} }
} }
} }

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,72 +73,69 @@ 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();
$segment_names = array_map(function($segment) { $segment_names = array_map(function($segment) {
return $segment->name; return $segment->name;
}, $segments); }, $segments);
$body = nl2br($signup_confirmation['body']); $body = nl2br($signup_confirmation['body']);
// replace list of segments shortcode // replace list of segments shortcode
$body = str_replace( $body = str_replace(
'[lists_to_confirm]', '[lists_to_confirm]',
'<strong>'.join(', ', $segment_names).'</strong>', '<strong>'.join(', ', $segment_names).'</strong>',
$body $body
); );
// replace activation link // replace activation link
$body = str_replace( $body = str_replace(
array( array(
'[activation_link]', '[activation_link]',
'[/activation_link]' '[/activation_link]'
), ),
array( array(
'<a href="'.esc_attr(Subscription\Url::getConfirmationUrl($this)).'">', '<a href="'.esc_attr(Subscription\Url::getConfirmationUrl($this)).'">',
'</a>' '</a>'
), ),
$body $body
); );
// build email data // build email data
$email = array( $email = array(
'subject' => $signup_confirmation['subject'], 'subject' => $signup_confirmation['subject'],
'body' => array( 'body' => array(
'html' => $body, 'html' => $body,
'text' => $body 'text' => $body
) )
); );
// convert subscriber to array // convert subscriber to array
$subscriber = $this->asArray(); $subscriber = $this->asArray();
// set from // set from
$from = ( $from = (
!empty($signup_confirmation['from']) !empty($signup_confirmation['from'])
&& !empty($signup_confirmation['from']['address']) && !empty($signup_confirmation['from']['address'])
) ? $signup_confirmation['from'] ) ? $signup_confirmation['from']
: false; : false;
// set reply to // set reply to
$reply_to = ( $reply_to = (
!empty($signup_confirmation['reply_to']) !empty($signup_confirmation['reply_to'])
&& !empty($signup_confirmation['reply_to']['address']) && !empty($signup_confirmation['reply_to']['address'])
) ? $signup_confirmation['reply_to'] ) ? $signup_confirmation['reply_to']
: false; : false;
// send email // send email
try { try {
$mailer = new Mailer(false, $from, $reply_to); $mailer = new Mailer(false, $from, $reply_to);
return $mailer->send($email, $subscriber); return $mailer->send($email, $subscriber);
} catch(\Exception $e) { } catch(\Exception $e) {
$this->setError($e->getMessage()); $this->setError($e->getMessage());
return false; return false;
}
} }
return false;
} }
static function generateToken($email = null) { static function generateToken($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::createOrUpdate($subscriber_data); $subscriber = self::findOne($subscriber_data['email']);
$errors = $subscriber->getErrors();
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); $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'),