Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/fix/PIWOO-125' into release/7.4.…
Browse files Browse the repository at this point in the history
…0-beta

# Conflicts:
#	src/Assets/AssetsModule.php
  • Loading branch information
inpsyde-maticluznar committed Sep 11, 2023
2 parents 7c02105 + 2019926 commit 0c72a7c
Showing 2 changed files with 47 additions and 21 deletions.
35 changes: 23 additions & 12 deletions src/Assets/AssetsModule.php
Original file line number Diff line number Diff line change
@@ -32,7 +32,10 @@ public function enqueueBlockCheckoutScripts(Data $dataService, array $gatewayIns
if (!has_block('woocommerce/checkout')) {
return;
}
wp_enqueue_script(MollieCheckoutBlocksSupport::getScriptHandle());
wp_enqueue_style('mollie-gateway-icons');

MollieCheckoutBlocksSupport::localizeWCBlocksData($dataService, $gatewayInstances);
}

public function registerButtonsBlockScripts(string $pluginUrl, string $pluginPath): void
@@ -361,7 +364,7 @@ public function enqueueComponentsAssets(Settings $settingsHelper): void
}

$locale = get_locale();
$locale = str_replace('_formal', '', $locale);
$locale = str_replace('_formal', '', $locale);
$allowedLocaleValues = AcceptedLocaleValuesDictionary::ALLOWED_LOCALES_KEYS_MAP;
if (!in_array($locale, $allowedLocaleValues, true)) {
$locale = AcceptedLocaleValuesDictionary::DEFAULT_LOCALE_VALUE;
@@ -440,7 +443,10 @@ protected function getPluginPath(string $pluginPath, string $path = ''): string
*/
protected function enqueueIconSettings(?string $current_section): void
{
$uri = isset($_SERVER['REQUEST_URI']) ? wc_clean(wp_unslash($_SERVER['REQUEST_URI'])) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$uri = isset($_SERVER['REQUEST_URI']) ? wc_clean(
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
wp_unslash($_SERVER['REQUEST_URI'])
) : '';
if (is_string($uri) && strpos($uri, 'tab=mollie_settings')) {
wp_enqueue_style('mollie-gateway-icons');
}
@@ -538,16 +544,27 @@ protected function setupModuleActions(ContainerInterface $container): void
add_action(
'woocommerce_blocks_loaded',
function () use ($dataService, $gatewayInstances, $pluginUrl, $pluginPath, $hasBlocksEnabled) {
if ($hasBlocksEnabled && class_exists('Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType')) {
if (
$hasBlocksEnabled && is_admin() && class_exists(
'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType'
)
) {
add_action(
'woocommerce_blocks_payment_method_type_registration',
function (PaymentMethodRegistry $paymentMethodRegistry) use ($dataService, $gatewayInstances, $pluginUrl, $pluginPath) {
function (PaymentMethodRegistry $paymentMethodRegistry) use (
$dataService,
$gatewayInstances,
$pluginUrl,
$pluginPath
) {
$paymentMethodRegistry->register(
new MollieCheckoutBlocksSupport(
$dataService,
$gatewayInstances,
$this->getPluginUrl($pluginUrl, '/public/js/mollieBlockIndex.min.js'),
(string) filemtime($this->getPluginPath($pluginPath, '/public/js/mollieBlockIndex.min.js'))
(string)filemtime(
$this->getPluginPath($pluginPath, '/public/js/mollieBlockIndex.min.js')
)
)
);
}
@@ -558,7 +575,7 @@ function (PaymentMethodRegistry $paymentMethodRegistry) use ($dataService, $gate

add_action(
'init',
function () use ($container, $hasBlocksEnabled, $settingsHelper, $pluginUrl, $pluginPath) {
function () use ($container, $hasBlocksEnabled, $settingsHelper, $pluginUrl, $pluginPath, $dataService) {
self::registerFrontendScripts($pluginUrl, $pluginPath);

// Enqueue Scripts
@@ -621,12 +638,6 @@ function () use ($container, $hasBlocksEnabled, $pluginVersion, $dataService, $p
true
);

if ($hasBlocksEnabled) {
/** @var array */
$gatewayInstances = $container->get('gateway.instances');
$this->enqueueBlockCheckoutScripts($dataService, $gatewayInstances);
}

$this->enqueueIconSettings($current_section);
}
}
33 changes: 24 additions & 9 deletions src/Assets/MollieCheckoutBlocksSupport.php
Original file line number Diff line number Diff line change
@@ -10,9 +10,10 @@

final class MollieCheckoutBlocksSupport extends AbstractPaymentMethodType
{
/** @var string $name */
protected $name = "mollie";
/** @var string $scriptHandle */
protected $scriptHandle = "mollie_block_index";
private static $scriptHandle = "mollie_block_index";
/** @var Data */
protected $dataService;
/** @var array */
@@ -28,6 +29,7 @@ public function __construct(
string $registerScriptUrl,
string $registerScriptVersion
) {

$this->dataService = $dataService;
$this->gatewayInstances = $gatewayInstances;
$this->registerScriptUrl = $registerScriptUrl;
@@ -39,27 +41,40 @@ public function initialize()
//
}

public static function getScriptHandle()
{

return self::$scriptHandle;
}

public function get_payment_method_script_handles(): array
{
wp_register_script(
$this->scriptHandle,
self::$scriptHandle,
$this->registerScriptUrl,
['wc-blocks-registry', 'underscore', 'jquery'],
$this->registerScriptVersion,
true
);

self::localizeWCBlocksData($this->dataService, $this->gatewayInstances);

return [self::$scriptHandle];
}

public static function localizeWCBlocksData($dataService, $gatewayInstances)
{

wp_localize_script(
$this->scriptHandle,
self::$scriptHandle,
'mollieBlockData',
[
'gatewayData' => $this->gatewayDataForWCBlocks($this->dataService, $this->gatewayInstances),
'gatewayData' => self::gatewayDataForWCBlocks($dataService, $gatewayInstances),
]
);
return [$this->scriptHandle];
}

private function gatewayDataForWCBlocks(Data $dataService, array $gatewayInstances): array
public static function gatewayDataForWCBlocks(Data $dataService, array $gatewayInstances): array
{
$filters = $dataService->wooCommerceFiltersForCheckout();
$availableGateways = WC()->payment_gateways()->get_available_payment_gateways();
@@ -100,7 +115,7 @@ private function gatewayDataForWCBlocks(Data $dataService, array $gatewayInstanc
$gatewayId = is_string($gateway->paymentMethod()->getProperty('id')) ? $gateway->paymentMethod(
)->getProperty('id') : "";

if ($gateway->enabled !== 'yes' || $gatewayId === 'directdebit') {
if ($gateway->enabled !== 'yes' || ($gatewayId === 'directdebit' && !is_admin())) {
continue;
}
$content = $gateway->paymentMethod()->getProcessedDescriptionForBlock();
@@ -134,7 +149,7 @@ private function gatewayDataForWCBlocks(Data $dataService, array $gatewayInstanc
$gateway->paymentMethod()->getProperty('allowed_countries')
) ? $gateway->paymentMethod()->getProperty('allowed_countries') : [],
'ariaLabel' => $gateway->paymentMethod()->getProperty('defaultDescription'),
'supports' => $this->gatewaySupportsFeatures($gateway->paymentMethod(), $isSepaEnabled),
'supports' => self::gatewaySupportsFeatures($gateway->paymentMethod(), $isSepaEnabled),
'errorMessage' => $gateway->paymentMethod()->getProperty('errorMessage'),
'companyPlaceholder' => $gateway->paymentMethod()->getProperty('companyPlaceholder'),
'phonePlaceholder' => $gateway->paymentMethod()->getProperty('phonePlaceholder'),
@@ -147,7 +162,7 @@ private function gatewayDataForWCBlocks(Data $dataService, array $gatewayInstanc
return $dataToScript;
}

public function gatewaySupportsFeatures(PaymentMethodI $paymentMethod, bool $isSepaEnabled): array
public static function gatewaySupportsFeatures(PaymentMethodI $paymentMethod, bool $isSepaEnabled): array
{
$supports = (array)$paymentMethod->getProperty('supports');
$isSepaPaymentMethod = (bool)$paymentMethod->getProperty('SEPA');

0 comments on commit 0c72a7c

Please sign in to comment.