Introduce a unified script loader to improve script loading, debugging and prevent conflicts

This commit is contained in:
Daniel Dudzic 2024-10-05 02:26:09 +02:00
parent 1325779582
commit 5e1c2a22d2
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
10 changed files with 186 additions and 65 deletions

View file

@ -52,11 +52,12 @@ class AxoManager {
billingView = null;
cardView = null;
constructor( axoConfig, ppcpConfig ) {
constructor( namespace, axoConfig, ppcpConfig ) {
this.namespace = namespace;
this.axoConfig = axoConfig;
this.ppcpConfig = ppcpConfig;
this.fastlane = new Fastlane();
this.fastlane = new Fastlane( namespace );
this.$ = jQuery;
this.status = {

View file

@ -1,5 +1,6 @@
class Fastlane {
construct() {
constructor( namespace ) {
this.namespace = namespace;
this.connection = null;
this.identity = null;
this.profile = null;
@ -10,7 +11,16 @@ class Fastlane {
connect( config ) {
return new Promise( ( resolve, reject ) => {
window.paypal
if ( ! window[ this.namespace ] ) {
reject(
new Error(
`Namespace ${ this.namespace } not found on window object`
)
);
return;
}
window[ this.namespace ]
.Fastlane( config )
.then( ( result ) => {
this.init( result );
@ -18,7 +28,7 @@ class Fastlane {
} )
.catch( ( error ) => {
console.error( error );
reject();
reject( error );
} );
} );
}

View file

@ -1,21 +1,27 @@
import AxoManager from './AxoManager';
import { loadPaypalScript } from '../../../ppcp-button/resources/js/modules/Helper/ScriptLoading';
import UnifiedScriptLoader from '../../../ppcp-button/resources/js/modules/Helper/UnifiedScriptLoader';
( function ( { axoConfig, ppcpConfig, jQuery } ) {
const namespace = 'ppcpPaypalClassicAxo';
const bootstrap = () => {
new AxoManager( axoConfig, ppcpConfig );
new AxoManager( namespace, axoConfig, ppcpConfig );
};
document.addEventListener( 'DOMContentLoaded', () => {
if ( ! typeof PayPalCommerceGateway ) {
if ( typeof PayPalCommerceGateway === 'undefined' ) {
console.error( 'AXO could not be configured.' );
return;
}
// Load PayPal
loadPaypalScript( ppcpConfig, () => {
bootstrap();
} );
UnifiedScriptLoader.loadPayPalScript( namespace, ppcpConfig )
.then( () => {
console.log( 'PayPal script loaded successfully' );
bootstrap();
} )
.catch( ( error ) => {
console.error( 'Failed to load PayPal script:', error );
} );
} );
} )( {
axoConfig: window.wc_ppcp_axo,

View file

@ -378,7 +378,8 @@ class AxoModule implements ServiceModule, ExtendingModule, ExecutableModule {
return ! is_user_logged_in()
&& CartCheckoutDetector::has_classic_checkout()
&& $dcc_configuration->use_fastlane()
&& ! $this->is_excluded_endpoint();
&& ! $this->is_excluded_endpoint()
&& is_checkout();
}
/**