From 1b6d258a47f067dfb1efaae99483a958f9872cc1 Mon Sep 17 00:00:00 2001 From: MlKilderkin Date: Mon, 7 Aug 2023 11:36:30 +0300 Subject: [PATCH 1/5] Make ajax validation unique for each plugin --- src/Uplink/Admin/Field.php | 9 +++++++-- src/Uplink/Admin/License_Field.php | 7 ++++--- src/Uplink/Admin/Provider.php | 4 +++- src/admin-views/fields/settings.php | 7 ++++--- src/assets/js/key-admin.js | 3 ++- 5 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/Uplink/Admin/Field.php b/src/Uplink/Admin/Field.php index 898513a8..0f81dd84 100644 --- a/src/Uplink/Admin/Field.php +++ b/src/Uplink/Admin/Field.php @@ -154,10 +154,11 @@ protected function get_plugin() { * @since 1.0.0 * * @param string $page Slug title of the admin page whose settings fields you want to show. - * @param string $section Slug title of the settings section whose fields you want to show. + * @param string $page Slug title of the admin page whose settings fields you want to show. + * @param string $plugin_slug Slug title of the settings section whose fields you want to show. * @param bool $show_title Whether to show the title or not. */ - public function do_settings_fields( string $page, string $section, bool $show_title = true ) { + public function do_settings_fields( string $page, string $section, string $plugin_slug, bool $show_title = true ) { global $wp_settings_fields; if ( ! isset( $wp_settings_fields[ $page ][ $section ] ) ) { @@ -171,6 +172,10 @@ public function do_settings_fields( string $page, string $section, bool $show_ti $class = ' class="' . esc_attr( $field['args']['class'] ) . '"'; } + if ( ! empty( $plugin_slug ) ) { + $field['args']['slug'] = $plugin_slug; + } + if ( $show_title ) { echo ""; if ( ! empty( $field['args']['label_for'] ) ) { diff --git a/src/Uplink/Admin/License_Field.php b/src/Uplink/Admin/License_Field.php index 1e2da6cb..56aed192 100644 --- a/src/Uplink/Admin/License_Field.php +++ b/src/Uplink/Admin/License_Field.php @@ -91,14 +91,15 @@ public function render( bool $show_title = true, bool $show_button = true ) { * @return void */ public function enqueue_assets() { - $handle = 'stellarwp-uplink-license-admin'; + $handle = sprintf( 'stellarwp-uplink-license-admin-%s', Config::get_hook_prefix() ); $path = preg_replace( '/.*\/vendor/', plugin_dir_url( $this->get_plugin()->get_path() ) . 'vendor', dirname( __DIR__, 2 ) ); $js_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_js_source', $path . '/assets/js/key-admin.js' ); - wp_register_script( $handle, $js_src, [ 'jquery' ], '1.0.0', true ); wp_enqueue_script( $handle ); + $action_postfix = strtolower( str_replace( '-', '_', sanitize_title( Config::get_hook_prefix() ) ) ); + wp_localize_script( $handle, sprintf( 'stellarwp_config_%s', $action_postfix ), [ 'action' => sprintf( 'pue-validate-key-uplink-%s', $action_postfix ) ] ); $css_src = apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/admin_css_source', $path . '/assets/css/main.css' ); - wp_enqueue_style( 'stellarwp-uplink-license-admin', $css_src ); + wp_enqueue_style( sprintf( 'stellarwp-uplink-license-admin-%s', Config::get_hook_prefix() ), $css_src ); } } diff --git a/src/Uplink/Admin/Provider.php b/src/Uplink/Admin/Provider.php index dce36a82..5c6a6ced 100644 --- a/src/Uplink/Admin/Provider.php +++ b/src/Uplink/Admin/Provider.php @@ -2,6 +2,7 @@ namespace StellarWP\Uplink\Admin; +use StellarWP\Uplink\Config; use StellarWP\Uplink\Uplink; use StellarWP\Uplink\Contracts\Abstract_Provider; @@ -38,7 +39,8 @@ public function register_hooks() { add_filter( 'upgrader_install_package_result', [ $this, 'filter_upgrader_install_package_result' ], 10, 2 ); add_filter( 'upgrader_source_selection', [ $this, 'filter_upgrader_source_selection_for_update_prevention' ], 15, 4 ); - add_action( 'wp_ajax_pue-validate-key-uplink', [ $this, 'ajax_validate_license' ], 10, 0 ); + $action = sprintf( 'wp_ajax_pue-validate-key-uplink-%s', strtolower( str_replace( '-', '_', sanitize_title( Config::get_hook_prefix() ) ) ) ); + add_action($action, [ $this, 'ajax_validate_license' ], 10, 0 ); add_action( 'admin_init', [ $this, 'admin_init' ], 10, 0 ); add_action( 'admin_enqueue_scripts', [ $this, 'display_plugin_messages' ], 1, 1 ); add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_assets' ], 10, 0 ); diff --git a/src/admin-views/fields/settings.php b/src/admin-views/fields/settings.php index cbdd18c7..e9e03be3 100644 --- a/src/admin-views/fields/settings.php +++ b/src/admin-views/fields/settings.php @@ -13,8 +13,9 @@ return; } -$field = Config::get_container()->get( License_Field::class ); -$group = $field->get_group_name( sanitize_title( $plugin->get_slug() ) ); +$field = Config::get_container()->get( License_Field::class ); +$group = $field->get_group_name( sanitize_title( $plugin->get_slug() ) ); +$action_postfix = strtolower( str_replace( '-', '_', sanitize_title( Config::get_hook_prefix() ) ) ); ?> @@ -31,7 +32,7 @@ diff --git a/src/assets/js/key-admin.js b/src/assets/js/key-admin.js index 2d5abd22..20acc197 100644 --- a/src/assets/js/key-admin.js +++ b/src/assets/js/key-admin.js @@ -21,6 +21,7 @@ obj.validateKey = function( $el ) { const field = $el.find( 'input[type="text"]' ) const plugin = $el.data( 'plugin' ); + const slug = $el.data( 'plugin-slug' ); let $validityMessage = $el.find( '.key-validity' ); if ( '' === field.val().trim() ) { @@ -38,7 +39,7 @@ field.val( licenseKey ); var data = { - action: 'pue-validate-key-uplink', + action: window[`stellarwp_config_${slug}`]['action'], plugin: plugin, key: licenseKey, _wpnonce: $( $el ).find( '.wp-nonce' ).val() From de1bbf3fff642a7b27a708f9921c31b63f53ef10 Mon Sep 17 00:00:00 2001 From: James Kemp Date: Tue, 8 Aug 2023 10:13:33 +0100 Subject: [PATCH 2/5] Add plugin-slug to field --- src/Uplink/Admin/Field.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Uplink/Admin/Field.php b/src/Uplink/Admin/Field.php index 0f81dd84..a39feaef 100644 --- a/src/Uplink/Admin/Field.php +++ b/src/Uplink/Admin/Field.php @@ -73,7 +73,7 @@ public function get_group_name( string $group_modifier = '' ) : string { */ public function field_html( array $args = [] ) { $field = sprintf( - '
+ '