Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Better js #161

Closed
wants to merge 2 commits into from

3 participants

@loxK

No description provided.

@divergeinfinity

It can't be auto-merged after 2 months and can't be automerged, plus I'm not sure if this is actually required?

@loxK

For sure now it is too late.

But I hope you kept that on a roadmap somewhere, because Jigoshop doesn't load its scripts in a clever way (understand where and when it is needed, which is a bad practice),

The first step is to make them lighter. Replacing jQuery with its $ alias is a first step and must be done.

The second step should be to load the scripts when and where needed, that is the purpose of the script loader. That makes a difference for WP power users (like web developer). Good quality and well written plugins do not overload your webite with unneeded scripts everywhere....

Then it will be needed to separate scripts used outside Jigoshop pages (eg. widgets scripts).

@studioromeo

@loxK It's never too late, git conflicts can be worked out & often do that :)
I'm not so sure on either of your views, for starters wp_enqueue_script should be called with the wp_enqueue_scripts hook not the init as it's too early. After thinking about it..we have modernizr...which has yepnope...which means we can bring in scripts & styles on the fly & also provide resource fallbacks so we can make use of Googles AJAX librarys but still hold a local copy.

Another benefit is it keeps javascript with the javascript, I'm weird and like things like that :P

What do you guys think? Could probably do with some speed tests I guess.

@loxK

Loading at init or at wp_enqueue_script action hook is not a rule, it needs a case per case approach. Also the good practice that any good web developer is aware of, is that Javascripts should be loaded at the end of the page (wp_footer)

Concerning modernizr, I don't think it is a good idea, jigoshop should load only a minimal amount of packed JS. But if it doesn't increase pages loading, why not, but I doubt it is the case. I understand that themes can make use of it, not Jigoshop.

Yepnote is a nice thing, I haven't looked deeply into it, but I think that it does initiate one request per script to load which slows down things.

@studioromeo

@loxK I'd be interested to hear the reasoning using init over wp_enqueue_scripts.

Agreed that as a rule we should try and keep the js down...that being said modernizr at least from my experience has become almost a standard tool (like jQuery) in creating cross platform themes. With this in mind it incurs 0 page load increase because chances are

1 it's already cached in the users browser
2 or its all ready being used by a theme.

Yep nope is included in the package along with respond.js (I think), and of course html5shiv so it has all the components needed.

Not sure what you mean by slowing down things, it doesn't inherit the resource blocking you get normally. The only thing I would say is we shouldn't use this to load external libraries, as in jQuery ect because of multiple versions being used ect

@loxK

Well wp_enqueue_scripts is the hook to use. I think its name says it all. But in some case you may want to be totally sure a script is queued before any other, in that case (the only one I see right now) init can be used.

I still think modernizr shouldn't be part of Jigoshop. I don't see any reason for that. And it is overloading websites using it because, even if cached, a request is done and because it does work on the DOM, and thus can slow down the websites using it on some low end devices. The inclusion of it should be left to themes, according to me.

Concerning Yepnote , my thought is that you plan to split Jigoshop javascripts into a lot of small js files: one for the cart, one for each widgets, one for the product list and maybe more. In that case, a request per script will be done, which is bad for page loading speed: HTTP requests are a major performance bottleneck for web page, less requests per page = faster loading time (that is why CSS sprites are good for page loading, bigger image, but less requests)

Concerning Jquery UI that Jigoshop loads from google, it is a bad idea to me. We should stick to the version bundled in wordpress. Then if one theme wants a more recent version, it is up to its developer to make use of a more recent version.

One very important thing I already talked about is loading jigoshop scripts in the footer.

@studioromeo

Ah I see, isn't that what the $deps attribute is for?

With regards to speed impact, from the man himself:

The fastest page is a blank one. Everything you add to a page slows it down.

That said, I can speak to the performance of Modernizr, which I've tuned to complete all its tests in under 20ms.
Modernizr only runs once, so it has no impact on the runtime performance. (Except if you try to print in oldIE, where
Modernizr and the html5shiv intercept that and fix it)

-Paul Irish

So yeah a little but not much. I'm just looking ahead most theme authors will probably use modernizr (hell of an assumption i know but its become standard practice to feature detect) Yepnopes included with this so for me it seems like a pretty good deal

Yepnope: Not at all, my plan was to simply swap your script loader for an asynchronous one which squeezes out more performance by removing the file blocking issue. How many scripts are split? Depends on the whats fastest doesn't it.

While I dislike the idea of using local scripts when there is a perfectly good CDN out there, WordPress wants us to use local scripts so I'm in favour.

Scripts in footer? Hell yeah!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
405 assets/js/script.js
@@ -5,10 +5,10 @@ jQuery.fn.animateHighlight = function(highlightColor, duration) {
this.stop().css("background-color", highlightBg).animate({backgroundColor: originalBg}, animateMs);
};
-jQuery(function(){
+jQuery(document).ready(function($) {
// Lightbox
- jQuery('a.zoom').fancybox({
+ $('a.zoom').fancybox({
'transitionIn' : 'elastic',
'transitionOut' : 'elastic',
'speedIn' : 600,
@@ -17,18 +17,18 @@ jQuery(function(){
});
// Star ratings
- jQuery('#rating').hide().before('<p class="stars"><span><a class="star-1" href="#">1</a><a class="star-2" href="#">2</a><a class="star-3" href="#">3</a><a class="star-4" href="#">4</a><a class="star-5" href="#">5</a></span></p>');
+ $('#rating').hide().before('<p class="stars"><span><a class="star-1" href="#">1</a><a class="star-2" href="#">2</a><a class="star-3" href="#">3</a><a class="star-4" href="#">4</a><a class="star-5" href="#">5</a></span></p>');
- jQuery('p.stars a').click(function(){
- jQuery('#rating').val(jQuery(this).text());
- jQuery('p.stars a').removeClass('active');
- jQuery(this).addClass('active');
+ $('p.stars a').click(function(){
+ $('#rating').val($(this).text());
+ $('p.stars a').removeClass('active');
+ $(this).addClass('active');
return false;
});
// Price slider
- var min_price = jQuery('.price_slider_amount #min_price').val();
- var max_price = jQuery('.price_slider_amount #max_price').val();
+ var min_price = $('.price_slider_amount #min_price').val();
+ var max_price = $('.price_slider_amount #max_price').val();
if (params.min_price) {
current_min_price = params.min_price;
@@ -42,55 +42,55 @@ jQuery(function(){
current_max_price = max_price;
}
- jQuery('.price_slider').slider({
+ $('.price_slider').slider({
range: true,
min: min_price,
max: max_price,
values: [ current_min_price, current_max_price ],
create : function( event, ui ) {
- jQuery( ".price_slider_amount span" ).html( params.currency_symbol + current_min_price + " - " + params.currency_symbol + current_max_price );
- jQuery( ".price_slider_amount #min_price" ).val(current_min_price);
- jQuery( ".price_slider_amount #max_price" ).val(current_max_price);
+ $( ".price_slider_amount span" ).html( params.currency_symbol + current_min_price + " - " + params.currency_symbol + current_max_price );
+ $( ".price_slider_amount #min_price" ).val(current_min_price);
+ $( ".price_slider_amount #max_price" ).val(current_max_price);
},
slide: function( event, ui ) {
- jQuery( ".price_slider_amount span" ).html( params.currency_symbol + ui.values[ 0 ] + " - " + params.currency_symbol + ui.values[ 1 ] );
- jQuery( "input#min_price" ).val(ui.values[ 0 ]);
- jQuery( "input#max_price" ).val(ui.values[ 1 ]);
+ $( ".price_slider_amount span" ).html( params.currency_symbol + ui.values[ 0 ] + " - " + params.currency_symbol + ui.values[ 1 ] );
+ $( "input#min_price" ).val(ui.values[ 0 ]);
+ $( "input#max_price" ).val(ui.values[ 1 ]);
}
});
// Quantity buttons
- jQuery("div.quantity, td.quantity").append('<input type="button" value="+" id="add1" class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
- jQuery(".plus").click(function()
+ $("div.quantity, td.quantity").append('<input type="button" value="+" id="add1" class="plus" />').prepend('<input type="button" value="-" id="minus1" class="minus" />');
+ $(".plus").click(function()
{
- var currentVal = parseInt(jQuery(this).prev(".qty").val());
+ var currentVal = parseInt($(this).prev(".qty").val());
if (!currentVal || currentVal=="" || currentVal == "NaN") currentVal = 0;
- jQuery(this).prev(".qty").val(currentVal + 1);
+ $(this).prev(".qty").val(currentVal + 1);
});
- jQuery(".minus").click(function()
+ $(".minus").click(function()
{
- var currentVal = parseInt(jQuery(this).next(".qty").val());
+ var currentVal = parseInt($(this).next(".qty").val());
if (currentVal == "NaN") currentVal = 0;
if (currentVal > 0)
{
- jQuery(this).next(".qty").val(currentVal - 1);
+ $(this).next(".qty").val(currentVal - 1);
}
});
/* states */
var states_json = params.countries.replace(/&quot;/g, '"');
- var states = jQuery.parseJSON( states_json );
+ var states = $.parseJSON( states_json );
- jQuery('select.country_to_state').change(function(){
+ $('select.country_to_state').change(function(){
- var country = jQuery(this).val();
- var state_box = jQuery('#' + jQuery(this).attr('rel'));
+ var country = $(this).val();
+ var state_box = $('#' + $(this).attr('rel'));
- var input_name = jQuery(state_box).attr('name');
- var input_id = jQuery(state_box).attr('id');
+ var input_name = $(state_box).attr('name');
+ var input_id = $(state_box).attr('id');
if (states[country]) {
var options = '';
@@ -98,51 +98,51 @@ jQuery(function(){
for(var index in state) {
options = options + '<option value="' + index + '">' + state[index] + '</option>';
}
- if (jQuery(state_box).is('input')) {
+ if ($(state_box).is('input')) {
// Change for select
- jQuery(state_box).replaceWith('<select name="' + input_name + '" id="' + input_id + '"><option value="">' + params.select_state_text + '</option></select>');
- state_box = jQuery('#' + jQuery(this).attr('rel'));
+ $(state_box).replaceWith('<select name="' + input_name + '" id="' + input_id + '"><option value="">' + params.select_state_text + '</option></select>');
+ state_box = $('#' + $(this).attr('rel'));
}
- jQuery(state_box).append(options);
+ $(state_box).append(options);
} else {
- if (jQuery(state_box).is('select')) {
- jQuery(state_box).replaceWith('<input type="text" placeholder="' + params.state_text + '" name="' + input_name + '" id="' + input_id + '" />');
- state_box = jQuery('#' + jQuery(this).attr('rel'));
+ if ($(state_box).is('select')) {
+ $(state_box).replaceWith('<input type="text" placeholder="' + params.state_text + '" name="' + input_name + '" id="' + input_id + '" />');
+ state_box = $('#' + $(this).attr('rel'));
}
}
}).change();
/* Tabs */
- jQuery('#tabs .panel:not(#tabs .panel)').hide();
- jQuery('#tabs li a').click(function(){
- var href = jQuery(this).attr('href');
- jQuery('#tabs li').removeClass('active');
- jQuery('div.panel').hide();
- jQuery('div' + href).show();
- jQuery(this).parent().addClass('active');
- jQuery.cookie('current_tab', href);
+ $('#tabs .panel:not(#tabs .panel)').hide();
+ $('#tabs li a').click(function(){
+ var href = $(this).attr('href');
+ $('#tabs li').removeClass('active');
+ $('div.panel').hide();
+ $('div' + href).show();
+ $(this).parent().addClass('active');
+ $.cookie('current_tab', href);
return false;
});
- if (jQuery('#tabs li.active').size()==0) {
- jQuery('#tabs li:first a').click();
+ if ($('#tabs li.active').size()==0) {
+ $('#tabs li:first a').click();
} else {
- jQuery('#tabs li.active a').click();
+ $('#tabs li.active a').click();
}
/* Shipping calculator */
- jQuery('.shipping-calculator-form').hide();
+ $('.shipping-calculator-form').hide();
- jQuery('.shipping-calculator-button').click(function() {
- jQuery('.shipping-calculator-form').slideToggle('slow', function() {
+ $('.shipping-calculator-button').click(function() {
+ $('.shipping-calculator-form').slideToggle('slow', function() {
// Animation complete.
});
});
// Stop anchors moving the viewport
- jQuery(".shipping-calculator-button").click(function() {return false;});
+ $(".shipping-calculator-button").click(function() {return false;});
/*################# VARIATIONS ###################*/
@@ -179,7 +179,7 @@ jQuery(function(){
//disable option fields that are unavaiable for current set of attributes
function update_variation_values(variations) {
- var current_attr_select = jQuery('.variations select').not('[disabled]').last();
+ var current_attr_select = $('.variations select').not('[disabled]').last();
current_attr_select.find('option:gt(0)').attr('disabled', 'disabled');
var current_attr_name = current_attr_select.attr('name');
@@ -200,50 +200,50 @@ jQuery(function(){
//show single variation details (price, stock, image)
function show_variation(variation) {
- var img = jQuery('div.images img:eq(0)');
- var link = jQuery('div.images a.zoom:eq(0)');
- var o_src = jQuery(img).attr('original-src');
- var o_link = jQuery(link).attr('original-href');
+ var img = $('div.images img:eq(0)');
+ var link = $('div.images a.zoom:eq(0)');
+ var o_src = $(img).attr('original-src');
+ var o_link = $(link).attr('original-href');
var variation_image = variation.image_src;
var variation_link = variation.image_link;
- jQuery('.single_variation').html( variation.price_html + variation.availability_html );
+ $('.single_variation').html( variation.price_html + variation.availability_html );
if (!o_src) {
- jQuery(img).attr('original-src', jQuery(img).attr('src'));
+ $(img).attr('original-src', $(img).attr('src'));
}
if (!o_link) {
- jQuery(link).attr('original-href', jQuery(link).attr('href'));
+ $(link).attr('original-href', $(link).attr('href'));
}
if (variation_image.length > 1) {
- jQuery(img).attr('src', variation_image);
- jQuery(link).attr('href', variation_link);
+ $(img).attr('src', variation_image);
+ $(link).attr('href', variation_link);
} else {
- jQuery(img).attr('src', o_src);
- jQuery(link).attr('href', o_link);
+ $(img).attr('src', o_src);
+ $(link).attr('href', o_link);
}
- jQuery('.variations_button, .single_variation').slideDown();
+ $('.variations_button, .single_variation').slideDown();
}
//when one of attributes is changed - check everything to show only valid options
function check_variations() {
- jQuery('form input[name=variation_id]').val('');
- jQuery('.single_variation').text('');
- jQuery('.variations_button, .single_variation').slideUp();
+ $('form input[name=variation_id]').val('');
+ $('.single_variation').text('');
+ $('.variations_button, .single_variation').slideUp();
var all_set = true;
var current_settings = {};
- jQuery('.variations select').each(function(){
- if (jQuery(this).val().length == 0) {
+ $('.variations select').each(function(){
+ if ($(this).val().length == 0) {
all_set = false;
}
- current_settings[jQuery(this).attr('name')] = jQuery(this).val();
+ current_settings[$(this).attr('name')] = $(this).val();
});
var matching_variations = find_matching_variations(current_settings);
@@ -251,161 +251,156 @@ jQuery(function(){
if(all_set) {
var variation = matching_variations.pop();
- jQuery('form input[name=variation_id]').val(variation.variation_id);
+ $('form input[name=variation_id]').val(variation.variation_id);
show_variation(variation);
} else {
update_variation_values(matching_variations);
}
}
- jQuery('.variations select').change(function(){
+ $('.variations select').change(function(){
//make sure that only selects before this one, and one after this are enabled
- var num = jQuery(this).data('num');
+ var num = $(this).data('num');
- if(jQuery(this).val().length > 0) {
+ if($(this).val().length > 0) {
num += 1;
}
- var selects = jQuery('.variations select');
+ var selects = $('.variations select');
selects.filter(':lt('+num+')').removeAttr('disabled');
selects.filter(':eq('+num+')').removeAttr('disabled').val('');
selects.filter(':gt('+num+')').attr('disabled', 'disabled').val('');
- check_variations(jQuery(this));
+ check_variations($(this));
});
//disable all but first select field
- jQuery('.variations select:gt(0)').attr('disabled', 'disabled');
+ $('.variations select:gt(0)').attr('disabled', 'disabled');
//numerate all selects
- jQuery.each(jQuery('.variations select'), function(i, item){
- jQuery(item).data('num', i);
+ $.each($('.variations select'), function(i, item){
+ $(item).data('num', i);
});
-});
+ if (params.is_checkout==1) {
-if (params.is_checkout==1) {
-
- var updateTimer;
-
- function update_checkout() {
-
- var method = jQuery('#shipping_method').val();
-
- var country = jQuery('#billing-country').val();
- var state = jQuery('#billing-state').val();
- var postcode = jQuery('input#billing-postcode').val();
-
- if (jQuery('#shiptobilling input').is(':checked') || jQuery('#shiptobilling input').size()==0) {
- var s_country = jQuery('#billing-country').val();
- var s_state = jQuery('#billing-state').val();
- var s_postcode = jQuery('input#billing-postcode').val();
-
- } else {
- var s_country = jQuery('#shipping-country').val();
- var s_state = jQuery('#shipping-state').val();
- var s_postcode = jQuery('input#shipping-postcode').val();
- }
-
- jQuery('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
- jQuery.ajax({
- type: 'POST',
- url: params.review_order_url,
- data: {shipping_method: method, country: country, state: state, postcode: postcode, s_country: s_country, s_state: s_state, s_postcode: s_postcode},
- success: function( code ) {
- jQuery('#order_methods, #order_review').remove();
- jQuery('#order_review_heading').after(code);
- jQuery('#order_review input[name=payment_method]:checked').click();
- },
- dataType: "html"
- });
-
- }
-
- jQuery(function(){
-
- jQuery('p.password').hide();
-
- jQuery('input.show_password').change(function(){
- jQuery('p.password').slideToggle();
- });
-
- jQuery('div.shipping-address').hide();
-
- jQuery('#shiptobilling input').change(function(){
- jQuery('div.shipping-address').hide();
- if (!jQuery(this).is(':checked')) {
- jQuery('div.shipping-address').slideDown();
- }
- }).change();
-
- if (params.option_guest_checkout=='yes') {
-
- jQuery('div.create-account').hide();
-
- jQuery('input#createaccount').change(function(){
- jQuery('div.create-account').hide();
- if (jQuery(this).is(':checked')) {
- jQuery('div.create-account').slideDown();
- }
- }).change();
-
- }
-
- jQuery('.payment_methods input.input-radio').live('click', function(){
- jQuery('div.payment_box').hide();
- if (jQuery(this).is(':checked')) {
- jQuery('div.payment_box.' + jQuery(this).attr('ID')).slideDown();
- }
- });
-
- jQuery('#order_review input[name=payment_method]:checked').click();
-
- jQuery('form.login').hide();
-
- jQuery('a.showlogin').click(function(){
- jQuery('form.login').slideToggle();
- });
-
- /* Update totals */
- jQuery('#shipping_method').live('change', function(){
- clearTimeout(updateTimer);
- update_checkout();
- }).change();
- jQuery('input#billing-country, input#billing-state, #billing-postcode, input#shipping-country, input#shipping-state, #shipping-postcode').live('keydown', function(){
- clearTimeout(updateTimer);
- updateTimer = setTimeout("update_checkout()", '1000');
- });
- jQuery('select#billing-country, select#billing-state, select#shipping-country, select#shipping-state, #shiptobilling input').live('change', function(){
- clearTimeout(updateTimer);
- update_checkout();
- });
-
- /* AJAX Form Submission */
- jQuery('form.checkout').submit(function(){
- var form = this;
- jQuery(form).block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
- jQuery.ajax({
- type: 'POST',
- url: params.checkout_url,
- data: jQuery(form).serialize(),
- success: function( code ) {
- jQuery('.jigoshop_error, .jigoshop_message').remove();
- try {
- success = jQuery.parseJSON( code );
- window.location = decodeURI(success.redirect);
- }
- catch(err) {
- jQuery(form).prepend( code );
- jQuery(form).unblock();
- jQuery.scrollTo(jQuery(form).parent(), {easing:'swing'});
- }
- },
- dataType: "html"
- });
- return false;
- });
-
- });
-
-}
+ var updateTimer;
+
+ function update_checkout() {
+
+ var method = $('#shipping_method').val();
+
+ var country = $('#billing-country').val();
+ var state = $('#billing-state').val();
+ var postcode = $('input#billing-postcode').val();
+
+ if ($('#shiptobilling input').is(':checked') || $('#shiptobilling input').size()==0) {
+ var s_country = $('#billing-country').val();
+ var s_state = $('#billing-state').val();
+ var s_postcode = $('input#billing-postcode').val();
+
+ } else {
+ var s_country = $('#shipping-country').val();
+ var s_state = $('#shipping-state').val();
+ var s_postcode = $('input#shipping-postcode').val();
+ }
+
+ $('#order_methods, #order_review').block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
+ $.ajax({
+ type: 'POST',
+ url: params.review_order_url,
+ data: {shipping_method: method, country: country, state: state, postcode: postcode, s_country: s_country, s_state: s_state, s_postcode: s_postcode},
+ success: function( code ) {
+ $('#order_methods, #order_review').remove();
+ $('#order_review_heading').after(code);
+ $('#order_review input[name=payment_method]:checked').click();
+ },
+ dataType: "html"
+ });
+
+ }
+
+ $('p.password').hide();
+
+ $('input.show_password').change(function(){
+ $('p.password').slideToggle();
+ });
+
+ $('div.shipping-address').hide();
+
+ $('#shiptobilling input').change(function(){
+ $('div.shipping-address').hide();
+ if (!$(this).is(':checked')) {
+ $('div.shipping-address').slideDown();
+ }
+ }).change();
+
+ if (params.option_guest_checkout=='yes') {
+
+ $('div.create-account').hide();
+
+ $('input#createaccount').change(function(){
+ $('div.create-account').hide();
+ if ($(this).is(':checked')) {
+ $('div.create-account').slideDown();
+ }
+ }).change();
+
+ }
+
+ $('.payment_methods input.input-radio').live('click', function(){
+ $('div.payment_box').hide();
+ if ($(this).is(':checked')) {
+ $('div.payment_box.' + $(this).attr('ID')).slideDown();
+ }
+ });
+
+ $('#order_review input[name=payment_method]:checked').click();
+
+ $('form.login').hide();
+
+ $('a.showlogin').click(function(){
+ $('form.login').slideToggle();
+ });
+
+ /* Update totals */
+ $('#shipping_method').live('change', function(){
+ clearTimeout(updateTimer);
+ update_checkout();
+ }).change();
+ $('input#billing-country, input#billing-state, #billing-postcode, input#shipping-country, input#shipping-state, #shipping-postcode').live('keydown', function(){
+ clearTimeout(updateTimer);
+ updateTimer = setTimeout("update_checkout()", '1000');
+ });
+ $('select#billing-country, select#billing-state, select#shipping-country, select#shipping-state, #shiptobilling input').live('change', function(){
+ clearTimeout(updateTimer);
+ update_checkout();
+ });
+
+ /* AJAX Form Submission */
+ $('form.checkout').submit(function(){
+ var form = this;
+ $(form).block({message: null, overlayCSS: {background: '#fff url(' + params.plugin_url + '/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6}});
+ $.ajax({
+ type: 'POST',
+ url: params.checkout_url,
+ data: $(form).serialize(),
+ success: function( code ) {
+ $('.jigoshop_error, .jigoshop_message').remove();
+ try {
+ success = $.parseJSON( code );
+ window.location = decodeURI(success.redirect);
+ }
+ catch(err) {
+ $(form).prepend( code );
+ $(form).unblock();
+ $.scrollTo($(form).parent(), {easing:'swing'});
+ }
+ },
+ dataType: "html"
+ });
+ return false;
+ });
+
+ }
+});
View
105 classes/jigoshop_script_loader.php
@@ -0,0 +1,105 @@
+<?php
+class jigoshop_script_loader {
+
+ private static $_instance;
+
+ private $scripts_to_load = array();
+
+ private $scripts_data = array();
+ private $scripts_data_names = array();
+
+ private $inline_scripts = array();
+
+ /** constructor */
+ private function __construct () {
+
+ // will be used when we will introduce dev/compressed scripts
+ $suffix = defined('SCRIPT_DEBUG') && SCRIPT_DEBUG ? '.dev' : '';
+
+ wp_register_script( 'jigoshop_frontend', jigoshop::plugin_url() . '/assets/js/jigoshop_frontend.js', array('jquery'), '1.0' );
+ wp_register_script( 'jigoshop_script', jigoshop::plugin_url() . '/assets/js/script.js', array('jquery'), '1.0' );
+ wp_register_script( 'fancybox', jigoshop::plugin_url() . '/assets/js/jquery.fancybox-1.3.4.pack.js', array('jquery'), '1.0' );
+
+ /* @TODO: we shouldn't include external scripts */
+ wp_register_script( 'jqueryui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js', array('jquery'), '1.0' );
+
+ add_action( 'wp_footer', array($this, 'wp_footer') );
+
+ }
+
+ public static function instance () {
+ if (!isset(self::$_instance)) {
+ $c = __CLASS__;
+ self::$_instance = new $c;
+ }
+ return self::$_instance;
+ }
+
+ /**
+ * Static call of load_script class
+ *
+ * @param string $handle The script handle
+ * @param array $data The optional l10n data
+ */
+ static function load ( $handle, $data=array(), $data_object_name = null ) {
+ return self::instance()->load_script($handle, $data, $data_object_name);
+ }
+
+ /**
+ * Add a javascript to load in footer
+ *
+ * @param string $handle The script handle
+ * @param array $data The optional l10n data
+ */
+ public function load_script ( $handle, $data=array(), $data_object_name = null ) {
+
+ if( ! in_array($handle, $this->scripts_to_load) ) $this->scripts_to_load[] = $handle;
+
+ if( ! empty($data) ) {
+
+ if( isset($this->scripts_data[$handle]) ) $this->scripts_data[$handle] = array_merge($this->scripts_data[$handle], (array) $data);
+ else $this->scripts_data[$handle] = (array) $data;
+
+ if( $data_object_name && ! isset($this->scripts_data_names[$handle]) ) $this->scripts_data_names[$handle] = $data_object_name;
+ }
+
+ return $this;
+ }
+
+ public function add_inline ( $script ) {
+
+ $this->inline_scripts[] = $script;
+ return $this;
+
+ }
+
+ /**
+ * Footer action to load scripts
+ */
+ public function wp_footer () {
+
+ foreach ( $this->scripts_to_load as $script_handle ) {
+
+ if( isset($this->scripts_data[$script_handle]) ) wp_localize_script($script_handle, !empty($this->scripts_data_names[$script_handle]) ? $this->scripts_data_names[$script_handle] : $script_handle, $this->scripts_data[$script_handle] );
+
+ wp_print_scripts( $script_handle );
+
+ }
+
+ if( sizeof($this->inline_scripts) ) {
+?>
+<script type="text/javascript">
+/* <![CDATA[ */
+<?php
+
+ foreach ($this->inline_scripts as $script) echo $script;
+
+?>
+/* ]]> */
+</script>
+<?php
+
+ }
+
+ }
+}
View
21 jigoshop.php
@@ -329,17 +329,6 @@ function jigoshop_admin_scripts() {
function jigoshop_frontend_scripts() {
- wp_register_script( 'jigoshop_frontend', jigoshop::plugin_url() . '/assets/js/jigoshop_frontend.js', 'jquery', '1.0' );
- wp_register_script( 'jigoshop_script', jigoshop::plugin_url() . '/assets/js/script.js', 'jquery', '1.0' );
- wp_register_script( 'fancybox', jigoshop::plugin_url() . '/assets/js/jquery.fancybox-1.3.4.pack.js', 'jquery', '1.0' );
- wp_register_script( 'jqueryui', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.13/jquery-ui.min.js', 'jquery', '1.0' );
-
- wp_enqueue_script('jquery');
- wp_enqueue_script('jqueryui');
- wp_enqueue_script('jigoshop_frontend');
- wp_enqueue_script('fancybox');
- wp_enqueue_script('jigoshop_script');
-
/* Script.js variables */
$params = array(
'currency_symbol' => get_jigoshop_currency_symbol(),
@@ -367,7 +356,15 @@ function jigoshop_frontend_scripts() {
$params['is_checkout'] = 0;
endif;
- wp_localize_script( 'jigoshop_script', 'params', $params );
+ include_once 'classes/jigoshop_script_loader.php';
+ $loader = jigoshop_script_loader::instance();
+
+ jigoshop_script_loader::instance() ->load('jqueryui')
+ ->load('jigoshop_frontend')
+ ->load('fancybox')
+ ->load('jigoshop_script', $params, 'params');
+
+
}
add_action('template_redirect', 'jigoshop_frontend_scripts');
View
27 templates/single-product-reviews.php
@@ -113,19 +113,18 @@
echo '</div></div>';
-?><div class="clear"></div></div>
-<script type="text/javascript">
-/* <![CDATA[ */
- jQuery(function(){
- jQuery('#review_form_wrapper').hide();
- jQuery('a.show_review_form').fancybox({
- 'transitionIn' : 'elastic',
- 'transitionOut' : 'elastic',
- 'speedIn' : 600,
- 'speedOut' : 200,
- 'overlayShow' : true
- });
+ jigoshop_script_loader::instance()->add_inline ( "
+jQuery(function($){
+ $('#review_form_wrapper').hide();
+ $('a.show_review_form').fancybox({
+ 'transitionIn' : 'elastic',
+ 'transitionOut' : 'elastic',
+ 'speedIn' : 600,
+ 'speedOut' : 200,
+ 'overlayShow' : true
});
-/* ]]> */
-</script>
+});
+" );
+
+?><div class="clear"></div></div>
<?php endif; ?>
Something went wrong with that request. Please try again.