♻️ Unify constructor arguments of PaymentButton

Factory method and constuctor have same arguments in the same order
This commit is contained in:
Philipp Stracker 2024-08-08 14:05:57 +02:00
parent 9ac4300995
commit 7729007774
No known key found for this signature in database
2 changed files with 32 additions and 5 deletions

View file

@ -139,6 +139,11 @@ export default class PaymentButton {
*/
#ppcpConfig;
/**
* A variation of a context bootstrap handler.
*/
#externalHandler;
/**
* A variation of a context handler object, like CheckoutHandler.
* This handler provides a standardized interface for certain standardized checks and actions.
@ -190,9 +195,9 @@ export default class PaymentButton {
if ( ! buttonInstances.has( instanceKey ) ) {
const button = new this(
context,
externalHandler,
buttonConfig,
ppcpConfig,
externalHandler,
contextHandler
);
@ -238,11 +243,18 @@ export default class PaymentButton {
*
* @private
* @param {string} context - Button context name.
* @param {Object} externalHandler - Handler object.
* @param {Object} buttonConfig - Payment button specific configuration.
* @param {Object} ppcpConfig - Plugin wide configuration object.
* @param {Object} contextHandler - Handler object.
*/
constructor( context, buttonConfig, ppcpConfig, contextHandler ) {
constructor(
context,
externalHandler = null,
buttonConfig = {},
ppcpConfig = {},
contextHandler = null
) {
if ( this.methodId === PaymentButton.methodId ) {
throw new Error( 'Cannot initialize the PaymentButton base class' );
}
@ -257,6 +269,7 @@ export default class PaymentButton {
this.#context = context;
this.#buttonConfig = buttonConfig;
this.#ppcpConfig = ppcpConfig;
this.#externalHandler = externalHandler;
this.#contextHandler = contextHandler;
this.#wrappers = this.constructor.getWrappers(
@ -339,6 +352,13 @@ export default class PaymentButton {
return this.#ppcpConfig;
}
/**
* @return {Object} The bootstrap handler instance, or an empty object.
*/
get externalHandler() {
return this.#externalHandler || {};
}
/**
* Access the button's context handler.
* When no context handler was provided (like for a preview button), an empty object is returned.
@ -708,13 +728,14 @@ export default class PaymentButton {
return;
}
this.log( 'addButton', button );
const wrapper = this.wrapperElement;
if ( this.#button ) {
this.removeButton();
}
this.log( 'addButton', button );
this.#button = button;
wrapper.appendChild( this.#button );
}