Make method getAttributionData globally accessible (#46965)

* Make method to getAttributionData externally accessible

* Include checks for sjbs availability

* Failsafe for the custom html element if sbjs isn't available

* Changelog
This commit is contained in:
Justin Palmer 2024-05-13 12:24:32 +02:00 committed by GitHub
parent b164d19328
commit 7796c13b65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 12 deletions

View File

@ -0,0 +1,4 @@
Significance: minor
Type: update
Make order attribution data globally accessible client side.

View File

@ -15,14 +15,15 @@
/**
* Get the order attribution data.
*
* Returns object full of `null`s if tracking is disabled.
* Returns object full of `null`s if tracking is disabled or if sourcebuster.js is blocked.
*
* @returns {Object} Schema compatible object.
*/
function getData() {
const accessor = params.allowTracking ? propertyAccessor : returnNull;
wc_order_attribution.getAttributionData = function() {
const accessor = params.allowTracking && isSbjsAvailable() ? propertyAccessor : returnNull;
const getter = isSbjsAvailable() ? sbjs.get : {};
const entries = Object.entries( wc_order_attribution.fields )
.map( ( [ key, property ] ) => [ key, accessor( sbjs.get, property ) ] );
.map( ( [ key, property ] ) => [ key, accessor( getter, property ) ] );
return Object.fromEntries( entries );
}
@ -55,6 +56,15 @@
}
}
/**
* Determin whether sourcebuster.js is available.
*
* @returns {boolean} Whether sourcebuster.js is available.
*/
function isSbjsAvailable() {
return typeof sbjs !== 'undefined';
}
/**
* Initialize sourcebuster & set data, or clear cookies & data.
*
@ -65,7 +75,7 @@
if ( ! allow ) {
// Reset cookies, and clear form data.
removeTrackingCookies();
} else if ( typeof sbjs === 'undefined' ) {
} else if ( ! isSbjsAvailable() ) {
return; // Do nothing, as sourcebuster.js is not loaded.
} else {
// If not done yet, initialize sourcebuster.js which populates `sbjs.get` object.
@ -75,7 +85,7 @@
timezone_offset: '0', // utc
} );
}
const values = getData();
const values = wc_order_attribution.getAttributionData();
updateFormValues( values );
updateCheckoutBlockData( values );
}
@ -117,7 +127,7 @@
// Update checkout block data once more if the checkout store was loaded after this script.
const unsubscribe = window.wp.data.subscribe( function () {
unsubscribe();
updateCheckoutBlockData( getData() );
updateCheckoutBlockData( wc_order_attribution.getAttributionData() );
}, CHECKOUT_STORE_KEY );
}
};
@ -157,7 +167,7 @@
connectedCallback() {
let inputs = '';
for( const fieldName of this._fieldNames ) {
const value = stringifyFalsyInputValue( this.values[ fieldName ] );
const value = stringifyFalsyInputValue( ( this.values && this.values[ fieldName ] ) || '' );
inputs += `<input type="hidden" name="${params.prefix}${fieldName}" value="${value}"/>`;
}
this.innerHTML = inputs;