diff --git a/assets/js/src/forms/list.jsx b/assets/js/src/forms/list.jsx index 0fabf70d73..fa68480414 100644 --- a/assets/js/src/forms/list.jsx +++ b/assets/js/src/forms/list.jsx @@ -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 (
diff --git a/assets/js/src/public.js b/assets/js/src/public.js index c998fac195..defbf2e02f 100644 --- a/assets/js/src/public.js +++ b/assets/js/src/public.js @@ -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('
') + ).show(); }).done(function(response) { - if(response.result === false) { - form.find('.mailpoet_validate_error').html( - response.errors.join('
') - ).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); } }); diff --git a/lib/API/API.php b/lib/API/API.php index 834206caec..65ac28213f 100644 --- a/lib/API/API.php +++ b/lib/API/API.php @@ -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(); + } } } diff --git a/lib/API/Endpoints/CustomFields.php b/lib/API/Endpoints/CustomFields.php index 1dcfa908b4..6fd6c14dfc 100644 --- a/lib/API/Endpoints/CustomFields.php +++ b/lib/API/Endpoints/CustomFields.php @@ -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); diff --git a/lib/API/Endpoints/Forms.php b/lib/API/Endpoints/Forms.php index b552e25c8e..24f0835f3b 100644 --- a/lib/API/Endpoints/Forms.php +++ b/lib/API/Endpoints/Forms.php @@ -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'; } diff --git a/lib/API/Endpoints/Subscribers.php b/lib/API/Endpoints/Subscribers.php index 20d066600a..a8a86ce36d 100644 --- a/lib/API/Endpoints/Subscribers.php +++ b/lib/API/Endpoints/Subscribers.php @@ -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 + )); + } } } } diff --git a/lib/Form/Block/Checkbox.php b/lib/Form/Block/Checkbox.php index 37c77bff64..b767a21eda 100644 --- a/lib/Form/Block/Checkbox.php +++ b/lib/Form/Block/Checkbox.php @@ -22,7 +22,6 @@ class Checkbox extends Base { foreach($options as $option) { $html .= '
diff --git a/views/forms.html b/views/forms.html index 18a990185f..5320358b52 100644 --- a/views/forms.html +++ b/views/forms.html @@ -41,6 +41,7 @@ 'formName': __('Name'), 'segments': __('Lists'), + 'userChoice': __('User choice:'), 'signups': __('Signups'), 'createdOn': __('Created On'), 'oneFormTrashed': __('1 form was moved to the trash'),