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

Add utility to migrate settings #711

Merged
merged 22 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
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
39 changes: 39 additions & 0 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 @@ -212,4 +213,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 service providers are integrated with AI features which impacted how settings were handled. Those settings have been migrated automatically, and you can %3$sview them here%4$s.', 'classifai' ),
dkotter marked this conversation as resolved.
Show resolved Hide resolved
'<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;
}
}
54 changes: 54 additions & 0 deletions includes/Classifai/Features/DescriptiveTextGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,58 @@ 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_computer_vision', array() );
$new_settings = $this->get_default_settings();

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

if ( isset( $old_settings['url'] ) ) {
$new_settings['ms_computer_vision']['endpoint_url'] = $old_settings['url'];
}

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

if ( isset( $old_settings['caption_threshold'] ) ) {
$new_settings['ms_computer_vision']['descriptive_confidence_threshold'] = $old_settings['caption_threshold'];
}

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

if ( isset( $old_settings['enable_image_captions'] ) ) {
$new_settings['descriptive_text_fields'] = $old_settings['enable_image_captions'];

foreach ( $new_settings['descriptive_text_fields'] as $key => $value ) {
if ( '0' !== $value ) {
$new_settings['status'] = '1';
break;
}
}
}

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

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

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

return $new_settings;
}
}
Loading
Loading