1( function ( wc_order_attribution ) { 2 'use strict'; 3 // Cache params reference for shorter reusability. 4 const params = wc_order_attribution.params; 5 6 // Helper functions. 7 const $ = document.querySelector.bind( document ); 8 const propertyAccessor = ( obj, path ) => path.split( '.' ).reduce( ( acc, part ) => acc && acc[ part ], obj ); 9 const returnNull = () => null; 10 11 // Hardcode Checkout store key (`wc.wcBlocksData.CHECKOUT_STORE_KEY`), as we no longer have `wc-blocks-checkout` as a dependency. 12 const CHECKOUT_STORE_KEY = 'wc/store/checkout'; 13 14 /** 15 * Get the order attribution data. 16 * 17 * Returns object full of `null`s if tracking is disabled. 18 * 19 * @returns {Object} Schema compatible object. 20 */ 21 function getData() { 22 const accessor = params.allowTracking ? propertyAccessor : returnNull; 23 const entries = Object.entries( wc_order_attribution.fields ) 24 .map( ( [ key, property ] ) => [ key, accessor( sbjs.get, property ) ] ); 25 return Object.fromEntries( entries ); 26 } 27 28 /** 29 * Update `wc_order_attribution` input elements' values. 30 * 31 * @param {Object} values Object containing field values. 32 */ 33 function updateFormValues( values ) { 34 // Update inputs if any exist. 35 if( $( `input[name^="${params.prefix}"]` ) ) { 36 for( const key of Object.keys( wc_order_attribution.fields ) ) { 37 $( `input[name="${params.prefix}${key}"]` ).value = values && values[ key ] || ''; 38 } 39 } 40 41 }; 42 43 /** 44 * Update Checkout extension data. 45 * 46 * @param {Object} values Object containing field values. 47 */ 48 function updateCheckoutBlockData( values ) { 49 // Update Checkout block data if available. 50 if ( window.wp && window.wp.data && window.wp.data.dispatch && window.wc && window.wc.wcBlocksData ) { 51 window.wp.data.dispatch( window.wc.wcBlocksData.CHECKOUT_STORE_KEY ).__internalSetExtensionData( 52 'woocommerce/order-attribution', 53 values, 54 true 55 ); 56 } 57 } 58 59 /** 60 * Initialize sourcebuster & set data, or clear cookies & data. 61 * 62 * @param {boolean} allow Whether to allow tracking or disable it. 63 */ 64 wc_order_attribution.setOrderTracking = function( allow ) { 65 params.allowTracking = allow; 66 if ( ! allow ) { 67 // Reset cookies, and clear form data. 68 removeTrackingCookies(); 69 } else { 70 // If not done yet, initialize sourcebuster.js which populates `sbjs.get` object. 71 sbjs.init( { 72 lifetime: Number( params.lifetime ), 73 session_length: Number( params.session ), 74 timezone_offset: '0', // utc 75 } ); 76 } 77 const values = getData(); 78 updateFormValues( values ); 79 updateCheckoutBlockData( values ); 80 } 81 82 /** 83 * Remove sourcebuster.js cookies. 84 * To be called whenever tracking is disabled or consent is revoked. 85 */ 86 function removeTrackingCookies() { 87 const domain = window.location.hostname; 88 const sbCookies = [ 89 'sbjs_current', 90 'sbjs_current_add', 91 'sbjs_first', 92 'sbjs_first_add', 93 'sbjs_session', 94 'sbjs_udata', 95 'sbjs_migrations', 96 'sbjs_promo' 97 ]; 98 99 // Remove cookies 100 sbCookies.forEach( ( name ) => { 101 document.cookie = `${name}=; path=/; max-age=-999; domain=.${domain};`; 102 } ); 103 } 104 105 // Run init. 106 wc_order_attribution.setOrderTracking( params.allowTracking ); 107 108 // Wait for (async) classic checkout initialization and set source values once loaded. 109 if ( $( 'form.woocommerce-checkout' ) !== null ) { 110 const previousInitCheckout = document.body.oninit_checkout; 111 document.body.oninit_checkout = () => { 112 updateFormValues( getData() ); 113 previousInitCheckout && previousInitCheckout(); 114 }; 115 } 116 117 // Work around the lack of explicit script dependency for the checkout block. 118 // Conditionally, wait for and use 'wp-data' & 'wc-blocks-checkout. 119 120 // Wait for (async) block checkout initialization and set source values once loaded. 121 function eventuallyInitializeCheckoutBlock() { 122 if ( 123 window.wp && window.wp.data && typeof window.wp.data.subscribe === 'function' 124 ) { 125 // Update checkout block data once more if the checkout store was loaded after this script. 126 const unsubscribe = window.wp.data.subscribe( function () { 127 unsubscribe(); 128 updateCheckoutBlockData( getData() ); 129 }, CHECKOUT_STORE_KEY ); 130 } 131 }; 132 // Wait for DOMContentLoaded to make sure wp.data is in place, if applicable for the page. 133 if (document.readyState === "loading") { 134 document.addEventListener("DOMContentLoaded", eventuallyInitializeCheckoutBlock); 135 } else { 136 eventuallyInitializeCheckoutBlock(); 137 } 138 139}( window.wc_order_attribution ) );
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.