Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fix/issues 2952 and 2951 #2953

Merged
merged 2 commits into from
Aug 17, 2022
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
2 changes: 1 addition & 1 deletion assets/js/sync/components/common/progress-bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div
Expand Down
14 changes: 14 additions & 0 deletions includes/classes/Indexable/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] )
Expand All @@ -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;
}

Expand Down
14 changes: 14 additions & 0 deletions tests/php/indexables/TestPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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'] );
}

/**
Expand Down