Skip to content

Conversation

@DavidCramer
Copy link
Contributor

@DavidCramer DavidCramer commented Jun 23, 2020

Allows for being able to alter the public_id for uploading as well as correcting the folder changing sync.

Testing notes.

This allows for changing the public ID of assets.
Below is an example of using the filter cloudinary_upload_options to use the assets slug as it's public_id
Add the snippet in the functions.php in your theme.
go to the media library page. If all assets slug is the same as the ID (it should be), then the cloud should be green.

Edit an asset, and change the slug (you may need to select Screen Options, and enable it).
Update and go back to the media library. The image should now be in the syncing state (orange cloud).
After a moment, it should be green and the public ID should be the same as the slug.

	add_filter( 'cloudinary_upload_options', function ( $options, $attachment_post ) {

		/**
		 * Structure of options as follows:
		 *
		 * $options = array(
		 *    'unique_filename' => false, // Allow for auto filenames. See Cloudinary docs.
		 *    'resource_type'   => $resource_type, // Type can be 'image' or 'video' or custom.
		 *    'public_id'       => $public_id, // Changing this will change the public ID as stored in Cloudinary.
		 *    'context'         => array(
		 *        'caption' => esc_attr( $post->post_title ), // Set the caption data in Cloudinary.
		 *        'alt'     => $post->_wp_attachment_image_alt, // set the alt text of the asset in Cloudinary
		 *    ),
		 * );
		 **/

		// To change the asset public_id.
		$post_name = strtolower( $attachment_post->post_name );
		if ( $post_name !== $options['public_id'] ) {
			$options['public_id'] = $post_name;
		}

		return $options;
	}, 10, 2 );

@DavidCramer DavidCramer requested a review from pereirinha June 23, 2020 09:53
Copy link
Contributor

@dugajean dugajean left a comment

Choose a reason for hiding this comment

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

Looks like a good refactor overall, with solid changes, but there's some unclarity that should be addressed before this PR moves forward.

Comment on lines 401 to 410
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;
}
Copy link
Contributor

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
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.

@dugajean
Copy link
Contributor

LGTM

Copy link
Contributor

@pereirinha pereirinha left a comment

Choose a reason for hiding this comment

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

Thanks, @DavidCramer for the hard work on this one.

@DavidCramer DavidCramer merged commit ec573e6 into develop Jun 30, 2020
@DavidCramer DavidCramer deleted the feature/public-id-sync branch June 30, 2020 19:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants