WordPress.org

Ready to get started?Download WordPress

Forums

eShop
[resolved] BUG: The javascript for eShop is not defined properly. (14 posts)

  1. trusktr
    Member
    Posted 2 years ago #

    There is some javascript that eShop adds to the page that looks like this:

    <script type="text/javascript">
    //<![CDATA[
    jQuery(document).ready(function($){
    	$('.addtocart').submit(function(){
    		var Id =$(this).attr("id");
    		var data = {action: 'eshop_special_action',post:$('#'+Id).serialize() };
    		$.post("http://savageworkouts.com/wp/wp-admin/admin-ajax.php", data,
    			function(response){
    			$('#'+Id +" .eshopajax").insertAfter(this).fadeIn(100).html(response).fadeOut(3000);
    			setTimeout (cleareshopCart,1000);
    			setTimeout (doeshopRequest,750);
    			setTimeout (cleareshopRequest,3000);
    		});
    		function doeshopRequest(){
    			var tdata = {action: 'eshop_cart'};
    			$.post("http://savageworkouts.com/wp/wp-admin/admin-ajax.php", tdata,
    			function(response){
    				$(".ajaxcart").insertAfter(this).fadeOut(50).html(response).fadeIn(700);
    			});
    		}
    		function cleareshopRequest(){
    			$(".eshopajax").empty();
    		}
    		function cleareshopCart(){
    			$(".ajaxcart").insertAfter();
    		}
    		return false;
    	});
    });
    //]]>
    </script>

    This code is improperly defined. It doesn't follow WordPress standards. How do I know? I installed a plugin called "JavaScript to Footer" which moves all properly defined scripts to the "footer" hook location. All the scripts on my page get moved except for that eShop code.

    Can you please fix?

    http://wordpress.org/extend/plugins/eshop/

  2. trusktr
    Member
    Posted 2 years ago #

    For example, you need to do something like this:

    // activate modified Shutter reloaded if not use the Shutter plugin
    <?php
    	if ( ($this->options['thumbEffect'] == "shutter") && !function_exists('srel_makeshutter') ) {
    		wp_register_script('shutter', NGGALLERY_URLPATH .'shutter/shutter-reloaded.js', false ,'1.3.3');
    		wp_localize_script('shutter', 'shutterSettings', array(
    					'msgLoading' => __('L O A D I N Ghello', 'nggallery'),
    					'msgClose' => __('Click to Close', 'nggallery'),
    					'imageCount' => '1'
    		) );
    		wp_enqueue_script( 'shutter' );
    	}
    ?>

    You must use the wp_enqueue_script() function.

  3. trusktr
    Member
    Posted 2 years ago #

    Why not do something like

    <script src="eshop_action_javascript.php?addfadein=100&addfadeout=3000&etc"></script>

    then include it with wp_enqueue_script()?

  4. esmi
    Forum Moderator
    Posted 2 years ago #

    The script you reference should not be placed in the page's footer. It simply will not work there. Secondly, please bear in mind that eShop is a pretty mature plugin (it's almost 5 years old). I agree that some of the early coding may need to be updated but with over 200 separate files and only a limited amount of available time, it's a task that has to be undertaken over a few versions. I've flagged the enqueue issue up for next version but it won't solve your issue as the script in question will still be added to the header. Move it and eShop will stop working properly.

    Finally, this is not a "bug". The plugin works just fine. It's just that WordPress itself has matured considerably since eShop was first created. Given that the plugin is currently on version 6.2.12, I think we're doing pretty well. :-)

  5. trusktr
    Member
    Posted 2 years ago #

    Hello esmi, I moved the script to the footer and it works just fine. After moving all other scripts to the footer, I changed 'wp_head' to 'wp_footer' on line 134 in eshop.php as a temporary fix. It works perfect.

    I'll make a working solution and post it in a little while.

  6. esmi
    Forum Moderator
    Posted 2 years ago #

    The issue that we're looking at if we enqueue it is how to add variables (such as the site/page url) dynamically to the script.

  7. trusktr
    Member
    Posted 2 years ago #

    Alright, I fixed it. The variables are added dynamically while still using wp_enqueue_script().

    So basically, I swapped line 134 with 135 in eshop.php, then on line 135 I changed 'wp_head' to 'wp_print_scripts' so in eshop.php we have this:
    http://pastie.org/3886667

    Then I removed the script from the eshop_action_javascript() function and put it in a seperate file called 'eshop-cart.js'. Now the eshop_action_javascript() function in public-functions.php looks like this:
    http://pastie.org/3886677

    The wp_localize_script() function writes the parameters to the HTML before the script is included on the page. So inside eshop-cart.js we use 'eshopCartParams.addfadein' (etc) to use the php-generated variables.

    The generated HTML looks like this:
    http://pastie.org/3886686

    and eshop-cart.js looks like this:
    http://pastie.org/3886689

    Now the script is properly registered and so it follows wordpress standards. It works now with plugins like "JavaScript to Footer".

    Below is a zip file containing the modified plugin. Can you checkout the differences and apply a patch to the plugin here on wordpress.org?

    http://savageworkouts.com/~/eshop.zip

  8. esmi
    Forum Moderator
    Posted 2 years ago #

    We can certainly have a look at it and maybe add it to the next release but that's not due until the end of June, I'm afraid.

  9. trusktr
    Member
    Posted 2 years ago #

    That's alright, I can use my modified version until then. ;) If my fix makes it into the source it'd be cool to know that I contributed. :D

  10. esmi
    Forum Moderator
    Posted 2 years ago #

    I've flagged this topic up again for Rich, so hopefully he'll get a chance to have a look at the patch over the next few days and get back to you. In the meantime, thanks for taking the time out to create and submit a patch.

  11. trusktr
    Member
    Posted 2 years ago #

    By the way, thanks to Otto for the article on passing PHP variables to Javascript:

    http://ottopress.com/2010/passing-parameters-from-php-to-javascripts-in-plugins/

  12. trusktr
    Member
    Posted 2 years ago #

    Sure, no prob. It benefits myself and the WordPress community at the same time.

  13. elfin
    Moderator
    Plugin Author

    Posted 2 years ago #

    won't have time to test atm, but thanks for the work on this. It is on my list of todo things for the next release now.

  14. trusktr
    Member
    Posted 2 years ago #

    Sweet. No prob. Should I post the patch somewhere? In the bug tracker?

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic

Tags