| 1 |
CHARITABLE = window.CHARITABLE || {}; |
| 2 |
|
| 3 |
( function( exports ) { |
| 4 |
var Sessions = function() { |
| 5 |
if ( CHARITABLE_SESSION.disable_cookie ) { |
| 6 |
Cookies.remove( CHARITABLE_SESSION.cookie_name ); |
| 7 |
return; |
| 8 |
} |
| 9 |
|
| 10 |
this.session_id = Cookies.get( CHARITABLE_SESSION.cookie_name ); |
| 11 |
|
| 12 |
// Set a cookie if none exists. |
| 13 |
if ( ! this.session_id ) { |
| 14 |
set_cookie(); |
| 15 |
} |
| 16 |
|
| 17 |
// If a session ID is set and it matches the one we received, proceed no further. |
| 18 |
if ( this.session_id && ( this.session_id === CHARITABLE_SESSION.id ) ) { |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
exports.content_loading = true; |
| 23 |
|
| 24 |
if (document.readyState != 'loading') { |
| 25 |
init(); |
| 26 |
exports.content_loading = false; |
| 27 |
} else if (document.addEventListener) { |
| 28 |
document.addEventListener('DOMContentLoaded', init); |
| 29 |
exports.content_loading = false; |
| 30 |
} else { |
| 31 |
document.attachEvent('onreadystatechange', function() { |
| 32 |
document.readyState != 'loading' && init(); |
| 33 |
exports.content_loading = false; |
| 34 |
}); |
| 35 |
} |
| 36 |
|
| 37 |
// Init. |
| 38 |
function init() { |
| 39 |
var elements = document.querySelectorAll('.charitable-session-content'); |
| 40 |
var data = 'action=charitable_get_session_content'; |
| 41 |
var element, template, args, key, value, i; |
| 42 |
|
| 43 |
if ( ! elements.length ) { |
| 44 |
return; |
| 45 |
} |
| 46 |
|
| 47 |
for (i = 0; i < elements.length; i++) { |
| 48 |
element = elements[i], |
| 49 |
template = element.getAttribute('data-template'), |
| 50 |
args = JSON.parse(element.getAttribute('data-args')), |
| 51 |
|
| 52 |
// Append template arguments to the data string. |
| 53 |
data += '&templates[' + i + '][template]=' + template; |
| 54 |
for (key in args) { |
| 55 |
data += '&templates[' + i + '][' + key + ']=' + args[key]; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
var request = new XMLHttpRequest(); |
| 60 |
request.open('POST', CHARITABLE_SESSION.ajaxurl, true); |
| 61 |
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); |
| 62 |
request.onreadystatechange = function() { |
| 63 |
if (this.readyState === 4 && (this.status >= 200 && this.status < 400)) { |
| 64 |
var response = JSON.parse( this.response ); |
| 65 |
if (!response.success) { |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
for (i = 0; i < response.data.length; i += 1) { |
| 70 |
if (!response.data[i].length) { |
| 71 |
continue; |
| 72 |
} |
| 73 |
|
| 74 |
elements[i].innerHTML = response.data[i]; |
| 75 |
elements[i].style.display = 'block'; |
| 76 |
} |
| 77 |
} |
| 78 |
}; |
| 79 |
request.send(data); |
| 80 |
request = null; |
| 81 |
} |
| 82 |
|
| 83 |
// Set cookie. |
| 84 |
function set_cookie() { |
| 85 |
Cookies.set( CHARITABLE_SESSION.cookie_name, |
| 86 |
CHARITABLE_SESSION.generated_id + '||' + CHARITABLE_SESSION.expiration + '||' + CHARITABLE_SESSION.expiration_variant, |
| 87 |
{ |
| 88 |
expires: new Date( new Date().getTime() + ( parseInt( CHARITABLE_SESSION.expiration ) * 1000 ) ), |
| 89 |
path: CHARITABLE_SESSION.cookie_path, |
| 90 |
domain: CHARITABLE_SESSION.cookie_domain, |
| 91 |
secure: CHARITABLE_SESSION.secure |
| 92 |
} |
| 93 |
); |
| 94 |
|
| 95 |
return CHARITABLE_SESSION.generated_id + '||' + CHARITABLE_SESSION.expiration + '||' + CHARITABLE_SESSION.expiration_variant; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
exports.Sessions = Sessions(); |
| 100 |
|
| 101 |
})( CHARITABLE ); |