Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) {
Expand All @@ -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'] );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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 );
Expand All @@ -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' );
Expand Down