Skip to content

Commit 9f4ee48

Browse files
authored
Merge pull request #559 from 10up/feature/update-azure-apis-versions
Update all Azure APIs to their latest public version
2 parents 2947c8b + 674ca8a commit 9f4ee48

File tree

8 files changed

+154
-11
lines changed

8 files changed

+154
-11
lines changed

includes/Classifai/Admin/Notifications.php

+136
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Classifai\Admin;
44

5+
use Classifai\Features\DescriptiveTextGenerator;
6+
57
class Notifications {
68

79
/**
@@ -24,6 +26,9 @@ public function can_register(): bool {
2426
public function register() {
2527
add_action( 'classifai_activation_hook', [ $this, 'add_activation_notice' ] );
2628
add_action( 'admin_notices', [ $this, 'maybe_render_notices' ], 0 );
29+
add_action( 'admin_notices', [ $this, 'thresholds_update_notice' ] );
30+
add_action( 'admin_enqueue_scripts', [ $this, 'add_dismiss_script' ] );
31+
add_action( 'wp_ajax_classifai_dismiss_notice', [ $this, 'ajax_maybe_dismiss_notice' ] );
2732
}
2833

2934
/**
@@ -76,4 +81,135 @@ public function maybe_render_notices() {
7681
delete_transient( 'classifai_activation_notice' );
7782
}
7883
}
84+
85+
/**
86+
* Print out a script to dismiss a notice.
87+
*
88+
* This allows us to save that a user has dismissed a notice.
89+
*
90+
* Influenced by https://github.com/WPTT/admin-notices/blob/af52f563398b42cff82d38eefa55c8121d698ebe/src/Dismiss.php#L77
91+
*/
92+
public function add_dismiss_script() {
93+
$nonce = wp_create_nonce( 'classifai_dismissible_notice' );
94+
$admin_ajax_url = esc_url( admin_url( 'admin-ajax.php' ) );
95+
96+
$script = <<<EOD
97+
jQuery( function() {
98+
const dismissBtn = document.querySelector( '.classifai-dismissible-notice' );
99+
100+
if ( ! dismissBtn ) {
101+
return;
102+
}
103+
104+
// Add an event listener to the dismiss button.
105+
dismissBtn.addEventListener( 'click', function( event ) {
106+
const id = dismissBtn.getAttribute( 'data-notice' );
107+
108+
if ( ! id ) {
109+
return;
110+
}
111+
112+
const httpRequest = new XMLHttpRequest();
113+
let postData = '';
114+
115+
// Build the data to send in our request.
116+
// Data has to be formatted as a string here.
117+
postData += 'notice_id=' + id;
118+
postData += '&action=classifai_dismiss_notice';
119+
postData += '&nonce=$nonce';
120+
121+
httpRequest.open( 'POST', '$admin_ajax_url' );
122+
httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' )
123+
httpRequest.send( postData );
124+
});
125+
});
126+
EOD;
127+
128+
wp_add_inline_script( 'common', $script, 'after' );
129+
}
130+
131+
/**
132+
* Verify ajax request and dismiss the notice.
133+
*
134+
* Influenced by https://github.com/WPTT/admin-notices/blob/af52f563398b42cff82d38eefa55c8121d698ebe/src/Dismiss.php#L133
135+
*/
136+
public function ajax_maybe_dismiss_notice() {
137+
if ( ! isset( $_POST['action'] ) || 'classifai_dismiss_notice' !== $_POST['action'] ) {
138+
return;
139+
}
140+
141+
if ( ! isset( $_POST['notice_id'] ) ) {
142+
return;
143+
}
144+
145+
check_ajax_referer( 'classifai_dismissible_notice', 'nonce' );
146+
147+
$notice_id = sanitize_text_field( wp_unslash( $_POST['notice_id'] ) );
148+
149+
update_user_meta( get_current_user_id(), "classifai_dismissed_{$notice_id}", true );
150+
}
151+
152+
/**
153+
* Display a dismissable admin notice when a threshold may need updating.
154+
*
155+
* We used to recommend thresholds between 70-75% but in the latest
156+
* version of the AI Vision API, seems 55% is a better threshold.
157+
*/
158+
public function thresholds_update_notice() {
159+
$features = [
160+
'feature_descriptive_text_generator' => 'Classifai\Features\DescriptiveTextGenerator',
161+
];
162+
163+
foreach ( $features as $name => $feature_class ) {
164+
if ( ! class_exists( $feature_class ) ) {
165+
continue;
166+
}
167+
168+
$feature_instance = new $feature_class();
169+
170+
// Don't show the notice if the feature is not enabled.
171+
if ( ! $feature_instance->is_feature_enabled() ) {
172+
continue;
173+
}
174+
175+
$settings = $feature_instance->get_settings( 'ms_computer_vision' );
176+
$key = '';
177+
$message = '';
178+
179+
switch ( $feature_instance::ID ) {
180+
case DescriptiveTextGenerator::ID:
181+
$key = 'descriptive_confidence_threshold';
182+
$message = __( 'The previous recommended threshold for descriptive text generation was 75% but we find better results now at around 55%.', 'classifai' );
183+
break;
184+
}
185+
186+
// Don't show the notice if the user has already dismissed it.
187+
if ( get_user_meta( get_current_user_id(), "classifai_dismissed_{$key}", true ) ) {
188+
continue;
189+
}
190+
191+
// Don't show the notice if the threshold is already at 55% or lower.
192+
if ( $key && isset( $settings[ $key ] ) && $settings[ $key ] <= 55 ) {
193+
continue;
194+
}
195+
?>
196+
197+
<div class="notice notice-warning is-dismissible classifai-dismissible-notice" data-notice="<?php echo esc_attr( $key ); ?>">
198+
<p>
199+
<?php
200+
echo wp_kses_post(
201+
sprintf(
202+
// translators: %1$s: Feature specific message; %2$s: URL to Feature settings.
203+
__( 'ClassifAI has updated to the v3.2 of the Azure AI Vision API. %1$s <a href="%2$s">Click here to adjust those settings</a>.', 'classifai' ),
204+
esc_html( $message ),
205+
esc_url( admin_url( "tools.php?page=classifai&tab=image_processing&feature=$name" ) )
206+
)
207+
);
208+
?>
209+
</p>
210+
</div>
211+
212+
<?php
213+
}
214+
}
79215
}

includes/Classifai/Providers/Azure/ComputerVision.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ComputerVision extends Provider {
2525
/**
2626
* @var string URL fragment to the analyze API endpoint
2727
*/
28-
protected $analyze_url = 'vision/v3.0/analyze';
28+
protected $analyze_url = 'vision/v3.2/analyze';
2929

3030
/**
3131
* ComputerVision constructor.
@@ -113,7 +113,7 @@ public function add_descriptive_text_generation_fields() {
113113
'min' => 1,
114114
'step' => 1,
115115
'default_value' => $settings['descriptive_confidence_threshold'],
116-
'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' ),
116+
'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' ),
117117
'class' => 'classifai-provider-field hidden provider-scope-' . static::ID, // Important to add this.
118118
]
119119
);
@@ -162,7 +162,7 @@ public function get_default_provider_settings(): array {
162162
return array_merge(
163163
$common_settings,
164164
[
165-
'descriptive_confidence_threshold' => 75,
165+
'descriptive_confidence_threshold' => 55,
166166
]
167167
);
168168

@@ -617,8 +617,14 @@ protected function scan_image( string $image_url, \Classifai\Features\Feature $f
617617
if ( ! is_wp_error( $response ) ) {
618618
$body = json_decode( wp_remote_retrieve_body( $response ) );
619619

620-
if ( 200 !== wp_remote_retrieve_response_code( $response ) && isset( $body->message ) ) {
621-
$rtn = new WP_Error( $body->code ?? 'error', $body->message, $body );
620+
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
621+
if ( isset( $body->error ) ) {
622+
$rtn = new WP_Error( $body->error->code ?? 'error', $body->error->message ?? esc_html__( 'An error occurred.', 'classifai' ), $body );
623+
} elseif ( isset( $body->message ) ) {
624+
$rtn = new WP_Error( $body->code ?? 'error', $body->message, $body );
625+
} else {
626+
$rtn = new WP_Error( 'error', esc_html__( 'An error occurred.', 'classifai' ), $body );
627+
}
622628
} else {
623629
$rtn = $body;
624630
}

includes/Classifai/Providers/Azure/SmartCropping.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class SmartCropping {
2626
*
2727
* @var string
2828
*/
29-
const API_PATH = 'vision/v3.1/generateThumbnail/';
29+
const API_PATH = 'vision/v3.2/generateThumbnail/';
3030

3131
/**
3232
* ComputerVisition settings.

tests/Classifai/Azure/ComputerVisionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function test_get_debug_information() {
4242
$this->assertEquals(
4343
[
4444
'Generate descriptive text' => '0, 0, 0',
45-
'Confidence threshold' => 75,
45+
'Confidence threshold' => 55,
4646
'Latest response:' => 'N/A',
4747
],
4848
$this->provider->get_debug_information(

tests/Classifai/Providers/Azure/ComputerVisionTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function test_no_computer_vision_option_set() {
8585
'endpoint_url' => '',
8686
'api_key' => '',
8787
'authenticated' => false,
88-
'descriptive_confidence_threshold' => 75,
88+
'descriptive_confidence_threshold' => 55,
8989
],
9090
]
9191
);

tests/Classifai/Providers/Azure/SmartCroppingTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function test_get_cropped_thumbnail() {
188188
*/
189189
public function test_get_api_url() {
190190
$this->assertEquals(
191-
'my-api-url.com/vision/v3.1/generateThumbnail/',
191+
'my-api-url.com/vision/v3.2/generateThumbnail/',
192192
$this->get_smart_cropping()->get_api_url()
193193
);
194194
}

tests/cypress/integration/language-processing/classify-content-ibm-watson.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@ describe( '[Language processing] Classify content (IBM Watson - NLU) Tests', ()
237237

238238
it( 'Can create post and taxonomy terms get created by ClassifAI (with default threshold)', () => {
239239
const threshold = 0.7;
240+
240241
// Create Test Post
241242
cy.createPost( {
242243
title: 'Test NLU post',

tests/test-plugin/e2e-test-plugin.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ function classifai_test_mock_http_requests( $preempt, $parsed_args, $url ) {
6464
);
6565
} elseif ( strpos( $url, 'https://api.openai.com/v1/embeddings' ) !== false ) {
6666
$response = file_get_contents( __DIR__ . '/embeddings.json' );
67-
} elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.0/analyze' ) !== false ) {
67+
} elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.2/analyze' ) !== false ) {
6868
$response = file_get_contents( __DIR__ . '/image_analyze.json' );
6969
} elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.2/ocr' ) !== false ) {
7070
$response = file_get_contents( __DIR__ . '/ocr.json' );
71-
} elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.1/generateThumbnail' ) !== false ) {
71+
} elseif ( strpos( $url, 'http://e2e-test-image-processing.test/vision/v3.2/generateThumbnail' ) !== false ) {
7272
$response = file_get_contents( __DIR__ . '../classifai/assets/img/icon256x256.png' );
7373
} elseif ( strpos( $url, 'http://e2e-test-image-processing.test/pdf-read-result' ) !== false ) {
7474
$response = file_get_contents( __DIR__ . '/pdf.json' );

0 commit comments

Comments
 (0)