🎨 Minor: Apply WP Code Style

- Consistent quote-style
- Insert missing semicolons in JS
- Fix some whitespaces
This commit is contained in:
Philipp Stracker 2024-06-12 15:31:33 +02:00
parent bc57fdcd6a
commit 874ebbc7a0
No known key found for this signature in database
4 changed files with 65 additions and 65 deletions

View file

@ -1,4 +1,4 @@
import merge from "deepmerge";
import merge from 'deepmerge';
/**
* Base class for APM button previews, used on the plugin's settings page.
@ -9,7 +9,10 @@ class PreviewButton {
* @param {object} apiConfig - PayPal configuration object; retrieved via a
* widgetBuilder API method
*/
constructor({selector, apiConfig}) {
constructor({
selector,
apiConfig,
}) {
this.apiConfig = apiConfig;
this.defaultAttributes = {};
this.buttonConfig = {};
@ -29,10 +32,10 @@ class PreviewButton {
* @return {jQuery} Always a single jQuery element with the new DOM node.
*/
createNewWrapper() {
const previewId = this.selector.replace('#', '')
const previewId = this.selector.replace('#', '');
const previewClass = 'ppcp-button-apm';
return jQuery(`<div id="${previewId}" class="${previewClass}">`)
return jQuery(`<div id='${previewId}' class='${previewClass}'>`);
}
/**
@ -53,8 +56,8 @@ class PreviewButton {
* @return {this} Reference to self, for chaining.
*/
setButtonConfig(config) {
this.buttonConfig = merge(this.defaultAttributes, config)
this.buttonConfig.button.wrapper = this.selector
this.buttonConfig = merge(this.defaultAttributes, config);
this.buttonConfig.button.wrapper = this.selector;
return this;
}
@ -97,7 +100,7 @@ class PreviewButton {
return;
}
this.domWrapper = this.createNewWrapper();
this.domWrapper.insertAfter(this.wrapper)
this.domWrapper.insertAfter(this.wrapper);
} else {
this.domWrapper.empty().show();
}
@ -113,14 +116,14 @@ class PreviewButton {
* previewButtonConfig.button.wrapper must be different from this.ppcpConfig.button.wrapper!
* If both selectors point to the same element, an infinite loop is triggered.
*/
const buttonWrapper = previewButtonConfig.button.wrapper.replace(/^#/, '')
const ppcpWrapper = this.ppcpConfig.button.wrapper.replace(/^#/, '')
const buttonWrapper = previewButtonConfig.button.wrapper.replace(/^#/, '');
const ppcpWrapper = this.ppcpConfig.button.wrapper.replace(/^#/, '');
if (buttonWrapper === ppcpWrapper) {
throw new Error(`[APM Preview Button] Infinite loop detected. Provide different selectors for the button/ppcp wrapper elements! Selector: "#${buttonWrapper}"`);
}
this.createButton(previewButtonConfig)
this.createButton(previewButtonConfig);
}
remove() {

View file

@ -1,5 +1,5 @@
import {loadCustomScript} from "@paypal/paypal-js";
import widgetBuilder from "./WidgetBuilder";
import { loadCustomScript } from '@paypal/paypal-js';
import widgetBuilder from './WidgetBuilder';
/**
* Manages all PreviewButton instances of a certain payment method on the page.
@ -19,14 +19,18 @@ class PreviewButtonManager {
*/
#onInit;
constructor({methodName, buttonConfig, defaultAttributes}) {
constructor({
methodName,
buttonConfig,
defaultAttributes,
}) {
// Define the payment method name in the derived class.
this.methodName = methodName;
this.buttonConfig = buttonConfig;
this.defaultAttributes = defaultAttributes;
this.isEnabled = true
this.isEnabled = true;
this.buttons = {};
this.apiConfig = null;
@ -77,7 +81,7 @@ class PreviewButtonManager {
* Output an error message to the console, with a module-specific prefix.
*/
error(message, ...args) {
console.error(`${this.methodName} ${message}`, ...args)
console.error(`${this.methodName} ${message}`, ...args);
}
/**
@ -86,7 +90,7 @@ class PreviewButtonManager {
* style settings that were provided from server-side.
*/
isDynamic() {
return !!document.querySelector(`[data-ppcp-apm-name="${this.methodName}"]`)
return !!document.querySelector(`[data-ppcp-apm-name="${this.methodName}"]`);
}
/**
@ -105,9 +109,11 @@ class PreviewButtonManager {
return;
}
// This is a localization object of "gateway-settings.js". If it's missing, the script was not loaded.
// This is a localization object of "gateway-settings.js". If it's missing, the script was
// not loaded.
if (!window.PayPalCommerceGatewaySettings) {
this.error('PayPal settings are not fully loaded. Please clear the cache and reload the page.');
this.error(
'PayPal settings are not fully loaded. Please clear the cache and reload the page.');
return;
}
@ -132,22 +138,22 @@ class PreviewButtonManager {
});
// Load the custom SDK script.
const customScriptPromise = loadCustomScript({url: this.buttonConfig.sdk_url});
const customScriptPromise = loadCustomScript({ url: this.buttonConfig.sdk_url });
// Wait for both promises to resolve before continuing.
await Promise
.all([customScriptPromise, paypalPromise])
.catch(err => {
console.log(`Failed to load ${this.methodName} dependencies:`, err)
})
console.log(`Failed to load ${this.methodName} dependencies:`, err);
});
/*
The fetchConfig method requires two objects to succeed:
(a) the SDK custom-script
(b) the `widgetBuilder.paypal` object
The fetchConfig method requires two objects to succeed:
(a) the SDK custom-script
(b) the `widgetBuilder.paypal` object
*/
this.apiConfig = await this.fetchConfig(widgetBuilder.paypal);
await this.#onInitResolver()
await this.#onInitResolver();
this.#onInit = null;
}
@ -159,10 +165,10 @@ class PreviewButtonManager {
* @param ppcpConfig - The button settings for the preview.
*/
renderPreview(ev, ppcpConfig) {
const id = ppcpConfig.button.wrapper
const id = ppcpConfig.button.wrapper;
if (!id) {
this.error('Button did not provide a wrapper ID', ppcpConfig)
this.error('Button did not provide a wrapper ID', ppcpConfig);
return;
}
@ -180,7 +186,7 @@ class PreviewButtonManager {
this.buttons[id]
.setDynamic(this.isDynamic())
.setPpcpConfig(ppcpConfig)
.render()
.render();
}
/**
@ -193,7 +199,7 @@ class PreviewButtonManager {
}
this.#configureButton(id, ppcpConfig);
}
};
if (this.#onInit) {
this.#onInit.then(createButton);
@ -209,9 +215,9 @@ class PreviewButtonManager {
*/
renderButtons() {
if (this.isEnabled) {
Object.values(this.buttons).forEach(button => button.render())
Object.values(this.buttons).forEach(button => button.render());
} else {
Object.values(this.buttons).forEach(button => button.remove())
Object.values(this.buttons).forEach(button => button.remove());
}
return this;