Skip to content

Commit b184a28

Browse files
committed
Accept zero-padded numeric string IDs in delete_many
1 parent afa709d commit b184a28

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

‎src/Schema/Traits/Custom_Table_Query_Methods.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static function delete_many( array $ids, string $column = '', string $mor
195195
$is_int = $column_object && PHP_Types::INT === $column_object->get_php_type();
196196

197197
$column_ids = $is_int ?
198-
array_filter( array_map( fn( $id ) => filter_var( $id, FILTER_VALIDATE_INT, [ 'options' => [ 'min_range' => 1 ] ] ), $ids ) ) :
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 ) ) :
199199
array_filter( array_map( 'strval', $ids ), fn( $id ) => '' !== $id );
200200

201201
if ( empty( $column_ids ) ) {

‎tests/wpunit/Traits/Custom_Table_Query_MethodsTest.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,9 +1036,11 @@ public function should_delete_many_by_int_and_numeric_string_ids() {
10361036
$table = $this->get_query_test_table();
10371037
Register::table( $table );
10381038

1039-
$ids = $this->insert_delete_fixtures( $table, 3 );
1039+
$ids = $this->insert_delete_fixtures( $table, 4 );
10401040

10411041
$this->assertSame( 2, $table::delete_many( [ $ids[0], (string) $ids[1] ] ) );
1042+
$this->assertSame( 1, $table::delete_many( [ '000' . $ids[3] ] ) );
1043+
$this->assertNull( $table::get_by_id( $ids[3] ) );
10421044
$this->assertNull( $table::get_by_id( $ids[0] ) );
10431045
$this->assertNull( $table::get_by_id( $ids[1] ) );
10441046
$this->assertNotNull( $table::get_by_id( $ids[2] ) );

0 commit comments

Comments
 (0)