diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php index d5ab87c82..0e9f17673 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-download-sync.php @@ -185,12 +185,13 @@ function ( $val ) use ( $media ) { /** * Download an attachment source to the file system. * - * @param int $attachment_id The attachment ID. - * @param string $source The optional source to download. + * @param int $attachment_id The attachment ID. + * @param string $source The optional source to download. + * @param string|null $date The date of the attachment to set storage folders. * * @return array|\WP_Error */ - public function download_asset( $attachment_id, $source = null ) { + public function download_asset( $attachment_id, $source = null, $date = null ) { require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; if ( empty( $source ) ) { @@ -200,7 +201,7 @@ public function download_asset( $attachment_id, $source = null ) { $file_name = basename( $source ); try { // Prime a file to stream to. - $upload = wp_upload_bits( $file_name, null, 'temp' ); + $upload = wp_upload_bits( $file_name, null, 'temp', $date ); if ( ! empty( $upload['error'] ) ) { return new \WP_Error( 'download_error', $upload['error'] ); } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php index f2bb63527..9cfa6cd96 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-storage.php @@ -157,7 +157,15 @@ public function validate_file_folder_sync( $attachment_id ) { * @return string */ public function generate_signature( $attachment_id ) { - return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ); + $file_exists = true; + if ( $this->settings['offload'] !== 'cld' ) { + $attachment_file = get_attached_file( $attachment_id ); + if ( ! file_exists( $attachment_file ) ) { + $file_exists = $attachment_file; + } + } + + return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) . $file_exists; } /** @@ -184,7 +192,8 @@ public function sync( $attachment_id ) { $url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, true ); break; case 'dual_full': - if ( ! empty( $previous_state ) && 'dual_full' !== $previous_state ) { + $exists = get_attached_file( $attachment_id ); + if ( ! empty( $previous_state ) && ! file_exists( $exists ) ) { // Only do this is it's changing a state. $transformations = $this->media->get_transformation_from_meta( $attachment_id ); $url = $this->media->cloudinary_url( $attachment_id, '', $transformations, null, false, false ); @@ -198,7 +207,8 @@ public function sync( $attachment_id ) { if ( 'cld' !== $previous_state ) { $this->remove_local_assets( $attachment_id ); } - $this->download->download_asset( $attachment_id, $url ); + $date = get_post_datetime( $attachment_id ); + $this->download->download_asset( $attachment_id, $url, $date->format( 'Y/m' ) ); } $this->sync->set_signature_item( $attachment_id, 'storage' );