2
2
3
3
namespace Classifai \Admin ;
4
4
5
+ use Classifai \Features \DescriptiveTextGenerator ;
6
+
5
7
class Notifications {
6
8
7
9
/**
@@ -24,6 +26,9 @@ public function can_register(): bool {
24
26
public function register () {
25
27
add_action ( 'classifai_activation_hook ' , [ $ this , 'add_activation_notice ' ] );
26
28
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 ' ] );
27
32
}
28
33
29
34
/**
@@ -76,4 +81,135 @@ public function maybe_render_notices() {
76
81
delete_transient ( 'classifai_activation_notice ' );
77
82
}
78
83
}
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
+ }
79
215
}
0 commit comments