PluginProbe
The Innovative Form Builder – IvyForms / 1.4
The Innovative Form Builder – IvyForms v1.4
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / backend / src / Services / InstallActions / Migrations / AddCcBccToNotifications.php

AddCcBccToNotifications.php in The Innovative Form Builder – IvyForms 1.4, at backend/src/Services/InstallActions/Migrations/AddCcBccToNotifications.php

107 lines 2.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace IvyForms\Services\InstallActions\Migrations;
4
5 use IvyForms\Services\InstallActions\DB\Notification\NotificationsTable;
6 use IvyForms\Services\Translations\BackendStrings;
7
8 class AddCcBccToNotifications
9 {
10 /**
11 * Run the migration
12 *
13 * @return bool
14 */
15 public static function run(): bool
16 {
17 global $wpdb;
18
19 $tableName = NotificationsTable::getTableName();
20
21 $ccColumnExists = self::columnExists($wpdb, $tableName, 'cc');
22 $bccColumnExists = self::columnExists($wpdb, $tableName, 'bcc');
23
24 if ($ccColumnExists === null || $bccColumnExists === null) {
25 error_log(sprintf(
26 BackendStrings::getExceptionStrings()['migration_error_add_column'],
27 'cc/bcc',
28 $tableName
29 ));
30
31 return false;
32 }
33
34 $columnsToAdd = [];
35 if (!$ccColumnExists) {
36 $columnsToAdd[] = '`cc` VARCHAR(500) NULL AFTER `receiver`';
37 }
38 if (!$bccColumnExists) {
39 $columnsToAdd[] = '`bcc` VARCHAR(500) NULL AFTER `cc`';
40 }
41
42 if ($columnsToAdd === []) {
43 return true;
44 }
45
46 $alterQuery = "ALTER TABLE `{$tableName}` ADD COLUMN "
47 . implode(', ADD COLUMN ', $columnsToAdd);
48
49 $result = $wpdb->query($alterQuery);
50
51 if ($result === false) {
52 error_log(sprintf(
53 BackendStrings::getExceptionStrings()['migration_error_add_column'],
54 'cc/bcc',
55 $tableName
56 ));
57 return false;
58 }
59
60 return true;
61 }
62
63 public static function rollback(): bool
64 {
65 global $wpdb;
66
67 $tableName = NotificationsTable::getTableName();
68
69 $ccExists = self::columnExists($wpdb, $tableName, 'cc');
70 if ($ccExists === null) {
71 return false;
72 }
73
74 if ($ccExists && $wpdb->query("ALTER TABLE `{$tableName}` DROP COLUMN `cc`") === false) {
75 return false;
76 }
77
78 $bccExists = self::columnExists($wpdb, $tableName, 'bcc');
79 if ($bccExists === null) {
80 return false;
81 }
82
83 if ($bccExists && $wpdb->query("ALTER TABLE `{$tableName}` DROP COLUMN `bcc`") === false) {
84 return false;
85 }
86
87 return true;
88 }
89
90 /**
91 * @param \wpdb $wpdb
92 * @return bool|null True when present, false when absent, null when lookup failed
93 */
94 private static function columnExists($wpdb, string $tableName, string $column): ?bool
95 {
96 $results = $wpdb->get_results(
97 "SHOW COLUMNS FROM `{$tableName}` LIKE '{$column}'"
98 );
99
100 if ($results === null) {
101 return null;
102 }
103
104 return !empty($results);
105 }
106 }
107