diff --git a/package-lock.json b/package-lock.json index 890d22f6..ba640f0b 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 2b4d07a5..972a0ec8 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.11" -} \ No newline at end of file +} diff --git a/php/class-assets.php b/php/class-assets.php index 601ba06b..52287e99 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 ); @@ -441,6 +441,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->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version ); + $this->sync_parent( $asset_path->ID ); } } } @@ -538,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, @@ -554,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; @@ -569,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. */ diff --git a/php/class-media.php b/php/class-media.php index aaf1cb5a..182782b3 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. * diff --git a/webpack.config.js b/webpack.config.js index feca91fe..40f9f00c 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', ], },