add-payment-method.js
3 years ago
add-payment-method.min.js
2 years ago
add-to-cart-variation.js
3 years ago
add-to-cart-variation.min.js
2 years ago
add-to-cart.js
2 years ago
add-to-cart.min.js
2 years ago
address-i18n.js
5 years ago
address-i18n.min.js
2 years ago
cart-fragments.js
3 years ago
cart-fragments.min.js
2 years ago
cart.js
2 years ago
cart.min.js
2 years ago
checkout.js
2 years ago
checkout.min.js
2 years ago
country-select.js
5 years ago
country-select.min.js
2 years ago
credit-card-form.js
8 years ago
credit-card-form.min.js
8 years ago
geolocation.js
2 years ago
geolocation.min.js
2 years ago
lost-password.js
8 years ago
lost-password.min.js
8 years ago
order-attribution.js
2 years ago
order-attribution.min.js
2 years ago
password-strength-meter.js
2 years ago
password-strength-meter.min.js
2 years ago
price-slider.js
5 years ago
price-slider.min.js
2 years ago
single-product.js
3 years ago
single-product.min.js
2 years ago
tokenization-form.js
5 years ago
tokenization-form.min.js
2 years ago
woocommerce.js
3 years ago
woocommerce.min.js
2 years ago
wp-consent-api-integration.js
2 years ago
wp-consent-api-integration.min.js
2 years ago
order-attribution.js
187 lines
| 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 | const stringifyFalsyInputValue = ( value ) => value === null || value === undefined ? '' : value; |
| 11 | |
| 12 | // Hardcode Checkout store key (`wc.wcBlocksData.CHECKOUT_STORE_KEY`), as we no longer have `wc-blocks-checkout` as a dependency. |
| 13 | const CHECKOUT_STORE_KEY = 'wc/store/checkout'; |
| 14 | |
| 15 | /** |
| 16 | * Get the order attribution data. |
| 17 | * |
| 18 | * Returns object full of `null`s if tracking is disabled. |
| 19 | * |
| 20 | * @returns {Object} Schema compatible object. |
| 21 | */ |
| 22 | function getData() { |
| 23 | const accessor = params.allowTracking ? propertyAccessor : returnNull; |
| 24 | const entries = Object.entries( wc_order_attribution.fields ) |
| 25 | .map( ( [ key, property ] ) => [ key, accessor( sbjs.get, property ) ] ); |
| 26 | return Object.fromEntries( entries ); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Update `wc_order_attribution` input elements' values. |
| 31 | * |
| 32 | * @param {Object} values Object containing field values. |
| 33 | */ |
| 34 | function updateFormValues( values ) { |
| 35 | // Update `<wc-order-attribution-inputs>` elements if any exist. |
| 36 | for( const element of document.querySelectorAll( 'wc-order-attribution-inputs' ) ) { |
| 37 | element.values = values; |
| 38 | } |
| 39 | |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Update Checkout extension data. |
| 44 | * |
| 45 | * @param {Object} values Object containing field values. |
| 46 | */ |
| 47 | function updateCheckoutBlockData( values ) { |
| 48 | // Update Checkout block data if available. |
| 49 | if ( window.wp && window.wp.data && window.wp.data.dispatch && window.wc && window.wc.wcBlocksData ) { |
| 50 | window.wp.data.dispatch( window.wc.wcBlocksData.CHECKOUT_STORE_KEY ).__internalSetExtensionData( |
| 51 | 'woocommerce/order-attribution', |
| 52 | values, |
| 53 | true |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Initialize sourcebuster & set data, or clear cookies & data. |
| 60 | * |
| 61 | * @param {boolean} allow Whether to allow tracking or disable it. |
| 62 | */ |
| 63 | wc_order_attribution.setOrderTracking = function( allow ) { |
| 64 | params.allowTracking = allow; |
| 65 | if ( ! allow ) { |
| 66 | // Reset cookies, and clear form data. |
| 67 | removeTrackingCookies(); |
| 68 | } else { |
| 69 | // If not done yet, initialize sourcebuster.js which populates `sbjs.get` object. |
| 70 | sbjs.init( { |
| 71 | lifetime: Number( params.lifetime ), |
| 72 | session_length: Number( params.session ), |
| 73 | timezone_offset: '0', // utc |
| 74 | } ); |
| 75 | } |
| 76 | const values = getData(); |
| 77 | updateFormValues( values ); |
| 78 | updateCheckoutBlockData( values ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Remove sourcebuster.js cookies. |
| 83 | * To be called whenever tracking is disabled or consent is revoked. |
| 84 | */ |
| 85 | function removeTrackingCookies() { |
| 86 | const domain = window.location.hostname; |
| 87 | const sbCookies = [ |
| 88 | 'sbjs_current', |
| 89 | 'sbjs_current_add', |
| 90 | 'sbjs_first', |
| 91 | 'sbjs_first_add', |
| 92 | 'sbjs_session', |
| 93 | 'sbjs_udata', |
| 94 | 'sbjs_migrations', |
| 95 | 'sbjs_promo' |
| 96 | ]; |
| 97 | |
| 98 | // Remove cookies |
| 99 | sbCookies.forEach( ( name ) => { |
| 100 | document.cookie = `${name}=; path=/; max-age=-999; domain=.${domain};`; |
| 101 | } ); |
| 102 | } |
| 103 | |
| 104 | // Run init. |
| 105 | wc_order_attribution.setOrderTracking( params.allowTracking ); |
| 106 | |
| 107 | // Work around the lack of explicit script dependency for the checkout block. |
| 108 | // Conditionally, wait for and use 'wp-data' & 'wc-blocks-checkout. |
| 109 | |
| 110 | // Wait for (async) block checkout initialization and set source values once loaded. |
| 111 | function eventuallyInitializeCheckoutBlock() { |
| 112 | if ( |
| 113 | window.wp && window.wp.data && typeof window.wp.data.subscribe === 'function' |
| 114 | ) { |
| 115 | // Update checkout block data once more if the checkout store was loaded after this script. |
| 116 | const unsubscribe = window.wp.data.subscribe( function () { |
| 117 | unsubscribe(); |
| 118 | updateCheckoutBlockData( getData() ); |
| 119 | }, CHECKOUT_STORE_KEY ); |
| 120 | } |
| 121 | }; |
| 122 | // Wait for DOMContentLoaded to make sure wp.data is in place, if applicable for the page. |
| 123 | if (document.readyState === "loading") { |
| 124 | document.addEventListener("DOMContentLoaded", eventuallyInitializeCheckoutBlock); |
| 125 | } else { |
| 126 | eventuallyInitializeCheckoutBlock(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Define an element to contribute order attribute values to the enclosing form. |
| 131 | * To be used with the classic checkout. |
| 132 | */ |
| 133 | window.customElements.define( 'wc-order-attribution-inputs', class extends HTMLElement { |
| 134 | // Our bundler version does not support private class members, so we use a convention of `_` prefix. |
| 135 | // #values |
| 136 | // #fieldNames |
| 137 | constructor(){ |
| 138 | super(); |
| 139 | // Cache fieldNames available at the construction time, to avoid malformed behavior if they change in runtime. |
| 140 | this._fieldNames = Object.keys( wc_order_attribution.fields ); |
| 141 | // Allow values to be lazily set before CE upgrade. |
| 142 | if ( this.hasOwnProperty( '_values' ) ) { |
| 143 | let values = this.values; |
| 144 | // Restore the setter. |
| 145 | delete this.values; |
| 146 | this.values = values || {}; |
| 147 | } |
| 148 | } |
| 149 | /** |
| 150 | * Stamp input elements to the element's light DOM. |
| 151 | * |
| 152 | * We could use `.elementInternals.setFromValue` and avoid sprouting `<input>` elements, |
| 153 | * but it's not yet supported in Safari. |
| 154 | */ |
| 155 | connectedCallback() { |
| 156 | let inputs = ''; |
| 157 | for( const fieldName of this._fieldNames ) { |
| 158 | const value = stringifyFalsyInputValue( this.values[ fieldName ] ); |
| 159 | inputs += `<input type="hidden" name="${params.prefix}${fieldName}" value="${value}"/>`; |
| 160 | } |
| 161 | this.innerHTML = inputs; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Update form values. |
| 166 | */ |
| 167 | set values( values ) { |
| 168 | this._values = values; |
| 169 | if( this.isConnected ) { |
| 170 | for( const fieldName of this._fieldNames ) { |
| 171 | const input = this.querySelector( `input[name="${params.prefix}${fieldName}"]` ); |
| 172 | if( input ) { |
| 173 | input.value = stringifyFalsyInputValue( this.values[ fieldName ] ); |
| 174 | } else { |
| 175 | console.warn( `Field "${fieldName}" not found. Most likely, the '<wc-order-attribution-inputs>' element was manipulated.`); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | get values() { |
| 181 | return this._values; |
| 182 | } |
| 183 | } ); |
| 184 | |
| 185 | |
| 186 | }( window.wc_order_attribution ) ); |
| 187 |