Skip to content

Commit

Permalink
Merge pull request #711 from 10up/upkeep/670
Browse files Browse the repository at this point in the history
Add utility to migrate settings
  • Loading branch information
dkotter authored Feb 28, 2024
2 parents aaf9e4a + 976b244 commit b7b5f82
Show file tree
Hide file tree
Showing 15 changed files with 847 additions and 19 deletions.
79 changes: 60 additions & 19 deletions includes/Classifai/Admin/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function register() {
add_action( 'classifai_activation_hook', [ $this, 'add_activation_notice' ] );
add_action( 'admin_notices', [ $this, 'maybe_render_notices' ], 0 );
add_action( 'admin_notices', [ $this, 'thresholds_update_notice' ] );
add_action( 'admin_notices', [ $this, 'v3_migration_completed_notice' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'add_dismiss_script' ] );
add_action( 'wp_ajax_classifai_dismiss_notice', [ $this, 'ajax_maybe_dismiss_notice' ] );
}
Expand Down Expand Up @@ -95,33 +96,35 @@ public function add_dismiss_script() {

$script = <<<EOD
jQuery( function() {
const dismissBtn = document.querySelector( '.classifai-dismissible-notice' );
const dismissBtns = document.querySelectorAll( '.classifai-dismissible-notice' );
if ( ! dismissBtn ) {
if ( ! dismissBtns.length ) {
return;
}
// Add an event listener to the dismiss button.
dismissBtn.addEventListener( 'click', function( event ) {
const id = dismissBtn.getAttribute( 'data-notice' );
// Add an event listener to the dismiss buttons.
dismissBtns.forEach( function( dismissBtn ) {
dismissBtn.addEventListener( 'click', function( event ) {
const id = dismissBtn.getAttribute( 'data-notice' );
if ( ! id ) {
return;
}
if ( ! id ) {
return;
}
const httpRequest = new XMLHttpRequest();
let postData = '';
const httpRequest = new XMLHttpRequest();
let postData = '';
// Build the data to send in our request.
// Data has to be formatted as a string here.
postData += 'notice_id=' + id;
postData += '&action=classifai_dismiss_notice';
postData += '&nonce=$nonce';
// Build the data to send in our request.
// Data has to be formatted as a string here.
postData += 'notice_id=' + id;
postData += '&action=classifai_dismiss_notice';
postData += '&nonce=$nonce';
httpRequest.open( 'POST', '$admin_ajax_url' );
httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' )
httpRequest.send( postData );
});
httpRequest.open( 'POST', '$admin_ajax_url' );
httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' )
httpRequest.send( postData );
});
});
});
EOD;

Expand Down Expand Up @@ -212,4 +215,42 @@ public function thresholds_update_notice() {
<?php
}
}

/**
* Displays the migration completed notice for feature-first refactor
* of the settings.
*
* @since 3.0.0
*/
public function v3_migration_completed_notice() {
// Bail if no need to show the notice.
$display_notice = get_option( 'classifai_display_v3_migration_notice', false );
if ( ! $display_notice ) {
return;
}

// Don't show the notice if the user has already dismissed it.
$key = 'v3_migration_completed';
if ( get_user_meta( get_current_user_id(), "classifai_dismissed_{$key}", true ) ) {
return;
}
?>
<div class="notice notice-info is-dismissible classifai-dismissible-notice classifai-migation-notice" data-notice="<?php echo esc_attr( $key ); ?>">
<p>
<?php
echo wp_kses_post(
sprintf(
// translators: %1$s: <a> tag starting; %2$s: <a> tag closing.
__( '%1$sClassifAI 3.0.0%2$s has changed how AI providers are integrated with individual features. This changes how settings are stored and requires that existing settings be migrated. This migration has happened automatically and you can %3$sverify your settings here%4$s.', 'classifai' ),
'<strong>',
'</strong>',
'<a href="' . esc_url( admin_url( 'tools.php?page=classifai' ) ) . '">',
'</a>'
)
);
?>
</p>
</div>
<?php
}
}
39 changes: 39 additions & 0 deletions includes/Classifai/Features/AudioTranscriptsGeneration.php
Original file line number Diff line number Diff line change
Expand Up @@ -347,4 +347,43 @@ public function add_transcription( string $text = '', int $attachment_id = 0 ) {
return $text;
}
}

/**
* Generates feature setting data required for migration from
* ClassifAI < 3.0.0 to 3.0.0
*
* @return array
*/
public function migrate_settings() {
$old_settings = get_option( 'classifai_openai_whisper', array() );
$new_settings = $this->get_settings();

if ( isset( $old_settings['enable_transcripts'] ) ) {
$new_settings['status'] = $old_settings['enable_transcripts'];
}

$new_settings['provider'] = 'openai_whisper';

if ( isset( $old_settings['api_key'] ) ) {
$new_settings['openai_whisper']['api_key'] = $old_settings['api_key'];
}

if ( isset( $old_settings['authenticated'] ) ) {
$new_settings['openai_whisper']['authenticated'] = $old_settings['authenticated'];
}

if ( isset( $old_settings['speech_to_text_users'] ) ) {
$new_settings['users'] = $old_settings['speech_to_text_users'];
}

if ( isset( $old_settings['speech_to_text_roles'] ) ) {
$new_settings['roles'] = $old_settings['speech_to_text_roles'];
}

if ( isset( $old_settings['speech_to_text_user_based_opt_out'] ) ) {
$new_settings['user_based_opt_out'] = $old_settings['speech_to_text_user_based_opt_out'];
}

return $new_settings;
}
}
143 changes: 143 additions & 0 deletions includes/Classifai/Features/Classification.php
Original file line number Diff line number Diff line change
Expand Up @@ -1029,4 +1029,147 @@ public function get_supported_taxonomies( array $post_types = [] ): array {
*/
return apply_filters( 'classifai_' . static::ID . '_setting_taxonomies', $supported, $this );
}

/**
* Generates feature setting data required for migration from
* ClassifAI < 3.0.0 to 3.0.0
*
* @return array
*/
public function migrate_settings() {
$old_settings = get_option( 'classifai_watson_nlu', array() );
$new_settings = $this->get_default_settings();

if ( isset( $old_settings['authenticated'] ) && $old_settings['authenticated'] ) {
$new_settings['provider'] = 'ibm_watson_nlu';

// Status
if ( isset( $old_settings['enable_content_classification'] ) ) {
$new_settings['status'] = $old_settings['enable_content_classification'];
}

// Post types
if ( isset( $old_settings['post_types'] ) ) {
if ( is_array( $old_settings['post_types'] ) ) {
foreach ( $old_settings['post_types'] as $post_type => $value ) {
if ( 1 === $value ) {
$new_settings['post_types'][ $post_type ] = $post_type;
continue;
} elseif ( is_null( $value ) ) {
$new_settings['post_types'][ $post_type ] = '0';
continue;
}
$new_settings['post_types'][ $post_type ] = $value;
}
}

unset( $new_settings['post_types']['attachment'] );
}

// Post statuses
if ( isset( $old_settings['post_statuses'] ) ) {
if ( is_array( $old_settings['post_statuses'] ) ) {
foreach ( $old_settings['post_statuses'] as $post_status => $value ) {
if ( 1 === $value ) {
$new_settings['post_statuses'][ $post_status ] = $post_status;
continue;
} elseif ( is_null( $value ) ) {
$new_settings['post_statuses'][ $post_status ] = '0';
continue;
}
$new_settings['post_statuses'][ $post_status ] = $value;
}
}
}

// Roles
if ( isset( $old_settings['content_classification_roles'] ) ) {
$new_settings['roles'] = $old_settings['content_classification_roles'];
}

// Users
if ( isset( $old_settings['content_classification_users'] ) ) {
$new_settings['users'] = $old_settings['content_classification_users'];
}

// Provider.
if ( isset( $old_settings['credentials'] ) && isset( $old_settings['credentials']['watson_url'] ) ) {
$new_settings['ibm_watson_nlu']['endpoint_url'] = $old_settings['credentials']['watson_url'];
}

if ( isset( $old_settings['credentials'] ) && isset( $old_settings['credentials']['watson_username'] ) ) {
$new_settings['ibm_watson_nlu']['username'] = $old_settings['credentials']['watson_username'];
}

if ( isset( $old_settings['credentials'] ) && isset( $old_settings['credentials']['watson_password'] ) ) {
$new_settings['ibm_watson_nlu']['password'] = $old_settings['credentials']['watson_password'];
}

if ( isset( $old_settings['classification_mode'] ) ) {
$new_settings['classification_mode'] = $old_settings['classification_mode'];
}

if ( isset( $old_settings['classification_method'] ) ) {
$new_settings['classification_method'] = $old_settings['classification_method'];
}

if ( isset( $old_settings['features'] ) ) {
foreach ( $old_settings['features'] as $feature => $value ) {
$new_settings[ $feature ] = $value;
}
}

if ( isset( $old_settings['authenticated'] ) ) {
$new_settings['ibm_watson_nlu']['authenticated'] = $old_settings['authenticated'];
}

if ( isset( $old_settings['content_classification_user_based_opt_out'] ) ) {
$new_settings['user_based_opt_out'] = $old_settings['content_classification_user_based_opt_out'];
}
} else {
$old_settings = get_option( 'classifai_openai_embeddings', array() );

if ( isset( $old_settings['enable_classification'] ) ) {
$new_settings['status'] = $old_settings['enable_classification'];
}

$new_settings['provider'] = 'openai_embeddings';

if ( isset( $old_settings['api_key'] ) ) {
$new_settings['openai_embeddings']['api_key'] = $old_settings['api_key'];
}

if ( isset( $old_settings['taxonomies'] ) ) {
foreach ( $old_settings['taxonomies'] as $feature => $value ) {
$new_settings[ $feature ] = $value;
}
}

if ( isset( $old_settings['authenticated'] ) ) {
$new_settings['openai_embeddings']['authenticated'] = $old_settings['authenticated'];
}

if ( isset( $old_settings['post_statuses'] ) ) {
$new_settings['post_statuses'] = $old_settings['post_statuses'];
}

if ( isset( $old_settings['post_types'] ) ) {
$new_settings['post_types'] = $old_settings['post_types'];
}

if ( isset( $old_settings['classification_roles'] ) ) {
$new_settings['roles'] = $old_settings['classification_roles'];
}

if ( isset( $old_settings['classification_users'] ) ) {
$new_settings['users'] = $old_settings['classification_users'];
}

if ( isset( $old_settings['classification_user_based_opt_out'] ) ) {
$new_settings['user_based_opt_out'] = $old_settings['classification_user_based_opt_out'];
}
}

return $new_settings;
}
}
51 changes: 51 additions & 0 deletions includes/Classifai/Features/ContentResizing.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,4 +305,55 @@ public function sanitize_default_feature_settings( array $new_settings ): array

return $new_settings;
}

/**
* Generates feature setting data required for migration from
* ClassifAI < 3.0.0 to 3.0.0
*
* @return array
*/
public function migrate_settings() {
$old_settings = get_option( 'classifai_openai_chatgpt', array() );
$new_settings = $this->get_default_settings();

if ( isset( $old_settings['enable_resize_content'] ) ) {
$new_settings['status'] = $old_settings['enable_resize_content'];
}

$new_settings['provider'] = 'openai_chatgpt';

if ( isset( $old_settings['api_key'] ) ) {
$new_settings['openai_chatgpt']['api_key'] = $old_settings['api_key'];
}

if ( isset( $old_settings['authenticated'] ) ) {
$new_settings['openai_chatgpt']['authenticated'] = $old_settings['authenticated'];
}

if ( isset( $old_settings['number_resize_content'] ) ) {
$new_settings['openai_chatgpt']['number_of_suggestions'] = $old_settings['number_resize_content'];
}

if ( isset( $old_settings['shrink_content_prompt'] ) ) {
$new_settings['condense_text_prompt'] = $old_settings['shrink_content_prompt'];
}

if ( isset( $old_settings['grow_content_prompt'] ) ) {
$new_settings['expand_text_prompt'] = $old_settings['grow_content_prompt'];
}

if ( isset( $old_settings['resize_content_roles'] ) ) {
$new_settings['roles'] = $old_settings['resize_content_roles'];
}

if ( isset( $old_settings['resize_content_users'] ) ) {
$new_settings['users'] = $old_settings['resize_content_users'];
}

if ( isset( $old_settings['resize_content_user_based_opt_out'] ) ) {
$new_settings['user_based_opt_out'] = $old_settings['resize_content_user_based_opt_out'];
}

return $new_settings;
}
}
Loading

0 comments on commit b7b5f82

Please sign in to comment.