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
108 changes: 61 additions & 47 deletions php/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ public function default_image_freeform_transformations( $default ) {
* @return string The converted URL.
*/
public function cloudinary_url( $attachment_id, $size = array(), $transformations = array(), $cloudinary_id = null, $overwrite_transformations = false ) {
if ( ! ( $cloudinary_id ) ) {
if ( ! $cloudinary_id ) {
$cloudinary_id = $this->cloudinary_id( $attachment_id );
if ( ! $cloudinary_id ) {
return null;
Expand Down Expand Up @@ -1094,7 +1094,7 @@ public function get_cloudinary_folder( $add_trailing_slash = true ) {
public function get_public_id( $attachment_id, $suffixed = false ) {
// Check for a public_id.
if ( $this->has_public_id( $attachment_id ) ) {
$public_id = get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
$public_id = $this->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
if ( $this->is_folder_synced( $attachment_id ) ) {
$public_id = $this->get_cloudinary_folder() . pathinfo( $public_id, PATHINFO_BASENAME );
}
Expand All @@ -1116,7 +1116,10 @@ public function get_public_id( $attachment_id, $suffixed = false ) {
* @return bool
*/
public function has_public_id( $attachment_id ) {
return ! empty( get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) );
$new_id = $this->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
$id = get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );

return ! empty( $new_id ) || ! empty( $id );
}

/**
Expand Down Expand Up @@ -1156,23 +1159,29 @@ public function get_cloudinary_id( $attachment_id ) {
*
* @param int $attachment_id The ID to get Cloudinary id for.
*
* @return string|null the ID or null if not existing.
* @return string|false the ID or false if not existing.
*/
public function cloudinary_id( $attachment_id ) {
static $cloudinary_ids = array();

if ( ! $this->is_media( $attachment_id ) ) {
return null;
}
// Return cached ID if we've already gotten it before.
if ( isset( $this->cloudinary_ids[ $attachment_id ] ) ) {
return $this->cloudinary_ids[ $attachment_id ];
if ( isset( $cloudinary_ids[ $attachment_id ] ) ) {
return $cloudinary_ids[ $attachment_id ];
}

if ( ! $this->is_media( $attachment_id ) ) {
$cloudinary_ids[ $attachment_id ] = false;
return false;
}

if ( ! $this->sync->is_synced( $attachment_id ) && ! defined( 'REST_REQUEST' ) ) {
$sync_type = $this->sync->maybe_prepare_sync( $attachment_id );
// Check sync type allows for continued rendering. i.e meta update, breakpoints etc, will still allow the URL to work,
// Where is type "file" will not since it's still being uploaded.
if ( ! is_null( $sync_type ) && $this->sync->is_required( $sync_type, $attachment_id ) ) {
return null; // Return and render local URLs.
if ( is_null( $sync_type ) || $this->sync->is_required( $sync_type, $attachment_id ) ) {
// Cache ID to prevent multiple lookups.
$cloudinary_ids[ $attachment_id ] = false;
return false; // Return and render local URLs.
}
}

Expand All @@ -1195,8 +1204,9 @@ public function cloudinary_id( $attachment_id ) {
* @param int $attachment_id The id of the asset.
*/
do_action( 'cloudinary_id', $cloudinary_id, $attachment_id );
// Cache ID to prevent multiple lookups.
$this->cloudinary_ids[ $attachment_id ] = $cloudinary_id;

$cloudinary_ids[ $attachment_id ] = $cloudinary_id;


return $cloudinary_id;
}
Expand Down Expand Up @@ -1480,18 +1490,16 @@ private function create_attachment( $asset, $public_id ) {

$sync_key = $asset['sync_key'];
// Capture public_id. Use core update_post_meta since this attachment data doesnt exist yet.
update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
$this->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );

// Capture version number.
$this->update_post_meta( $attachment_id, Sync::META_KEYS['version'], $asset['version'] );
if ( ! empty( $asset['transformations'] ) ) {
// Save a combined key.
$sync_key .= wp_json_encode( $asset['transformations'] );
$this->update_post_meta( $attachment_id, Sync::META_KEYS['transformation'], $asset['transformations'] );
}
// create a trackable key in post meta.
update_post_meta( $attachment_id, '_' . md5( $sync_key ), true );
// record a base to ensure primary isn't deleted.
update_post_meta( $attachment_id, '_' . md5( 'base_' . $public_id ), true );

// capture the delivery type.
$this->update_post_meta( $attachment_id, Sync::META_KEYS['delivery'], $asset['type'] );
// Capture the ALT Text.
Expand Down Expand Up @@ -1835,23 +1843,24 @@ public function get_transformation_from_meta( $post_id ) {
*/
public function get_post_meta( $post_id, $key = '', $single = false ) {

$meta_data = wp_get_attachment_metadata( $post_id, true );
if ( ! is_array( $meta_data ) ) {
return get_post_meta( $post_id, $key, $single );
$meta = get_post_meta( $post_id, Sync::META_KEYS['cloudinary'], true );
if ( empty( $meta ) ) {
$meta = array();
$old_meta = wp_get_attachment_metadata( $post_id, true );
if ( isset( $old_meta[ Sync::META_KEYS['cloudinary'] ] ) ) {
$meta = $old_meta[ Sync::META_KEYS['cloudinary'] ];
// Add public ID.
$public_id = get_post_meta( $post_id, Sync::META_KEYS['public_id'], true );
$meta[ Sync::META_KEYS['public_id'] ] = $public_id;
update_post_meta( $post_id, Sync::META_KEYS['cloudinary'], $meta );
delete_post_meta( $post_id, Sync::META_KEYS['public_id'] );
}
}
if ( ! isset( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) ) {
$meta_data[ Sync::META_KEYS['cloudinary'] ] = array();
if ( '' !== $key ) {
$meta = isset( $meta[ $key ] ) ? $meta[ $key ] : null;
}

if ( '' === $key ) {
$data = $meta_data[ Sync::META_KEYS['cloudinary'] ];
} elseif ( ! empty( $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] ) ) {
$data = $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ];
} else {
$data = $this->build_cached_meta( $post_id, $key, $single );
}

return $data;
return $single ? $meta : (array) $meta;
}

/**
Expand All @@ -1878,34 +1887,39 @@ public function build_cached_meta( $post_id, $key, $single ) {
* @param int $post_id The attachment ID.
* @param string $key The meta key to get.
* @param string|array $data $the meta data to update.
*
* @return bool
*/
public function update_post_meta( $post_id, $key, $data ) {
$meta_data = wp_get_attachment_metadata( $post_id, true );
if ( is_array( $meta_data ) ) {
if ( ! isset( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) ) {
$meta_data[ Sync::META_KEYS['cloudinary'] ] = array();
}
$meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] = $data;
wp_update_attachment_metadata( $post_id, $meta_data );
} else {
// Update core mete data for consistency.
update_post_meta( $post_id, $key, $data );

$meta = $this->get_post_meta( $post_id );
if ( ! isset( $meta[ $key ] ) ) {
$meta[ $key ] = '';
}

if ( $meta[ $key ] !== $data ) {
$meta[ $key ] = $data;
}

return update_post_meta( $post_id, Sync::META_KEYS['cloudinary'], $meta );
}

/**
* Delete cloudinary metadata.
*
* @param int $post_id The attachment ID.
* @param string $key The meta key to get.
*
* @return bool
*/
public function delete_post_meta( $post_id, $key ) {
$meta_data = wp_get_attachment_metadata( $post_id, true );
if ( is_array( $meta_data ) && isset( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) && is_array( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) ) {
// Only do this side if has been set before.
unset( $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] );
wp_update_attachment_metadata( $post_id, $meta_data );

$meta = $this->get_post_meta( $post_id );
if ( isset( $meta[ $key ] ) ) {
unset( $meta[ $key ] );
}

return update_post_meta( $post_id, Sync::META_KEYS['cloudinary'], $meta );
}

/**
Expand Down
3 changes: 2 additions & 1 deletion php/class-report.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,8 @@ public function image_meta_viewer() {
*/
public function render( $post ) {
if ( 'attachment' === $post->post_type ) {
$meta = wp_get_attachment_metadata( $post->ID );
$sync = $this->plugin->get_component( 'sync' );
$meta = get_post_meta( $post->ID, $sync::META_KEYS['cloudinary'], true );

$args = array(
'type' => 'tag',
Expand Down
74 changes: 56 additions & 18 deletions php/class-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,24 +160,55 @@ public function been_synced( $attachment_id ) {
/**
* Checks if an asset is synced and up to date.
*
* @param int $post_id The post id to check.
* @param int $post_id The post id to check.
* @param bool $re_check Flag to bypass cache and recheck.
*
* @return bool
*/
public function is_synced( $post_id ) {
$expecting = $this->generate_signature( $post_id );

if ( ! is_wp_error( $expecting ) ) {
$signature = $this->get_signature( $post_id );
// Sort to align orders for comparison.
ksort( $signature );
ksort( $expecting );
if ( ! empty( $signature ) && ! empty( $expecting ) && $expecting === $signature ) {
return true;
public function is_synced( $post_id, $re_check = false ) {
static $synced = array();
if ( isset( $synced[ $post_id ] ) && false === $re_check ) {
return $synced[ $post_id ];
}
$return = false;
if ( $this->managers['media']->has_public_id( $post_id ) ) {
$expecting = $this->generate_signature( $post_id );
if ( ! is_wp_error( $expecting ) ) {
$signature = $this->get_signature( $post_id );
// Sort to align orders for comparison.
ksort( $signature );
ksort( $expecting );
if ( ! empty( $signature ) && ! empty( $expecting ) && $expecting === $signature ) {
$return = true;
}
}
}
$synced[ $post_id ] = $return;

return false;
return $synced[ $post_id ];
}

/**
* Log a sync result.
*
* @param int $attachment_id The attachment id.
* @param string $type The sync type.
* @param mixed $result The result.
*/
public function log_sync_result( $attachment_id, $type, $result ) {
$log = $this->managers['media']->get_post_meta( $attachment_id, self::META_KEYS['process_log'], true );
$keys = array_keys( $this->sync_base_struct );
if ( empty( $log ) || count( $log ) !== count( $keys ) ) {
$log = array_fill_keys( $keys, array() );
}
if ( isset( $log[ $type ] ) ) {

$log[ $type ][ time() ] = $result;
if ( 5 < count( $log[ $type ] ) ) {
array_shift( $log[ $type ] );
}
$this->managers['media']->update_post_meta( $attachment_id, self::META_KEYS['process_log'], $log );
}
}

/**
Expand Down Expand Up @@ -472,7 +503,7 @@ public function setup_sync_base_struct() {
'priority' => 25,
'sync' => array( $this->managers['upload'], 'explicit_update' ),
'validate' => function ( $attachment_id ) {
$delivery = $this->managers['media']->get_post_meta( $attachment_id, self::META_KEYS['delivery'] );
$delivery = $this->managers['media']->get_post_meta( $attachment_id, self::META_KEYS['delivery'], true );

return empty( $delivery ) || 'upload' === $delivery;
},
Expand Down Expand Up @@ -512,7 +543,7 @@ public function setup_sync_base_struct() {
'sync' => function ( $attachment_id ) {
$meta = $this->managers['media']->get_post_meta( $attachment_id );
foreach ( $meta as $key => $value ) {
if ( self::META_KEYS['public_id'] === $key ) {
if ( Sync::META_KEYS['cloudinary'] === $key ) {
$this->managers['media']->delete_post_meta( $attachment_id, $key );
continue;
}
Expand Down Expand Up @@ -665,10 +696,17 @@ public function sync_base( $post ) {
* @return string | null
*/
public function maybe_prepare_sync( $attachment_id ) {

$type = $this->get_sync_type( $attachment_id );
if ( $type && $this->can_sync( $attachment_id, $type ) ) {
$this->add_to_sync( $attachment_id );
$type = null;
if ( $this->can_sync( $attachment_id, $type ) ) {
$type = $this->get_sync_type( $attachment_id );
if ( $type ) {
$this->add_to_sync( $attachment_id );
} else {
// if null, and can sync but has no type, realtime syncs may have been applied. so recheck.
if ( $this->is_synced( $attachment_id, true ) ) {
$type = true;
}
}
}

return $type;
Expand Down
6 changes: 6 additions & 0 deletions php/connect/class-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,12 @@ function_exists( 'wp_get_original_image_url' ) &&
$file_url = wp_get_attachment_url( $attachment_id );
}
$media = get_plugin_instance()->get_component( 'media' );
if ( ! $media->is_local_media( $attachment_id ) ) {
$disable_https_fetch = false; // Remote can upload via url.
// translators: variable is thread name and queue size.
$action_message = sprintf( __( 'Uploading remote url: %1$s.', 'cloudinary' ), $file_url );
do_action( '_cloudinary_queue_action', $action_message );
}
$tempfile = false;
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 Down
8 changes: 4 additions & 4 deletions php/media/class-upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public function convert_cloudinary_version( $attachment_id ) {
if ( empty( $public_id ) ) {
$public_id = implode( '/', $parts );
}
update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['version'], $asset_version );
if ( ! empty( $asset_transformations ) ) {
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['transformation'], $asset_transformations );
Expand All @@ -115,7 +115,7 @@ public function convert_cloudinary_version( $attachment_id ) {
}
$public_id .= $suffix;
$this->media->delete_post_meta( $attachment_id, Sync::META_KEYS['suffix'] );
update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
}
// Check folder sync in order.
if ( $this->media->is_folder_synced( $attachment_id ) ) {
Expand All @@ -142,8 +142,8 @@ public function convert_cloudinary_version( $attachment_id ) {
if ( ! empty( $transformations ) ) {
$sync_key .= wp_json_encode( $transformations );
}
update_post_meta( $attachment_id, '_' . md5( $sync_key ), true );
update_post_meta( $attachment_id, '_' . md5( 'base_' . $public_id ), true );
delete_post_meta( $attachment_id, '_' . md5( $sync_key ), true );
delete_post_meta( $attachment_id, '_' . md5( 'base_' . $public_id ), true );
// Get a new uncached signature.
$this->sync->get_signature( $attachment_id, true );

Expand Down
17 changes: 2 additions & 15 deletions php/sync/class-download-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,21 +174,8 @@ public function download_asset( $attachment_id, $source = null, $date = null ) {

// Prepare the asset.
update_attached_file( $attachment_id, $upload['file'] );
$old_meta = wp_get_attachment_metadata( $attachment_id );
ob_start(); // Catch possible errors in WordPress's ID3 module when setting meta for transformed videos.
$meta = wp_generate_attachment_metadata( $attachment_id, $upload['file'] );
$captured_errors = ob_get_clean(); // Capture issues.
// Be sure to record v2 meta.
if ( ! empty( $old_meta[ Sync::META_KEYS['cloudinary'] ] ) ) {
$meta[ Sync::META_KEYS['cloudinary'] ] = $old_meta[ Sync::META_KEYS['cloudinary'] ];
} else {
// Maybe capture newest meta.
$maybe_new = wp_get_attachment_metadata( $attachment_id );
if ( ! empty( $maybe_new[ Sync::META_KEYS['cloudinary'] ] ) ) {
$meta[ Sync::META_KEYS['cloudinary'] ] = $maybe_new[ Sync::META_KEYS['cloudinary'] ];
}
}
wp_update_attachment_metadata( $attachment_id, $meta );
wp_generate_attachment_metadata( $attachment_id, $upload['file'] );

// Update the folder synced flag.
$public_id = $this->media->get_public_id( $attachment_id );
$asset_folder = strpos( $public_id, '/' ) ? dirname( $public_id ) : '/';
Expand Down
Loading