| @@ -88,9 +88,9 @@ | ||
| 88 | 88 | * |
| 89 | 89 | * @var array<string> |
| 90 | 90 | * @since 1.8.0 |
| 91 | 91 | */ |
| 92 | - private $allowed_where_operators = [ 'LIKE', 'IN', '=', '!=', '>', '<', '>=', '<=' ]; | |
| 92 | + private $allowed_where_operators = [ 'LIKE', 'IN', 'NOT IN', '=', '!=', '>', '<', '>=', '<=' ]; | |
| 93 | 93 | |
| 94 | 94 | /** |
| 95 | 95 | * Init class. |
| 96 | 96 | * |
| @@ -228,8 +228,194 @@ | ||
| 228 | 228 | return $this->table_name; |
| 229 | 229 | } |
| 230 | 230 | |
| 231 | 231 | /** |
| 232 | + * Whether this table currently exists in the database. | |
| 233 | + * | |
| 234 | + * Deliberately `SHOW TABLES LIKE` rather than the existing get_columns(): | |
| 235 | + * `SHOW COLUMNS FROM <missing table>` is a MySQL error, so it pollutes | |
| 236 | + * $wpdb->last_error, prints under WP_DEBUG_DISPLAY, and cannot tell "the table | |
| 237 | + * is gone" apart from "SHOW is denied". This returns a clean empty set instead. | |
| 238 | + * | |
| 239 | + * esc_like() matters because $wpdb->prefix contains `_`, which is a LIKE | |
| 240 | + * wildcard — without it `wp_srfm_entries` would also match `wpXsrfm_entries`. | |
| 241 | + * The comparison is against the real, unescaped name so the match stays exact. | |
| 242 | + * | |
| 243 | + * Fails safe: any DB-level error reports the table as present. A false "your | |
| 244 | + * database needs updating" on a transient connection blip is worse than a | |
| 245 | + * missed one, because the notice it drives asks the user to alter their schema. | |
| 246 | + * | |
| 247 | + * @since 2.12.6 | |
| 248 | + * @return bool True when the table exists, or when existence cannot be determined. | |
| 249 | + */ | |
| 250 | + public function table_exists() { | |
| 251 | + $wpdb = $this->wpdb; | |
| 252 | + $table = $this->get_tablename(); | |
| 253 | + | |
| 254 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema lookup; the caller owns caching, and a cached answer here would defeat the check. | |
| 255 | + $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ); | |
| 256 | + | |
| 257 | + if ( ! empty( $wpdb->last_error ) ) { | |
| 258 | + return true; | |
| 259 | + } | |
| 260 | + | |
| 261 | + return $found === $table; | |
| 262 | + } | |
| 263 | + | |
| 264 | + /** | |
| 265 | + * A table holding this table's data under a different prefix, if there is one. | |
| 266 | + * | |
| 267 | + * Changing `$table_prefix` — a manual edit, a restored dump from a site with a | |
| 268 | + * different prefix, or a security plugin that renames tables and misses the ones | |
| 269 | + * it does not know about — leaves our data behind under the old name while the | |
| 270 | + * plugin looks for the new one. Creating a fresh empty table there would strand | |
| 271 | + * every stored entry, so look for the old one first and adopt it instead. | |
| 272 | + * | |
| 273 | + * Refuses to guess. Returns '' unless exactly one credible candidate exists, and | |
| 274 | + * only when that candidate carries every column this table's schema declares — | |
| 275 | + * an unrelated table that merely ends in the same words is never touched. | |
| 276 | + * | |
| 277 | + * On multisite, other blogs' tables are legitimate and belong to those blogs. | |
| 278 | + * Anything matching the `{base_prefix}{digits}_` pattern, or the base prefix | |
| 279 | + * itself, is excluded so a subsite can never adopt another subsite's data. | |
| 280 | + * | |
| 281 | + * @since 2.12.6 | |
| 282 | + * @return string Full table name to adopt, or '' when there is nothing safe to adopt. | |
| 283 | + */ | |
| 284 | + public function find_adoptable_table() { | |
| 285 | + $wpdb = $this->wpdb; | |
| 286 | + $correct = $this->get_tablename(); | |
| 287 | + $needle = 'srfm_' . $this->table_suffix; | |
| 288 | + | |
| 289 | + // Wildcard on the left only: the name must *end* at the suffix, so a | |
| 290 | + // deliberate copy such as `wp_srfm_entries_backup` is never a candidate. | |
| 291 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema lookup; a cached answer would defeat the check. | |
| 292 | + $found = $wpdb->get_col( $wpdb->prepare( 'SHOW TABLES LIKE %s', '%' . $wpdb->esc_like( $needle ) ) ); | |
| 293 | + | |
| 294 | + if ( ! empty( $wpdb->last_error ) || ! is_array( $found ) ) { | |
| 295 | + return ''; | |
| 296 | + } | |
| 297 | + | |
| 298 | + $base = $wpdb->base_prefix; | |
| 299 | + $blog_table = '/^' . preg_quote( $base, '/' ) . '\d+_' . preg_quote( $needle, '/' ) . '$/'; | |
| 300 | + $candidates = []; | |
| 301 | + | |
| 302 | + foreach ( $found as $table ) { | |
| 303 | + $table = (string) $table; | |
| 304 | + | |
| 305 | + // The table we are looking for, another blog's table, or the network's | |
| 306 | + // main-site table — none of these are ours to rename. | |
| 307 | + if ( $table === $correct || $base . $needle === $table || preg_match( $blog_table, $table ) ) { | |
| 308 | + continue; | |
| 309 | + } | |
| 310 | + | |
| 311 | + $candidates[] = $table; | |
| 312 | + } | |
| 313 | + | |
| 314 | + // More than one and we cannot tell which holds the real data. Refuse rather | |
| 315 | + // than pick, and let the caller fall back to creating an empty table. | |
| 316 | + if ( 1 !== count( $candidates ) ) { | |
| 317 | + return ''; | |
| 318 | + } | |
| 319 | + | |
| 320 | + return $this->has_expected_columns( $candidates[0] ) ? $candidates[0] : ''; | |
| 321 | + } | |
| 322 | + | |
| 323 | + /** | |
| 324 | + * Rename a differently-prefixed table into this table's expected name. | |
| 325 | + * | |
| 326 | + * RENAME rather than create-and-copy: it is atomic, needs no second copy of the | |
| 327 | + * data, and cannot half-succeed and leave rows in two places. | |
| 328 | + * | |
| 329 | + * @param string $from Full name of the table to adopt. | |
| 330 | + * @since 2.12.6 | |
| 331 | + * @return bool True when the table is in place afterwards. | |
| 332 | + */ | |
| 333 | + public function adopt_table( $from ) { | |
| 334 | + $wpdb = $this->wpdb; | |
| 335 | + $to = $this->get_tablename(); | |
| 336 | + | |
| 337 | + if ( empty( $from ) || $from === $to ) { | |
| 338 | + return false; | |
| 339 | + } | |
| 340 | + | |
| 341 | + // Never rename over an existing table; the one already in place wins. | |
| 342 | + if ( $this->table_exists() ) { | |
| 343 | + return true; | |
| 344 | + } | |
| 345 | + | |
| 346 | + $query = $wpdb->prepare( 'RENAME TABLE %1s TO %2s', str_replace( '`', '', $from ), str_replace( '`', '', $to ) ); // phpcs:ignore -- Same complex-placeholder pattern as create(): identifiers must not be quoted, and both names come from SHOW TABLES / $wpdb->prefix. | |
| 347 | + | |
| 348 | + if ( ! $query ) { | |
| 349 | + // prepare() returned nothing usable; do not fall through to a raw query. | |
| 350 | + return false; | |
| 351 | + } | |
| 352 | + | |
| 353 | + $wpdb->query( $query ); // phpcs:ignore -- We are already using prepare above, and one-off DDL has nothing to cache. | |
| 354 | + | |
| 355 | + if ( ! empty( $wpdb->last_error ) ) { | |
| 356 | + /** This action is documented in inc/database/base.php */ | |
| 357 | + do_action( 'srfm_db_upgrade_query_failed', $wpdb->last_error, 'RENAME TABLE', $to ); | |
| 358 | + } | |
| 359 | + | |
| 360 | + return $this->table_exists(); | |
| 361 | + } | |
| 362 | + | |
| 363 | + /** | |
| 364 | + * Stamp this site's owner signature onto a table's MySQL comment. | |
| 365 | + * | |
| 366 | + * Best-effort: a host that refuses ALTER simply leaves the table unstamped, | |
| 367 | + * which later reads as "ownership unproven" — the safe direction. | |
| 368 | + * | |
| 369 | + * @param string $table Full table name; defaults to this table's own name. | |
| 370 | + * @since 2.12.6 | |
| 371 | + * @return void | |
| 372 | + */ | |
| 373 | + public function stamp_owner_signature( $table = '' ) { | |
| 374 | + $wpdb = $this->wpdb; | |
| 375 | + $table = '' === $table ? $this->get_tablename() : $table; | |
| 376 | + | |
| 377 | + $query = $wpdb->prepare( 'ALTER TABLE %1s COMMENT = %s', str_replace( '`', '', $table ), $this->get_owner_signature() ); // phpcs:ignore -- Identifier must not be quoted; the comment value is a bound, quoted string. | |
| 378 | + | |
| 379 | + if ( ! $query ) { | |
| 380 | + return; | |
| 381 | + } | |
| 382 | + | |
| 383 | + $wpdb->query( $query ); // phpcs:ignore -- Prepared above; one-off DDL with nothing to cache. | |
| 384 | + } | |
| 385 | + | |
| 386 | + /** | |
| 387 | + * Whether a table carries this site's owner signature. | |
| 388 | + * | |
| 389 | + * Gates adoption: on shared hosting a different install's identically-named, | |
| 390 | + * same-schema table can be the only candidate, and renaming it in would destroy | |
| 391 | + * that site's data. Deny by default — anything but an exact signature match | |
| 392 | + * (including a read error, an empty comment, or a legacy table stamped before | |
| 393 | + * this plugin wrote signatures) returns false. | |
| 394 | + * | |
| 395 | + * @param string $table Full table name to inspect. | |
| 396 | + * @since 2.12.6 | |
| 397 | + * @return bool | |
| 398 | + */ | |
| 399 | + public function table_belongs_to_site( $table ) { | |
| 400 | + $wpdb = $this->wpdb; | |
| 401 | + $bare = str_replace( '`', '', (string) $table ); | |
| 402 | + | |
| 403 | + if ( '' === $bare ) { | |
| 404 | + return false; | |
| 405 | + } | |
| 406 | + | |
| 407 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema lookup; a cached answer would defeat the check. | |
| 408 | + $comment = $wpdb->get_var( $wpdb->prepare( 'SELECT TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = %s', $bare ) ); | |
| 409 | + | |
| 410 | + if ( ! empty( $wpdb->last_error ) || ! is_string( $comment ) || '' === $comment ) { | |
| 411 | + return false; | |
| 412 | + } | |
| 413 | + | |
| 414 | + return hash_equals( $this->get_owner_signature(), $comment ); | |
| 415 | + } | |
| 416 | + | |
| 417 | + /** | |
| 232 | 418 | * Conditionally returns current database charset or collate. |
| 233 | 419 | * |
| 234 | 420 | * @since 0.0.10 |
| 235 | 421 | * @return string |
| @@ -286,10 +472,31 @@ | ||
| 286 | 472 | |
| 287 | 473 | if ( false === $result ) { |
| 288 | 474 | // Stop DB alteration if we have any error. |
| 289 | 475 | $this->db_upgradable = false; |
| 476 | + | |
| 477 | + /** | |
| 478 | + * Fires when a table could not be created. | |
| 479 | + * | |
| 480 | + * Column changes have announced their failures since 2.11.0 but table | |
| 481 | + * creation never did — so the one failure that leaves a site with no | |
| 482 | + * table at all, a host denying CREATE TABLE, was the only silent one. | |
| 483 | + * Same signature as the ALTER case so one listener can handle both. | |
| 484 | + * | |
| 485 | + * @param string $last_error The database error. | |
| 486 | + * @param string $query The query that failed. | |
| 487 | + * @param string $table_name The table it was for. | |
| 488 | + * @since 2.12.6 | |
| 489 | + */ | |
| 490 | + do_action( 'srfm_db_upgrade_query_failed', $wpdb->last_error, $query, $this->get_tablename() ); | |
| 290 | 491 | } |
| 291 | 492 | |
| 493 | + if ( false !== $result ) { | |
| 494 | + // Stamp our own table so a future adoption can prove it belongs to this | |
| 495 | + // site before renaming it in. See stamp_owner_signature(). | |
| 496 | + $this->stamp_owner_signature(); | |
| 497 | + } | |
| 498 | + | |
| 292 | 499 | return $result; |
| 293 | 500 | } |
| 294 | 501 | |
| 295 | 502 | /** |
| @@ -582,9 +789,14 @@ | ||
| 582 | 789 | * @since 0.0.13 |
| 583 | 790 | * @return int|false The number of rows deleted, or false on error. |
| 584 | 791 | */ |
| 585 | 792 | public function use_delete( $where, $where_format = null ) { |
| 586 | - return $this->wpdb->delete( $this->get_tablename(), $where, $where_format ); | |
| 793 | + $result = $this->wpdb->delete( $this->get_tablename(), $where, $where_format ); | |
| 794 | + | |
| 795 | + // Reset cache so subsequent queries in the same request exclude the deleted row. | |
| 796 | + $this->cache_reset(); | |
| 797 | + | |
| 798 | + return $result; | |
| 587 | 799 | } |
| 588 | 800 | |
| 589 | 801 | /** |
| 590 | 802 | * Retrieve results from the database based on the given WHERE clauses and selected columns. |
| @@ -621,10 +833,12 @@ | ||
| 621 | 833 | // Add a semicolon at the end of the query. |
| 622 | 834 | $query = rtrim( trim( $query ), ';' ) . ';'; |
| 623 | 835 | |
| 624 | 836 | $cached_results = $this->cache_get( $query ); |
| 625 | - if ( $cached_results ) { | |
| 626 | - // Return the cached data if exists. | |
| 837 | + if ( null !== $cached_results ) { | |
| 838 | + // Return the cached data if exists. Tested against null rather than | |
| 839 | + // truthiness: an empty result set is a real answer, and re-running the | |
| 840 | + // query for it means every no-match lookup runs once per caller. | |
| 627 | 841 | return Helper::get_array_value( $cached_results ); |
| 628 | 842 | } |
| 629 | 843 | |
| 630 | 844 | // phpcs:ignore |
| @@ -710,10 +924,12 @@ | ||
| 710 | 924 | // Add a semicolon at the end of the query. |
| 711 | 925 | $query = rtrim( trim( $query ), ';' ) . ';'; |
| 712 | 926 | |
| 713 | 927 | $cached_results = $this->cache_get( $query ); |
| 714 | - if ( $cached_results ) { | |
| 715 | - // Return the cached data if exists. | |
| 928 | + if ( null !== $cached_results ) { | |
| 929 | + // Return the cached data if exists. Tested against null rather than | |
| 930 | + // truthiness: a count of zero is a real answer, and the editor exclusion | |
| 931 | + // makes zero the common case rather than the exception. | |
| 716 | 932 | return Helper::get_integer_value( $cached_results ); |
| 717 | 933 | } |
| 718 | 934 | |
| 719 | 935 | // phpcs:ignore |
| @@ -723,8 +939,59 @@ | ||
| 723 | 939 | return Helper::get_integer_value( $this->cache_set( $query, $results ) ); |
| 724 | 940 | } |
| 725 | 941 | |
| 726 | 942 | /** |
| 943 | + * The signature this plugin stamps on tables it owns on this site. | |
| 944 | + * | |
| 945 | + * A random per-site token, generated once and stored in options. Embedded in | |
| 946 | + * the table's MySQL comment at creation time; the comment survives RENAME, so a | |
| 947 | + * table that moved under a different prefix still carries it, while an unrelated | |
| 948 | + * install sharing the same database carries a different one. | |
| 949 | + * | |
| 950 | + * @since 2.12.6 | |
| 951 | + * @return string | |
| 952 | + */ | |
| 953 | + protected function get_owner_signature() { | |
| 954 | + $token = get_option( 'srfm_db_owner_token' ); | |
| 955 | + | |
| 956 | + if ( ! is_string( $token ) || '' === $token ) { | |
| 957 | + $token = wp_generate_password( 20, false ); | |
| 958 | + update_option( 'srfm_db_owner_token', $token, false ); | |
| 959 | + } | |
| 960 | + | |
| 961 | + return 'srfm-owner:' . $token; | |
| 962 | + } | |
| 963 | + | |
| 964 | + /** | |
| 965 | + * Whether a table carries every column this table's schema declares. | |
| 966 | + * | |
| 967 | + * Guards adoption: a same-named table from an unrelated source should never be | |
| 968 | + * renamed into place just because its name matches. | |
| 969 | + * | |
| 970 | + * @param string $table Full table name to inspect. | |
| 971 | + * @since 2.12.6 | |
| 972 | + * @return bool | |
| 973 | + */ | |
| 974 | + protected function has_expected_columns( $table ) { | |
| 975 | + $wpdb = $this->wpdb; | |
| 976 | + | |
| 977 | + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema lookup; a cached answer would defeat the check. | |
| 978 | + $columns = $wpdb->get_col( $wpdb->prepare( 'SHOW COLUMNS FROM %1s', str_replace( '`', '', $table ) ) ); // phpcs:ignore -- Same complex-placeholder pattern as create(): an identifier must not be quoted, and the name comes from SHOW TABLES on this connection. | |
| 979 | + | |
| 980 | + if ( ! empty( $wpdb->last_error ) || ! is_array( $columns ) ) { | |
| 981 | + return false; | |
| 982 | + } | |
| 983 | + | |
| 984 | + foreach ( array_keys( $this->get_schema() ) as $column ) { | |
| 985 | + if ( ! in_array( $column, $columns, true ) ) { | |
| 986 | + return false; | |
| 987 | + } | |
| 988 | + } | |
| 989 | + | |
| 990 | + return true; | |
| 991 | + } | |
| 992 | + | |
| 993 | + /** | |
| 727 | 994 | * Get the allowed column names for ORDER BY clauses. |
| 728 | 995 | * Child classes may override this method to restrict orderable columns further. |
| 729 | 996 | * |
| 730 | 997 | * @since 2.6.0 |
| @@ -793,8 +1060,9 @@ | ||
| 793 | 1060 | * @type string $RELATION Optional. The logical relation ('AND' or 'OR'). |
| 794 | 1061 | * } |
| 795 | 1062 | * } |
| 796 | 1063 | * |
| 1064 | + * @since 2.12.7 -- Added support for "NOT IN" compare. | |
| 797 | 1065 | * @since 1.1.1 -- Added support for "IN" compare. |
| 798 | 1066 | * @since 0.0.13 |
| 799 | 1067 | * @return string The prepared SQL WHERE clause with placeholders, or an empty string if no clauses were provided. |
| 800 | 1068 | */ |
| @@ -819,10 +1087,19 @@ | ||
| 819 | 1087 | if ( is_int( $key ) ) { |
| 820 | 1088 | $clause_parts = []; |
| 821 | 1089 | foreach ( $value as $_key => $_value ) { |
| 822 | 1090 | if ( is_int( $_key ) ) { |
| 1091 | + // Normalised before the allowlist test. Payments' | |
| 1092 | + // builder upper-cases and trims, this one compared | |
| 1093 | + // strictly -- so a caller writing 'not in' was honoured | |
| 1094 | + // by one and silently dropped by the other. A dropped | |
| 1095 | + // condition used to be harmless; now that NOT IN is the | |
| 1096 | + // exclusion primitive, dropping it disables the | |
| 1097 | + // exclusion without a word. | |
| 1098 | + $compare = strtoupper( trim( Helper::get_string_value( $_value['compare'] ) ) ); | |
| 1099 | + | |
| 823 | 1100 | // Check if the operator is allowed. |
| 824 | - if ( ! in_array( $_value['compare'], $this->allowed_where_operators, true ) ) { | |
| 1101 | + if ( ! in_array( $compare, $this->allowed_where_operators, true ) ) { | |
| 825 | 1102 | continue; |
| 826 | 1103 | } |
| 827 | 1104 | |
| 828 | 1105 | // Skip if key is not in schema. |
| @@ -829,9 +1106,9 @@ | ||
| 829 | 1106 | if ( ! isset( $schema[ $_value['key'] ] ) ) { |
| 830 | 1107 | continue; |
| 831 | 1108 | } |
| 832 | 1109 | |
| 833 | - switch ( $_value['compare'] ) { | |
| 1110 | + switch ( $compare ) { | |
| 834 | 1111 | case 'LIKE': |
| 835 | 1112 | // Single quotes to match WP core. Under a MySQL session with |
| 836 | 1113 | // ANSI_QUOTES set (not in WP's incompatible_modes list, which |
| 837 | 1114 | // only names the compound ANSI mode) a double-quoted pattern |
| @@ -836,21 +1113,51 @@ | ||
| 836 | 1113 | // ANSI_QUOTES set (not in WP's incompatible_modes list, which |
| 837 | 1114 | // only names the compound ANSI mode) a double-quoted pattern |
| 838 | 1115 | // parses as an identifier and the query hard-fails, taking out |
| 839 | 1116 | // both the listing and its COUNT(*). |
| 840 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . " '%%" . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . "%%'"; | |
| 1117 | + $clause_parts[] = $_value['key'] . ' ' . $compare . " '%%" . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . "%%'"; | |
| 841 | 1118 | $values[] = $_value['value']; |
| 842 | 1119 | break; |
| 843 | 1120 | |
| 844 | 1121 | case 'IN': |
| 1122 | + case 'NOT IN': | |
| 1123 | + // A scalar is a caller bug, not an empty set, and it must | |
| 1124 | + // surface. 'NOT IN' with value 5 -- a plausible typo for | |
| 1125 | + // [ 5 ] -- would otherwise drop the condition and exclude | |
| 1126 | + // nobody, with no error and a green test suite, while the | |
| 1127 | + // same typo on 'IN' fails closed. On a primitive whose only | |
| 1128 | + // job is scoping data, that asymmetry is a hazard. | |
| 1129 | + if ( ! is_array( $_value['value'] ) ) { | |
| 1130 | + _doing_it_wrong( | |
| 1131 | + __METHOD__, | |
| 1132 | + esc_html( "{$compare} requires an array value, received " . gettype( $_value['value'] ) . '.' ), | |
| 1133 | + '2.12.7' | |
| 1134 | + ); | |
| 1135 | + break; | |
| 1136 | + } | |
| 1137 | + | |
| 1138 | + // An empty list cannot be interpolated: "col IN ()" is a syntax | |
| 1139 | + // error that fails the whole query, listing and COUNT alike. | |
| 1140 | + // An empty IN matches nothing, so '1 = 0' says that in any | |
| 1141 | + // relation. An empty NOT IN excludes nothing, but a literal | |
| 1142 | + // would be '1 = 1', and that makes an enclosing OR group | |
| 1143 | + // unconditionally true. Dropping the condition means the same | |
| 1144 | + // thing under AND and stays fail-closed under OR. | |
| 1145 | + if ( [] === $_value['value'] ) { | |
| 1146 | + if ( 'IN' === $compare ) { | |
| 1147 | + $clause_parts[] = '1 = 0'; | |
| 1148 | + } | |
| 1149 | + break; | |
| 1150 | + } | |
| 1151 | + | |
| 845 | 1152 | // Based on the number of values and datatype, it will create WHERE clause for $wpdb::prepare method. Eg: for ID with three values column: ID IN (%d, %d, %d). |
| 846 | 1153 | $datatype = $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); |
| 847 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' (' . implode( ', ', array_fill( 0, count( $_value['value'] ), $datatype ) ) . ')'; | |
| 1154 | + $clause_parts[] = $_value['key'] . ' ' . $compare . ' (' . implode( ', ', array_fill( 0, count( $_value['value'] ), $datatype ) ) . ')'; | |
| 848 | 1155 | $values = array_merge( $values, $_value['value'] ); |
| 849 | 1156 | break; |
| 850 | 1157 | |
| 851 | 1158 | default: |
| 852 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 1159 | + $clause_parts[] = $_value['key'] . ' ' . $compare . ' ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 853 | 1160 | $values[] = $_value['value']; |
| 854 | 1161 | break; |
| 855 | 1162 | } |
| 856 | 1163 | } |
| @@ -875,8 +1182,16 @@ | ||
| 875 | 1182 | return ''; |
| 876 | 1183 | } |
| 877 | 1184 | |
| 878 | 1185 | $where = ' WHERE ' . implode( ' AND ', $groups ); |
| 1186 | + | |
| 1187 | + if ( [] === $values ) { | |
| 1188 | + // Every branch that builds a placeholder also pushes a value, so an | |
| 1189 | + // empty list here means the only conditions were constant ones. There | |
| 1190 | + // is nothing for prepare() to fill, and calling it with no placeholder | |
| 1191 | + // trips _doing_it_wrong. | |
| 1192 | + return $where; | |
| 1193 | + } | |
| 879 | 1194 | |
| 880 | 1195 | // Prepare the query with placeholders. |
| 881 | 1196 | // @phpstan-ignore-next-line -- We are already assigning non-literal string above using "get_format_by_datatype" methods. |
| 882 | 1197 | return $wpdb->prepare( $where, ...$values ); // phpcs:ignore -- We are returning prepared sql query here. We are already using necessary placeholders in $where variable. |