From 8eb3d4d9d4cdf52ab1c7aacd38dee934970eeb0f Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Fri, 4 Apr 2025 16:37:39 +0300 Subject: [PATCH 1/2] Split the activate and upate asset parents logic --- php/class-assets.php | 107 +++++++++++++++++++++++++++---------------- 1 file changed, 67 insertions(+), 40 deletions(-) diff --git a/php/class-assets.php b/php/class-assets.php index 4c366b2b0..43a778a1d 100644 --- a/php/class-assets.php +++ b/php/class-assets.php @@ -10,6 +10,7 @@ use Cloudinary\Assets\Rest_Assets; use Cloudinary\Connect\Api; use Cloudinary\Sync; +use Cloudinary\Cron; use Cloudinary\Traits\Params_Trait; use Cloudinary\Utils; use WP_Error; @@ -388,27 +389,75 @@ public function connect_post_type( $query ) { } /** - * Register an asset path. + * Update asset paths. * - * @param string $path The path/URL to register. - * @param string $version The version. + * @return void */ - public static function register_asset_path( $path, $version ) { - $assets = self::$instance; - if ( $assets && ! $assets->is_locked() ) { - $asset_path = $assets->get_asset_parent( $path ); - if ( null === $asset_path ) { - $asset_parent_id = $assets->create_asset_parent( $path, $version ); - if ( is_wp_error( $asset_parent_id ) ) { - return; // Bail. + public function update_asset_paths() { + $assets = $this->settings->get_setting( 'assets' )->get_settings(); + + if ( empty( $assets ) || ! $this->is_locked() ) { + return; + } + + foreach ( $assets as $asset ) { + $paths = $asset->get_setting( 'paths' ); + foreach ( $paths->get_settings() as $path ) { + if ( 'on' === $path->get_value() ) { + $conf = $path->get_params(); + $path = urldecode( trailingslashit( $conf['url'] ) ); + $version = $conf['version']; + + $asset_path = $this->get_asset_parent( $path ); + + if ( null === $asset_path ) { + $asset_parent_id = $assets->create_asset_parent( $path, $version ); + if ( is_wp_error( $asset_parent_id ) ) { + return; // Bail. + } + $asset_path = get_post( $asset_parent_id ); + } + + // 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 ); + } } - $asset_path = get_post( $asset_parent_id ); } - // Check and update version if needed. - if ( $assets->media->get_post_meta( $asset_path->ID, Sync::META_KEYS['version'], true ) !== $version ) { - $assets->media->update_post_meta( $asset_path->ID, Sync::META_KEYS['version'], $version ); + } + } + + /** + * Activate parent assets based on the current settings and purges unused parent assets. + * + * @return void + */ + protected function activate_parents() { + $assets = $this->settings->get_setting( 'assets' )->get_settings(); + + if ( empty( $assets ) || $this->is_locked() ) { + return; + } + + foreach ( $assets as $asset ) { + $paths = $asset->get_setting( 'paths' ); + + foreach ( $paths->get_settings() as $path ) { + if ( 'on' === $path->get_value() ) { + $conf = $path->get_params(); + self::activate_parent( urldecode( trailingslashit( $conf['url'] ) ) ); + } } - $assets->activate_parent( $path ); + } + + // Get the disabled items. + foreach ( $this->asset_parents as $url => $parent ) { + if ( isset( $this->active_parents[ $url ] ) ) { + continue; + } + $this->purge_parent( $parent->ID ); + // Remove parent. + wp_delete_post( $parent->ID ); } } @@ -1155,30 +1204,8 @@ protected function register_post_type() { * @hook cloudinary_init_settings */ public function setup() { - - $assets = $this->settings->get_setting( 'assets' )->get_settings(); - $full = 'on' === $this->settings->get_value( 'cache.enable' ); - foreach ( $assets as $asset ) { - - $paths = $asset->get_setting( 'paths' ); - - foreach ( $paths->get_settings() as $path ) { - if ( 'on' === $path->get_value() ) { - $conf = $path->get_params(); - self::register_asset_path( urldecode( trailingslashit( $conf['url'] ) ), $conf['version'] ); - } - } - } - - // Get the disabled items. - foreach ( $this->asset_parents as $url => $parent ) { - if ( isset( $this->active_parents[ $url ] ) ) { - continue; - } - $this->purge_parent( $parent->ID ); - // Remove parent. - wp_delete_post( $parent->ID ); - } + $this->activate_parents(); + Cron::register_process( 'update_asset_paths', array( $this, 'update_asset_paths' ), 60 ); } /** From 63bbf8d43c4d3bd6e21438440dff4e35475d5a5e Mon Sep 17 00:00:00 2001 From: Aleksandar Atanasov Date: Tue, 8 Apr 2025 11:05:08 +0300 Subject: [PATCH 2/2] Enable the update_asset_paths in the admin area --- php/class-assets.php | 36 ++++++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/php/class-assets.php b/php/class-assets.php index 43a778a1d..601ba06be 100644 --- a/php/class-assets.php +++ b/php/class-assets.php @@ -388,15 +388,33 @@ public function connect_post_type( $query ) { return $query; } + /** + * Retrieve the assets settings. + * + * This method fetches the assets settings from the configuration. + * If the assets settings are empty or the settings are locked, it returns an empty array. + * + * @return array The assets settings array. + */ + public function get_assets_settings() { + $assets = $this->settings->get_setting( 'assets' )->get_settings(); + + if ( empty( $assets ) || $this->is_locked() ) { + return array(); + } + + return $assets; + } + /** * Update asset paths. * * @return void */ public function update_asset_paths() { - $assets = $this->settings->get_setting( 'assets' )->get_settings(); + $assets = $this->get_assets_settings(); - if ( empty( $assets ) || ! $this->is_locked() ) { + if ( empty( $assets ) ) { return; } @@ -411,10 +429,12 @@ public function update_asset_paths() { $asset_path = $this->get_asset_parent( $path ); if ( null === $asset_path ) { - $asset_parent_id = $assets->create_asset_parent( $path, $version ); + $asset_parent_id = $this->create_asset_parent( $path, $version ); + if ( is_wp_error( $asset_parent_id ) ) { return; // Bail. } + $asset_path = get_post( $asset_parent_id ); } @@ -433,9 +453,9 @@ public function update_asset_paths() { * @return void */ protected function activate_parents() { - $assets = $this->settings->get_setting( 'assets' )->get_settings(); + $assets = $this->get_assets_settings(); - if ( empty( $assets ) || $this->is_locked() ) { + if ( empty( $assets ) ) { return; } @@ -1204,8 +1224,12 @@ protected function register_post_type() { * @hook cloudinary_init_settings */ public function setup() { + if ( is_user_logged_in() && is_admin() && ! Utils::is_rest_api() ) { + $this->update_asset_paths(); + } + + Cron::register_process( 'update_asset_paths', array( $this, 'update_asset_paths' ), DAY_IN_SECONDS ); $this->activate_parents(); - Cron::register_process( 'update_asset_paths', array( $this, 'update_asset_paths' ), 60 ); } /**