| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Modules\Payments\Migrations; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Exit if accessed directly. |
| 7 |
} |
| 8 |
|
| 9 |
class Transactions |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Migrate the table. |
| 13 |
* |
| 14 |
* @return void |
| 15 |
*/ |
| 16 |
public static function migrate() |
| 17 |
{ |
| 18 |
global $wpdb; |
| 19 |
|
| 20 |
$charsetCollate = $wpdb->get_charset_collate(); |
| 21 |
|
| 22 |
$table = $wpdb->prefix . 'fluentform_transactions'; |
| 23 |
|
| 24 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Migration file, direct query needed |
| 25 |
if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) != $table) { |
| 26 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.SchemaChange -- Migration file, schema change is the purpose |
| 27 |
$sql = "CREATE TABLE $table ( |
| 28 |
id int(11) NOT NULL AUTO_INCREMENT, |
| 29 |
transaction_hash varchar(255) NULL, |
| 30 |
payer_name varchar(255) NULL, |
| 31 |
payer_email varchar(255) NULL, |
| 32 |
billing_address varchar(255) NULL, |
| 33 |
shipping_address varchar(255) NULL, |
| 34 |
form_id int(11) NOT NULL, |
| 35 |
user_id int(11) DEFAULT NULL, |
| 36 |
submission_id int(11) NULL, |
| 37 |
subscription_id int(11) NULL, |
| 38 |
transaction_type varchar(255) DEFAULT 'onetime', |
| 39 |
payment_method varchar(255), |
| 40 |
card_last_4 int(4), |
| 41 |
card_brand varchar(255), |
| 42 |
charge_id varchar(255), |
| 43 |
payment_total BIGINT UNSIGNED DEFAULT 1, |
| 44 |
status varchar(255), |
| 45 |
currency varchar(255), |
| 46 |
payment_mode varchar(255), |
| 47 |
payment_note longtext, |
| 48 |
created_at timestamp NULL, |
| 49 |
updated_at timestamp NULL, |
| 50 |
PRIMARY KEY (id), |
| 51 |
KEY ff_txn_created_at (created_at), |
| 52 |
KEY ff_txn_form_created (form_id, created_at) |
| 53 |
) $charsetCollate;"; |
| 54 |
|
| 55 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 56 |
|
| 57 |
dbDelta($sql); |
| 58 |
} else { |
| 59 |
self::maybeAlterColumns(); |
| 60 |
} |
| 61 |
|
| 62 |
self::maybeAddIndexes(); |
| 63 |
} |
| 64 |
|
| 65 |
public static function maybeAddIndexes() |
| 66 |
{ |
| 67 |
global $wpdb; |
| 68 |
$table = $wpdb->prefix . 'fluentform_transactions'; |
| 69 |
|
| 70 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, reading index metadata, %1s is for identifier |
| 71 |
$indexes = $wpdb->get_results($wpdb->prepare('SHOW INDEX FROM %1s', $table)); |
| 72 |
$existing = []; |
| 73 |
if (is_array($indexes)) { |
| 74 |
foreach ($indexes as $index) { |
| 75 |
$existing[$index->Key_name] = true; |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
// Reports and the MCP payment/trend tools filter transactions by created_at |
| 80 |
// (cross-form) and by (form_id, created_at) (form-scoped); neither had an |
| 81 |
// index, so every summary scanned the whole table. Additive + idempotent. |
| 82 |
if (!isset($existing['ff_txn_created_at'])) { |
| 83 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, schema change is the purpose, %1s is for identifier |
| 84 |
$wpdb->query($wpdb->prepare('ALTER TABLE %1s ADD INDEX ff_txn_created_at (created_at)', $table)); |
| 85 |
} |
| 86 |
|
| 87 |
if (!isset($existing['ff_txn_form_created'])) { |
| 88 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, schema change is the purpose, %1s is for identifier |
| 89 |
$wpdb->query($wpdb->prepare('ALTER TABLE %1s ADD INDEX ff_txn_form_created (form_id, created_at)', $table)); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public static function maybeAlterColumns() |
| 94 |
{ |
| 95 |
global $wpdb; |
| 96 |
$table = $wpdb->prefix . 'fluentform_transactions'; |
| 97 |
|
| 98 |
// find the data types of each column |
| 99 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, checking table structure, %1s is for identifier |
| 100 |
$results = $wpdb->get_results($wpdb->prepare('DESCRIBE %1s', $table)); |
| 101 |
$items = []; |
| 102 |
foreach ($results as $result) { |
| 103 |
$items[$result->Field] = $result->Type; |
| 104 |
} |
| 105 |
|
| 106 |
$paymentTotalMigrated = strpos($items['payment_total'], 'bigint') !== false; |
| 107 |
if (!$paymentTotalMigrated) { |
| 108 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.PreparedSQLPlaceholders.UnquotedComplexPlaceholder -- Migration file, schema change is the purpose, %1s is for identifier |
| 109 |
$wpdb->query($wpdb->prepare('ALTER TABLE %1s MODIFY payment_total BIGINT UNSIGNED DEFAULT 1', $table)); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|