kasparsd / minit

A WordPress plugin to combine CSS and Javascript files.
GNU General Public License v2.0
286 stars 46 forks source link

jQuery/Jetpack Sharing/Minit #32

Closed ronalfy closed 9 years ago

ronalfy commented 9 years ago

I'll do my own investigating of this, but minit doesn't seem to play well with Jetpack's sharing buttons. I keep getting jQuery undefined errors, but elsewhere the script is working fine.

When I have the time, I'll investigate the problem and do a pull request if the error is on this side.

Here's a sample post if you want to see the console errors: http://www.ronalfy.com/when-the-wife-gets-a-tattoo-and-doesnt-tell-you/

ronalfy commented 9 years ago

BTW, I am using the Google Pagespeed Mod, but I disabled it for testing and have concluded that Google Pagespeed is not a factor in this case.

kasparsd commented 9 years ago

I think it is because Jetpack is adding footer scripts by adding sharing_add_footer() to wp_footer action call during the_content which then calls the relevant wp_enqueue_script() during wp_footer.

Will try to debug this locally.

kasparsd commented 9 years ago

Basically sharing_add_footer() relies on jQuery being available in the header because it prints inline JS. A quick fix would be to move

   add_action( 'wp_footer', 'sharing_add_footer' );

to something later (after wp_print_footer_scripts which is priority 20 during wp_footer)

   remove_action( 'wp_footer', 'sharing_add_footer' );
   add_action( 'wp_footer', 'sharing_add_footer', 25 );
kasparsd commented 9 years ago

I just verified that the following snipped does indeed solve the problem:

add_action( 'wp_footer', 'fix_jetpack_sharing_js', 5 );

function fix_jetpack_sharing_js() {
    remove_action( 'wp_footer', 'sharing_add_footer' );
    add_action( 'wp_footer', 'sharing_add_footer', 25 );
}
ronalfy commented 9 years ago

Thanks Kaspars! This is a good fix. It would be nice if there was a way to add inline script dependencies the way styles work. I searched trac and haven't really found anything.

ronalfy commented 9 years ago

That whole Jetpack section needs to be rewritten. Your code got me partially there. The actual counts aren't working with the code you supplied because the sharing-js handle isn't able to run.

I added an extra action to fix the problem. Thanks for the help Kaspars!

add_action( 'wp_footer', 'fix_jetpack_sharing_js', 5 );
add_action( 'wp_footer', 'fix_jetpack_sharing_enqueue', 24 );
function fix_jetpack_sharing_enqueue() {
    if ( wp_script_is( 'jquery', 'done' ) ) {
        wp_print_scripts( 'sharing-js' );   
    }   
}
function fix_jetpack_sharing_js() {
    remove_action( 'wp_footer', 'sharing_add_footer' );
    add_action( 'wp_footer', 'sharing_add_footer', 25 );
}

This would all be solved if Jetpack would properly enqueue their dependencies :D - I added a comment to your Jetpack issue that may help solve it, but I want to get a blessing before I take the time with a pull request.

ronalfy commented 9 years ago

Sorry, grr, the above code worked, but I have no idea why. I'll dig more into this later. Too tired atm :D

ronalfy commented 9 years ago

Kaspars, sorry to keep spamming you :)

This code works!

https://gist.github.com/ronalfy/3d5de0527095ff00907b