1/* global wc_cart_fragments_params, Cookies */ 2jQuery( function( $ ) { 3 4 // wc_cart_fragments_params is required to continue, ensure the object exists 5 if ( typeof wc_cart_fragments_params === 'undefined' ) { 6 return false; 7 } 8 9 /* Storage Handling */ 10 var $supports_html5_storage = true, 11 cart_hash_key = wc_cart_fragments_params.cart_hash_key; 12 13 try { 14 $supports_html5_storage = ( 'sessionStorage' in window && window.sessionStorage !== null ); 15 window.sessionStorage.setItem( 'wc', 'test' ); 16 window.sessionStorage.removeItem( 'wc' ); 17 window.localStorage.setItem( 'wc', 'test' ); 18 window.localStorage.removeItem( 'wc' ); 19 } catch( err ) { 20 $supports_html5_storage = false; 21 } 22 23 /* Cart session creation time to base expiration on */ 24 function set_cart_creation_timestamp() { 25 if ( $supports_html5_storage ) { 26 sessionStorage.setItem( 'wc_cart_created', ( new Date() ).getTime() ); 27 } 28 } 29 30 /** Set the cart hash in both session and local storage */ 31 function set_cart_hash( cart_hash ) { 32 if ( $supports_html5_storage ) { 33 localStorage.setItem( cart_hash_key, cart_hash ); 34 sessionStorage.setItem( cart_hash_key, cart_hash ); 35 } 36 } 37 38 var $fragment_refresh = { 39 url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_refreshed_fragments' ), 40 type: 'POST', 41 data: new URLSearchParams( { 42 time: new Date().getTime() 43 } ).toString(), 44 timeout: wc_cart_fragments_params.request_timeout, 45 success: function( data ) { 46 if ( data && data.fragments ) { 47 48 $.each( data.fragments, function( key, value ) { 49 $( key ).replaceWith( value ); 50 }); 51 52 if ( $supports_html5_storage ) { 53 sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( data.fragments ) ); 54 set_cart_hash( data.cart_hash ); 55 56 if ( data.cart_hash ) { 57 set_cart_creation_timestamp(); 58 } 59 } 60 61 $( document.body ).trigger( 'wc_fragments_refreshed' ); 62 } 63 }, 64 error: function() { 65 $( document.body ).trigger( 'wc_fragments_ajax_error' ); 66 } 67 }; 68 69 /* Named callback for refreshing cart fragment */ 70 function refresh_cart_fragment() { 71 const controller = new AbortController(); 72 const timeoutId = setTimeout( () => controller.abort(), $fragment_refresh.timeout ); 73 74 window.fetch( $fragment_refresh.url, { 75 method: $fragment_refresh.type, 76 headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, 77 body: $fragment_refresh.data, 78 signal: controller.signal 79 } ) 80 .then( response => { 81 clearTimeout( timeoutId ); 82 83 if ( !response.ok ) { 84 throw new Error( response.statusText ); 85 } 86 return response.json(); 87 } ) 88 .then( $fragment_refresh.success ) 89 .catch( error => $fragment_refresh.error() ); 90 } 91 92 /* Cart Handling */ 93 if ( $supports_html5_storage ) { 94 95 var cart_timeout = null, 96 day_in_ms = ( 24 * 60 * 60 * 1000 ); 97 98 $( document.body ).on( 'wc_fragment_refresh updated_wc_div', function() { 99 refresh_cart_fragment(); 100 }); 101 102 $( document.body ).on( 'added_to_cart removed_from_cart', function( event, fragments, cart_hash ) { 103 var prev_cart_hash = sessionStorage.getItem( cart_hash_key ); 104 105 if ( prev_cart_hash === null || prev_cart_hash === undefined || prev_cart_hash === '' ) { 106 set_cart_creation_timestamp(); 107 } 108 109 sessionStorage.setItem( wc_cart_fragments_params.fragment_name, JSON.stringify( fragments ) ); 110 set_cart_hash( cart_hash ); 111 }); 112 113 $( document.body ).on( 'wc_fragments_refreshed', function() { 114 clearTimeout( cart_timeout ); 115 cart_timeout = setTimeout( refresh_cart_fragment, day_in_ms ); 116 } ); 117 118 // Refresh when storage changes in another tab 119 $( window ).on( 'storage onstorage', function ( e ) { 120 if ( 121 cart_hash_key === e.originalEvent.key && localStorage.getItem( cart_hash_key ) !== sessionStorage.getItem( cart_hash_key ) 122 ) { 123 refresh_cart_fragment(); 124 } 125 }); 126 127 // Refresh when page is shown after back button (safari) 128 $( window ).on( 'pageshow' , function( e ) { 129 if ( e.originalEvent.persisted ) { 130 $( '.widget_shopping_cart_content' ).empty(); 131 $( document.body ).trigger( 'wc_fragment_refresh' ); 132 } 133 } ); 134 135 try { 136 var wc_fragments = JSON.parse( sessionStorage.getItem( wc_cart_fragments_params.fragment_name ) ), 137 cart_hash = sessionStorage.getItem( cart_hash_key ), 138 cookie_hash = Cookies.get( 'woocommerce_cart_hash'), 139 cart_created = sessionStorage.getItem( 'wc_cart_created' ); 140 141 if ( cart_hash === null || cart_hash === undefined || cart_hash === '' ) { 142 cart_hash = ''; 143 } 144 145 if ( cookie_hash === null || cookie_hash === undefined || cookie_hash === '' ) { 146 cookie_hash = ''; 147 } 148 149 if ( cart_hash && ( cart_created === null || cart_created === undefined || cart_created === '' ) ) { 150 throw 'No cart_created'; 151 } 152 153 if ( cart_created ) { 154 var cart_expiration = ( ( 1 * cart_created ) + day_in_ms ), 155 timestamp_now = ( new Date() ).getTime(); 156 if ( cart_expiration < timestamp_now ) { 157 throw 'Fragment expired'; 158 } 159 cart_timeout = setTimeout( refresh_cart_fragment, ( cart_expiration - timestamp_now ) ); 160 } 161 162 if ( wc_fragments && wc_fragments['div.widget_shopping_cart_content'] && cart_hash === cookie_hash ) { 163 164 $.each( wc_fragments, function( key, value ) { 165 $( key ).replaceWith(value); 166 }); 167 168 $( document.body ).trigger( 'wc_fragments_loaded' ); 169 } else { 170 throw 'No fragment'; 171 } 172 173 } catch( err ) { 174 refresh_cart_fragment(); 175 } 176 177 } else { 178 refresh_cart_fragment(); 179 } 180 181 /* Cart Hiding */ 182 if ( Cookies.get( 'woocommerce_items_in_cart' ) > 0 ) { 183 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show(); 184 } else { 185 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).hide(); 186 } 187 188 $( document.body ).on( 'adding_to_cart', function() { 189 $( '.hide_cart_widget_if_empty' ).closest( '.widget_shopping_cart' ).show(); 190 }); 191 192 // Customiser support. 193 var hasSelectiveRefresh = ( 194 'undefined' !== typeof wp && 195 wp.customize && 196 wp.customize.selectiveRefresh && 197 wp.customize.widgetsPreview && 198 wp.customize.widgetsPreview.WidgetPartial 199 ); 200 if ( hasSelectiveRefresh ) { 201 wp.customize.selectiveRefresh.bind( 'partial-content-rendered', function() { 202 refresh_cart_fragment(); 203 } ); 204 } 205});
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.