From 1824bbc5dfb938b02c90342b2de1e373d8da83ce 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 | 81 ++++++++++++++++--------- 1 file changed, 53 insertions(+), 28 deletions(-) diff --git a/includes/database/engine/class-date.php b/includes/database/engine/class-date.php index d5b2e0d2a2..c3412cbbad 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. * @@ -145,79 +158,79 @@ class Date extends Base { * 'compare'. When 'compare' is 'IN' or 'NOT IN', arrays are accepted; when 'compare' is 'BETWEEN' or 'NOT * BETWEEN', arrays of two valid values are required. See individual argument descriptions for accepted values. * - * @since 1.0.0 - * * @param array $date_query { * Array of date query clauses. * - * @type array { - * @type string $column Optional. The column to query against. If undefined, inherits the value of + * @type array { + * @type string $column Optional. The column to query against. If undefined, inherits the value of * 'date_created'. Accepts 'date_created', 'date_created_gmt', * 'post_modified','post_modified_gmt', 'comment_date', 'comment_date_gmt'. * Default 'date_created'. - * @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=', + * @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=', * 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default '='. - * @type string $relation Optional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'. + * @type string $relation Optional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'. * Default 'OR'. - * @type array { + * @type array { * Optional. An array of first-order clause parameters, or another fully-formed date query. * - * @type string|array $before { + * @type string|array $before { * Optional. Date to retrieve posts before. Accepts `strtotime()`-compatible string, * or array of 'year', 'month', 'day' values. * - * @type string $year The four-digit year. Default empty. Accepts any four-digit year. - * @type string $month Optional when passing array.The month of the year. + * @type string $year The four-digit year. Default empty. Accepts any four-digit year. + * @type string $month Optional when passing array.The month of the year. * Default (string:empty)|(array:1). Accepts numbers 1-12. - * @type string $day Optional when passing array.The day of the month. + * @type string $day Optional when passing array.The day of the month. * Default (string:empty)|(array:1). Accepts numbers 1-31. * } - * @type string|array $after { + * @type string|array $after { * Optional. Date to retrieve posts after. Accepts `strtotime()`-compatible string, * or array of 'year', 'month', 'day' values. * - * @type string $year The four-digit year. Accepts any four-digit year. Default empty. - * @type string $month Optional when passing array. The month of the year. Accepts numbers 1-12. + * @type string $year The four-digit year. Accepts any four-digit year. Default empty. + * @type string $month Optional when passing array. The month of the year. Accepts numbers 1-12. * Default (string:empty)|(array:12). - * @type string $day Optional when passing array.The day of the month. Accepts numbers 1-31. + * @type string $day Optional when passing array.The day of the month. Accepts numbers 1-31. * Default (string:empty)|(array:last day of month). * } - * @type string $column Optional. Used to add a clause comparing a column other than the + * @type string $column Optional. Used to add a clause comparing a column other than the * column specified in the top-level `$column` parameter. Accepts * 'date_created', 'date_created_gmt', 'post_modified', 'post_modified_gmt', * 'comment_date', 'comment_date_gmt'. Default is the value of * top-level `$column`. - * @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', + * @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', * '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 'IN', * 'NOT IN', 'BETWEEN', and 'NOT BETWEEN'. Comparisons support * arrays in some time-related parameters. Default '='. - * @type bool $inclusive Optional. Include results from dates specified in 'before' or + * @type bool $inclusive Optional. Include results from dates specified in 'before' or * 'after'. Default false. - * @type int|array $year Optional. The four-digit year number. Accepts any four-digit year + * @type int|array $year Optional. The four-digit year number. Accepts any four-digit year * or an array of years if `$compare` supports it. Default empty. - * @type int|array $month Optional. The two-digit month number. Accepts numbers 1-12 or an + * @type int|array $month Optional. The two-digit month number. Accepts numbers 1-12 or an * array of valid numbers if `$compare` supports it. Default empty. - * @type int|array $week Optional. The week number of the year. Accepts numbers 0-53 or an + * @type int|array $week Optional. The week number of the year. Accepts numbers 0-53 or an * array of valid numbers if `$compare` supports it. Default empty. - * @type int|array $dayofyear Optional. The day number of the year. Accepts numbers 1-366 or an + * @type int|array $dayofyear Optional. The day number of the year. Accepts numbers 1-366 or an * array of valid numbers if `$compare` supports it. - * @type int|array $day Optional. The day of the month. Accepts numbers 1-31 or an array + * @type int|array $day Optional. The day of the month. Accepts numbers 1-31 or an array * of valid numbers if `$compare` supports it. Default empty. - * @type int|array $dayofweek Optional. The day number of the week. Accepts numbers 1-7 (1 is + * @type int|array $dayofweek Optional. The day number of the week. Accepts numbers 1-7 (1 is * Sunday) or an array of valid numbers if `$compare` supports it. * Default empty. - * @type int|array $dayofweek_iso Optional. The day number of the week (ISO). Accepts numbers 1-7 + * @type int|array $dayofweek_iso Optional. The day number of the week (ISO). Accepts numbers 1-7 * (1 is Monday) or an array of valid numbers if `$compare` supports it. * Default empty. - * @type int|array $hour Optional. The hour of the day. Accepts numbers 0-23 or an array + * @type int|array $hour Optional. The hour of the day. Accepts numbers 0-23 or an array * of valid numbers if `$compare` supports it. Default empty. - * @type int|array $minute Optional. The minute of the hour. Accepts numbers 0-60 or an array + * @type int|array $minute Optional. The minute of the hour. Accepts numbers 0-60 or an array * of valid numbers if `$compare` supports it. Default empty. - * @type int|array $second Optional. The second of the minute. Accepts numbers 0-60 or an + * @type int|array $second Optional. The second of the minute. Accepts numbers 0-60 or an * array of valid numbers if `$compare` supports it. Default empty. * } * } * } + * @since 1.0.0 + * */ public function __construct( $date_query = array() ) { @@ -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,17 @@ 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.