Skip to content

Commit 8fccd96

Browse files
author
David Cramer
authored
Merge pull request #187 from cloudinary/fix/cname-use-credentials
RC-2 fixes
2 parents 637eaf8 + 7da8fb6 commit 8fccd96

File tree

10 files changed

+189
-75
lines changed

10 files changed

+189
-75
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Plugin Name: Cloudinary
44
* Plugin URI: https://cloudinary.com/documentation/wordpress_integration
55
* Description: With the Cloudinary plugin, you can upload and manage your media assets in the cloud, then deliver them to your users through a fast content delivery network, improving your website’s loading speed and overall user experience. Apply multiple transformations and take advantage of a full digital asset management solution without leaving WordPress.
6-
* Version: 2.2.0-rc2
6+
* Version: 2.2.0-rc3
77
* Author: Cloudinary Ltd., XWP
88
* Author URI: https://cloudinary.com/
99
* License: GPLv2+

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/css/cloudinary.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cloudinary-image-management-and-manipulation-in-the-cloud-cdn/css/src/components/_settings.scss

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
font-size : 1.2em;
9191
color : #5F5F5F;
9292

93+
&.expanded {
94+
padding-top : 40px;
95+
margin-bottom : 25px;
96+
}
97+
9398
.dashicons {
9499
color : #AC0000;
95100
}
@@ -108,7 +113,8 @@
108113
color : #23282d;
109114

110115
&.expanded {
111-
padding-top : 40px;
116+
padding-top : 40px;
117+
margin-bottom : 25px;
112118
}
113119

114120
.dashicons {
@@ -210,29 +216,25 @@
210216
}
211217

212218
.settings-warning {
213-
display: inline-block;
214-
padding: 5px 7px;
215-
background-color: #e9faff;
216-
border: 1px solid #ccd0d4;
217-
border-left: 4px solid #00a0d2;
218-
box-shadow: 0 1px 1px rgba(0,0,0,.04);
219+
display : inline-block;
220+
padding : 5px 7px;
221+
background-color : #e9faff;
222+
border : 1px solid #ccd0d4;
223+
border-left : 4px solid #00a0d2;
224+
box-shadow : 0 1px 1px rgba(0, 0, 0, .04);
219225
}
220226

221227
.field-radio {
222228
input[type=radio].cld-field {
223-
margin: 0;
224-
margin-right: 5px;
229+
margin : 0;
230+
margin-right : 5px;
225231
}
226232

227233
label {
228-
margin-right: 10px;
234+
margin-right : 10px;
229235
}
230236
}
231237

232238
.settings-tab-section h2 {
233-
margin: 0;
239+
margin : 0;
234240
}
235-
236-
hr {
237-
margin-top: 25px;
238-
}

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

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

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/php/sync/class-storage.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,12 @@ public function sync( $attachment_id ) {
176176
break;
177177
case 'dual_low':
178178
$transformations = $this->media->get_transformation_from_meta( $attachment_id );
179-
// Add low quality transformations.
180-
$transformations[] = array( 'quality' => 'auto:low' );
181-
$url = $this->media->cloudinary_url( $attachment_id, 'full', $transformations, null, false, true );
179+
// Only low res image items.
180+
if ( ! $this->media->is_preview_only( $attachment_id ) && wp_attachment_is_image( $attachment_id ) ) {
181+
// Add low quality transformations.
182+
$transformations[] = array( 'quality' => 'auto:low' );
183+
}
184+
$url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, true );
182185
break;
183186
case 'dual_full':
184187
if ( ! empty( $previous_state ) && 'dual_full' !== $previous_state ) {

0 commit comments

Comments
 (0)