Skip to content

Commit a6837f9

Browse files
Merge pull request #1072 from cloudinary/fix/plugin-assets-sync
Fix/plugin assets sync
2 parents a17ab33 + e6dabea commit a6837f9

File tree

5 files changed

+71
-9
lines changed

5 files changed

+71
-9
lines changed

package-lock.json

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
"cross-env": "^7.0.2",
8585
"css-loader": "^5.0.1",
8686
"css-minimizer-webpack-plugin": "^4.2.2",
87+
"css-unicode-loader": "^1.0.3",
8788
"cssnano": "^5.0.2",
8889
"dot-object": "^2.1.4",
8990
"dotenv": "^8.2.0",
@@ -144,4 +145,4 @@
144145
"webpackbar": "^5.0.2"
145146
},
146147
"version": "3.2.11"
147-
}
148+
}

php/class-assets.php

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ protected function register_hooks() {
156156
add_filter( 'cloudinary_asset_state', array( $this, 'filter_asset_state' ), 10, 2 );
157157
add_filter( 'cloudinary_set_usable_asset', array( $this, 'check_usable_asset' ) );
158158
// Actions.
159-
add_action( 'cloudinary_init_settings', array( $this, 'setup' ) );
159+
add_action( 'cloudinary_ready', array( $this, 'setup' ) );
160160
add_action( 'cloudinary_thread_queue_details_query', array( $this, 'connect_post_type' ) );
161161
add_action( 'cloudinary_build_queue_query', array( $this, 'connect_post_type' ) );
162162
add_action( 'cloudinary_string_replace', array( $this, 'add_url_replacements' ), 20 );
@@ -441,6 +441,7 @@ public function update_asset_paths() {
441441
// Check and update version if needed.
442442
if ( $this->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) {
443443
$this->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version );
444+
$this->sync_parent( $asset_path->ID );
444445
}
445446
}
446447
}
@@ -538,11 +539,12 @@ public function create_asset_parent( $path, $version ) {
538539
}
539540

540541
/**
541-
* Purge a single asset parent.
542+
* Process all child assets of a parent with a given callback.
542543
*
543-
* @param int $parent_id The Asset parent to purge.
544+
* @param int $parent_id The Asset parent to process.
545+
* @param callable $callback The callback function to execute on each post.
544546
*/
545-
public function purge_parent( $parent_id ) {
547+
private function process_parent_assets( $parent_id, $callback ) {
546548
$query_args = array(
547549
'post_type' => self::POST_TYPE_SLUG,
548550
'posts_per_page' => 100,
@@ -554,11 +556,13 @@ public function purge_parent( $parent_id ) {
554556
);
555557
$query = new \WP_Query( $query_args );
556558
$previous_total = $query->found_posts;
559+
557560
do {
558561
$this->lock_assets();
559562
$posts = $query->get_posts();
563+
560564
foreach ( $posts as $post_id ) {
561-
wp_delete_post( $post_id );
565+
call_user_func( $callback, $post_id );
562566
}
563567

564568
$query_args = $query->query_vars;
@@ -569,6 +573,40 @@ public function purge_parent( $parent_id ) {
569573
} while ( $query->have_posts() );
570574
}
571575

576+
/**
577+
* Sync the assets of a parent.
578+
*
579+
* @param int $parent_id The Asset parent to sync.
580+
*/
581+
public function sync_parent( $parent_id ) {
582+
$this->process_parent_assets(
583+
$parent_id,
584+
function ( $post_id ) {
585+
if ( empty( $this->media->sync ) || ! $this->media->sync->can_sync( $post_id ) ) {
586+
return;
587+
}
588+
589+
$this->media->sync->set_signature_item( $post_id, 'file', '' );
590+
$this->media->sync->set_signature_item( $post_id, 'cld_asset' );
591+
$this->media->sync->add_to_sync( $post_id );
592+
}
593+
);
594+
}
595+
596+
/**
597+
* Purge a single asset parent.
598+
*
599+
* @param int $parent_id The Asset parent to purge.
600+
*/
601+
public function purge_parent( $parent_id ) {
602+
$this->process_parent_assets(
603+
$parent_id,
604+
function ( $post_id ) {
605+
wp_delete_post( $post_id );
606+
}
607+
);
608+
}
609+
572610
/**
573611
* Lock asset creation for performing things like purging that require no changes.
574612
*/

php/class-media.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Cloudinary;
99

10+
use Cloudinary\Assets;
1011
use Cloudinary\Component\Setup;
1112
use Cloudinary\Connect\Api;
1213
use Cloudinary\Media\Filter;
@@ -2963,9 +2964,17 @@ public function apply_srcset( $content, $attachment_id, $overwrite_transformatio
29632964
public function get_cloudinary_version( $attachment_id ) {
29642965
$version = (int) $this->get_post_meta( $attachment_id, Sync::META_KEYS['version'], true );
29652966

2967+
if ( empty( $version ) ) {
2968+
// This might be also an asset from the hidden post type (Assets::POST_TYPE_SLUG).
2969+
$attachment = get_post( $attachment_id );
2970+
2971+
if ( ! empty( $attachment ) && Assets::POST_TYPE_SLUG === $attachment->post_type && ! empty( $attachment->post_parent ) ) {
2972+
$version = (int) preg_replace( '/\D/', '', $this->get_post_meta( (int) $attachment->post_parent, Sync::META_KEYS['version'], true ) );
2973+
}
2974+
}
2975+
29662976
return $version ? $version : 1;
29672977
}
2968-
29692978
/**
29702979
* Upgrade media related settings, including global transformations etc.
29712980
*

webpack.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ const cldCore = {
118118
loader: MiniCssExtractPlugin.loader,
119119
},
120120
'css-loader',
121+
'css-unicode-loader',
121122
'sass-loader',
122123
],
123124
},

0 commit comments

Comments
 (0)