Skip to content

Commit 8bcd22b

Browse files
committed
Keep numeric IDs, cast them, and drop duplicates in delete_many
1 parent b184a28 commit 8bcd22b

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

‎src/Schema/Traits/Custom_Table_Query_Methods.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public static function delete( int $uid, string $column = '' ): bool {
177177
* Deletes multiple rows from the table.
178178
*
179179
* @since 3.0.0
180-
* @since TBD IDs are passed to the query as placeholders, and non-positive IDs are ignored for integer columns.
180+
* @since TBD IDs are passed to the query as placeholders, non-numeric IDs are ignored for integer columns, and duplicates are removed.
181181
*
182182
* @param array<int|string> $ids The IDs of the rows to delete.
183183
* @param string $column The column to use for the delete query.
@@ -194,9 +194,11 @@ public static function delete_many( array $ids, string $column = '', string $mor
194194
$column_object = $columns->get( $column_name );
195195
$is_int = $column_object && PHP_Types::INT === $column_object->get_php_type();
196196

197-
$column_ids = $is_int ?
198-
array_filter( array_map( fn( $id ) => filter_var( is_string( $id ) && ctype_digit( $id ) ? ltrim( $id, '0' ) : $id, FILTER_VALIDATE_INT, [ 'options' => [ 'min_range' => 1 ] ] ), $ids ) ) :
199-
array_filter( array_map( 'strval', $ids ), fn( $id ) => '' !== $id );
197+
$column_ids = array_unique(
198+
$is_int ?
199+
array_map( 'intval', array_filter( $ids, 'is_numeric' ) ) :
200+
array_filter( array_map( 'strval', $ids ), fn( $id ) => '' !== $id )
201+
);
200202

201203
if ( empty( $column_ids ) ) {
202204
return false;

‎tests/wpunit/Traits/Custom_Table_Query_MethodsTest.php‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,7 +1038,7 @@ public function should_delete_many_by_int_and_numeric_string_ids() {
10381038

10391039
$ids = $this->insert_delete_fixtures( $table, 4 );
10401040

1041-
$this->assertSame( 2, $table::delete_many( [ $ids[0], (string) $ids[1] ] ) );
1041+
$this->assertSame( 2, $table::delete_many( [ $ids[0], (string) $ids[1], $ids[0] ] ) );
10421042
$this->assertSame( 1, $table::delete_many( [ '000' . $ids[3] ] ) );
10431043
$this->assertNull( $table::get_by_id( $ids[3] ) );
10441044
$this->assertNull( $table::get_by_id( $ids[0] ) );
@@ -1057,7 +1057,7 @@ public function should_not_delete_anything_for_non_positive_ids() {
10571057

10581058
$ids = $this->insert_delete_fixtures( $table, 2 );
10591059

1060-
$this->assertFalse( $table::delete_many( [ -$ids[0], 0, '-' . $ids[1], '0' ] ) );
1060+
$this->assertSame( 0, $table::delete_many( [ -$ids[0], 0, '-' . $ids[1], '0' ] ) );
10611061
$this->assertFalse( $table::delete( -$ids[0] ) );
10621062
$this->assertFalse( $table::delete( 0 ) );
10631063
$this->assertEquals( 2, $table::get_total_items() );

0 commit comments

Comments
 (0)