diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/css/loading.svg b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/css/loading.svg new file mode 100644 index 000000000..0cf4e763f --- /dev/null +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/css/loading.svg @@ -0,0 +1,21 @@ + + + + + + + + + + + + diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/js/src/components/sync.js b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/js/src/components/sync.js index 941580985..e08df5416 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/js/src/components/sync.js +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/js/src/components/sync.js @@ -4,11 +4,10 @@ const Sync = { progress: document.getElementById( 'progress-wrapper' ), submitButton: document.getElementById( 'submit' ), stopButton: document.getElementById( 'stop-sync' ), - progressCount: document.getElementById( 'sync-progress' ), - barSyncCount: document.getElementById( 'sync-total' ), completed: document.getElementById( 'completed-notice' ), show: 'inline-block', hide: 'none', + isRunning: false, getStatus: function getStatus() { var self = this, resourceType = [], @@ -22,8 +21,9 @@ const Sync = { request.setRequestHeader( 'X-WP-Nonce', cloudinaryApi.nonce ); }, } ).done( function( data ) { - if ( data.done < data.total ) { - setTimeout( Sync.getStatus, 2000 ); + Sync.isRunning = data.is_running; + if ( Sync.isRunning ) { + setTimeout( Sync.getStatus, 10000 ); } Sync._updateUI( data ); } ); @@ -32,6 +32,8 @@ const Sync = { var self = this, url = cloudinaryApi.restUrl + 'cloudinary/v1/sync'; + Sync.isRunning = false; + wp.ajax.send( { url: url, data: { @@ -48,11 +50,16 @@ const Sync = { var self = this, url = cloudinaryApi.restUrl + 'cloudinary/v1/sync'; + Sync.isRunning = true; + Sync.progress.style.display = Sync.show; + wp.ajax.send( { url: url, beforeSend: function( request ) { request.setRequestHeader( 'X-WP-Nonce', cloudinaryApi.nonce ); }, + } ).done( function ( data ) { + setTimeout( Sync.getStatus, 10000 ); } ); }, _updateUI: function _updateUI( data ) { @@ -75,13 +82,14 @@ const Sync = { this.stopButton.style.display = this.hide; } - if ( data.percent < 100 ) { - this.barSyncCount.innerText = data.total; - this.progressCount.innerText = data.done; + if ( data.percent === 100 ) { + this.completed.style.display = this.show; + } + + if ( this.isRunning ) { this.progress.style.display = this.show; } else { - this.completed.style.display = this.show; this.progress.style.display = this.hide; } }, diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-queue.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-queue.php index 6d0b77683..e76dbedae 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-queue.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/php/sync/class-upload-queue.php @@ -37,7 +37,7 @@ class Upload_Queue { * * @var bool */ - private $running = null; + private $running = false; /** * Upload_Queue constructor. @@ -127,7 +127,7 @@ public function mark( $id, $type = 'done' ) { * @return bool */ public function is_running() { - if ( null === $this->running ) { + if ( false === $this->running ) { $queue = $this->get_queue(); $this->running = empty( $queue['started'] ) ? false : true; } @@ -175,6 +175,8 @@ public function get_queue_status() { $this->stop_queue(); } + $return['is_running'] = $this->is_running(); + return $return; } diff --git a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/ui-definitions/tabs/sync-media-footer.php b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/ui-definitions/tabs/sync-media-footer.php index 509472490..63a2da3c2 100644 --- a/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/ui-definitions/tabs/sync-media-footer.php +++ b/cloudinary-image-management-and-manipulation-in-the-cloud-cdn/ui-definitions/tabs/sync-media-footer.php @@ -26,7 +26,10 @@ - / + + <?php esc_attr_e( 'Syncing…', 'cloudinary' ); ?> + +