1/** 2 * CommerceKit â nonce broker (CK-1100 extraction). 3 * 4 * Caches the `commercekit_nonce` in a 2-hour cookie and refreshes it via 5 * `wp_ajax_commercekit_get_nonce` only when: 6 * - the cookie is missing, 7 * - the user's login state changed (logged in â logged out), 8 * - the user was switched (admin "User Switching"), 9 * - or the fast-search token is missing while fast search is enabled. 10 * 11 * The fresh nonce is written into the `#commercekit_nonce` hidden input and 12 * `commercekit_ajs.ajax_nonce` is flipped to `1`. Other modules 13 * (`commercekit-stats.js`, Order Bump, etc.) wait for that signal before 14 * firing their own AJAX / REST calls. 15 * 16 * Reads its PHP-side configuration from `window.commercekitNonceCfg`, 17 * populated via `wp_localize_script()` from `commercekit_nonce_footer_script()`. 18 * 19 * Depends on the `Cookies` global (js.cookie) which is enqueued earlier as 20 * `cgkit-js-cookie`. 21 * 22 * @package CommerceKit 23 */ 24 25( function () { 26 'use strict'; 27 28 if ( typeof window.Cookies === 'undefined' || typeof window.commercekit_ajs === 'undefined' ) { 29 return; 30 } 31 32 var cfg = window.commercekitNonceCfg || {}; 33 34 function commercekit_update_nonce( nonce ) { 35 var nonce_input = document.querySelector( '#commercekit_nonce' ); 36 if ( nonce_input ) { 37 nonce_input.value = nonce; 38 } else { 39 document.body.insertAdjacentHTML( 40 'beforeend', 41 '<' + 'input type="hidden" id="commercekit_nonce" name="commercekit_nonce" value="' + nonce + '"' + '>' 42 ); 43 } 44 window.commercekit_ajs.ajax_nonce = 1; 45 if ( typeof window.cgkit_update_order_bump_views === 'function' ) { 46 window.cgkit_update_order_bump_views(); 47 } 48 } 49 50 // Expose globally for back-compat: existing inline snippets in other 51 // modules call `commercekit_update_nonce()` after their own AJAX flows. 52 window.commercekit_update_nonce = commercekit_update_nonce; 53 54 function bootstrap() { 55 var cgkit_nonce_ustate = Number( cfg.userState ) || 0; 56 var cgkit_nonce_cvalue = window.Cookies.get( 'commercekit-nonce-value' ); 57 var cgkit_nonce_cstate = window.Cookies.get( 'commercekit-nonce-state' ); 58 var cgkit_user_switched = Number( cfg.userSwitched ) || 0; 59 var cgkit_fast_token = window.Cookies.get( 'commercekit-fast-token' ); 60 cgkit_nonce_cvalue = cgkit_nonce_cvalue !== undefined ? cgkit_nonce_cvalue : ''; 61 cgkit_fast_token = cgkit_fast_token !== undefined ? cgkit_fast_token : ''; 62 63 var fastSearchOn = window.commercekit_ajs.fast_ajax_search !== undefined && Number( window.commercekit_ajs.fast_ajax_search ) === 1; 64 65 if ( cgkit_nonce_cvalue === '' || String( cgkit_nonce_cstate ) !== String( cgkit_nonce_ustate ) || cgkit_user_switched === 1 || ( cgkit_fast_token === '' && fastSearchOn ) ) { 66 var timestamp = new Date().getTime(); 67 fetch( window.commercekit_ajs.ajax_url + '=commercekit_get_nonce&v=' + timestamp, { 68 method: 'GET' 69 } ).then( function ( response ) { 70 return response.json(); 71 } ).then( function ( json ) { 72 if ( Number( json.status ) !== 1 ) { 73 return; 74 } 75 var twohrs = new Date( new Date().getTime() + 120 * 60 * 1000 ); 76 var allowCookies = true; 77 78 if ( window.Cookiebot ) { 79 allowCookies = !! ( window.Cookiebot.consent.preferences || window.Cookiebot.consent.statistics || window.Cookiebot.consent.marketing ); 80 } 81 82 if ( allowCookies ) { 83 window.Cookies.set( 'commercekit-nonce-value', json.nonce, { expires: twohrs } ); 84 window.Cookies.set( 'commercekit-nonce-state', json.state, { expires: twohrs } ); 85 if ( json.fast_token !== '' ) { 86 window.Cookies.set( 'commercekit-fast-token', json.fast_token, { expires: twohrs } ); 87 } 88 } else { 89 window.Cookies.remove( 'commercekit-nonce-value' ); 90 window.Cookies.remove( 'commercekit-nonce-state' ); 91 window.Cookies.remove( 'commercekit-fast-token' ); 92 } 93 94 commercekit_update_nonce( json.nonce ); 95 } ); 96 } else { 97 commercekit_update_nonce( cgkit_nonce_cvalue ); 98 } 99 } 100 101 document.addEventListener( 'DOMContentLoaded', function () { 102 bootstrap(); 103 } ); 104} )();
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.