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;
|
|
|
|
|
2024-02-15 16:05:56 +01:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingAgreementsEndpoint;
|
2023-02-14 11:25:23 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
2023-12-12 17:30:55 +00:00
|
|
|
use WooCommerce\PayPalCommerce\WcGateway\Endpoint\RefreshFeatureStatusEndpoint;
|
2023-10-17 16:29:56 +02:00
|
|
|
use WooCommerce\PayPalCommerce\WcSubscriptions\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;
|
|
|
|
|
2024-02-15 16:05:56 +01:00
|
|
|
/**
|
|
|
|
* Billing Agreements endpoint.
|
|
|
|
*
|
|
|
|
* @var BillingAgreementsEndpoint
|
|
|
|
*/
|
|
|
|
private $billing_agreements_endpoint;
|
|
|
|
|
2021-03-25 10:21:28 +02:00
|
|
|
/**
|
|
|
|
* Assets constructor.
|
2021-03-25 13:45:57 +02:00
|
|
|
*
|
2024-02-15 16:05:56 +01:00
|
|
|
* @param string $module_url The url of this module.
|
|
|
|
* @param string $version The assets version.
|
|
|
|
* @param SubscriptionHelper $subscription_helper The subscription helper.
|
|
|
|
* @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.
|
|
|
|
* @param BillingAgreementsEndpoint $billing_agreements_endpoint Billing Agreements endpoint.
|
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,
|
2024-02-15 16:05:56 +01:00
|
|
|
bool $is_acdc_enabled,
|
|
|
|
BillingAgreementsEndpoint $billing_agreements_endpoint
|
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;
|
2024-02-15 16:05:56 +01:00
|
|
|
$this->billing_agreements_endpoint = $billing_agreements_endpoint;
|
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();
|
2023-11-21 16:37:45 +00:00
|
|
|
if ( ! $screen || $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' ),
|
2023-12-12 17:30:55 +00:00
|
|
|
'ajax' => array(
|
|
|
|
'refresh_feature_status' => array(
|
|
|
|
'endpoint' => \WC_AJAX::get_endpoint( RefreshFeatureStatusEndpoint::ENDPOINT ),
|
|
|
|
'nonce' => wp_create_nonce( RefreshFeatureStatusEndpoint::nonce() ),
|
|
|
|
'button' => '.ppcp-refresh-feature-status',
|
2024-01-04 15:23:01 +00:00
|
|
|
'messages' => array(
|
|
|
|
'waiting' => __( 'Checking features...', 'woocommerce-paypal-payments' ),
|
|
|
|
'success' => __( 'Feature status refreshed.', 'woocommerce-paypal-payments' ),
|
|
|
|
),
|
2023-12-12 17:30:55 +00:00
|
|
|
),
|
|
|
|
),
|
2024-02-15 16:05:56 +01:00
|
|
|
'reference_transaction_enabled' => $this->billing_agreements_endpoint->reference_transaction_enabled(),
|
2024-02-15 16:23:19 +01:00
|
|
|
'vaulting_must_enable_advanced_wallet_message' => sprintf(
|
2024-02-15 16:05:56 +01:00
|
|
|
// translators: %1$s and %2$s are the opening and closing of HTML <a> tag.
|
|
|
|
esc_html__( 'Your PayPal account must be enabled for the %1$sAdvanced PayPal Wallet%2$s to use PayPal Vaulting.', 'woocommerce-paypal-payments' ),
|
|
|
|
'<a href="/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=ppcp-gateway&ppcp-tab=ppcp-connection#field-credentials_feature_onboarding_heading">',
|
|
|
|
'</a>'
|
|
|
|
),
|
2023-09-21 11:58:51 +01:00
|
|
|
)
|
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
|
|
|
}
|