search-tax-query.php 13.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
<?php
/**
 * /lib/search-tax-query.php
 *
 * Responsible for converting tax_query parameters to MySQL query restrictions.
 *
 * @package Relevanssi
 * @author  Mikko Saari
 * @license https://wordpress.org/about/gpl/ GNU General Public License
 * @see     https://www.relevanssi.com/
 */

/**
 * Processes the tax query to formulate a query restriction to the MySQL query.
 *
 * @uses relevanssi_process_tax_query_row()
 *
 * @param string $tax_query_relation The base tax query relation. Default 'and'.
 * @param array  $tax_query          The tax query array.
 *
 * @return string The query restrictions for the MySQL query.
 */
function relevanssi_process_tax_query( string $tax_query_relation, array $tax_query ): string {
	$query_restrictions = '';
	if ( empty( $tax_query_relation ) ) {
		$tax_query_relation = 'and';
	}
	$tax_query_relation = relevanssi_strtolower( $tax_query_relation );
	$term_tax_ids       = array();
	$not_term_tax_ids   = array();
	$and_term_tax_ids   = array();
	$exist_queries      = array();

	$is_sub_row = false;
	foreach ( $tax_query as $row ) {
		if ( isset( $row['terms'] ) || ( isset( $row['operator'] ) && ( 'not exists' === strtolower( $row['operator'] ) || 'exists' === strtolower( $row['operator'] ) ) ) ) {
			list( $query_restrictions, $term_tax_ids, $not_term_tax_ids, $and_term_tax_ids, $exist_queries ) =
			relevanssi_process_tax_query_row( $row, $is_sub_row, $tax_query_relation, $query_restrictions, $tax_query_relation, $term_tax_ids, $not_term_tax_ids, $and_term_tax_ids, $exist_queries );
		} else {
			$row_tax_query_relation = $tax_query_relation;
			if ( isset( $row['relation'] ) ) {
				$row_tax_query_relation = relevanssi_strtolower( $row['relation'] );
			}
			if ( is_array( $row ) ) {
				foreach ( $row as $subrow ) {
					$is_sub_row = true;
					if ( isset( $subrow['terms'] ) ) {
						list( $query_restrictions, $term_tax_ids, $not_term_tax_ids, $and_term_tax_ids, $exist_queries ) =
						relevanssi_process_tax_query_row( $subrow, $is_sub_row, $tax_query_relation, $query_restrictions, $row_tax_query_relation, $term_tax_ids, $not_term_tax_ids, $and_term_tax_ids, $exist_queries );
					}
				}
				if ( 'or' === $row_tax_query_relation ) {
					$query_restrictions .= relevanssi_process_term_tax_ids(
						$term_tax_ids,
						$not_term_tax_ids,
						$and_term_tax_ids,
						$exist_queries
					);
				}
			}
		}
	}

	if ( 'or' === $tax_query_relation ) {
		$query_restrictions .= relevanssi_process_term_tax_ids(
			$term_tax_ids,
			$not_term_tax_ids,
			$and_term_tax_ids,
			$exist_queries
		);
	}

	return $query_restrictions;
}

/**
 * Processes one tax_query row.
 *
 * @global object $wpdb The WordPress database interface.
 *
 * @param array   $row                The tax_query row array.
 * @param boolean $is_sub_row         True if this is a subrow.
 * @param string  $global_relation    The global tax_query relation (AND or OR).
 * @param string  $query_restrictions The MySQL query restriction.
 * @param string  $tax_query_relation The tax_query relation.
 * @param array   $term_tax_ids       Array of term taxonomy IDs.
 * @param array   $not_term_tax_ids   Array of excluded term taxonomy IDs.
 * @param array   $and_term_tax_ids   Array of AND term taxonomy IDs.
 * @param array   $exist_queries      MySQL queries for EXIST subqueries.
 *
 * @return array Returns an array where the first item is the updated
 * $query_restrictions, then $term_tax_ids, $not_term_tax_ids, $and_term_tax_ids
 * and $exist_queries.
 */
function relevanssi_process_tax_query_row(
	array $row,
	bool $is_sub_row,
	string $global_relation,
	string $query_restrictions,
	string $tax_query_relation,
	array $term_tax_ids,
	array $not_term_tax_ids,
	array $and_term_tax_ids,
	array $exist_queries
): array {
	global $wpdb;

	$local_term_tax_ids     = array();
	$local_not_term_tax_ids = array();
	$local_and_term_tax_ids = array();
	$term_tax_id            = array();

	$exists_query = false;
	if ( isset( $row['operator'] ) && ( 'exists' === strtolower( $row['operator'] ) || 'not exists' === strtolower( $row['operator'] ) ) ) {
		$exists_query = true;
	}

	if ( $exists_query ) {
		$row['field'] = 'exists';
	}
	if ( ! isset( $row['field'] ) ) {
		$row['field'] = 'term_id'; // In case 'field' is not set, go with the WP default of 'term_id'.
	}
	$row['field'] = strtolower( $row['field'] ); // In some cases, you can get 'ID' instead of 'id'.

	if ( in_array( $row['field'], array( 'slug', 'name', 'id', 'term_id' ), true ) ) {
		$term_tax_id = relevanssi_term_tax_id_from_row( $row );
	}

	if ( 'term_taxonomy_id' === $row['field'] ) {
		if ( ! is_array( $row['terms'] ) ) {
			$row['terms'] = array( $row['terms'] );
		}
		$term_tax_id = array_filter( $row['terms'], 'is_numeric' );
	}

	if ( ! $exists_query && ( ! isset( $row['include_children'] ) || true === $row['include_children'] ) ) {
		foreach ( $term_tax_id as $t_id ) {
			$t_term = get_term_by( 'term_taxonomy_id', $t_id, $row['taxonomy'] );
			$t_id   = $t_term->term_id;
			$kids   = get_term_children( $t_id, $row['taxonomy'] );
			foreach ( $kids as $kid ) {
				$kid_term_tax_id = relevanssi_get_term_tax_id( $kid, $row['taxonomy'] );
				if ( $kid_term_tax_id ) {
					// In some weird cases, this may be null. See: https://wordpress.org/support/topic/childrens-of-chosen-product_cat-not-showing-up/.
					$term_tax_id[] = $kid_term_tax_id;
				}
			}
		}
	}

	$term_tax_id = array_unique( $term_tax_id );
	if ( ! empty( $term_tax_id ) ) {
		$n           = count( $term_tax_id );
		$term_tax_id = implode( ',', $term_tax_id );

		$tq_operator = 'IN'; // Assuming the default operator "IN", unless something else is provided.
		if ( isset( $row['operator'] ) ) {
			$tq_operator = strtoupper( $row['operator'] );
		}
		if ( ! in_array( $tq_operator, array( 'IN', 'NOT IN', 'AND' ), true ) ) {
			$tq_operator = 'IN';
		}
		if ( 'and' === $tax_query_relation ) {
			if ( 'AND' === $tq_operator ) {
				$query_restrictions .= " AND relevanssi.doc IN (
					SELECT ID FROM $wpdb->posts WHERE 1=1
					AND (
						SELECT COUNT(1)
						FROM $wpdb->term_relationships AS tr
						WHERE tr.term_taxonomy_id IN ($term_tax_id)
						AND tr.object_id = $wpdb->posts.ID ) = $n
					)";
				// Clean: $term_tax_id and $n are Relevanssi-generated.
			} elseif ( 'or' === $global_relation ) {
				$local_and_term_tax_ids[] = $term_tax_id;
			} else {
				$query_restrictions .= " AND relevanssi.doc $tq_operator (
					SELECT DISTINCT(tr.object_id)
						FROM $wpdb->term_relationships AS tr
						WHERE tr.term_taxonomy_id IN ($term_tax_id))";
				// Clean: all variables are Relevanssi-generated.
			}
		} else {
			if ( 'IN' === $tq_operator ) {
				$local_term_tax_ids[] = $term_tax_id;
			}
			if ( 'NOT IN' === $tq_operator ) {
				$local_not_term_tax_ids[] = $term_tax_id;
			}
			if ( 'AND' === $tq_operator ) {
				$local_and_term_tax_ids[] = $term_tax_id;
			}
		}
	} else {
		global $wp_query;
		$wp_query->is_category = false;
	}

	$copy_term_tax_ids = false;
	if ( ! $is_sub_row ) {
		$copy_term_tax_ids = true;
	}
	if ( $is_sub_row && ( 'or' === $global_relation || 'or' === $tax_query_relation ) ) {
		$copy_term_tax_ids = true;
	}

	if ( $copy_term_tax_ids ) {
		$term_tax_ids     = array_merge( $term_tax_ids, $local_term_tax_ids );
		$not_term_tax_ids = array_merge( $not_term_tax_ids, $local_not_term_tax_ids );
		$and_term_tax_ids = array_merge( $and_term_tax_ids, $local_and_term_tax_ids );
	}

	if ( $exists_query ) {
		$taxonomy = $row['taxonomy'];
		$operator = 'IN';
		if ( 'not exists' === strtolower( $row['operator'] ) ) {
			$operator = 'NOT IN';
		}
		$exist_query_sql = " relevanssi.doc $operator (
				SELECT DISTINCT(tr.object_id)
				FROM $wpdb->term_relationships AS tr, $wpdb->term_taxonomy AS tt
				WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
				AND tt.taxonomy = '$taxonomy' )";
		$exist_queries[] = $exist_query_sql;
		if ( 'and' === $tax_query_relation ) {
			$query_restrictions .= ' AND ' . $exist_query_sql;
		}
	}

	return array(
		$query_restrictions,
		$term_tax_ids,
		$not_term_tax_ids,
		$and_term_tax_ids,
		$exist_queries,
	);
}

/**
 * Generates query restrictions from the term taxonomy ids.
 *
 * Combines different term tax ID arrays into a set of query restrictions that
 * can be used in an OR query.
 *
 * @global object $wpdb The WP database interface.
 *
 * @param array $term_tax_ids     The regular terms.
 * @param array $not_term_tax_ids The NOT terms.
 * @param array $and_term_tax_ids The AND terms.
 * @param array $exist_queries    The EXIST queries.
 *
 * @return string The MySQL query restrictions.
 */
function relevanssi_process_term_tax_ids(
	array $term_tax_ids,
	array $not_term_tax_ids,
	array $and_term_tax_ids,
	array $exist_queries
): string {
	global $wpdb;

	$query_restriction_parts = array();
	$query_restrictions      = '';

	$term_tax_ids = array_unique( $term_tax_ids );
	if ( count( $term_tax_ids ) > 0 ) {
		$term_tax_ids              = implode( ',', $term_tax_ids );
		$query_restriction_parts[] = " relevanssi.doc IN (
			SELECT DISTINCT(tr.object_id)
			FROM $wpdb->term_relationships AS tr
			WHERE tr.term_taxonomy_id IN ($term_tax_ids)
		)";
		// Clean: all variables are Relevanssi-generated.
	}
	if ( count( $not_term_tax_ids ) > 0 ) {
		$not_term_tax_ids           = implode( ',', $not_term_tax_ids );
		$query_restriction_parts[] .= " relevanssi.doc NOT IN (
			SELECT DISTINCT(tr.object_id)
			FROM $wpdb->term_relationships AS tr
			WHERE tr.term_taxonomy_id IN ($not_term_tax_ids)
		)";
		// Clean: all variables are Relevanssi-generated.
	}
	if ( count( $and_term_tax_ids ) > 0 ) {
		$single_term_ids = array();
		foreach ( $and_term_tax_ids as $term_ids ) {
			$n = count( explode( ',', $term_ids ) );
			if ( 1 === $n ) {
				$single_term_ids[] = $term_ids;
				continue;
			}
			$query_restriction_parts[] .= " relevanssi.doc IN (
				SELECT ID FROM $wpdb->posts WHERE 1=1
				AND (
					SELECT COUNT(1)
					FROM $wpdb->term_relationships AS tr
					WHERE tr.term_taxonomy_id IN ($term_ids)
					AND tr.object_id = $wpdb->posts.ID ) = $n
				)";
			// Clean: all variables are Relevanssi-generated.
		}
		if ( count( $single_term_ids ) > 0 ) {
			$n                          = count( $single_term_ids );
			$term_ids                   = implode( ',', $single_term_ids );
			$query_restriction_parts[] .= " relevanssi.doc IN (
				SELECT ID FROM $wpdb->posts WHERE 1=1
				AND (
					SELECT COUNT(1)
					FROM $wpdb->term_relationships AS tr
					WHERE tr.term_taxonomy_id IN ($term_ids)
					AND tr.object_id = $wpdb->posts.ID ) = $n
				)";
			// Clean: all variables are Relevanssi-generated.
		}
	}

	if ( $exist_queries ) {
		$query_restriction_parts = array_merge( $query_restriction_parts, $exist_queries );
	}

	if ( count( $query_restriction_parts ) > 1 ) {
		$query_restrictions .= '(';
	}
	$query_restrictions .= implode( ' OR', $query_restriction_parts );
	if ( count( $query_restriction_parts ) > 1 ) {
		$query_restrictions .= ')';
	}

	if ( $query_restrictions ) {
		$query_restrictions = ' AND ' . $query_restrictions;
	}

	return $query_restrictions;
}

/**
 * Gets and sanitizes the taxonomy name and slug parameters.
 *
 * Checks parameters: if they're numeric, pass them for term_id filtering,
 * otherwise sanitize and create a comma-separated list.
 *
 * @param string|array $terms_parameter The 'terms' field from the tax_query
 * row.
 * @param string       $taxonomy        The taxonomy name.
 * @param string       $field_name      The field name ('slug', 'name').
 *
 * @return array An array containing numeric terms and the list of sanitized
 * term names.
 */
function relevanssi_get_term_in(
	$terms_parameter,
	string $taxonomy,
	string $field_name
): array {
	$numeric_terms = array();
	$names         = array();

	if ( ! is_array( $terms_parameter ) ) {
		$terms_parameter = array( $terms_parameter );
	}
	foreach ( $terms_parameter as $name ) {
		$term = get_term_by( $field_name, $name, $taxonomy );
		if ( ! $term ) {
			if ( ctype_digit( strval( $name ) ) ) {
				$numeric_terms[] = $name;
			}
		} elseif ( isset( $term->term_id ) && in_array( $field_name, array( 'slug', 'name' ), true ) ) {
			$names[] = "'" . esc_sql( $name ) . "'";
		} else {
			$numeric_terms[] = $name;
		}
	}

	return array(
		'numeric_terms' => implode( ',', $numeric_terms ),
		'term_in'       => implode( ',', $names ),
	);
}

/**
 * Gets the term_tax_id from a row with 'field' set to 'slug' or 'name'.
 *
 * If the slugs or names are all numeric values, will switch the 'field'
 * parameter to 'term_id'.
 *
 * @param array $row The taxonomy query row.
 *
 * @return array An array of term taxonomy IDs.
 */
function relevanssi_term_tax_id_from_row( array $row ): array {
	global $wpdb;

	$type = $row['field'];

	$term_in_results = relevanssi_get_term_in( $row['terms'], $row['taxonomy'], $type );
	$numeric_terms   = $term_in_results['numeric_terms'];
	$term_in         = $term_in_results['term_in'];
	$term_tax_id     = array();

	if ( ! empty( $numeric_terms ) ) {
		$type    = 'term_id';
		$term_in = $numeric_terms;
	}

	if ( ! empty( $term_in ) ) {
		$row_taxonomy = sanitize_text_field( $row['taxonomy'] );

		$tt_q = "SELECT tt.term_taxonomy_id
				  FROM $wpdb->term_taxonomy AS tt
				  LEFT JOIN $wpdb->terms AS t ON (tt.term_id=t.term_id)
				  WHERE tt.taxonomy = '$row_taxonomy' AND t.$type IN ($term_in)";
		// Clean: $row_taxonomy is sanitized, each term in $term_in is sanitized.
		$term_tax_id = $wpdb->get_col( $tt_q ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
	}

	return $term_tax_id;
}