🐛 Fix bug with undefined log() method

This commit is contained in:
Philipp Stracker 2024-07-26 17:06:42 +02:00
parent a14276bf52
commit cbf9bff808
No known key found for this signature in database

View file

@ -43,8 +43,10 @@ const CONTEXT = {
BlockCart: 'cart-block',
BlockCheckout: 'checkout-block',
Preview: 'preview',
// Block editor contexts.
Blocks: [ 'cart-block', 'checkout-block' ],
// Custom gateway contexts.
Gateways: [ 'checkout', 'pay-now' ],
};
@ -89,6 +91,8 @@ class ApplePayButton {
initialPaymentRequest = null;
constructor( context, externalHandler, buttonConfig, ppcpConfig ) {
this._initDebug( !! buttonConfig?.is_debug );
apmButtonsInit( ppcpConfig );
this.context = context;
@ -103,25 +107,34 @@ class ApplePayButton {
);
this.refreshContextData();
}
this.log = () => {};
/**
* NOOP log function to avoid errors when debugging is disabled.
*/
log() {}
// Debug helpers
if ( this.buttonConfig.is_debug ) {
document.ppcpApplepayButtons = document.ppcpApplepayButtons || {};
document.ppcpApplepayButtons[ this.context ] = this;
this.log = function () {
console.log(
`[ApplePayButton | ${ this.context }]`,
...arguments
);
};
jQuery( document ).on( 'ppcp-applepay-debug', () => {
this.log( this );
} );
/**
* Enables debugging tools, when the button's is_debug flag is set.
*
* @param {boolean} enableDebugging If debugging features should be enabled for this instance.
* @private
*/
_initDebug( enableDebugging ) {
if ( ! enableDebugging || this.#isInitialized ) {
return;
}
document.ppcpApplepayButtons = document.ppcpApplepayButtons || {};
document.ppcpApplepayButtons[ this.context ] = this;
this.log = ( ...args ) => {
console.log( `[ApplePayButton | ${ this.context }]`, ...args );
};
jQuery( document ).on( 'ppcp-applepay-debug', () => {
this.log( this );
} );
}
/**