Skip to content

Improve the update asset parent paths functionality for better performance #1027

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Apr 15, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 89 additions & 38 deletions php/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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();
}

/**
Expand Down