Skip to content

Commit e6dabea

Browse files
committed
Format new code
1 parent 0179784 commit e6dabea

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

php/class-assets.php

Lines changed: 43 additions & 6 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 );
@@ -440,8 +440,8 @@ public function update_asset_paths() {
440440

441441
// Check and update version if needed.
442442
if ( $this->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) {
443-
$this->purge_parent( $asset_path->ID );
444443
$this->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version );
444+
$this->sync_parent( $asset_path->ID );
445445
}
446446
}
447447
}
@@ -539,11 +539,12 @@ public function create_asset_parent( $path, $version ) {
539539
}
540540

541541
/**
542-
* Purge a single asset parent.
542+
* Process all child assets of a parent with a given callback.
543543
*
544-
* @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.
545546
*/
546-
public function purge_parent( $parent_id ) {
547+
private function process_parent_assets( $parent_id, $callback ) {
547548
$query_args = array(
548549
'post_type' => self::POST_TYPE_SLUG,
549550
'posts_per_page' => 100,
@@ -555,11 +556,13 @@ public function purge_parent( $parent_id ) {
555556
);
556557
$query = new \WP_Query( $query_args );
557558
$previous_total = $query->found_posts;
559+
558560
do {
559561
$this->lock_assets();
560562
$posts = $query->get_posts();
563+
561564
foreach ( $posts as $post_id ) {
562-
wp_delete_post( $post_id );
565+
call_user_func( $callback, $post_id );
563566
}
564567

565568
$query_args = $query->query_vars;
@@ -570,6 +573,40 @@ public function purge_parent( $parent_id ) {
570573
} while ( $query->have_posts() );
571574
}
572575

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+
573610
/**
574611
* Lock asset creation for performing things like purging that require no changes.
575612
*/

0 commit comments

Comments
 (0)