Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,11 @@ public function get_public_id( $attachment_id ) {
public function get_cloudinary_id( $attachment_id ) {

// A cloudinary_id is a public_id with a file extension.
$public_id = $this->get_public_id( $attachment_id );
$public_id = $this->get_public_id( $attachment_id );
$suffix_data = $this->get_post_meta( $attachment_id, Sync::META_KEYS['suffix'], true );
if ( is_array( $suffix_data ) && ! empty( $suffix_data['suffix'] ) && $suffix_data['public_id'] === $public_id ) {
$public_id = $public_id . $suffix_data['suffix'];
}
$file = get_attached_file( $attachment_id );
$info = pathinfo( $file );
$cloudinary_id = $public_id . '.' . $info['extension'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class Sync implements Setup, Assets {
'sync_error' => '_sync_error',
'cloudinary' => '_cloudinary_v2',
'folder_sync' => '_folder_sync',
'suffix' => '_suffix',
'syncing' => '_cloudinary_syncing',
'downloading' => '_cloudinary_downloading',
);
Expand Down Expand Up @@ -172,13 +173,85 @@ public function get_signature( $post_id ) {
return $return;
}

/**
* Generate a new Public ID for an asset.
*
* @param int $attachment_id The attachment ID for the new public ID.
*
* @return string|null
*/
public function generate_public_id( $attachment_id ) {
$settings = $this->plugin->config['settings'];
$cld_folder = trailingslashit( $settings['sync_media']['cloudinary_folder'] );
$file = get_attached_file( $attachment_id );
$file_info = pathinfo( $file );
$public_id = $cld_folder . $file_info['filename'];

return $public_id;
}

/**
* Maybe add a suffix to the public ID if it's not unique.
*
* @param string $public_id The public ID to maybe add a suffix.
* @param int $attachment_id The attachment ID.
* @param string|null $suffix The suffix to maybe add.
*
* @return string The public ID.
*/
public function add_suffix_maybe( $public_id, $attachment_id, $suffix = null ) {

// Test if asset exists by calling just the head on the asset url, to prevent API rate limits.
$url = $this->plugin->components['connect']->api->cloudinary_url( $public_id . $suffix );
$req = wp_remote_head( $url, array( 'body' => array( 'rdm' => wp_rand( 100, 999 ) ) ) );
$asset_error = strtolower( wp_remote_retrieve_header( $req, 'x-cld-error' ) );
$code = wp_remote_retrieve_response_code( $req );

// If the request is not a 404 & does not have a cld-error header stating resource not found, it exists and should be checked that it's not a resync or generate a prefixed ID.
if ( 404 !== $code && false === strpos( $asset_error, 'resource not found' ) ) {

// Get the attachment type.
if ( wp_attachment_is( 'image', $attachment_id ) ) {
$type = 'image';
} elseif ( wp_attachment_is( 'video', $attachment_id ) ) {
$type = 'video';
} elseif ( wp_attachment_is( 'audio', $attachment_id ) ) {
$type = 'audio';
} else {
// not supported.
return null;
}
$cld_asset = $this->plugin->components['connect']->api->get_asset_details( $public_id, $type );
if ( ! is_wp_error( $cld_asset ) && ! empty( $cld_asset['public_id'] ) ) {
$context_id = null;

// Exists, check to see if this asset originally belongs to this ID.
if ( ! empty( $cld_asset['context'] ) && ! empty( $cld_asset['context']['custom'] ) && ! empty( $cld_asset['context']['custom']['wp_id'] ) ) {
$context_id = (int) $cld_asset['context']['custom']['wp_id'];
}

// Generate new ID only if context ID is not related.
if ( $context_id !== $attachment_id ) {
// Generate a new ID with a uniqueID prefix.
$suffix = '-' . uniqid();

// Return new potential suffixed ID.
return $this->add_suffix_maybe( $public_id, $attachment_id, $suffix );
}
}
}

return $suffix;
}

/**
* Additional component setup.
*/
public function setup() {
if ( $this->plugin->config['connect'] ) {
$this->managers['upload']->setup();
$this->managers['delete']->setup();
$this->managers['push']->setup();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ public function cloudinary_url( $public_id, $args = array(), $size = array(), $c
return implode( '/', $url_parts );
}

/**
* Get the details of an asset by public ID.
*
* @param string $public_id The public_id to check.
* @param string $type The asset type.
*
* @return array|\WP_Error
*/
public function get_asset_details( $public_id, $type ) {
$url = $this->url( 'resources', $type . '/upload/' . $public_id, true );

return $this->call( $url, array( 'body' => $args ), 'get' );
}

/**
* Upload a large asset in chunks.
*
Expand Down
Loading