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
17 changes: 15 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -144,4 +145,4 @@
"webpackbar": "^5.0.2"
},
"version": "3.2.11"
}
}
48 changes: 43 additions & 5 deletions php/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
}
}
}
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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.
*/
Expand Down
11 changes: 10 additions & 1 deletion php/class-media.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Cloudinary;

use Cloudinary\Assets;
use Cloudinary\Component\Setup;
use Cloudinary\Connect\Api;
use Cloudinary\Media\Filter;
Expand Down Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ const cldCore = {
loader: MiniCssExtractPlugin.loader,
},
'css-loader',
'css-unicode-loader',
'sass-loader',
],
},
Expand Down