diff --git a/admin/AmazonAI-Common.php b/admin/AmazonAI-Common.php index 2d2800c2..ffcbc349 100644 --- a/admin/AmazonAI-Common.php +++ b/admin/AmazonAI-Common.php @@ -582,8 +582,6 @@ public function create_tim_limitless_installkey(){ $responseData=json_decode($response, TRUE); update_option( TIM_LIMITLESS_INSTALLKEY, $responseData["installkey"]); update_option(TIM_LIMITLESS_VIEWKEY,$responseData["viewkey"]); - $polly_service = new AmazonAI_PollyService(); - $polly_service->tim_limitless_ajax_bulk_synthesize(false); } else{ $this->show_error_notice("notice-error", "Can't connect to tim limitless! Please contact Tim Support support@thetimmedia.com"); diff --git a/admin/AmazonAI-PollyService.php b/admin/AmazonAI-PollyService.php index ad5db35a..db285cda 100644 --- a/admin/AmazonAI-PollyService.php +++ b/admin/AmazonAI-PollyService.php @@ -100,42 +100,46 @@ private function update_tim_limitless_post_hash($text,$post_meta_field,$installk update_post_meta( $post_id, $post_meta_field, $responseData['postHash']); } } - public function tim_limitless_ajax_bulk_synthesize_enable_polly(){ - header('Content-type: application/json'); - if($this->tim_limitless_ajax_bulk_synthesize(true)){ - $result="success"; - }else{ - $result="failure"; - } - echo wp_json_encode( - array( - "status"=>$result - ) - ); - wp_die(); - } - public function tim_limitless_ajax_bulk_synthesize($enable_polly){ + public function tim_limitless_ajax_bulk_update(){ + header('Content-type: application/json'); $common = new AmazonAI_Common(); $common->init(); $post_ids = get_posts(array( 'fields' => 'ids', // Only get post IDs 'posts_per_page' => -1 )); - $isError = false; - foreach ($post_ids as $id){ - try{ - $this->save_post_tim_limitless($common,$id); - if($enable_polly){ - update_post_meta( $id, 'amazon_polly_enable', 1); - } - } - catch (Exception $e){ - error_log("bulk update error for post_id:".$id." error:".$e->getMessage(), 0); - $isError=true; - } + $index = $_POST['index']; + $num_posts = sizeof($post_ids); + if($num_posts-1<$index){ + echo wp_json_encode( + array( + "status"=>"done", + "index"=>$index, + "num_posts"=>$num_posts + ) + ); + wp_die(); + return; } - return !$isError; + try{ + $this->save_post_tim_limitless($common,$post_ids[index]->id); + update_post_meta( $post_ids[index]->id, 'amazon_polly_enable', 1); + + } + catch (Exception $e){ + error_log("bulk update error for post_id:".$post_ids[index]->id." error:".$e->getMessage(), 0); + } + + echo wp_json_encode( + array( + "status"=>"not_done", + "index"=>$index, + "num_posts"=>$num_posts + ) + ); + wp_die(); + } /** diff --git a/admin/css/amazonpolly-admin.css b/admin/css/amazonpolly-admin.css index 6b59ee2f..e070a225 100755 --- a/admin/css/amazonpolly-admin.css +++ b/admin/css/amazonpolly-admin.css @@ -21,3 +21,7 @@ font-size: .8em; color: blue; } + +#successLabel{ + margin-left: 72px; +} diff --git a/admin/js/tim_limitless-admin.js b/admin/js/tim_limitless-admin.js index 20fa3fe7..d9ad3fb3 100644 --- a/admin/js/tim_limitless-admin.js +++ b/admin/js/tim_limitless-admin.js @@ -35,13 +35,14 @@ timDiv.insertBefore(div, timDiv.childNodes[0]); } - function updateAllPosts() { + function updateAllPosts(index) { $.ajax({ type: 'POST', url: ajaxurl, data: { action: 'tim_limitless_bulk_update', + index:index }, dataType: "json", beforeSend: function() { @@ -51,12 +52,14 @@ complete: function() { }, success: function( response ) { - showLoader(false); var label = document.getElementById("successLabel"); - if(response.status=="success"){ - label.innerText="All posts were updated successfully."; + if(response.status=="not_done"){ + response.index++; + label.innerText = response.index + " of " +response.num_posts +" posts were updated."; + updateAllPosts(response.index); }else{ - label.innerText="Update finished. Some posts failed to update."; + showLoader(false); + label.innerText="All posts were updated"; } } }).fail(function (response) { @@ -75,7 +78,7 @@ $( '#tim_limitless_update_all' ).click( function(){ addLoaderToPage(); - updateAllPosts(); + updateAllPosts(0); } ); diff --git a/includes/class-amazonpolly.php b/includes/class-amazonpolly.php index 79d6de40..c947f8d7 100755 --- a/includes/class-amazonpolly.php +++ b/includes/class-amazonpolly.php @@ -200,7 +200,7 @@ private function define_admin_hooks() { $this->loader->add_action( 'before_delete_post', $common, 'delete_post' ); $this->loader->add_action( 'wp_ajax_polly_transcribe', $polly_service, 'ajax_bulk_synthesize' ); if($common->is_trinity_connected()){ - $this->loader->add_action( 'wp_ajax_tim_limitless_bulk_update', $polly_service, 'tim_limitless_ajax_bulk_synthesize_enable_polly' ); + $this->loader->add_action( 'wp_ajax_tim_limitless_bulk_update', $polly_service, 'tim_limitless_ajax_bulk_update' ); }