woocommerce-paypal-payments/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php

266 lines
6.7 KiB
PHP
Raw Normal View History

<?php
/**
* Register and configure assets provided by this module.
2021-03-25 13:55:43 +02:00
*
* @package WooCommerce\PayPalCommerce\WcGateway\Assets
*/
declare(strict_types=1);
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
2023-11-08 08:39:53 +02:00
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
2021-03-25 13:55:43 +02:00
/**
* Class SettingsPageAssets
*/
2021-03-25 13:45:57 +02:00
class SettingsPageAssets {
/**
2021-03-25 13:55:43 +02:00
* The URL of this module.
*
* @var string
*/
private $module_url;
2021-03-25 14:29:56 +02:00
/**
2022-02-17 18:51:24 +02:00
* The assets version.
2021-03-25 14:29:56 +02:00
*
* @var string
*/
2022-02-17 18:51:24 +02:00
private $version;
/**
* The subscription helper.
*
* @var SubscriptionHelper
*/
protected $subscription_helper;
2022-10-19 11:38:49 +03:00
/**
* The PayPal SDK client ID.
*
* @var string
*/
private $client_id;
/**
* 3-letter currency code of the shop.
*
* @var string
*/
private $currency;
/**
* 2-letter country code of the shop.
*
* @var string
*/
private $country;
/**
* The environment object.
*
* @var Environment
*/
private $environment;
/**
* Whether Pay Later button is enabled either for checkout, cart or product page.
*
* @var bool
*/
protected $is_pay_later_button_enabled;
/**
* The list of disabled funding sources.
*
* @var array
*/
protected $disabled_sources;
/**
* The list of all existing funding sources.
*
* @var array
*/
protected $all_funding_sources;
/**
* Whether it's a settings page of this plugin.
*
* @var bool
*/
private $is_settings_page;
/**
* Whether the ACDC gateway is enabled.
*
* @var bool
*/
private $is_acdc_enabled;
/**
* Assets constructor.
2021-03-25 13:45:57 +02:00
*
* @param string $module_url The url of this module.
* @param string $version The assets version.
* @param SubscriptionHelper $subscription_helper The subscription helper.
2022-10-19 11:38:49 +03:00
* @param string $client_id The PayPal SDK client ID.
* @param string $currency 3-letter currency code of the shop.
* @param string $country 2-letter country code of the shop.
* @param Environment $environment The environment object.
* @param bool $is_pay_later_button_enabled Whether Pay Later button is enabled either for checkout, cart or product page.
* @param array $disabled_sources The list of disabled funding sources.
* @param array $all_funding_sources The list of all existing funding sources.
* @param bool $is_settings_page Whether it's a settings page of this plugin.
* @param bool $is_acdc_enabled Whether the ACDC gateway is enabled.
*/
2022-10-19 11:38:49 +03:00
public function __construct(
string $module_url,
string $version,
SubscriptionHelper $subscription_helper,
string $client_id,
string $currency,
string $country,
Environment $environment,
bool $is_pay_later_button_enabled,
array $disabled_sources,
array $all_funding_sources,
bool $is_settings_page,
bool $is_acdc_enabled
2022-10-19 11:38:49 +03:00
) {
$this->module_url = $module_url;
$this->version = $version;
$this->subscription_helper = $subscription_helper;
$this->client_id = $client_id;
$this->currency = $currency;
$this->country = $country;
$this->environment = $environment;
$this->is_pay_later_button_enabled = $is_pay_later_button_enabled;
$this->disabled_sources = $disabled_sources;
$this->all_funding_sources = $all_funding_sources;
$this->is_settings_page = $is_settings_page;
$this->is_acdc_enabled = $is_acdc_enabled;
}
/**
* Register assets provided by this module.
2023-08-11 16:15:10 +01:00
*
* @return void
*/
2023-08-11 16:15:10 +01:00
public function register_assets(): void {
add_action(
'admin_enqueue_scripts',
function() {
2022-01-18 13:40:50 +02:00
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}
if ( $this->is_settings_page ) {
$this->register_admin_assets();
}
if ( $this->is_paypal_payment_method_page() ) {
$this->register_paypal_admin_assets();
}
}
);
}
/**
* Check whether the current page is PayPal payment method settings.
*
* @return bool
*/
private function is_paypal_payment_method_page(): bool {
if ( ! function_exists( 'get_current_screen' ) ) {
return false;
}
$screen = get_current_screen();
2022-10-18 15:59:11 +02:00
if ( $screen->id !== 'woocommerce_page_wc-settings' ) {
return false;
}
// phpcs:disable WordPress.Security.NonceVerification.Recommended
2022-10-18 15:59:11 +02:00
$tab = wc_clean( wp_unslash( $_GET['tab'] ?? '' ) );
$section = wc_clean( wp_unslash( $_GET['section'] ?? '' ) );
// phpcs:enable WordPress.Security.NonceVerification.Recommended
2022-10-18 15:59:11 +02:00
2023-11-08 08:39:53 +02:00
return 'checkout' === $tab && in_array( $section, array( PayPalGateway::ID, CardButtonGateway::ID ), true );
}
/**
* Register assets for PayPal admin pages.
*/
private function register_paypal_admin_assets(): void {
2022-10-19 11:38:49 +03:00
wp_enqueue_style(
'ppcp-gateway-settings',
trailingslashit( $this->module_url ) . 'assets/css/gateway-settings.css',
array(),
$this->version
);
wp_enqueue_script(
'ppcp-gateway-settings',
trailingslashit( $this->module_url ) . 'assets/js/gateway-settings.js',
array(),
2022-02-17 18:51:24 +02:00
$this->version,
true
2021-03-25 13:45:57 +02:00
);
2022-10-19 11:38:49 +03:00
/**
* Psalm cannot find it for some reason.
*
* @psalm-suppress UndefinedConstant
*/
wp_localize_script(
'ppcp-gateway-settings',
'PayPalCommerceGatewaySettings',
apply_filters(
'woocommerce_paypal_payments_admin_gateway_settings',
array(
'is_subscriptions_plugin_active' => $this->subscription_helper->plugin_is_active(),
'client_id' => $this->client_id,
'currency' => $this->currency,
'country' => $this->country,
'environment' => $this->environment->current_environment(),
'integration_date' => PAYPAL_INTEGRATION_DATE,
'is_pay_later_button_enabled' => $this->is_pay_later_button_enabled,
'is_acdc_enabled' => $this->is_acdc_enabled,
'disabled_sources' => $this->disabled_sources,
'all_funding_sources' => $this->all_funding_sources,
'components' => array( 'buttons', 'funding-eligibility', 'messages' ),
)
)
);
}
/**
* Register assets for PayPal admin pages.
*/
private function register_admin_assets(): void {
wp_enqueue_style(
'ppcp-admin-common',
trailingslashit( $this->module_url ) . 'assets/css/common.css',
array(),
$this->version
);
wp_enqueue_script(
'ppcp-admin-common',
trailingslashit( $this->module_url ) . 'assets/js/common.js',
array(),
$this->version,
true
);
}
2021-03-25 13:45:57 +02:00
}