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;
|
|
|
|
|
2022-09-02 12:59:53 +02:00
|
|
|
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
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;
|
|
|
|
|
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;
|
|
|
|
|
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.
|
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.
|
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,
|
|
|
|
bool $is_pay_later_button_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;
|
|
|
|
$this->is_pay_later_button_enabled = $is_pay_later_button_enabled;
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register assets provided by this module.
|
|
|
|
*/
|
2021-03-25 13:52:19 +02:00
|
|
|
public function register_assets() {
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $this->is_paypal_payment_method_page() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-20 13:10:37 -03:00
|
|
|
$this->register_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();
|
|
|
|
|
|
|
|
$tab = filter_input( INPUT_GET, 'tab', FILTER_SANITIZE_STRING );
|
|
|
|
$section = filter_input( INPUT_GET, 'section', FILTER_SANITIZE_STRING );
|
|
|
|
|
|
|
|
if ( ! 'woocommerce_page_wc-settings' === $screen->id ) {
|
|
|
|
return false;
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
2021-04-01 13:22:06 +03:00
|
|
|
|
|
|
|
return 'checkout' === $tab && 'ppcp-gateway' === $section;
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register assets for admin pages.
|
|
|
|
*/
|
2021-11-20 13:10:37 -03:00
|
|
|
private function register_admin_assets() {
|
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',
|
|
|
|
array(
|
|
|
|
'is_subscriptions_plugin_active' => $this->subscription_helper->plugin_is_active(),
|
2022-10-19 11:38:49 +03:00
|
|
|
'client_id' => $this->client_id,
|
|
|
|
'currency' => $this->currency,
|
|
|
|
'country' => $this->country,
|
|
|
|
'integration_date' => PAYPAL_INTEGRATION_DATE,
|
2022-11-02 17:36:08 +04:00
|
|
|
'is_pay_later_button_enabled' => $this->is_pay_later_button_enabled,
|
2022-09-02 12:59:53 +02:00
|
|
|
)
|
|
|
|
);
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
2021-03-25 13:45:57 +02:00
|
|
|
}
|