Skip to content

Commit 2f9485f

Browse files
committed
move is_connected to use background checks to use correct credentials
1 parent 0ebe81e commit 2f9485f

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/class-connect.php

Lines changed: 60 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/connect/class-api.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,10 @@ class Api {
143143
public function __construct( $connect, $version ) {
144144
$this->credentials = $connect->get_credentials();
145145
$this->plugin_version = $version;
146+
// Use CNAME.
147+
if ( ! empty( $this->credentials['cname'] ) ) {
148+
$this->asset_url = $this->credentials['cname'];
149+
}
146150
}
147151

148152
/**
@@ -163,10 +167,6 @@ public function url( $resource, $function = null, $endpoint = false ) {
163167
} else {
164168
$parts[] = $this->asset_url;
165169
}
166-
167-
if ( $cname = get_option( Connect::META_KEYS['cname'] ) ) {
168-
$parts[0] = $cname;
169-
}
170170

171171
if ( empty( $this->credentials['cname'] ) || $endpoint ) {
172172
$parts[] = $this->credentials['cloud_name'];

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/ui-definitions/tabs/connect-content.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
?>
9-
<?php if ( ! empty( $this->plugin->config['connect'] ) ) : ?>
9+
<?php if ( $this->plugin->get_component( 'connect' )->is_connected() ) : ?>
1010
<div class="settings-tab-section-card">
1111
<div class="settings-tab-section-fields-dashboard-success">
1212
<span class="dashicons dashicons-yes"></span> <strong><?php esc_html_e( 'Connected to Cloudinary', 'cloudinary' ); ?></strong>

0 commit comments

Comments
 (0)