CustomOrdersTableController.php
3 years ago
DataSynchronizer.php
3 years ago
OrdersTableDataStore.php
3 years ago
OrdersTableDataStoreMeta.php
3 years ago
OrdersTableFieldQuery.php
3 years ago
OrdersTableMetaQuery.php
3 years ago
OrdersTableQuery.php
3 years ago
OrdersTableRefundDataStore.php
3 years ago
OrdersTableSearchQuery.php
3 years ago
OrdersTableMetaQuery.php
660 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\Internal\DataStores\Orders; |
| 3 | |
| 4 | defined( 'ABSPATH' ) || exit; |
| 5 | |
| 6 | /** |
| 7 | * Class used to implement meta queries for the orders table datastore via {@see OrdersTableQuery}. |
| 8 | * Heavily inspired by WordPress' own `WP_Meta_Query` for backwards compatibility reasons. |
| 9 | * |
| 10 | * Parts of the implementation have been adapted from {@link https://core.trac.wordpress.org/browser/tags/6.0.1/src/wp-includes/class-wp-meta-query.php}. |
| 11 | */ |
| 12 | class OrdersTableMetaQuery { |
| 13 | |
| 14 | /** |
| 15 | * List of non-numeric SQL operators used for comparisons in meta queries. |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | private const NON_NUMERIC_OPERATORS = array( |
| 20 | '=', |
| 21 | '!=', |
| 22 | 'LIKE', |
| 23 | 'NOT LIKE', |
| 24 | 'IN', |
| 25 | 'NOT IN', |
| 26 | 'EXISTS', |
| 27 | 'NOT EXISTS', |
| 28 | 'RLIKE', |
| 29 | 'REGEXP', |
| 30 | 'NOT REGEXP', |
| 31 | ); |
| 32 | |
| 33 | /** |
| 34 | * List of numeric SQL operators used for comparisons in meta queries. |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | private const NUMERIC_OPERATORS = array( |
| 39 | '>', |
| 40 | '>=', |
| 41 | '<', |
| 42 | '<=', |
| 43 | 'BETWEEN', |
| 44 | 'NOT BETWEEN', |
| 45 | |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * Prefix used when generating aliases for the metadata table. |
| 50 | * |
| 51 | * @var string |
| 52 | */ |
| 53 | private const ALIAS_PREFIX = 'meta'; |
| 54 | |
| 55 | /** |
| 56 | * Name of the main orders table. |
| 57 | * |
| 58 | * @var string |
| 59 | */ |
| 60 | private $meta_table = ''; |
| 61 | |
| 62 | /** |
| 63 | * Name of the metadata table. |
| 64 | * |
| 65 | * @var string |
| 66 | */ |
| 67 | private $orders_table = ''; |
| 68 | |
| 69 | /** |
| 70 | * Sanitized `meta_query`. |
| 71 | * |
| 72 | * @var array |
| 73 | */ |
| 74 | private $queries = array(); |
| 75 | |
| 76 | /** |
| 77 | * Flat list of clauses by name. |
| 78 | * |
| 79 | * @var array |
| 80 | */ |
| 81 | private $flattened_clauses = array(); |
| 82 | |
| 83 | /** |
| 84 | * JOIN clauses to add to the main SQL query. |
| 85 | * |
| 86 | * @var array |
| 87 | */ |
| 88 | private $join = array(); |
| 89 | |
| 90 | /** |
| 91 | * WHERE clauses to add to the main SQL query. |
| 92 | * |
| 93 | * @var array |
| 94 | */ |
| 95 | private $where = array(); |
| 96 | |
| 97 | /** |
| 98 | * Table aliases in use by the meta query. Used to optimize JOINs when possible. |
| 99 | * |
| 100 | * @var array |
| 101 | */ |
| 102 | private $table_aliases = array(); |
| 103 | |
| 104 | /** |
| 105 | * Constructor. |
| 106 | * |
| 107 | * @param OrdersTableQuery $q The main query being performed. |
| 108 | */ |
| 109 | public function __construct( OrdersTableQuery $q ) { |
| 110 | $meta_query = $q->get( 'meta_query' ); |
| 111 | |
| 112 | if ( ! $meta_query ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $this->queries = $this->sanitize_meta_query( $meta_query ); |
| 117 | |
| 118 | $this->meta_table = $q->get_table_name( 'meta' ); |
| 119 | $this->orders_table = $q->get_table_name( 'orders' ); |
| 120 | |
| 121 | $this->build_query(); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Returns JOIN and WHERE clauses to be appended to the main SQL query. |
| 126 | * |
| 127 | * @return array { |
| 128 | * @type string $join JOIN clause. |
| 129 | * @type string $where WHERE clause. |
| 130 | * } |
| 131 | */ |
| 132 | public function get_sql_clauses(): array { |
| 133 | return array( |
| 134 | 'join' => $this->sanitize_join( $this->join ), |
| 135 | 'where' => $this->flatten_where_clauses( $this->where ), |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns a list of names (corresponding to meta_query clauses) that can be used as an 'orderby' arg. |
| 141 | * |
| 142 | * @since 7.4 |
| 143 | * |
| 144 | * @return array |
| 145 | */ |
| 146 | public function get_orderby_keys(): array { |
| 147 | if ( ! $this->flattened_clauses ) { |
| 148 | return array(); |
| 149 | } |
| 150 | |
| 151 | $keys = array(); |
| 152 | $keys[] = 'meta_value'; |
| 153 | $keys[] = 'meta_value_num'; |
| 154 | |
| 155 | $first_clause = reset( $this->flattened_clauses ); |
| 156 | if ( $first_clause && ! empty( $first_clause['key'] ) ) { |
| 157 | $keys[] = $first_clause['key']; |
| 158 | } |
| 159 | |
| 160 | $keys = array_merge( |
| 161 | $keys, |
| 162 | array_keys( $this->flattened_clauses ) |
| 163 | ); |
| 164 | |
| 165 | return $keys; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Returns an SQL fragment for the given meta_query key that can be used in an ORDER BY clause. |
| 170 | * Call {@see 'get_orderby_keys'} to obtain a list of valid keys. |
| 171 | * |
| 172 | * @since 7.4 |
| 173 | * |
| 174 | * @param string $key The key name. |
| 175 | * @return string |
| 176 | * |
| 177 | * @throws \Exception When an invalid key is passed. |
| 178 | */ |
| 179 | public function get_orderby_clause_for_key( string $key ): string { |
| 180 | $clause = false; |
| 181 | |
| 182 | if ( isset( $this->flattened_clauses[ $key ] ) ) { |
| 183 | $clause = $this->flattened_clauses[ $key ]; |
| 184 | } else { |
| 185 | $first_clause = reset( $this->flattened_clauses ); |
| 186 | |
| 187 | if ( $first_clause && ! empty( $first_clause['key'] ) ) { |
| 188 | if ( 'meta_value_num' === $key ) { |
| 189 | return "{$first_clause['alias']}.meta_value+0"; |
| 190 | } |
| 191 | |
| 192 | if ( 'meta_value' === $key || $first_clause['key'] === $key ) { |
| 193 | $clause = $first_clause; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | if ( ! $clause ) { |
| 199 | // translators: %s is a meta_query key. |
| 200 | throw new \Exception( sprintf( __( 'Invalid meta_query clause key: %s.', 'woocommerce' ), $key ) ); |
| 201 | } |
| 202 | |
| 203 | return "CAST({$clause['alias']}.meta_value AS {$clause['cast']})"; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Checks whether a given meta_query clause is atomic or not (i.e. not nested). |
| 208 | * |
| 209 | * @param array $arg The meta_query clause. |
| 210 | * @return boolean TRUE if atomic, FALSE otherwise. |
| 211 | */ |
| 212 | private function is_atomic( array $arg ): bool { |
| 213 | return isset( $arg['key'] ) || isset( $arg['value'] ); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Sanitizes the meta_query argument. |
| 218 | * |
| 219 | * @param array $q A meta_query array. |
| 220 | * @return array A sanitized meta query array. |
| 221 | */ |
| 222 | private function sanitize_meta_query( array $q ): array { |
| 223 | $sanitized = array(); |
| 224 | |
| 225 | foreach ( $q as $key => $arg ) { |
| 226 | if ( 'relation' === $key ) { |
| 227 | $relation = $arg; |
| 228 | } elseif ( ! is_array( $arg ) ) { |
| 229 | continue; |
| 230 | } elseif ( $this->is_atomic( $arg ) ) { |
| 231 | if ( isset( $arg['value'] ) && array() === $arg['value'] ) { |
| 232 | unset( $arg['value'] ); |
| 233 | } |
| 234 | |
| 235 | $arg['compare'] = isset( $arg['compare'] ) ? strtoupper( $arg['compare'] ) : ( isset( $arg['value'] ) && is_array( $arg['value'] ) ? 'IN' : '=' ); |
| 236 | $arg['compare_key'] = isset( $arg['compare_key'] ) ? strtoupper( $arg['compare_key'] ) : ( isset( $arg['key'] ) && is_array( $arg['key'] ) ? 'IN' : '=' ); |
| 237 | |
| 238 | if ( ! in_array( $arg['compare'], self::NON_NUMERIC_OPERATORS, true ) && ! in_array( $arg['compare'], self::NUMERIC_OPERATORS, true ) ) { |
| 239 | $arg['compare'] = '='; |
| 240 | } |
| 241 | |
| 242 | if ( ! in_array( $arg['compare_key'], self::NON_NUMERIC_OPERATORS, true ) ) { |
| 243 | $arg['compare_key'] = '='; |
| 244 | } |
| 245 | |
| 246 | $sanitized[ $key ] = $arg; |
| 247 | $sanitized[ $key ]['index'] = $key; |
| 248 | } else { |
| 249 | $sanitized_arg = $this->sanitize_meta_query( $arg ); |
| 250 | |
| 251 | if ( $sanitized_arg ) { |
| 252 | $sanitized[ $key ] = $sanitized_arg; |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | if ( $sanitized ) { |
| 258 | $sanitized['relation'] = 1 === count( $sanitized ) ? 'OR' : $this->sanitize_relation( $relation ?? 'AND' ); |
| 259 | } |
| 260 | |
| 261 | return $sanitized; |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Makes sure we use an AND or OR relation. Defaults to AND. |
| 266 | * |
| 267 | * @param string $relation An unsanitized relation prop. |
| 268 | * @return string |
| 269 | */ |
| 270 | private function sanitize_relation( string $relation ): string { |
| 271 | if ( ! empty( $relation ) && 'OR' === strtoupper( $relation ) ) { |
| 272 | return 'OR'; |
| 273 | } |
| 274 | |
| 275 | return 'AND'; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Returns the correct type for a given meta type. |
| 280 | * |
| 281 | * @param string $type MySQL type. |
| 282 | * @return string MySQL type. |
| 283 | */ |
| 284 | private function sanitize_cast_type( string $type = '' ): string { |
| 285 | $meta_type = strtoupper( $type ); |
| 286 | |
| 287 | if ( ! $meta_type || ! preg_match( '/^(?:BINARY|CHAR|DATE|DATETIME|SIGNED|UNSIGNED|TIME|NUMERIC(?:\(\d+(?:,\s?\d+)?\))?|DECIMAL(?:\(\d+(?:,\s?\d+)?\))?)$/', $meta_type ) ) { |
| 288 | return 'CHAR'; |
| 289 | } |
| 290 | |
| 291 | if ( 'NUMERIC' === $meta_type ) { |
| 292 | $meta_type = 'SIGNED'; |
| 293 | } |
| 294 | |
| 295 | return $meta_type; |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Makes sure a JOIN array does not have duplicates. |
| 300 | * |
| 301 | * @param array $join A JOIN array. |
| 302 | * @return array A sanitized JOIN array. |
| 303 | */ |
| 304 | private function sanitize_join( array $join ): array { |
| 305 | return array_filter( array_unique( array_map( 'trim', $join ) ) ); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Flattens a nested WHERE array. |
| 310 | * |
| 311 | * @param array $where A possibly nested WHERE array with AND/OR operators. |
| 312 | * @return string An SQL WHERE clause. |
| 313 | */ |
| 314 | private function flatten_where_clauses( $where ): string { |
| 315 | if ( is_string( $where ) ) { |
| 316 | return trim( $where ); |
| 317 | } |
| 318 | |
| 319 | $chunks = array(); |
| 320 | $operator = $this->sanitize_relation( $where['operator'] ?? '' ); |
| 321 | |
| 322 | foreach ( $where as $key => $w ) { |
| 323 | if ( 'operator' === $key ) { |
| 324 | continue; |
| 325 | } |
| 326 | |
| 327 | $flattened = $this->flatten_where_clauses( $w ); |
| 328 | if ( $flattened ) { |
| 329 | $chunks[] = $flattened; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | if ( $chunks ) { |
| 334 | return '(' . implode( " {$operator} ", $chunks ) . ')'; |
| 335 | } else { |
| 336 | return ''; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * Builds all the required internal bits for this meta query. |
| 342 | * |
| 343 | * @return void |
| 344 | */ |
| 345 | private function build_query(): void { |
| 346 | if ( ! $this->queries ) { |
| 347 | return; |
| 348 | } |
| 349 | |
| 350 | $queries = $this->queries; |
| 351 | $sql_where = $this->process( $queries ); |
| 352 | $this->where = $sql_where; |
| 353 | |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Processes meta_query entries and generates the necessary table aliases, JOIN statements and WHERE conditions. |
| 358 | * |
| 359 | * @param array $arg A meta query. |
| 360 | * @param null|array $parent The parent of the element being processed. |
| 361 | * @return array A nested array of WHERE conditions. |
| 362 | */ |
| 363 | private function process( array &$arg, &$parent = null ): array { |
| 364 | $where = array(); |
| 365 | |
| 366 | if ( $this->is_atomic( $arg ) ) { |
| 367 | $arg['alias'] = $this->find_or_create_table_alias_for_clause( $arg, $parent ); |
| 368 | $arg['cast'] = $this->sanitize_cast_type( $arg['type'] ?? '' ); |
| 369 | |
| 370 | $where = array_filter( |
| 371 | array( |
| 372 | $this->generate_where_for_clause_key( $arg ), |
| 373 | $this->generate_where_for_clause_value( $arg ), |
| 374 | ) |
| 375 | ); |
| 376 | |
| 377 | // Store clauses by their key for ORDER BY purposes. |
| 378 | $flat_clause_key = is_int( $arg['index'] ) ? $arg['alias'] : $arg['index']; |
| 379 | |
| 380 | $unique_flat_key = $flat_clause_key; |
| 381 | $i = 1; |
| 382 | while ( isset( $this->flattened_clauses[ $unique_flat_key ] ) ) { |
| 383 | $unique_flat_key = $flat_clause_key . '-' . $i; |
| 384 | $i++; |
| 385 | } |
| 386 | |
| 387 | $this->flattened_clauses[ $unique_flat_key ] =& $arg; |
| 388 | } else { |
| 389 | // Nested. |
| 390 | $relation = $arg['relation']; |
| 391 | unset( $arg['relation'] ); |
| 392 | |
| 393 | foreach ( $arg as $index => &$clause ) { |
| 394 | $chunks[] = $this->process( $clause, $arg ); |
| 395 | } |
| 396 | |
| 397 | // Merge chunks of the form OR(m) with the surrounding clause. |
| 398 | if ( 1 === count( $chunks ) ) { |
| 399 | $where = $chunks[0]; |
| 400 | } else { |
| 401 | $where = array_merge( |
| 402 | array( |
| 403 | 'operator' => $relation, |
| 404 | ), |
| 405 | $chunks |
| 406 | ); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | return $where; |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * Generates a JOIN clause to handle an atomic meta_query clause. |
| 415 | * |
| 416 | * @param array $clause An atomic meta_query clause. |
| 417 | * @param string $alias Metadata table alias to use. |
| 418 | * @return string An SQL JOIN clause. |
| 419 | */ |
| 420 | private function generate_join_for_clause( array $clause, string $alias ): string { |
| 421 | global $wpdb; |
| 422 | |
| 423 | if ( 'NOT EXISTS' === $clause['compare'] ) { |
| 424 | if ( 'LIKE' === $clause['compare_key'] ) { |
| 425 | return $wpdb->prepare( |
| 426 | "LEFT JOIN {$this->meta_table} AS {$alias} ON ( {$this->orders_table}.id = {$alias}.order_id AND {$alias}.meta_key LIKE %s )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 427 | '%' . $wpdb->esc_like( $clause['key'] ) . '%' |
| 428 | ); |
| 429 | } else { |
| 430 | return $wpdb->prepare( |
| 431 | "LEFT JOIN {$this->meta_table} AS {$alias} ON ( {$this->orders_table}.id = {$alias}.order_id AND {$alias}.meta_key = %s )", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 432 | $clause['key'] |
| 433 | ); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | return "INNER JOIN {$this->meta_table} AS {$alias} ON ( {$this->orders_table}.id = {$alias}.order_id )"; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Finds a common table alias that the meta_query clause can use, or creates one. |
| 442 | * |
| 443 | * @param array $clause An atomic meta_query clause. |
| 444 | * @param array $parent_query The parent query this clause is in. |
| 445 | * @return string A table alias for use in an SQL JOIN clause. |
| 446 | */ |
| 447 | private function find_or_create_table_alias_for_clause( array $clause, array $parent_query ): string { |
| 448 | if ( ! empty( $clause['alias'] ) ) { |
| 449 | return $clause['alias']; |
| 450 | } |
| 451 | |
| 452 | $alias = false; |
| 453 | $siblings = array_filter( |
| 454 | $parent_query, |
| 455 | array( __CLASS__, 'is_atomic' ) |
| 456 | ); |
| 457 | |
| 458 | foreach ( $siblings as $sibling ) { |
| 459 | if ( empty( $sibling['alias'] ) ) { |
| 460 | continue; |
| 461 | } |
| 462 | |
| 463 | if ( $this->is_operator_compatible_with_shared_join( $clause, $sibling, $parent_query['relation'] ?? 'AND' ) ) { |
| 464 | $alias = $sibling['alias']; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if ( ! $alias ) { |
| 470 | $alias = self::ALIAS_PREFIX . count( $this->table_aliases ); |
| 471 | $this->join[] = $this->generate_join_for_clause( $clause, $alias ); |
| 472 | $this->table_aliases[] = $alias; |
| 473 | } |
| 474 | |
| 475 | return $alias; |
| 476 | } |
| 477 | |
| 478 | /** |
| 479 | * Checks whether two meta_query clauses can share a JOIN. |
| 480 | * |
| 481 | * @param array $clause An atomic meta_query clause. |
| 482 | * @param array $sibling An atomic meta_query clause. |
| 483 | * @param string $relation The relation involving both clauses. |
| 484 | * @return boolean TRUE if the clauses can share a table alias, FALSE otherwise. |
| 485 | */ |
| 486 | private function is_operator_compatible_with_shared_join( array $clause, array $sibling, string $relation = 'AND' ): bool { |
| 487 | if ( ! $this->is_atomic( $clause ) || ! $this->is_atomic( $sibling ) ) { |
| 488 | return false; |
| 489 | } |
| 490 | |
| 491 | $valid_operators = array(); |
| 492 | |
| 493 | if ( 'OR' === $relation ) { |
| 494 | $valid_operators = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' ); |
| 495 | } elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) { |
| 496 | $valid_operators = array( '!=', 'NOT IN', 'NOT LIKE' ); |
| 497 | } |
| 498 | |
| 499 | return in_array( strtoupper( $clause['compare'] ), $valid_operators, true ) && in_array( strtoupper( $sibling['compare'] ), $valid_operators, true ); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * Generates an SQL WHERE clause for a given meta_query atomic clause based on its meta key. |
| 504 | * Adapted from WordPress' `WP_Meta_Query::get_sql_for_clause()` method. |
| 505 | * |
| 506 | * @param array $clause An atomic meta_query clause. |
| 507 | * @return string An SQL WHERE clause or an empty string if $clause is invalid. |
| 508 | */ |
| 509 | private function generate_where_for_clause_key( array $clause ): string { |
| 510 | global $wpdb; |
| 511 | |
| 512 | if ( ! array_key_exists( 'key', $clause ) ) { |
| 513 | return ''; |
| 514 | } |
| 515 | |
| 516 | if ( 'NOT EXISTS' === $clause['compare'] ) { |
| 517 | return "{$clause['alias']}.order_id IS NULL"; |
| 518 | } |
| 519 | |
| 520 | $alias = $clause['alias']; |
| 521 | |
| 522 | if ( in_array( $clause['compare_key'], array( '!=', 'NOT IN', 'NOT LIKE', 'NOT EXISTS', 'NOT REGEXP' ), true ) ) { |
| 523 | $i = count( $this->table_aliases ); |
| 524 | $subquery_alias = self::ALIAS_PREFIX . $i; |
| 525 | $this->table_aliases[] = $subquery_alias; |
| 526 | |
| 527 | $meta_compare_string_start = 'NOT EXISTS ('; |
| 528 | $meta_compare_string_start .= "SELECT 1 FROM {$this->meta_table} {$subquery_alias} "; |
| 529 | $meta_compare_string_start .= "WHERE {$subquery_alias}.order_id = {$alias}.order_id "; |
| 530 | $meta_compare_string_end = 'LIMIT 1'; |
| 531 | $meta_compare_string_end .= ')'; |
| 532 | } |
| 533 | |
| 534 | switch ( $clause['compare_key'] ) { |
| 535 | case '=': |
| 536 | case 'EXISTS': |
| 537 | $where = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 538 | break; |
| 539 | case 'LIKE': |
| 540 | $meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%'; |
| 541 | $where = $wpdb->prepare( "$alias.meta_key LIKE %s", $meta_compare_value ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 542 | break; |
| 543 | case 'IN': |
| 544 | $meta_compare_string = "$alias.meta_key IN (" . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ')'; |
| 545 | $where = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 546 | break; |
| 547 | case 'RLIKE': |
| 548 | case 'REGEXP': |
| 549 | $operator = $clause['compare_key']; |
| 550 | if ( isset( $clause['type_key'] ) && 'BINARY' === strtoupper( $clause['type_key'] ) ) { |
| 551 | $cast = 'BINARY'; |
| 552 | } else { |
| 553 | $cast = ''; |
| 554 | } |
| 555 | $where = $wpdb->prepare( "$alias.meta_key $operator $cast %s", trim( $clause['key'] ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 556 | break; |
| 557 | case '!=': |
| 558 | case 'NOT EXISTS': |
| 559 | $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key = %s " . $meta_compare_string_end; |
| 560 | $where = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 561 | break; |
| 562 | case 'NOT LIKE': |
| 563 | $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key LIKE %s " . $meta_compare_string_end; |
| 564 | |
| 565 | $meta_compare_value = '%' . $wpdb->esc_like( trim( $clause['key'] ) ) . '%'; |
| 566 | $where = $wpdb->prepare( $meta_compare_string, $meta_compare_value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 567 | break; |
| 568 | case 'NOT IN': |
| 569 | $array_subclause = '(' . substr( str_repeat( ',%s', count( $clause['key'] ) ), 1 ) . ') '; |
| 570 | $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key IN " . $array_subclause . $meta_compare_string_end; |
| 571 | $where = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 572 | break; |
| 573 | case 'NOT REGEXP': |
| 574 | $operator = $clause['compare_key']; |
| 575 | if ( isset( $clause['type_key'] ) && 'BINARY' === strtoupper( $clause['type_key'] ) ) { |
| 576 | $cast = 'BINARY'; |
| 577 | } else { |
| 578 | $cast = ''; |
| 579 | } |
| 580 | |
| 581 | $meta_compare_string = $meta_compare_string_start . "AND $subquery_alias.meta_key REGEXP $cast %s " . $meta_compare_string_end; |
| 582 | $where = $wpdb->prepare( $meta_compare_string, $clause['key'] ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 583 | break; |
| 584 | default: |
| 585 | $where = ''; |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | return $where; |
| 590 | } |
| 591 | |
| 592 | /** |
| 593 | * Generates an SQL WHERE clause for a given meta_query atomic clause based on its meta value. |
| 594 | * Adapted from WordPress' `WP_Meta_Query::get_sql_for_clause()` method. |
| 595 | * |
| 596 | * @param array $clause An atomic meta_query clause. |
| 597 | * @return string An SQL WHERE clause or an empty string if $clause is invalid. |
| 598 | */ |
| 599 | private function generate_where_for_clause_value( $clause ): string { |
| 600 | global $wpdb; |
| 601 | |
| 602 | if ( ! array_key_exists( 'value', $clause ) ) { |
| 603 | return ''; |
| 604 | } |
| 605 | |
| 606 | $meta_value = $clause['value']; |
| 607 | |
| 608 | if ( in_array( $clause['compare'], array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ), true ) ) { |
| 609 | if ( ! is_array( $meta_value ) ) { |
| 610 | $meta_value = preg_split( '/[,\s]+/', $meta_value ); |
| 611 | } |
| 612 | } elseif ( is_string( $meta_value ) ) { |
| 613 | $meta_value = trim( $meta_value ); |
| 614 | } |
| 615 | |
| 616 | $meta_compare = $clause['compare']; |
| 617 | |
| 618 | switch ( $meta_compare ) { |
| 619 | case 'IN': |
| 620 | case 'NOT IN': |
| 621 | $where = $wpdb->prepare( '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')', $meta_value ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 622 | break; |
| 623 | |
| 624 | case 'BETWEEN': |
| 625 | case 'NOT BETWEEN': |
| 626 | $where = $wpdb->prepare( '%s AND %s', $meta_value[0], $meta_value[1] ); |
| 627 | break; |
| 628 | |
| 629 | case 'LIKE': |
| 630 | case 'NOT LIKE': |
| 631 | $where = $wpdb->prepare( '%s', '%' . $wpdb->esc_like( $meta_value ) . '%' ); |
| 632 | break; |
| 633 | |
| 634 | // EXISTS with a value is interpreted as '='. |
| 635 | case 'EXISTS': |
| 636 | $meta_compare = '='; |
| 637 | $where = $wpdb->prepare( '%s', $meta_value ); |
| 638 | break; |
| 639 | |
| 640 | // 'value' is ignored for NOT EXISTS. |
| 641 | case 'NOT EXISTS': |
| 642 | $where = ''; |
| 643 | break; |
| 644 | |
| 645 | default: |
| 646 | $where = $wpdb->prepare( '%s', $meta_value ); |
| 647 | break; |
| 648 | } |
| 649 | |
| 650 | if ( $where ) { |
| 651 | if ( 'CHAR' === $clause['cast'] ) { |
| 652 | return "{$clause['alias']}.meta_value {$meta_compare} {$where}"; |
| 653 | } else { |
| 654 | return "CAST({$clause['alias']}.meta_value AS {$clause['cast']}) {$meta_compare} {$where}"; |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | } |
| 660 |