diff --git a/assets/css/lib/jquery.validationEngine.css b/assets/css/lib/jquery.validationEngine.css new file mode 120000 index 0000000000..fbedb59e0b --- /dev/null +++ b/assets/css/lib/jquery.validationEngine.css @@ -0,0 +1 @@ +../../../node_modules/jquery-validation-engine/css/validationEngine.jquery.css \ No newline at end of file diff --git a/assets/js/lib/jQuery.validationEngine-en.js b/assets/js/lib/jQuery.validationEngine-en.js deleted file mode 120000 index 9183295f7d..0000000000 --- a/assets/js/lib/jQuery.validationEngine-en.js +++ /dev/null @@ -1 +0,0 @@ -../../../node_modules/jquery-validation-engine/js/languages/jquery.validationEngine-en.js \ No newline at end of file diff --git a/assets/js/lib/jquery.validationEngine.js b/assets/js/lib/jquery.validationEngine.js deleted file mode 120000 index 1b3c93e403..0000000000 --- a/assets/js/lib/jquery.validationEngine.js +++ /dev/null @@ -1 +0,0 @@ -../../../node_modules/jquery-validation-engine/js/jquery.validationEngine.js \ No newline at end of file diff --git a/assets/js/lib/validation/jquery.validationEngine.js b/assets/js/lib/validation/jquery.validationEngine.js new file mode 120000 index 0000000000..ca95dcf708 --- /dev/null +++ b/assets/js/lib/validation/jquery.validationEngine.js @@ -0,0 +1 @@ +../../../../node_modules/jquery-validation-engine/js/jquery.validationEngine.js \ No newline at end of file diff --git a/assets/js/lib/validation/languages b/assets/js/lib/validation/languages new file mode 120000 index 0000000000..9a24188060 --- /dev/null +++ b/assets/js/lib/validation/languages @@ -0,0 +1 @@ +../../../../node_modules/jquery-validation-engine/js/languages/ \ No newline at end of file diff --git a/assets/js/widget.js b/assets/js/widget.js new file mode 100644 index 0000000000..77a834885d --- /dev/null +++ b/assets/js/widget.js @@ -0,0 +1,88 @@ +jQuery(function($) { + function isSameDomain(url) { + var link = document.createElement('a'); + link.href = url; + return (window.location.hostname === link.hostname); + } + + // backwards compatibility for older jQuery versions + if($.fn.on === undefined) { + // mimic on() function using live() + $.fn.on = function(events, selector, data, handler) { + if(typeof(selector) === 'function') { + $(this.context).live(events, selector); + } else { + $(selector).live(events, data, handler); + } + return this; + }; + } + + $(function() { + // setup form validation + $('form.mailpoet_form').validationEngine('attach', { + promptPosition: (parseInt(MailPoetWidget.is_rtl, 10) === 1) + ? 'centerLeft' : 'centerRight', + scroll: false, + onValidationComplete: function(form, is_form_valid) { + if(is_form_valid === true) { + // get data + var raw_data = $(form).serializeArray(), + data = {}; + + // clear messages + $(form).find('.mailpoet_message').html(''); + // hide all validation messages + $(form).validationEngine('hideAll'); + + // format data + $.each(raw_data, function(index, value) { + if(value.name.endsWith('[]')) { + var value_name = value.name.substr(0, value.name.length - 2); + // it's an array + if(data[value_name] === undefined) { + data[value_name] = []; + } + data[value_name].push(value.value); + } else { + data[value.name] = value.value; + } + }); + + if(isSameDomain(MailPoetWidget.ajax_url)) { + $.ajax(MailPoetWidget.ajax_url+ '?action=mailpoet_form_subscribe', { + data: JSON.stringify(data), + processData: false, + contentType: "application/json; charset=utf-8", + type : 'POST', + dataType: 'json', + success:function(response) { + // display response + if(response.success === true) { + if(response.page !== undefined) { + window.location.href = response.page; + } else if(response.message !== undefined) { + // success + $(form) + .find('.mailpoet_message') + .html('
'+response.message+'
'); + } + + $(form).trigger('reset'); + } else { + if(response.message !== undefined) { + $(form) + .find('.mailpoet_message') + .html(''+response.message+'
'); + } + } + } + }); + } else { + return true; + } + } + } + }); + }); +}); diff --git a/lib/Config/Initializer.php b/lib/Config/Initializer.php index e24c69d069..9df628660a 100644 --- a/lib/Config/Initializer.php +++ b/lib/Config/Initializer.php @@ -57,8 +57,7 @@ class Initializer { } function setupWidget() { - add_action('widgets_init', function() { - register_widget('\MailPoet\Form\Widget'); - }); + $widget = new Widget(); + $widget->init(); } } diff --git a/lib/Config/Renderer.php b/lib/Config/Renderer.php index e239ea610c..12827d6018 100644 --- a/lib/Config/Renderer.php +++ b/lib/Config/Renderer.php @@ -12,7 +12,7 @@ class Renderer { $file_system = new TwigFileSystem(Env::$views_path); $this->renderer = new TwigEnv( $file_system, - array('cache' => $this->detectCache()) + array('cache' => $this->getCachePath()) ); } @@ -49,9 +49,8 @@ class Renderer { $this->renderer->setLexer($lexer); } - function detectCache() { - $cache_path = Env::$views_path . '/cache'; - if (WP_DEBUG === false) return $cache_path; - return false; + function getCachePath() { + if(WP_DEBUG === true) { return false; } + return Env::$views_path . '/cache'; } } diff --git a/lib/Config/Widget.php b/lib/Config/Widget.php new file mode 100644 index 0000000000..b3c70efc6f --- /dev/null +++ b/lib/Config/Widget.php @@ -0,0 +1,91 @@ +registerWidget(); + $this->setupActions(); + $this->setupDependencies(); + } + + private function registerWidget() { + add_action('widgets_init', + function() { + register_widget('\MailPoet\Form\Widget'); + }); + } + + private function setupDependencies() { + add_action('widgets_init', function() { + $locale = \get_locale(); + + wp_enqueue_script( + 'mailpoet_validation_i18n', + Env::$assets_url.'/js/lib/validation/languages/'. + 'jquery.validationEngine-'.substr($locale, 0, 2).'.js', + array(), + Env::$version, + true + ); + + wp_enqueue_script( + 'mailpoet_validation', + Env::$assets_url.'/js/lib/validation/jquery.validationEngine.js', + array(), + Env::$version, + true + ); + + wp_enqueue_style('mailpoet_validation', + Env::$assets_url.'/css/lib/jquery.validationEngine.css', + array(), + Env::$version + ); + + wp_enqueue_script('mailpoet_widget', + Env::$assets_url.'/js/widget.js', + array(), + Env::$version, + true + ); + + wp_localize_script( + 'mailpoet_widget', + 'MailPoetWidget', + array( + 'is_rtl' => (int)(function_exists('is_rtl') && is_rtl()), + 'ajax_url' => admin_url('admin-ajax.php') + )); + }); + } + + private function setupActions() { + // ajax requests + add_action( + 'wp_ajax_mailpoet_form_subscribe', + 'mailpoet_form_subscribe' + ); + add_action( + 'wp_ajax_nopriv_mailpoet_form_subscribe', + 'mailpoet_form_subscribe' + ); + // post request + add_action( + 'admin_post_nopriv_mailpoet_form_subscribe', + 'mailpoet_form_subscribe' + ); + add_action( + 'admin_post_mailpoet_form_subscribe', + 'mailpoet_form_subscribe' + ); + /*add_action( + 'init', + 'mailpoet_form_subscribe' + );*/ + } +} diff --git a/lib/Form/Widget.php b/lib/Form/Widget.php index e2652fca3b..dccd20302b 100644 --- a/lib/Form/Widget.php +++ b/lib/Form/Widget.php @@ -87,7 +87,7 @@ class Widget extends \WP_Widget { $output .= ''; $output .= ' '; $output .= '
';