Skip to content

Commit 84b425f

Browse files
Copilotswissspidy
andcommitted
Make esc_sql_ident protected in parent class and call it statically
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 7f2d7d9 commit 84b425f

2 files changed

Lines changed: 9 additions & 20 deletions

File tree

‎src/DB_Command.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,7 @@ private static function is_text_col( $type ) {
19231923
*
19241924
* @phpstan-return ($idents is string ? string : array)
19251925
*/
1926-
private static function esc_sql_ident( $idents ) {
1926+
protected static function esc_sql_ident( $idents ) {
19271927
$backtick = static function ( $v ) {
19281928
// Escape any backticks in the identifier by doubling.
19291929
return '`' . str_replace( '`', '``', $v ) . '`';

‎src/DB_Users_Command.php‎

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ public function create( $args, $assoc_args ) {
6464
$grant_privileges = Utils\get_flag_value( $assoc_args, 'grant-privileges', false );
6565

6666
// Escape identifiers for SQL
67-
$username_escaped = $this->esc_sql_ident( $username );
68-
$host_escaped = $this->esc_sql_ident( $host );
69-
$user_identifier = "{$username_escaped}@{$host_escaped}";
67+
$username_escaped = self::esc_sql_ident( $username );
68+
$host_escaped = self::esc_sql_ident( $host );
69+
assert( is_string( $username_escaped ) );
70+
assert( is_string( $host_escaped ) );
71+
$user_identifier = "{$username_escaped}@{$host_escaped}";
7072

7173
// Create user
7274
$create_query = "CREATE USER {$user_identifier}";
@@ -81,8 +83,9 @@ public function create( $args, $assoc_args ) {
8183
// Grant privileges if requested
8284
if ( $grant_privileges ) {
8385
$database = DB_NAME;
84-
$database_escaped = $this->esc_sql_ident( $database );
85-
$grant_query = "GRANT ALL PRIVILEGES ON {$database_escaped}.* TO {$user_identifier};";
86+
$database_escaped = self::esc_sql_ident( $database );
87+
assert( is_string( $database_escaped ) );
88+
$grant_query = "GRANT ALL PRIVILEGES ON {$database_escaped}.* TO {$user_identifier};";
8689
parent::run_query( $grant_query, $assoc_args );
8790

8891
// Flush privileges
@@ -106,18 +109,4 @@ private function esc_sql_string( $value ) {
106109
$value = str_replace( "'", "''", $value );
107110
return "'" . $value . "'";
108111
}
109-
110-
/**
111-
* Escapes (backticks) MySQL identifiers (aka schema object names).
112-
*
113-
* Note: This duplicates functionality from parent DB_Command::esc_sql_ident()
114-
* which is private static and cannot be accessed from child classes.
115-
*
116-
* @param string $ident A single identifier.
117-
* @return string An escaped string.
118-
*/
119-
private function esc_sql_ident( $ident ) {
120-
// Escape any backticks in the identifier by doubling.
121-
return '`' . str_replace( '`', '``', $ident ) . '`';
122-
}
123112
}

0 commit comments

Comments
 (0)