| 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 |
* Adds customerName column to the payments table for the Transactions admin list. |
| 11 |
*/ |
| 12 |
class AddCustomerNameToPayments |
| 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 |
|
| 24 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 25 |
$columnExists = $wpdb->get_results( |
| 26 |
"SHOW COLUMNS FROM `{$tableName}` LIKE 'customerName'" |
| 27 |
); |
| 28 |
|
| 29 |
if (!empty($columnExists)) { |
| 30 |
return true; |
| 31 |
} |
| 32 |
|
| 33 |
$alterQuery = "ALTER TABLE `{$tableName}` ADD COLUMN `customerName` VARCHAR(255) NULL AFTER `paymentMethod`"; |
| 34 |
|
| 35 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.PreparedSQL.NotPrepared |
| 36 |
$result = $wpdb->query($alterQuery); |
| 37 |
|
| 38 |
if ($result === false) { |
| 39 |
error_log(sprintf( |
| 40 |
BackendStrings::getExceptionStrings()['migration_error_add_column'], |
| 41 |
'customerName', |
| 42 |
$tableName |
| 43 |
)); |
| 44 |
return false; |
| 45 |
} |
| 46 |
|
| 47 |
return true; |
| 48 |
} |
| 49 |
} |
| 50 |
|