Skip to content

Commit ce039f2

Browse files
authored
Merge pull request #409 from cloudinary/fix/fetch-delivery-type
Fix fetch delivery type
2 parents 30d00be + e6fd502 commit ce039f2

File tree

5 files changed

+63
-48
lines changed

5 files changed

+63
-48
lines changed

php/class-media.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,7 @@ public function get_cloudinary_id( $attachment_id ) {
11401140
$extension = $image_format;
11411141
}
11421142
}
1143+
$cloudinary_id = $public_id;
11431144
if ( 'fetch' !== $this->get_media_delivery( $attachment_id ) ) {
11441145
$cloudinary_id = $public_id . '.' . $extension;
11451146
}
@@ -1687,14 +1688,7 @@ public function media_column( $cols ) {
16871688
*/
16881689
public function media_column_value( $column_name, $attachment_id ) {
16891690
if ( 'cld_status' === $column_name ) {
1690-
if (
1691-
$this->is_media( $attachment_id ) &&
1692-
in_array(
1693-
$this->get_media_delivery( $attachment_id ),
1694-
$this->get_syncable_delivery_types(),
1695-
true
1696-
)
1697-
) :
1691+
if ( $this->sync->is_syncable( $attachment_id ) ) :
16981692
$status = array(
16991693
'state' => 'inactive',
17001694
'note' => esc_html__( 'Not Synced', 'cloudinary' ),

php/class-sync.php

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Cloudinary\Sync\Push_Sync;
1616
use Cloudinary\Sync\Sync_Queue;
1717
use Cloudinary\Sync\Upload_Sync;
18+
use WP_Error;
1819

1920
/**
2021
* Class Sync
@@ -244,13 +245,7 @@ public function can_sync( $attachment_id, $type = 'file' ) {
244245
}
245246

246247
// Can sync only syncable delivery types.
247-
if (
248-
! in_array(
249-
$this->managers['media']->get_media_delivery( $attachment_id ),
250-
$this->managers['media']->get_syncable_delivery_types(),
251-
true
252-
)
253-
) {
248+
if ( ! $this->is_syncable( $attachment_id ) ) {
254249
$can = false;
255250
}
256251

@@ -304,6 +299,18 @@ public function get_signature( $attachment_id, $cached = true ) {
304299
$return = wp_parse_args( $signature, $this->sync_types );
305300
}
306301

302+
/**
303+
* Filter the get signature of the asset.
304+
*
305+
* @hook cloudinary_get_signature
306+
*
307+
* @param $signature {array} The attachment signature.
308+
* @param $attachment_id {int} The attachment ID.
309+
*
310+
* @return {array}
311+
*/
312+
$return = apply_filters( 'cloudinary_get_signature', $signatures[ $attachment_id ], $attachment_id );
313+
307314
return $return;
308315
}
309316

@@ -328,6 +335,29 @@ public function generate_public_id( $attachment_id ) {
328335
return ltrim( $public_id, '/' );
329336
}
330337

338+
/**
339+
* Is syncable asset.
340+
*
341+
* @param int $attachment_id The attachment ID.
342+
*
343+
* @return bool
344+
*/
345+
public function is_syncable( $attachment_id ) {
346+
$syncable = false;
347+
if (
348+
$this->managers['media']->is_media( $attachment_id )
349+
&& in_array(
350+
$this->managers['media']->get_media_delivery( $attachment_id ),
351+
$this->managers['media']->get_syncable_delivery_types(),
352+
true
353+
)
354+
) {
355+
$syncable = true;
356+
}
357+
358+
return $syncable;
359+
}
360+
331361
/**
332362
* Register a new sync type.
333363
*
@@ -856,6 +886,23 @@ public function filter_get_cloudinary_folder( $value, $slug ) {
856886
return $value;
857887
}
858888

889+
/**
890+
* Filter the signature.
891+
*
892+
* @param array $signature The signature array.
893+
* @param int $attachment_id The attachment ID.
894+
*
895+
* @return array|bool|string|WP_Error
896+
*/
897+
public function get_signature_syncable_type( $signature, $attachment_id ) {
898+
899+
if ( ! $this->is_syncable( $attachment_id ) ) {
900+
$signature = $this->generate_signature( $attachment_id );
901+
}
902+
903+
return $signature;
904+
}
905+
859906
/**
860907
* Checks if auto sync feature is enabled.
861908
*
@@ -916,6 +963,7 @@ public function setup() {
916963
$this->managers['queue']->setup( $this );
917964

918965
add_filter( 'cloudinary_setting_get_value', array( $this, 'filter_get_cloudinary_folder' ), 10, 2 );
966+
add_filter( 'cloudinary_get_signature', array( $this, 'get_signature_syncable_type' ), 10, 2 );
919967
}
920968
}
921969

php/sync/class-push-sync.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,7 @@ public function process_assets( $attachments = array() ) {
202202
continue;
203203
}
204204
// Skip unsyncable delivery types.
205-
if (
206-
! in_array(
207-
$this->media->get_media_delivery( $attachment_id ),
208-
$this->media->get_syncable_delivery_types(),
209-
true
210-
)
211-
) {
205+
if ( ! $this->sync->is_syncable( $attachment_id ) ) {
212206
continue;
213207
}
214208
// Flag attachment as being processed.

php/sync/class-upload-sync.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -118,13 +118,7 @@ public function add_inline_action( $actions, $post ) {
118118
if ( ! $this->media->is_local_media( $post->ID ) ) {
119119
return $actions;
120120
}
121-
if (
122-
! in_array(
123-
$this->media->get_media_delivery( $post->ID ),
124-
$this->media->get_syncable_delivery_types(),
125-
true
126-
)
127-
) {
121+
if ( ! $this->sync->is_syncable( $post->ID ) ) {
128122
return $actions;
129123
}
130124
if ( ! $this->plugin->components['sync']->is_synced( $post->ID ) ) {
@@ -168,13 +162,7 @@ public function handle_bulk_actions( $location, $action, $post_ids ) {
168162
$this->sync->delete_cloudinary_meta( $post_id );
169163
continue;
170164
}
171-
if (
172-
! in_array(
173-
$this->media->get_media_delivery( $post_id ),
174-
$this->media->get_syncable_delivery_types(),
175-
true
176-
)
177-
) {
165+
if ( ! $this->sync->is_syncable( $post_id ) ) {
178166
continue;
179167
}
180168
$this->sync->set_signature_item( $post_id, 'file', '' );

php/traits/trait-cli.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -201,11 +201,7 @@ protected function process_sync( $posts, $total ) {
201201
if (
202202
! $this->plugin->get_component( 'sync' )->is_synced( $asset )
203203
&& $this->plugin->get_component( 'media' )->is_local_media( $asset )
204-
&& in_array(
205-
$this->plugin->get_component( 'media' )->get_media_delivery( $asset ),
206-
$this->plugin->get_component( 'media' )->get_syncable_delivery_types(),
207-
true
208-
)
204+
&& $this->plugin->get_component( 'sync' )->is_syncable( $asset )
209205
) {
210206
$this->plugin->get_component( 'sync' )->managers['push']->process_assets( $asset, $bar );
211207
}
@@ -248,13 +244,8 @@ protected function process_analyze( $posts, $total ) {
248244
$done ++;
249245
$key = '_cld_unsupported';
250246
if (
251-
$this->plugin->get_component( 'media' )->is_media( $asset )
252-
&& $this->plugin->get_component( 'media' )->is_local_media( $asset )
253-
&& in_array(
254-
$this->plugin->get_component( 'media' )->get_media_delivery( $asset ),
255-
$this->plugin->get_component( 'media' )->get_syncable_delivery_types(),
256-
true
257-
)
247+
$this->plugin->get_component( 'media' )->is_local_media( $asset )
248+
&& $this->plugin->get_component( 'sync' )->is_syncable( $asset )
258249
) {
259250
// Add a key.
260251
$key = '_cld_synced';

0 commit comments

Comments
 (0)