Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

Commit e972e47

Browse files
committed
Simplify and document WP table names unquoting
1 parent c745a66 commit e972e47

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

wp-includes/sqlite-ast/class-wp-sqlite-information-schema-reconstructor.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,18 @@ private function get_wp_create_table_statements(): array {
202202

203203
$create_node = $ast->get_first_descendant_node( 'createStatement' );
204204
if ( $create_node && $create_node->has_child_node( 'createTable' ) ) {
205-
$name_node = $create_node->get_first_descendant_node( 'tableName' );
206-
$name = $this->unquote_mysql_identifier(
207-
substr( $schema, $name_node->get_start(), $name_node->get_length() )
208-
);
209-
205+
$name_node = $create_node->get_first_descendant_node( 'tableName' );
206+
$name_value = substr( $schema, $name_node->get_start(), $name_node->get_length() );
207+
208+
/*
209+
* Unquote the table name.
210+
*
211+
* For WordPress Core tables, this naive approach is sufficient,
212+
* as WordPress doesn't use any special characters in table names,
213+
* nor does it allow them in the WPDB prefix, in order to permit
214+
* inlining them in SQL, such as "SELECT * FROM {$wpdb->blogs}".
215+
*/
216+
$name = trim( $name_value, '`"' );
210217
$wp_tables[ $name ] = $create_node;
211218
}
212219
}
@@ -651,21 +658,4 @@ private function escape_mysql_string_literal( string $literal ): string {
651658
// See: https://www.php.net/manual/en/mysqli.real-escape-string.php
652659
return "'" . addcslashes( $literal, "\0\n\r'\"\Z" ) . "'";
653660
}
654-
655-
/**
656-
* Unquote a quoted MySQL identifier.
657-
*
658-
* Remove bounding quotes and replace escaped quotes with their values.
659-
*
660-
* @param string $quoted_identifier The quoted identifier value.
661-
* @return string The unquoted identifier value.
662-
*/
663-
private function unquote_mysql_identifier( string $quoted_identifier ): string {
664-
$first_byte = $quoted_identifier[0] ?? null;
665-
if ( '"' === $first_byte || '`' === $first_byte ) {
666-
$unquoted = substr( $quoted_identifier, 1, -1 );
667-
return str_replace( $first_byte . $first_byte, $first_byte, $unquoted );
668-
}
669-
return $quoted_identifier;
670-
}
671661
}

0 commit comments

Comments
 (0)