Skip to content
Merged
Show file tree
Hide file tree
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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"grunt-contrib-copy": "1.0.0",
"grunt-shell": "3.0.1",
"grunt-wp-deploy": "2.0.0",
"lodash": "4.17.11",
"lodash": "4.17.19",
"mini-css-extract-plugin": "0.7.0",
"moment": "2.24.0",
"npm-run-all": "4.1.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ class Upgrade {
*/
private $sync;

/**
* The cron frequency to ensure that the queue is progressing.
*
* @var int
*/
protected $cron_frequency;

/**
* The cron offset since the last update.
*
* @var int
*/
protected $cron_start_offset;

/**
* Filter constructor.
*
Expand All @@ -42,6 +56,10 @@ class Upgrade {
public function __construct( \Cloudinary\Media $media ) {
$this->media = $media;
$this->sync = $media->plugin->components['sync'];

$this->cron_frequency = apply_filters( 'cloudinary_cron_frequency', 600 );
$this->cron_start_offset = apply_filters( 'cloudinary_cron_start_offset', 60 );

$this->setup_hooks();
}

Expand Down Expand Up @@ -167,13 +185,44 @@ function ( $val ) use ( $media ) {
update_post_meta( $attachment_id, Sync::META_KEYS['downloading'], true );
delete_post_meta( $attachment_id, Sync::META_KEYS['syncing'] );

if ( ! wp_next_scheduled( 'cloudinary_resume_upgrade' ) ) {
wp_schedule_single_event( time() + $this->cron_frequency, 'cloudinary_resume_upgrade' );
}

if ( ! defined( 'DOING_BULK_SYNC' ) ) {
$this->sync->managers['upload']->add_to_sync( $attachment_id ); // Auto sync if upgrading outside of bulk sync.
}

return $public_id;
}

/**
* Maybe resume the upgrading assets.
* This is a fallback mechanism to resume the upgrade when it stops unexpectedly.
*
* @return void
*/
public function maybe_resume_upgrade() {
global $wpdb;

$assets = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery
$wpdb->prepare(
"SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s",
Sync::META_KEYS['downloading']
)
);

if ( ! empty( $assets ) ) {
wp_schedule_single_event( time() + $this->cron_frequency, 'cloudinary_resume_upgrade' );

foreach ( $assets as $asset ) {
$this->sync->managers['upload']->add_to_sync( $asset );
}
}
}

/**
* Setup hooks for the filters.
*/
Expand All @@ -193,5 +242,7 @@ public function setup_hooks() {
}
} );
}

add_action( 'cloudinary_resume_upgrade', array( $this, 'maybe_resume_upgrade' ) );
}
}