@@ -79,6 +79,7 @@ class Connect implements Config, Setup, Notice {
7979 'url ' => 'cloudinary_url ' ,
8080 'connect ' => 'cloudinary_connect ' ,
8181 'cache ' => 'cloudinary_settings_cache ' ,
82+ 'status ' => 'cloudinary_status ' ,
8283 );
8384
8485 /**
@@ -94,6 +95,8 @@ class Connect implements Config, Setup, Notice {
9495 public function __construct ( Plugin $ plugin ) {
9596 $ this ->plugin = $ plugin ;
9697 add_filter ( 'pre_update_option_cloudinary_connect ' , array ( $ this , 'verify_connection ' ) );
98+ add_filter ( 'cron_schedules ' , array ( $ this , 'get_status_schedule ' ) );
99+ add_action ( 'cloudinary_status ' , array ( $ this , 'check_status ' ) );
97100 }
98101
99102 /**
@@ -206,11 +209,6 @@ public function is_connected() {
206209 return false ;
207210 }
208211
209- // Get the last test transient.
210- if ( get_transient ( $ signature ) ) {
211- return true ;
212- }
213-
214212 $ connect_data = get_option ( self ::META_KEYS ['connect ' ], [] );
215213 $ current_url = isset ( $ connect_data ['cloudinary_url ' ] ) ? $ connect_data ['cloudinary_url ' ] : null ;
216214
@@ -221,21 +219,26 @@ public function is_connected() {
221219 if ( md5 ( $ current_url ) !== $ signature ) {
222220 return false ;
223221 }
224- $ this ->config_from_url ( $ current_url );
225- $ api = new Connect \Api ( $ this , $ this ->plugin ->version );
226- $ ping = $ api ->ping ();
227- if ( is_wp_error ( $ ping ) ) {
228- $ this ->notices [] = array (
229- 'message ' => ucwords ( str_replace ( '_ ' , ' ' , $ ping ->get_error_message () ) ),
230- 'type ' => 'error ' ,
231- 'dismissible ' => true ,
232- );
222+
223+ $ status = get_option ( self ::META_KEYS ['status ' ], null );
224+ if ( is_wp_error ( $ status ) ) {
225+ // Error, we stop here.
226+ if ( ! isset ( $ this ->notices ['__status ' ] ) ) {
227+ $ error = ucwords ( str_replace ( '_ ' , ' ' , $ status ->get_error_message () ) );
228+ $ this ->notices ['__status ' ] = array (
229+ 'message ' => sprintf (
230+ // translators: Placeholder refers the error from API.
231+ __ ( 'Cloudinary Error: %s ' , 'cloudinary ' ),
232+ $ error
233+ ),
234+ 'type ' => 'error ' ,
235+ 'dismissible ' => true ,
236+ );
237+ }
233238
234239 return false ;
235240 }
236241
237- // Set a 30 second transient to prevent continued pinging.
238- set_transient ( $ signature , true , 30 );
239242
240243 return true ;
241244 }
@@ -283,9 +286,7 @@ function ( $a ) {
283286 }
284287
285288 $ this ->config_from_url ( $ url );
286-
287- $ test = new Connect \Api ( $ this , $ this ->plugin ->version );
288- $ test_result = $ test ->ping ();
289+ $ test_result = check_status ();
289290
290291 if ( is_wp_error ( $ test_result ) ) {
291292 $ result ['type ' ] = 'connection_error ' ;
@@ -298,6 +299,29 @@ function ( $a ) {
298299 return $ result ;
299300 }
300301
302+ /**
303+ * Check the status of Cloudinary.
304+ *
305+ * @return array|\WP_Error
306+ */
307+ public function check_status () {
308+ $ status = $ this ->test_ping ();
309+ update_option ( self ::META_KEYS ['status ' ], $ status );
310+
311+ return $ status ;
312+ }
313+
314+ /**
315+ * Do a ping test on the API.
316+ *
317+ * @return array|\WP_Error
318+ */
319+ public function test_ping () {
320+ $ test = new Connect \Api ( $ this , $ this ->plugin ->version );
321+
322+ return $ test ->ping ();
323+ }
324+
301325 /**
302326 * Extracts the CNAME from a parsed connection URL.
303327 *
@@ -423,6 +447,23 @@ public function setup() {
423447 $ this ->config_from_url ( $ config ['cloudinary_url ' ] );
424448 $ this ->api = new Connect \Api ( $ this , $ this ->plugin ->version );
425449 $ this ->usage_stats ();
450+ $ this ->setup_status_cron ();
451+ }
452+ }
453+
454+ public function get_status_schedule ( $ schedules ) {
455+ $ schedules ['every_minute ' ] = array (
456+ 'interval ' => MINUTE_IN_SECONDS ,
457+ 'display ' => __ ( 'Every Minute ' , 'cloudinary ' ),
458+ );
459+
460+ return $ schedules ;
461+ }
462+
463+ protected function setup_status_cron () {
464+ if ( false === wp_get_schedule ( 'cloudinary_status ' ) ) {
465+ $ now = current_time ( 'timestamp ' );
466+ wp_schedule_event ( $ now + ( MINUTE_IN_SECONDS ), 'every_minute ' , 'cloudinary_status ' );
426467 }
427468 }
428469
0 commit comments