|
10 | 10 | use Cloudinary\Assets\Rest_Assets; |
11 | 11 | use Cloudinary\Connect\Api; |
12 | 12 | use Cloudinary\Sync; |
| 13 | +use Cloudinary\Cron; |
13 | 14 | use Cloudinary\Traits\Params_Trait; |
14 | 15 | use Cloudinary\Utils; |
15 | 16 | use WP_Error; |
@@ -388,27 +389,75 @@ public function connect_post_type( $query ) { |
388 | 389 | } |
389 | 390 |
|
390 | 391 | /** |
391 | | - * Register an asset path. |
| 392 | + * Update asset paths. |
392 | 393 | * |
393 | | - * @param string $path The path/URL to register. |
394 | | - * @param string $version The version. |
| 394 | + * @return void |
395 | 395 | */ |
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 | + } |
404 | 425 | } |
405 | | - $asset_path = get_post( $asset_parent_id ); |
406 | 426 | } |
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 | + } |
410 | 450 | } |
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 ); |
412 | 461 | } |
413 | 462 | } |
414 | 463 |
|
@@ -1155,30 +1204,8 @@ protected function register_post_type() { |
1155 | 1204 | * @hook cloudinary_init_settings |
1156 | 1205 | */ |
1157 | 1206 | 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 ); |
1182 | 1209 | } |
1183 | 1210 |
|
1184 | 1211 | /** |
|
0 commit comments