From 5ca1ab79e8da89cf635e104df307eb3ba944f298 Mon Sep 17 00:00:00 2001 From: Felipe Elia Date: Wed, 17 Aug 2022 10:26:22 -0300 Subject: [PATCH 1/2] If total is 0, then it is complete --- assets/js/sync/components/common/progress-bar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/sync/components/common/progress-bar.js b/assets/js/sync/components/common/progress-bar.js index 8bd4ce7d2c..59daabd5d0 100644 --- a/assets/js/sync/components/common/progress-bar.js +++ b/assets/js/sync/components/common/progress-bar.js @@ -13,7 +13,7 @@ import { WPElement } from '@wordpress/element'; * @returns {WPElement} Component. */ export default ({ isComplete, current, total }) => { - const now = Math.floor((current / total) * 100); + const now = total ? Math.floor((current / total) * 100) : 100; return (
Date: Wed, 17 Aug 2022 10:56:21 -0300 Subject: [PATCH 2/2] Subtract pw posts from the total count if needed --- includes/classes/Indexable/Post/Post.php | 14 ++++++++++++++ tests/php/indexables/TestPost.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/includes/classes/Indexable/Post/Post.php b/includes/classes/Indexable/Post/Post.php index cfa790127e..069dddb06c 100644 --- a/includes/classes/Indexable/Post/Post.php +++ b/includes/classes/Indexable/Post/Post.php @@ -219,6 +219,8 @@ protected function get_total_objects_for_query( $query_args ) { * @return int The total posts. */ protected function get_total_objects_for_query_from_db( $query_args ) { + global $wpdb; + $post_count = 0; if ( ! isset( $query_args['post_type'] ) || isset( $query_args['ep_indexing_upper_limit_object_id'] ) @@ -236,6 +238,18 @@ protected function get_total_objects_for_query_from_db( $query_args ) { } } + /** + * As `wp_count_posts` will also count posts with password, we need to remove + * them from the final count if they will not be used. + * + * The if below will pass if `has_password` is false but not null. + */ + if ( isset( $query_args['has_password'] ) && ! $query_args['has_password'] ) { + $posts_with_password = (int) $wpdb->get_var( "SELECT COUNT(1) AS posts_with_password FROM {$wpdb->posts} WHERE post_password != ''" ); + + $post_count -= $posts_with_password; + } + return $post_count; } diff --git a/tests/php/indexables/TestPost.php b/tests/php/indexables/TestPost.php index f834b1903a..468083be2b 100644 --- a/tests/php/indexables/TestPost.php +++ b/tests/php/indexables/TestPost.php @@ -5453,6 +5453,7 @@ public function testQueryDb() { $post_id_1 = Functions\create_and_sync_post(); $post_id_2 = Functions\create_and_sync_post(); $post_id_3 = Functions\create_and_sync_post(); + $post_id_4 = Functions\create_and_sync_post( [ 'post_password' => '123' ] ); // Test the first loop of the indexing. $results = $indexable_post_object->query_db( @@ -5548,6 +5549,19 @@ public function testQueryDb() { $this->assertCount( 3, $results['objects'] ); $this->assertEquals( 3, $results['total_objects'] ); + + // Test the first loop of the indexing. + $results = $indexable_post_object->query_db( + [ + 'per_page' => 1, + 'has_password' => null, // `null` here makes WP ignore passwords completely, bringing everything + ] + ); + + $post_ids = wp_list_pluck( $results['objects'], 'ID' ); + $this->assertEquals( $post_id_4, $post_ids[0] ); + $this->assertCount( 1, $results['objects'] ); + $this->assertEquals( 4, $results['total_objects'] ); } /**