Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 23 additions & 19 deletions lib/CleantalkSP/SpbctWP/ListTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,33 @@ public function getData()
$order_by_direction = 'asc';
}

// 1) By `sprintf` replace non-preparable items (column names) and build WHERE section
$sql_for_sprintf = 'SELECT %s FROM %s %s ORDER BY %s %s';
$sql_for_prepare = sprintf(
$sql_for_sprintf,
implode(', ', $columns), // COLUMNS
$this->sql['table'], // TABLE
$this->sql['where'], // WHERE
$order_by_column, // ORDER BY COLUMN NAME (sanitized)
$order_by_direction // ORDER BY DIRECTION (sanitized)
// 1) Build base query with sprintf (identifiers only - columns, table, order by)
$sql_base = sprintf(
'SELECT %s FROM %s',
implode(', ', $columns),
$this->sql['table']
);
$preparable_variables = [];

if ($this->sql['limit'] !== false) {
$sql_for_prepare .= ' LIMIT %d, %d';
$preparable_variables[] = $this->sql['offset'];
$preparable_variables[] = $this->sql['limit'];
// 2) ORDER BY (sanitized identifiers)
$sql_order = sprintf(' ORDER BY %s %s', $order_by_column, $order_by_direction);

// 3) Use prepare() for LIMIT/OFFSET, escape % in WHERE for prepare() compatibility
$sql_where = ! empty($this->sql['where']) ? str_replace('%', '%%', $this->sql['where']) : '';
$sql_full = $sql_base . $sql_where . $sql_order . ' LIMIT %d, %d';
if ($this->sql['limit'] !== false && $this->sql['limit'] !== 0) {
$sql_prepared = $wpdb->prepare(
$sql_full,
(int) $this->sql['offset'],
(int) $this->sql['limit']
);
} else {
$sql_prepared = $wpdb->prepare(
$sql_full,
(int) $this->sql['offset'],
PHP_INT_MAX
);
}

// 2) By `prepare` replace all the rest placeholders
$sql_prepared = $wpdb->prepare(
$sql_for_prepare,
$preparable_variables
);
$this->rows = $wpdb->get_results($sql_prepared);
}

Expand Down
Loading