@@ -351,6 +351,9 @@ public function prepare_upload( $post, $down_sync = false ) {
351351 return new \WP_Error ( 'attachment_post_expected ' , __ ( 'An attachment post was expected. ' , 'cloudinary ' ) );
352352 }
353353
354+ // Get the media component.
355+ $ media = $ this ->plugin ->components ['media ' ];
356+
354357 // First check if this has a file and it can be uploaded.
355358 $ file = get_attached_file ( $ post ->ID );
356359 $ file_size = 0 ;
@@ -359,7 +362,7 @@ public function prepare_upload( $post, $down_sync = false ) {
359362 } elseif ( ! file_exists ( $ file ) ) {
360363 // May be an old upload type.
361364 $ src = get_post_meta ( $ post ->ID , '_wp_attached_file ' , true );
362- if ( $ this -> plugin -> components [ ' media ' ] ->is_cloudinary_url ( $ src ) ) {
365+ if ( $ media ->is_cloudinary_url ( $ src ) ) {
363366 // Download first maybe.
364367 if ( true === $ down_sync ) {
365368 $ download = $ this ->plugin ->components ['sync ' ]->managers ['download ' ]->down_sync ( $ post ->ID );
@@ -384,7 +387,7 @@ public function prepare_upload( $post, $down_sync = false ) {
384387
385388 // translators: variable is file size.
386389 $ error = sprintf ( __ ( 'File size exceeds the maximum of %s. This media asset will be served from WordPress. ' , 'cloudinary ' ), $ max_size_hr );
387- $ this -> plugin -> components [ ' media ' ] ->delete_post_meta ( $ post ->ID , Sync::META_KEYS ['pending ' ] ); // Remove Flag.
390+ $ media ->delete_post_meta ( $ post ->ID , Sync::META_KEYS ['pending ' ] ); // Remove Flag.
388391
389392 return new \WP_Error ( 'upload_error ' , $ error );
390393 }
@@ -409,7 +412,14 @@ public function prepare_upload( $post, $down_sync = false ) {
409412 $ public_id_folder = trailingslashit ( $ public_id_info ['dirname ' ] );
410413 $ public_id_file = $ public_id_info ['filename ' ];
411414 }
412-
415+ // Check if this asset is a folder sync.
416+ $ folder_sync = $ media ->get_post_meta ( $ post ->ID , Sync::META_KEYS ['folder_sync ' ], true );
417+ if ( ! empty ( $ folder_sync ) ) {
418+ $ public_id_folder = $ cld_folder ; // Ensure the public ID folder is constant.
419+ } else {
420+ // Not folder synced, so set the folder to the folder that the asset originally came from.
421+ $ cld_folder = $ public_id_folder ;
422+ }
413423 // Prepare upload options.
414424 $ options = array (
415425 'unique_filename ' => false ,
@@ -436,7 +446,7 @@ public function prepare_upload( $post, $down_sync = false ) {
436446 $ imagesize = getimagesize ( $ file );
437447 $ meta ['width ' ] = $ imagesize [0 ];
438448 }
439- $ max_width = $ this -> plugin -> components [ ' media ' ] ->get_max_width ();
449+ $ max_width = $ media ->get_max_width ();
440450 // Add breakpoints request options.
441451 if ( ! empty ( $ settings ['global_transformations ' ]['enable_breakpoints ' ] ) ) {
442452 $ options ['responsive_breakpoints ' ] = array (
@@ -446,7 +456,7 @@ public function prepare_upload( $post, $down_sync = false ) {
446456 'max_width ' => $ meta ['width ' ] < $ max_width ? $ meta ['width ' ] : $ max_width ,
447457 'min_width ' => $ settings ['global_transformations ' ]['min_width ' ],
448458 );
449- $ transformations = $ this -> plugin -> components [ ' media ' ] ->get_transformation_from_meta ( $ post ->ID );
459+ $ transformations = $ media ->get_transformation_from_meta ( $ post ->ID );
450460 if ( ! empty ( $ transformations ) ) {
451461 $ options ['responsive_breakpoints ' ]['transformation ' ] = Api::generate_transformation_string ( $ transformations );
452462 }
@@ -526,6 +536,8 @@ public function push_attachments( $attachments ) {
526536 'total ' => count ( $ attachments ),
527537 'processed ' => 0 ,
528538 );
539+ // Get media component.
540+ $ media = $ this ->plugin ->components ['media ' ];
529541
530542 // Go over each attachment.
531543 foreach ( $ attachments as $ attachment ) {
@@ -580,7 +592,7 @@ public function push_attachments( $attachments ) {
580592 } elseif ( 'rename ' === $ sync_type ) {
581593 // Rename an asset.
582594 $ args = array (
583- 'from_public_id ' => $ this -> plugin -> components [ ' media ' ] ->get_post_meta ( $ attachment ->ID , Sync::META_KEYS ['public_id ' ] ),
595+ 'from_public_id ' => $ media ->get_post_meta ( $ attachment ->ID , Sync::META_KEYS ['public_id ' ] ),
584596 'to_public_id ' => $ upload ['public_id ' ],
585597 );
586598 $ result = $ this ->plugin ->components ['connect ' ]->api ->{$ upload ['options ' ]['resource_type ' ]}( 'rename ' , 'POST ' , $ args );
@@ -597,7 +609,7 @@ public function push_attachments( $attachments ) {
597609 if ( is_wp_error ( $ result ) ) {
598610 $ error = $ result ->get_error_message ();
599611 $ stats ['fail ' ][] = $ error ;
600- $ this -> plugin -> components [ ' media ' ] ->update_post_meta ( $ attachment ->ID , Sync::META_KEYS ['sync_error ' ], $ error );
612+ $ media ->update_post_meta ( $ attachment ->ID , Sync::META_KEYS ['sync_error ' ], $ error );
601613 continue ;
602614 }
603615
@@ -611,8 +623,8 @@ public function push_attachments( $attachments ) {
611623 if ( ! empty ( $ result ['version ' ] ) ) {
612624 $ meta_data [ Sync::META_KEYS ['version ' ] ] = $ result ['version ' ];
613625 }
614- $ this -> plugin -> components [ ' media ' ] ->delete_post_meta ( $ attachment ->ID , Sync::META_KEYS ['pending ' ] );
615- $ this -> plugin -> components [ ' media ' ] ->delete_post_meta ( $ attachment ->ID , Sync::META_KEYS ['sync_error ' ], false );
626+ $ media ->delete_post_meta ( $ attachment ->ID , Sync::META_KEYS ['pending ' ] );
627+ $ media ->delete_post_meta ( $ attachment ->ID , Sync::META_KEYS ['sync_error ' ], false );
616628 if ( ! empty ( $ this ->plugin ->config ['settings ' ]['global_transformations ' ]['enable_breakpoints ' ] ) ) {
617629 if ( ! empty ( $ result ['responsive_breakpoints ' ] ) ) { // Images only.
618630 $ meta_data [ Sync::META_KEYS ['breakpoints ' ] ] = $ result ['responsive_breakpoints ' ][0 ]['breakpoints ' ];
@@ -633,7 +645,7 @@ public function push_attachments( $attachments ) {
633645 $ meta = wp_get_attachment_metadata ( $ attachment ->ID , true );
634646 $ meta [ Sync::META_KEYS ['cloudinary ' ] ] = $ meta_data ;
635647 wp_update_attachment_metadata ( $ attachment ->ID , $ meta );
636- $ this -> plugin -> components [ ' media ' ] ->update_post_meta ( $ attachment ->ID , Sync::META_KEYS ['public_id ' ], $ upload ['options ' ]['public_id ' ] );
648+ $ media ->update_post_meta ( $ attachment ->ID , Sync::META_KEYS ['public_id ' ], $ upload ['options ' ]['public_id ' ] );
637649 // Search and update link references in content.
638650 $ content_search = new \WP_Query ( array ( 's ' => 'wp-image- ' . $ attachment ->ID , 'fields ' => 'ids ' , 'posts_per_page ' => 1000 ) );
639651 if ( ! empty ( $ content_search ->found_posts ) ) {
0 commit comments