diff --git a/includes/Classifai/Admin/Notifications.php b/includes/Classifai/Admin/Notifications.php index 307d06865..75a5292bf 100644 --- a/includes/Classifai/Admin/Notifications.php +++ b/includes/Classifai/Admin/Notifications.php @@ -2,6 +2,8 @@ namespace Classifai\Admin; +use Classifai\Features\DescriptiveTextGenerator; + class Notifications { /** @@ -24,6 +26,9 @@ public function can_register(): bool { 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_enqueue_scripts', [ $this, 'add_dismiss_script' ] ); + add_action( 'wp_ajax_classifai_dismiss_notice', [ $this, 'ajax_maybe_dismiss_notice' ] ); } /** @@ -76,4 +81,135 @@ public function maybe_render_notices() { delete_transient( 'classifai_activation_notice' ); } } + + /** + * Print out a script to dismiss a notice. + * + * This allows us to save that a user has dismissed a notice. + * + * Influenced by https://github.com/WPTT/admin-notices/blob/af52f563398b42cff82d38eefa55c8121d698ebe/src/Dismiss.php#L77 + */ + public function add_dismiss_script() { + $nonce = wp_create_nonce( 'classifai_dismissible_notice' ); + $admin_ajax_url = esc_url( admin_url( 'admin-ajax.php' ) ); + + $script = << 'Classifai\Features\DescriptiveTextGenerator', + ]; + + foreach ( $features as $name => $feature_class ) { + if ( ! class_exists( $feature_class ) ) { + continue; + } + + $feature_instance = new $feature_class(); + + // Don't show the notice if the feature is not enabled. + if ( ! $feature_instance->is_feature_enabled() ) { + continue; + } + + $settings = $feature_instance->get_settings( 'ms_computer_vision' ); + $key = ''; + $message = ''; + + switch ( $feature_instance::ID ) { + case DescriptiveTextGenerator::ID: + $key = 'descriptive_confidence_threshold'; + $message = __( 'The previous recommended threshold for descriptive text generation was 75% but we find better results now at around 55%.', 'classifai' ); + break; + } + + // Don't show the notice if the user has already dismissed it. + if ( get_user_meta( get_current_user_id(), "classifai_dismissed_{$key}", true ) ) { + continue; + } + + // Don't show the notice if the threshold is already at 55% or lower. + if ( $key && isset( $settings[ $key ] ) && $settings[ $key ] <= 55 ) { + continue; + } + ?> + +
+

+ Click here to adjust those settings.', 'classifai' ), + esc_html( $message ), + esc_url( admin_url( "tools.php?page=classifai&tab=image_processing&feature=$name" ) ) + ) + ); + ?> +

+
+ + 1, 'step' => 1, 'default_value' => $settings['descriptive_confidence_threshold'], - 'description' => esc_html__( 'Minimum confidence score for automatically added generated text, numeric value from 0-100. Recommended to be set to at least 75.', 'classifai' ), + 'description' => esc_html__( 'Minimum confidence score for automatically added generated text, numeric value from 0-100. Recommended to be set to at least 55.', 'classifai' ), 'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this. ] ); @@ -162,7 +162,7 @@ public function get_default_provider_settings(): array { return array_merge( $common_settings, [ - 'descriptive_confidence_threshold' => 75, + 'descriptive_confidence_threshold' => 55, ] ); @@ -617,8 +617,14 @@ protected function scan_image( string $image_url, \Classifai\Features\Feature $f if ( ! is_wp_error( $response ) ) { $body = json_decode( wp_remote_retrieve_body( $response ) ); - if ( 200 !== wp_remote_retrieve_response_code( $response ) && isset( $body->message ) ) { - $rtn = new WP_Error( $body->code ?? 'error', $body->message, $body ); + if ( 200 !== wp_remote_retrieve_response_code( $response ) ) { + if ( isset( $body->error ) ) { + $rtn = new WP_Error( $body->error->code ?? 'error', $body->error->message ?? esc_html__( 'An error occurred.', 'classifai' ), $body ); + } elseif ( isset( $body->message ) ) { + $rtn = new WP_Error( $body->code ?? 'error', $body->message, $body ); + } else { + $rtn = new WP_Error( 'error', esc_html__( 'An error occurred.', 'classifai' ), $body ); + } } else { $rtn = $body; } diff --git a/includes/Classifai/Providers/Azure/SmartCropping.php b/includes/Classifai/Providers/Azure/SmartCropping.php index c80562e3a..4ab177314 100644 --- a/includes/Classifai/Providers/Azure/SmartCropping.php +++ b/includes/Classifai/Providers/Azure/SmartCropping.php @@ -26,7 +26,7 @@ class SmartCropping { * * @var string */ - const API_PATH = 'vision/v3.1/generateThumbnail/'; + const API_PATH = 'vision/v3.2/generateThumbnail/'; /** * ComputerVisition settings. diff --git a/tests/Classifai/Azure/ComputerVisionTest.php b/tests/Classifai/Azure/ComputerVisionTest.php index 867bf5cef..1abb424e1 100644 --- a/tests/Classifai/Azure/ComputerVisionTest.php +++ b/tests/Classifai/Azure/ComputerVisionTest.php @@ -42,7 +42,7 @@ public function test_get_debug_information() { $this->assertEquals( [ 'Generate descriptive text' => '0, 0, 0', - 'Confidence threshold' => 75, + 'Confidence threshold' => 55, 'Latest response:' => 'N/A', ], $this->provider->get_debug_information( diff --git a/tests/Classifai/Providers/Azure/ComputerVisionTest.php b/tests/Classifai/Providers/Azure/ComputerVisionTest.php index 24ff0bfe0..afcd874b8 100644 --- a/tests/Classifai/Providers/Azure/ComputerVisionTest.php +++ b/tests/Classifai/Providers/Azure/ComputerVisionTest.php @@ -85,7 +85,7 @@ public function test_no_computer_vision_option_set() { 'endpoint_url' => '', 'api_key' => '', 'authenticated' => false, - 'descriptive_confidence_threshold' => 75, + 'descriptive_confidence_threshold' => 55, ], ] ); diff --git a/tests/Classifai/Providers/Azure/SmartCroppingTest.php b/tests/Classifai/Providers/Azure/SmartCroppingTest.php index c9ae44a9a..5d15cd4b4 100644 --- a/tests/Classifai/Providers/Azure/SmartCroppingTest.php +++ b/tests/Classifai/Providers/Azure/SmartCroppingTest.php @@ -188,7 +188,7 @@ public function test_get_cropped_thumbnail() { */ public function test_get_api_url() { $this->assertEquals( - 'my-api-url.com/vision/v3.1/generateThumbnail/', + 'my-api-url.com/vision/v3.2/generateThumbnail/', $this->get_smart_cropping()->get_api_url() ); } diff --git a/tests/cypress/integration/language-processing/classify-content-ibm-watson.test.js b/tests/cypress/integration/language-processing/classify-content-ibm-watson.test.js index 27830c048..578b153da 100644 --- a/tests/cypress/integration/language-processing/classify-content-ibm-watson.test.js +++ b/tests/cypress/integration/language-processing/classify-content-ibm-watson.test.js @@ -237,6 +237,7 @@ describe( '[Language processing] Classify content (IBM Watson - NLU) Tests', () it( 'Can create post and taxonomy terms get created by ClassifAI (with default threshold)', () => { const threshold = 0.7; + // Create Test Post cy.createPost( { title: 'Test NLU post', diff --git a/tests/test-plugin/e2e-test-plugin.php b/tests/test-plugin/e2e-test-plugin.php index de9ea512c..8ce052df1 100644 --- a/tests/test-plugin/e2e-test-plugin.php +++ b/tests/test-plugin/e2e-test-plugin.php @@ -64,11 +64,11 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) { ); } elseif ( strpos( $url, 'https://api.openai.com/v1/embeddings' ) !== false ) { $response = file_get_contents( __DIR__ . '/embeddings.json' ); - } elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.0/analyze' ) !== false ) { + } elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.2/analyze' ) !== false ) { $response = file_get_contents( __DIR__ . '/image_analyze.json' ); } elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.2/ocr' ) !== false ) { $response = file_get_contents( __DIR__ . '/ocr.json' ); - } elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.1/generateThumbnail' ) !== false ) { + } elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.2/generateThumbnail' ) !== false ) { $response = file_get_contents( __DIR__ . '../classifai/assets/img/icon256x256.png' ); } elseif ( strpos( $url, 'http://e2e-test-image-processing.test/pdf-read-result' ) !== false ) { $response = file_get_contents( __DIR__ . '/pdf.json' );