diff --git a/php/class-assets.php b/php/class-assets.php index 4c366b2b0..601ba06be 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,95 @@ public function connect_post_type( $query ) { } /** - * Register an asset path. + * Retrieve the assets settings. * - * @param string $path The path/URL to register. - * @param string $version The version. + * 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->get_assets_settings(); + + if ( empty( $assets ) ) { + 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 = $this->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 ); + } + } + } + } + } + + /** + * Activate parent assets based on the current settings and purges unused parent assets. + * + * @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. + protected function activate_parents() { + $assets = $this->get_assets_settings(); + + if ( empty( $assets ) ) { + 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'] ) ) ); } - $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 ); + } + + // Get the disabled items. + foreach ( $this->asset_parents as $url => $parent ) { + if ( isset( $this->active_parents[ $url ] ) ) { + continue; } - $assets->activate_parent( $path ); + $this->purge_parent( $parent->ID ); + // Remove parent. + wp_delete_post( $parent->ID ); } } @@ -1155,30 +1224,12 @@ 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'] ); - } - } + if ( is_user_logged_in() && is_admin() && ! Utils::is_rest_api() ) { + $this->update_asset_paths(); } - // 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 ); - } + Cron::register_process( 'update_asset_paths', array( $this, 'update_asset_paths' ), DAY_IN_SECONDS ); + $this->activate_parents(); } /**