Skip to content

Commit 8eb3d4d

Browse files
committed
Split the activate and upate asset parents logic
1 parent 4d36b34 commit 8eb3d4d

1 file changed

Lines changed: 67 additions & 40 deletions

File tree

php/class-assets.php

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Cloudinary\Assets\Rest_Assets;
1111
use Cloudinary\Connect\Api;
1212
use Cloudinary\Sync;
13+
use Cloudinary\Cron;
1314
use Cloudinary\Traits\Params_Trait;
1415
use Cloudinary\Utils;
1516
use WP_Error;
@@ -388,27 +389,75 @@ public function connect_post_type( $query ) {
388389
}
389390

390391
/**
391-
* Register an asset path.
392+
* Update asset paths.
392393
*
393-
* @param string $path The path/URL to register.
394-
* @param string $version The version.
394+
* @return void
395395
*/
396-
public static function register_asset_path( $path, $version ) {
397-
$assets = self::$instance;
398-
if ( $assets && ! $assets->is_locked() ) {
399-
$asset_path = $assets->get_asset_parent( $path );
400-
if ( null === $asset_path ) {
401-
$asset_parent_id = $assets->create_asset_parent( $path, $version );
402-
if ( is_wp_error( $asset_parent_id ) ) {
403-
return; // Bail.
396+
public function update_asset_paths() {
397+
$assets = $this->settings->get_setting( 'assets' )->get_settings();
398+
399+
if ( empty( $assets ) || ! $this->is_locked() ) {
400+
return;
401+
}
402+
403+
foreach ( $assets as $asset ) {
404+
$paths = $asset->get_setting( 'paths' );
405+
foreach ( $paths->get_settings() as $path ) {
406+
if ( 'on' === $path->get_value() ) {
407+
$conf = $path->get_params();
408+
$path = urldecode( trailingslashit( $conf['url'] ) );
409+
$version = $conf['version'];
410+
411+
$asset_path = $this->get_asset_parent( $path );
412+
413+
if ( null === $asset_path ) {
414+
$asset_parent_id = $assets->create_asset_parent( $path, $version );
415+
if ( is_wp_error( $asset_parent_id ) ) {
416+
return; // Bail.
417+
}
418+
$asset_path = get_post( $asset_parent_id );
419+
}
420+
421+
// Check and update version if needed.
422+
if ( $this->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) {
423+
$this->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version );
424+
}
404425
}
405-
$asset_path = get_post( $asset_parent_id );
406426
}
407-
// Check and update version if needed.
408-
if ( $assets->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) {
409-
$assets->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version );
427+
}
428+
}
429+
430+
/**
431+
* Activate parent assets based on the current settings and purges unused parent assets.
432+
*
433+
* @return void
434+
*/
435+
protected function activate_parents() {
436+
$assets = $this->settings->get_setting( 'assets' )->get_settings();
437+
438+
if ( empty( $assets ) || $this->is_locked() ) {
439+
return;
440+
}
441+
442+
foreach ( $assets as $asset ) {
443+
$paths = $asset->get_setting( 'paths' );
444+
445+
foreach ( $paths->get_settings() as $path ) {
446+
if ( 'on' === $path->get_value() ) {
447+
$conf = $path->get_params();
448+
self::activate_parent( urldecode( trailingslashit( $conf['url'] ) ) );
449+
}
410450
}
411-
$assets->activate_parent( $path );
451+
}
452+
453+
// Get the disabled items.
454+
foreach ( $this->asset_parents as $url => $parent ) {
455+
if ( isset( $this->active_parents[ $url ] ) ) {
456+
continue;
457+
}
458+
$this->purge_parent( $parent->ID );
459+
// Remove parent.
460+
wp_delete_post( $parent->ID );
412461
}
413462
}
414463

@@ -1155,30 +1204,8 @@ protected function register_post_type() {
11551204
* @hook cloudinary_init_settings
11561205
*/
11571206
public function setup() {
1158-
1159-
$assets = $this->settings->get_setting( 'assets' )->get_settings();
1160-
$full = 'on' === $this->settings->get_value( 'cache.enable' );
1161-
foreach ( $assets as $asset ) {
1162-
1163-
$paths = $asset->get_setting( 'paths' );
1164-
1165-
foreach ( $paths->get_settings() as $path ) {
1166-
if ( 'on' === $path->get_value() ) {
1167-
$conf = $path->get_params();
1168-
self::register_asset_path( urldecode( trailingslashit( $conf['url'] ) ), $conf['version'] );
1169-
}
1170-
}
1171-
}
1172-
1173-
// Get the disabled items.
1174-
foreach ( $this->asset_parents as $url => $parent ) {
1175-
if ( isset( $this->active_parents[ $url ] ) ) {
1176-
continue;
1177-
}
1178-
$this->purge_parent( $parent->ID );
1179-
// Remove parent.
1180-
wp_delete_post( $parent->ID );
1181-
}
1207+
$this->activate_parents();
1208+
Cron::register_process( 'update_asset_paths', array( $this, 'update_asset_paths' ), 60 );
11821209
}
11831210

11841211
/**

0 commit comments

Comments
 (0)