Skip to content

Commit

Permalink
Merge pull request #33 from alleyinteractive/fix/issue-30/bulk-task-l…
Browse files Browse the repository at this point in the history
…oops-all-post-types

Remove Stepping and Max ID Limit
  • Loading branch information
kevinfodness authored Jul 22, 2024
2 parents 6772f7a + aa17153 commit 0cf4e67
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 45 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.4] Unpublished
## [0.3.0] Unpublished

### Added

- [#27](https://github.com/alleyinteractive/wp-bulk-task/issues/27): feat: Pass the current query object to the callback.
- Removed the `stepping` property and refactored the query to only use a minimum ID and a limit, which should improve performance in most cases.
- The current query object is now passed to the callback.

## [0.2.3] - 2024-04-08

Expand Down
51 changes: 11 additions & 40 deletions src/class-bulk-task.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,6 @@ class Bulk_Task {
*/
protected string $object_hash;

/**
* The slice of IDs in the database within which to look for matching posts.
* By keeping this number higher than the posts_per_page, but smaller than
* the total number of results in very large databases, it enables the right
* balance of performant lookups with moving through the database quickly when
* there are no matches in a particular slice of results.
*
* This property is public and can be overridden, and the logic for
* determining the max ID in the range will take the larger of this property
* and posts_per_page.
*
* @var int
*/
public int $stepping = 10000;

/**
* Constructor. Accepts a unique key, which is used to keep track of the
* cursor within the database.
Expand Down Expand Up @@ -178,11 +163,9 @@ public function filter__posts_where( $where, $query ): string {
global $wpdb;

return sprintf(
'AND %s.ID > %d AND %s.ID <= %d %s',
'AND %s.ID > %d %s',
$wpdb->posts,
$this->min_id,
$wpdb->posts,
$this->min_id + $this->stepping,
$where
);
}
Expand Down Expand Up @@ -210,9 +193,8 @@ public function filter__terms_where( $clauses ): array {

if ( ! empty( $this->query ) && spl_object_hash( $this->query ) === $this->object_hash ) {
$clauses['where'] .= sprintf(
' AND tt.term_taxonomy_id > %d AND tt.term_taxonomy_id <= %d',
$this->min_id,
$this->min_id + $this->stepping
' AND tt.term_taxonomy_id > %d',
$this->min_id
);
}

Expand All @@ -238,11 +220,9 @@ public function filter__users_where( $query ): void {
$user_table = $wpdb->users; // phpcs:ignore WordPressVIPMinimum.Variables.RestrictedVariables.user_meta__wpdb__users

$query->query_where .= sprintf(
' AND %s.ID > %d AND %s.ID <= %d',
$user_table,
$this->min_id,
' AND %s.ID > %d',
$user_table,
$this->min_id + $this->stepping
$this->min_id
);
}

Expand Down Expand Up @@ -407,9 +387,6 @@ public function run_wp_term_query( array $args, callable $callable ): void {
$args['orderby'] = 'term_id';
$args['update_term_meta_cache'] = false;

// Ensure stepping is the larger of the configured value and number.
$this->stepping = max( $this->stepping, $args['number'] );

// Set the min ID from the cursor.
$this->min_id = $this->cursor->get();

Expand Down Expand Up @@ -441,8 +418,8 @@ public function run_wp_term_query( array $args, callable $callable ): void {
// Update our min ID for the next query.
$this->min_id = end( $this->query->terms )->term_taxonomy_id;
} else {
// No results found in the block of terms, so skip ahead.
$this->min_id += $this->stepping;
// No results found in the block of terms, so skip to the end.
$this->min_id = $this->max_id;
}

// Actions to run after each batch of results.
Expand Down Expand Up @@ -502,9 +479,6 @@ public function run_wp_post_query( array $args, callable $callable ): void {
$args['paged'] = 1;
$args['suppress_filters'] = false;

// Ensure stepping is the larger of the configured value and posts_per_page.
$this->stepping = max( $this->stepping, $args['posts_per_page'] );

// Set the min ID from the cursor.
$this->min_id = $this->cursor->get();

Expand Down Expand Up @@ -540,8 +514,8 @@ public function run_wp_post_query( array $args, callable $callable ): void {
$last_post = end( $this->query->posts );
$this->min_id = $last_post instanceof WP_Post ? $last_post->ID : 0;
} else {
// No results found in the block of posts, so skip ahead.
$this->min_id += $this->stepping;
// No results found in the block of posts, so skip to the end.
$this->min_id = $this->max_id;
}

// Actions to run after each batch of results.
Expand Down Expand Up @@ -594,9 +568,6 @@ public function run_wp_user_query( array $args, callable $callable ): void {
$args['has_published_posts'] = false;
$args['count_total'] = false;

// Ensure stepping is the larger of the configured value and number.
$this->stepping = max( $this->stepping, $args['number'] );

// Set the min ID from the cursor.
$this->min_id = $this->cursor->get();

Expand Down Expand Up @@ -634,8 +605,8 @@ public function run_wp_user_query( array $args, callable $callable ): void {
// Update our min ID for the next query.
$this->min_id = end( $results )->ID;
} else {
// No results found in the block of users, so skip ahead.
$this->min_id += $this->stepping;
// No results found in the block of users, so skip to the end.
$this->min_id = $this->max_id;
}

// Actions to run after each batch of results.
Expand Down
2 changes: 1 addition & 1 deletion src/class-cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,6 @@ public function reset(): bool {
* @return bool True if the value was successfully set, false otherwise.
*/
public function set( int $value ): bool {
return update_option( $this->option_name, $value, 'no' );
return update_option( $this->option_name, $value, false );
}
}
4 changes: 2 additions & 2 deletions tests/class-test-cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function test_lifecycle(): void {
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
public function test_curso_option_not_autoload(): void {
public function test_cursor_option_not_autoload(): void {
global $wpdb;

$option_name = 'test-cursor';
Expand All @@ -50,7 +50,7 @@ public function test_curso_option_not_autoload(): void {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$option_autoloaded = $wpdb->get_var( $wpdb->prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option_name_key ) );
$this->assertNotEmpty( $option_autoloaded );
$this->assertSame( 'no', $option_autoloaded );
$this->assertSame( 'off', $option_autoloaded );

$this->assertEquals( 1234, $cursor->get() );
$cursor->reset();
Expand Down

0 comments on commit 0cf4e67

Please sign in to comment.