Skip to content

Commit 1e8d90f

Browse files
author
David Cramer
authored
Merge pull request #111 from cloudinary/develop
develop - master Accepted as a RC-1 for version 2.1.3
2 parents df8dc2b + 735c00c commit 1e8d90f

File tree

16 files changed

+215
-72
lines changed

16 files changed

+215
-72
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.1.2
6+
* Version: 2.1.3
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: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,11 @@
208208
}
209209
}
210210
}
211+
212+
.settings-warning {
213+
display: inline-block;
214+
padding: 5px 7px;
215+
background-color: #ffea98;
216+
border-left: 4px solid #ffcc00;
217+
box-shadow: 0px 0px 5px 0px rgba(112,112,112,0.4);
218+
}

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

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/js/src/components/settings-page.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
(function() {
22

3+
// Disable the "off" dropdown option for Autoplay if
4+
// the player isn't set to Cloudinary or if Show Controls if unchecked.
5+
const disableAutoplayOff = function() {
6+
const player = jQuery( '#field-video_player' ).val();
7+
const showControls = jQuery( '#field-video_controls' ).prop( 'checked' );
8+
const offSelection = jQuery( '#field-video_autoplay_mode option[value="off"]' );
9+
10+
if ( player === 'cld' && ! showControls ) {
11+
offSelection.prop( 'disabled', true );
12+
if ( offSelection.prop( 'selected' ) ) {
13+
offSelection.next().prop( 'selected', true );
14+
}
15+
} else {
16+
offSelection.prop( 'disabled', false );
17+
}
18+
}
19+
20+
disableAutoplayOff();
21+
jQuery( document ).on( 'change', '#field-video_player', disableAutoplayOff );
22+
jQuery( document ).on( 'change', '#field-video_controls', disableAutoplayOff );
23+
324
jQuery( document ).ready( function( $ ) {
425

526
// Initilize instance events

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,8 @@ public function get_usage_stat( $type, $stat = null ) {
363363
if ( 'limit' === $stat && isset( $this->usage[ $type ]['usage'] ) ) {
364364
$value = $this->usage[ $type ]['usage'];
365365
} elseif ( 'used_percent' === $stat && isset( $this->usage[ $type ]['credits_usage'] ) ) {
366-
$value = $this->usage[ $type ]['credits_usage'];
366+
// Calculate percentage based on credit limit and usage.
367+
$value = round( $this->usage[ $type ]['credits_usage']/$this->usage['credits']['limit'] * 100, 2 );
367368
}
368369
}
369370
}
@@ -432,28 +433,27 @@ public function get_config() {
432433
*/
433434
public function usage_notices() {
434435
if ( ! empty( $this->usage ) ) {
435-
$usage_type = 'used_percent';
436-
if ( isset( $this->usage['credits'] ) ) {
437-
$usage_type = 'credits_usage';
438-
}
439436
foreach ( $this->usage as $stat => $values ) {
440437

441-
if ( ! is_array( $values ) || ! isset( $values[ $usage_type ] ) || 0 > $values[ $usage_type ] ) {
438+
if ( ! is_array( $values ) ) {
439+
continue;
440+
}
441+
$usage = $this->get_usage_stat( $stat, 'used_percent' );
442+
if ( empty ( $usage ) ) {
442443
continue;
443444
}
444-
445445
$link = null;
446446
$link_text = null;
447-
if ( 90 <= $values[ $usage_type ] ) {
447+
if ( 90 <= $usage ) {
448448
// 90% used - show error.
449449
$level = 'error';
450450
$link = 'https://cloudinary.com/console/lui/upgrade_options';
451451
$link_text = __( 'upgrade your account', 'cloudinary' );
452-
} elseif ( 80 <= $values[ $usage_type ] ) {
453-
$level = 'warning';
452+
} elseif ( 80 <= $usage ) {
453+
$level = 'warning';
454454
$link_text = __( 'upgrade your account', 'cloudinary' );
455-
} elseif ( 70 <= $values[ $usage_type ] ) {
456-
$level = 'neutral';
455+
} elseif ( 70 <= $usage ) {
456+
$level = 'neutral';
457457
$link_text = __( 'upgrade your account', 'cloudinary' );
458458
} else {
459459
continue;
@@ -465,7 +465,7 @@ public function usage_notices() {
465465
'cloudinary'
466466
),
467467
ucwords( $stat ),
468-
$values[ $usage_type ] . '%',
468+
$usage . '%',
469469
$link,
470470
$link_text
471471
);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1027,7 +1027,7 @@ public function down_sync_asset() {
10271027
if ( $format !== $file_info['extension'] ) {
10281028
// Format transformation.
10291029
$this->set_transformation( $transformations, 'fetch_format', $file_info['extension'] );
1030-
$url = $file_info['dirname'] . '/' . $file_info['filename'] . '.' . $format;
1030+
$url = $file_info['dirname'] . '/' . $file_info['filename'] . '.' . $file_info['extension'];
10311031
}
10321032
// Try to find the Attachment ID in context meta data.
10331033
$attachment_id = $this->get_id_from_sync_key( $sync_key );

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

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class Sync implements Setup, Assets {
4848
'transformation' => '_transformations',
4949
'sync_error' => '_sync_error',
5050
'cloudinary' => '_cloudinary_v2',
51+
'folder_sync' => '_folder_sync',
5152
);
5253

5354
/**
@@ -100,8 +101,9 @@ public function is_active() {
100101
*/
101102
public function is_synced( $post_id ) {
102103
$return = false;
103-
$signature = $this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['signature'], true );
104-
if ( ! empty( $signature ) && $this->generate_signature( $post_id ) === $signature ) {
104+
$signature = $this->get_signature( $post_id );
105+
$expecting = $this->generate_signature( $post_id );
106+
if ( ! empty( $signature ) && ! empty( $expecting ) && $expecting === $signature ) {
105107
$return = $signature;
106108
}
107109

@@ -113,10 +115,16 @@ public function is_synced( $post_id ) {
113115
*
114116
* @param int $post_id The post id to generate a signature for.
115117
*
116-
* @return string
118+
* @return string|bool
117119
*/
118120
public function generate_signature( $post_id ) {
119-
$upload = $this->managers['push']->prepare_upload( $post_id );
121+
$upload = $this->managers['push']->prepare_upload( $post_id );
122+
// Check if has an error (ususally due to file quotas).
123+
if ( is_wp_error( $upload ) ) {
124+
$this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['sync_error'], $upload->get_error_message() );
125+
126+
return false;
127+
}
120128
$credentials = $this->plugin->components['connect']->get_credentials();
121129
$upload['cloud_name'] = $credentials['cloud_name'];
122130
$return = array_map(
@@ -133,6 +141,31 @@ function ( $item ) {
133141
return $return;
134142
}
135143

144+
/**
145+
* Get the current sync signature of an asset.
146+
*
147+
* @param int $post_id The post ID.
148+
*
149+
* @return array|bool
150+
*/
151+
public function get_signature( $post_id ) {
152+
static $signatures = array(); // Cache signatures already fetched.
153+
154+
$return = false;
155+
if ( ! empty( $signatures[ $post_id ] ) ) {
156+
$return = $signatures[ $post_id ];
157+
} else {
158+
$signature = $this->plugin->components['media']->get_post_meta( $post_id, self::META_KEYS['signature'], true );
159+
if ( ! empty( $signature ) ) {
160+
$base_signatures = $this->generate_signature( $post_id );
161+
$signatures[ $post_id ] = wp_parse_args( $signature, $base_signatures );
162+
$return = $signatures[ $post_id ];
163+
}
164+
}
165+
166+
return $return;
167+
}
168+
136169
/**
137170
* Additional component setup.
138171
*/

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,17 @@ function ( $item ) use ( $transformation_index ) {
189189
if ( is_string ( $item ) ) {
190190
return $item;
191191
}
192+
192193
foreach ( $item as $type => $value ) { // phpcs:ignore
193194
$key = array_search( $type, $transformation_index, true );
194-
if ( 'wpsize' === $type ) {
195+
if ( false !== strpos( $type, 'wpsize' ) ) {
195196
if ( ! empty( $item['clean'] ) ) {
196197
continue;
197198
}
199+
198200
$value = '!' . $value . '!';
199201
}
202+
200203
if ( false !== $key ) {
201204
$transform[] = $key . '_' . $value;
202205
}
@@ -236,13 +239,19 @@ public function cloudinary_url( $public_id, $args = array(), $size = array(), $c
236239
$args['version'] = 'v1';
237240
}
238241

242+
// Determine if we're dealing with a fetched
243+
// ...or uploaded image and update the URL accordingly.
244+
$asset_endpoint = filter_var( $public_id, FILTER_VALIDATE_URL ) ? 'fetch' : 'upload';
245+
239246
$url_parts = array(
240247
'https:/',
241-
$this->url( $args['resource_type'], 'upload' ),
248+
$this->url( $args['resource_type'], $asset_endpoint ),
242249
);
250+
243251
if ( ! empty( $args['transformation'] ) ) {
244252
$url_parts[] = self::generate_transformation_string( $args['transformation'] );
245253
}
254+
246255
// Add size.
247256
if ( ! empty( $size ) && is_array( $size ) ) {
248257
if ( true === $clean ) {
@@ -252,7 +261,6 @@ public function cloudinary_url( $public_id, $args = array(), $size = array(), $c
252261
}
253262

254263
$url_parts[] = $args['version'];
255-
256264
$url_parts[] = $public_id;
257265

258266
// Clear out empty parts.

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public function __construct( \Cloudinary\Media $media ) {
4646
public function check_cloudinary_version( $cloudinary_id, $attachment_id ) {
4747
if ( false === $cloudinary_id ) {
4848
// Backwards compat.
49-
$meta = wp_get_attachment_metadata( $attachment_id );
49+
$meta = wp_get_attachment_metadata( $attachment_id );
50+
if ( ! empty( $meta[ Sync::META_KEYS['cloudinary'] ] ) ) {
51+
return $cloudinary_id; // Current version.
52+
}
5053
$public_id = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
5154

5255
/*
@@ -59,6 +62,24 @@ public function check_cloudinary_version( $cloudinary_id, $attachment_id ) {
5962
// Has public ID, but not fully down synced.
6063
$cloudinary_id = $public_id;
6164
}
65+
} else {
66+
// Backwards compat.
67+
$folder_sync = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true );
68+
if ( 0 === strlen( $folder_sync ) ) {
69+
// Does not exist, add it to be compatible with v1.2.2.
70+
$public_id = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
71+
// Set the folder sync to 0 to flag it by default as not synced.
72+
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], '0' );
73+
if ( false !== strpos( $public_id, '/' ) ) {
74+
$path = pathinfo( $public_id );
75+
$asset_folder = trailingslashit( $path['dirname'] );
76+
$cloudinary_folder = trailingslashit( $this->media->plugin->config['settings']['sync_media']['cloudinary_folder'] );
77+
if ( $asset_folder === $cloudinary_folder ) {
78+
// The asset folder matches the defined cloudinary folder, flag it as being in a folder sync.
79+
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], '1' );
80+
}
81+
}
82+
}
6283
}
6384

6485
return $cloudinary_id;

0 commit comments

Comments
 (0)