2021-03-25 10:21:28 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Register and configure assets provided by this module.
|
2021-03-25 13:55:43 +02:00
|
|
|
*
|
|
|
|
* @package WooCommerce\PayPalCommerce\WcGateway\Assets
|
2021-03-25 10:21:28 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace WooCommerce\PayPalCommerce\WcGateway\Assets;
|
|
|
|
|
2023-02-14 11:25:23 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
2022-09-02 12:59:53 +02:00
|
|
|
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-04-29 17:40:41 +02:00
|
|
|
|
2021-03-25 13:55:43 +02:00
|
|
|
/**
|
|
|
|
* Class SettingsPageAssets
|
|
|
|
*/
|
2021-03-25 13:45:57 +02:00
|
|
|
class SettingsPageAssets {
|
|
|
|
|
2021-03-25 10:21:28 +02:00
|
|
|
/**
|
2021-03-25 13:55:43 +02:00
|
|
|
* The URL of this module.
|
|
|
|
*
|
2021-03-25 10:21:28 +02:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $module_url;
|
2021-04-29 17:40:41 +02:00
|
|
|
|
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;
|
2021-03-25 10:21:28 +02:00
|
|
|
|
2022-09-02 12:59:53 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2023-02-14 11:25:23 +02:00
|
|
|
/**
|
|
|
|
* The environment object.
|
|
|
|
*
|
|
|
|
* @var Environment
|
|
|
|
*/
|
|
|
|
private $environment;
|
|
|
|
|
2022-11-02 17:36:08 +04:00
|
|
|
/**
|
|
|
|
* Whether Pay Later button is enabled either for checkout, cart or product page.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $is_pay_later_button_enabled;
|
|
|
|
|
2022-11-10 15:16:35 +04:00
|
|
|
/**
|
|
|
|
* The list of disabled funding sources.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $disabled_sources;
|
|
|
|
|
2022-11-17 16:58:58 +04:00
|
|
|
/**
|
|
|
|
* The list of all existing funding sources.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $all_funding_sources;
|
|
|
|
|
2023-08-11 10:35:50 +01:00
|
|
|
/**
|
|
|
|
* Whether it's a settings page of this plugin.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $is_settings_page;
|
|
|
|
|
2023-11-08 09:46:52 +02:00
|
|
|
/**
|
|
|
|
* Whether the ACDC gateway is enabled.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $is_acdc_enabled;
|
|
|
|
|
2021-03-25 10:21:28 +02:00
|
|
|
/**
|
|
|
|
* Assets constructor.
|
2021-03-25 13:45:57 +02:00
|
|
|
*
|
2022-09-02 12:59:53 +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.
|
2023-02-14 11:25:23 +02:00
|
|
|
* @param Environment $environment The environment object.
|
2022-11-02 17:36:08 +04:00
|
|
|
* @param bool $is_pay_later_button_enabled Whether Pay Later button is enabled either for checkout, cart or product page.
|
2022-11-10 15:16:35 +04:00
|
|
|
* @param array $disabled_sources The list of disabled funding sources.
|
2022-11-17 16:58:58 +04:00
|
|
|
* @param array $all_funding_sources The list of all existing funding sources.
|
2023-08-11 10:35:50 +01:00
|
|
|
* @param bool $is_settings_page Whether it's a settings page of this plugin.
|
2023-11-08 09:46:52 +02:00
|
|
|
* @param bool $is_acdc_enabled Whether the ACDC gateway is enabled.
|
2021-03-25 10:21:28 +02:00
|
|
|
*/
|
2022-10-19 11:38:49 +03:00
|
|
|
public function __construct(
|
|
|
|
string $module_url,
|
|
|
|
string $version,
|
|
|
|
SubscriptionHelper $subscription_helper,
|
|
|
|
string $client_id,
|
|
|
|
string $currency,
|
2022-11-02 17:36:08 +04:00
|
|
|
string $country,
|
2023-02-14 11:25:23 +02:00
|
|
|
Environment $environment,
|
2022-11-10 15:16:35 +04:00
|
|
|
bool $is_pay_later_button_enabled,
|
2022-11-17 16:58:58 +04:00
|
|
|
array $disabled_sources,
|
2023-08-11 10:35:50 +01:00
|
|
|
array $all_funding_sources,
|
2023-11-08 09:46:52 +02:00
|
|
|
bool $is_settings_page,
|
|
|
|
bool $is_acdc_enabled
|
2022-10-19 11:38:49 +03:00
|
|
|
) {
|
2022-11-02 17:36:08 +04: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;
|
2023-02-14 11:25:23 +02:00
|
|
|
$this->environment = $environment;
|
2022-11-02 17:36:08 +04:00
|
|
|
$this->is_pay_later_button_enabled = $is_pay_later_button_enabled;
|
2022-11-10 15:16:35 +04:00
|
|
|
$this->disabled_sources = $disabled_sources;
|
2022-11-17 16:58:58 +04:00
|
|
|
$this->all_funding_sources = $all_funding_sources;
|
2023-08-11 10:35:50 +01:00
|
|
|
$this->is_settings_page = $is_settings_page;
|
2023-11-08 09:46:52 +02:00
|
|
|
$this->is_acdc_enabled = $is_acdc_enabled;
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register assets provided by this module.
|
2023-08-11 16:15:10 +01:00
|
|
|
*
|
|
|
|
* @return void
|
2021-03-25 10:21:28 +02:00
|
|
|
*/
|
2023-08-11 16:15:10 +01:00
|
|
|
public function register_assets(): void {
|
2021-04-01 13:22:06 +03:00
|
|
|
add_action(
|
|
|
|
'admin_enqueue_scripts',
|
2021-11-20 13:10:37 -03:00
|
|
|
function() {
|
2022-01-18 13:40:50 +02:00
|
|
|
if ( ! is_admin() || wp_doing_ajax() ) {
|
2021-04-01 13:22:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:35:50 +01:00
|
|
|
if ( $this->is_settings_page ) {
|
|
|
|
$this->register_admin_assets();
|
2021-04-01 13:22:06 +03:00
|
|
|
}
|
|
|
|
|
2023-08-11 10:35:50 +01:00
|
|
|
if ( $this->is_paypal_payment_method_page() ) {
|
|
|
|
$this->register_paypal_admin_assets();
|
|
|
|
}
|
2021-04-01 13:22:06 +03:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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' ) {
|
2021-04-01 13:22:06 +03:00
|
|
|
return false;
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
2021-04-01 13:22:06 +03:00
|
|
|
|
2022-10-19 16:52:43 +02:00
|
|
|
// 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'] ?? '' ) );
|
2022-10-19 16:52:43 +02:00
|
|
|
// 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 );
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-08-11 10:35:50 +01:00
|
|
|
* Register assets for PayPal admin pages.
|
2021-03-25 10:21:28 +02:00
|
|
|
*/
|
2023-08-11 10:35:50 +01:00
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2021-04-01 13:22:06 +03:00
|
|
|
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,
|
2021-04-01 13:22:06 +03:00
|
|
|
true
|
2021-03-25 13:45:57 +02:00
|
|
|
);
|
2022-09-02 12:59:53 +02:00
|
|
|
|
2022-10-19 11:38:49 +03:00
|
|
|
/**
|
|
|
|
* Psalm cannot find it for some reason.
|
|
|
|
*
|
|
|
|
* @psalm-suppress UndefinedConstant
|
|
|
|
*/
|
2022-09-02 12:59:53 +02:00
|
|
|
wp_localize_script(
|
|
|
|
'ppcp-gateway-settings',
|
|
|
|
'PayPalCommerceGatewaySettings',
|
2023-09-21 11:58:51 +01:00
|
|
|
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,
|
2023-11-08 09:46:52 +02:00
|
|
|
'is_acdc_enabled' => $this->is_acdc_enabled,
|
2023-09-21 11:58:51 +01:00
|
|
|
'disabled_sources' => $this->disabled_sources,
|
|
|
|
'all_funding_sources' => $this->all_funding_sources,
|
|
|
|
'components' => array( 'buttons', 'funding-eligibility', 'messages' ),
|
|
|
|
)
|
2022-09-02 12:59:53 +02:00
|
|
|
)
|
|
|
|
);
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
2023-08-11 10:35:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Register assets for PayPal admin pages.
|
|
|
|
*/
|
|
|
|
private function register_admin_assets(): void {
|
2023-09-14 17:48:46 +01:00
|
|
|
wp_enqueue_style(
|
|
|
|
'ppcp-admin-common',
|
|
|
|
trailingslashit( $this->module_url ) . 'assets/css/common.css',
|
|
|
|
array(),
|
|
|
|
$this->version
|
|
|
|
);
|
|
|
|
|
2023-08-11 10:35:50 +01:00
|
|
|
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
|
|
|
}
|