Skip to content

Commit

Permalink
add deletion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Sidsector9 committed Feb 19, 2024
1 parent 78b1b53 commit 0805395
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 12 deletions.
26 changes: 23 additions & 3 deletions includes/Classifai/Admin/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' ) );
Expand Down Expand Up @@ -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' ) );
}

/**
Expand All @@ -282,11 +301,12 @@ public function v3_migration_notice() {
if ( ! $show_notice ) :
?>
<div class="classifai-migation-notice notice notice-info classifai-dismissible-notice">
<p>
<?php esc_html_e( 'Migrate settings from the older version?' ); ?>
<p id="classifai-migation-notice__interactive">
<?php esc_html_e( 'Migrate settings from the older version?', 'classifai' ); ?>
</p>
<p>
<button type="button" class="button button-primary" id="classifai-migrate-settings"><?php esc_html_e( 'Migrate settings', 'classifai' ); ?></button>
<button type="button" class="button button-primary" id="classifai-delete-pre-v3-settings"><?php esc_html_e( 'Delete old settings', 'classifai' ); ?></button>
&nbsp;
<a href="#" id="classifai-skip-migration-settings"><?php esc_html_e( 'Skip migration', 'classifai' ); ?></a>
</p>
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Features/Classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
3 changes: 1 addition & 2 deletions includes/Classifai/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
28 changes: 22 additions & 6 deletions src/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand All @@ -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, {
Expand All @@ -404,10 +405,25 @@ document.addEventListener( 'DOMContentLoaded', function () {
routine: 'migrate',
nonce: ClassifAI.ajax_nonce,
},
} ).done( function () {
migrateSettingsBtn.text( ogText );
noticeWrapper.html( `<p>${ ClassifAI.migration_complete }</p>` );
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( `<p>${ response.data }</p>` );
} );
} );
} )( jQuery );

0 comments on commit 0805395

Please sign in to comment.