diff --git a/wp-includes/sqlite-ast/class-wp-sqlite-driver.php b/wp-includes/sqlite-ast/class-wp-sqlite-driver.php index 2945b7e5..1afce54d 100644 --- a/wp-includes/sqlite-ast/class-wp-sqlite-driver.php +++ b/wp-includes/sqlite-ast/class-wp-sqlite-driver.php @@ -483,13 +483,48 @@ public function __construct( WP_SQLite_Connection $connection, string $database // Check the SQLite version. $sqlite_version = $this->get_sqlite_version(); if ( version_compare( $sqlite_version, self::MINIMUM_SQLITE_VERSION, '<' ) ) { - throw $this->new_driver_exception( - sprintf( - 'The SQLite version %s is not supported. Minimum required version is %s.', - $sqlite_version, - self::MINIMUM_SQLITE_VERSION - ) - ); + if ( defined( 'WP_SQLITE_UNSAFE_ENABLE_UNSUPPORTED_VERSIONS' ) && WP_SQLITE_UNSAFE_ENABLE_UNSUPPORTED_VERSIONS ) { + // When "WP_SQLITE_UNSAFE_ENABLE_UNSUPPORTED_VERSIONS" is enabled, + // allow using legacy SQLite versions, but not older than 3.27.0. + if ( version_compare( $sqlite_version, '3.27.0', '<' ) ) { + throw $this->new_driver_exception( + sprintf( + 'The SQLite version %s is not supported. Minimum required version is %s.' + . ' With "WP_SQLITE_UNSAFE_ENABLE_UNSUPPORTED_VERSIONS" enabled, you must use 3.27.0 or newer.', + $sqlite_version, + self::MINIMUM_SQLITE_VERSION + ) + ); + } + + /* + * SQLite versions prior to 3.37.0 do not support STRICT tables. + * + * However, a database created with SQLite >= 3.37.0 can be used + * with SQLite versions < 3.37.0 when "PRAGMA writable_schema" is + * set to "ON", which also enables error-tolerant schema parsing. + * + * This is an unsafe opt-in feature for special back compatibility + * use cases, as it can corrupt the database by allowing incorrect + * types into STRICT tables. Additionally, depending on the legacy + * SQLite version used, there is no guarantee that all features of + * the SQLite driver will work as expected. Use this with caution. + * + * See: https://www.sqlite.org/stricttables.html#accessing_strict_tables_in_earlier_versions_of_sqlite + * + * TODO: Remove this flag when we drop support for PHP 8.0. + * From PHP 8.1, SQLite 3.46.1 is used by default. + */ + $this->execute_sqlite_query( 'PRAGMA writable_schema=ON' ); + } else { + throw $this->new_driver_exception( + sprintf( + 'The SQLite version %s is not supported. Minimum required version is %s.', + $sqlite_version, + self::MINIMUM_SQLITE_VERSION + ) + ); + } } // Load SQLite version to a property used by WordPress health info.