1/* 2 * This file is part of the hyyan/woo-poly-integration plugin. 3 * (c) Hyyan Abo Fakher <[email protected]> 4 * 5 * For the full copyright and license information, please view the LICENSE 6 * file that was distributed with this source code. 7 * 8 * Modified WooCommerce cart-fragments.js script to break HTML5 fragment caching. 9 * Useful when switching languages. Adds support new Cart page ajax. 10 **/ 11 12/* global wc_cart_fragments_params */ 13jQuery(function ($) { 14 15 // wc_cart_fragments_params is required to continue, ensure the object exists 16 if (typeof wc_cart_fragments_params === 'undefined') { 17 return false; 18 } 19 20 /* Storage Handling */ 21 var $supports_html5_storage; 22 var cart_hash_key = wc_cart_fragments_params.ajax_url.toString() + '-wc_cart_hash'; 23 24 try { 25 $supports_html5_storage = ('sessionStorage' in window && window.sessionStorage !== null); 26 window.sessionStorage.setItem('wc', 'test'); 27 window.sessionStorage.removeItem('wc'); 28 window.localStorage.setItem('wc', 'test'); 29 window.localStorage.removeItem('wc'); 30 } catch (err) { 31 $supports_html5_storage = false; 32 } 33 34 /* Cart session creation time to base expiration on */ 35 function set_cart_creation_timestamp() { 36 if ($supports_html5_storage) { 37 sessionStorage.setItem('wc_cart_created', (new Date()).getTime()); 38 } 39 } 40 41 /* Set the cart hash in both session and local storage */ 42 function set_cart_hash(cart_hash) { 43 if ($supports_html5_storage) { 44 localStorage.setItem(cart_hash_key, cart_hash); 45 sessionStorage.setItem(cart_hash_key, cart_hash); 46 } 47 } 48 49 /* Get current Polylang language */ 50 function get_pll_language() { 51 var pll_lang = $.cookie('pll_language'); 52 53 if (pll_lang === null || pll_lang === undefined || pll_lang === '') { 54 pll_lang = ''; 55 } 56 57 return pll_lang; 58 } 59 60 var $fragment_refresh = { 61 url: wc_cart_fragments_params.wc_ajax_url.toString().replace('%%endpoint%%', 'get_refreshed_fragments'), 62 type: 'POST', 63 success: function (data) { 64 if (data && data.fragments) { 65 66 $.each(data.fragments, function (key, value) { 67 $(key).replaceWith(value); 68 }); 69 70 if ($supports_html5_storage) { 71 sessionStorage.setItem(wc_cart_fragments_params.fragment_name, JSON.stringify(data.fragments)); 72 set_cart_hash(data.cart_hash); 73 74 if (data.cart_hash) { 75 set_cart_creation_timestamp(); 76 } 77 } 78 79 $(document.body).trigger('wc_fragments_refreshed'); 80 } 81 } 82 }; 83 84 /* Named callback for refreshing cart fragment */ 85 function refresh_cart_fragment() { 86 $.ajax($fragment_refresh); 87 } 88 89 /* Cart Handling */ 90 if ($supports_html5_storage) { 91 92 var cart_timeout = null, 93 day_in_ms = (24 * 60 * 60 * 1000); 94 95 $(document.body).bind('wc_fragment_refresh updated_wc_div', function () { 96 refresh_cart_fragment(); 97 }); 98 99 $(document.body).bind('added_to_cart', function (event, fragments, cart_hash) { 100 var prev_cart_hash = sessionStorage.getItem(cart_hash_key); 101 102 if (prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '') { 103 set_cart_creation_timestamp(); 104 } 105 106 sessionStorage.setItem(wc_cart_fragments_params.fragment_name, JSON.stringify(fragments)); 107 set_cart_hash(cart_hash); 108 }); 109 110 $(document.body).bind('wc_fragments_refreshed', function () { 111 clearTimeout(cart_timeout); 112 cart_timeout = setTimeout(refresh_cart_fragment, day_in_ms); 113 }); 114 115 // Refresh when storage changes in another tab 116 $(window).on('storage onstorage', function (e) { 117 if (cart_hash_key === e.originalEvent.key && localStorage.getItem(cart_hash_key) !== sessionStorage.getItem(cart_hash_key)) { 118 refresh_cart_fragment(); 119 } 120 }); 121 122 try { 123 var wc_fragments = $.parseJSON(sessionStorage.getItem(wc_cart_fragments_params.fragment_name)), 124 cart_hash = sessionStorage.getItem(cart_hash_key), 125 cookie_hash = $.cookie('woocommerce_cart_hash'), 126 cart_created = sessionStorage.getItem('wc_cart_created'); 127 128 if (cart_hash === null || cart_hash === undefined || cart_hash === '') { 129 cart_hash = ''; 130 } 131 132 if (cookie_hash === null || cookie_hash === undefined || cookie_hash === '') { 133 cookie_hash = ''; 134 } 135 136 if (cart_hash && (cart_created === null || cart_created === undefined || cart_created === '')) { 137 throw 'No cart_created'; 138 } 139 140 if (cart_created) { 141 var cart_expiration = ((1 * cart_created) + day_in_ms), 142 timestamp_now = (new Date()).getTime(); 143 if (cart_expiration < timestamp_now) { 144 throw 'Fragment expired'; 145 } 146 cart_timeout = setTimeout(refresh_cart_fragment, (cart_expiration - timestamp_now)); 147 } 148 149 if (wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash) { 150 151 $.each(wc_fragments, function (key, value) { 152 $(key).replaceWith(value); 153 }); 154 155 $(document.body).trigger('wc_fragments_loaded'); 156 } else { 157 throw 'No fragment'; 158 } 159 160 // Refresh when the display language changes 161 var prev_pll_lang = sessionStorage.getItem('pll_language'), 162 pll_lang = get_pll_language(); 163 164 if (prev_pll_lang === null || prev_pll_lang === undefined || prev_pll_lang === '') { 165 prev_pll_lang = ''; 166 } 167 168 if (pll_lang) { 169 if (!prev_pll_lang || prev_pll_lang !== pll_lang) { 170 sessionStorage.setItem('pll_language', pll_lang); 171 throw 'Language changed'; 172 } 173 } else { 174 throw 'Language not found'; 175 } 176 177 } catch (err) { 178 refresh_cart_fragment(); 179 } 180 181 } else { 182 refresh_cart_fragment(); 183 } 184 185 /* Cart Hiding */ 186 if ($.cookie('woocommerce_items_in_cart') > 0) { 187 $('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').show(); 188 } else { 189 $('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').hide(); 190 } 191 192 $(document.body).bind('adding_to_cart', function () { 193 $('.hide_cart_widget_if_empty').closest('.widget_shopping_cart').show(); 194 }); 195});
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.