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

Update all Azure APIs to their latest public version #559

Merged
merged 15 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
136 changes: 136 additions & 0 deletions includes/Classifai/Admin/Notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Classifai\Admin;

use Classifai\Features\DescriptiveTextGenerator;

class Notifications {

/**
Expand All @@ -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' ] );
}

/**
Expand Down Expand Up @@ -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 = <<<EOD
jQuery( function() {
const dismissBtn = document.querySelector( '.classifai-dismissible-notice' );

if ( ! dismissBtn ) {
return;
}

// Add an event listener to the dismiss button.
dismissBtn.addEventListener( 'click', function( event ) {
const id = dismissBtn.getAttribute( 'data-notice' );

if ( ! id ) {
return;
}

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';

httpRequest.open( 'POST', '$admin_ajax_url' );
httpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' )
httpRequest.send( postData );
});
});
EOD;

wp_add_inline_script( 'common', $script, 'after' );
}

/**
* Verify ajax request and dismiss the notice.
*
* Influenced by https://github.com/WPTT/admin-notices/blob/af52f563398b42cff82d38eefa55c8121d698ebe/src/Dismiss.php#L133
*/
public function ajax_maybe_dismiss_notice() {
if ( ! isset( $_POST['action'] ) || 'classifai_dismiss_notice' !== $_POST['action'] ) {
return;
}

if ( ! isset( $_POST['notice_id'] ) ) {
return;
}

check_ajax_referer( 'classifai_dismissible_notice', 'nonce' );

$notice_id = sanitize_text_field( wp_unslash( $_POST['notice_id'] ) );

update_user_meta( get_current_user_id(), "classifai_dismissed_{$notice_id}", true );
}

/**
* Display a dismissable admin notice when a threshold may need updating.
*
* We used to recommend thresholds between 70-75% but in the latest
* version of the AI Vision API, seems 55% is a better threshold.
*/
public function thresholds_update_notice() {
$features = [
'feature_descriptive_text_generator' => '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;
}
?>

<div class="notice notice-warning is-dismissible classifai-dismissible-notice" data-notice="<?php echo esc_attr( $key ); ?>">
<p>
<?php
echo wp_kses_post(
sprintf(
// translators: %1$s: Feature specific message; %2$s: URL to Feature settings.
__( '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' ),
esc_html( $message ),
esc_url( admin_url( "tools.php?page=classifai&tab=image_processing&feature=$name" ) )
)
);
?>
</p>
</div>

<?php
}
}
}
16 changes: 11 additions & 5 deletions includes/Classifai/Providers/Azure/ComputerVision.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ComputerVision extends Provider {
/**
* @var string URL fragment to the analyze API endpoint
*/
protected $analyze_url = 'vision/v3.0/analyze';
protected $analyze_url = 'vision/v3.2/analyze';

/**
* ComputerVision constructor.
Expand Down Expand Up @@ -126,7 +126,7 @@ public function add_descriptive_text_generation_fields() {
'min' => 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.
]
);
Expand Down Expand Up @@ -175,7 +175,7 @@ public function get_default_provider_settings(): array {
return array_merge(
$common_settings,
[
'descriptive_confidence_threshold' => 75,
'descriptive_confidence_threshold' => 55,
]
);

Expand Down Expand Up @@ -630,8 +630,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;
}
Expand Down
2 changes: 1 addition & 1 deletion includes/Classifai/Providers/Azure/SmartCropping.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class SmartCropping {
*
* @var string
*/
const API_PATH = 'vision/v3.1/generateThumbnail/';
const API_PATH = 'vision/v3.2/generateThumbnail/';

/**
* ComputerVisition settings.
Expand Down
2 changes: 1 addition & 1 deletion tests/Classifai/Azure/ComputerVisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/Classifai/Providers/Azure/ComputerVisionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function test_no_computer_vision_option_set() {
'endpoint_url' => '',
'api_key' => '',
'authenticated' => false,
'descriptive_confidence_threshold' => 75,
'descriptive_confidence_threshold' => 55,
],
]
);
Expand Down
2 changes: 1 addition & 1 deletion tests/Classifai/Providers/Azure/SmartCroppingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 2 additions & 2 deletions tests/test-plugin/e2e-test-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,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' );
Expand Down
Loading