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 @@ -495,6 +495,7 @@ public function get_crop( $url, $attachment_id ) {
// Make the WP Size array.
$wp_size = array(
'wpsize' => $size_name,
'file' => $size['file'],
'width' => $size['width'],
'height' => $size['height'],
'crop' => $cropped ? 'fill' : 'scale',
Expand Down Expand Up @@ -604,9 +605,6 @@ public function get_transformations_from_string( $str, $type = 'image' ) {
$transformation_chains = explode( '/', $str );
$transformations = array();
foreach ( $transformation_chains as $index => $chain ) {
if ( false !== strpos( $chain, 'wpsize' ) ) {
continue; // A wpsize is not a transformation.
}
$items = explode( ',', $chain );
foreach ( $items as $item ) {
$item = trim( $item );
Expand Down Expand Up @@ -724,7 +722,7 @@ public function apply_default_transformations( array $transformations, $type = '
*
* @return string The converted URL.
*/
public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $overwrite_transformations = false, $clean = false ) {
public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $overwrite_transformations = false ) {

if ( ! ( $cloudinary_id ) ) {
$cloudinary_id = $this->cloudinary_id( $attachment_id );
Expand All @@ -744,13 +742,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
'resource_type' => $resource_type,
);

// Check size and correct if string or size.
if ( is_string( $size ) || ( is_array( $size ) && 3 === count( $size ) ) ) {
$intermediate = image_get_intermediate_size( $attachment_id, $size );
if ( is_array( $intermediate ) ) {
$size = $this->get_crop( $intermediate['url'], $attachment_id );
}
}
$size = $this->prepare_size( $attachment_id, $size );
if ( false === $overwrite_transformations ) {
$overwrite_transformations = $this->maybe_overwrite_featured_image( $attachment_id );
}
Expand Down Expand Up @@ -789,6 +781,44 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
return apply_filters( 'cloudinary_converted_url', $url, $attachment_id, $pre_args );
}

/**
* Prepare the Size array for the Cloudinary URL API.
*
* @param int $attachment_id The attachment ID.
* @param array|string $size The size array or slug.
*
* @return array|string
*/
public function prepare_size( $attachment_id, $size ) {
// Check size and correct if string or size.
if ( empty( $size ) || 'full' === $size ) {
// Maybe get full size if scaled.
$meta = wp_get_attachment_metadata( $attachment_id, true );
if ( ! empty( $meta['original_image'] ) ) {
$size = array(
'width' => $meta['width'],
'height' => $meta['height'],
'full' => true,
);
}
} elseif ( is_string( $size ) || ( is_array( $size ) && 3 === count( $size ) ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@DavidCramer

Nothing major, but there's an extra parenthesis on the && condition

$intermediate = image_get_intermediate_size( $attachment_id, $size );
if ( is_array( $intermediate ) ) {
$size = $this->get_crop( $intermediate['url'], $attachment_id );
}
} elseif ( array_keys( $size ) === array( 0, 1 ) ) {
$size = array(
'width' => $size[0],
'height' => $size[1],
);
if ( $size['width'] === $size['height'] ) {
$size['crop'] = 'fill';
}
}

return $size;
}

/**
* Add domain to subdir.
*
Expand Down Expand Up @@ -998,7 +1028,7 @@ public function convert_url( $url, $attachment_id, $transformations = array(), $
}
$size = $this->get_crop( $url, $attachment_id );

return $this->cloudinary_url( $attachment_id, $size, $transformations, null, $overwrite_transformations, true );
return $this->cloudinary_url( $attachment_id, $size, $transformations, null, $overwrite_transformations );
}

/**
Expand All @@ -1017,8 +1047,8 @@ public function image_srcset( $sources, $size_array, $image_src, $image_meta, $a
if ( ! $cloudinary_id ) {
return $sources; // Return WordPress default sources.
}
// Get transformations from URL.
$transformations = $this->get_transformations_from_string( $image_src );
// Get transformations if any.
$transformations = $this->get_post_meta( $attachment_id, Sync::META_KEYS['transformation'], true );
// Use Cloudinary breakpoints for same ratio.

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] ) ) {
Expand Down Expand Up @@ -1050,7 +1080,7 @@ function ( $item ) use ( $crop ) {
'width' => $breakpoint['width'],
);
$sources[ $breakpoint['width'] ] = array(
'url' => $this->cloudinary_url( $attachment_id, $size, $transformations, $cloudinary_id, true ),
'url' => $this->cloudinary_url( $attachment_id, $size, $transformations, $cloudinary_id, $image_meta['overwrite_transformations'] ),
'descriptor' => 'w',
'value' => $breakpoint['width'],
);
Expand All @@ -1076,35 +1106,13 @@ function ( $item ) use ( $crop ) {
// Use current sources, but convert the URLS.
foreach ( $sources as &$source ) {
if ( ! $this->is_cloudinary_url( $source['url'] ) ) {
$source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, true ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
$source['url'] = $this->convert_url( $source['url'], $attachment_id, $transformations, $image_meta['overwrite_transformations'] ); // Overwrite transformations applied, since the $transformations includes globals from the primary URL.
}
}

return $sources;
}

/**
* Alter the image sizes metadata to match the Cloudinary ID so that WordPress can detect a matched source for responsive breakpoints.
*
* @param array $image_meta The image metadata array.
* @param array $size_array The size array.
* @param string $image_src The image src.
* @param int $attachment_id The attachment ID.
*
* @return array
*/
public function match_responsive_sources( $image_meta, $size_array, $image_src, $attachment_id ) {
if ( wp_attachment_is_image( $attachment_id ) && ! empty( $image_meta['sizes'] ) ) {
$cloudinary_id = $this->cloudinary_id( $attachment_id );
if ( $cloudinary_id ) {
// Set the file to the Cloudinary ID so that it will be matched.
$image_meta['file'] = $cloudinary_id;
}
}

return $image_meta;
}

/**
* Check if a url is a cloudinary url or not.
*
Expand Down Expand Up @@ -1789,8 +1797,6 @@ public function setup() {

// Filter live URLS. (functions that return a URL).
add_filter( 'wp_calculate_image_srcset', array( $this, 'image_srcset' ), 10, 5 );
add_filter( 'wp_calculate_image_srcset_meta', array( $this, 'match_responsive_sources' ), 10, 4 );
add_filter( 'wp_get_attachment_metadata', array( $this, 'match_file_name_with_cloudinary_source' ), 10, 2 );
add_filter( 'wp_get_attachment_url', array( $this, 'attachment_url' ), 10, 2 );
add_filter( 'image_downsize', array( $this, 'filter_downsize' ), 10, 3 );

Expand All @@ -1802,23 +1808,4 @@ public function setup() {
add_action( 'begin_fetch_post_thumbnail_html', array( $this, 'set_doing_featured' ), 10, 2 );
}
}

/**
* Ensure the file in image meta is the same as the Cloudinary ID.
*
* @param array $image_meta Meta information of the attachment.
* @param int $attachment_id The attachment ID.
*
* @return array
*/
public function match_file_name_with_cloudinary_source( $image_meta, $attachment_id ) {
if ( $this->has_public_id( $attachment_id ) ) {
$cld_file = 'v' . $this->get_cloudinary_version( $attachment_id ) . '/' . $this->get_cloudinary_id( $attachment_id );
if ( false === strpos( $image_meta['file'], $cld_file ) ) {
$image_meta['file'] = $cld_file;
}
}

return $image_meta;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public function get_signature( $attachment_id, $cached = true ) {
public function generate_public_id( $attachment_id ) {

$cld_folder = $this->managers['media']->get_cloudinary_folder();
$file = get_attached_file( $attachment_id );
$file = wp_get_original_image_path( $attachment_id );
$file_info = pathinfo( $file );
$public_id = $cld_folder . $file_info['filename'];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ class Api {
'pg' => 'page',
'sp' => 'streaming_profile',
'vs' => 'video_sampling',
'$wpsize' => 'wpsize',
),
'video' => array(
'w' => 'width',
Expand Down Expand Up @@ -141,7 +140,7 @@ class Api {
* @param string The plugin version.
*/
public function __construct( $connect, $version ) {
$this->credentials = $connect->get_credentials();
$this->credentials = $connect->get_credentials();
$this->plugin_version = $version;
// Use CNAME.
if ( ! empty( $this->credentials['cname'] ) ) {
Expand Down Expand Up @@ -172,8 +171,12 @@ public function url( $resource, $function = null, $endpoint = false ) {
$parts[] = $this->credentials['cloud_name'];
}

$parts[] = $resource;
$parts[] = $function;
if ( false === $endpoint && 'image' === $resource ) {
$parts[] = 'images';
} else {
$parts[] = $resource;
$parts[] = $function;
}

$parts = array_filter( $parts );
$url = implode( '/', $parts );
Expand Down Expand Up @@ -203,14 +206,6 @@ function ( $item ) use ( $transformation_index ) {

foreach ( $item as $type => $value ) { // phpcs:ignore
$key = array_search( $type, $transformation_index, true );
if ( false !== strpos( $type, 'wpsize' ) ) {
if ( ! empty( $item['clean'] ) ) {
continue;
}

$value = '!' . $value . '!';
}

if ( false !== $key ) {
$transform[] = $key . '_' . $value;
}
Expand All @@ -231,13 +226,12 @@ function ( $item ) use ( $transformation_index ) {
* Generate a Cloudinary URL.
*
* @param string|null $public_id The Public ID to get a url for.
* @param array $args Additional args.
* @param array $size The WP Size array.
* @param bool $clean Flag to produce a non variable size url.
* @param array $args Additional args.
* @param array $size The WP Size array.
*
* @return string
*/
public function cloudinary_url( $public_id = null, $args = array(), $size = array(), $clean = false ) {
public function cloudinary_url( $public_id = null, $args = array(), $size = array() ) {

if ( null === $public_id ) {
return 'https://' . $this->url( null, null );
Expand Down Expand Up @@ -267,22 +261,18 @@ public function cloudinary_url( $public_id = null, $args = array(), $size = arra
if ( ! empty( $args['transformation'] ) ) {
$url_parts[] = self::generate_transformation_string( $args['transformation'] );
}

$base = pathinfo( $public_id );
if ( 'image' === $args['resource_type'] ) {
$new_path = $base['filename'] . '/' . $base['basename'];
$public_id = str_replace( $base['basename'], $new_path, $public_id );
}
// Add size.
if ( ! empty( $size ) && is_array( $size ) ) {
if ( true === $clean ) {
$size['clean'] = true;
}
if ( array_keys( $size ) == array( 0, 1 ) ) {
$size = array(
'width' => $size[0],
'height' => $size[1],
);
if ( $size['width'] === $size['height'] ) {
$size['crop'] = 'fill';
}
}
$url_parts[] = self::generate_transformation_string( array( $size ) );
// add size to ID if scaled.
if ( ! empty( $size['file'] ) ) {
$public_id = str_replace( $base['basename'], $size['file'], $public_id );
}
}

$url_parts[] = $args['version'];
Expand Down Expand Up @@ -418,7 +408,7 @@ public function upload( $attachment_id, $args, $headers = array() ) {
$url = $this->url( $resource, 'upload', true );
$args = $this->clean_args( $args );
$disable_https_fetch = get_transient( '_cld_disable_http_upload' );
$file_url = wp_get_attachment_url( $attachment_id );
$file_url = wp_get_original_image_url( $attachment_id );
$media = get_plugin_instance()->get_component( 'media' );
if ( $media && $media->is_cloudinary_url( $file_url ) ) {
// If this is a Cloudinary URL, then we can use it to fetch from that location.
Expand All @@ -430,7 +420,7 @@ public function upload( $attachment_id, $args, $headers = array() ) {
} else {
// We should have the file in args at this point, but if the transient was set, it will be defaulting here.
if ( empty( $args['file'] ) ) {
$args['file'] = get_attached_file( $attachment_id );
$args['file'] = wp_get_original_image_path( $attachment_id );
}
// Headers indicate chunked upload.
if ( empty( $headers ) ) {
Expand Down Expand Up @@ -466,7 +456,7 @@ public function upload( $attachment_id, $args, $headers = array() ) {
// Hook in flag to allow for non accessible URLS.
if ( is_wp_error( $result ) ) {
$error = $result->get_error_message();
$code = $result->get_error_code();
$code = $result->get_error_code();
/**
* If there's an error and the file is a URL in the error message,
* it's likely due to CURL or the location does not support URL file attachments.
Expand Down
Loading