From 9977031a5d713067da3e870e381353961972a47f Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 8 Apr 2013 18:43:07 +0100 Subject: [PATCH] Nonces cannot be used on all pages with cache - removed them for add to cart actions + fixed caching for hiding the cart --- assets/js/frontend/add-to-cart.js | 1 - assets/js/frontend/add-to-cart.min.js | 2 +- assets/js/frontend/cart-fragments.js | 6 ++++++ assets/js/frontend/cart-fragments.min.js | 2 +- classes/class-wc-session-handler.php | 2 +- classes/widgets/class-wc-widget-cart.php | 10 ---------- woocommerce-ajax.php | 12 ++++-------- woocommerce-functions.php | 4 ++-- woocommerce.php | 2 ++ 9 files changed, 17 insertions(+), 24 deletions(-) diff --git a/assets/js/frontend/add-to-cart.js b/assets/js/frontend/add-to-cart.js index e02b29c3603..54127aaf635 100644 --- a/assets/js/frontend/add-to-cart.js +++ b/assets/js/frontend/add-to-cart.js @@ -17,7 +17,6 @@ jQuery(document).ready(function($) { action: 'woocommerce_add_to_cart', product_id: $thisbutton.attr('data-product_id'), quantity: $thisbutton.attr('data-quantity'), - security: woocommerce_params.add_to_cart_nonce }; // Trigger event diff --git a/assets/js/frontend/add-to-cart.min.js b/assets/js/frontend/add-to-cart.min.js index 789a3e8a13f..4c58bb4eb44 100644 --- a/assets/js/frontend/add-to-cart.min.js +++ b/assets/js/frontend/add-to-cart.min.js @@ -1 +1 @@ -jQuery(document).ready(function(e){e(document).on("click",".add_to_cart_button",function(){var t=e(this);if(t.is(".product_type_simple, .product_type_downloadable, .product_type_virtual")){if(!t.attr("data-product_id"))return!0;t.removeClass("added");t.addClass("loading");var n={action:"woocommerce_add_to_cart",product_id:t.attr("data-product_id"),quantity:t.attr("data-quantity"),security:woocommerce_params.add_to_cart_nonce};e("body").trigger("adding_to_cart",[t,n]);e.post(woocommerce_params.ajax_url,n,function(n){if(!n)return;var r=window.location.toString();r=r.replace("add-to-cart","added-to-cart");if(n.error&&n.product_url){window.location=n.product_url;return}if(woocommerce_params.cart_redirect_after_add=="yes"){window.location=woocommerce_params.cart_url;return}t.removeClass("loading");fragments=n.fragments;cart_hash=n.cart_hash;fragments&&e.each(fragments,function(t,n){e(t).addClass("updating")});e(".shop_table.cart, .updating, .cart_totals").fadeTo("400","0.6").block({message:null,overlayCSS:{background:"transparent url("+woocommerce_params.ajax_loader_url+") no-repeat center",backgroundSize:"16px 16px",opacity:.6}});t.parent().find(".added_to_cart").size()==0&&t.addClass("added").after(' '+woocommerce_params.i18n_view_cart+"");fragments&&e.each(fragments,function(t,n){e(t).replaceWith(n)});e(".widget_shopping_cart, .updating").stop(!0).css("opacity","1").unblock();e(".shop_table.cart").load(r+" .shop_table.cart:eq(0) > *",function(){e("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").addClass("buttons_added").append('').prepend('');e(".shop_table.cart").stop(!0).css("opacity","1").unblock();e("body").trigger("cart_page_refreshed")});e(".cart_totals").load(r+" .cart_totals:eq(0) > *",function(){e(".cart_totals").stop(!0).css("opacity","1").unblock()});e("body").trigger("added_to_cart",[fragments,cart_hash])});return!1}return!0})}); \ No newline at end of file +jQuery(document).ready(function(e){e(document).on("click",".add_to_cart_button",function(){var t=e(this);if(t.is(".product_type_simple, .product_type_downloadable, .product_type_virtual")){if(!t.attr("data-product_id"))return!0;t.removeClass("added");t.addClass("loading");var n={action:"woocommerce_add_to_cart",product_id:t.attr("data-product_id"),quantity:t.attr("data-quantity")};e("body").trigger("adding_to_cart",[t,n]);e.post(woocommerce_params.ajax_url,n,function(n){if(!n)return;var r=window.location.toString();r=r.replace("add-to-cart","added-to-cart");if(n.error&&n.product_url){window.location=n.product_url;return}if(woocommerce_params.cart_redirect_after_add=="yes"){window.location=woocommerce_params.cart_url;return}t.removeClass("loading");fragments=n.fragments;cart_hash=n.cart_hash;fragments&&e.each(fragments,function(t,n){e(t).addClass("updating")});e(".shop_table.cart, .updating, .cart_totals").fadeTo("400","0.6").block({message:null,overlayCSS:{background:"transparent url("+woocommerce_params.ajax_loader_url+") no-repeat center",backgroundSize:"16px 16px",opacity:.6}});t.parent().find(".added_to_cart").size()==0&&t.addClass("added").after(' '+woocommerce_params.i18n_view_cart+"");fragments&&e.each(fragments,function(t,n){e(t).replaceWith(n)});e(".widget_shopping_cart, .updating").stop(!0).css("opacity","1").unblock();e(".shop_table.cart").load(r+" .shop_table.cart:eq(0) > *",function(){e("div.quantity:not(.buttons_added), td.quantity:not(.buttons_added)").addClass("buttons_added").append('').prepend('');e(".shop_table.cart").stop(!0).css("opacity","1").unblock();e("body").trigger("cart_page_refreshed")});e(".cart_totals").load(r+" .cart_totals:eq(0) > *",function(){e(".cart_totals").stop(!0).css("opacity","1").unblock()});e("body").trigger("added_to_cart",[fragments,cart_hash])});return!1}return!0})}); \ No newline at end of file diff --git a/assets/js/frontend/cart-fragments.js b/assets/js/frontend/cart-fragments.js index 5bc3ca1c804..b0304045c9d 100644 --- a/assets/js/frontend/cart-fragments.js +++ b/assets/js/frontend/cart-fragments.js @@ -52,4 +52,10 @@ jQuery(document).ready(function($) { $.ajax( $fragment_refresh ); } + /* Cart hiding */ + if ( $.cookie( "woocommerce_items_in_cart" ) > 0 ) + jQuery('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').show(); + else + jQuery('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').hide(); + }); \ No newline at end of file diff --git a/assets/js/frontend/cart-fragments.min.js b/assets/js/frontend/cart-fragments.min.js index 577903490fe..eef077a8721 100644 --- a/assets/js/frontend/cart-fragments.min.js +++ b/assets/js/frontend/cart-fragments.min.js @@ -1 +1 @@ -jQuery(document).ready(function(e){$supports_html5_storage="sessionStorage"in window&&window.sessionStorage!==null;$fragment_refresh={url:woocommerce_params.ajax_url,type:"POST",data:{action:"woocommerce_get_refreshed_fragments"},success:function(t){if(t&&t.fragments){e.each(t.fragments,function(t,n){e(t).replaceWith(n)});if($supports_html5_storage){sessionStorage.setItem("wc_fragments",JSON.stringify(t.fragments));sessionStorage.setItem("wc_cart_hash",t.cart_hash)}}}};if($supports_html5_storage){e("body").bind("added_to_cart",function(e,t,n){sessionStorage.setItem("wc_fragments",JSON.stringify(t));sessionStorage.setItem("wc_cart_hash",n)});try{var t=e.parseJSON(sessionStorage.getItem("wc_fragments")),n=sessionStorage.getItem("wc_cart_hash");if(!t||!t["div.widget_shopping_cart_content"]||n!=e.cookie("woocommerce_cart_hash"))throw"No fragment";e.each(t,function(t,n){e(t).replaceWith(n)})}catch(r){e.ajax($fragment_refresh)}}else e.ajax($fragment_refresh)}); \ No newline at end of file +jQuery(document).ready(function(e){$supports_html5_storage="sessionStorage"in window&&window.sessionStorage!==null;$fragment_refresh={url:woocommerce_params.ajax_url,type:"POST",data:{action:"woocommerce_get_refreshed_fragments"},success:function(t){if(t&&t.fragments){e.each(t.fragments,function(t,n){e(t).replaceWith(n)});if($supports_html5_storage){sessionStorage.setItem("wc_fragments",JSON.stringify(t.fragments));sessionStorage.setItem("wc_cart_hash",t.cart_hash)}}}};if($supports_html5_storage){e("body").bind("added_to_cart",function(e,t,n){sessionStorage.setItem("wc_fragments",JSON.stringify(t));sessionStorage.setItem("wc_cart_hash",n)});try{var t=e.parseJSON(sessionStorage.getItem("wc_fragments")),n=sessionStorage.getItem("wc_cart_hash");if(!t||!t["div.widget_shopping_cart_content"]||n!=e.cookie("woocommerce_cart_hash"))throw"No fragment";e.each(t,function(t,n){e(t).replaceWith(n)})}catch(r){e.ajax($fragment_refresh)}}else e.ajax($fragment_refresh);e.cookie("woocommerce_items_in_cart")>0?jQuery(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").show():jQuery(".hide_cart_widget_if_empty").closest(".widget_shopping_cart").hide()}); \ No newline at end of file diff --git a/classes/class-wc-session-handler.php b/classes/class-wc-session-handler.php index 12ca58929c3..1b1f69f200a 100644 --- a/classes/class-wc-session-handler.php +++ b/classes/class-wc-session-handler.php @@ -138,7 +138,7 @@ class WC_Session_Handler extends WC_Session { if ( false === get_option( $session_option ) ) { add_option( $session_option, $this->_data ); // No autoload - W3TC compat - add_option( $session_expiry_option, $this->_session_expiration, '', 'no' ); + add_option( $session_expiry_option, $this->_session_expiration ); } else { update_option( $session_option, $this->_data ); } diff --git a/classes/widgets/class-wc-widget-cart.php b/classes/widgets/class-wc-widget-cart.php index bccd3121139..ebd0b303184 100644 --- a/classes/widgets/class-wc-widget-cart.php +++ b/classes/widgets/class-wc-widget-cart.php @@ -76,16 +76,6 @@ class WC_Widget_Cart extends WP_Widget { echo ''; echo $after_widget; - - if ( $hide_if_empty && sizeof( $woocommerce->cart->get_cart() ) == 0 ) { - $woocommerce->add_inline_js( " - jQuery('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').hide(); - - jQuery('body').bind('adding_to_cart', function(){ - jQuery('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').fadeIn(); - }); - " ); - } } diff --git a/woocommerce-ajax.php b/woocommerce-ajax.php index 71cafbc465d..ebc504641aa 100644 --- a/woocommerce-ajax.php +++ b/woocommerce-ajax.php @@ -179,15 +179,11 @@ add_action('wp_ajax_nopriv_woocommerce_update_order_review', 'woocommerce_ajax_u * @return void */ function woocommerce_ajax_add_to_cart() { - global $woocommerce; - check_ajax_referer( 'add-to-cart', 'security' ); - - $product_id = apply_filters('woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) ); - $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] ); - - $passed_validation = apply_filters('woocommerce_add_to_cart_validation', true, $product_id, $quantity ); + $product_id = apply_filters( 'woocommerce_add_to_cart_product_id', absint( $_POST['product_id'] ) ); + $quantity = empty( $_POST['quantity'] ) ? 1 : apply_filters( 'woocommerce_stock_amount', $_POST['quantity'] ); + $passed_validation = apply_filters( 'woocommerce_add_to_cart_validation', true, $product_id, $quantity ); if ( $passed_validation && $woocommerce->cart->add_to_cart( $product_id, $quantity ) ) { @@ -208,7 +204,7 @@ function woocommerce_ajax_add_to_cart() { // If there was an error adding to the cart, redirect to the product page to show any errors $data = array( 'error' => true, - 'product_url' => apply_filters('woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id) + 'product_url' => apply_filters( 'woocommerce_cart_redirect_after_error', get_permalink( $product_id ), $product_id ) ); $woocommerce->set_messages(); diff --git a/woocommerce-functions.php b/woocommerce-functions.php index e004716e3c5..5c53e74c3a6 100644 --- a/woocommerce-functions.php +++ b/woocommerce-functions.php @@ -1041,7 +1041,7 @@ function woocommerce_download_product() { * handles the various permutations. */ $scheme = parse_url( $file_path, PHP_URL_SCHEME ); - + if ( $scheme ) { $site_url = set_url_scheme( site_url( '' ), $scheme ); } else { @@ -1107,7 +1107,7 @@ function woocommerce_download_product() { // IE bug prevents download via SSL when Cache Control and Pragma no-cache headers set. header( 'Expires: Wed, 11 Jan 1984 05:00:00 GMT' ); header( 'Cache-Control: private' ); - } else { + } else { nocache_headers(); } diff --git a/woocommerce.php b/woocommerce.php index 7e82d02e4c7..a26dff932e7 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -1847,6 +1847,8 @@ class Woocommerce { public function nocache() { if ( ! defined( 'DONOTCACHEPAGE' ) ) define( "DONOTCACHEPAGE", "true" ); + + nocache_headers(); }