| @@ -228,194 +228,8 @@ | ||
| 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 | - /** | |
| 418 | 232 | * Conditionally returns current database charset or collate. |
| 419 | 233 | * |
| 420 | 234 | * @since 0.0.10 |
| 421 | 235 | * @return string |
| @@ -472,31 +286,10 @@ | ||
| 472 | 286 | |
| 473 | 287 | if ( false === $result ) { |
| 474 | 288 | // Stop DB alteration if we have any error. |
| 475 | 289 | $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 | 290 | } |
| 492 | 291 | |
| 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 | 292 | return $result; |
| 500 | 293 | } |
| 501 | 294 | |
| 502 | 295 | /** |
| @@ -627,22 +420,10 @@ | ||
| 627 | 420 | // Execute the query. |
| 628 | 421 | $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 | 422 | |
| 630 | 423 | 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). | |
| 424 | + // Stop DB alteration if we have any error. | |
| 634 | 425 | $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 | 426 | } |
| 646 | 427 | |
| 647 | 428 | return $result; |
| 648 | 429 | } |
| @@ -729,12 +510,8 @@ | ||
| 729 | 510 | $format = $prepared_data['format']; |
| 730 | 511 | } |
| 731 | 512 | |
| 732 | 513 | $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 | 514 | return $result ? $this->wpdb->insert_id : false; |
| 738 | 515 | } |
| 739 | 516 | |
| 740 | 517 | /** |
| @@ -930,59 +707,8 @@ | ||
| 930 | 707 | return Helper::get_integer_value( $this->cache_set( $query, $results ) ); |
| 931 | 708 | } |
| 932 | 709 | |
| 933 | 710 | /** |
| 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 | 711 | * Get the allowed column names for ORDER BY clauses. |
| 986 | 712 | * Child classes may override this method to restrict orderable columns further. |
| 987 | 713 | * |
| 988 | 714 | * @since 2.6.0 |
| @@ -1089,14 +815,9 @@ | ||
| 1089 | 815 | } |
| 1090 | 816 | |
| 1091 | 817 | switch ( $_value['compare'] ) { |
| 1092 | 818 | 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'] ) ) . "%%'"; | |
| 819 | + $clause_parts[] = $_value['key'] . ' ' . $_value['compare'] . ' "%%' . $this->get_format_by_datatype( Helper::get_string_value( $schema[ $_value['key'] ]['type'] ) ) . '%%"'; | |
| 1099 | 820 | $values[] = $_value['value']; |
| 1100 | 821 | break; |
| 1101 | 822 | |
| 1102 | 823 | case 'IN': |