Skip to content

Commit f6225ab

Browse files
committed
feat: enhance version switch UI with warning and button state management
1 parent 5af841c commit f6225ab

File tree

1 file changed

+42
-15
lines changed

1 file changed

+42
-15
lines changed

src/php/settings/version-switch.php

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -378,32 +378,44 @@ function render_version_switch_field( array $args ): void {
378378
</p>
379379

380380
<p>
381-
<button type="button" id="switch-version-btn" class="button button-secondary"
381+
<button type="button" id="switch-version-btn" class="button button-secondary" disabled
382382
<?php disabled( empty( $available_versions ) ); ?>>
383383
<?php esc_html_e( 'Switch Version', 'code-snippets' ); ?>
384384
</button>
385385
</p>
386386

387387
<div id="version-switch-result" class="notice" style="display: none;"></div>
388-
389-
<p class="description">
390-
<?php
391-
esc_html_e( 'Warning: Switching versions may cause compatibility issues. Always backup your site before switching versions. This feature allows you to install different versions of the Code Snippets plugin.', 'code-snippets' );
392-
?>
393-
</p>
394388
<?php endif; ?>
395389
</div>
396390

397391
<script type="text/javascript">
398392
jQuery(document).ready(function($) {
399-
$('#switch-version-btn').on('click', function() {
400-
var targetVersion = $('#target_version').val();
401-
var $button = $(this);
402-
var $result = $('#version-switch-result');
393+
var currentVersion = '<?php echo esc_js( $current_version ); ?>';
394+
var $button = $('#switch-version-btn');
395+
var $dropdown = $('#target_version');
396+
var $result = $('#version-switch-result');
397+
398+
// Handle dropdown changes - enable/disable button and show/hide warning
399+
$dropdown.on('change', function() {
400+
var selectedVersion = $(this).val();
401+
402+
if (!selectedVersion || selectedVersion === currentVersion) {
403+
// Current version or no selection - disable button and hide warning
404+
$button.prop('disabled', true);
405+
$('#version-switch-warning').hide();
406+
} else {
407+
// Different version selected - enable button and show warning
408+
$button.prop('disabled', false);
409+
$('#version-switch-warning').show();
410+
}
411+
});
412+
413+
$button.on('click', function() {
414+
var targetVersion = $dropdown.val();
403415

404-
if (!targetVersion) {
416+
if (!targetVersion || targetVersion === currentVersion) {
405417
$result.removeClass('notice-success notice-error').addClass('notice-warning')
406-
.html('<p><?php esc_html_e( 'Please select a version to switch to.', 'code-snippets' ); ?></p>')
418+
.html('<p><?php esc_html_e( 'Please select a different version to switch to.', 'code-snippets' ); ?></p>')
407419
.show();
408420
return;
409421
}
@@ -493,7 +505,7 @@ function render_refresh_versions_field( array $args ): void {
493505
<?php esc_html_e( 'Refresh Available Versions', 'code-snippets' ); ?>
494506
</button>
495507
<p class="description">
496-
<?php esc_html_e( 'Clear the cached list of available versions and fetch the latest from WordPress.org.', 'code-snippets' ); ?>
508+
<?php esc_html_e( 'Check for the latest available plugin versions from WordPress.org.', 'code-snippets' ); ?>
497509
</p>
498510

499511
<script type="text/javascript">
@@ -546,9 +558,24 @@ function ajax_refresh_versions(): void {
546558
get_available_versions();
547559

548560
wp_send_json_success( [
549-
'message' => __( 'Versions cache refreshed successfully.', 'code-snippets' ),
561+
'message' => __( 'Available versions updated successfully.', 'code-snippets' ),
550562
] );
551563
}
552564

553565
// Register AJAX handler
554566
add_action( 'wp_ajax_code_snippets_refresh_versions', __NAMESPACE__ . '\\ajax_refresh_versions' );
567+
568+
/**
569+
* Render the version switch warning that appears at the bottom
570+
* This should be called after all other version-related fields
571+
*/
572+
function render_version_switch_warning(): void {
573+
?>
574+
<div id="version-switch-warning" class="notice notice-warning" style="display: none; margin-top: 20px;">
575+
<p>
576+
<strong><?php esc_html_e( 'Warning:', 'code-snippets' ); ?></strong>
577+
<?php esc_html_e( 'Switching versions may cause compatibility issues. Always backup your site before switching versions.', 'code-snippets' ); ?>
578+
</p>
579+
</div>
580+
<?php
581+
}

0 commit comments

Comments
 (0)