mirror of
https://github.com/proelements/proelements.git
synced 2026-04-24 20:40:40 +00:00
v3.33.1
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\Forms\Submissions\Database\Migrations;
|
||||
|
||||
use Elementor\Core\Base\Base_Object;
|
||||
use ElementorPro\Modules\Forms\Submissions\Database\Query;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
abstract class Base_Migration extends Base_Object {
|
||||
/*
|
||||
* Ref: wp-admin/includes/schema.php::wp_get_db_schema
|
||||
*
|
||||
* Indexes have a maximum size of 767 bytes. Historically, we haven't need to be concerned about that.
|
||||
* As of 4.2, however, we moved to utf8mb4, which uses 4 bytes per character. This means that an index which
|
||||
* used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters.
|
||||
*/
|
||||
const MAX_INDEX_LENGTH = 191;
|
||||
|
||||
/**
|
||||
* @var \wpdb
|
||||
*/
|
||||
protected $wpdb;
|
||||
|
||||
/**
|
||||
* @var Query
|
||||
*/
|
||||
protected $query;
|
||||
|
||||
/**
|
||||
* Base_Migration constructor.
|
||||
*
|
||||
* @param \wpdb $wpdb
|
||||
*/
|
||||
public function __construct( \wpdb $wpdb ) {
|
||||
$this->wpdb = $wpdb;
|
||||
$this->query = Query::get_instance();
|
||||
}
|
||||
|
||||
/**
|
||||
* Run migration.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function run();
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\Forms\Submissions\Database\Migrations;
|
||||
|
||||
use Elementor\Core\Utils\Collection;
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit; // Exit if accessed directly.
|
||||
}
|
||||
|
||||
class Fix_Indexes extends Base_Migration {
|
||||
/**
|
||||
* In the previous migrations some databases had problems with the indexes.
|
||||
* this migration checks if user's tables are filled with required indexes, if not it creates them.
|
||||
*/
|
||||
public function run() {
|
||||
$indexes = $this->get_indexes();
|
||||
|
||||
foreach ( $indexes as $table => $table_indexes ) {
|
||||
$existing_indexes = $this->get_existed_indexes_of( $table );
|
||||
|
||||
// Protect from database errors (for example: table do not exists somehow).
|
||||
if ( null === $existing_indexes ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$indexes_query = $table_indexes->except( $existing_indexes )->implode( ',' );
|
||||
|
||||
$this->wpdb->query( "ALTER TABLE `{$table}` {$indexes_query};" ); // phpcs:ignore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the user exited indexes
|
||||
*
|
||||
* @param $table_name
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
private function get_existed_indexes_of( $table_name ) {
|
||||
$result = $this->wpdb->get_results( "SHOW INDEX FROM {$table_name};", ARRAY_A ); // phpcs:ignore
|
||||
|
||||
if ( false === $result ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ( new Collection( $result ) )
|
||||
->map( function ( $row ) {
|
||||
if ( ! isset( $row['Key_name'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $row['Key_name'];
|
||||
} )
|
||||
->filter()
|
||||
->values();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all the database indexes.
|
||||
*
|
||||
* @return Collection[]
|
||||
*/
|
||||
private function get_indexes() {
|
||||
$max_index_length = static::MAX_INDEX_LENGTH;
|
||||
|
||||
return [
|
||||
$this->query->get_table_submissions() => new Collection( [
|
||||
'main_meta_id_index' => 'ADD INDEX `main_meta_id_index` (`main_meta_id`)',
|
||||
'hash_id_unique_index' => "ADD UNIQUE INDEX `hash_id_unique_index` (`hash_id` ({$max_index_length}))",
|
||||
'hash_id_index' => "ADD INDEX `hash_id_index` (`hash_id` ({$max_index_length}))",
|
||||
'type_index' => "ADD INDEX `type_index` (`type` ({$max_index_length}))",
|
||||
'post_id_index' => 'ADD INDEX `post_id_index` (`post_id`)',
|
||||
'element_id_index' => "ADD INDEX `element_id_index` (`element_id` ({$max_index_length}))",
|
||||
'campaign_id_index' => 'ADD INDEX `campaign_id_index` (`campaign_id`)',
|
||||
'user_id_index' => 'ADD INDEX `user_id_index` (`user_id`)',
|
||||
'user_ip_index' => 'ADD INDEX `user_ip_index` (`user_ip`)',
|
||||
'status_index' => 'ADD INDEX `status_index` (`status`)',
|
||||
'is_read_index' => 'ADD INDEX `is_read_index` (`is_read`)',
|
||||
'created_at_gmt_index' => 'ADD INDEX `created_at_gmt_index` (`created_at_gmt`)',
|
||||
'updated_at_gmt_index' => 'ADD INDEX `updated_at_gmt_index` (`updated_at_gmt`)',
|
||||
'created_at_index' => 'ADD INDEX `created_at_index` (`created_at`)',
|
||||
'updated_at_index' => 'ADD INDEX `updated_at_index` (`updated_at`)',
|
||||
'referer_index' => "ADD INDEX `referer_index` (`referer` ({$max_index_length}))",
|
||||
'referer_title_index' => "ADD INDEX `referer_title_index` (`referer_title` ({$max_index_length}))",
|
||||
] ),
|
||||
$this->query->get_table_submissions_values() => new Collection( [
|
||||
'submission_id_index' => 'ADD INDEX `submission_id_index` (`submission_id`)',
|
||||
'key_index' => "ADD INDEX `key_index` (`key` ({$max_index_length}))",
|
||||
] ),
|
||||
$this->query->get_table_form_actions_log() => new Collection( [
|
||||
'submission_id_index' => 'ADD INDEX `submission_id_index` (`submission_id`)',
|
||||
'action_name_index' => "ADD INDEX `action_name_index` (`action_name` ({$max_index_length}))",
|
||||
'status_index' => 'ADD INDEX `status_index` (`status`)',
|
||||
'created_at_gmt_index' => 'ADD INDEX `created_at_gmt_index` (`created_at_gmt`)',
|
||||
'updated_at_gmt_index' => 'ADD INDEX `updated_at_gmt_index` (`updated_at_gmt`)',
|
||||
'created_at_index' => 'ADD INDEX `created_at_index` (`created_at`)',
|
||||
'updated_at_index' => 'ADD INDEX `updated_at_index` (`updated_at`)',
|
||||
] ),
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\Forms\Submissions\Database\Migrations;
|
||||
|
||||
class Initial extends Base_Migration {
|
||||
public function run() {
|
||||
$this->create_tables();
|
||||
$this->add_indexes();
|
||||
}
|
||||
|
||||
private function create_tables() {
|
||||
$charset_collate = $this->wpdb->get_charset_collate();
|
||||
|
||||
$e_submission_table = "CREATE TABLE `{$this->query->get_table_submissions()}` (
|
||||
id bigint(20) unsigned auto_increment primary key,
|
||||
type varchar(60) null,
|
||||
hash_id varchar(60) not null,
|
||||
main_meta_id bigint(20) unsigned not null comment 'Id of main field. to represent the main meta field',
|
||||
post_id bigint(20) unsigned not null,
|
||||
referer varchar(500) not null,
|
||||
element_id varchar(20) not null,
|
||||
form_name varchar(60) not null,
|
||||
campaign_id bigint(20) unsigned not null,
|
||||
user_id bigint(20) unsigned null,
|
||||
user_ip varchar(46) not null,
|
||||
user_agent text not null,
|
||||
actions_count INT DEFAULT 0,
|
||||
actions_succeeded_count INT DEFAULT 0,
|
||||
status varchar(20) not null,
|
||||
is_read tinyint(1) default 0 not null,
|
||||
meta text null,
|
||||
created_at_gmt datetime not null,
|
||||
updated_at_gmt datetime not null,
|
||||
created_at datetime not null,
|
||||
updated_at datetime not null
|
||||
) {$charset_collate};";
|
||||
|
||||
$e_submission_values_table = "CREATE TABLE `{$this->query->get_table_submissions_values()}` (
|
||||
id bigint(20) unsigned auto_increment primary key,
|
||||
submission_id bigint(20) unsigned not null default 0,
|
||||
`key` varchar(60) null,
|
||||
value longtext null
|
||||
) {$charset_collate};";
|
||||
|
||||
$e_submission_actions_log_table = "CREATE TABLE `{$this->query->get_table_form_actions_log()}` (
|
||||
id bigint(20) unsigned auto_increment primary key,
|
||||
submission_id bigint(20) unsigned not null,
|
||||
action_name varchar(60) not null,
|
||||
action_label varchar(60) null,
|
||||
status varchar(20) not null,
|
||||
log text null,
|
||||
created_at_gmt datetime not null,
|
||||
updated_at_gmt datetime not null,
|
||||
created_at datetime not null,
|
||||
updated_at datetime not null
|
||||
) {$charset_collate};";
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
dbDelta( $e_submission_table . $e_submission_values_table . $e_submission_actions_log_table );
|
||||
}
|
||||
|
||||
private function add_indexes() {
|
||||
// phpcs:disable
|
||||
$this->wpdb->query( "ALTER TABLE `{$this->query->get_table_submissions()}`
|
||||
ADD INDEX `main_meta_id_index` (`main_meta_id`),
|
||||
ADD UNIQUE INDEX `hash_id_unique_index` (`hash_id`),
|
||||
ADD INDEX `hash_id_index` (`hash_id`),
|
||||
ADD INDEX `type_index` (`type`),
|
||||
ADD INDEX `post_id_index` (`post_id`),
|
||||
ADD INDEX `element_id_index` (`element_id`),
|
||||
ADD INDEX `campaign_id_index` (`campaign_id`),
|
||||
ADD INDEX `user_id_index` (`user_id`),
|
||||
ADD INDEX `user_ip_index` (`user_ip`),
|
||||
ADD INDEX `status_index` (`status`),
|
||||
ADD INDEX `is_read_index` (`is_read`),
|
||||
ADD INDEX `created_at_gmt_index` (`created_at_gmt`),
|
||||
ADD INDEX `updated_at_gmt_index` (`updated_at_gmt`),
|
||||
ADD INDEX `created_at_index` (`created_at`),
|
||||
ADD INDEX `updated_at_index` (`updated_at`)
|
||||
" );
|
||||
|
||||
$this->wpdb->query( "ALTER TABLE `{$this->query->get_table_submissions_values()}`
|
||||
ADD INDEX `submission_id_index` (`submission_id`),
|
||||
ADD INDEX `key_index` (`key`)
|
||||
" );
|
||||
|
||||
$this->wpdb->query( "ALTER TABLE `{$this->query->get_table_form_actions_log()}`
|
||||
ADD INDEX `submission_id_index` (`submission_id`),
|
||||
ADD INDEX `action_name_index` (`action_name`),
|
||||
ADD INDEX `status_index` (`status`),
|
||||
ADD INDEX `created_at_gmt_index` (`created_at_gmt`),
|
||||
ADD INDEX `updated_at_gmt_index` (`updated_at_gmt`),
|
||||
ADD INDEX `created_at_index` (`created_at`),
|
||||
ADD INDEX `updated_at_index` (`updated_at`)
|
||||
" );
|
||||
// phpcs:enable
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
namespace ElementorPro\Modules\Forms\Submissions\Database\Migrations;
|
||||
|
||||
class Referer_Extra extends Base_Migration {
|
||||
public function run() {
|
||||
$max_index_length = static::MAX_INDEX_LENGTH;
|
||||
|
||||
// phpcs:disable
|
||||
$this->wpdb->query("
|
||||
ALTER TABLE `{$this->query->get_table_submissions()}`
|
||||
ADD COLUMN `referer_title` varchar(300) null AFTER `referer`;
|
||||
");
|
||||
|
||||
$this->wpdb->query("
|
||||
ALTER TABLE `{$this->query->get_table_submissions()}`
|
||||
ADD INDEX `referer_index` (`referer`({$max_index_length})),
|
||||
ADD INDEX `referer_title_index` (`referer_title`({$max_index_length}));
|
||||
");
|
||||
// phpcs:enable
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user