get_var( 'SELECT VERSION()' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- SELECT VERSION() reads server metadata that no WordPress API exposes. $is_mariadb = self::is_mariadb_version( $raw_version ); // MariaDB prefixes a fake "5.5.5-" ahead of its real version. $clean = (string) preg_replace( '/^5\.5\.5-/', '', $raw_version ); $numeric = (string) preg_replace( '/[^0-9.].*/', '', $clean ); return [ 'mysql_version' => $clean, 'is_mariadb' => $is_mariadb, 'numeric_version' => $numeric, 'max_allowed_packet' => (int) self::variable( $wpdb, 'max_allowed_packet' ), 'sql_mode' => (string) self::variable( $wpdb, 'sql_mode' ), 'lower_case_table_names' => self::int_variable( $wpdb, 'lower_case_table_names' ), 'server_charset' => (string) self::variable( $wpdb, 'character_set_server' ), 'server_collation' => (string) self::variable( $wpdb, 'collation_server' ), 'php_memory_limit' => self::bytes_from_ini( (string) ini_get( 'memory_limit' ) ), 'max_execution_time' => (int) ini_get( 'max_execution_time' ), ]; } /** Whether a SELECT VERSION() string comes from MariaDB. */ public static function is_mariadb_version( string $raw_version ): bool { return false !== stripos( $raw_version, 'mariadb' ); } /** One MySQL variable value, or '' when unreadable. */ private static function variable( \wpdb $wpdb, string $name ): string { // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- a server variable, nothing to cache $row = $wpdb->get_row( $wpdb->prepare( 'SHOW VARIABLES LIKE %s', $wpdb->esc_like( $name ) ), ARRAY_N ); return is_array( $row ) && isset( $row[1] ) ? (string) $row[1] : ''; } /** One variable as an int, or null when unreported, so "zero" differs from "unknown". */ private static function int_variable( \wpdb $wpdb, string $name ): ?int { $value = self::variable( $wpdb, $name ); return '' === $value ? null : (int) $value; } /** A PHP ini size ('256M', '1G') in bytes; -1 and 0 (no limit) become 0. */ private static function bytes_from_ini( string $value ): int { $value = trim( $value ); if ( '' === $value || '-1' === $value ) { return 0; } $unit = strtolower( $value[ strlen( $value ) - 1 ] ); $number = (int) $value; return match ( $unit ) { 'g' => $number * 1024 * 1024 * 1024, 'm' => $number * 1024 * 1024, 'k' => $number * 1024, default => max( 0, (int) $value ), }; } }