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
84 changes: 56 additions & 28 deletions php/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,40 @@ public function get_transformation( $transformations, $type ) {
return false;
}

/**
* Get transformations for an attachment to use in a final URL.
*
* @param int $attachment_id The attachment ID.
* @param array $transformations Base/starter set of transformations.
* @param bool $overwrite_transformations Flag to indicate if default transformations should not be applied.
*
* @return array
*/
public function get_transformations( $attachment_id, $transformations = array(), $overwrite_transformations = false ) {
// If not provided, get transformations from the attachment meta.
if ( empty( $transformations ) ) {
$transformations = $this->get_transformation_from_meta( $attachment_id );
}
if ( false === $overwrite_transformations ) {
$overwrite_transformations = $this->maybe_overwrite_featured_image( $attachment_id );
}

// Defaults are only to be added on front, main images ( not breakpoints, since these are adapted down), and videos.
if ( false === $overwrite_transformations && ! is_admin() ) {
$transformations = $this->apply_default_transformations( $transformations, $attachment_id );
}

/**
* Filter the Cloudinary transformations.
*
* @param array $transformations Array of transformation options.
* @param int $attachment_id The id of the asset.
*
* @return array
*/
return apply_filters( 'cloudinary_transformations', $transformations, $attachment_id );
}

/**
* Extract the crop size part of a transformation that was done in the DAM widget.
*
Expand Down Expand Up @@ -698,13 +732,24 @@ public function attachment_url( $url, $attachment_id ) {
/**
* Apply default image transformations before building the URL.
*
* @param array $transformations The set of transformations.
* @param string $type Default transformations type.
* @param array $transformations The set of transformations.
* @param int $attachment_id The attachment ID.
*
* @return array
*/
public function apply_default_transformations( array $transformations, $type = 'image' ) {

public function apply_default_transformations( array $transformations, $attachment_id ) {
/**
* Filter to allow bypassing defaults. Return false to not apply defaults.
*
* @param bool $true True to apply defaults.
* @param int $attachment_id The current attachment ID.
*
* @return bool
*/
if ( false === apply_filters( 'cloudinary_apply_default_transformations', true, $attachment_id ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

@DavidCramer Can we add documentation here to this filter?

return $transformations;
}
$type = $this->get_media_type( $attachment_id );
// Base image level.
$new_transformations = array(
'image' => Api::generate_transformation_string( $transformations, $type ),
Expand Down Expand Up @@ -806,10 +851,10 @@ public function default_image_freeform_transformations( $default ) {
/**
* Generate a Cloudinary URL based on attachment ID and required size.
*
* @param int $attachment_id The id of the attachment.
* @param array|string $size The wp size to set for the URL.
* @param array $transformations Set of transformations to apply to this url.
* @param string $cloudinary_id Optional forced cloudinary ID.
* @param int $attachment_id The id of the attachment.
* @param array|string $size The wp size to set for the URL.
* @param array $transformations Set of transformations to apply to this url.
* @param string $cloudinary_id Optional forced cloudinary ID.
* @param bool $overwrite_transformations Flag url is a breakpoint URL to stop re-applying default transformations.
*
* @return string The converted URL.
Expand All @@ -821,9 +866,7 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
return null;
}
}
if ( empty( $transformations ) ) {
$transformations = $this->get_transformation_from_meta( $attachment_id );
}

// Get the attachment resource type.
$resource_type = $this->get_media_type( $attachment_id );
// Setup initial args for cloudinary_url.
Expand All @@ -834,24 +877,9 @@ public function cloudinary_url( $attachment_id, $size = array(), $transformation
);

$size = $this->prepare_size( $attachment_id, $size );
if ( false === $overwrite_transformations ) {
$overwrite_transformations = $this->maybe_overwrite_featured_image( $attachment_id );
}
/**
* Filter the Cloudinary transformations.
*
* @param array $transformations Array of transformation options.
* @param int $attachment_id The id of the asset.
*
* @return array
*/
$pre_args['transformation'] = apply_filters( 'cloudinary_transformations', $transformations, $attachment_id );
$apply_default_transformations = apply_filters( 'cloudinary_apply_default_transformations', true );

// Defaults are only to be added on front, main images ( not breakpoints, since these are adapted down), and videos.
if ( true === $apply_default_transformations && false === $overwrite_transformations && ! is_admin() ) {
$pre_args['transformation'] = $this->apply_default_transformations( $pre_args['transformation'], $resource_type );
}
// Prepare transformations.
$pre_args['transformation'] = $this->get_transformations( $attachment_id, $transformations, $overwrite_transformations );

// Make a copy as not to destroy the options in \Cloudinary::cloudinary_url().
$args = $pre_args;
Expand Down
24 changes: 24 additions & 0 deletions php/class-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static function get_active_setting() {
*
* @param array $input The array that will be processed.
* @param string $separator Separator string.
*
* @return array
*/
public static function expand_dot_notation( array $input, $separator = '.' ) {
Expand Down Expand Up @@ -105,4 +106,27 @@ public static function is_amp( $html_string ) {
public static function is_webstory_post_type( $post_type ) {
return class_exists( Story_Post_Type::class ) && Story_Post_Type::POST_TYPE_SLUG === $post_type;
}

/**
* Get all the attributes from an HTML tag.
*
* @param string $tag HTML tag to get attributes from.
*
* @return array
*/
public static function get_tag_attributes( $tag ) {
$tag = strstr( $tag, ' ', false );
$tag = trim( $tag, '> ' );
$args = shortcode_parse_atts( $tag );
$return = array();
foreach ( $args as $key => $value ) {
if ( is_int( $key ) ) {
$return[ $value ] = 'true';
continue;
}
$return[ $key ] = $value;
}

return $return;
}
}
Loading