| @@ -83,16 +83,8 @@ | ||
| 83 | 83 | */ |
| 84 | 84 | private $caches = []; |
| 85 | 85 | |
| 86 | 86 | /** |
| 87 | - * Allowed operators for the database. | |
| 88 | - * | |
| 89 | - * @var array<string> | |
| 90 | - * @since 1.8.0 | |
| 91 | - */ | |
| 92 | - private $allowed_where_operators = [ 'LIKE', 'IN', '=', '!=', '>', '<', '>=', '<=' ]; | |
| 93 | - | |
| 94 | - /** | |
| 95 | 87 | * Init class. |
| 96 | 88 | * |
| 97 | 89 | * @since 0.0.10 |
| 98 | 90 | * @return void |
| @@ -228,194 +220,8 @@ | ||
| 228 | 220 | return $this->table_name; |
| 229 | 221 | } |
| 230 | 222 | |
| 231 | 223 | /** |
| 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 | - /** | |
| 418 | 224 | * Conditionally returns current database charset or collate. |
| 419 | 225 | * |
| 420 | 226 | * @since 0.0.10 |
| 421 | 227 | * @return string |
| @@ -472,31 +278,10 @@ | ||
| 472 | 278 | |
| 473 | 279 | if ( false === $result ) { |
| 474 | 280 | // Stop DB alteration if we have any error. |
| 475 | 281 | $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() ); | |
| 491 | 282 | } |
| 492 | 283 | |
| 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 | - | |
| 499 | 284 | return $result; |
| 500 | 285 | } |
| 501 | 286 | |
| 502 | 287 | /** |
| @@ -603,9 +388,9 @@ | ||
| 603 | 388 | continue; |
| 604 | 389 | } |
| 605 | 390 | |
| 606 | 391 | preg_match( '/(\w+)\s/', $column_definition, $column_matches ); |
| 607 | - $column_name = $column_matches[1] ?? ''; | |
| 392 | + $column_name = $column_matches[1]; | |
| 608 | 393 | |
| 609 | 394 | // If the column does not exist, add it. |
| 610 | 395 | if ( ! isset( $existing_columns[ $column_name ] ) ) { |
| 611 | 396 | $alter_queries[] = $wpdb->prepare( 'ADD COLUMN %1s', $column_definition ); // phpcs:ignore -- We don't need quote here. |
| @@ -627,22 +412,10 @@ | ||
| 627 | 412 | // Execute the query. |
| 628 | 413 | $result = $wpdb->query( $query ); // phpcs:ignore -- It is okay. We are already using prepare above and we need to do DB query directly here. |
| 629 | 414 | |
| 630 | 415 | if ( false === $result ) { |
| 631 | - // Stop DB alteration if we have any error. A failed ALTER leaves the table | |
| 632 | - // version un-bumped, so it retries on every request — expose the underlying | |
| 633 | - // error so persistent failures are diagnosable (hook for logging/monitoring). | |
| 416 | + // Stop DB alteration if we have any error. | |
| 634 | 417 | $this->db_upgradable = false; |
| 635 | - | |
| 636 | - /** | |
| 637 | - * Fires when a SureForms DB schema-upgrade query fails. | |
| 638 | - * | |
| 639 | - * @since 2.11.0 | |
| 640 | - * @param string $last_error The DB error message ( $wpdb->last_error ). | |
| 641 | - * @param string $query The ALTER query that failed. | |
| 642 | - * @param string $table The table being altered. | |
| 643 | - */ | |
| 644 | - do_action( 'srfm_db_upgrade_query_failed', $this->wpdb->last_error, $query, $this->get_tablename() ); | |
| 645 | 418 | } |
| 646 | 419 | |
| 647 | 420 | return $result; |
| 648 | 421 | } |
| @@ -729,12 +502,8 @@ | ||
| 729 | 502 | $format = $prepared_data['format']; |
| 730 | 503 | } |
| 731 | 504 | |
| 732 | 505 | $result = $this->wpdb->insert( $this->get_tablename(), $prepared_data['data'], $format ); |
| 733 | - | |
| 734 | - // Reset cache so subsequent queries in the same request include the new row. | |
| 735 | - $this->cache_reset(); | |
| 736 | - | |
| 737 | 506 | return $result ? $this->wpdb->insert_id : false; |
| 738 | 507 | } |
| 739 | 508 | |
| 740 | 509 | /** |
| @@ -761,14 +530,11 @@ | ||
| 761 | 530 | * @var array<string>|string|null $format Format specifier for the data. |
| 762 | 531 | */ |
| 763 | 532 | $format = $prepared_data['format']; |
| 764 | 533 | |
| 765 | - // Reset the cache on update. | |
| 766 | - $this->cache_reset(); | |
| 767 | - | |
| 768 | 534 | return $this->wpdb->update( |
| 769 | 535 | $this->get_tablename(), |
| 770 | - $prepared_data['data'], | |
| 536 | + $data, | |
| 771 | 537 | $where, |
| 772 | 538 | $format |
| 773 | 539 | ); |
| 774 | 540 | } |
| @@ -847,57 +613,8 @@ | ||
| 847 | 613 | return Helper::get_array_value( $this->cache_set( $query, $results ) ); |
| 848 | 614 | } |
| 849 | 615 | |
| 850 | 616 | /** |
| 851 | - * Retrieves a list of records based on the provided arguments. | |
| 852 | - * | |
| 853 | - * This method fetches results from the database, allowing for various | |
| 854 | - * customization options such as filtering, pagination, and sorting. | |
| 855 | - * | |
| 856 | - * @param array<string,mixed> $args { | |
| 857 | - * Optional. An array of arguments to customize the query. | |
| 858 | - * | |
| 859 | - * @type array $where An associative array of conditions to filter the results. | |
| 860 | - * @type int $limit The maximum number of results to return. Default is 10. | |
| 861 | - * @type int $offset The number of records to skip before starting to collect results. Default is 0. | |
| 862 | - * @type string $orderby The column by which to order the results. Default is 'created_at'. | |
| 863 | - * @type string $order The direction of the order (ASC or DESC). Default is 'DESC'. | |
| 864 | - * } | |
| 865 | - * @param bool $set_limit Whether to set the limit on the query. Default is true. | |
| 866 | - * | |
| 867 | - * @since 1.13.0 | |
| 868 | - * @return array<mixed> The results of the query, typically an array of objects or associative arrays. | |
| 869 | - */ | |
| 870 | - public function get_records_by_args( $args = [], $set_limit = true ) { | |
| 871 | - $_args = wp_parse_args( | |
| 872 | - $args, | |
| 873 | - [ | |
| 874 | - 'where' => [], | |
| 875 | - 'columns' => '*', | |
| 876 | - 'limit' => 10, | |
| 877 | - 'offset' => 0, | |
| 878 | - 'orderby' => 'created_at', | |
| 879 | - 'order' => 'DESC', | |
| 880 | - ] | |
| 881 | - ); | |
| 882 | - $allowed_orderby = $this->get_allowed_orderby_columns(); | |
| 883 | - $orderby = in_array( $_args['orderby'], $allowed_orderby, true ) ? $_args['orderby'] : 'created_at'; | |
| 884 | - $order = 'ASC' === strtoupper( Helper::get_string_value( $_args['order'] ) ) ? 'ASC' : 'DESC'; | |
| 885 | - $extra_queries = [ | |
| 886 | - sprintf( 'ORDER BY `%1$s` %2$s', $orderby, $order ), | |
| 887 | - ]; | |
| 888 | - | |
| 889 | - if ( $set_limit ) { | |
| 890 | - $extra_queries[] = sprintf( 'LIMIT %1$d, %2$d', absint( $_args['offset'] ), absint( $_args['limit'] ) ); | |
| 891 | - } | |
| 892 | - return $this->get_results( | |
| 893 | - $_args['where'], | |
| 894 | - $_args['columns'], | |
| 895 | - $extra_queries | |
| 896 | - ); | |
| 897 | - } | |
| 898 | - | |
| 899 | - /** | |
| 900 | 617 | * Get the total number of rows in the table. |
| 901 | 618 | * |
| 902 | 619 | * @param array<mixed> $where_clauses Optional. An associative array of WHERE clauses for the SQL query. |
| 903 | 620 | * @since 0.0.13 |
| @@ -930,70 +647,8 @@ | ||
| 930 | 647 | return Helper::get_integer_value( $this->cache_set( $query, $results ) ); |
| 931 | 648 | } |
| 932 | 649 | |
| 933 | 650 | /** |
| 934 | - * The signature this plugin stamps on tables it owns on this site. | |
| 935 | - * | |
| 936 | - * A random per-site token, generated once and stored in options. Embedded in | |
| 937 | - * the table's MySQL comment at creation time; the comment survives RENAME, so a | |
| 938 | - * table that moved under a different prefix still carries it, while an unrelated | |
| 939 | - * install sharing the same database carries a different one. | |
| 940 | - * | |
| 941 | - * @since 2.12.6 | |
| 942 | - * @return string | |
| 943 | - */ | |
| 944 | - protected function get_owner_signature() { | |
| 945 | - $token = get_option( 'srfm_db_owner_token' ); | |
| 946 | - | |
| 947 | - if ( ! is_string( $token ) || '' === $token ) { | |
| 948 | - $token = wp_generate_password( 20, false ); | |
| 949 | - update_option( 'srfm_db_owner_token', $token, false ); | |
| 950 | - } | |
| 951 | - | |
| 952 | - return 'srfm-owner:' . $token; | |
| 953 | - } | |
| 954 | - | |
| 955 | - /** | |
| 956 | - * Whether a table carries every column this table's schema declares. | |
| 957 | - * | |
| 958 | - * Guards adoption: a same-named table from an unrelated source should never be | |
| 959 | - * renamed into place just because its name matches. | |
| 960 | - * | |
| 961 | - * @param string $table Full table name to inspect. | |
| 962 | - * @since 2.12.6 | |
| 963 | - * @return bool | |
| 964 | - */ | |
| 965 | - protected function has_expected_columns( $table ) { | |
| 966 | - $wpdb = $this->wpdb; | |
| 967 | - | |
| 968 | - // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema lookup; a cached answer would defeat the check. | |
| 969 | - $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. | |
| 970 | - | |
| 971 | - if ( ! empty( $wpdb->last_error ) || ! is_array( $columns ) ) { | |
| 972 | - return false; | |
| 973 | - } | |
| 974 | - | |
| 975 | - foreach ( array_keys( $this->get_schema() ) as $column ) { | |
| 976 | - if ( ! in_array( $column, $columns, true ) ) { | |
| 977 | - return false; | |
| 978 | - } | |
| 979 | - } | |
| 980 | - | |
| 981 | - return true; | |
| 982 | - } | |
| 983 | - | |
| 984 | - /** | |
| 985 | - * Get the allowed column names for ORDER BY clauses. | |
| 986 | - * Child classes may override this method to restrict orderable columns further. | |
| 987 | - * | |
| 988 | - * @since 2.6.0 | |
| 989 | - * @return array<string> | |
| 990 | - */ | |
| 991 | - protected function get_allowed_orderby_columns() { | |
| 992 | - return array_merge( array_keys( $this->get_schema() ), [ 'updated_at' ] ); | |
| 993 | - } | |
| 994 | - | |
| 995 | - /** | |
| 996 | 651 | * Retrieve a cached value by its key. |
| 997 | 652 | * |
| 998 | 653 | * @param string $key The cache key. |
| 999 | 654 | * @since 0.0.10 |
| @@ -1064,9 +719,9 @@ | ||
| 1064 | 719 | $wpdb = $this->wpdb; |
| 1065 | 720 | |
| 1066 | 721 | // If there are WHERE clauses, prepare and append them to the query. |
| 1067 | 722 | if ( is_array( $where_clauses ) ) { |
| 1068 | - $groups = []; | |
| 723 | + $where = ''; | |
| 1069 | 724 | $values = []; |
| 1070 | 725 | $schema = $this->get_schema(); |
| 1071 | 726 | |
| 1072 | 727 | foreach ( $where_clauses as $key => $value ) { |
| @@ -1071,53 +726,32 @@ | ||
| 1071 | 726 | |
| 1072 | 727 | foreach ( $where_clauses as $key => $value ) { |
| 1073 | 728 | |
| 1074 | 729 | $relation = ! empty( $value['RELATION'] ) ? trim( $value['RELATION'] ) : 'AND'; |
| 1075 | - $relation = in_array( strtoupper( $relation ), [ 'AND', 'OR' ], true ) ? strtoupper( $relation ) : 'AND'; | |
| 1076 | 730 | |
| 1077 | 731 | if ( is_int( $key ) ) { |
| 1078 | - $clause_parts = []; | |
| 1079 | 732 | foreach ( $value as $_key => $_value ) { |
| 1080 | 733 | if ( is_int( $_key ) ) { |
| 1081 | - // Check if the operator is allowed. | |
| 1082 | - if ( ! in_array( $_value['compare'], $this->allowed_where_operators, true ) ) { | |
| 1083 | - continue; | |
| 1084 | - } | |
| 1085 | - | |
| 1086 | - // Skip if key is not in schema. | |
| 1087 | - if ( ! isset( $schema[ $_value['key'] ] ) ) { | |
| 1088 | - continue; | |
| 1089 | - } | |
| 1090 | - | |
| 1091 | 734 | switch ( $_value['compare'] ) { |
| 1092 | 735 | case 'LIKE': |
| 1093 | - // Single quotes to match WP core. Under a MySQL session with | |
| 1094 | - // ANSI_QUOTES set (not in WP's incompatible_modes list, which | |
| 1095 | - // only names the compound ANSI mode) a double-quoted pattern | |
| 1096 | - // parses as an identifier and the query hard-fails, taking out | |
| 1097 | - // both the listing and its COUNT(*). | |
| 1098 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . " '%%" . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . "%%'"; | |
| 1099 | - $values[] = $_value['value']; | |
| 736 | + $where .= ' ' . $_value['key'] . ' ' . $_value['compare'] . ' "%%' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . '%%" ' . $relation; | |
| 737 | + $values[] = $_value['value']; | |
| 1100 | 738 | break; |
| 1101 | 739 | |
| 1102 | 740 | case 'IN': |
| 1103 | 741 | // 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). |
| 1104 | - $datatype = $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 1105 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' (' . implode( ', ', array_fill( 0, count( $_value['value'] ), $datatype ) ) . ')'; | |
| 1106 | - $values = array_merge( $values, $_value['value'] ); | |
| 742 | + $datatype = $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 743 | + $where .= ' ' . $_value['key'] . ' ' . $_value['compare'] . ' (' . implode( ', ', array_fill( 0, count( $_value['value'] ), $datatype ) ) . ') ' . $relation; | |
| 744 | + $values = array_merge( $values, $_value['value'] ); | |
| 1107 | 745 | break; |
| 1108 | 746 | |
| 1109 | 747 | default: |
| 1110 | - $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ); | |
| 1111 | - $values[] = $_value['value']; | |
| 748 | + $where .= ' ' . $_value['key'] . ' ' . $_value['compare'] . ' ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . ' ' . $relation; | |
| 749 | + $values[] = $_value['value']; | |
| 1112 | 750 | break; |
| 1113 | 751 | } |
| 1114 | 752 | } |
| 1115 | 753 | } |
| 1116 | - | |
| 1117 | - if ( ! empty( $clause_parts ) ) { | |
| 1118 | - $groups[] = '(' . implode( ' ' . $relation . ' ', $clause_parts ) . ')'; | |
| 1119 | - } | |
| 1120 | 754 | continue; |
| 1121 | 755 | } |
| 1122 | 756 | |
| 1123 | 757 | if ( ! isset( $schema[ $key ] ) ) { |
| @@ -1124,17 +758,17 @@ | ||
| 1124 | 758 | // Skip strictly if current key is not in our schema. |
| 1125 | 759 | continue; |
| 1126 | 760 | } |
| 1127 | 761 | |
| 1128 | - $groups[] = '(' . $key . ' = ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $key ]['type'] ) ) . ')'; | |
| 762 | + $where .= ' ' . $key . ' = ' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $key ]['type'] ) ) . ' ' . $relation; | |
| 1129 | 763 | $values[] = $value; |
| 1130 | 764 | } |
| 1131 | 765 | |
| 1132 | - if ( empty( $groups ) ) { | |
| 766 | + if ( ! $where ) { | |
| 1133 | 767 | return ''; |
| 1134 | 768 | } |
| 1135 | 769 | |
| 1136 | - $where = ' WHERE ' . implode( ' AND ', $groups ); | |
| 770 | + $where = ' WHERE ' . trim( trim( $where, $relation ) ); | |
| 1137 | 771 | |
| 1138 | 772 | // Prepare the query with placeholders. |
| 1139 | 773 | // @phpstan-ignore-next-line -- We are already assigning non-literal string above using "get_format_by_datatype" methods. |
| 1140 | 774 | return $wpdb->prepare( $where, ...$values ); // phpcs:ignore -- We are returning prepared sql query here. We are already using necessary placeholders in $where variable. |
| @@ -1231,9 +865,8 @@ | ||
| 1231 | 865 | * - 'string': Encoded as a string. |
| 1232 | 866 | * - 'number': Encoded as an integer. |
| 1233 | 867 | * - 'boolean': Encoded as a boolean. |
| 1234 | 868 | * - 'array': Encoded as a JSON string. |
| 1235 | - * @since 1.8.0 - 'datetime': Returns the value as it is, assuming it is already in SQL DATETIME format. | |
| 1236 | 869 | */ |
| 1237 | 870 | protected function encode_by_datatype( $value, $type ) { |
| 1238 | 871 | switch ( $type ) { |
| 1239 | 872 | case 'string': |
| @@ -1247,11 +880,7 @@ | ||
| 1247 | 880 | |
| 1248 | 881 | case 'array': |
| 1249 | 882 | // Lets json_encode array values instead of serializing it. |
| 1250 | 883 | return Helper::encode_json( Helper::get_array_value( $value ) ); |
| 1251 | - | |
| 1252 | - case 'datetime': | |
| 1253 | - // For datetime, we will return the value as it is because we are using sql DATETIME format. | |
| 1254 | - return $value; | |
| 1255 | 884 | } |
| 1256 | 885 | } |
| 1257 | 886 | } |