diff --git a/php/class-media.php b/php/class-media.php index 934342faa..22b6e2302 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -1140,6 +1140,7 @@ public function get_cloudinary_id( $attachment_id ) { $extension = $image_format; } } + $cloudinary_id = $public_id; if ( 'fetch' !== $this->get_media_delivery( $attachment_id ) ) { $cloudinary_id = $public_id . '.' . $extension; } @@ -1687,14 +1688,7 @@ public function media_column( $cols ) { */ public function media_column_value( $column_name, $attachment_id ) { if ( 'cld_status' === $column_name ) { - if ( - $this->is_media( $attachment_id ) && - in_array( - $this->get_media_delivery( $attachment_id ), - $this->get_syncable_delivery_types(), - true - ) - ) : + if ( $this->sync->is_syncable( $attachment_id ) ) : $status = array( 'state' => 'inactive', 'note' => esc_html__( 'Not Synced', 'cloudinary' ), diff --git a/php/class-sync.php b/php/class-sync.php index 24120df6b..6913f7616 100644 --- a/php/class-sync.php +++ b/php/class-sync.php @@ -15,6 +15,7 @@ use Cloudinary\Sync\Push_Sync; use Cloudinary\Sync\Sync_Queue; use Cloudinary\Sync\Upload_Sync; +use WP_Error; /** * Class Sync @@ -244,13 +245,7 @@ public function can_sync( $attachment_id, $type = 'file' ) { } // Can sync only syncable delivery types. - if ( - ! in_array( - $this->managers['media']->get_media_delivery( $attachment_id ), - $this->managers['media']->get_syncable_delivery_types(), - true - ) - ) { + if ( ! $this->is_syncable( $attachment_id ) ) { $can = false; } @@ -304,6 +299,18 @@ public function get_signature( $attachment_id, $cached = true ) { $return = wp_parse_args( $signature, $this->sync_types ); } + /** + * Filter the get signature of the asset. + * + * @hook cloudinary_get_signature + * + * @param $signature {array} The attachment signature. + * @param $attachment_id {int} The attachment ID. + * + * @return {array} + */ + $return = apply_filters( 'cloudinary_get_signature', $signatures[ $attachment_id ], $attachment_id ); + return $return; } @@ -328,6 +335,29 @@ public function generate_public_id( $attachment_id ) { return ltrim( $public_id, '/' ); } + /** + * Is syncable asset. + * + * @param int $attachment_id The attachment ID. + * + * @return bool + */ + public function is_syncable( $attachment_id ) { + $syncable = false; + if ( + $this->managers['media']->is_media( $attachment_id ) + && in_array( + $this->managers['media']->get_media_delivery( $attachment_id ), + $this->managers['media']->get_syncable_delivery_types(), + true + ) + ) { + $syncable = true; + } + + return $syncable; + } + /** * Register a new sync type. * @@ -856,6 +886,23 @@ public function filter_get_cloudinary_folder( $value, $slug ) { return $value; } + /** + * Filter the signature. + * + * @param array $signature The signature array. + * @param int $attachment_id The attachment ID. + * + * @return array|bool|string|WP_Error + */ + public function get_signature_syncable_type( $signature, $attachment_id ) { + + if ( ! $this->is_syncable( $attachment_id ) ) { + $signature = $this->generate_signature( $attachment_id ); + } + + return $signature; + } + /** * Checks if auto sync feature is enabled. * @@ -916,6 +963,7 @@ public function setup() { $this->managers['queue']->setup( $this ); add_filter( 'cloudinary_setting_get_value', array( $this, 'filter_get_cloudinary_folder' ), 10, 2 ); + add_filter( 'cloudinary_get_signature', array( $this, 'get_signature_syncable_type' ), 10, 2 ); } } diff --git a/php/sync/class-push-sync.php b/php/sync/class-push-sync.php index 792882af7..b35257844 100644 --- a/php/sync/class-push-sync.php +++ b/php/sync/class-push-sync.php @@ -202,13 +202,7 @@ public function process_assets( $attachments = array() ) { continue; } // Skip unsyncable delivery types. - if ( - ! in_array( - $this->media->get_media_delivery( $attachment_id ), - $this->media->get_syncable_delivery_types(), - true - ) - ) { + if ( ! $this->sync->is_syncable( $attachment_id ) ) { continue; } // Flag attachment as being processed. diff --git a/php/sync/class-upload-sync.php b/php/sync/class-upload-sync.php index 06172432d..cc613ca06 100644 --- a/php/sync/class-upload-sync.php +++ b/php/sync/class-upload-sync.php @@ -118,13 +118,7 @@ public function add_inline_action( $actions, $post ) { if ( ! $this->media->is_local_media( $post->ID ) ) { return $actions; } - if ( - ! in_array( - $this->media->get_media_delivery( $post->ID ), - $this->media->get_syncable_delivery_types(), - true - ) - ) { + if ( ! $this->sync->is_syncable( $post->ID ) ) { return $actions; } if ( ! $this->plugin->components['sync']->is_synced( $post->ID ) ) { @@ -168,13 +162,7 @@ public function handle_bulk_actions( $location, $action, $post_ids ) { $this->sync->delete_cloudinary_meta( $post_id ); continue; } - if ( - ! in_array( - $this->media->get_media_delivery( $post_id ), - $this->media->get_syncable_delivery_types(), - true - ) - ) { + if ( ! $this->sync->is_syncable( $post_id ) ) { continue; } $this->sync->set_signature_item( $post_id, 'file', '' ); diff --git a/php/traits/trait-cli.php b/php/traits/trait-cli.php index bf56efc78..9e6b071d1 100644 --- a/php/traits/trait-cli.php +++ b/php/traits/trait-cli.php @@ -201,11 +201,7 @@ protected function process_sync( $posts, $total ) { if ( ! $this->plugin->get_component( 'sync' )->is_synced( $asset ) && $this->plugin->get_component( 'media' )->is_local_media( $asset ) - && in_array( - $this->plugin->get_component( 'media' )->get_media_delivery( $asset ), - $this->plugin->get_component( 'media' )->get_syncable_delivery_types(), - true - ) + && $this->plugin->get_component( 'sync' )->is_syncable( $asset ) ) { $this->plugin->get_component( 'sync' )->managers['push']->process_assets( $asset, $bar ); } @@ -248,13 +244,8 @@ protected function process_analyze( $posts, $total ) { $done ++; $key = '_cld_unsupported'; if ( - $this->plugin->get_component( 'media' )->is_media( $asset ) - && $this->plugin->get_component( 'media' )->is_local_media( $asset ) - && in_array( - $this->plugin->get_component( 'media' )->get_media_delivery( $asset ), - $this->plugin->get_component( 'media' )->get_syncable_delivery_types(), - true - ) + $this->plugin->get_component( 'media' )->is_local_media( $asset ) + && $this->plugin->get_component( 'sync' )->is_syncable( $asset ) ) { // Add a key. $key = '_cld_synced';