Skip to content

Commit 6d003d6

Browse files
wojteknclaude
andcommitted
Use the connection charset when stripping invalid text without a charset
When $wpdb->charset is empty and a value must be checked by the database, wpdb::strip_invalid_text() falls back to mysqli_character_set_name(), which fails with a TypeError for the SQLite driver. Use the emulated connection charset (utf8mb4) instead. This surfaced in Tests_DB_Charset::test_no_db_charset_defined once tables with a non-UTF-8 DEFAULT CHARSET got their charset recorded correctly. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7cd15a0 commit 6d003d6

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

‎packages/plugin-sqlite-database-integration/wp-includes/sqlite/class-wp-sqlite-db.php‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,41 @@ private function _do_query( $query ) {
738738
}
739739
}
740740

741+
/**
742+
* Strips any invalid characters based on value/charset pairs.
743+
*
744+
* This overrides wpdb::strip_invalid_text() to enable the parent's implementation
745+
* for SQLite when no charset is set, by temporarily using the connection charset.
746+
*
747+
* @see wpdb::strip_invalid_text()
748+
*
749+
* @param array $data Array of value arrays. Each value array has the keys 'value',
750+
* 'charset', and 'length'. An optional 'ascii' key can be set
751+
* to false to avoid redundant ASCII checks.
752+
* @return array|WP_Error The $data parameter, with invalid characters removed from each value.
753+
* This works as a passthrough: any additional keys such as 'field' are
754+
* retained in each value array. If we cannot remove invalid characters,
755+
* a WP_Error object is returned.
756+
*/
757+
protected function strip_invalid_text( $data ) {
758+
$original_charset = $this->charset;
759+
if ( $original_charset ) {
760+
return parent::strip_invalid_text( $data );
761+
}
762+
763+
/*
764+
* Without a charset, the parent method falls back to the charset of the
765+
* mysqli connection. The emulated MySQL connection always uses utf8mb4
766+
* (see init_charset()), so we use it temporarily instead.
767+
*/
768+
try {
769+
$this->charset = 'utf8mb4';
770+
return parent::strip_invalid_text( $data );
771+
} finally {
772+
$this->charset = $original_charset;
773+
}
774+
}
775+
741776
/**
742777
* Method to set the class variable $col_info.
743778
*

0 commit comments

Comments
 (0)