Skip to content

Commit bd1e2ee

Browse files
authored
Merge pull request #220 from cloudinary/add/original-image-storage
Add/original image storage
2 parents dd4899b + 6b1cc5e commit bd1e2ee

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,19 @@ public function attachment_url( $url, $attachment_id ) {
633633
if ( false !== $previous_url ) {
634634
return substr( $url, $previous_url );
635635
}
636-
if ( ! doing_action( 'wp_insert_post_data' ) && false === $this->in_downsize ) {
636+
if (
637+
! doing_action( 'wp_insert_post_data' )
638+
&& false === $this->in_downsize
639+
/**
640+
* Filter doing upload.
641+
* If so, return the default attachment URL.
642+
*
643+
* @param bool Default false.
644+
*
645+
* @return bool
646+
*/
647+
&& ! apply_filters( 'cloudinary_doing_upload', false )
648+
) {
637649
if ( $this->cloudinary_id( $attachment_id ) ) {
638650
$url = $this->cloudinary_url( $attachment_id );
639651
}
@@ -762,7 +774,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
762774

763775
// Make a copy as not to destroy the options in \Cloudinary::cloudinary_url().
764776
$args = $pre_args;
765-
$url = $this->plugin->components['connect']->api->cloudinary_url( $cloudinary_id, $args, $size, $clean );
777+
$url = $this->plugin->components['connect']->api->cloudinary_url( $cloudinary_id, $args, $size );
766778

767779
// Check if this type is a preview only type. i.e PDF.
768780
if ( ! empty( $size ) && $this->is_preview_only( $attachment_id ) ) {
@@ -1051,6 +1063,8 @@ public function image_srcset( $sources, $size_array, $image_src, $image_meta, $a
10511063
$transformations = $this->get_post_meta( $attachment_id, Sync::META_KEYS['transformation'], true );
10521064
// Use Cloudinary breakpoints for same ratio.
10531065

1066+
$image_meta['overwrite_transformations'] = ! empty( $image_meta['overwrite_transformations'] ) ? $image_meta['overwrite_transformations'] : false;
1067+
10541068
if ( 'on' === $this->plugin->config['settings']['global_transformations']['enable_breakpoints'] && wp_image_matches_ratio( $image_meta['width'], $image_meta['height'], $size_array[0], $size_array[1] ) ) {
10551069
$meta = $this->get_post_meta( $attachment_id, Sync::META_KEYS['breakpoints'], true );
10561070
if ( ! empty( $meta ) ) {
@@ -1676,12 +1690,22 @@ public function get_context_options( $attachment_id ) {
16761690
*/
16771691
public function is_folder_synced( $attachment_id ) {
16781692

1679-
$return = true; // By default all assets in WordPress will be synced.
1693+
$is_folder_synced = true; // By default all assets in WordPress will be synced.
16801694
if ( $this->sync->been_synced( $attachment_id ) ) {
1681-
$return = ! empty( $this->get_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true ) );
1695+
$is_folder_synced = ! empty( $this->get_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true ) );
16821696
}
16831697

1684-
return $return;
1698+
/**
1699+
* Filter is folder synced flag.
1700+
*
1701+
* @param bool $is_folder_synced Flag value for is folder sync.
1702+
* @param int $attachment_id The attachment ID.
1703+
*
1704+
* @return bool
1705+
*/
1706+
$is_folder_synced = apply_filters( 'cloudinary_is_folder_synced', $is_folder_synced, $attachment_id );
1707+
1708+
return (bool) $is_folder_synced;
16851709
}
16861710

16871711
/**

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ public function upload( $attachment_id, $args, $headers = array(), $try_remote =
412412
$disable_https_fetch = get_transient( '_cld_disable_http_upload' );
413413
$file_url = wp_get_original_image_url( $attachment_id );
414414
$media = get_plugin_instance()->get_component( 'media' );
415+
$tempfile = false;
415416
if ( $media && $media->is_cloudinary_url( $file_url ) ) {
416417
// If this is a Cloudinary URL, then we can use it to fetch from that location.
417418
$disable_https_fetch = false;
@@ -431,7 +432,6 @@ public function upload( $attachment_id, $args, $headers = array(), $try_remote =
431432
return $this->upload_large( $attachment_id, $args );
432433
}
433434
}
434-
$tempfile = false;
435435
if ( false !== strpos( $args['file'], 'vip://' ) ) {
436436
$args['file'] = $this->create_local_copy( $args['file'] );
437437
if ( is_wp_error( $args['file'] ) ) {

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ public function setup() {
201201
*/
202202
public function upload_asset( $attachment_id ) {
203203

204+
add_filter( 'cloudinary_doing_upload', '__return_true' );
205+
206+
add_filter(
207+
'cloudinary_is_folder_synced',
208+
function( $is_synced, $post_id ) use ( $attachment_id ) {
209+
if ( $post_id === $attachment_id ) {
210+
return true;
211+
}
212+
213+
return $is_synced;
214+
},
215+
10,
216+
2
217+
);
218+
204219
$type = $this->sync->get_sync_type( $attachment_id );
205220
$options = $this->media->get_upload_options( $attachment_id );
206221
$public_id = $options['public_id'];
@@ -218,6 +233,8 @@ public function upload_asset( $attachment_id ) {
218233
// Run the upload Call.
219234
$result = $this->connect->api->upload( $attachment_id, $options, array(), $try_remote );
220235

236+
remove_filter( 'cloudinary_doing_upload', '__return_true' );
237+
221238
if ( ! is_wp_error( $result ) ) {
222239

223240
// Check that this wasn't an existing.
@@ -234,7 +251,6 @@ public function upload_asset( $attachment_id ) {
234251
}
235252
}
236253

237-
238254
// Set folder Synced.
239255
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], $this->media->is_folder_synced( $attachment_id ) );
240256
// Set public_id.

0 commit comments

Comments
 (0)