Skip to content

Instantly share code, notes, and snippets.

@jazlopez
Created March 29, 2016 01:37
Show Gist options
  • Save jazlopez/343003019f6c5f4236f0 to your computer and use it in GitHub Desktop.
Save jazlopez/343003019f6c5f4236f0 to your computer and use it in GitHub Desktop.
theme updates
$(document).ready(function(){
/**
* Proxy
* @type {string}
*/
var proxy = '/wp-content/themes/samples.theme/proxy.php?url=';
/**
* Growl errors
* @param error
*/
var handleError = function(error){
$.growl.error({ message: error});
}
/**
* IMA sanitiy checker
*/
var loadIMA = function(){
try {
if (!$.ima)
throw new Error('IMA plugin has not been loaded.');
$.ima._base = 'http://54.210.151.33/';
/**
* Attempt to register cookie
*/
$.ima.register();
}catch(e){
handleError(e.toString());
}
}
/**
* Attempt to load IMA plugin
* Required for communication between samples and IMA inventory API
*/
loadIMA();
function getParameterByName(name, url) {
if (!url) url = window.location.href;
url = url.toLowerCase(); // This is just to avoid case sensitiveness
name = name.replace(/[\[\]]/g, "\\$&").toLowerCase();// This is just to avoid case sensitiveness for query parameter name
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function adjustAds(){
var parent = $('.product-block.with-ads .ads-block').height();
var ad = $('.product-block.with-ads .responsive-square').height();
var spacing = Math.floor((parent - ad) / 2) - 12;
$('.ads-content.ads-square-column').css('padding-top' , spacing);
}
function getToken(){
var product_post_id = $('#product-post').attr('data-product-id');
var uri = 'http://54.210.151.33/api/token/' + product_post_id;
var getUrl = '/wp-content/themes/samples.theme/proxy.php?url=' + uri;
var targetUrl = $('#product-post').attr('data-url');
$.get(getUrl, function( data ) {
if(data.error == null){
window.location.href = targetUrl + '?product_token=' + data.token + '&product_post_id=' + product_post_id;
}
});
}
function decreaseCap(){
var product_post_id = $('#product-post').attr('data-product-id');
var uri = '/wp-content/themes/samples.theme/update_cap.php?product_post_id=' + product_post_id;
$.get(uri, function( data ) {
if(data.success == true){
console.log(data);
}
});
}
//Sticky nav
$('.fixedsticky').sticky(
{
topSpacing:0
}
);
$(window).resize(function(){
adjustAds();
});
$('#newsletter-form .button').click(function(e){
e.preventDefault();
var uri = '/wp-content/themes/samples.theme/inc/newsletter.php?email=' + $('#newsletter-form input[name=email]').val(); ;
var data = $('#newsletter-form').serialize();
$.getJSON( uri )
.done(function( json ) {
console.log(json);
if(json.sent == true) {
$('#newsletter-form').hide();
}
$('#newsletter-message').html('<span style="color:#FFF;font-weight:bold;">' + json.message + '</span>');
})
.fail(function( jqxhr, textStatus, error ) {
})
});
window.setTimeout(function() {
adjustAds();
}, 2500);
/**
* Attempt to redeem product post
* Validate token if present.
*
* @throw an error if token is not present or the ip address generator does not match with this ip address
*/
$('#product-post').click(function(e){
e.preventDefault();
try{
if (!$.ima)
throw new Error('IMA plugin has not been loaded. Unable to redeem this product at this time.');
if(!Cookies.get('_JWT_ID_'))
throw new Error('You are not authorized to redeem this product. Missing token.');
$.ima._call(proxy + decodeURI($.ima._base + 'api/token/' + Cookies.get('_JWT_ID_') + '?verify=t'));
// decreaseCap();
}catch(e){
handleError(e.toString());
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment