@@ -68,6 +68,13 @@ class Connect implements Config, Setup, Notice {
6868 */
6969 protected $ notices = array ();
7070
71+ /**
72+ * Account Disabled Flag.
73+ *
74+ * @var bool
75+ */
76+ public $ disabled = false ;
77+
7178 /**
7279 * Holds the meta keys for connect meta to maintain consistency.
7380 */
@@ -79,7 +86,7 @@ class Connect implements Config, Setup, Notice {
7986 'url ' => 'cloudinary_url ' ,
8087 'connect ' => 'cloudinary_connect ' ,
8188 'cache ' => 'cloudinary_settings_cache ' ,
82- 'cname ' => 'cloudinary_url_cname ' ,
89+ 'status ' => 'cloudinary_status ' ,
8390 );
8491
8592 /**
@@ -95,6 +102,8 @@ class Connect implements Config, Setup, Notice {
95102 public function __construct ( Plugin $ plugin ) {
96103 $ this ->plugin = $ plugin ;
97104 add_filter ( 'pre_update_option_cloudinary_connect ' , array ( $ this , 'verify_connection ' ) );
105+ add_filter ( 'cron_schedules ' , array ( $ this , 'get_status_schedule ' ) );
106+ add_action ( 'cloudinary_status ' , array ( $ this , 'check_status ' ) );
98107 }
99108
100109 /**
@@ -144,7 +153,6 @@ public function media_library_script() {
144153 public function verify_connection ( $ data ) {
145154 if ( empty ( $ data ['cloudinary_url ' ] ) ) {
146155 delete_option ( self ::META_KEYS ['signature ' ] );
147- delete_option ( self ::META_KEYS ['cname ' ] );
148156
149157 add_settings_error (
150158 'cloudinary_connect ' ,
@@ -164,9 +172,6 @@ public function verify_connection( $data ) {
164172 return $ data ;
165173 }
166174
167- // Always clear out CNAME when re-saving.
168- delete_option ( self ::META_KEYS ['cname ' ] );
169-
170175 // Pattern match to ensure validity of the provided url
171176 if ( ! preg_match ( '~ ' . self ::CLOUDINARY_VARIABLE_REGEX . '~ ' , $ data ['cloudinary_url ' ] ) ) {
172177 add_settings_error (
@@ -187,12 +192,6 @@ public function verify_connection( $data ) {
187192 return $ current ;
188193 }
189194
190- // Check if the given URL has a cname and store it if present.
191- $ cname = $ this ->extract_cname ( wp_parse_url ( $ data ['cloudinary_url ' ] ) );
192- if ( $ cname && $ this ->validate_domain ( $ cname ) ) {
193- update_option ( self ::META_KEYS ['cname ' ], $ cname );
194- }
195-
196195 add_settings_error (
197196 'cloudinary_connect ' ,
198197 'connection_success ' ,
@@ -216,11 +215,6 @@ public function is_connected() {
216215 if ( null === $ signature ) {
217216 return false ;
218217 }
219-
220- // Get the last test transient.
221- if ( get_transient ( $ signature ) ) {
222- return true ;
223- }
224218
225219 $ connect_data = get_option ( self ::META_KEYS ['connect ' ], [] );
226220 $ current_url = isset ( $ connect_data ['cloudinary_url ' ] ) ? $ connect_data ['cloudinary_url ' ] : null ;
@@ -233,22 +227,37 @@ public function is_connected() {
233227 return false ;
234228 }
235229
236- $ api = new Connect \Api ( $ this , $ this ->plugin ->version );
237- $ ping = $ api ->ping ();
238-
239- if ( is_wp_error ( $ ping ) || ( is_array ( $ ping ) && $ ping ['status ' ] !== 'ok ' ) ) {
240- delete_option ( self ::META_KEYS ['signature ' ] );
241-
242- $ this ->notices [] = array (
243- 'message ' => __ ( 'You have been disconnected due to an account error. ' , 'cloudinary ' ),
244- 'type ' => 'error ' ,
245- 'dismissible ' => true ,
246- );
230+ $ status = get_option ( self ::META_KEYS ['status ' ], null );
231+ if ( is_wp_error ( $ status ) ) {
232+ // Error, we stop here.
233+ if ( ! isset ( $ this ->notices ['__status ' ] ) ) {
234+ $ error = $ status ->get_error_message ();
235+ $ message = sprintf (
236+ // translators: Placeholder refers the error from API.
237+ __ ( 'Cloudinary Error: %s ' , 'cloudinary ' ),
238+ ucwords ( $ error )
239+ );
240+ if ( 'disabled account ' === strtolower ( $ error ) ) {
241+ // Flag general disabled.
242+ $ this ->disabled = true ;
243+ $ message = sprintf (
244+ // translators: Placeholders are <a> tags.
245+ __ ( 'Cloudinary Account Disabled. %1$s Upgrade your plan %3$s or %2$s submit a support request %3$s for assistance. ' , 'cloudinary ' ),
246+ '<a href="https://cloudinary.com/console/upgrade_options" target="_blank"> ' ,
247+ '<a href="https://support.cloudinary.com/hc/en-us/requests/new" target="_blank"> ' ,
248+ '</a> '
249+ );
250+ }
251+ $ this ->notices ['__status ' ] = array (
252+ 'message ' => $ message ,
253+ 'type ' => 'error ' ,
254+ 'dismissible ' => true ,
255+ );
256+ }
247257
248258 return false ;
249259 }
250- // Set a 30 second transient to prevent continued pinging.
251- set_transient ( $ signature , true , 30 );
260+
252261
253262 return true ;
254263 }
@@ -296,26 +305,51 @@ function ( $a ) {
296305 }
297306
298307 $ this ->config_from_url ( $ url );
299-
300- $ test = new Connect \Api ( $ this , $ this ->plugin ->version );
301- $ test_result = $ test ->ping ();
308+ $ test_result = $ this ->check_status ();
302309
303310 if ( is_wp_error ( $ test_result ) ) {
304- $ result ['type ' ] = 'connection_error ' ;
311+ $ error = $ test_result ->get_error_message ();
312+ if ( 'disabled account ' !== strtolower ( $ error ) ) {
313+ // Account Disabled, is still successful, so allow it, else we will never be able to change it.
314+ $ result ['type ' ] = 'connection_error ' ;
315+ }
305316 $ result ['message ' ] = ucwords ( str_replace ( '_ ' , ' ' , $ test_result ->get_error_message () ) );
306317 } else {
307- $ this ->api = $ test ;
308318 $ this ->usage_stats ( true );
309319 }
310320
311321 return $ result ;
312322 }
313323
324+ /**
325+ * Check the status of Cloudinary.
326+ *
327+ * @return array|\WP_Error
328+ */
329+ public function check_status () {
330+ $ status = $ this ->test_ping ();
331+ update_option ( self ::META_KEYS ['status ' ], $ status );
332+
333+ return $ status ;
334+ }
335+
336+ /**
337+ * Do a ping test on the API.
338+ *
339+ * @return array|\WP_Error
340+ */
341+ public function test_ping () {
342+ $ test = new Connect \Api ( $ this , $ this ->plugin ->version );
343+ $ this ->api = $ test ;
344+
345+ return $ test ->ping ();
346+ }
347+
314348 /**
315349 * Extracts the CNAME from a parsed connection URL.
316350 *
317351 * @param array $parsed_url
318- *
352+ *
319353 * @return string|null
320354 */
321355 protected function extract_cname ( $ parsed_url ) {
@@ -336,7 +370,7 @@ protected function extract_cname( $parsed_url ) {
336370 * Safely validate a domain.
337371 *
338372 * @param string $domain
339- *
373+ *
340374 * @return bool
341375 */
342376 protected function validate_domain ( $ domain ) {
@@ -416,6 +450,12 @@ public function config_from_url( $url ) {
416450 $ this ->set_credentials ( $ config_params );
417451 }
418452 }
453+
454+ // Specifically set CNAME
455+ $ cname = $ this ->extract_cname ( $ parts );
456+ if ( ! empty ( $ cname ) ) {
457+ $ this ->set_credentials ( array ( 'cname ' => $ cname ) );
458+ }
419459 }
420460
421461 /**
@@ -430,6 +470,33 @@ public function setup() {
430470 $ this ->config_from_url ( $ config ['cloudinary_url ' ] );
431471 $ this ->api = new Connect \Api ( $ this , $ this ->plugin ->version );
432472 $ this ->usage_stats ();
473+ $ this ->setup_status_cron ();
474+ }
475+ }
476+
477+ /**
478+ * Add our every minute schedule.
479+ *
480+ * @param array $schedules Array of schedules.
481+ *
482+ * @return array
483+ */
484+ public function get_status_schedule ( $ schedules ) {
485+ $ schedules ['every_minute ' ] = array (
486+ 'interval ' => MINUTE_IN_SECONDS ,
487+ 'display ' => __ ( 'Every Minute ' , 'cloudinary ' ),
488+ );
489+
490+ return $ schedules ;
491+ }
492+
493+ /**
494+ * Setup Status cron.
495+ */
496+ protected function setup_status_cron () {
497+ if ( false === wp_get_schedule ( 'cloudinary_status ' ) ) {
498+ $ now = current_time ( 'timestamp ' );
499+ wp_schedule_event ( $ now + ( MINUTE_IN_SECONDS ), 'every_minute ' , 'cloudinary_status ' );
433500 }
434501 }
435502
0 commit comments