fixed list selection widget + started form submission
This commit is contained in:
@ -296,7 +296,7 @@ var WysijaForm = {
|
|||||||
var settings_elements = $('mailpoet_form_settings').getElements();
|
var settings_elements = $('mailpoet_form_settings').getElements();
|
||||||
settings_elements.each(function(setting) {
|
settings_elements.each(function(setting) {
|
||||||
// skip lists
|
// skip lists
|
||||||
if(setting.name === 'lists') {
|
if(setting.name === 'segments') {
|
||||||
return true;
|
return true;
|
||||||
} else if(setting.name === 'on_success') {
|
} else if(setting.name === 'on_success') {
|
||||||
// if the input value is equal to the one stored in the settings
|
// if the input value is equal to the one stored in the settings
|
||||||
@ -402,7 +402,7 @@ var WysijaForm = {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// hide list selection if a list widget has been dragged into the editor
|
// hide list selection if a list widget has been dragged into the editor
|
||||||
$('mailpoet_settings_list_selection')[(($$('#' + WysijaForm.options.editor + ' [wysija_field="list"]').length > 0) === true) ? 'hide' : 'show']();
|
$('mailpoet_settings_segment_selection')[(($$('#' + WysijaForm.options.editor + ' [wysija_field="segment"]').length > 0) === true) ? 'hide' : 'show']();
|
||||||
},
|
},
|
||||||
setBlockPositions: function(event, target) {
|
setBlockPositions: function(event, target) {
|
||||||
// release dragging lock
|
// release dragging lock
|
||||||
@ -862,6 +862,18 @@ WysijaForm.Block.create = function(block, target) {
|
|||||||
template = Handlebars.compile($('form_template_' + block.type).innerHTML),
|
template = Handlebars.compile($('form_template_' + block.type).innerHTML),
|
||||||
output = '';
|
output = '';
|
||||||
|
|
||||||
|
if(block.type === 'segment') {
|
||||||
|
if(block.params.values === undefined) {
|
||||||
|
var settings_segments = jQuery('#mailpoet_form_segments').val();
|
||||||
|
if(settings_segments.length > 0){
|
||||||
|
mailpoet_segments.filter(function(segment) {
|
||||||
|
return (settings_segments.indexOf(segment.id) !== -1);
|
||||||
|
});
|
||||||
|
block.params.values = mailpoet_segments;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set block template (depending on the block type)
|
// set block template (depending on the block type)
|
||||||
block.template = template(block);
|
block.template = template(block);
|
||||||
output = block_template(block);
|
output = block_template(block);
|
||||||
@ -873,13 +885,14 @@ WysijaForm.Block.create = function(block, target) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if the drop target was the bottom placeholder
|
// if the drop target was the bottom placeholder
|
||||||
|
var element = null;
|
||||||
if(target.identify() === 'block_placeholder') {
|
if(target.identify() === 'block_placeholder') {
|
||||||
// insert block at the bottom
|
// insert block at the bottom
|
||||||
body.insert(output);
|
element = body.insert(output);
|
||||||
//block = body.childElements().last();
|
//block = body.childElements().last();
|
||||||
} else {
|
} else {
|
||||||
// insert block before the drop target
|
// insert block before the drop target
|
||||||
target.insert({
|
element = target.insert({
|
||||||
before: output
|
before: output
|
||||||
});
|
});
|
||||||
//block = target.previous('.mailpoet_form_block');
|
//block = target.previous('.mailpoet_form_block');
|
||||||
|
@ -67,7 +67,7 @@ class Migrator {
|
|||||||
$attributes = array(
|
$attributes = array(
|
||||||
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
'id mediumint(9) NOT NULL AUTO_INCREMENT,',
|
||||||
'name varchar(20) NOT NULL,',
|
'name varchar(20) NOT NULL,',
|
||||||
'value varchar(255) NOT NULL,',
|
'value longtext,',
|
||||||
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
'created_at TIMESTAMP NOT NULL DEFAULT 0,',
|
||||||
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
'updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,',
|
||||||
'PRIMARY KEY (id),',
|
'PRIMARY KEY (id),',
|
||||||
|
@ -13,7 +13,7 @@ class Widget {
|
|||||||
add_action('widgets_init', array($this, 'registerWidget'));
|
add_action('widgets_init', array($this, 'registerWidget'));
|
||||||
|
|
||||||
if(!is_admin()) {
|
if(!is_admin()) {
|
||||||
add_action('widgets_init', array($this, 'setupActions'));
|
//$this->setupActions();
|
||||||
add_action('widgets_init', array($this, 'setupDependencies'));
|
add_action('widgets_init', array($this, 'setupDependencies'));
|
||||||
} else {
|
} else {
|
||||||
add_action('widgets_init', array($this, 'setupAdminDependencies'));
|
add_action('widgets_init', array($this, 'setupAdminDependencies'));
|
||||||
@ -85,9 +85,9 @@ class Widget {
|
|||||||
'admin_post_mailpoet_form_subscribe',
|
'admin_post_mailpoet_form_subscribe',
|
||||||
'mailpoet_form_subscribe'
|
'mailpoet_form_subscribe'
|
||||||
);
|
);
|
||||||
/*add_action(
|
add_action(
|
||||||
'init',
|
'init',
|
||||||
'mailpoet_form_subscribe'
|
'mailpoet_form_subscribe'
|
||||||
);*/
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -22,8 +22,8 @@ class Segment extends Base {
|
|||||||
|
|
||||||
$html .= '<label class="mailpoet_checkbox_label">';
|
$html .= '<label class="mailpoet_checkbox_label">';
|
||||||
$html .= '<input type="checkbox" class="mailpoet_checkbox" ';
|
$html .= '<input type="checkbox" class="mailpoet_checkbox" ';
|
||||||
$html .= 'name="'.$field_name.'" ';
|
$html .= 'name="'.$field_name.'[]" ';
|
||||||
$html .= 'value="'.$segment['id'].'" '.$is_checked;
|
$html .= 'value="'.$segment['id'].'" '.$is_checked.' ';
|
||||||
$html .= $field_validation;
|
$html .= $field_validation;
|
||||||
$html .= ' />'.$segment['name'];
|
$html .= ' />'.$segment['name'];
|
||||||
$html .= '</label>';
|
$html .= '</label>';
|
||||||
|
@ -32,11 +32,16 @@ class Styles {
|
|||||||
.mailpoet_textarea,
|
.mailpoet_textarea,
|
||||||
.mailpoet_select,
|
.mailpoet_select,
|
||||||
.mailpoet_radio,
|
.mailpoet_radio,
|
||||||
.mailpoet_checkbox,
|
|
||||||
.mailpoet_date {
|
.mailpoet_date {
|
||||||
display:block;
|
display:block;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.mailpoet_checkbox {
|
||||||
|
display:inline;
|
||||||
|
margin-right: 5px;
|
||||||
|
vertical-align:middle;
|
||||||
|
}
|
||||||
|
|
||||||
.mailpoet_validate_success {
|
.mailpoet_validate_success {
|
||||||
color:#468847;
|
color:#468847;
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,31 @@
|
|||||||
namespace MailPoet\Form;
|
namespace MailPoet\Form;
|
||||||
use \MailPoet\Config\Renderer;
|
use \MailPoet\Config\Renderer;
|
||||||
use \MailPoet\Models\Form;
|
use \MailPoet\Models\Form;
|
||||||
|
use \MailPoet\Models\Segment;
|
||||||
|
use \MailPoet\Models\Setting;
|
||||||
use \MailPoet\Models\Subscriber;
|
use \MailPoet\Models\Subscriber;
|
||||||
use \MailPoet\Form\Renderer as FormRenderer;
|
use \MailPoet\Form\Renderer as FormRenderer;
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
class Widget extends \WP_Widget {
|
class Widget extends \WP_Widget {
|
||||||
|
|
||||||
function __construct () {
|
function __construct () {
|
||||||
|
add_action(
|
||||||
|
'wp_ajax_mailpoet_form_subscribe',
|
||||||
|
array($this, 'subscribe')
|
||||||
|
);
|
||||||
|
add_action(
|
||||||
|
'wp_ajax_nopriv_mailpoet_form_subscribe',
|
||||||
|
array($this, 'subscribe')
|
||||||
|
);
|
||||||
|
add_action(
|
||||||
|
'admin_post_nopriv_mailpoet_form_subscribe',
|
||||||
|
array($this, 'subscribe')
|
||||||
|
);
|
||||||
|
add_action(
|
||||||
|
'admin_post_mailpoet_form_subscribe',
|
||||||
|
array($this, 'subscribe')
|
||||||
|
);
|
||||||
return parent::__construct(
|
return parent::__construct(
|
||||||
'mailpoet_form',
|
'mailpoet_form',
|
||||||
__("MailPoet Subscription Form"),
|
__("MailPoet Subscription Form"),
|
||||||
@ -173,6 +190,249 @@ class Widget extends \WP_Widget {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function subscribe() {
|
||||||
|
// check to see if we're in an ajax request or post request
|
||||||
|
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
||||||
|
|
||||||
|
if(isset($_GET['action']) && $_GET['action'] === 'mailpoet_form_subscribe') {
|
||||||
|
// input data
|
||||||
|
$data = array();
|
||||||
|
|
||||||
|
// output errors
|
||||||
|
$errors = array();
|
||||||
|
|
||||||
|
// get posted data
|
||||||
|
// ajax data
|
||||||
|
$data = json_decode(file_get_contents('php://input'), true);
|
||||||
|
// -or- post data
|
||||||
|
if($data === NULL && !empty($_POST)) { $data = $_POST; }
|
||||||
|
|
||||||
|
// create or update subscriber
|
||||||
|
$subscriber = Subscriber::where('email', $data['email'])->findOne();
|
||||||
|
|
||||||
|
// is signup confirmation enabled?
|
||||||
|
$signup_confirmation = Setting::getValue('signup_confirmation');
|
||||||
|
if($subscriber === false) {
|
||||||
|
// create new subscriber
|
||||||
|
$data['status'] = (
|
||||||
|
($signup_confirmation['enabled'] === true)
|
||||||
|
? 'unconfirmed' : 'subscribed'
|
||||||
|
);
|
||||||
|
|
||||||
|
// // set custom fields
|
||||||
|
// $meta_fields = $mailpoet->getOption('mailpoet_subscriber_meta', array());
|
||||||
|
// if(!empty($meta_fields)) {
|
||||||
|
// // loop through data to see if any meta field has been passed
|
||||||
|
// foreach($meta_fields as $field => $field_data) {
|
||||||
|
// // check if it's a mandatory field
|
||||||
|
// $is_required = (isset($field_data['params']['required']) && (bool)$field_data['params']['required'] === true);
|
||||||
|
|
||||||
|
// if(array_key_exists($field, $data)) {
|
||||||
|
// // check if it's a mandatory field
|
||||||
|
// if($is_required === true && empty($data[$field])) {
|
||||||
|
// // if it's missing, throw an error
|
||||||
|
// $errors[] = sprintf(__('"%s" is required'), $field_data['name']);
|
||||||
|
// } else {
|
||||||
|
// // assign field to subscriber
|
||||||
|
// $subscriber[$field] = $data[$field];
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
if(empty($errors)) {
|
||||||
|
// insert new subscriber
|
||||||
|
$subscriber = Subscriber::createOrUpdate($data);
|
||||||
|
if($subscriber === false || !$subscriber->id()) {
|
||||||
|
$errors = array_merge($errors, $subscriber->getValidationErrors());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// restore deleted subscriber
|
||||||
|
if($subscriber->deleted_at !== NULL) {
|
||||||
|
// reset subscriber state (depends whether signup confirmation is enabled)
|
||||||
|
$subscriber
|
||||||
|
->set('status', array(
|
||||||
|
($signup_confirmation['enabled'] === true)
|
||||||
|
? 'unconfirmed' : 'subscribed'
|
||||||
|
))
|
||||||
|
->setExpr('deleted_at', 'NULL');
|
||||||
|
|
||||||
|
|
||||||
|
if(!$subscriber->save()) {
|
||||||
|
$errors[] = __('An error occurred. Please try again later.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if form id has been passed
|
||||||
|
if(isset($data['form']) && (int)$data['form'] > 0) {
|
||||||
|
// get form id
|
||||||
|
$form_id = (int)$data['form'];
|
||||||
|
// get form
|
||||||
|
$form = Form::findOne($form_id);
|
||||||
|
|
||||||
|
if($form === false || !$form->id()) {
|
||||||
|
$errors[] = __('This form does not exist. Please check your forms.');
|
||||||
|
} else {
|
||||||
|
// set subscriptions
|
||||||
|
if(empty($data['segments'])) {
|
||||||
|
$errors[] = __('You need to select a list');
|
||||||
|
} else {
|
||||||
|
// get segments
|
||||||
|
$segments = Segment::whereIn('id', $data['segments'])->findMany();
|
||||||
|
$segments_subscribed = array();
|
||||||
|
foreach($segments as $segment) {
|
||||||
|
if($segment->addSubscriber($subscriber->id())) {
|
||||||
|
$segments_subscribed[] = $segment->id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if signup confirmation is enabled and the subscriber is unconfirmed
|
||||||
|
if($signup_confirmation['enabled'] === true
|
||||||
|
&& !empty($segments_subscribed)
|
||||||
|
&& $subscriber->status !== 'subscribed'
|
||||||
|
) {
|
||||||
|
// TODO: send confirmation email
|
||||||
|
// resend confirmation email
|
||||||
|
$is_sent = static::sendSignupConfirmation(
|
||||||
|
$subscriber->asArray(),
|
||||||
|
$segments->asArray()
|
||||||
|
);
|
||||||
|
|
||||||
|
// error message if the email could not be sent
|
||||||
|
if($is_sent === false) {
|
||||||
|
$errors[] = __('The signup confirmation email could not be sent. Please check your settings.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// get success message to display after subscription
|
||||||
|
$form_settings = (
|
||||||
|
isset($form->settings)
|
||||||
|
? unserialize($form->settings) : null
|
||||||
|
);
|
||||||
|
|
||||||
|
if($subscriber !== null && empty($errors)) {
|
||||||
|
$success = true;
|
||||||
|
$message = $form_settings['success_message'];
|
||||||
|
} else {
|
||||||
|
$success = false;
|
||||||
|
$message = join('<br />', $errors);
|
||||||
|
}
|
||||||
|
|
||||||
|
if($form_settings !== null) {
|
||||||
|
|
||||||
|
// url params for non ajax requests
|
||||||
|
if($doing_ajax === false) {
|
||||||
|
// get referer
|
||||||
|
$referer = (wp_get_referer() !== false) ? wp_get_referer() : $_SERVER['HTTP_REFERER'];
|
||||||
|
|
||||||
|
// redirection parameters
|
||||||
|
$params = array(
|
||||||
|
'mailpoet_form' => (int)$data['form']
|
||||||
|
);
|
||||||
|
|
||||||
|
// handle success/error messages
|
||||||
|
if($success === false) {
|
||||||
|
$params['mailpoet_error'] = urlencode($message);
|
||||||
|
} else {
|
||||||
|
$params['mailpoet_success'] = urlencode($message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch ($form_settings['on_success']) {
|
||||||
|
case 'page':
|
||||||
|
// response depending on context
|
||||||
|
if($doing_ajax === true) {
|
||||||
|
echo json_encode(array(
|
||||||
|
'success' => $success,
|
||||||
|
'page' => get_permalink($form_settings['success_page']),
|
||||||
|
'message' => $message
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
$redirect_to = ($success === false) ? $referer : get_permalink($form_settings['success_page']);
|
||||||
|
wp_redirect(add_query_arg($params, $redirect_to));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'message':
|
||||||
|
default:
|
||||||
|
// response depending on context
|
||||||
|
if($doing_ajax === true) {
|
||||||
|
echo json_encode(array(
|
||||||
|
'success' => $success,
|
||||||
|
'message' => $message
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
// redirect to previous page
|
||||||
|
wp_redirect(add_query_arg($params, $referer));
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static function sendSignupConfirmation(array $subscriber, array $segments) {
|
||||||
|
print "<pre>";
|
||||||
|
print_r($subscriber);
|
||||||
|
print_r($segments);
|
||||||
|
print "</pre>";
|
||||||
|
//$mailer = new MailPoetMailer($mailpoet->settings()->getAll());
|
||||||
|
$signup_confirmation = Setting::getValue('signup_confirmation');
|
||||||
|
|
||||||
|
$body = (
|
||||||
|
!empty($signup_confirmation['body'])
|
||||||
|
? $signup_confirmation['body'] : ''
|
||||||
|
);
|
||||||
|
|
||||||
|
// check for lists_to_confirm tag
|
||||||
|
if(strpos($body, '[lists_to_confirm]') !== FALSE) {
|
||||||
|
// gather all names from lists
|
||||||
|
$segment_names = array_map(function($segment) { return $segment['list_name']; }, $segments);
|
||||||
|
// replace shortcode by list names in email's body
|
||||||
|
$body = str_replace('[lists_to_confirm]', join(', ', $segment_names), $body);
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for activation_link tags
|
||||||
|
if(strpos($body, '[activation_link]') !== FALSE && strpos($body, '[/activation_link]') !== FALSE) {
|
||||||
|
// get confirmation page id
|
||||||
|
$confirmation_page_id = $mailpoet->settings()->get('signup_confirmation_page');
|
||||||
|
|
||||||
|
// generate confirmation link
|
||||||
|
$confirmation_link = add_query_arg(array(
|
||||||
|
'mailpoet_key' => $subscriber['subscriber_digest']
|
||||||
|
), get_permalink($confirmation_page_id));
|
||||||
|
|
||||||
|
// we have both tags
|
||||||
|
$body = str_replace(
|
||||||
|
array('[activation_link]', '[/activation_link]'),
|
||||||
|
array('<a href="'.$confirmation_link.'">', '</a>'),
|
||||||
|
$body
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
// no activation link tags detected
|
||||||
|
// TODO...
|
||||||
|
}
|
||||||
|
|
||||||
|
// send confirmation email
|
||||||
|
return $mailer->send(array(
|
||||||
|
'from_email' => $mailpoet->settings()->get('signup_confirmation_from_email'),
|
||||||
|
'from_name' => $mailpoet->settings()->get('signup_confirmation_from_name'),
|
||||||
|
'reply_email' => $mailpoet->settings()->get('signup_confirmation_reply_email'),
|
||||||
|
'reply_name' => $mailpoet->settings()->get('signup_confirmation_reply_name'),
|
||||||
|
'subject' => $signup_confirmation['subject'],
|
||||||
|
'html' => nl2br($signup_confirmation['body']),
|
||||||
|
// 'text' => '',
|
||||||
|
'to_email' => $subscriber['subscriber_email'],
|
||||||
|
'to_name' => $subscriber['subscriber_email'],
|
||||||
|
));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// mailpoet shortcodes
|
// mailpoet shortcodes
|
||||||
@ -194,27 +454,23 @@ function mailpoet_form_shortcode($params = array()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
add_action('wp_ajax_mailpoet_form_subscribe', 'mailpoet_form_subscribe');
|
add_action(
|
||||||
add_action('wp_ajax_nopriv_mailpoet_form_subscribe', 'mailpoet_form_subscribe');
|
'init',
|
||||||
add_action('admin_post_nopriv_mailpoet_form_subscribe', 'mailpoet_form_subscribe');
|
array(__NAMESPACE__.'\Widget', 'subscribe')
|
||||||
add_action('admin_post_mailpoet_form_subscribe', 'mailpoet_form_subscribe');
|
);
|
||||||
add_action('init', 'mailpoet_form_subscribe');
|
*/
|
||||||
|
|
||||||
|
|
||||||
// set the content filter to replace the shortcode
|
// set the content filter to replace the shortcode
|
||||||
|
|
||||||
if(isset($_GET['mailpoet_page']) && strlen(trim($_GET['mailpoet_page'])) > 0) {
|
if(isset($_GET['mailpoet_page']) && strlen(trim($_GET['mailpoet_page'])) > 0) {
|
||||||
$mailpoet = new MailPoetWPI();
|
|
||||||
|
|
||||||
switch($_GET['mailpoet_page']) {
|
switch($_GET['mailpoet_page']) {
|
||||||
|
|
||||||
case 'mailpoet_form_iframe':
|
case 'mailpoet_form_iframe':
|
||||||
$form_id = (isset($_GET['mailpoet_form']) && (int)$_GET['mailpoet_form'] > 0) ? (int)$_GET['mailpoet_form'] : null;
|
$id = (isset($_GET['mailpoet_form']) && (int)$_GET['mailpoet_form'] > 0) ? (int)$_GET['mailpoet_form'] : null;
|
||||||
$form = $mailpoet->forms()->fetchById($form_id);
|
$form = Form::findOne($id);
|
||||||
|
|
||||||
if($form !== null) {
|
if($form !== false) {
|
||||||
// render form
|
// render form
|
||||||
print FormRenderer::getExport('html', $form);
|
print FormRenderer::getExport('html', $form->asArray());
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@ -228,9 +484,9 @@ if(isset($_GET['mailpoet_page']) && strlen(trim($_GET['mailpoet_page'])) > 0) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function mailpoet_page_title($title = '', $id = null) {
|
function mailpoet_page_title($title = '', $id = null) {
|
||||||
$mailpoet = new MailPoetWPI();
|
|
||||||
// get signup confirmation page id
|
// get signup confirmation page id
|
||||||
$page_id = $mailpoet->settings()->get('signup_confirmation_page');
|
$signup_confirmation = Setting::getValue('signup_confirmation');
|
||||||
|
$page_id = $signup_confirmation['page'];
|
||||||
|
|
||||||
// check if we're on the signup confirmation page
|
// check if we're on the signup confirmation page
|
||||||
if((int)$page_id === (int)$id) {
|
if((int)$page_id === (int)$id) {
|
||||||
@ -292,272 +548,4 @@ function mailpoet_page_content($content = '') {
|
|||||||
$content = str_replace('[mailpoet_page]', '', $content);
|
$content = str_replace('[mailpoet_page]', '', $content);
|
||||||
}
|
}
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
function mailpoet_signup_confirmation_email(array $subscriber, array $lists) {
|
|
||||||
$mailpoet = new MailPoetWPI();
|
|
||||||
|
|
||||||
$mailer = new MailPoetMailer($mailpoet->settings()->getAll());
|
|
||||||
|
|
||||||
// email subject & body
|
|
||||||
$email_subject = $mailpoet->settings()->get('signup_confirmation_email_subject');
|
|
||||||
$email_body = nl2br($mailpoet->settings()->get('signup_confirmation_email_body'));
|
|
||||||
|
|
||||||
// check for lists_to_confirm tag
|
|
||||||
if(strpos($email_body, '[lists_to_confirm]') !== FALSE) {
|
|
||||||
// gather all names from lists
|
|
||||||
$list_names = array_map(function($list) { return $list['list_name']; }, $lists);
|
|
||||||
// replace shortcode by list names in email's body
|
|
||||||
$email_body = str_replace('[lists_to_confirm]', join(', ', $list_names), $email_body);
|
|
||||||
}
|
|
||||||
|
|
||||||
// check for activation_link tags
|
|
||||||
if(strpos($email_body, '[activation_link]') !== FALSE && strpos($email_body, '[/activation_link]') !== FALSE) {
|
|
||||||
// get confirmation page id
|
|
||||||
$confirmation_page_id = $mailpoet->settings()->get('signup_confirmation_page');
|
|
||||||
|
|
||||||
// generate confirmation link
|
|
||||||
$confirmation_link = add_query_arg(array(
|
|
||||||
'mailpoet_key' => $subscriber['subscriber_digest']
|
|
||||||
), get_permalink($confirmation_page_id));
|
|
||||||
|
|
||||||
// we have both tags
|
|
||||||
$email_body = str_replace(
|
|
||||||
array('[activation_link]', '[/activation_link]'),
|
|
||||||
array('<a href="'.$confirmation_link.'">', '</a>'),
|
|
||||||
$email_body
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
// no activation link tags detected
|
|
||||||
// TODO...
|
|
||||||
}
|
|
||||||
|
|
||||||
// send confirmation email
|
|
||||||
return $mailer->send(array(
|
|
||||||
'from_email' => $mailpoet->settings()->get('signup_confirmation_from_email'),
|
|
||||||
'from_name' => $mailpoet->settings()->get('signup_confirmation_from_name'),
|
|
||||||
'reply_email' => $mailpoet->settings()->get('signup_confirmation_reply_email'),
|
|
||||||
'reply_name' => $mailpoet->settings()->get('signup_confirmation_reply_name'),
|
|
||||||
'subject' => $email_subject,
|
|
||||||
'html' => $email_body,
|
|
||||||
'text' => '',
|
|
||||||
'to_email' => $subscriber['subscriber_email'],
|
|
||||||
'to_name' => $subscriber['subscriber_email'],
|
|
||||||
));
|
|
||||||
}
|
|
||||||
|
|
||||||
function mailpoet_form_subscribe() {
|
|
||||||
// check to see if we're in an ajax request or post request
|
|
||||||
$doing_ajax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
|
|
||||||
|
|
||||||
if(isset($_GET['action']) && $_GET['action'] === 'mailpoet_form_subscribe') {
|
|
||||||
|
|
||||||
// MailPoetWPI instance
|
|
||||||
$mailpoet = new MailPoetWPI();
|
|
||||||
|
|
||||||
// input data
|
|
||||||
$data = array();
|
|
||||||
|
|
||||||
// output errors
|
|
||||||
$errors = array();
|
|
||||||
|
|
||||||
// get posted data
|
|
||||||
// ajax data
|
|
||||||
$data = json_decode(file_get_contents('php://input'), true);
|
|
||||||
// -or- post data
|
|
||||||
if($data === NULL && !empty($_POST)) { $data = $_POST; }
|
|
||||||
|
|
||||||
// define null subscriber
|
|
||||||
$subscriber = null;
|
|
||||||
|
|
||||||
// check if email is valid
|
|
||||||
if(MailPoetMailer::isEmailAddress($data['email']) === false) {
|
|
||||||
// email invalid
|
|
||||||
$errors[] = __('Invalid email address.');
|
|
||||||
} else {
|
|
||||||
// search for subscriber in database
|
|
||||||
$subscribers = $mailpoet->subscribers()->select(array(
|
|
||||||
'filter' => array(
|
|
||||||
'subscriber_email' => $data['email']
|
|
||||||
),
|
|
||||||
'offset' => 0,
|
|
||||||
'limit' => 1
|
|
||||||
));
|
|
||||||
|
|
||||||
// check if we have any subscriber that matches
|
|
||||||
if(count($subscribers) > 0) {
|
|
||||||
// use existing subscriber
|
|
||||||
$subscriber = $subscribers[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
// is signup confirmation enabled?
|
|
||||||
$needs_confirmation = (bool)$mailpoet->settings()->get('signup_confirmation');
|
|
||||||
|
|
||||||
if($subscriber === null) {
|
|
||||||
// create new subscriber
|
|
||||||
$subscriber = array(
|
|
||||||
'subscriber_email' => $data['email'],
|
|
||||||
'subscriber_firstname' => (isset($data['subscriber_firstname'])) ? $data['subscriber_firstname'] : '',
|
|
||||||
'subscriber_lastname' => (isset($data['subscriber_lastname'])) ? $data['subscriber_lastname'] : '',
|
|
||||||
'subscriber_state' => ($needs_confirmation === true) ? MailPoetSubscribers::STATE_UNCONFIRMED : MailPoetSubscribers::STATE_SUBSCRIBED,
|
|
||||||
'subscriber_created_at' => time()
|
|
||||||
);
|
|
||||||
|
|
||||||
// set custom fields
|
|
||||||
$meta_fields = $mailpoet->getOption('mailpoet_subscriber_meta', array());
|
|
||||||
if(!empty($meta_fields)) {
|
|
||||||
// loop through data to see if any meta field has been passed
|
|
||||||
foreach($meta_fields as $field => $field_data) {
|
|
||||||
// check if it's a mandatory field
|
|
||||||
$is_required = (isset($field_data['params']['required']) && (bool)$field_data['params']['required'] === true);
|
|
||||||
|
|
||||||
if(array_key_exists($field, $data)) {
|
|
||||||
// check if it's a mandatory field
|
|
||||||
if($is_required === true && empty($data[$field])) {
|
|
||||||
// if it's missing, throw an error
|
|
||||||
$errors[] = sprintf(__('"%s" is required'), $field_data['name']);
|
|
||||||
} else {
|
|
||||||
// assign field to subscriber
|
|
||||||
$subscriber[$field] = $data[$field];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(empty($errors)) {
|
|
||||||
// insert new subscriber
|
|
||||||
try {
|
|
||||||
$subscriber = $mailpoet->subscribers()->insert($subscriber);
|
|
||||||
} catch(Exception $e) {
|
|
||||||
$errors[] = $e->getMessage();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// restore deleted subscriber
|
|
||||||
if($subscriber['subscriber_deleted_at'] !== NULL) {
|
|
||||||
// reset subscriber state (depends whether signup confirmation is enabled)
|
|
||||||
if($needs_confirmation === true) {
|
|
||||||
$subscriber['subscriber_state'] = MailPoetSubscribers::STATE_UNCONFIRMED;
|
|
||||||
} else {
|
|
||||||
$subscriber['subscriber_state'] = MailPoetSubscribers::STATE_SUBSCRIBED;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
// update subscriber (reset deleted_at and state)
|
|
||||||
$mailpoet->subscribers()->update(array(
|
|
||||||
'subscriber' => $subscriber['subscriber'],
|
|
||||||
'subscriber_state' => $subscriber['subscriber_state'],
|
|
||||||
'subscriber_deleted_at' => NULL
|
|
||||||
));
|
|
||||||
} catch(Exception $e) {
|
|
||||||
$errors[] = __('An error occurred. Please try again later.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if form id has been passed
|
|
||||||
if(isset($data['form']) && (int)$data['form'] > 0) {
|
|
||||||
// get form id
|
|
||||||
$form_id = (int)$data['form'];
|
|
||||||
// get form
|
|
||||||
$form = $mailpoet->forms()->fetchById($form_id);
|
|
||||||
|
|
||||||
if($form === null) {
|
|
||||||
$errors[] = __('This form does not exist. Please check your forms.');
|
|
||||||
} else {
|
|
||||||
// set subscriptions
|
|
||||||
if(empty($data['lists'])) {
|
|
||||||
$errors[] = __('You need to select a list');
|
|
||||||
} else {
|
|
||||||
// extract list ids
|
|
||||||
$list_ids = array_map('intval', explode(',', $data['lists']));
|
|
||||||
// get lists
|
|
||||||
$lists = $mailpoet->lists()->fetchByIds($list_ids);
|
|
||||||
|
|
||||||
// subscribe user to lists
|
|
||||||
$has_subscribed = $mailpoet->subscriptions()->addSubscriberToLists($subscriber, $lists, $form);
|
|
||||||
|
|
||||||
// if signup confirmation is enabled and the subscriber is unconfirmed
|
|
||||||
if( $needs_confirmation === true
|
|
||||||
&& $has_subscribed === true
|
|
||||||
&& !empty($lists)
|
|
||||||
&& !empty($subscriber['subscriber_digest'])
|
|
||||||
&& $subscriber['subscriber_state'] !== MailPoetSubscribers::STATE_SUBSCRIBED
|
|
||||||
) {
|
|
||||||
// resend confirmation email
|
|
||||||
$is_sent = mailpoet_signup_confirmation_email($subscriber, $lists);
|
|
||||||
|
|
||||||
// error message if the email could not be sent
|
|
||||||
if($is_sent === false) {
|
|
||||||
$errors[] = __('The signup confirmation email could not be sent. Please check your settings.');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// get success message to display after subscription
|
|
||||||
$form_settings = (isset($form['settings']) ? $form['settings'] : null);
|
|
||||||
|
|
||||||
if($subscriber !== null && empty($errors)) {
|
|
||||||
$success = true;
|
|
||||||
$message = $form_settings['success_message'];
|
|
||||||
} else {
|
|
||||||
$success = false;
|
|
||||||
$message = join('<br />', $errors);
|
|
||||||
}
|
|
||||||
|
|
||||||
if($form_settings !== null) {
|
|
||||||
|
|
||||||
// url params for non ajax requests
|
|
||||||
if($doing_ajax === false) {
|
|
||||||
// get referer
|
|
||||||
$referer = (wp_get_referer() !== false) ? wp_get_referer() : $_SERVER['HTTP_REFERER'];
|
|
||||||
|
|
||||||
// redirection parameters
|
|
||||||
$params = array(
|
|
||||||
'mailpoet_form' => (int)$data['form']
|
|
||||||
);
|
|
||||||
|
|
||||||
// handle success/error messages
|
|
||||||
if($success === false) {
|
|
||||||
$params['mailpoet_error'] = urlencode($message);
|
|
||||||
} else {
|
|
||||||
$params['mailpoet_success'] = urlencode($message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($form_settings['on_success']) {
|
|
||||||
case 'page':
|
|
||||||
// response depending on context
|
|
||||||
if($doing_ajax === true) {
|
|
||||||
echo json_encode(array(
|
|
||||||
'success' => $success,
|
|
||||||
'page' => get_permalink($form_settings['success_page']),
|
|
||||||
'message' => $message
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
$redirect_to = ($success === false) ? $referer : get_permalink($form_settings['success_page']);
|
|
||||||
wp_redirect(add_query_arg($params, $redirect_to));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'message':
|
|
||||||
default:
|
|
||||||
// response depending on context
|
|
||||||
if($doing_ajax === true) {
|
|
||||||
echo json_encode(array(
|
|
||||||
'success' => $success,
|
|
||||||
'message' => $message
|
|
||||||
));
|
|
||||||
} else {
|
|
||||||
// redirect to previous page
|
|
||||||
wp_redirect(add_query_arg($params, $referer));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
exit();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
@ -79,6 +79,33 @@ class Form extends Model {
|
|||||||
$form = self::findOne((int)$data['id']);
|
$form = self::findOne((int)$data['id']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// check if the user gets to pick his own lists
|
||||||
|
// or if it's selected by the admin
|
||||||
|
$has_segment_selection = false;
|
||||||
|
|
||||||
|
if(!empty($body)) {
|
||||||
|
foreach ($body as $i => $block) {
|
||||||
|
if($block['type'] === 'segment') {
|
||||||
|
$has_segment_selection = true;
|
||||||
|
if(!empty($block['params']['values'])) {
|
||||||
|
$list_selection = array_map(function($segment) {
|
||||||
|
if(!empty($segment)) {
|
||||||
|
return (int)$segment['id'];
|
||||||
|
}
|
||||||
|
}, $block['params']['values']);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check list selectio
|
||||||
|
if($has_segment_selection === true) {
|
||||||
|
$settings['segments_selected_by'] = 'user';
|
||||||
|
} else {
|
||||||
|
$settings['segments_selected_by'] = 'admin';
|
||||||
|
}
|
||||||
|
|
||||||
if($form === false) {
|
if($form === false) {
|
||||||
$form = self::create();
|
$form = self::create();
|
||||||
$form->hydrate($data);
|
$form->hydrate($data);
|
||||||
|
@ -29,6 +29,44 @@ class Segment extends Model {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function subscribers() {
|
||||||
|
return $this->has_many_through(
|
||||||
|
__NAMESPACE__.'\Subscriber',
|
||||||
|
__NAMESPACE__.'\SubscriberSegment',
|
||||||
|
'segment_id',
|
||||||
|
'subscriber_id'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function duplicate($data = array()) {
|
||||||
|
$duplicate = parent::duplicate($data);
|
||||||
|
|
||||||
|
if($duplicate !== false) {
|
||||||
|
foreach($this->subscribers()->findResultSet() as $relation) {
|
||||||
|
$new_relation = SubscriberSegment::create();
|
||||||
|
$new_relation->set('subscriber_id', $relation->id);
|
||||||
|
$new_relation->set('segment_id', $duplicate->id);
|
||||||
|
$new_relation->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return $duplicate;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
function addSubscriber($subscriber_id) {
|
||||||
|
$relation = SubscriberSegment::create();
|
||||||
|
$relation->set('subscriber_id', $subscriber_id);
|
||||||
|
$relation->set('segment_id', $this->id);
|
||||||
|
return $relation->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
function removeSubscriber($subscriber_id) {
|
||||||
|
return SubscriberSegment::where('subscriber_id', $subscriber_id)
|
||||||
|
->where('segment_id', $this->id)
|
||||||
|
->delete();
|
||||||
|
}
|
||||||
|
|
||||||
static function search($orm, $search = '') {
|
static function search($orm, $search = '') {
|
||||||
return $orm->where_like('name', '%'.$search.'%');
|
return $orm->where_like('name', '%'.$search.'%');
|
||||||
}
|
}
|
||||||
@ -83,29 +121,4 @@ class Segment extends Model {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function duplicate($data = array()) {
|
|
||||||
$duplicate = parent::duplicate($data);
|
|
||||||
|
|
||||||
if($duplicate !== false) {
|
|
||||||
foreach($this->subscribers()->findResultSet() as $relation) {
|
|
||||||
$new_relation = SubscriberSegment::create();
|
|
||||||
$new_relation->set('subscriber_id', $relation->id);
|
|
||||||
$new_relation->set('segment_id', $duplicate->id);
|
|
||||||
$new_relation->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
return $duplicate;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
function subscribers() {
|
|
||||||
return $this->has_many_through(
|
|
||||||
__NAMESPACE__.'\Subscriber',
|
|
||||||
__NAMESPACE__.'\SubscriberSegment',
|
|
||||||
'segment_id',
|
|
||||||
'subscriber_id'
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -168,17 +168,8 @@ class Subscriber extends Model {
|
|||||||
$subscriber->set($data);
|
$subscriber->set($data);
|
||||||
}
|
}
|
||||||
|
|
||||||
$saved = $subscriber->save();
|
$subscriber->save();
|
||||||
|
return $subscriber;
|
||||||
if($saved === true) {
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
$errors = $subscriber->getValidationErrors();
|
|
||||||
if(!empty($errors)) {
|
|
||||||
return $errors;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static function bulkMoveToList($orm, $data = array()) {
|
static function bulkMoveToList($orm, $data = array()) {
|
||||||
|
@ -149,32 +149,6 @@ class Forms {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if the user gets to pick his own lists or if it's selected by the admin
|
|
||||||
$has_segment_selection = false;
|
|
||||||
|
|
||||||
if(!empty($body)) {
|
|
||||||
foreach ($body as $i => $block) {
|
|
||||||
if($block['type'] === 'segment') {
|
|
||||||
$has_segment_selection = true;
|
|
||||||
if(!empty($block['params']['values'])) {
|
|
||||||
$list_selection = array_map(function($segment) {
|
|
||||||
if(!empty($segment)) {
|
|
||||||
return (int)$segment['id'];
|
|
||||||
}
|
|
||||||
}, $block['params']['values']);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check list selectio
|
|
||||||
if($has_segment_selection === true) {
|
|
||||||
$settings['segments_selected_by'] = 'user';
|
|
||||||
} else {
|
|
||||||
$settings['segments_selected_by'] = 'admin';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$form = Form::createOrUpdate(array(
|
$form = Form::createOrUpdate(array(
|
||||||
|
@ -39,7 +39,7 @@
|
|||||||
id="mailpoet_form_id"
|
id="mailpoet_form_id"
|
||||||
value="<%= form.id | default(0) %>"
|
value="<%= form.id | default(0) %>"
|
||||||
/>
|
/>
|
||||||
<div id="mailpoet_settings_list_selection">
|
<div id="mailpoet_settings_segment_selection">
|
||||||
<!-- Form settings: list selection -->
|
<!-- Form settings: list selection -->
|
||||||
<p>
|
<p>
|
||||||
<strong><%= __('This form adds subscribers to these lists:') %></strong>
|
<strong><%= __('This form adds subscribers to these lists:') %></strong>
|
||||||
@ -195,6 +195,8 @@
|
|||||||
)%>
|
)%>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
var mailpoet_segments = <%= json_encode(segments) %>;
|
||||||
|
|
||||||
jQuery(function($) {
|
jQuery(function($) {
|
||||||
function mailpoet_form_fields(data) {
|
function mailpoet_form_fields(data) {
|
||||||
var template = Handlebars.compile(jQuery('#form_template_fields').html());
|
var template = Handlebars.compile(jQuery('#form_template_fields').html());
|
||||||
@ -241,11 +243,10 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "<%= __('List selection') %>",
|
name: "<%= __('List selection') %>",
|
||||||
field: 'list',
|
field: 'segments',
|
||||||
type: 'list',
|
type: 'segment',
|
||||||
params: {
|
params: {
|
||||||
label: "<%= __('Select list(s):') %>",
|
label: "<%= __('Select list(s):') %>"
|
||||||
values: []
|
|
||||||
},
|
},
|
||||||
readonly: true
|
readonly: true
|
||||||
},
|
},
|
||||||
@ -271,7 +272,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
// toolbar: open default section
|
// toolbar: open default section
|
||||||
$('.mailpoet_toolbar_section[data-section="settings"]').removeClass('closed');
|
$('.mailpoet_toolbar_section[data-section="fields"]').removeClass('closed');
|
||||||
|
|
||||||
// form: edit name (in place editor)
|
// form: edit name (in place editor)
|
||||||
$('#mailpoet_form_edit_name').on('click', function() {
|
$('#mailpoet_form_edit_name').on('click', function() {
|
||||||
@ -372,7 +373,7 @@
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
mailpoet_form_export();
|
mailpoet_form_export(<%= exports | json_encode | raw %>);
|
||||||
|
|
||||||
function mailpoet_form_save(callback) {
|
function mailpoet_form_save(callback) {
|
||||||
var form = WysijaForm.save();
|
var form = WysijaForm.save();
|
||||||
@ -424,9 +425,9 @@
|
|||||||
$('#mailpoet_on_success_'+value).show();
|
$('#mailpoet_on_success_'+value).show();
|
||||||
}
|
}
|
||||||
|
|
||||||
function mailpoet_form_export() {
|
function mailpoet_form_export(exports) {
|
||||||
var template = Handlebars.compile($('#form_template_exports').html());
|
var template = Handlebars.compile($('#form_template_exports').html());
|
||||||
$('#mailpoet_form_export').html(template({ exports: <%= exports | json_encode | raw %> }));
|
$('#mailpoet_form_export').html(template({ exports: exports }));
|
||||||
}
|
}
|
||||||
|
|
||||||
$(document).on('click', '.mailpoet_form_export_toggle', function() {
|
$(document).on('click', '.mailpoet_form_export_toggle', function() {
|
||||||
@ -512,15 +513,10 @@
|
|||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
|
|
||||||
// toolbar: list selection
|
// toolbar: segment selection
|
||||||
var selected_segments = <%= form.settings.segments | json_encode | raw %>;
|
var selected_segments = <%= form.settings.segments | json_encode | raw %>;
|
||||||
var selected_segments_ids = [];
|
|
||||||
if(selected_segments !== null) {
|
// enable select2 for segment selection
|
||||||
selected_segments_ids = selected_segments.map(function(segment) {
|
|
||||||
return parseInt(segment.id, 10);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// enable select2 for list selection
|
|
||||||
$('#mailpoet_form_segments').select2({
|
$('#mailpoet_form_segments').select2({
|
||||||
width:'100%',
|
width:'100%',
|
||||||
templateResult: function(item) {
|
templateResult: function(item) {
|
||||||
@ -534,10 +530,10 @@
|
|||||||
|
|
||||||
// subscriber meta fields
|
// subscriber meta fields
|
||||||
var meta_fields = [
|
var meta_fields = [
|
||||||
{"name":"CF text input (mandatory + numbers only)","field":"627e1c8d4fe97712836484e0f2377abd","type":"input","params":{"required":"1","validate":"onlyNumberSp","label":"CF text input (mandatory + numbers only)"}},
|
// {"name":"CF text input (mandatory + numbers only)","field":"627e1c8d4fe97712836484e0f2377abd","type":"input","params":{"required":"1","validate":"onlyNumberSp","label":"CF text input (mandatory + numbers only)"}},
|
||||||
{"name":"CF text area (no validation)","field":"6607e6e447b89384c59adc124e979d6b","type":"textarea","params":{"required":"","validate":"","label":"CF text area (no validation)"}},
|
// {"name":"CF text area (no validation)","field":"6607e6e447b89384c59adc124e979d6b","type":"textarea","params":{"required":"","validate":"","label":"CF text area (no validation)"}},
|
||||||
{"name":"CF text area (letters only)","field":"47a9cae88f8b3b3e23a16990922e7905","type":"textarea","params":{"required":"","validate":"onlyLetterSp","label":"CF text area (letters only)"}},
|
// {"name":"CF text area (letters only)","field":"47a9cae88f8b3b3e23a16990922e7905","type":"textarea","params":{"required":"","validate":"onlyLetterSp","label":"CF text area (letters only)"}},
|
||||||
{"name":"CF radio (mandatory)","field":"5f01b4ccc146fdd6b889bab63dc1b5cd","type":"radio","params":{"values":[{"value":"un"},{"is_checked":"1","value":"deux"},{"value":"trois"}],"required":"1","label":"CF radio (mandatory)"}}
|
// {"name":"CF radio (mandatory)","field":"5f01b4ccc146fdd6b889bab63dc1b5cd","type":"radio","params":{"values":[{"value":"un"},{"is_checked":"1","value":"deux"},{"value":"trois"}],"required":"1","label":"CF radio (mandatory)"}}
|
||||||
];
|
];
|
||||||
|
|
||||||
// toolbar: fiels
|
// toolbar: fiels
|
||||||
@ -559,7 +555,7 @@
|
|||||||
<%= partial('form_template_divider', 'form/templates/blocks/divider.hbs') %>
|
<%= partial('form_template_divider', 'form/templates/blocks/divider.hbs') %>
|
||||||
<%= partial('form_template_input', 'form/templates/blocks/input.hbs') %>
|
<%= partial('form_template_input', 'form/templates/blocks/input.hbs') %>
|
||||||
<%= partial('form_template_submit', 'form/templates/blocks/submit.hbs') %>
|
<%= partial('form_template_submit', 'form/templates/blocks/submit.hbs') %>
|
||||||
<%= partial('form_template_list', 'form/templates/blocks/list.hbs') %>
|
<%= partial('form_template_segment', 'form/templates/blocks/segment.hbs') %>
|
||||||
<%= partial('form_template_radio', 'form/templates/blocks/radio.hbs') %>
|
<%= partial('form_template_radio', 'form/templates/blocks/radio.hbs') %>
|
||||||
<%= partial('form_template_select', 'form/templates/blocks/select.hbs') %>
|
<%= partial('form_template_select', 'form/templates/blocks/select.hbs') %>
|
||||||
<%= partial('form_template_checkbox', 'form/templates/blocks/checkbox.hbs') %>
|
<%= partial('form_template_checkbox', 'form/templates/blocks/checkbox.hbs') %>
|
||||||
@ -584,8 +580,8 @@
|
|||||||
<%= partial('field_settings_values_item', 'form/templates/settings/values_item.hbs') %>
|
<%= partial('field_settings_values_item', 'form/templates/settings/values_item.hbs') %>
|
||||||
<%= partial('field_settings_date_formats', 'form/templates/settings/date_formats.hbs') %>
|
<%= partial('field_settings_date_formats', 'form/templates/settings/date_formats.hbs') %>
|
||||||
<%= partial('field_settings_date_type', 'form/templates/settings/date_types.hbs') %>
|
<%= partial('field_settings_date_type', 'form/templates/settings/date_types.hbs') %>
|
||||||
<%= partial('field_settings_list_selection_item', 'form/templates/settings/list_selection_item.hbs') %>
|
<%= partial('field_settings_segment_selection_item', 'form/templates/settings/segment_selection_item.hbs') %>
|
||||||
<%= partial('field_settings_list_selection', 'form/templates/settings/list_selection.hbs', '_settings_list_selection') %>
|
<%= partial('field_settings_segment_selection', 'form/templates/settings/segment_selection.hbs', '_settings_segment_selection') %>
|
||||||
|
|
||||||
<!-- custom field: new -->
|
<!-- custom field: new -->
|
||||||
<%= partial('form_template_field_new', 'form/templates/settings/field_new.hbs') %>
|
<%= partial('form_template_field_new', 'form/templates/settings/field_new.hbs') %>
|
||||||
|
@ -2,6 +2,6 @@
|
|||||||
{{#unless params.values}}<p class="mailpoet_error"><%= __('You have to select at least 1 list') %></p>{{/unless}}
|
{{#unless params.values}}<p class="mailpoet_error"><%= __('You have to select at least 1 list') %></p>{{/unless}}
|
||||||
{{#each params.values}}
|
{{#each params.values}}
|
||||||
<p>
|
<p>
|
||||||
<input class="mailpoet_checkbox" type="checkbox" data-list-id="{{ id }}" {{#if is_checked}}checked="checked"{{/if}} disabled="disabled" />{{ name }}
|
<input class="mailpoet_checkbox" type="checkbox" {{#if is_checked}}checked="checked"{{/if}} disabled="disabled" />{{ name }}
|
||||||
</p>
|
</p>
|
||||||
{{/each}}
|
{{/each}}
|
@ -31,9 +31,9 @@
|
|||||||
{{> _settings_label }}
|
{{> _settings_label }}
|
||||||
{{/ifCond}}
|
{{/ifCond}}
|
||||||
|
|
||||||
{{#ifCond type '==' 'list'}}
|
{{#ifCond type '==' 'segment'}}
|
||||||
{{> _settings_label }}
|
{{> _settings_label }}
|
||||||
{{> _settings_list_selection }}
|
{{> _settings_segment_selection }}
|
||||||
{{/ifCond}}
|
{{/ifCond}}
|
||||||
|
|
||||||
{{#ifCond type '==' 'select'}}
|
{{#ifCond type '==' 'select'}}
|
||||||
|
@ -1,124 +0,0 @@
|
|||||||
<p class="mailpoet_error" data-error="user_no_list">
|
|
||||||
<%= __('You need to specify at least 1 list.') %>
|
|
||||||
</p>
|
|
||||||
|
|
||||||
<ul id="mailpoet_list_selection" class="clearfix"></ul>
|
|
||||||
|
|
||||||
<div id="mailpoet_list_available_container">
|
|
||||||
<h3><%= __('Select the list you want to add:') %></h3>
|
|
||||||
|
|
||||||
<select class="mailpoet_list_available"></select>
|
|
||||||
|
|
||||||
<a href="javascript:;" class="mailpoet_list_add"><span><%= __('Add') %></span></a>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script type="text/javascript">
|
|
||||||
<% autoescape false %>
|
|
||||||
var selected_lists = {{#if params.values}}{{{ json_encode params.values }}}
|
|
||||||
{{else}}{}{{/if}},
|
|
||||||
available_lists = [
|
|
||||||
<% for list in lists %>
|
|
||||||
<%= list | json_encode %>,
|
|
||||||
<% endfor %>
|
|
||||||
];
|
|
||||||
<% endautoescape %>
|
|
||||||
|
|
||||||
jQuery(function($) {
|
|
||||||
$(function() {
|
|
||||||
mailpoet_list_available_render();
|
|
||||||
mailpoet_list_selection_render();
|
|
||||||
|
|
||||||
setInputNames();
|
|
||||||
|
|
||||||
// make list selection sortable
|
|
||||||
Sortable.create('mailpoet_list_selection', {
|
|
||||||
handles: $$('#mailpoet_list_selection .handle')
|
|
||||||
});
|
|
||||||
|
|
||||||
// add list
|
|
||||||
$('.mailpoet_list_add').on('click', function() {
|
|
||||||
// add currently selected list to the selection
|
|
||||||
var selected_list = $('.mailpoet_list_available :selected');
|
|
||||||
|
|
||||||
// add list to selection
|
|
||||||
selected_lists.push({
|
|
||||||
list: selected_list.val(),
|
|
||||||
list_name: selected_list.text(),
|
|
||||||
is_checked: 0
|
|
||||||
});
|
|
||||||
|
|
||||||
// remove list from available lists
|
|
||||||
selected_list.remove();
|
|
||||||
|
|
||||||
// render selection
|
|
||||||
mailpoet_list_selection_render();
|
|
||||||
|
|
||||||
setInputNames();
|
|
||||||
});
|
|
||||||
|
|
||||||
// remove list
|
|
||||||
$('#mailpoet_list_selection').on('click', '.remove', function() {
|
|
||||||
var element = $(this).parents('li');
|
|
||||||
// remove currently selected list to the selection
|
|
||||||
var selected_list = parseInt(element.data('list'), 10);
|
|
||||||
|
|
||||||
// remove list from selection
|
|
||||||
selected_lists = selected_lists.filter(function(list) {
|
|
||||||
return (parseInt(list.id, 10) !== selected_list);
|
|
||||||
});
|
|
||||||
|
|
||||||
// remove element
|
|
||||||
element.remove();
|
|
||||||
|
|
||||||
// render available list
|
|
||||||
mailpoet_list_available_render();
|
|
||||||
|
|
||||||
setInputNames();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
function mailpoet_list_available_render() {
|
|
||||||
// get selected lists ids
|
|
||||||
var selected_lists_ids = selected_lists.map(function(list) { return parseInt(list.id, 10); });
|
|
||||||
|
|
||||||
// clear available lists
|
|
||||||
$('.mailpoet_list_available').html('');
|
|
||||||
|
|
||||||
// display available lists
|
|
||||||
$.each(available_lists, function(i, list) {
|
|
||||||
if($.inArray(list.id, selected_lists_ids) < 0) {
|
|
||||||
$('.mailpoet_list_available').append('<option value="'+list.id+'">'+list.name+'</option>');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
mailpoet_list_available_toggle();
|
|
||||||
}
|
|
||||||
|
|
||||||
function mailpoet_list_selection_render() {
|
|
||||||
// list item template
|
|
||||||
var template = Handlebars.compile($('#field_settings_list_selection_item').html());
|
|
||||||
|
|
||||||
// update view
|
|
||||||
$('#mailpoet_list_selection').html(template({ lists: selected_lists }));
|
|
||||||
|
|
||||||
mailpoet_list_available_toggle();
|
|
||||||
}
|
|
||||||
|
|
||||||
function mailpoet_list_available_toggle() {
|
|
||||||
// toggle visibility of available lists
|
|
||||||
if($('.mailpoet_list_available option').length === 0) {
|
|
||||||
$('#mailpoet_list_available_container').hide();
|
|
||||||
} else {
|
|
||||||
$('#mailpoet_list_available_container').show();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function setInputNames() {
|
|
||||||
$('#mailpoet_list_selection li').each(function(index, item) {
|
|
||||||
$(item).find('.mailpoet_is_checked').attr('name', 'params[values]['+index+'][is_checked]');
|
|
||||||
$(item).find('.mailpoet_list_id').attr('name', 'params[values]['+index+'][id]');
|
|
||||||
$(item).find('.mailpoet_list_name').attr('name', 'params[values]['+index+'][name]');
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
<{{!}}/script>
|
|
117
views/form/templates/settings/segment_selection.hbs
Normal file
117
views/form/templates/settings/segment_selection.hbs
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
<ul id="mailpoet_segment_selection" class="clearfix"></ul>
|
||||||
|
|
||||||
|
<div id="mailpoet_segment_available_container">
|
||||||
|
<h3><%= __('Select the segment you want to add:') %></h3>
|
||||||
|
|
||||||
|
<select class="mailpoet_segment_available"></select>
|
||||||
|
|
||||||
|
<a href="javascript:;" class="mailpoet_segment_add"><span><%= __('Add') %></span></a>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<script type="text/javascript">
|
||||||
|
jQuery(function($) {
|
||||||
|
<% autoescape false %>
|
||||||
|
var selected_segments = {{#if params.values}}{{{ json_encode params.values }}}
|
||||||
|
{{else}}[]{{/if}};
|
||||||
|
<% endautoescape %>
|
||||||
|
|
||||||
|
$(function() {
|
||||||
|
mailpoet_segment_available_render();
|
||||||
|
mailpoet_segment_selection_render();
|
||||||
|
|
||||||
|
setInputNames();
|
||||||
|
|
||||||
|
// make segment selection sortable
|
||||||
|
Sortable.create('mailpoet_segment_selection', {
|
||||||
|
handles: $$('#mailpoet_segment_selection .handle')
|
||||||
|
});
|
||||||
|
|
||||||
|
// add segment
|
||||||
|
$('.mailpoet_segment_add').on('click', function() {
|
||||||
|
// add currently selected segment to the selection
|
||||||
|
var selected_segment = $('.mailpoet_segment_available :selected');
|
||||||
|
|
||||||
|
// add segment to selection
|
||||||
|
selected_segments.push({
|
||||||
|
id: selected_segment.val(),
|
||||||
|
name: selected_segment.text(),
|
||||||
|
is_checked: 0
|
||||||
|
});
|
||||||
|
|
||||||
|
// remove segment from available segments
|
||||||
|
selected_segment.remove();
|
||||||
|
|
||||||
|
// render selection
|
||||||
|
mailpoet_segment_selection_render();
|
||||||
|
|
||||||
|
setInputNames();
|
||||||
|
});
|
||||||
|
|
||||||
|
// remove segment
|
||||||
|
$('#mailpoet_segment_selection').on('click', '.remove', function(e) {
|
||||||
|
if($('#mailpoet_segment_selection').children().length === 1) {
|
||||||
|
return e.preventDefault();
|
||||||
|
}
|
||||||
|
var element = $(this).parents('li');
|
||||||
|
// remove currently selected segment to the selection
|
||||||
|
var selected_segment = parseInt(element.data('segment'), 10);
|
||||||
|
|
||||||
|
// remove segment from selection
|
||||||
|
selected_segments = selected_segments.filter(function(segment) {
|
||||||
|
return (parseInt(segment.id, 10) !== selected_segment);
|
||||||
|
});
|
||||||
|
|
||||||
|
// remove element
|
||||||
|
element.remove();
|
||||||
|
|
||||||
|
// render available segment
|
||||||
|
mailpoet_segment_available_render();
|
||||||
|
|
||||||
|
setInputNames();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
function mailpoet_segment_available_render() {
|
||||||
|
// clear available segments
|
||||||
|
$('.mailpoet_segment_available').html('');
|
||||||
|
|
||||||
|
var selected_segment_ids = selected_segments.map(function(segment) {
|
||||||
|
return segment.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
// display available segments
|
||||||
|
$.each(mailpoet_segments, function(i, segment) {
|
||||||
|
if($.inArray(segment.id, selected_segment_ids) < 0) {
|
||||||
|
$('.mailpoet_segment_available').append('<option value="'+segment.id+'">'+segment.name+'</option>');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
mailpoet_segment_available_toggle();
|
||||||
|
}
|
||||||
|
|
||||||
|
function mailpoet_segment_selection_render() {
|
||||||
|
// segment item template
|
||||||
|
var template = Handlebars.compile($('#field_settings_segment_selection_item').html());
|
||||||
|
// update view
|
||||||
|
$('#mailpoet_segment_selection').html(template({ segments: selected_segments }));
|
||||||
|
|
||||||
|
mailpoet_segment_available_toggle();
|
||||||
|
}
|
||||||
|
|
||||||
|
function mailpoet_segment_available_toggle() {
|
||||||
|
// toggle visibility of available segments
|
||||||
|
if($('.mailpoet_segment_available option').length === 0) {
|
||||||
|
$('#mailpoet_segment_available_container').hide();
|
||||||
|
} else {
|
||||||
|
$('#mailpoet_segment_available_container').show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function setInputNames() {
|
||||||
|
$('#mailpoet_segment_selection li').each(function(index, item) {
|
||||||
|
$(item).find('.mailpoet_is_checked').attr('name', 'params[values]['+index+'][is_checked]');
|
||||||
|
$(item).find('.mailpoet_segment_id').attr('name', 'params[values]['+index+'][id]');
|
||||||
|
$(item).find('.mailpoet_segment_name').attr('name', 'params[values]['+index+'][name]');
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
<{{!}}/script>
|
@ -1,10 +1,10 @@
|
|||||||
{{#each lists}}
|
{{#each segments}}
|
||||||
<li data-list="{{ id }}">
|
<li data-segment="{{ id }}">
|
||||||
<label>
|
<label>
|
||||||
<input class="mailpoet_list_id" type="hidden" value="{{ id }}" />
|
<input class="mailpoet_segment_id" type="hidden" value="{{ id }}" />
|
||||||
<input class="mailpoet_is_checked" type="checkbox" value="1"
|
<input class="mailpoet_is_checked" type="checkbox" value="1"
|
||||||
{{#ifCond is_checked '>' 0}}checked="checked"{{/ifCond}} />
|
{{#ifCond is_checked '>' 0}}checked="checked"{{/ifCond}} />
|
||||||
<input class="mailpoet_list_name" type="hidden" value="{{ name }}" />
|
<input class="mailpoet_segment_name" type="hidden" value="{{ name }}" />
|
||||||
{{ name }}
|
{{ name }}
|
||||||
</label>
|
</label>
|
||||||
<a class="remove" href="javascript:;"><%= __('Remove') %></a>
|
<a class="remove" href="javascript:;"><%= __('Remove') %></a>
|
Reference in New Issue
Block a user