+ /**
+ * Generate SQL clauses for a single query array.
+ *
+ * If nested subqueries are found, this method recurses the tree to
+ * produce the properly nested SQL.
+ *
+ * @since 4.1.0
+ * @access protected
+ *
+ * @param array $query Query to parse, passed by reference.
+ * @param int $depth Optional. Number of tree levels deep we currently are.
+ * Used to calculate indentation. Default 0.
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to a single query array.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
+ */
+ protected function get_sql_for_query( &$query, $depth = 0 ) {
+ $sql_chunks = array(
+ 'join' => array(),
+ 'where' => array(),
+ );
+
+ $sql = array(
+ 'join' => '',
+ 'where' => '',
+ );
+
+ $indent = '';
+ for ( $i = 0; $i < $depth; $i++ ) {
+ $indent .= " ";
+ }
+
+ foreach ( $query as $key => &$clause ) {
+ if ( 'relation' === $key ) {
+ $relation = $query['relation'];
+ } elseif ( is_array( $clause ) ) {
+
+ // This is a first-order clause.
+ if ( $this->is_first_order_clause( $clause ) ) {
+ $clause_sql = $this->get_sql_for_clause( $clause, $query, $key );
+
+ $where_count = count( $clause_sql['where'] );
+ if ( ! $where_count ) {
+ $sql_chunks['where'][] = '';
+ } elseif ( 1 === $where_count ) {
+ $sql_chunks['where'][] = $clause_sql['where'][0];
+ } else {
+ $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
+ }
+
+ $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
+ // This is a subquery, so we recurse.
+ } else {
+ $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
+
+ $sql_chunks['where'][] = $clause_sql['where'];
+ $sql_chunks['join'][] = $clause_sql['join'];
+ }
+ }
+ }
+
+ // Filter to remove empties.
+ $sql_chunks['join'] = array_filter( $sql_chunks['join'] );
+ $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
+
+ if ( empty( $relation ) ) {
+ $relation = 'AND';
+ }
+
+ // Filter duplicate JOIN clauses and combine into a single string.
+ if ( ! empty( $sql_chunks['join'] ) ) {
+ $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
+ }
+
+ // Generate a single WHERE clause with proper brackets and indentation.
+ if ( ! empty( $sql_chunks['where'] ) ) {
+ $sql['where'] = '( ' . "\n " . $indent . implode( ' ' . "\n " . $indent . $relation . ' ' . "\n " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
+ }
+
+ return $sql;
+ }
+
+ /**
+ * Generate SQL JOIN and WHERE clauses for a first-order query clause.
+ *
+ * "First-order" means that it's an array with a 'key' or 'value'.
+ *
+ * @since 4.1.0
+ * @access public
+ *
+ * @global wpdb $wpdb
+ *
+ * @param array $clause Query clause, passed by reference.
+ * @param array $parent_query Parent query array.
+ * @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query`
+ * parameters. If not provided, a key will be generated automatically.
+ * @return array {
+ * Array containing JOIN and WHERE SQL clauses to append to a first-order query.
+ *
+ * @type string $join SQL fragment to append to the main JOIN clause.
+ * @type string $where SQL fragment to append to the main WHERE clause.
+ * }
+ */
+ public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
+ global $wpdb;
+
+ $sql_chunks = array(
+ 'where' => array(),
+ 'join' => array(),
+ );
+
+ if ( isset( $clause['compare'] ) ) {
+ $clause['compare'] = strtoupper( $clause['compare'] );
+ } else {
+ $clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';
+ }
+
+ if ( ! in_array( $clause['compare'], array(
+ '=', '!=', '>', '>=', '<', '<=',
+ 'LIKE', 'NOT LIKE',
+ 'IN', 'NOT IN',
+ 'BETWEEN', 'NOT BETWEEN',
+ 'EXISTS', 'NOT EXISTS',
+ 'REGEXP', 'NOT REGEXP', 'RLIKE'
+ ) ) ) {
+ $clause['compare'] = '=';
+ }
+
+ $meta_compare = $clause['compare'];
+
+ // First build the JOIN clause, if one is required.
+ $join = '';
+
+ // We prefer to avoid joins if possible. Look for an existing join compatible with this clause.
+ $alias = $this->find_compatible_table_alias( $clause, $parent_query );
+ if ( false === $alias ) {
+ $i = count( $this->table_aliases );
+ $alias = $i ? 'mt' . $i : $this->meta_table;
+
+ // JOIN clauses for NOT EXISTS have their own syntax.
+ if ( 'NOT EXISTS' === $meta_compare ) {
+ $join .= " LEFT JOIN $this->meta_table";
+ $join .= $i ? " AS $alias" : '';
+ $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
+
+ // All other JOIN clauses.
+ } else {
+ $join .= " INNER JOIN $this->meta_table";
+ $join .= $i ? " AS $alias" : '';
+ $join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column )";