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;
|
|
|
|
|
2021-04-29 17:40:41 +02:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
2021-07-02 12:31:20 +02:00
|
|
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
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
|
|
|
/**
|
|
|
|
* The filesystem path to the module dir.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $module_path;
|
2021-03-25 10:21:28 +02:00
|
|
|
|
2021-04-29 17:40:41 +02:00
|
|
|
/**
|
|
|
|
* The bearer.
|
|
|
|
*
|
|
|
|
* @var Bearer
|
|
|
|
*/
|
|
|
|
private $bearer;
|
|
|
|
|
2021-03-25 10:21:28 +02:00
|
|
|
/**
|
|
|
|
* Assets constructor.
|
2021-03-25 13:45:57 +02:00
|
|
|
*
|
2021-03-25 10:21:28 +02:00
|
|
|
* @param string $module_url The url of this module.
|
2021-03-25 14:29:56 +02:00
|
|
|
* @param string $module_path The filesystem path to this module.
|
2021-04-29 17:40:41 +02:00
|
|
|
* @param Bearer $bearer The bearer.
|
2021-03-25 10:21:28 +02:00
|
|
|
*/
|
2021-04-29 17:40:41 +02:00
|
|
|
public function __construct( string $module_url, string $module_path, Bearer $bearer ) {
|
2021-03-25 14:39:13 +02:00
|
|
|
$this->module_url = $module_url;
|
2021-03-25 14:29:56 +02:00
|
|
|
$this->module_path = $module_path;
|
2021-04-29 17:40:41 +02:00
|
|
|
$this->bearer = $bearer;
|
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-29 17:40:41 +02:00
|
|
|
$bearer = $this->bearer;
|
2021-04-01 13:22:06 +03:00
|
|
|
add_action(
|
|
|
|
'admin_enqueue_scripts',
|
2021-04-29 17:40:41 +02:00
|
|
|
function() use ( $bearer ) {
|
2021-04-01 13:22:06 +03:00
|
|
|
if ( ! is_admin() || is_ajax() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $this->is_paypal_payment_method_page() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-04-29 17:40:41 +02:00
|
|
|
$this->register_admin_assets( $bearer );
|
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-04-29 17:40:41 +02:00
|
|
|
*
|
|
|
|
* @param Bearer $bearer The bearer.
|
2021-03-25 10:21:28 +02:00
|
|
|
*/
|
2021-04-29 17:40:41 +02:00
|
|
|
private function register_admin_assets( Bearer $bearer ) {
|
2021-03-25 14:39:13 +02:00
|
|
|
$gateway_settings_script_path = trailingslashit( $this->module_path ) . 'assets/js/gateway-settings.js';
|
2021-03-25 14:29:56 +02:00
|
|
|
|
2021-04-01 13:22:06 +03:00
|
|
|
wp_enqueue_script(
|
|
|
|
'ppcp-gateway-settings',
|
|
|
|
trailingslashit( $this->module_url ) . 'assets/js/gateway-settings.js',
|
|
|
|
array(),
|
|
|
|
file_exists( $gateway_settings_script_path ) ? (string) filemtime( $gateway_settings_script_path ) : null,
|
|
|
|
true
|
2021-03-25 13:45:57 +02:00
|
|
|
);
|
2021-04-29 17:40:41 +02:00
|
|
|
|
2021-07-02 12:31:20 +02:00
|
|
|
try {
|
|
|
|
$token = $bearer->bearer();
|
|
|
|
wp_localize_script(
|
|
|
|
'ppcp-gateway-settings',
|
|
|
|
'PayPalCommerceGatewaySettings',
|
|
|
|
array(
|
|
|
|
'vaulting_features_available' => $token->vaulting_available(),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} catch ( RuntimeException $exception ) {
|
2021-07-12 11:22:19 +02:00
|
|
|
return;
|
2021-07-02 12:31:20 +02:00
|
|
|
}
|
2021-03-25 10:21:28 +02:00
|
|
|
}
|
2021-03-25 13:45:57 +02:00
|
|
|
}
|