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 @@ -633,7 +633,19 @@ public function attachment_url( $url, $attachment_id ) {
if ( false !== $previous_url ) {
return substr( $url, $previous_url );
}
if ( ! doing_action( 'wp_insert_post_data' ) && false === $this->in_downsize ) {
if (
! doing_action( 'wp_insert_post_data' )
&& false === $this->in_downsize
/**
* Filter doing upload.
* If so, return the default attachment URL.
*
* @param bool Default false.
*
* @return bool
*/
&& ! apply_filters( 'cloudinary_doing_upload', false )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One small correction here:

Suggested change
&& ! apply_filters( 'cloudinary_doing_upload', false )
&& apply_filters( 'cloudinary_doing_upload', false )

Otherwise we default to true. Thanks for addressing this 😁

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dugajean

The default should be true. We should almost always resolve to serve the Cloudinary. However, in the case of uploading, we should bail any change.

) {
if ( $this->cloudinary_id( $attachment_id ) ) {
$url = $this->cloudinary_url( $attachment_id );
}
Expand Down Expand Up @@ -762,7 +774,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation

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

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

$image_meta['overwrite_transformations'] = ! empty( $image_meta['overwrite_transformations'] ) ? $image_meta['overwrite_transformations'] : false;

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] ) ) {
$meta = $this->get_post_meta( $attachment_id, Sync::META_KEYS['breakpoints'], true );
if ( ! empty( $meta ) ) {
Expand Down Expand Up @@ -1676,12 +1690,22 @@ public function get_context_options( $attachment_id ) {
*/
public function is_folder_synced( $attachment_id ) {

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

return $return;
/**
* Filter is folder synced flag.
*
* @param bool $is_folder_synced Flag value for is folder sync.
* @param int $attachment_id The attachment ID.
*
* @return bool
*/
$is_folder_synced = apply_filters( 'cloudinary_is_folder_synced', $is_folder_synced, $attachment_id );

return (bool) $is_folder_synced;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ public function upload( $attachment_id, $args, $headers = array() ) {
$disable_https_fetch = get_transient( '_cld_disable_http_upload' );
$file_url = wp_get_original_image_url( $attachment_id );
$media = get_plugin_instance()->get_component( 'media' );
$tempfile = false;
if ( $media && $media->is_cloudinary_url( $file_url ) ) {
// If this is a Cloudinary URL, then we can use it to fetch from that location.
$disable_https_fetch = false;
Expand All @@ -429,7 +430,6 @@ public function upload( $attachment_id, $args, $headers = array() ) {
return $this->upload_large( $attachment_id, $args );
}
}
$tempfile = false;
if ( false !== strpos( $args['file'], 'vip://' ) ) {
$args['file'] = $this->create_local_copy( $args['file'] );
if ( is_wp_error( $args['file'] ) ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ public function setup() {
*/
public function upload_asset( $attachment_id ) {

add_filter( 'cloudinary_doing_upload', '__return_true' );

add_filter(
'cloudinary_is_folder_synced',
function( $is_synced, $post_id ) use ( $attachment_id ) {
if ( $post_id === $attachment_id ) {
return true;
}

return $is_synced;
},
10,
2
);

$type = $this->sync->get_sync_type( $attachment_id );
$options = $this->media->get_upload_options( $attachment_id );
$public_id = $options['public_id'];
Expand All @@ -217,6 +232,8 @@ public function upload_asset( $attachment_id ) {
// Run the upload Call.
$result = $this->connect->api->upload( $attachment_id, $options );

remove_filter( 'cloudinary_doing_upload', '__return_true' );

if ( ! is_wp_error( $result ) ) {

// Check that this wasn't an existing.
Expand All @@ -233,7 +250,6 @@ public function upload_asset( $attachment_id ) {
}
}


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