From 32f2d98cf7f725133ec5c7d38c64318c08fe7a58 Mon Sep 17 00:00:00 2001 From: Joey Kudish Date: Fri, 20 Sep 2024 05:38:50 +1000 Subject: [PATCH] Use table alias/name when building date query (#164) * 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 * Update get_sql params as per WP_Meta_Query * Update get_column to only use the table_name * Removed the table alias as that's not something WP_Meta_Query uses --- src/Database/Queries/Date.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Database/Queries/Date.php b/src/Database/Queries/Date.php index 2772cce..e246906 100644 --- a/src/Database/Queries/Date.php +++ b/src/Database/Queries/Date.php @@ -152,6 +152,13 @@ class Date extends Base { 'AND' ); + + /** + * @since 2.0.2 + * @var string|null Table name + */ + public $table_name = null; + /** * Constructor. * @@ -388,6 +395,10 @@ public function get_column( $query = array() ) { ? esc_sql( $this->validate_column( $query['column'] ) ) : $this->column; + if (!empty($this->table_name)) { + $retval = $this->table_name . '.' . $retval; + } + return $retval; } @@ -645,8 +656,9 @@ public function validate_column( $column = '' ) { * * @return string MySQL WHERE clauses. */ - public function get_sql() { - $sql = $this->get_sql_clauses(); + public function get_sql( $type, $primary_table, $primary_id_column, $context = null ) { + $this->table_name = $this->sanitize_table_name( $primary_table ); + $sql = $this->get_sql_clauses(); /** * Filters the date query clauses.