|
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,95 @@ public function connect_post_type( $query ) { |
388 | 389 | } |
389 | 390 |
|
390 | 391 | /** |
391 | | - * Register an asset path. |
| 392 | + * Retrieve the assets settings. |
392 | 393 | * |
393 | | - * @param string $path The path/URL to register. |
394 | | - * @param string $version The version. |
| 394 | + * This method fetches the assets settings from the configuration. |
| 395 | + * If the assets settings are empty or the settings are locked, it returns an empty array. |
| 396 | + * |
| 397 | + * @return array The assets settings array. |
| 398 | + */ |
| 399 | + public function get_assets_settings() { |
| 400 | + $assets = $this->settings->get_setting( 'assets' )->get_settings(); |
| 401 | + |
| 402 | + if ( empty( $assets ) || $this->is_locked() ) { |
| 403 | + return array(); |
| 404 | + } |
| 405 | + |
| 406 | + return $assets; |
| 407 | + } |
| 408 | + |
| 409 | + /** |
| 410 | + * Update asset paths. |
| 411 | + * |
| 412 | + * @return void |
| 413 | + */ |
| 414 | + public function update_asset_paths() { |
| 415 | + $assets = $this->get_assets_settings(); |
| 416 | + |
| 417 | + if ( empty( $assets ) ) { |
| 418 | + return; |
| 419 | + } |
| 420 | + |
| 421 | + foreach ( $assets as $asset ) { |
| 422 | + $paths = $asset->get_setting( 'paths' ); |
| 423 | + foreach ( $paths->get_settings() as $path ) { |
| 424 | + if ( 'on' === $path->get_value() ) { |
| 425 | + $conf = $path->get_params(); |
| 426 | + $path = urldecode( trailingslashit( $conf['url'] ) ); |
| 427 | + $version = $conf['version']; |
| 428 | + |
| 429 | + $asset_path = $this->get_asset_parent( $path ); |
| 430 | + |
| 431 | + if ( null === $asset_path ) { |
| 432 | + $asset_parent_id = $this->create_asset_parent( $path, $version ); |
| 433 | + |
| 434 | + if ( is_wp_error( $asset_parent_id ) ) { |
| 435 | + return; // Bail. |
| 436 | + } |
| 437 | + |
| 438 | + $asset_path = get_post( $asset_parent_id ); |
| 439 | + } |
| 440 | + |
| 441 | + // Check and update version if needed. |
| 442 | + if ( $this->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) { |
| 443 | + $this->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version ); |
| 444 | + } |
| 445 | + } |
| 446 | + } |
| 447 | + } |
| 448 | + } |
| 449 | + |
| 450 | + /** |
| 451 | + * Activate parent assets based on the current settings and purges unused parent assets. |
| 452 | + * |
| 453 | + * @return void |
395 | 454 | */ |
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. |
| 455 | + protected function activate_parents() { |
| 456 | + $assets = $this->get_assets_settings(); |
| 457 | + |
| 458 | + if ( empty( $assets ) ) { |
| 459 | + return; |
| 460 | + } |
| 461 | + |
| 462 | + foreach ( $assets as $asset ) { |
| 463 | + $paths = $asset->get_setting( 'paths' ); |
| 464 | + |
| 465 | + foreach ( $paths->get_settings() as $path ) { |
| 466 | + if ( 'on' === $path->get_value() ) { |
| 467 | + $conf = $path->get_params(); |
| 468 | + self::activate_parent( urldecode( trailingslashit( $conf['url'] ) ) ); |
404 | 469 | } |
405 | | - $asset_path = get_post( $asset_parent_id ); |
406 | 470 | } |
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 ); |
| 471 | + } |
| 472 | + |
| 473 | + // Get the disabled items. |
| 474 | + foreach ( $this->asset_parents as $url => $parent ) { |
| 475 | + if ( isset( $this->active_parents[ $url ] ) ) { |
| 476 | + continue; |
410 | 477 | } |
411 | | - $assets->activate_parent( $path ); |
| 478 | + $this->purge_parent( $parent->ID ); |
| 479 | + // Remove parent. |
| 480 | + wp_delete_post( $parent->ID ); |
412 | 481 | } |
413 | 482 | } |
414 | 483 |
|
@@ -1155,30 +1224,12 @@ protected function register_post_type() { |
1155 | 1224 | * @hook cloudinary_init_settings |
1156 | 1225 | */ |
1157 | 1226 | 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 | | - } |
| 1227 | + if ( is_user_logged_in() && is_admin() && ! Utils::is_rest_api() ) { |
| 1228 | + $this->update_asset_paths(); |
1171 | 1229 | } |
1172 | 1230 |
|
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 | | - } |
| 1231 | + Cron::register_process( 'update_asset_paths', array( $this, 'update_asset_paths' ), DAY_IN_SECONDS ); |
| 1232 | + $this->activate_parents(); |
1182 | 1233 | } |
1183 | 1234 |
|
1184 | 1235 | /** |
|
0 commit comments