From cbd7f0ac9143e34c7bc54ed66f98bb7e439df558 Mon Sep 17 00:00:00 2001 From: Joey Kudish Date: Mon, 27 Nov 2023 19:51:15 -0800 Subject: [PATCH] use table alias/name when building date query use the received table name & table alias arguments in the `get_sql` function to set those as properties on the Date class. Then, when building the SQL query, use the alias or name (only if available) to prepend to the column name in the query. This prevents and fixes the issue described in https://github.com/awesomemotive/easy-digital-downloads/issues/9699 --- includes/database/engine/class-date.php | 27 +++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/includes/database/engine/class-date.php b/includes/database/engine/class-date.php index d5b2e0d2a2..0e318ae3c8 100644 --- a/includes/database/engine/class-date.php +++ b/includes/database/engine/class-date.php @@ -137,6 +137,19 @@ class Date extends Base { 'AND' ); + + /** + * @since 3.2.6 + * @var string|null Table name + */ + public $table_name = null; + + /** + * @since 3.2.6 + * @var string|null Table alias + */ + public $table_alias = null; + /** * Constructor. * @@ -362,6 +375,12 @@ public function get_column( $query = array() ) { ? esc_sql( $this->validate_column( $query['column'] ) ) : $this->column; + if (!empty($this->table_alias)) { + $retval = $this->table_alias . '.' . $retval; + } elseif (!empty($this->table_name)) { + $retval = $this->table_name . '.' . $retval; + } + return $retval; } @@ -602,11 +621,15 @@ public function validate_column( $column = '' ) { * Generate WHERE clause to be appended to a main query. * * @since 1.0.0 + * @param string $table_name Optional. Table name. Default null. + * @param string $table_alias Optional. Table alias. Default null. * * @return string MySQL WHERE clauses. */ - public function get_sql() { - $sql = $this->get_sql_clauses(); + public function get_sql( $table_name = null, $table_alias = null ) { + $this->table_name = $this->sanitize_table_name( $table_name ); + $this->table_alias = $this->sanitize_table_name( $table_alias ); + $sql = $this->get_sql_clauses(); /** * Filters the date query clauses.