Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -46,7 +46,10 @@ public function __construct( \Cloudinary\Media $media ) {
public function check_cloudinary_version( $cloudinary_id, $attachment_id ) {
if ( false === $cloudinary_id ) {
// Backwards compat.
$meta = wp_get_attachment_metadata( $attachment_id );
$meta = wp_get_attachment_metadata( $attachment_id );
if ( ! empty( $meta[ Sync::META_KEYS['cloudinary'] ] ) ) {
return $cloudinary_id; // Current version.
}
$public_id = $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@ public function __construct( \Cloudinary\Plugin $plugin ) {
// Define the sync types and their option keys.
$sync_types = array(
'file' => 'upload',
'public_id' => 'rename',
'breakpoints' => 'explicit',
'options' => 'context',
'folder' => 'upload',
'folder' => 'rename',
'cloud_name' => 'upload',
);
$this->sync_types = apply_filters( 'cloudinary_sync_types', $sync_types );
Expand Down Expand Up @@ -303,7 +304,7 @@ private function get_sync_type( $attachment ) {

$type = 'upload';
// Check for explicit (has public_id, but no breakpoints).
$attachment_signature = $attachment->{Sync::META_KEYS['signature']};
$attachment_signature = $this->plugin->components['media']->get_post_meta( $attachment->ID, Sync::META_KEYS['signature'] );
if ( empty( $attachment_signature ) ) {
if ( ! empty( $attachment->{Sync::META_KEYS['public_id']} ) ) {
// Has a public id but no signature, explicit update to complete download.
Expand Down Expand Up @@ -389,25 +390,17 @@ public function prepare_upload( $post, $down_sync = false ) {
}

// If it's got a public ID, then this is an explicit update.
$settings = $this->plugin->config['settings'];
$public_id = $post->{Sync::META_KEYS['public_id']}; // use the __get method on the \WP_Post to get post_meta.
$dirs = wp_get_upload_dir();
$cld_folder = false;
$folder = trailingslashit( $dirs['cloudinary_folder'] );
if ( '/' === $dirs['cloudinary_folder'] ) {
$folder = '';
}
$cld_folder = trailingslashit( $settings['sync_media']['cloudinary_folder'] );
if ( empty( $public_id ) ) {
$file_info = pathinfo( $file );
$public_id = $folder . $file_info['filename'];
}

// Check if cloudinary folder is in public_id.
$parts = explode( '/', $public_id );
if ( untrailingslashit( $dirs['cloudinary_folder'] ) === $parts[0] ) {
$cld_folder = $dirs['cloudinary_folder'];
$public_id = $file_info['filename'];
} else {
$public_id_parts = pathinfo( $public_id );
$public_id = $public_id_parts['basename'];
}
Comment on lines +401 to 422
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe we can switch to this in order to satisfy linters:

Suggested change
if ( false !== strpos( $public_id, '/' ) ) {
// Split the public_id into path and filename to allow filtering just the ID and not giving access to the path.
$public_id_info = pathinfo( $public_id );
$public_id_folder = trailingslashit( $public_id_info['dirname'] );
$public_id_file = $public_id_info['filename'];
} else {
// File is in the root of cloudinary.
$public_id_folder = '';
$public_id_file = $public_id;
}
// File is in the root of cloudinary.
$public_id_folder = '';
$public_id_file = $public_id;
if ( false !== strpos( $public_id, '/' ) ) {
// Split the public_id into path and filename to allow filtering just the ID and not giving access to the path.
$public_id_info = pathinfo( $public_id );
$public_id_folder = trailingslashit( $public_id_info['dirname'] );
$public_id_file = $public_id_info['filename'];
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@dugajean Nice idea. I usually do prefer to have a default, and if it's not what's expected then change.



// Prepare upload options.
$options = array(
'unique_filename' => false,
Expand Down Expand Up @@ -436,13 +429,13 @@ public function prepare_upload( $post, $down_sync = false ) {
}
$max_width = $this->plugin->components['media']->get_max_width();
// Add breakpoints request options.
if ( ! empty( $this->plugin->config['settings']['global_transformations']['enable_breakpoints'] ) ) {
if ( ! empty( $settings['global_transformations']['enable_breakpoints'] ) ) {
$options['responsive_breakpoints'] = array(
'create_derived' => true,
'bytes_step' => $this->plugin->config['settings']['global_transformations']['bytes_step'],
'max_images' => $this->plugin->config['settings']['global_transformations']['breakpoints'],
'bytes_step' => $settings['global_transformations']['bytes_step'],
'max_images' => $settings['global_transformations']['breakpoints'],
'max_width' => $meta['width'] < $max_width ? $meta['width'] : $max_width,
'min_width' => $this->plugin->config['settings']['global_transformations']['min_width'],
'min_width' => $settings['global_transformations']['min_width'],
);
$transformations = $this->plugin->components['media']->get_transformation_from_meta( $post->ID );
if ( ! empty( $transformations ) ) {
Expand Down Expand Up @@ -474,14 +467,19 @@ public function prepare_upload( $post, $down_sync = false ) {
$breakpoints['context'] = http_build_query( $breakpoints['context'], null, '|' );
}

// Stage folder to public_id.
$public_id = $cld_folder . $options['public_id'];
$return = array(
'file' => $file,
'folder' => $cld_folder,
'public_id' => $public_id,
'breakpoints' => array(),
'options' => $options,
);
$return['options']['public_id'] = $public_id;
if ( ! empty( $breakpoints ) ) {
$return['breakpoints'] = $breakpoints;
$return['breakpoints']['public_id'] = $public_id; // Stage public ID to folder for breakpoints.
}
$this->upload_options[ $post->ID ] = $return;

Expand Down Expand Up @@ -560,7 +558,7 @@ public function push_attachments( $attachments ) {
if ( 'explicit' === $sync_type ) {
// Explicit update.
$args = array(
'public_id' => $upload['options']['public_id'],
'public_id' => $upload['public_id'],
'type' => 'upload',
);
if ( ! empty( $upload['options']['responsive_breakpoints'] ) ) {
Expand All @@ -570,6 +568,13 @@ public function push_attachments( $attachments ) {
$args['context'] = $upload['options']['context'];
}
$result = $this->plugin->components['connect']->api->explicit( $args );
} elseif ( 'rename' === $sync_type ) {
// Rename an asset.
$args = array(
'from_public_id' => $this->plugin->components['media']->get_post_meta( $attachment->ID, Sync::META_KEYS['public_id'] ),
'to_public_id' => $upload['public_id'],
);
$result = $this->plugin->components['connect']->api->{$upload['options']['resource_type']}( 'rename', 'POST', $args );
} else {
// dynamic sync type..
$result = $this->plugin->components['connect']->api->{$sync_type}( $upload['file'], $upload['options'] );
Expand Down