Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ajax validation unique for each plugin #23

Merged
merged 5 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/Uplink/Admin/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function get_group_name( string $group_modifier = '' ) : string {
*/
public function field_html( array $args = [] ) {
$field = sprintf(
'<div class="%6$s" id="%2$s" data-slug="%2$s" data-plugin="%9$s">
'<div class="%6$s" id="%2$s" data-slug="%2$s" data-plugin="%9$s" data-plugin-slug="%10$s">
<fieldset class="stellarwp-uplink__settings-group">
<input type="%1$s" name="%3$s" value="%4$s" placeholder="%5$s" class="regular-text stellarwp-uplink__settings-field" />
%7$s
Expand All @@ -88,7 +88,8 @@ public function field_html( array $args = [] ) {
esc_attr( $args['html_classes'] ?: '' ),
$this->get_html_content( $args ),
$this->add_nonce_field(),
$args['plugin']
$args['plugin'],
strtolower( str_replace( '-', '_', sanitize_title( Config::get_hook_prefix() ) ) )
jamesckemp marked this conversation as resolved.
Show resolved Hide resolved
);

echo apply_filters( 'stellarwp/uplink/' . Config::get_hook_prefix() . '/license_field_html_render', $field, $args );
Expand Down Expand Up @@ -154,10 +155,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 ] ) ) {
Expand All @@ -171,6 +173,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 "<tr{$class}>";
if ( ! empty( $field['args']['label_for'] ) ) {
Expand Down
7 changes: 4 additions & 3 deletions src/Uplink/Admin/License_Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = Config::get_hook_prefix_underscored();
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( $handle, $css_src );
}
}
4 changes: 3 additions & 1 deletion src/Uplink/Admin/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace StellarWP\Uplink\Admin;

use StellarWP\Uplink\Config;
use StellarWP\Uplink\Uplink;
use StellarWP\Uplink\Contracts\Abstract_Provider;

Expand Down Expand Up @@ -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', Config::get_hook_prefix_underscored() );
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 );
Expand Down
15 changes: 15 additions & 0 deletions src/Uplink/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,21 @@ public static function get_hook_prefix() {
return static::$hook_prefix;
}

/**
* Gets the hook underscored prefix.
*
* @since 1.0.0
*
* @return string
*/
public static function get_hook_prefix_underscored() {
if ( self::$hook_prefix === null ) {
throw new \RuntimeException( 'You must provide a hook prefix via StellarWP\Uplink\Config::set_hook_prefix() before attempting to fetch it.' );
}

return strtolower( str_replace( '-', '_', sanitize_title( static::$hook_prefix ) ) );
}

/**
* Returns whether the container has been set.
*
Expand Down
7 changes: 4 additions & 3 deletions src/admin-views/fields/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = Config::get_hook_prefix_underscored();

?>
<?php if ( $show_title ) : ?>
Expand All @@ -31,7 +32,7 @@
<table class="form-table" role="presentation">
<?php endif; ?>
<div class="stellarwp-uplink__license-field">
<?php $field->do_settings_fields( $group, sprintf( '%s_%s', License_Field::LICENSE_FIELD_ID, sanitize_title( $plugin->get_slug() ) ), $show_title ); ?>
<?php $field->do_settings_fields( $group, sprintf( '%s_%s', License_Field::LICENSE_FIELD_ID, sanitize_title( Config::get_hook_prefix() ) ), $action_postfix, $show_title ); ?>
</div>
<?php if ( $show_title ) : ?>
</table>
Expand Down
3 changes: 2 additions & 1 deletion src/assets/js/key-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() ) {
Expand All @@ -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()
Expand Down