From 750607f096c5462c99bbd9dc8ade904d4c6cb9c1 Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Tue, 1 Jul 2025 15:27:26 +0300 Subject: [PATCH 1/3] Set asset version based on parent --- php/class-assets.php | 1 + php/class-media.php | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/php/class-assets.php b/php/class-assets.php index 601ba06be..f663abb0c 100644 --- a/php/class-assets.php +++ b/php/class-assets.php @@ -440,6 +440,7 @@ public function update_asset_paths() { // Check and update version if needed. if ( $this->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) { + $this->purge_parent( $asset_path->ID ); $this->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version ); } } diff --git a/php/class-media.php b/php/class-media.php index aaf1cb5a6..182782b3d 100644 --- a/php/class-media.php +++ b/php/class-media.php @@ -7,6 +7,7 @@ namespace Cloudinary; +use Cloudinary\Assets; use Cloudinary\Component\Setup; use Cloudinary\Connect\Api; use Cloudinary\Media\Filter; @@ -2963,9 +2964,17 @@ public function apply_srcset( $content, $attachment_id, $overwrite_transformatio public function get_cloudinary_version( $attachment_id ) { $version = (int) $this->get_post_meta( $attachment_id, Sync::META_KEYS['version'], true ); + if ( empty( $version ) ) { + // This might be also an asset from the hidden post type (Assets::POST_TYPE_SLUG). + $attachment = get_post( $attachment_id ); + + if ( ! empty( $attachment ) && Assets::POST_TYPE_SLUG === $attachment->post_type && ! empty( $attachment->post_parent ) ) { + $version = (int) preg_replace( '/\D/', '', $this->get_post_meta( (int) $attachment->post_parent, Sync::META_KEYS['version'], true ) ); + } + } + return $version ? $version : 1; } - /** * Upgrade media related settings, including global transformations etc. * From 0933bee259272991788cde42eabfcee4e488bec2 Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Tue, 8 Jul 2025 15:17:27 +0300 Subject: [PATCH 2/3] Add CSS Unicode Loader --- package-lock.json | 17 +++++++++++++++-- package.json | 3 ++- webpack.config.js | 1 + 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 890d22f61..ba640f0ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "cloudinary", - "version": "3.2.7", + "version": "3.2.10", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "cloudinary", - "version": "3.2.7", + "version": "3.2.10", "hasInstallScript": true, "license": "GPL-2.0+", "devDependencies": { @@ -44,6 +44,7 @@ "cross-env": "^7.0.2", "css-loader": "^5.0.1", "css-minimizer-webpack-plugin": "^4.2.2", + "css-unicode-loader": "^1.0.3", "cssnano": "^5.0.2", "dot-object": "^2.1.4", "dotenv": "^8.2.0", @@ -18728,6 +18729,12 @@ "node": ">=8.0.0" } }, + "node_modules/css-unicode-loader": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/css-unicode-loader/-/css-unicode-loader-1.0.3.tgz", + "integrity": "sha512-mszxqB0LCBO2ixbaz/tAH17iEAXmytUv0j/YXtPxhOi8D8Teh+mOXqmz+DZRtDil5tFx7ltbgw16dUptEw4jOg==", + "dev": true + }, "node_modules/css-what": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz", @@ -55147,6 +55154,12 @@ "source-map": "^0.6.1" } }, + "css-unicode-loader": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/css-unicode-loader/-/css-unicode-loader-1.0.3.tgz", + "integrity": "sha512-mszxqB0LCBO2ixbaz/tAH17iEAXmytUv0j/YXtPxhOi8D8Teh+mOXqmz+DZRtDil5tFx7ltbgw16dUptEw4jOg==", + "dev": true + }, "css-what": { "version": "6.1.0", "resolved": "https://registry.npmjs.org/css-what/-/css-what-6.1.0.tgz", diff --git a/package.json b/package.json index e18a8a9b7..7e4894785 100644 --- a/package.json +++ b/package.json @@ -84,6 +84,7 @@ "cross-env": "^7.0.2", "css-loader": "^5.0.1", "css-minimizer-webpack-plugin": "^4.2.2", + "css-unicode-loader": "^1.0.3", "cssnano": "^5.0.2", "dot-object": "^2.1.4", "dotenv": "^8.2.0", @@ -144,4 +145,4 @@ "webpackbar": "^5.0.2" }, "version": "3.2.10" -} \ No newline at end of file +} diff --git a/webpack.config.js b/webpack.config.js index feca91fe7..40f9f00c0 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -118,6 +118,7 @@ const cldCore = { loader: MiniCssExtractPlugin.loader, }, 'css-loader', + 'css-unicode-loader', 'sass-loader', ], }, From e6dabea98b46279c7d9052de3208f33bbb2e1d6b Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Fri, 11 Jul 2025 17:00:31 +0300 Subject: [PATCH 3/3] Format new code --- php/class-assets.php | 49 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 6 deletions(-) diff --git a/php/class-assets.php b/php/class-assets.php index f663abb0c..52287e99f 100644 --- a/php/class-assets.php +++ b/php/class-assets.php @@ -156,7 +156,7 @@ protected function register_hooks() { add_filter( 'cloudinary_asset_state', array( $this, 'filter_asset_state' ), 10, 2 ); add_filter( 'cloudinary_set_usable_asset', array( $this, 'check_usable_asset' ) ); // Actions. - add_action( 'cloudinary_init_settings', array( $this, 'setup' ) ); + add_action( 'cloudinary_ready', array( $this, 'setup' ) ); add_action( 'cloudinary_thread_queue_details_query', array( $this, 'connect_post_type' ) ); add_action( 'cloudinary_build_queue_query', array( $this, 'connect_post_type' ) ); add_action( 'cloudinary_string_replace', array( $this, 'add_url_replacements' ), 20 ); @@ -440,8 +440,8 @@ public function update_asset_paths() { // Check and update version if needed. if ( $this->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) { - $this->purge_parent( $asset_path->ID ); $this->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version ); + $this->sync_parent( $asset_path->ID ); } } } @@ -539,11 +539,12 @@ public function create_asset_parent( $path, $version ) { } /** - * Purge a single asset parent. + * Process all child assets of a parent with a given callback. * - * @param int $parent_id The Asset parent to purge. + * @param int $parent_id The Asset parent to process. + * @param callable $callback The callback function to execute on each post. */ - public function purge_parent( $parent_id ) { + private function process_parent_assets( $parent_id, $callback ) { $query_args = array( 'post_type' => self::POST_TYPE_SLUG, 'posts_per_page' => 100, @@ -555,11 +556,13 @@ public function purge_parent( $parent_id ) { ); $query = new \WP_Query( $query_args ); $previous_total = $query->found_posts; + do { $this->lock_assets(); $posts = $query->get_posts(); + foreach ( $posts as $post_id ) { - wp_delete_post( $post_id ); + call_user_func( $callback, $post_id ); } $query_args = $query->query_vars; @@ -570,6 +573,40 @@ public function purge_parent( $parent_id ) { } while ( $query->have_posts() ); } + /** + * Sync the assets of a parent. + * + * @param int $parent_id The Asset parent to sync. + */ + public function sync_parent( $parent_id ) { + $this->process_parent_assets( + $parent_id, + function ( $post_id ) { + if ( empty( $this->media->sync ) || ! $this->media->sync->can_sync( $post_id ) ) { + return; + } + + $this->media->sync->set_signature_item( $post_id, 'file', '' ); + $this->media->sync->set_signature_item( $post_id, 'cld_asset' ); + $this->media->sync->add_to_sync( $post_id ); + } + ); + } + + /** + * Purge a single asset parent. + * + * @param int $parent_id The Asset parent to purge. + */ + public function purge_parent( $parent_id ) { + $this->process_parent_assets( + $parent_id, + function ( $post_id ) { + wp_delete_post( $post_id ); + } + ); + } + /** * Lock asset creation for performing things like purging that require no changes. */