From 52318b4de791c6fd5eed8737a69731fa94083519 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Mon, 20 Nov 2023 15:37:08 +0100 Subject: [PATCH] Use single public namespace for OSA JS --- .../js/frontend/order-source-attribution.js | 18 +++++++++--------- .../legacy/js/frontend/wccom-integration.js | 2 +- .../js/frontend/wp-consent-api-integration.js | 4 ++-- .../Orders/SourceAttributionController.php | 18 ++++++++++-------- 4 files changed, 22 insertions(+), 20 deletions(-) diff --git a/plugins/woocommerce/client/legacy/js/frontend/order-source-attribution.js b/plugins/woocommerce/client/legacy/js/frontend/order-source-attribution.js index 57b7013177e..75757807883 100644 --- a/plugins/woocommerce/client/legacy/js/frontend/order-source-attribution.js +++ b/plugins/woocommerce/client/legacy/js/frontend/order-source-attribution.js @@ -1,16 +1,16 @@ -( function ( $, params ) { +( function ( $, wc_order_source_attribution ) { 'use strict'; + const params = wc_order_source_attribution.params; const prefix = params.prefix; const cookieLifetime = Number( params.lifetime ); const sessionLength = Number( params.session ); - window.woocommerce_order_source_attribution = {}; - woocommerce_order_source_attribution.initOrderTracking = () => { + wc_order_source_attribution.initOrderTracking = () => { if ( params.allowTracking === 'no' ) { - woocommerce_order_source_attribution.removeTrackingCookies(); + wc_order_source_attribution.removeTrackingCookies(); return; } @@ -66,19 +66,19 @@ /** * Enable or disable order tracking analytics and marketing consent init and change. */ - woocommerce_order_source_attribution.setAllowTrackingConsent = ( allow ) => { + wc_order_source_attribution.setAllowTrackingConsent = ( allow ) => { if ( ! allow ) { return; } params.allowTracking = 'yes'; - woocommerce_order_source_attribution.initOrderTracking(); + wc_order_source_attribution.initOrderTracking(); } /** * Remove sourcebuster.js cookies whenever tracking is disabled or consent is revoked. */ - woocommerce_order_source_attribution.removeTrackingCookies = () => { + wc_order_source_attribution.removeTrackingCookies = () => { const domain = window.location.hostname; const sbCookies = [ 'sbjs_current', @@ -99,6 +99,6 @@ } // Run init. - woocommerce_order_source_attribution.initOrderTracking(); + wc_order_source_attribution.initOrderTracking(); -}( jQuery, window.wc_order_attribute_source_params ) ); +}( jQuery, window.wc_order_source_attribution ) ); diff --git a/plugins/woocommerce/client/legacy/js/frontend/wccom-integration.js b/plugins/woocommerce/client/legacy/js/frontend/wccom-integration.js index 6319e743d8f..97462d9d06f 100644 --- a/plugins/woocommerce/client/legacy/js/frontend/wccom-integration.js +++ b/plugins/woocommerce/client/legacy/js/frontend/wccom-integration.js @@ -1,6 +1,6 @@ window.addEventListener( 'load' , function ( e ) { if ( window.wccom && window.wccom.canTrackUser('analytics') ) { - window.woocommerce_order_source_attribution.setAllowTrackingConsent( true ); + window.wc_order_source_attribution.setAllowTrackingConsent( true ); } } ); diff --git a/plugins/woocommerce/client/legacy/js/frontend/wp-consent-api-integration.js b/plugins/woocommerce/client/legacy/js/frontend/wp-consent-api-integration.js index f2301c4d508..7a5464fc510 100644 --- a/plugins/woocommerce/client/legacy/js/frontend/wp-consent-api-integration.js +++ b/plugins/woocommerce/client/legacy/js/frontend/wp-consent-api-integration.js @@ -8,7 +8,7 @@ for ( const key in changedConsentCategory ) { if ( changedConsentCategory.hasOwnProperty( key ) ) { if ( key === CONSENT_CATEGORY_MARKING && changedConsentCategory[ key ] === 'allow' ) { - window.woocommerce_order_source_attribution.setAllowTrackingConsent( true ); + window.wc_order_source_attribution.setAllowTrackingConsent( true ); } } } @@ -17,7 +17,7 @@ // Init order source attribution as soon as consent type is defined. $( document ).on( 'wp_consent_type_defined', () => { if ( wp_has_consent( CONSENT_CATEGORY_MARKING ) ) { - window.woocommerce_order_source_attribution.setAllowTrackingConsent( true ); + window.wc_order_source_attribution.setAllowTrackingConsent( true ); } } ); }( jQuery ) ); diff --git a/plugins/woocommerce/src/Internal/Orders/SourceAttributionController.php b/plugins/woocommerce/src/Internal/Orders/SourceAttributionController.php index f1110069d5a..e705fb6d083 100644 --- a/plugins/woocommerce/src/Internal/Orders/SourceAttributionController.php +++ b/plugins/woocommerce/src/Internal/Orders/SourceAttributionController.php @@ -221,16 +221,18 @@ class SourceAttributionController implements RegisterHooksInterface { */ $allow_tracking = wc_bool_to_string( apply_filters( 'wc_order_source_attribution_allow_tracking', true ) ); - // Pass parameters to Order Source Attribution JS. - $params = array( - 'lifetime' => $lifetime, - 'session' => $session_length, - 'ajaxurl' => admin_url( 'admin-ajax.php' ), - 'prefix' => $this->field_prefix, - 'allowTracking' => $allow_tracking, + // Create Order Source Attribution JS namespace with parameters. + $namespace = array( + 'params' => array ( + 'lifetime' => $lifetime, + 'session' => $session_length, + 'ajaxurl' => admin_url( 'admin-ajax.php' ), + 'prefix' => $this->field_prefix, + 'allowTracking' => $allow_tracking, + ), ); - wp_localize_script( 'woocommerce-order-source-attribution-js', 'wc_order_attribute_source_params', $params ); + wp_localize_script( 'woocommerce-order-source-attribution-js', 'wc_order_source_attribution', $namespace ); } /**