Skip to content

Commit

Permalink
* Success page widget - WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahaenor committed Jun 4, 2017
1 parent 7e6ca2f commit 7a35b2a
Show file tree
Hide file tree
Showing 9 changed files with 254 additions and 28 deletions.
167 changes: 165 additions & 2 deletions assets/js/public.js
@@ -1,3 +1,121 @@
/*!
* jQuery Cookie Plugin v1.4.1
* https://github.com/carhartl/jquery-cookie
*
* Copyright 2013 Klaus Hartl
* Released under the MIT license
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD
define(['jquery'], factory);
} else if (typeof exports === 'object') {
// CommonJS
factory(require('jquery'));
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {

var pluses = /\+/g;

function encode(s) {
return config.raw ? s : encodeURIComponent(s);
}

function decode(s) {
return config.raw ? s : decodeURIComponent(s);
}

function stringifyCookieValue(value) {
return encode(config.json ? JSON.stringify(value) : String(value));
}

function parseCookieValue(s) {
if (s.indexOf('"') === 0) {
// This is a quoted cookie as according to RFC2068, unescape...
s = s.slice(1, -1).replace(/\\"/g, '"').replace(/\\\\/g, '\\');
}

try {
// Replace server-side written pluses with spaces.
// If we can't decode the cookie, ignore it, it's unusable.
// If we can't parse the cookie, ignore it, it's unusable.
s = decodeURIComponent(s.replace(pluses, ' '));
return config.json ? JSON.parse(s) : s;
} catch(e) {}
}

function read(s, converter) {
var value = config.raw ? s : parseCookieValue(s);
return $.isFunction(converter) ? converter(value) : value;
}

var config = $.cookie = function (key, value, options) {

// Write

if (value !== undefined && !$.isFunction(value)) {
options = $.extend({}, config.defaults, options);

if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setTime(+t + days * 864e+5);
}

return (document.cookie = [
encode(key), '=', stringifyCookieValue(value),
options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}

// Read

var result = key ? undefined : {};

// To prevent the for loop in the first place assign an empty array
// in case there are no cookies at all. Also prevents odd result when
// calling $.cookie().
var cookies = document.cookie ? document.cookie.split('; ') : [];

for (var i = 0, l = cookies.length; i < l; i++) {
var parts = cookies[i].split('=');
var name = decode(parts.shift());
var cookie = parts.join('=');

if (key && key === name) {
// If second argument (value) is a function it's a converter...
result = read(cookie, value);
break;
}

// Prevent storing a cookie that we couldn't decode.
if (!key && (cookie = read(cookie)) !== undefined) {
result[name] = cookie;
}
}

return result;
};

config.defaults = {};

$.removeCookie = function (key, options) {
if ($.cookie(key) === undefined) {
return false;
}

// Must not alter options, thus extending a fresh object...
$.cookie(key, '', $.extend({}, options, { expires: -1 }));
return !$.cookie(key);
};

}));

/*
* Class to manipulate donation form from bottom
*/
Expand Down Expand Up @@ -631,6 +749,7 @@ window.LeykaPageMain.prototype = {
$(window).on('hashchange', function() {
self.handleHashChange();
});

},

setupNoScroll: function() {
Expand All @@ -650,11 +769,53 @@ window.LeykaPageMain.prototype = {

});
},

initForms: function() {

var self = this; var $ = self.$;

$('.leyka-pf').leykaForm();

/** Leyka success widget behavior - BEGIN */

function leyka_remembered_data(data_name) {

return $.cookie(data_name) ? $.cookie(data_name) : ''; // add local storage check...

}

var $success_forms = $('.leyka-success-form'),
donation_id = leyka_remembered_data('leyka_donation_id');
console.log('Donation ID cookie:', donation_id)

if( !donation_id ) { // Hide the success form if there are no donation ID stored...
$success_forms.hide();
} else { // ... or display them if there is one in the local storage
$success_forms.each(function(index, element) {

var $form = $(element),
$donation_id_field = $form.find('input[name="leyka_donation_id"]');

if( !$donation_id_field.val() ) {

$donation_id_field.val(donation_id);
$form.show();

}

});
}

$success_forms.on('submit', function(e){

e.preventDefault();

var $this = $(this);

});

/** Leyka success widget behavior - END */

},

inpageCardColumns: function() {
Expand All @@ -674,10 +835,12 @@ window.LeykaPageMain.prototype = {
},

setupCustomRangeControl: function() {

var self = this; var $ = self.$;

$('.amount__range_overlay').show();
$('.amount__range_custom').show();

},

handleHashChange: function() {
Expand Down
2 changes: 1 addition & 1 deletion gulpfile.js
Expand Up @@ -41,7 +41,7 @@ var changeEvent = function(evt) {

//js
gulp.task('build-js', function() {
var vendorFiles = [],
var vendorFiles = [basePaths.npm+'jquery.cookie/jquery.cookie.js'],
appFiles = [basePaths.src+'js/front/*']; //our own JS files

return gulp.src(vendorFiles.concat(appFiles)) //join them
Expand Down
20 changes: 20 additions & 0 deletions inc/leyka-ajax.php
Expand Up @@ -123,8 +123,28 @@ function leyka_get_gateway_redirect_data() {
)));
}

$donor_name = leyka_pf_get_donor_name_value();
if($donor_name && !leyka_validate_donor_name($donor_name)) {

// $error = new WP_Error('incorrect_donor_name', __('Incorrect donor name given while trying to add a donation', 'leyka'));
// $this->add_payment_form_error($error);

}

$donor_email = leyka_pf_get_donor_email_value();
if($donor_email && !leyka_validate_email($donor_email)) {

// $error = new WP_Error('incorrect_donor_email', __('Incorrect donor email given while trying to add a donation', 'leyka'));
// $this->add_payment_form_error($error);

}

$donation_id = leyka()->log_submission();

leyka_remembered_data('donor_name', $donor_name);
leyka_remembered_data('donor_email', $donor_email);
leyka_remembered_data('donation_id', $donation_id);

do_action('leyka_payment_form_submission-'.$pm[0], $pm[0], implode('-', array_slice($pm, 1)), $donation_id, $_POST);

$payment_vars = array(
Expand Down
7 changes: 2 additions & 5 deletions inc/leyka-core.php
Expand Up @@ -147,10 +147,7 @@ function leyka_get_posts(WP_Query $query) {

function leyka_successful_page_widget_template($content) {

if(
get_post()->ID != leyka_options()->opt('success_page') ||
!leyka_options()->opt('show_subscription_on_success')
) {
if(get_post()->ID != leyka_options()->opt('success_page') || !leyka_options()->opt('show_subscription_on_success')) {
return $content;
}

Expand Down Expand Up @@ -1035,7 +1032,7 @@ public function _do_payment_form_submission() {
}

$donor_email = leyka_pf_get_donor_email_value();
if($donor_name && !leyka_validate_email($donor_email)) {
if($donor_email && !leyka_validate_email($donor_email)) {

$error = new WP_Error('incorrect_donor_email', __('Incorrect donor email given while trying to add a donation', 'leyka'));
$this->add_payment_form_error($error);
Expand Down
4 changes: 1 addition & 3 deletions inc/leyka-functions.php
Expand Up @@ -933,10 +933,8 @@ function leyka_get_donations_archive_url($campaign_id = false) {
}

} else {

$donations_permalink = get_option('permalink-structure') ?
home_url('/donations/') : home_url('?post_type='.Leyka_Donation_Management::$post_type);

}

return $donations_permalink;
Expand All @@ -948,7 +946,7 @@ function leyka_remembered_data($name, $value = null) {
$name = stripos($name, 'leyka_') === false ? 'leyka_'.$name : $name;

if($value) {
return setcookie('leyka_donation_id', trim($value), 0, '', '', true);
return setcookie($name, trim($value), time()+60*60, COOKIEPATH, COOKIE_DOMAIN, false); /** @todo Last argument is for HTTPS-only */
} else {
return empty($_COOKIE[$name]) ? '' : trim($_COOKIE[$name]);
}
Expand Down
2 changes: 1 addition & 1 deletion inc/leyka-options-meta.php
Expand Up @@ -624,7 +624,7 @@
'length' => '', // For text fields
'list_entries' => array(
'during-donation' => __('As a step during donation process', 'leyka'),
'thank-you-page' => __('On a payment success page', 'leyka'),
'success-page' => __('On a payment success page', 'leyka'),
'never' => __("Don't display the fields", 'leyka'),
),
'validation_rules' => array(), // List of regexp?..
Expand Down
3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -37,7 +37,8 @@
"main-bower-files": "^2.9.0",
"node-bourbon": "^4.2.3",
"relative-path": "^1.0.0",
"run-sequence": "^1.1.2"
"run-sequence": "^1.1.2",
"jquery.cookie": "^1.4.1"
},
"dependencies": {}
}
49 changes: 47 additions & 2 deletions src/js/front/page-main.js
Expand Up @@ -42,6 +42,7 @@ window.LeykaPageMain.prototype = {
$(window).on('hashchange', function() {
self.handleHashChange();
});

},

setupNoScroll: function() {
Expand All @@ -61,11 +62,53 @@ window.LeykaPageMain.prototype = {

});
},

initForms: function() {

var self = this; var $ = self.$;

$('.leyka-pf').leykaForm();

/** Leyka success widget behavior - BEGIN */

function leyka_remembered_data(data_name) {

return $.cookie(data_name) ? $.cookie(data_name) : ''; // add local storage check...

}

var $success_forms = $('.leyka-success-form'),
donation_id = leyka_remembered_data('leyka_donation_id');
console.log('Donation ID cookie:', donation_id)

if( !donation_id ) { // Hide the success form if there are no donation ID stored...
$success_forms.hide();
} else { // ... or display them if there is one in the local storage
$success_forms.each(function(index, element) {

var $form = $(element),
$donation_id_field = $form.find('input[name="leyka_donation_id"]');

if( !$donation_id_field.val() ) {

$donation_id_field.val(donation_id);
$form.show();

}

});
}

$success_forms.on('submit', function(e){

e.preventDefault();

var $this = $(this);

});

/** Leyka success widget behavior - END */

},

inpageCardColumns: function() {
Expand All @@ -85,10 +128,12 @@ window.LeykaPageMain.prototype = {
},

setupCustomRangeControl: function() {

var self = this; var $ = self.$;

$('.amount__range_overlay').show();
$('.amount__range_custom').show();

},

handleHashChange: function() {
Expand Down

0 comments on commit 7a35b2a

Please sign in to comment.