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
34 changes: 16 additions & 18 deletions php/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,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 = $this->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
$public_id = 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 @@ -1114,7 +1114,7 @@ public function get_public_id( $attachment_id, $suffixed = false ) {
* @return bool
*/
public function has_public_id( $attachment_id ) {
return ! empty( $this->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) );
return ! empty( get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) );
}

/**
Expand All @@ -1129,9 +1129,8 @@ public function get_cloudinary_id( $attachment_id ) {
$cloudinary_id = null;
// A cloudinary_id is a public_id with a file extension.
if ( $this->has_public_id( $attachment_id ) ) {
$public_id = $this->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true );
// Since this is based on saved and not a dynamic, append a suffix.
$public_id .= $this->get_post_meta( $attachment_id, Sync::META_KEYS['suffix'], true );
$public_id = $this->get_public_id( $attachment_id, true );
// Get the file, and use the same extension.
$file = get_attached_file( $attachment_id );
// @todo: Make this use the globals, overrides, and application conversion.
Expand Down Expand Up @@ -1443,24 +1442,24 @@ 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.
update_post_meta( $attachment_id, Sync::META_KEYS['version'], $asset['version'] );
$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'] );
update_post_meta( $attachment_id, Sync::META_KEYS['transformation'], $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.
update_post_meta( $attachment_id, Sync::META_KEYS['delivery'], $asset['type'] );
$this->update_post_meta( $attachment_id, Sync::META_KEYS['delivery'], $asset['type'] );
// Capture the ALT Text.
if ( ! empty( $asset['meta']['alt'] ) ) {
$alt_text = wp_strip_all_tags( $asset['meta']['alt'] );
update_post_meta( $attachment_id, '_wp_attachment_image_alt', $alt_text );
$this->update_post_meta( $attachment_id, '_wp_attachment_image_alt', $alt_text );
}

return $attachment_id;
Expand Down Expand Up @@ -1775,13 +1774,13 @@ public function get_transformation_from_meta( $post_id ) {
/**
* Get Cloudinary related Post meta.
*
* @param int $post_id The attachment ID.
* @param string $key The meta key to get.
* @param bool $single If single or not.
* @param int $post_id The attachment ID.
* @param string|null $key The meta key to get.
* @param bool $single If single or not.
*
* @return mixed
*/
public function get_post_meta( $post_id, $key, $single = false ) {
public function get_post_meta( $post_id, $key = null, $single = false ) {

$meta_data = wp_get_attachment_metadata( $post_id, true );
if ( ! is_array( $meta_data ) ) {
Expand All @@ -1790,7 +1789,10 @@ public function get_post_meta( $post_id, $key, $single = false ) {
if ( ! isset( $meta_data[ Sync::META_KEYS['cloudinary'] ] ) ) {
$meta_data[ Sync::META_KEYS['cloudinary'] ] = array();
}
if ( ! empty( $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] ) ) {

if ( null === $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 );
Expand All @@ -1812,8 +1814,6 @@ public function build_cached_meta( $post_id, $key, $single ) {
$data = get_post_meta( $post_id, $key, $single );
if ( '' !== $data ) {
$this->update_post_meta( $post_id, $key, $data );
// Remove the low level meta.
delete_post_meta( $post_id, $key );
}

return $data;
Expand Down Expand Up @@ -1853,8 +1853,6 @@ public function delete_post_meta( $post_id, $key ) {
unset( $meta_data[ Sync::META_KEYS['cloudinary'] ][ $key ] );
wp_update_attachment_metadata( $post_id, $meta_data );
}
// Delete meta data.
delete_post_meta( $post_id, $key );
}

/**
Expand Down
49 changes: 40 additions & 9 deletions php/class-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public function register_sync_type( $type, $structure ) {
public function setup_sync_base_struct() {

$base_struct = array(
'upgrade' => array(
'upgrade' => array(
'generate' => array( $this, 'get_sync_version' ), // Method to generate a signature.
'validate' => array( $this, 'been_synced' ),
'priority' => 0,
Expand All @@ -386,7 +386,7 @@ public function setup_sync_base_struct() {
'note' => __( 'Upgrading from previous version', 'cloudinary' ),
'realtime' => true,
),
'download' => array(
'download' => array(
'generate' => '__return_false',
'validate' => function ( $attachment_id ) {
$file = get_attached_file( $attachment_id );
Expand All @@ -398,7 +398,7 @@ public function setup_sync_base_struct() {
'state' => 'info downloading',
'note' => __( 'Downloading from Cloudinary', 'cloudinary' ),
),
'file' => array(
'file' => array(
'generate' => array( $this, 'generate_file_signature' ),
'priority' => 5.1,
'sync' => array( $this->managers['upload'], 'upload_asset' ),
Expand All @@ -409,7 +409,7 @@ public function setup_sync_base_struct() {
'note' => __( 'Uploading to Cloudinary', 'cloudinary' ),
'required' => true, // Required to complete URL render flag.
),
'folder' => array(
'folder' => array(
'generate' => array( $this->managers['media'], 'get_cloudinary_folder' ),
'validate' => array( $this->managers['media'], 'is_folder_synced' ),
'priority' => 10,
Expand All @@ -424,7 +424,7 @@ public function setup_sync_base_struct() {
},
'required' => true, // Required to complete URL render flag.
),
'public_id' => array(
'public_id' => array(
'generate' => array( $this->managers['media'], 'get_public_id' ),
'validate' => function ( $attachment_id ) {
$public_id = $this->managers['media']->has_public_id( $attachment_id );
Expand All @@ -437,7 +437,7 @@ public function setup_sync_base_struct() {
'note' => __( 'Updating metadata', 'cloudinary' ),
'required' => true,
),
'breakpoints' => array(
'breakpoints' => array(
'generate' => array( $this->managers['media'], 'get_breakpoint_options' ),
'priority' => 25,
'sync' => array( $this->managers['upload'], 'explicit_update' ),
Expand All @@ -449,21 +449,49 @@ public function setup_sync_base_struct() {
'state' => 'info syncing',
'note' => __( 'Updating breakpoints', 'cloudinary' ),
),
'options' => array(
'options' => array(
'generate' => array( $this->managers['media'], 'get_upload_options' ),
'priority' => 30,
'sync' => array( $this->managers['upload'], 'context_update' ),
'state' => 'info syncing',
'note' => __( 'Updating metadata', 'cloudinary' ),
),
'cloud_name' => array(
'cloud_name' => array(
'generate' => array( $this->managers['connect'], 'get_cloud_name' ),
'priority' => 5.5,
'sync' => array( $this->managers['upload'], 'upload_asset' ),
'state' => 'uploading',
'note' => __( 'Uploading to new cloud name.', 'cloudinary' ),
'required' => true,
),
'meta_cleanup' => array(
'generate' => function ( $attachment_id ) {
$meta = $this->managers['media']->get_post_meta( $attachment_id );
$return = false;
foreach ( $meta as $key => $value ) {
if ( get_post_meta( $attachment_id, $key, true ) === $value ) {
$return = true;
break;
}
}

return $return;
},
'priority' => 100, // Always be the highest.
'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 ) {
$this->managers['media']->delete_post_meta( $attachment_id, $key );
continue;
}
delete_post_meta( $attachment_id, $key, $value );
}
$this->set_signature_item( $attachment_id, 'meta_cleanup' );
},
'required' => true,
'realtime' => true,
),
);

/**
Expand Down Expand Up @@ -909,7 +937,10 @@ public function settings() {
array(
'type' => 'radio',
'title' => __( 'Sync method', 'cloudinary' ),
'tooltip_text' => __( 'Auto sync: Ensures that all of your WordPress assets are automatically synced with Cloudinary when they are added to the WordPress Media Library. Manual sync: Assets must be synced manually using the WordPress Media Library', 'cloudinary' ),
'tooltip_text' => __(
'Auto sync: Ensures that all of your WordPress assets are automatically synced with Cloudinary when they are added to the WordPress Media Library. Manual sync: Assets must be synced manually using the WordPress Media Library',
'cloudinary'
),
'slug' => 'auto_sync',
'no_cached' => true,
'default' => 'on',
Expand Down
2 changes: 1 addition & 1 deletion php/media/class-upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function convert_cloudinary_version( $attachment_id ) {
$public_id = $this->media->get_public_id( $attachment_id, true );
// Check folder sync in order.
if ( $this->media->is_folder_synced( $attachment_id ) ) {
$public_id_folder = ltrim( dirname( $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) ), '.' );
$public_id_folder = ltrim( dirname( $this->media->get_public_id( $attachment_id ) ) );
$test_signature = md5( false );
$folder_signature = md5( $public_id_folder );
$signature = $this->sync->get_signature( $attachment_id );
Expand Down
60 changes: 1 addition & 59 deletions php/sync/class-download-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,63 +125,6 @@ public function rest_download_asset( \WP_REST_Request $request ) {
return rest_ensure_response( $response );
}

/**
* Prepare and sync down an asset stored remotely.
*
* @param int $attachment_id The attachment ID.
*
* @return array|\WP_Error
*/
public function down_sync( $attachment_id ) {
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
$path = wp_parse_url( $file, PHP_URL_PATH );
$media = $this->plugin->components['media'];
$parts = explode( '/', $path );
$parts = array_map(
function ( $val ) use ( $media ) {
if ( empty( $val ) ) {
return false;
}
if ( $val === $media->credentials['cloud_name'] ) {
return false;
}
if ( in_array( $val, array( 'image', 'video', 'upload' ), true ) ) {
return false;
}
$transformation_maybe = $media->get_transformations_from_string( $val );
if ( ! empty( $transformation_maybe ) ) {
return false;
}
if ( substr( $val, 0, 1 ) === 'v' && is_numeric( substr( $val, 1 ) ) ) {
return false;
}

return $val;
},
$parts
);
// Build public_id.
$parts = array_filter( $parts );
$public_id = implode( '/', $parts );
// Remove extension.
$path = pathinfo( $public_id );
$public_id = strstr( $public_id, '.' . $path['extension'], true );
// Save public ID.
$media->update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );
// Check if the asset is in the same folder as the defined Cloudinary folder.
if ( false !== strpos( $public_id, '/' ) ) {
$path = pathinfo( $public_id );
$asset_folder = trailingslashit( $path['dirname'] );
$cloudinary_folder = trailingslashit( $this->plugin->settings->get_value( 'cloudinary_folder' ) );
if ( $asset_folder === $cloudinary_folder ) {
// The asset folder matches the defined cloudinary folder, flag it as being in a folder sync.
$media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true );
}
}

return $this->download_asset( $attachment_id, $file );
}

/**
* Download an attachment source to the file system.
*
Expand Down Expand Up @@ -253,8 +196,7 @@ public function download_asset( $attachment_id, $source = null, $date = null ) {
if ( $asset_folder === $cloudinary_folder ) {
$this->media->update_post_meta( $attachment_id, Sync::META_KEYS['folder_sync'], true );
}
// Create synced post meta as a way to search for synced / unsynced items.
update_post_meta( $attachment_id, Sync::META_KEYS['public_id'], $public_id );

// Generate signatures.
$this->sync->set_signature_item( $attachment_id, 'options' );
$this->sync->set_signature_item( $attachment_id, 'cloud_name' );
Expand Down
1 change: 1 addition & 0 deletions php/sync/class-push-sync.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public function process_assets( $attachments = array() ) {
}
// Flag attachment as being processed.
update_post_meta( $attachment_id, Sync::META_KEYS['syncing'], time() );
$stat[ $attachment_id ] = array();
while ( $type = $this->sync->get_sync_type( $attachment_id, false ) ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition
if ( isset( $stat[ $attachment_id ][ $type ] ) ) {
// Loop prevention.
Expand Down
15 changes: 12 additions & 3 deletions php/sync/class-storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ public function generate_signature( $attachment_id ) {
}
}

return $this->settings['offload'] . $this->media->get_post_meta( $attachment_id, Sync::META_KEYS['public_id'], true ) . $file_exists;
return $this->settings['offload'] . $this->media->get_public_id( $attachment_id ) . $file_exists;
}

/**
Expand Down Expand Up @@ -241,13 +241,22 @@ public function sync( $attachment_id ) {
*/
protected function remove_local_assets( $attachment_id ) {
// Delete local versions of images.
$meta = wp_get_attachment_metadata( $attachment_id );
$meta = wp_get_attachment_metadata( $attachment_id );
$backup_sizes = '';
if ( ! empty( $meta['backup_sizes'] ) ) {
// Replace backup sizes.
$meta['sizes'] = $meta['backup_sizes'];
}

return wp_delete_attachment_files( $attachment_id, $meta, array(), get_attached_file( $attachment_id ) );
if ( ! empty( $meta['sizes'] ) ) {
$backup_sizes = $meta['sizes'];
}

if ( empty( $meta['file'] ) ) {
$meta['file'] = get_the_guid( $attachment_id );
}

return wp_delete_attachment_files( $attachment_id, $meta, $backup_sizes, get_attached_file( $attachment_id ) );
}

/**
Expand Down
4 changes: 2 additions & 2 deletions php/sync/class-sync-queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,13 @@ public function build_queue() {
);

// translators: variable is page number.
$action_message = sprintf( __( 'Building Queue.', 'cloudinary' ), $args['paged'] );
$action_message = __( 'Building Queue.', 'cloudinary' );
do_action( '_cloudinary_queue_action', $action_message );

$query = new \WP_Query( $args );
if ( ! $query->have_posts() ) {
// translators: variable is page number.
$action_message = sprintf( __( 'No posts', 'cloudinary' ), $args['paged'] );
$action_message = __( 'No posts', 'cloudinary' );
do_action( '_cloudinary_queue_action', $action_message );

return;
Expand Down