ivyforms
/
backend
/
src
/
Services
/
InstallActions
/
Migrations
/
AddPaymentStatusAndNullableEntryId.php
AddPaymentStatusAndNullableEntryId.php in The Innovative Form Builder – IvyForms 1.5, at backend/src/Services/InstallActions/Migrations/AddPaymentStatusAndNullableEntryId.php
| 1 | <?php |
| 2 | |
| 3 | namespace IvyForms\Services\InstallActions\Migrations; |
| 4 | |
| 5 | use IvyForms\Common\Exceptions\InvalidArgumentException; |
| 6 | use IvyForms\Services\InstallActions\DB\Payment\PaymentsTable; |
| 7 | use IvyForms\Services\Translations\BackendStrings; |
| 8 | |
| 9 | /** |
| 10 | * Makes payments.entryId nullable and adds status for failed/orphan rows. |
| 11 | */ |
| 12 | class AddPaymentStatusAndNullableEntryId |
| 13 | { |
| 14 | /** |
| 15 | * @return bool |
| 16 | * @throws InvalidArgumentException |
| 17 | */ |
| 18 | public static function run(): bool |
| 19 | { |
| 20 | global $wpdb; |
| 21 | |
| 22 | $tableName = PaymentsTable::getTableName(); |
| 23 | $ok = true; |
| 24 | |
| 25 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 26 | $entryIdColumn = $wpdb->get_row("SHOW COLUMNS FROM `{$tableName}` LIKE 'entryId'", ARRAY_A); |
| 27 | if (is_array($entryIdColumn) && strtoupper((string) ($entryIdColumn['Null'] ?? '')) !== 'YES') { |
| 28 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 29 | $result = $wpdb->query( |
| 30 | "ALTER TABLE `{$tableName}` MODIFY COLUMN `entryId` INT(11) NULL" |
| 31 | ); |
| 32 | if ($result === false) { |
| 33 | error_log(sprintf( |
| 34 | BackendStrings::getExceptionStrings()['migration_error_add_column'], |
| 35 | 'entryId (nullable)', |
| 36 | $tableName |
| 37 | )); |
| 38 | $ok = false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 43 | $statusExists = $wpdb->get_results("SHOW COLUMNS FROM `{$tableName}` LIKE 'status'"); |
| 44 | if (empty($statusExists)) { |
| 45 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 46 | $alterStatus = "ALTER TABLE `{$tableName}` " |
| 47 | . "ADD COLUMN `status` VARCHAR(50) NOT NULL DEFAULT 'succeeded' AFTER `squareReceiptUrl`"; |
| 48 | $result = $wpdb->query($alterStatus); |
| 49 | if ($result === false) { |
| 50 | error_log(sprintf( |
| 51 | BackendStrings::getExceptionStrings()['migration_error_add_column'], |
| 52 | 'status', |
| 53 | $tableName |
| 54 | )); |
| 55 | $ok = false; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 60 | $statusIndex = $wpdb->get_var("SHOW INDEX FROM `{$tableName}` WHERE Key_name = 'status'"); |
| 61 | if ($statusIndex === null) { |
| 62 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 63 | $result = $wpdb->query("ALTER TABLE `{$tableName}` ADD KEY `status` (`status`)"); |
| 64 | if ($result === false) { |
| 65 | error_log(sprintf( |
| 66 | BackendStrings::getExceptionStrings()['migration_error_add_column'], |
| 67 | 'status (index)', |
| 68 | $tableName |
| 69 | )); |
| 70 | $ok = false; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return $ok; |
| 75 | } |
| 76 | } |
| 77 |