*/ private static array $existsCache = array(); /** * Reset the static existence cache (primarily for unit tests). */ public static function resetCache(): void { $table = self::name(); if ( function_exists( 'wp_cache_delete' ) ) { wp_cache_delete( 'table_exists_' . $table, 'rankkernel_tables' ); } self::$existsCache = array(); } /** * Full table name for the current site. * * @return string Prefixed table name. */ public static function name(): string { global $wpdb; if ( ! isset( $wpdb ) || ! is_object( $wpdb ) || ! isset( $wpdb->prefix ) ) { return 'wp_' . self::SUFFIX; } return (string) $wpdb->prefix . self::SUFFIX; } /** * Cheap existence check used to fail open on the hot path. * * @return bool True when the table exists. */ public static function exists(): bool { global $wpdb; if ( ! isset( $wpdb ) || ! is_object( $wpdb ) ) { return false; } $table = self::name(); if ( array_key_exists( $table, self::$existsCache ) ) { return self::$existsCache[ $table ]; } // Check WP Object Cache first to prevent database queries on every request when persistent object cache is enabled. if ( function_exists( 'wp_cache_get' ) ) { $foundInCache = false; $cached = wp_cache_get( 'table_exists_' . $table, 'rankkernel_tables', false, $foundInCache ); if ( $foundInCache && is_bool( $cached ) ) { self::$existsCache[ $table ] = $cached; return $cached; } } // Custom table existence probe, single prepared SHOW statement, fail open guard. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ); $exists = is_string( $found ) && $found === $table; self::$existsCache[ $table ] = $exists; if ( function_exists( 'wp_cache_set' ) ) { wp_cache_set( 'table_exists_' . $table, $exists, 'rankkernel_tables', DAY_IN_SECONDS ); } return $exists; } /** * Create the table when missing, safe to call on every enable. * * @return bool True when the table exists after the call. */ public static function ensureTables(): bool { global $wpdb; if ( ! isset( $wpdb ) || ! is_object( $wpdb ) ) { return false; } // The migration ledger is the cross-request schema gate. Once it // matches the running code, an existing table needs no dbDelta on // ordinary requests; a missing table still falls through to repair. $ledger = function_exists( 'get_option' ) ? get_option( 'rankkernel_db_version', '' ) : ''; $code = defined( 'RANKKERNEL_VERSION' ) ? (string) RANKKERNEL_VERSION : ''; if ( '' !== $ledger && '' !== $code && version_compare( $ledger, $code, '>=' ) && self::exists() ) { return true; } self::resetCache(); if ( ! function_exists( 'dbDelta' ) ) { $upgrade = defined( 'ABSPATH' ) ? ABSPATH . 'wp-admin/includes/upgrade.php' : ''; if ( '' !== $upgrade && file_exists( $upgrade ) ) { require_once $upgrade; } } $table = self::name(); $charset = method_exists( $wpdb, 'get_charset_collate' ) ? (string) $wpdb->get_charset_collate() : ''; // dbDelta splits definitions on newlines: one definition per line // keeps its parser from attributing one column's default to another. $sql = "CREATE TABLE `{$table}` (\n" . "id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,\n" . "url TEXT NOT NULL,\n" . "host VARCHAR(255) NOT NULL DEFAULT '',\n" . "code SMALLINT UNSIGNED NOT NULL DEFAULT 0,\n" . "source VARCHAR(20) NOT NULL DEFAULT '',\n" . "message VARCHAR(500) NOT NULL DEFAULT '',\n" . "created DATETIME NOT NULL,\n" . "PRIMARY KEY (id),\n" . "KEY created (created),\n" . "KEY code (code),\n" . "KEY source (source)\n" . ") {$charset};"; if ( function_exists( 'dbDelta' ) ) { // Module table creation via dbDelta, idempotent by design. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange dbDelta( $sql ); } else { // Fallback when the upgrade library is unavailable, same idempotent statement with no user input. // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQL.NotPrepared $wpdb->query( $sql ); } self::resetCache(); return self::exists(); } }