Add module boilerplate

This commit is contained in:
Emili Castells Guasch 2023-11-08 12:46:01 +01:00
parent ac7779bc41
commit 1125ca6ad5
14 changed files with 2565 additions and 0 deletions

View file

@ -0,0 +1,45 @@
<?php
/**
* The Card Fields module.
*
* @package WooCommerce\PayPalCommerce\CardFields
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\CardFields;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
use WooCommerce\PayPalCommerce\Vendor\Interop\Container\ServiceProviderInterface;
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
class CardFieldsModule implements ModuleInterface {
public function setup(): ServiceProviderInterface {
return new ServiceProvider(
require __DIR__ . '/../services.php',
require __DIR__ . '/../extensions.php'
);
}
public function run(ContainerInterface $c): void {
if ( ! $c->get( 'card-fields.eligible' ) ) {
return;
}
add_action(
'wp_enqueue_scripts',
function () use ($c) {
$module_url = $c->get( 'card-fields.module.url' );
wp_enqueue_script(
'ppcp-card-fields-boot',
untrailingslashit( $module_url ) . '/assets/js/boot.js',
array( 'jquery' ),
$c->get( 'ppcp.asset-version' ),
true
);
}
);
}
}

View file

@ -0,0 +1,66 @@
<?php
/**
* Service for checking whether Card Fields can be used in the current country and the current currency.
*
* @package WooCommerce\PayPalCommerce\CardFields\Helper
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\CardFields\Helper;
/**
* Class CardFieldsApplies
*/
class CardFieldsApplies {
/**
* The matrix which countries and currency combinations can be used.
*
* @var array
*/
private $allowed_country_currency_matrix;
/**
* 3-letter currency code of the shop.
*
* @var string
*/
private $currency;
/**
* 2-letter country code of the shop.
*
* @var string
*/
private $country;
/**
* CardFieldsApplies constructor.
*
* @param array $allowed_country_currency_matrix The matrix which countries and currency combinations can be used.
* @param string $currency 3-letter currency code of the shop.
* @param string $country 2-letter country code of the shop.
*/
public function __construct(
array $allowed_country_currency_matrix,
string $currency,
string $country
) {
$this->allowed_country_currency_matrix = $allowed_country_currency_matrix;
$this->currency = $currency;
$this->country = $country;
}
/**
* Returns whether Card Fields can be used in the current country and the current currency.
*
* @return bool
*/
public function for_country_currency(): bool {
if ( ! in_array( $this->country, array_keys( $this->allowed_country_currency_matrix ), true ) ) {
return false;
}
return in_array( $this->currency, $this->allowed_country_currency_matrix[ $this->country ], true );
}
}