From 0805395efee18a82fba4c1dab0758dae45b13b54 Mon Sep 17 00:00:00 2001 From: Siddharth Thevaril Date: Mon, 19 Feb 2024 23:15:46 +0530 Subject: [PATCH] add deletion logic --- includes/Classifai/Admin/Notifications.php | 26 +++++++++++++++-- .../Classifai/Features/Classification.php | 2 +- includes/Classifai/Plugin.php | 3 +- src/js/admin.js | 28 +++++++++++++++---- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/includes/Classifai/Admin/Notifications.php b/includes/Classifai/Admin/Notifications.php index b15526c82..470e1143f 100644 --- a/includes/Classifai/Admin/Notifications.php +++ b/includes/Classifai/Admin/Notifications.php @@ -231,6 +231,25 @@ public function v3_migration_script() { wp_send_json_error( esc_html__( 'Migration routine empty.', 'classifai' ) ); } + if ( 'delete_old' === $routine ) { + $old_settings = array( + 'classifai_watson_nlu', + 'classifai_openai_whisper', + 'classifai_openai_chatgpt', + 'classifai_openai_embeddings', + 'classifai_azure_text_to_speech', + 'classifai_openai_whisper', + 'classifai_computer_vision', + 'classifai_openai_dalle', + ); + + foreach ( $old_settings as $old_setting ) { + delete_option( $old_setting ); + } + + wp_send_json_success( esc_html__( 'Old settings deleted.', 'classifai' ) ); + } + if ( 'skip' === $routine ) { update_option( 'classifai-v3-migration-status', true ); wp_send_json_success( esc_html__( 'V3 migration skipped.', 'classifai' ) ); @@ -269,7 +288,7 @@ public function v3_migration_script() { } update_option( 'classifai-v3-migration-status', true ); - wp_send_json_success( esc_html__( 'V3 migration completed', 'classifai' ) ); + wp_send_json_success( esc_html__( 'Migration completed', 'classifai' ) ); } /** @@ -282,11 +301,12 @@ public function v3_migration_notice() { if ( ! $show_notice ) : ?>
-

- +

+

+  

diff --git a/includes/Classifai/Features/Classification.php b/includes/Classifai/Features/Classification.php index b7b2b39cf..d5ae3ddda 100644 --- a/includes/Classifai/Features/Classification.php +++ b/includes/Classifai/Features/Classification.php @@ -256,7 +256,7 @@ public function migrate_settings() { $new_settings['users'] = $old_settings['content_classification_users']; } } else { - $old_settings = get_option( 'classifai_openai_whisper', array() ); + $old_settings = get_option( 'classifai_openai_embeddings', array() ); if ( isset( $old_settings['enable_classification'] ) ) { $new_settings['status'] = $old_settings['enable_classification']; diff --git a/includes/Classifai/Plugin.php b/includes/Classifai/Plugin.php index e9431b2d1..09b84c5ae 100644 --- a/includes/Classifai/Plugin.php +++ b/includes/Classifai/Plugin.php @@ -185,8 +185,7 @@ public function enqueue_admin_assets() { 'ajax_nonce' => wp_create_nonce( 'classifai' ), 'opt_out_enabled_features' => array_keys( $allowed_features ), 'profile_url' => esc_url( get_edit_profile_url( get_current_user_id() ) . '#classifai-profile-features-section' ), - 'migrating_progress' => esc_html__( 'Migrating...', 'classifai' ), - 'migration_complete' => esc_html__( 'Migration complete.', 'classifai' ), + 'migration_progress' => esc_html__( 'Migrating...', 'classifai' ), ]; wp_localize_script( diff --git a/src/js/admin.js b/src/js/admin.js index 9dc275565..75fe8d5d2 100644 --- a/src/js/admin.js +++ b/src/js/admin.js @@ -376,6 +376,8 @@ document.addEventListener( 'DOMContentLoaded', function () { ( function ( $ ) { const noticeWrapper = $( '.classifai-migation-notice' ); const migrateSettingsBtn = $( '#classifai-migrate-settings' ); + const deleteOldSettings = $( '#classifai-delete-pre-v3-settings' ); + const interactiveTxt = $( '#classifai-migation-notice__interactive' ); const skipMigrationBtn = $( '#classifai-skip-migration-settings' ); skipMigrationBtn.on( 'click', function () { @@ -392,9 +394,8 @@ document.addEventListener( 'DOMContentLoaded', function () { } ); migrateSettingsBtn.on( 'click', function () { - const ogText = migrateSettingsBtn.text(); skipMigrationBtn.hide(); - migrateSettingsBtn.text( ClassifAI.migrating_progress ); + migrateSettingsBtn.text( ClassifAI.migration_progress ); migrateSettingsBtn.prop( 'disabled', true ); $.ajax( ajaxurl, { @@ -404,10 +405,25 @@ document.addEventListener( 'DOMContentLoaded', function () { routine: 'migrate', nonce: ClassifAI.ajax_nonce, }, - } ).done( function () { - migrateSettingsBtn.text( ogText ); - noticeWrapper.html( `

${ ClassifAI.migration_complete }

` ); - window.location.reload(); + } ).done( function ( response ) { + migrateSettingsBtn.hide(); + interactiveTxt.text( response.data ); + deleteOldSettings.show(); + } ); + } ); + + deleteOldSettings.on( 'click', function () { + deleteOldSettings.prop( 'disabled', true ); + + $.ajax( ajaxurl, { + type: 'POST', + data: { + action: 'v3_migration_script', + routine: 'delete_old', + nonce: ClassifAI.ajax_nonce, + }, + } ).done( function ( response ) { + noticeWrapper.html( `

${ response.data }

` ); } ); } ); } )( jQuery );