Skip to content

Commit 058611e

Browse files
author
David Cramer
authored
Merge pull request #175 from cloudinary/fix/pre-uat-testing
Fix sync issues detected in internal testing
2 parents ab41c81 + 6643166 commit 058611e

File tree

5 files changed

+114
-44
lines changed

5 files changed

+114
-44
lines changed

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ class Connect implements Config, Setup, Notice {
7979
'url' => 'cloudinary_url',
8080
'connect' => 'cloudinary_connect',
8181
'cache' => 'cloudinary_settings_cache',
82-
'cname' => 'cloudinary_url_cname',
82+
'cname' => 'cloudinary_url_cname',
8383
);
8484

8585
/**
@@ -178,6 +178,7 @@ public function verify_connection( $data ) {
178178

179179
if ( ! empty( $result['message'] ) ) {
180180
add_settings_error( 'cloudinary_connect', $result['type'], $result['message'], 'error' );
181+
181182
return $current;
182183
}
183184

@@ -187,11 +188,11 @@ public function verify_connection( $data ) {
187188
update_option( self::META_KEYS['cname'], $cname[1] );
188189
}
189190

190-
add_settings_error(
191-
'cloudinary_connect',
192-
'connection_success',
193-
__( 'Successfully connected to Cloudinary.', 'cloudinary' ),
194-
'updated'
191+
add_settings_error(
192+
'cloudinary_connect',
193+
'connection_success',
194+
__( 'Successfully connected to Cloudinary.', 'cloudinary' ),
195+
'updated'
195196
);
196197

197198
update_option( self::META_KEYS['signature'], md5( $data['cloudinary_url'] ) );
@@ -202,18 +203,22 @@ public function verify_connection( $data ) {
202203
/**
203204
* Check whether a connection was established.
204205
*
205-
* @return boolean
206+
* @return boolean
206207
*/
207208
public function is_connected() {
208209
$signature = get_option( self::META_KEYS['signature'], null );
209-
210+
210211
if ( null === $signature ) {
211212
return false;
212213
}
213-
214+
// Get the last test transient.
215+
if ( get_transient( $signature ) ) {
216+
return true;
217+
}
218+
214219
$connect_data = get_option( self::META_KEYS['connect'], [] );
215220
$current_url = isset( $connect_data['cloudinary_url'] ) ? $connect_data['cloudinary_url'] : null;
216-
221+
217222
if ( null === $current_url ) {
218223
return false;
219224
}
@@ -236,6 +241,8 @@ public function is_connected() {
236241

237242
return false;
238243
}
244+
// Set a 30 second transient to prevent continued pinging.
245+
set_transient( $signature, true, 30 );
239246

240247
return true;
241248
}
@@ -291,7 +298,7 @@ function ( $a ) {
291298
}
292299

293300
$this->config_from_url( $url );
294-
301+
295302
$test = new Connect\Api( $this, $this->plugin->version );
296303
$test_result = $test->ping();
297304

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1318,11 +1318,16 @@ public function down_sync_asset() {
13181318
$ids = $this->get_id_from_sync_key( 'base_' . $asset['public_id'], true );
13191319
$resync = array();
13201320
foreach ( $ids as $id ) {
1321-
$meta = wp_get_attachment_metadata( $id );
1322-
wp_delete_attachment_files( $id, $meta, array(), get_attached_file( $id ) );
1321+
// Update the version to the asset.
13231322
$this->update_post_meta( $id, Sync::META_KEYS['version'], $asset['version'] );
1324-
// Set signature for storage, since this will be more effective at downloading or not.
1325-
$this->sync->set_signature_item( $id, 'storage', '' );
1323+
// Get the storage state, and only set storage signature if we have local copies.
1324+
$storage_state = $this->get_post_meta( $id, Sync::META_KEYS['storage'], true );
1325+
if ( 'cld' !== $storage_state ) {
1326+
// State is local and Cloudinary. So lets force the storage to downsync again.
1327+
$this->update_post_meta( $id, Sync::META_KEYS['storage'], 'resync' );
1328+
// Set signature for storage, since this will be more effective at downloading or not.
1329+
$this->sync->set_signature_item( $id, 'storage', '' );
1330+
}
13261331
if ( $id !== $asset['attachment_id'] ) {
13271332
$resync[] = wp_prepare_attachment_for_js( $id );
13281333
}
@@ -1802,9 +1807,11 @@ public function match_file_name_with_cloudinary_source( $image_meta ) {
18021807

18031808
if ( false === strpos( $image_meta['file'], $cld_file ) ) {
18041809
$image_meta['file'] = $cld_file;
1805-
1810+
18061811
// Match sizes to exclude sizes suffix.
18071812
if ( isset( $image_meta['sizes'] ) ) {
1813+
// Create backup sizes.
1814+
$image_meta['backup_sizes'] = $image_meta['sizes'];
18081815
// Match sizes to exclude sizes suffix.
18091816
foreach ( $image_meta['sizes'] as &$size ) {
18101817
$size['file'] = basename( $cld_file );

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ public function init() {
120120
$this->components['connect'] = new Connect( $this );
121121

122122
if ( $this->components['connect'] && $this->components['connect']->is_connected() ) {
123-
$this->components['sync'] = new Sync( $this );
124-
$this->components['api'] = new REST_API( $this );
125-
$this->components['media'] = new Media( $this );
126-
$this->components['storage'] = new Storage( $this );
123+
$this->components['sync'] = new Sync( $this );
124+
$this->components['api'] = new REST_API( $this );
125+
$this->components['media'] = new Media( $this );
126+
$this->components['storage'] = new Storage( $this );
127127
}
128128
}
129129

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

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ public function register_admin() {
8888
if ( ! empty( $this->ui['pages'] ) ) {
8989
foreach ( $this->ui['pages'] as $page ) {
9090
// If this page has "require_config" set, ensure we're fully connected to cloudinary.
91-
if (
92-
! empty( $page['requires_config'] ) &&
93-
(
94-
! $this->plugin->config['connect'] ||
91+
if (
92+
! empty( $page['requires_config'] ) &&
93+
(
94+
! $this->plugin->config['connect'] ||
9595
! $this->plugin->components['connect'] ||
96-
! $this->plugin->components['connect']->is_connected()
97-
)
96+
! $this->plugin->components['connect']->is_connected()
97+
)
9898
) {
9999
continue;
100100
}
@@ -179,7 +179,7 @@ private function register_section( $tab_slug ) {
179179
array( $this, 'load_section_content' ),
180180
$setting_slug
181181
);
182-
182+
183183
$this->register_section_fields( $tab, $setting_slug );
184184
}
185185

@@ -194,11 +194,20 @@ private function register_section( $tab_slug ) {
194194
private function register_section_fields( $tab, $setting_slug ) {
195195

196196
foreach ( $tab['fields'] as $field_slug => $field ) {
197+
$field['slug'] = $field_slug;
198+
199+
/**
200+
* Filter the field just before rendering to allow field manipulation.
201+
*
202+
* @var array $field The field array.
203+
* @var string $setting_slug The setting slug
204+
*/
205+
$field = apply_filters( 'cloudinary_render_field', $field, $tab['slug'] );
206+
197207
if ( ! empty( $field['type'] ) && is_callable( $field['type'] ) ) {
198208
continue;
199209
}
200210
$type = ! empty( $field['type'] ) ? $field['type'] : 'text';
201-
$field['slug'] = $field_slug;
202211
$args = $field;
203212
$args['class'] = 'field-row-' . $field['slug'] . ' field-' . $type;
204213
if ( 'heading' !== $type ) {
@@ -306,21 +315,21 @@ public function render_field( $field, $value = null, $show_description = true )
306315
break;
307316
case 'radio':
308317
foreach ( $field['choices'] as $key => $option ) :
309-
?>
310-
<input
311-
type="<?php echo esc_attr( $type ); ?>"
312-
class="cld-field regular-<?php echo esc_attr( $type ); ?>"
313-
id="<?php echo esc_attr( $field['label_for'] . '_' . $key ); ?>"
314-
name="<?php echo esc_attr( $setting_slug ); ?>[<?php echo esc_attr( $field['slug'] ); ?>]"
318+
?>
319+
<input
320+
type="<?php echo esc_attr( $type ); ?>"
321+
class="cld-field regular-<?php echo esc_attr( $type ); ?>"
322+
id="<?php echo esc_attr( $field['label_for'] . '_' . $key ); ?>"
323+
name="<?php echo esc_attr( $setting_slug ); ?>[<?php echo esc_attr( $field['slug'] ); ?>]"
315324
<?php if ( ! empty( $field['pattern'] ) ) : ?>
316325
pattern="<?php echo esc_attr( $field['pattern'] ); ?>"
317-
<?php endif; ?>
318-
data-condition="<?php echo esc_attr( $condition ); ?>"
319-
data-context="<?php echo esc_attr( $context ); ?>"
320-
<?php echo esc_attr( $required ); ?>
321-
<?php checked( $key, $value ); ?>
322-
value="<?php echo esc_attr( $key ); ?>"
323-
/>
326+
<?php endif; ?>
327+
data-condition="<?php echo esc_attr( $condition ); ?>"
328+
data-context="<?php echo esc_attr( $context ); ?>"
329+
<?php echo esc_attr( $required ); ?>
330+
<?php checked( $key, $value ); ?>
331+
value="<?php echo esc_attr( $key ); ?>"
332+
/>
324333
<label for="<?php echo esc_attr( $field['label_for'] . '_' . $key ); ?>"><?php esc_html_e( $option ); ?></label>
325334
<?php
326335
endforeach;
@@ -332,7 +341,22 @@ class="cld-field regular-<?php echo esc_attr( $type ); ?>"
332341
break;
333342
default:
334343
?>
335-
<input <?php echo empty( $field['placeholder'] ) ? '' : sprintf( 'placeholder="%s"', esc_attr( $field['placeholder'] ) ); ?> type="<?php echo esc_attr( $type ); ?>" class="cld-field regular-<?php echo esc_attr( $type ); ?>" id="<?php echo esc_attr( $field['label_for'] ); ?>" name="<?php echo esc_attr( $setting_slug ); ?>[<?php echo esc_attr( $field['slug'] ); ?>]" <?php if ( ! empty( $field['pattern'] ) ) : ?>pattern="<?php echo esc_attr( $field['pattern'] ); ?>"<?php endif; ?> data-condition="<?php echo esc_attr( $condition ); ?>" value="<?php echo esc_attr( $value ); ?>" data-context="<?php echo esc_attr( $context ); ?>" <?php echo esc_attr( $required ); ?>>
344+
<input
345+
<?php echo empty( $field['placeholder'] ) ? '' : sprintf( 'placeholder="%s"', esc_attr( $field['placeholder'] ) ); ?>
346+
type="<?php echo esc_attr( $type ); ?>"
347+
class="cld-field regular-<?php echo esc_attr( $type ); ?>"
348+
id="<?php echo esc_attr( $field['label_for'] ); ?>"
349+
name="<?php echo esc_attr( $setting_slug ); ?>[<?php echo esc_attr( $field['slug'] ); ?>]"
350+
<?php if ( ! empty( $field['pattern'] ) ) : ?>
351+
pattern="<?php echo esc_attr( $field['pattern'] ); ?>"
352+
<?php endif; ?>
353+
<?php if ( ! empty( $field['disabled'] ) ) : ?>
354+
disabled="disabled"
355+
<?php endif; ?>
356+
data-condition="<?php echo esc_attr( $condition ); ?>"
357+
value="<?php echo esc_attr( $value ); ?>"
358+
data-context="<?php echo esc_attr( $context ); ?>"
359+
<?php echo esc_attr( $required ); ?>>
336360
<?php
337361
break;
338362
}

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

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,30 @@ public function __construct( \Cloudinary\Plugin $plugin ) {
8282
add_action( 'cloudinary_register_sync_types', array( $this, 'setup' ), 20 );
8383
// Add File validation sync.
8484
add_filter( 'cloudinary_sync_base_struct', array( $this, 'add_file_folder_validators' ) );
85+
// Add sync storage checks.
86+
add_filter( 'cloudinary_render_field', array( $this, 'maybe_disable_connect' ), 10, 2 );
87+
}
88+
89+
/**
90+
* Disable the cloudinary_url input if media is offloaded and warn the user to sync items.
91+
*
92+
* @param array $field The field settings.
93+
* @param string $slug The settings slug.
94+
*
95+
* @return array
96+
*/
97+
public function maybe_disable_connect( $field, $slug ) {
98+
99+
if ( 'connect' === $slug && 'cloudinary_url' === $field['slug'] ) {
100+
$field['description'] = __( 'Please ensure all media is fully synced before changing the environment variable URL.', 'cloudinary' );
101+
if ( 'dual_full' !== $this->settings['offload'] ) {
102+
$field['suffix'] = null;
103+
$field['description'] = __( 'You can only change the environment variable URL when storage is set to "Cloudinary and WordPress" and all media has been fully synced.', 'cloudinary' );
104+
$field['disabled'] = true;
105+
}
106+
}
107+
108+
return $field;
85109
}
86110

87111
/**
@@ -146,12 +170,16 @@ public function sync( $attachment_id ) {
146170
update_post_meta( $attachment_id, '_wp_attached_file', $this->media->cloudinary_url( $attachment_id ) );
147171
break;
148172
case 'dual_low':
149-
$url = $this->media->cloudinary_url( $attachment_id, 'full', array( array( 'effect' => 'blur:100', 'quality' => $this->settings['low_res'] . ':440' ) ), null, false, true );
173+
$transformations = $this->media->get_transformation_from_meta( $attachment_id );
174+
// Add low quality transformations.
175+
$transformations[] = array( 'effect' => 'blur:100', 'quality' => $this->settings['low_res'] . ':440' );
176+
$url = $this->media->cloudinary_url( $attachment_id, 'full', $transformations, null, false, true );
150177
break;
151178
case 'dual_full':
152179
if ( ! empty( $previous_state ) && 'dual_full' !== $previous_state ) {
153180
// Only do this is it's changing a state.
154-
$url = $this->media->cloudinary_url( $attachment_id, '', array(), null, false, false );
181+
$transformations = $this->media->get_transformation_from_meta( $attachment_id );
182+
$url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, false );
155183
}
156184
break;
157185
}
@@ -180,6 +208,10 @@ public function sync( $attachment_id ) {
180208
protected function remove_local_assets( $attachment_id ) {
181209
// Delete local versions of images.
182210
$meta = wp_get_attachment_metadata( $attachment_id );
211+
if ( ! empty( $meta['backup_sizes'] ) ) {
212+
// Replace backup sizes.
213+
$meta['sizes'] = $meta['backup_sizes'];
214+
}
183215

184216
return wp_delete_attachment_files( $attachment_id, $meta, array(), get_attached_file( $attachment_id ) );
185217
}

0 commit comments

Comments
 (0)