// phpcs:ignore Generic.Commenting.DocComment.MissingShort
/** @noinspection PhpIllegalPsrClassPathInspection */
namespace WPForms\Helpers;
// phpcs:ignore WPForms.PHP.UseStatement.UnusedUseStatement
* Existing tables transient name.
* @since 1.9.0 Changed from 'wpforms_existing_tables' to 'existing_tables'
const EXISTING_TABLES_TRANSIENT_NAME = 'existing_tables';
* Existing tables transient expiration, sec.
* @noinspection SummerTimeUnsafeTimeManipulationInspection
const EXISTING_TABLES_TRANSIENT_EXPIRATION = WEEK_IN_SECONDS; // A week.
private static $existing_tables = [];
* Get the list of existing tables and cache the result.
* @param string $table_name Table name. Can have SQL wildcard.
* @return array List of table names.
public static function get_existing_tables( string $table_name ): array {
* Filters existence of a table before a request to the database is executed.
* @param array $tables Existing tables with given table name.
* @param string $table_name Table name.
$tables = (array) apply_filters( 'wpforms_helpers_db_pre_get_existing_tables', [], $table_name );
$tables = self::get_existing_tables_cache( $table_name );
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$tables = $wpdb->get_results(
$wpdb->prepare( 'SHOW TABLES LIKE %s', $table_name ),
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching
$tables = ! empty( $tables ) ? wp_list_pluck( $tables, 0 ) : [];
self::set_existing_tables_cache( $tables, $table_name );
return self::$existing_tables[ $table_name ] ?? [];
* Get the list of all existing custom tables starting with `wpforms_*` and cache the result.
* @return array List of table names.
public static function get_existing_custom_tables(): array {
return self::get_existing_tables( "{$wpdb->prefix}wpforms_%" );
* Check if the database table exists and cache the result.
* @param string $table_name Table name. Can have SQL wildcard.
public static function table_exists( string $table_name ): bool {
* Filters existence of a table before a request to the database is executed.
* @param integer $exists Table exists.
* @param string $table_name Table name.
if ( apply_filters( 'wpforms_helpers_db_pre_table_exists', false, $table_name ) ) {
foreach ( self::get_existing_tables( $table_name ) as $existing_table ) {
if ( self::wildcard_match( $table_name, $existing_table ) ) {
* Get the list of existing tables from cache.
* @param string $table_name Table name. Can have SQL wildcard.
* @return array List of table names.
private static function get_existing_tables_cache( string $table_name ): array {
$tables = Transient::get( self::EXISTING_TABLES_TRANSIENT_NAME );
self::$existing_tables = $tables ? $tables : [];
return self::$existing_tables[ $table_name ] ?? [];
* Set existing tables cache.
* @param array $tables Existing tables with given table name.
* @param string $table_name Table name.
private static function set_existing_tables_cache( array $tables, string $table_name ) {
if ( empty( $tables ) ) {
self::$existing_tables[ $table_name ] = $tables;
* Filters existing tables transient expiration time.
* @param integer $expiration Expiration time.
$expiration = apply_filters( 'wpforms_helpers_db_existing_tables_transient_expiration', self::EXISTING_TABLES_TRANSIENT_EXPIRATION );
Transient::set( self::EXISTING_TABLES_TRANSIENT_NAME, self::$existing_tables, $expiration );
* Flush existing tables cache.
public static function flush_existing_tables_cache() {
self::$existing_tables = [];
Transient::delete( self::EXISTING_TABLES_TRANSIENT_NAME );
* Works as MySQL LIKE match.
* @param string $pattern Pattern.
* @param string $subject String to search into.
private static function wildcard_match( string $pattern, string $subject ) {
[ '%', '_' ], // MySQL wildcard chars.
[ '.*', '.' ], // Regexp chars.
preg_quote( $pattern, '/' )
return preg_match( '/^' . $regex . '$/is', $subject );
* Check if all custom tables exist.
* @return bool True if all custom tables exist. False if any is missing.
public static function custom_tables_exist(): bool {
$existing_tables = self::get_existing_custom_tables();
$custom_tables = wpforms()->is_pro() ? WPForms_Pro::CUSTOM_TABLES : WPForms_Lite::CUSTOM_TABLES;
foreach ( $custom_tables as $table_name => $handler_class ) {
if ( ! in_array( $wpdb->prefix . $table_name, $existing_tables, true ) ) {
* Create all custom DB tables.
* @param bool $flush_cache Clear existing custom tables cache.
* @noinspection PhpPossiblePolymorphicInvocationInspection
public static function create_custom_tables( bool $flush_cache = false ) {
self::flush_existing_tables_cache();
$existing_tables = self::get_existing_custom_tables();
$custom_tables = wpforms()->is_pro() ? WPForms_Pro::CUSTOM_TABLES : WPForms_Lite::CUSTOM_TABLES;
foreach ( $custom_tables as $table_name => $handler_class ) {
if ( in_array( $wpdb->prefix . $table_name, $existing_tables, true ) ) {
* Child class of WPForms_DB.
* @var $handler WPForms_DB
$handler = new $handler_class();
$handler->create_table();
Transient::delete( self::EXISTING_TABLES_TRANSIENT_NAME );