register script for the plugin settings page only

This commit is contained in:
Kirill Braslavsky 2021-04-01 13:22:06 +03:00
parent 8d5698614d
commit 1eb9643ff3

View file

@ -42,9 +42,44 @@ class SettingsPageAssets {
* Register assets provided by this module.
*/
public function register_assets() {
if ( is_admin() && ! is_ajax() ) {
$this->register_admin_assets();
add_action(
'admin_enqueue_scripts',
function() {
if ( ! is_admin() || is_ajax() ) {
return;
}
if ( ! $this->is_paypal_payment_method_page() ) {
return;
}
$this->register_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();
$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;
}
return 'checkout' === $tab && 'ppcp-gateway' === $section;
}
/**
@ -53,17 +88,12 @@ class SettingsPageAssets {
private function register_admin_assets() {
$gateway_settings_script_path = trailingslashit( $this->module_path ) . 'assets/js/gateway-settings.js';
add_action(
'admin_enqueue_scripts',
function() use ( $gateway_settings_script_path ) {
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
);
}
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
);
}
}