Updates form subscription class to use the main API class instead of calling directly API endpoint
Modifies forms to pass api_version Modifies forms to pass store form-specific values (e.g., form_id, email) inside a separate data array
This commit is contained in:
@ -31,7 +31,7 @@ function(
|
|||||||
});
|
});
|
||||||
|
|
||||||
form.parsley().on('form:submit', function(parsley) {
|
form.parsley().on('form:submit', function(parsley) {
|
||||||
var data = form.serializeObject() || {};
|
var form_data = form.serializeObject() || {};
|
||||||
// check if we're on the same domain
|
// check if we're on the same domain
|
||||||
if(isSameDomain(MailPoetForm.ajax_url) === false) {
|
if(isSameDomain(MailPoetForm.ajax_url) === false) {
|
||||||
// non ajax post request
|
// non ajax post request
|
||||||
@ -40,10 +40,11 @@ function(
|
|||||||
// ajax request
|
// ajax request
|
||||||
MailPoet.Ajax.post({
|
MailPoet.Ajax.post({
|
||||||
url: MailPoetForm.ajax_url,
|
url: MailPoetForm.ajax_url,
|
||||||
token: data.token,
|
api_version: form_data.api_version,
|
||||||
|
token: form_data.token,
|
||||||
endpoint: 'subscribers',
|
endpoint: 'subscribers',
|
||||||
action: 'subscribe',
|
action: 'subscribe',
|
||||||
data: data
|
data: form_data.data
|
||||||
}).fail(function(response) {
|
}).fail(function(response) {
|
||||||
form.find('.mailpoet_validate_error').html(
|
form.find('.mailpoet_validate_error').html(
|
||||||
response.errors.map(function(error) {
|
response.errors.map(function(error) {
|
||||||
|
@ -19,7 +19,7 @@ class Text extends Base {
|
|||||||
|
|
||||||
$html .= '<input type="'.$type.'" class="mailpoet_text" ';
|
$html .= '<input type="'.$type.'" class="mailpoet_text" ';
|
||||||
|
|
||||||
$html .= 'name="'.static::getFieldName($block).'" ';
|
$html .= 'name="data['.static::getFieldName($block).']" ';
|
||||||
|
|
||||||
$html .= 'title="'.static::getFieldLabel($block).'" ';
|
$html .= 'title="'.static::getFieldLabel($block).'" ';
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Form;
|
namespace MailPoet\Form;
|
||||||
|
use MailPoet\API\API;
|
||||||
use MailPoet\Config\Renderer;
|
use MailPoet\Config\Renderer;
|
||||||
use MailPoet\Models\Form;
|
use MailPoet\Models\Form;
|
||||||
use MailPoet\Form\Renderer as FormRenderer;
|
use MailPoet\Form\Renderer as FormRenderer;
|
||||||
@ -159,6 +160,9 @@ class Widget extends \WP_Widget {
|
|||||||
// generate security token
|
// generate security token
|
||||||
$data['token'] = Security::generateToken();
|
$data['token'] = Security::generateToken();
|
||||||
|
|
||||||
|
// add API version
|
||||||
|
$data['api_version'] = API::CURRENT_VERSION;
|
||||||
|
|
||||||
// render form
|
// render form
|
||||||
$renderer = new Renderer();
|
$renderer = new Renderer();
|
||||||
try {
|
try {
|
||||||
|
@ -1,39 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace MailPoet\Subscription;
|
namespace MailPoet\Subscription;
|
||||||
|
|
||||||
|
use MailPoet\API\API;
|
||||||
use MailPoet\API\Endpoints\Subscribers;
|
use MailPoet\API\Endpoints\Subscribers;
|
||||||
use MailPoet\API\Response as APIResponse;
|
use MailPoet\API\Response as APIResponse;
|
||||||
use MailPoet\Util\Url;
|
use MailPoet\Util\Url;
|
||||||
|
|
||||||
class Form {
|
class Form {
|
||||||
static function onSubmit() {
|
static function onSubmit() {
|
||||||
$reserved_keywords = array(
|
$api = new API();
|
||||||
'token',
|
$api->getRequestData($_REQUEST);
|
||||||
'endpoint',
|
$form_id = (!empty($_REQUEST['data']['form_id'])) ? $_REQUEST['data']['form_id']: false;
|
||||||
'method',
|
$response = $api->processRoute();
|
||||||
'mailpoet_redirect'
|
|
||||||
);
|
|
||||||
|
|
||||||
$data = array_diff_key($_POST, array_flip($reserved_keywords));
|
|
||||||
$form_id = isset($data['form_id']) ? $data['form_id'] : 0;
|
|
||||||
|
|
||||||
$endpoint = new Subscribers();
|
|
||||||
|
|
||||||
$response = $endpoint->subscribe($data);
|
|
||||||
|
|
||||||
if($response->status !== APIResponse::STATUS_OK) {
|
if($response->status !== APIResponse::STATUS_OK) {
|
||||||
Url::redirectBack(array(
|
Url::redirectBack(array(
|
||||||
'mailpoet_error' => isset($data['form_id']) ? $data['form_id'] : true,
|
'mailpoet_error' => ($form_id) ? $form_id : true,
|
||||||
'mailpoet_success' => null
|
'mailpoet_success' => null
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
if(isset($response->meta['redirect_url'])) {
|
(isset($response->meta['redirect_url'])) ?
|
||||||
Url::redirectTo($response->meta['redirect_url']);
|
Url::redirectTo($response->meta['redirect_url']) :
|
||||||
} else {
|
Url::redirectBack(
|
||||||
Url::redirectBack(array(
|
array(
|
||||||
'mailpoet_success' => $form_id,
|
'mailpoet_success' => $form_id,
|
||||||
'mailpoet_error' => null
|
'mailpoet_error' => null
|
||||||
));
|
)
|
||||||
}
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -13,8 +13,9 @@
|
|||||||
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="data[form_id]" value="<%= form.id %>" />
|
||||||
<input type="hidden" name="token" value="<%= token %>" />
|
<input type="hidden" name="token" value="<%= token %>" />
|
||||||
|
<input type="hidden" name="api_version" value="<%= api_version %>" />
|
||||||
<input type="hidden" name="endpoint" value="subscribers" />
|
<input type="hidden" name="endpoint" value="subscribers" />
|
||||||
<input type="hidden" name="mailpoet_method" value="subscribe" />
|
<input type="hidden" name="mailpoet_method" value="subscribe" />
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user