Completed
Branch master (465a38)
by
unknown
27:45 queued 22:40
created
core/data_migration_scripts/EE_DMS_Core_4_7_0.dms.php 2 patches
Indentation   +186 added lines, -186 removed lines patch added patch discarded remove patch
@@ -10,9 +10,9 @@  discard block
 block discarded – undo
10 10
 $stages            = glob(EE_CORE . 'data_migration_scripts/4_7_0_stages/*');
11 11
 $class_to_filepath = [];
12 12
 foreach ($stages as $filepath) {
13
-    $matches = [];
14
-    preg_match('~4_7_0_stages/(.*).dmsstage.php~', $filepath, $matches);
15
-    $class_to_filepath[ $matches[1] ] = $filepath;
13
+	$matches = [];
14
+	preg_match('~4_7_0_stages/(.*).dmsstage.php~', $filepath, $matches);
15
+	$class_to_filepath[ $matches[1] ] = $filepath;
16 16
 }
17 17
 // give addons a chance to autoload their stages too
18 18
 $class_to_filepath = apply_filters('FHEE__EE_DMS_4_7_0__autoloaded_stages', $class_to_filepath);
@@ -35,71 +35,71 @@  discard block
 block discarded – undo
35 35
 class EE_DMS_Core_4_7_0 extends EE_Data_Migration_Script_Base
36 36
 {
37 37
 
38
-    /**
39
-     * return EE_DMS_Core_4_7_0
40
-     *
41
-     * @param TableManager|null  $table_manager
42
-     * @param TableAnalysis|null $table_analysis
43
-     */
44
-    public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
45
-    {
46
-        $this->_pretty_name      = esc_html__("Data Update to Event Espresso 4.7.0", "event_espresso");
47
-        $this->_priority         = 10;
48
-        $this->_migration_stages = [
49
-            new EE_DMS_4_7_0_Add_Taxes_To_REG_Final_Price(),
50
-            new EE_DMS_4_7_0_Registration_Payments(),
51
-        ];
52
-        parent::__construct($table_manager, $table_analysis);
53
-    }
54
-
55
-
56
-    /**
57
-     * @param array $version_array
58
-     * @return bool
59
-     * @throws EE_Error
60
-     */
61
-    public function can_migrate_from_version($version_array)
62
-    {
63
-        $version_string = $version_array['Core'];
64
-        if (
65
-            (version_compare($version_string, '4.7.0.decaf', '<') &&
66
-             version_compare($version_string, '4.6.0.decaf', '>=')) ||
67
-            (version_compare($version_string, '4.7.0.decaf', '>=') &&
68
-             ! $this->_get_table_analysis()->tableExists('esp_registration_payment') &&
69
-             $this->_get_table_analysis()->tableExists('esp_registration'))
70
-        ) {
71
-            return true;
72
-        } elseif (! $version_string) {
73
-            // no version string provided... this must be pre 4.3
74
-            return false;// changed mind. dont want people thinking they should migrate yet because they cant
75
-        } else {
76
-            return false;
77
-        }
78
-    }
79
-
80
-
81
-    /**
82
-     * @return bool
83
-     * @throws EE_Error
84
-     * @throws ReflectionException
85
-     */
86
-    public function schema_changes_before_migration()
87
-    {
88
-        // relies on 4.1's EEH_Activation::create_table
89
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
90
-
91
-        $table_name = 'esp_answer';
92
-        $sql        = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
38
+	/**
39
+	 * return EE_DMS_Core_4_7_0
40
+	 *
41
+	 * @param TableManager|null  $table_manager
42
+	 * @param TableAnalysis|null $table_analysis
43
+	 */
44
+	public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
45
+	{
46
+		$this->_pretty_name      = esc_html__("Data Update to Event Espresso 4.7.0", "event_espresso");
47
+		$this->_priority         = 10;
48
+		$this->_migration_stages = [
49
+			new EE_DMS_4_7_0_Add_Taxes_To_REG_Final_Price(),
50
+			new EE_DMS_4_7_0_Registration_Payments(),
51
+		];
52
+		parent::__construct($table_manager, $table_analysis);
53
+	}
54
+
55
+
56
+	/**
57
+	 * @param array $version_array
58
+	 * @return bool
59
+	 * @throws EE_Error
60
+	 */
61
+	public function can_migrate_from_version($version_array)
62
+	{
63
+		$version_string = $version_array['Core'];
64
+		if (
65
+			(version_compare($version_string, '4.7.0.decaf', '<') &&
66
+			 version_compare($version_string, '4.6.0.decaf', '>=')) ||
67
+			(version_compare($version_string, '4.7.0.decaf', '>=') &&
68
+			 ! $this->_get_table_analysis()->tableExists('esp_registration_payment') &&
69
+			 $this->_get_table_analysis()->tableExists('esp_registration'))
70
+		) {
71
+			return true;
72
+		} elseif (! $version_string) {
73
+			// no version string provided... this must be pre 4.3
74
+			return false;// changed mind. dont want people thinking they should migrate yet because they cant
75
+		} else {
76
+			return false;
77
+		}
78
+	}
79
+
80
+
81
+	/**
82
+	 * @return bool
83
+	 * @throws EE_Error
84
+	 * @throws ReflectionException
85
+	 */
86
+	public function schema_changes_before_migration()
87
+	{
88
+		// relies on 4.1's EEH_Activation::create_table
89
+		require_once(EE_HELPERS . 'EEH_Activation.helper.php');
90
+
91
+		$table_name = 'esp_answer';
92
+		$sql        = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
93 93
 					REG_ID int(10) unsigned NOT NULL,
94 94
 					QST_ID int(10) unsigned NOT NULL,
95 95
 					ANS_value text NOT NULL,
96 96
 					PRIMARY KEY  (ANS_ID),
97 97
 					KEY REG_ID (REG_ID),
98 98
 					KEY QST_ID (QST_ID)";
99
-        $this->_table_is_changed_in_this_version($table_name, $sql);
99
+		$this->_table_is_changed_in_this_version($table_name, $sql);
100 100
 
101
-        $table_name = 'esp_attendee_meta';
102
-        $sql        = "ATTM_ID int(10) unsigned NOT	NULL AUTO_INCREMENT,
101
+		$table_name = 'esp_attendee_meta';
102
+		$sql        = "ATTM_ID int(10) unsigned NOT	NULL AUTO_INCREMENT,
103 103
 						ATT_ID bigint(20) unsigned NOT NULL,
104 104
 						ATT_fname varchar(45) NOT NULL,
105 105
 						ATT_lname varchar(45) NOT	NULL,
@@ -116,10 +116,10 @@  discard block
 block discarded – undo
116 116
 								KEY ATT_email (ATT_email(191)),
117 117
 								KEY ATT_lname (ATT_lname),
118 118
 								KEY ATT_fname (ATT_fname)";
119
-        $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB');
119
+		$this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB');
120 120
 
121
-        $table_name = 'esp_country';
122
-        $sql        = "CNT_ISO varchar(2) COLLATE utf8_bin NOT NULL,
121
+		$table_name = 'esp_country';
122
+		$sql        = "CNT_ISO varchar(2) COLLATE utf8_bin NOT NULL,
123 123
 					  CNT_ISO3 varchar(3) COLLATE utf8_bin NOT NULL,
124 124
 					  RGN_ID tinyint(3) unsigned DEFAULT NULL,
125 125
 					  CNT_name varchar(45) COLLATE utf8_bin NOT NULL,
@@ -135,20 +135,20 @@  discard block
 block discarded – undo
135 135
 					  CNT_is_EU tinyint(1) DEFAULT '0',
136 136
 					  CNT_active tinyint(1) DEFAULT '0',
137 137
 					  PRIMARY KEY  (CNT_ISO)";
138
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
138
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
139 139
 
140
-        $table_name = 'esp_currency';
141
-        $sql        = "CUR_code varchar(6) COLLATE utf8_bin NOT NULL,
140
+		$table_name = 'esp_currency';
141
+		$sql        = "CUR_code varchar(6) COLLATE utf8_bin NOT NULL,
142 142
 				CUR_single varchar(45) COLLATE utf8_bin DEFAULT 'dollar',
143 143
 				CUR_plural varchar(45) COLLATE utf8_bin DEFAULT 'dollars',
144 144
 				CUR_sign varchar(45) COLLATE utf8_bin DEFAULT '$',
145 145
 				CUR_dec_plc varchar(1) COLLATE utf8_bin NOT NULL DEFAULT '2',
146 146
 				CUR_active tinyint(1) DEFAULT '0',
147 147
 				PRIMARY KEY  (CUR_code)";
148
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
148
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
149 149
 
150
-        $table_name = 'esp_datetime';
151
-        $sql        = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
150
+		$table_name = 'esp_datetime';
151
+		$sql        = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
152 152
 				  EVT_ID bigint(20) unsigned NOT NULL,
153 153
 				  DTT_name varchar(255) NOT NULL DEFAULT '',
154 154
 				  DTT_description text NOT NULL,
@@ -164,10 +164,10 @@  discard block
 block discarded – undo
164 164
 						KEY DTT_EVT_start (DTT_EVT_start),
165 165
 						KEY EVT_ID (EVT_ID),
166 166
 						KEY DTT_is_primary (DTT_is_primary)";
167
-        $this->_table_is_changed_in_this_version($table_name, $sql);
167
+		$this->_table_is_changed_in_this_version($table_name, $sql);
168 168
 
169
-        $table_name = 'esp_event_meta';
170
-        $sql        = "
169
+		$table_name = 'esp_event_meta';
170
+		$sql        = "
171 171
 			EVTM_ID int(10) NOT NULL AUTO_INCREMENT,
172 172
 			EVT_ID bigint(20) unsigned NOT NULL,
173 173
 			EVT_display_desc tinyint(1) unsigned NOT NULL DEFAULT 1,
@@ -183,38 +183,38 @@  discard block
 block discarded – undo
183 183
 			EVT_donations tinyint(1) NULL,
184 184
 			PRIMARY KEY  (EVTM_ID),
185 185
 			KEY EVT_ID (EVT_ID)";
186
-        $this->_table_is_changed_in_this_version($table_name, $sql);
186
+		$this->_table_is_changed_in_this_version($table_name, $sql);
187 187
 
188
-        $table_name = 'esp_event_question_group';
189
-        $sql        = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
188
+		$table_name = 'esp_event_question_group';
189
+		$sql        = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
190 190
 					EVT_ID bigint(20) unsigned NOT NULL,
191 191
 					QSG_ID int(10) unsigned NOT NULL,
192 192
 					EQG_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
193 193
 					PRIMARY KEY  (EQG_ID),
194 194
 					KEY EVT_ID (EVT_ID),
195 195
 					KEY QSG_ID (QSG_ID)";
196
-        $this->_table_is_changed_in_this_version($table_name, $sql);
196
+		$this->_table_is_changed_in_this_version($table_name, $sql);
197 197
 
198
-        $table_name = 'esp_event_venue';
199
-        $sql        = "EVV_ID int(11) NOT NULL AUTO_INCREMENT,
198
+		$table_name = 'esp_event_venue';
199
+		$sql        = "EVV_ID int(11) NOT NULL AUTO_INCREMENT,
200 200
 				EVT_ID bigint(20) unsigned NOT NULL,
201 201
 				VNU_ID bigint(20) unsigned NOT NULL,
202 202
 				EVV_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
203 203
 				PRIMARY KEY  (EVV_ID)";
204
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
204
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
205 205
 
206
-        $table_name = 'esp_extra_meta';
207
-        $sql        = "EXM_ID int(11) NOT NULL AUTO_INCREMENT,
206
+		$table_name = 'esp_extra_meta';
207
+		$sql        = "EXM_ID int(11) NOT NULL AUTO_INCREMENT,
208 208
 				OBJ_ID int(11) DEFAULT NULL,
209 209
 				EXM_type varchar(45) DEFAULT NULL,
210 210
 				EXM_key varchar(45) DEFAULT NULL,
211 211
 				EXM_value text,
212 212
 				PRIMARY KEY  (EXM_ID),
213 213
 				KEY EXM_type (EXM_type, OBJ_ID, EXM_key(45))";
214
-        $this->_table_is_changed_in_this_version($table_name, $sql);
214
+		$this->_table_is_changed_in_this_version($table_name, $sql);
215 215
 
216
-        $table_name = 'esp_line_item';
217
-        $sql        = "LIN_ID int(11) NOT NULL AUTO_INCREMENT,
216
+		$table_name = 'esp_line_item';
217
+		$sql        = "LIN_ID int(11) NOT NULL AUTO_INCREMENT,
218 218
 				LIN_code varchar(245) NOT NULL DEFAULT '',
219 219
 				TXN_ID int(11) DEFAULT NULL,
220 220
 				LIN_name varchar(245) NOT NULL DEFAULT '',
@@ -232,10 +232,10 @@  discard block
 block discarded – undo
232 232
 				PRIMARY KEY  (LIN_ID),
233 233
 				KEY LIN_code (LIN_code(191)),
234 234
 				KEY TXN_ID (TXN_ID)";
235
-        $this->_table_is_changed_in_this_version($table_name, $sql);
235
+		$this->_table_is_changed_in_this_version($table_name, $sql);
236 236
 
237
-        $table_name = 'esp_log';
238
-        $sql        = "LOG_ID int(11) NOT NULL AUTO_INCREMENT,
237
+		$table_name = 'esp_log';
238
+		$sql        = "LOG_ID int(11) NOT NULL AUTO_INCREMENT,
239 239
 				LOG_time datetime DEFAULT NULL,
240 240
 				OBJ_ID varchar(45) DEFAULT NULL,
241 241
 				OBJ_type varchar(45) DEFAULT NULL,
@@ -246,20 +246,20 @@  discard block
 block discarded – undo
246 246
 				KEY LOG_time (LOG_time),
247 247
 				KEY OBJ (OBJ_type,OBJ_ID),
248 248
 				KEY LOG_type (LOG_type)";
249
-        $this->_table_is_changed_in_this_version($table_name, $sql);
249
+		$this->_table_is_changed_in_this_version($table_name, $sql);
250 250
 
251
-        $table_name = 'esp_message_template';
252
-        $sql        = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
251
+		$table_name = 'esp_message_template';
252
+		$sql        = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
253 253
 					GRP_ID int(10) unsigned NOT NULL,
254 254
 					MTP_context varchar(50) NOT NULL,
255 255
 					MTP_template_field varchar(30) NOT NULL,
256 256
 					MTP_content text NOT NULL,
257 257
 					PRIMARY KEY  (MTP_ID),
258 258
 					KEY GRP_ID (GRP_ID)";
259
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
259
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
260 260
 
261
-        $table_name = 'esp_message_template_group';
262
-        $sql        = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
261
+		$table_name = 'esp_message_template_group';
262
+		$sql        = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
263 263
 					MTP_user_id int(10) NOT NULL DEFAULT '1',
264 264
 					MTP_name varchar(245) NOT NULL DEFAULT '',
265 265
 					MTP_description varchar(245) NOT NULL DEFAULT '',
@@ -271,19 +271,19 @@  discard block
 block discarded – undo
271 271
 					MTP_is_active tinyint(1) NOT NULL DEFAULT '1',
272 272
 					PRIMARY KEY  (GRP_ID),
273 273
 					KEY MTP_user_id (MTP_user_id)";
274
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
274
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
275 275
 
276
-        $table_name = 'esp_event_message_template';
277
-        $sql        = "EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
276
+		$table_name = 'esp_event_message_template';
277
+		$sql        = "EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
278 278
 					EVT_ID bigint(20) unsigned NOT NULL DEFAULT 0,
279 279
 					GRP_ID int(10) unsigned NOT NULL DEFAULT 0,
280 280
 					PRIMARY KEY  (EMT_ID),
281 281
 					KEY EVT_ID (EVT_ID),
282 282
 					KEY GRP_ID (GRP_ID)";
283
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
283
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
284 284
 
285
-        $table_name = 'esp_payment';
286
-        $sql        = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
285
+		$table_name = 'esp_payment';
286
+		$sql        = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
287 287
 					TXN_ID int(10) unsigned DEFAULT NULL,
288 288
 					STS_ID varchar(3) COLLATE utf8_bin DEFAULT NULL,
289 289
 					PAY_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
@@ -300,10 +300,10 @@  discard block
 block discarded – undo
300 300
 					PRIMARY KEY  (PAY_ID),
301 301
 					KEY PAY_timestamp (PAY_timestamp),
302 302
 					KEY TXN_ID (TXN_ID)";
303
-        $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB');
303
+		$this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB');
304 304
 
305
-        $table_name = 'esp_payment_method';
306
-        $sql        = "PMD_ID int(11) NOT NULL AUTO_INCREMENT,
305
+		$table_name = 'esp_payment_method';
306
+		$sql        = "PMD_ID int(11) NOT NULL AUTO_INCREMENT,
307 307
 				PMD_type varchar(124) DEFAULT NULL,
308 308
 				PMD_name varchar(255) DEFAULT NULL,
309 309
 				PMD_desc text,
@@ -319,36 +319,36 @@  discard block
 block discarded – undo
319 319
 				PRIMARY KEY  (PMD_ID),
320 320
 				UNIQUE KEY PMD_slug_UNIQUE (PMD_slug),
321 321
 				KEY PMD_type (PMD_type)";
322
-        $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB');
322
+		$this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB');
323 323
 
324
-        $table_name = "esp_ticket_price";
325
-        $sql        = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
324
+		$table_name = "esp_ticket_price";
325
+		$sql        = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
326 326
 					  TKT_ID int(10) unsigned NOT NULL,
327 327
 					  PRC_ID int(10) unsigned NOT NULL,
328 328
 					  PRIMARY KEY  (TKP_ID),
329 329
 					  KEY TKT_ID (TKT_ID),
330 330
 					  KEY PRC_ID (PRC_ID)";
331
-        $this->_table_is_changed_in_this_version($table_name, $sql);
331
+		$this->_table_is_changed_in_this_version($table_name, $sql);
332 332
 
333
-        $table_name = "esp_datetime_ticket";
334
-        $sql        = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
333
+		$table_name = "esp_datetime_ticket";
334
+		$sql        = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
335 335
 					  DTT_ID int(10) unsigned NOT NULL,
336 336
 					  TKT_ID int(10) unsigned NOT NULL,
337 337
 					  PRIMARY KEY  (DTK_ID),
338 338
 					  KEY DTT_ID (DTT_ID),
339 339
 					  KEY TKT_ID (TKT_ID)";
340
-        $this->_table_is_changed_in_this_version($table_name, $sql);
340
+		$this->_table_is_changed_in_this_version($table_name, $sql);
341 341
 
342
-        $table_name = "esp_ticket_template";
343
-        $sql        = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
342
+		$table_name = "esp_ticket_template";
343
+		$sql        = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
344 344
 					  TTM_name varchar(45) NOT NULL,
345 345
 					  TTM_description text,
346 346
 					  TTM_file varchar(45),
347 347
 					  PRIMARY KEY  (TTM_ID)";
348
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
348
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
349 349
 
350
-        $table_name = 'esp_question';
351
-        $sql        = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
350
+		$table_name = 'esp_question';
351
+		$sql        = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
352 352
 					QST_display_text text NOT NULL,
353 353
 					QST_admin_label varchar(255) NOT NULL,
354 354
 					QST_system varchar(25) DEFAULT NULL,
@@ -361,20 +361,20 @@  discard block
 block discarded – undo
361 361
 					QST_deleted tinyint(2) unsigned NOT NULL DEFAULT 0,
362 362
 					PRIMARY KEY  (QST_ID),
363 363
 					KEY QST_order (QST_order)';
364
-        $this->_table_is_changed_in_this_version($table_name, $sql);
364
+		$this->_table_is_changed_in_this_version($table_name, $sql);
365 365
 
366
-        $table_name = 'esp_question_group_question';
367
-        $sql        = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
366
+		$table_name = 'esp_question_group_question';
367
+		$sql        = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
368 368
 					QSG_ID int(10) unsigned NOT NULL,
369 369
 					QST_ID int(10) unsigned NOT NULL,
370 370
 					QGQ_order int(10) unsigned NOT NULL DEFAULT 0,
371 371
 					PRIMARY KEY  (QGQ_ID),
372 372
 					KEY QST_ID (QST_ID),
373 373
 					KEY QSG_ID_order (QSG_ID, QGQ_order)";
374
-        $this->_table_is_changed_in_this_version($table_name, $sql);
374
+		$this->_table_is_changed_in_this_version($table_name, $sql);
375 375
 
376
-        $table_name = 'esp_question_option';
377
-        $sql        = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
376
+		$table_name = 'esp_question_option';
377
+		$sql        = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
378 378
 					QSO_value varchar(255) NOT NULL,
379 379
 					QSO_desc text NOT NULL,
380 380
 					QST_ID int(10) unsigned NOT NULL,
@@ -383,10 +383,10 @@  discard block
 block discarded – undo
383 383
 					PRIMARY KEY  (QSO_ID),
384 384
 					KEY QST_ID (QST_ID),
385 385
 					KEY QSO_order (QSO_order)";
386
-        $this->_table_is_changed_in_this_version($table_name, $sql);
386
+		$this->_table_is_changed_in_this_version($table_name, $sql);
387 387
 
388
-        $table_name = 'esp_registration';
389
-        $sql        = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
388
+		$table_name = 'esp_registration';
389
+		$sql        = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
390 390
 					  EVT_ID bigint(20) unsigned NOT NULL,
391 391
 					  ATT_ID bigint(20) unsigned NOT NULL,
392 392
 					  TXN_ID int(10) unsigned NOT NULL,
@@ -410,20 +410,20 @@  discard block
 block discarded – undo
410 410
 					  KEY TKT_ID (TKT_ID),
411 411
 					  KEY EVT_ID (EVT_ID),
412 412
 					  KEY STS_ID (STS_ID)";
413
-        $this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB');
413
+		$this->_table_is_changed_in_this_version($table_name, $sql, 'ENGINE=InnoDB');
414 414
 
415
-        $table_name = 'esp_registration_payment';
416
-        $sql        = "RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
415
+		$table_name = 'esp_registration_payment';
416
+		$sql        = "RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
417 417
 					  REG_ID int(10) unsigned NOT NULL,
418 418
 					  PAY_ID int(10) unsigned NULL,
419 419
 					  RPY_amount decimal(10,3) NOT NULL DEFAULT '0.00',
420 420
 					  PRIMARY KEY  (RPY_ID),
421 421
 					  KEY REG_ID (REG_ID),
422 422
 					  KEY PAY_ID (PAY_ID)";
423
-        $this->_table_is_new_in_this_version($table_name, $sql);
423
+		$this->_table_is_new_in_this_version($table_name, $sql);
424 424
 
425
-        $table_name = 'esp_checkin';
426
-        $sql        = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
425
+		$table_name = 'esp_checkin';
426
+		$sql        = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
427 427
 					REG_ID int(10) unsigned NOT NULL,
428 428
 					DTT_ID int(10) unsigned NOT NULL,
429 429
 					CHK_in tinyint(1) unsigned NOT NULL DEFAULT 1,
@@ -431,10 +431,10 @@  discard block
 block discarded – undo
431 431
 					PRIMARY KEY  (CHK_ID),
432 432
 					KEY REG_ID (REG_ID),
433 433
 					KEY DTT_ID (DTT_ID)";
434
-        $this->_table_is_changed_in_this_version($table_name, $sql);
434
+		$this->_table_is_changed_in_this_version($table_name, $sql);
435 435
 
436
-        $table_name = 'esp_state';
437
-        $sql        = "STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT,
436
+		$table_name = 'esp_state';
437
+		$sql        = "STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT,
438 438
 					  CNT_ISO varchar(2) COLLATE utf8_bin NOT NULL,
439 439
 					  STA_abbrev varchar(24) COLLATE utf8_bin NOT NULL,
440 440
 					  STA_name varchar(100) COLLATE utf8_bin NOT NULL,
@@ -442,10 +442,10 @@  discard block
 block discarded – undo
442 442
 					  PRIMARY KEY  (STA_ID),
443 443
 					  KEY STA_abbrev (STA_abbrev),
444 444
 					  KEY CNT_ISO (CNT_ISO)";
445
-        $this->_table_is_changed_in_this_version($table_name, $sql);
445
+		$this->_table_is_changed_in_this_version($table_name, $sql);
446 446
 
447
-        $table_name = 'esp_status';
448
-        $sql        = "STS_ID varchar(3) COLLATE utf8_bin NOT NULL,
447
+		$table_name = 'esp_status';
448
+		$sql        = "STS_ID varchar(3) COLLATE utf8_bin NOT NULL,
449 449
 					  STS_code varchar(45) COLLATE utf8_bin NOT NULL,
450 450
 					  STS_type set('event','registration','transaction','payment','email') COLLATE utf8_bin NOT NULL,
451 451
 					  STS_can_edit tinyint(1) NOT NULL DEFAULT 0,
@@ -453,10 +453,10 @@  discard block
 block discarded – undo
453 453
 					  STS_open tinyint(1) NOT NULL DEFAULT 1,
454 454
 					  UNIQUE KEY STS_ID_UNIQUE (STS_ID),
455 455
 					  KEY STS_type (STS_type)";
456
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
456
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
457 457
 
458
-        $table_name = 'esp_transaction';
459
-        $sql        = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
458
+		$table_name = 'esp_transaction';
459
+		$sql        = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
460 460
 					  TXN_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
461 461
 					  TXN_total decimal(10,3) DEFAULT '0.00',
462 462
 					  TXN_paid decimal(10,3) NOT NULL DEFAULT '0.00',
@@ -468,10 +468,10 @@  discard block
 block discarded – undo
468 468
 					  PRIMARY KEY  (TXN_ID),
469 469
 					  KEY TXN_timestamp (TXN_timestamp),
470 470
 					  KEY STS_ID (STS_ID)";
471
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
471
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
472 472
 
473
-        $table_name = 'esp_venue_meta';
474
-        $sql        = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
473
+		$table_name = 'esp_venue_meta';
474
+		$sql        = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
475 475
 			VNU_ID bigint(20) unsigned NOT NULL DEFAULT 0,
476 476
 			VNU_address varchar(255) DEFAULT NULL,
477 477
 			VNU_address2 varchar(255) DEFAULT NULL,
@@ -490,10 +490,10 @@  discard block
 block discarded – undo
490 490
 			KEY VNU_ID (VNU_ID),
491 491
 			KEY STA_ID (STA_ID),
492 492
 			KEY CNT_ISO (CNT_ISO)";
493
-        $this->_table_is_changed_in_this_version($table_name, $sql);
494
-        // modified tables
495
-        $table_name = "esp_price";
496
-        $sql        = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
493
+		$this->_table_is_changed_in_this_version($table_name, $sql);
494
+		// modified tables
495
+		$table_name = "esp_price";
496
+		$sql        = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
497 497
 					  PRT_ID tinyint(3) unsigned NOT NULL,
498 498
 					  PRC_amount decimal(10,3) NOT NULL DEFAULT '0.00',
499 499
 					  PRC_name varchar(245) NOT NULL,
@@ -506,10 +506,10 @@  discard block
 block discarded – undo
506 506
 					  PRC_parent int(10) unsigned DEFAULT 0,
507 507
 					  PRIMARY KEY  (PRC_ID),
508 508
 					  KEY PRT_ID (PRT_ID)";
509
-        $this->_table_is_changed_in_this_version($table_name, $sql);
509
+		$this->_table_is_changed_in_this_version($table_name, $sql);
510 510
 
511
-        $table_name = "esp_price_type";
512
-        $sql        = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
511
+		$table_name = "esp_price_type";
512
+		$sql        = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
513 513
 				  PRT_name varchar(45) NOT NULL,
514 514
 				  PBT_ID tinyint(3) unsigned NOT NULL DEFAULT '1',
515 515
 				  PRT_is_percent tinyint(1) NOT NULL DEFAULT '0',
@@ -518,10 +518,10 @@  discard block
 block discarded – undo
518 518
 				  PRT_deleted tinyint(1) NOT NULL DEFAULT '0',
519 519
 				  UNIQUE KEY PRT_name_UNIQUE (PRT_name),
520 520
 				  PRIMARY KEY  (PRT_ID)";
521
-        $this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB');
521
+		$this->_table_has_not_changed_since_previous($table_name, $sql, 'ENGINE=InnoDB');
522 522
 
523
-        $table_name = "esp_ticket";
524
-        $sql        = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
523
+		$table_name = "esp_ticket";
524
+		$sql        = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
525 525
 					  TTM_ID int(10) unsigned NOT NULL,
526 526
 					  TKT_name varchar(245) NOT NULL DEFAULT '',
527 527
 					  TKT_description text NOT NULL,
@@ -543,10 +543,10 @@  discard block
 block discarded – undo
543 543
 					  TKT_deleted tinyint(1) NOT NULL DEFAULT '0',
544 544
 					  PRIMARY KEY  (TKT_ID),
545 545
 					  KEY TKT_start_date (TKT_start_date)";
546
-        $this->_table_is_changed_in_this_version($table_name, $sql);
546
+		$this->_table_is_changed_in_this_version($table_name, $sql);
547 547
 
548
-        $table_name = 'esp_question_group';
549
-        $sql        = 'QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
548
+		$table_name = 'esp_question_group';
549
+		$sql        = 'QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
550 550
 					QSG_name varchar(255) NOT NULL,
551 551
 					QSG_identifier varchar(100) NOT NULL,
552 552
 					QSG_desc text NULL,
@@ -559,36 +559,36 @@  discard block
 block discarded – undo
559 559
 					PRIMARY KEY  (QSG_ID),
560 560
 					UNIQUE KEY QSG_identifier_UNIQUE (QSG_identifier),
561 561
 					KEY QSG_order (QSG_order)';
562
-        $this->_table_is_changed_in_this_version($table_name, $sql);
563
-        /** @var EE_DMS_Core_4_1_0 $script_4_1_defaults */
564
-        $script_4_1_defaults = EE_Registry::instance()->load_dms('Core_4_1_0');
565
-        // (because many need to convert old string states to foreign keys into the states table)
566
-        $script_4_1_defaults->insert_default_states();
567
-        $script_4_1_defaults->insert_default_countries();
568
-        /** @var EE_DMS_Core_4_5_0 $script_4_5_defaults */
569
-        $script_4_5_defaults = EE_Registry::instance()->load_dms('Core_4_5_0');
570
-        $script_4_5_defaults->insert_default_price_types();
571
-        $script_4_5_defaults->insert_default_prices();
572
-        $script_4_5_defaults->insert_default_tickets();
573
-        /** @var EE_DMS_Core_4_6_0 $script_4_6_defaults */
574
-        $script_4_6_defaults = EE_Registry::instance()->load_dms('Core_4_6_0');
575
-        $script_4_6_defaults->add_default_admin_only_payments();
576
-        $script_4_6_defaults->insert_default_currencies();
577
-        return true;
578
-    }
579
-
580
-
581
-    /**
582
-     * @return boolean
583
-     */
584
-    public function schema_changes_after_migration()
585
-    {
586
-        return true;
587
-    }
588
-
589
-
590
-    public function migration_page_hooks()
591
-    {
592
-    }
562
+		$this->_table_is_changed_in_this_version($table_name, $sql);
563
+		/** @var EE_DMS_Core_4_1_0 $script_4_1_defaults */
564
+		$script_4_1_defaults = EE_Registry::instance()->load_dms('Core_4_1_0');
565
+		// (because many need to convert old string states to foreign keys into the states table)
566
+		$script_4_1_defaults->insert_default_states();
567
+		$script_4_1_defaults->insert_default_countries();
568
+		/** @var EE_DMS_Core_4_5_0 $script_4_5_defaults */
569
+		$script_4_5_defaults = EE_Registry::instance()->load_dms('Core_4_5_0');
570
+		$script_4_5_defaults->insert_default_price_types();
571
+		$script_4_5_defaults->insert_default_prices();
572
+		$script_4_5_defaults->insert_default_tickets();
573
+		/** @var EE_DMS_Core_4_6_0 $script_4_6_defaults */
574
+		$script_4_6_defaults = EE_Registry::instance()->load_dms('Core_4_6_0');
575
+		$script_4_6_defaults->add_default_admin_only_payments();
576
+		$script_4_6_defaults->insert_default_currencies();
577
+		return true;
578
+	}
579
+
580
+
581
+	/**
582
+	 * @return boolean
583
+	 */
584
+	public function schema_changes_after_migration()
585
+	{
586
+		return true;
587
+	}
588
+
589
+
590
+	public function migration_page_hooks()
591
+	{
592
+	}
593 593
 }
594 594
 // end of file: /core/data_migration_scripts/EE_DMS_Core_4_7_0.dms.php
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -7,12 +7,12 @@  discard block
 block discarded – undo
7 7
 // unfortunately, this needs to be done upon INCLUSION of this file,
8 8
 // instead of construction, because it only gets constructed on first page load
9 9
 // (all other times it gets resurrected from a wordpress option)
10
-$stages            = glob(EE_CORE . 'data_migration_scripts/4_7_0_stages/*');
10
+$stages            = glob(EE_CORE.'data_migration_scripts/4_7_0_stages/*');
11 11
 $class_to_filepath = [];
12 12
 foreach ($stages as $filepath) {
13 13
     $matches = [];
14 14
     preg_match('~4_7_0_stages/(.*).dmsstage.php~', $filepath, $matches);
15
-    $class_to_filepath[ $matches[1] ] = $filepath;
15
+    $class_to_filepath[$matches[1]] = $filepath;
16 16
 }
17 17
 // give addons a chance to autoload their stages too
18 18
 $class_to_filepath = apply_filters('FHEE__EE_DMS_4_7_0__autoloaded_stages', $class_to_filepath);
@@ -69,9 +69,9 @@  discard block
 block discarded – undo
69 69
              $this->_get_table_analysis()->tableExists('esp_registration'))
70 70
         ) {
71 71
             return true;
72
-        } elseif (! $version_string) {
72
+        } elseif ( ! $version_string) {
73 73
             // no version string provided... this must be pre 4.3
74
-            return false;// changed mind. dont want people thinking they should migrate yet because they cant
74
+            return false; // changed mind. dont want people thinking they should migrate yet because they cant
75 75
         } else {
76 76
             return false;
77 77
         }
@@ -86,7 +86,7 @@  discard block
 block discarded – undo
86 86
     public function schema_changes_before_migration()
87 87
     {
88 88
         // relies on 4.1's EEH_Activation::create_table
89
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
89
+        require_once(EE_HELPERS.'EEH_Activation.helper.php');
90 90
 
91 91
         $table_name = 'esp_answer';
92 92
         $sql        = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
Please login to merge, or discard this patch.
4_7_0_stages/EE_DMS_4_7_0_Registration_Payments.dmsstage.php 2 patches
Indentation   +214 added lines, -214 removed lines patch added patch discarded remove patch
@@ -10,218 +10,218 @@
 block discarded – undo
10 10
 class EE_DMS_4_7_0_Registration_Payments extends EE_Data_Migration_Script_Stage_Table
11 11
 {
12 12
 
13
-    protected string $_payment_table;
14
-
15
-    protected string $_registration_table;
16
-
17
-    protected string $_registration_payment_table;
18
-
19
-
20
-    public function __construct()
21
-    {
22
-        /** @type WPDB $wpdb */ global $wpdb;
23
-        $this->_pretty_name = esc_html__('Registration Payment Record Generation', 'event_espresso');
24
-        // define tables
25
-        $this->_old_table                  = $wpdb->prefix . 'esp_transaction';
26
-        $this->_payment_table              = $wpdb->prefix . 'esp_payment';
27
-        $this->_registration_table         = $wpdb->prefix . 'esp_registration';
28
-        $this->_registration_payment_table = $wpdb->prefix . 'esp_registration_payment';
29
-        // build SQL WHERE clauses
30
-        $this->_extra_where_sql = "WHERE STS_ID IN ( 'TIN', 'TCM' ) AND TXN_Total != '0.000'";
31
-        parent::__construct();
32
-    }
33
-
34
-
35
-    /**
36
-     * @param array $old_row
37
-     * @return void
38
-     */
39
-    protected function _migrate_old_row($old_row)
40
-    {
41
-        /** @type WPDB $wpdb */ global $wpdb;
42
-        $TXN_ID = absint($old_row['TXN_ID']);
43
-        if (! $TXN_ID) {
44
-            $this->add_error(
45
-                sprintf(
46
-                    esc_html__('Invalid transaction with ID=%1$d. Error: "%2$s"', 'event_espresso'),
47
-                    $TXN_ID,
48
-                    $wpdb->last_error
49
-                )
50
-            );
51
-            return;
52
-        }
53
-        // get all payments for the TXN
54
-        $payments = $this->_get_payments($TXN_ID);
55
-        if (empty($payments)) {
56
-            return;
57
-        }
58
-        // then the registrants
59
-        $registrations = $this->_get_registrations($TXN_ID);
60
-        if (empty($registrations)) {
61
-            return;
62
-        }
63
-        // now loop thru each payment and apply it to each of the registrations
64
-        foreach ($payments as $payment) {
65
-            if ($payment->STS_ID === 'PAP' && $payment->PAY_amount > 0) {
66
-                $this->_process_registration_payments($payment, $registrations);
67
-            }
68
-        }
69
-    }
70
-
71
-
72
-    /**
73
-     * _get_registrations
74
-     *
75
-     * @param int $TXN_ID
76
-     * @return array
77
-     */
78
-    protected function _get_registrations(int $TXN_ID): array
79
-    {
80
-        /** @type WPDB $wpdb */ global $wpdb;
81
-        $SQL = "SELECT * FROM $this->_registration_table WHERE TXN_ID = %d AND STS_ID IN ( 'RPP', 'RAP' )";
82
-        return $wpdb->get_results($wpdb->prepare($SQL, $TXN_ID), OBJECT_K);
83
-    }
84
-
85
-
86
-    /**
87
-     * _get_payments
88
-     *
89
-     * @param int $TXN_ID
90
-     * @return array
91
-     */
92
-    protected function _get_payments(int $TXN_ID): array
93
-    {
94
-        /** @type WPDB $wpdb */ global $wpdb;
95
-        return $wpdb->get_results(
96
-            $wpdb->prepare("SELECT * FROM $this->_payment_table WHERE TXN_ID = %d", $TXN_ID),
97
-            OBJECT_K
98
-        );
99
-    }
100
-
101
-
102
-    /**
103
-     * _get_possibly_updated_REG_paid
104
-     *
105
-     * @param int $REG_ID
106
-     * @return float
107
-     */
108
-    protected function _get_possibly_updated_REG_paid(int $REG_ID): float
109
-    {
110
-        /** @type WPDB $wpdb */ global $wpdb;
111
-        return (float) $wpdb->get_var(
112
-            $wpdb->prepare("SELECT REG_paid FROM $this->_registration_table WHERE REG_ID = %d", $REG_ID)
113
-        );
114
-    }
115
-
116
-
117
-    /**
118
-     * _process_registration_payments
119
-     * basically a copy of the "Sequential Registration Payment Application Strategy"  logic
120
-     * currently in EE_Payment_Processor::process_registration_payments()
121
-     *
122
-     * @param stdClass $payment
123
-     * @param array    $registrations
124
-     * @return void
125
-     */
126
-    protected function _process_registration_payments(stdClass $payment, array $registrations = [])
127
-    {
128
-        // how much is available to apply to registrations?
129
-        $available_payment_amount = $payment->PAY_amount;
130
-        foreach ($registrations as $REG_ID => $registration) {
131
-            // nothing left, then we are done here?
132
-            if (! $available_payment_amount > 0) {
133
-                break;
134
-            }
135
-            // ensure REG_final_price has a valid value, and skip if it turns out to be zero
136
-            $registration->REG_final_price =
137
-                ! empty($registration->REG_final_price) ? (float) $registration->REG_final_price : 0.00;
138
-            if (! $registration->REG_final_price > 0) {
139
-                continue;
140
-            }
141
-            // because REG_paid may have been updated by a previous payment, we need to retrieve the value from the db
142
-            $registration->REG_paid = $this->_get_possibly_updated_REG_paid($REG_ID);
143
-            // calculate amount owing, and skip if it turns out to be zero
144
-            $owing = $registration->REG_final_price - $registration->REG_paid;
145
-            if (! $owing > 0) {
146
-                continue;
147
-            }
148
-            // don't allow payment amount to exceed the available payment amount, OR the amount owing
149
-            $payment_amount = min($available_payment_amount, $owing);
150
-            // update $available_payment_amount
151
-            $available_payment_amount = $available_payment_amount - $payment_amount;
152
-            // add relation between registration and payment and set amount
153
-            if ($this->_insert_registration_payment($registration->REG_ID, $payment->PAY_ID, (float) $payment_amount)) {
154
-                // calculate and set new REG_paid
155
-                $registration->REG_paid = $registration->REG_paid + $payment_amount;
156
-                $this->_update_registration_paid($registration->REG_ID, (float) $registration->REG_paid);
157
-            }
158
-        }
159
-    }
160
-
161
-
162
-    /**
163
-     * _insert_registration_payment
164
-     *
165
-     * @param int   $REG_ID
166
-     * @param int   $PAY_ID
167
-     * @param float $PAY_amount
168
-     * @return bool
169
-     */
170
-    protected function _insert_registration_payment(int $REG_ID = 0, int $PAY_ID = 0, float $PAY_amount = 0.00): bool
171
-    {
172
-        global $wpdb;
173
-        $success = $wpdb->insert(
174
-            $this->_registration_payment_table,
175
-            ['REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount,],  // data
176
-            ['%f']                                                                     // data format
177
-        );
178
-        if ($success === false) {
179
-            $this->add_error(
180
-                sprintf(
181
-                    esc_html__(
182
-                        'Could not update registration paid value for registration ID=%1$d because "%2$s"',
183
-                        'event_espresso'
184
-                    ),
185
-                    $REG_ID,
186
-                    $wpdb->last_error
187
-                )
188
-            );
189
-            return false;
190
-        }
191
-        return true;
192
-    }
193
-
194
-
195
-    /**
196
-     * _update_registration_paid
197
-     *
198
-     * @param int   $REG_ID
199
-     * @param float $REG_paid
200
-     * @return bool
201
-     */
202
-    protected function _update_registration_paid(int $REG_ID = 0, float $REG_paid = 0.00): bool
203
-    {
204
-        /** @type WPDB $wpdb */ global $wpdb;
205
-        $success = $wpdb->update(
206
-            $this->_registration_table,
207
-            ['REG_paid' => $REG_paid],  // data
208
-            ['REG_ID' => $REG_ID],      // where
209
-            ['%f'],                     // data format
210
-            ['%d']                      // where format
211
-        );
212
-        if ($success === false) {
213
-            $this->add_error(
214
-                sprintf(
215
-                    esc_html__(
216
-                        'Could not update registration paid value for registration ID=%1$d because "%2$s"',
217
-                        'event_espresso'
218
-                    ),
219
-                    $REG_ID,
220
-                    $wpdb->last_error
221
-                )
222
-            );
223
-            return false;
224
-        }
225
-        return true;
226
-    }
13
+	protected string $_payment_table;
14
+
15
+	protected string $_registration_table;
16
+
17
+	protected string $_registration_payment_table;
18
+
19
+
20
+	public function __construct()
21
+	{
22
+		/** @type WPDB $wpdb */ global $wpdb;
23
+		$this->_pretty_name = esc_html__('Registration Payment Record Generation', 'event_espresso');
24
+		// define tables
25
+		$this->_old_table                  = $wpdb->prefix . 'esp_transaction';
26
+		$this->_payment_table              = $wpdb->prefix . 'esp_payment';
27
+		$this->_registration_table         = $wpdb->prefix . 'esp_registration';
28
+		$this->_registration_payment_table = $wpdb->prefix . 'esp_registration_payment';
29
+		// build SQL WHERE clauses
30
+		$this->_extra_where_sql = "WHERE STS_ID IN ( 'TIN', 'TCM' ) AND TXN_Total != '0.000'";
31
+		parent::__construct();
32
+	}
33
+
34
+
35
+	/**
36
+	 * @param array $old_row
37
+	 * @return void
38
+	 */
39
+	protected function _migrate_old_row($old_row)
40
+	{
41
+		/** @type WPDB $wpdb */ global $wpdb;
42
+		$TXN_ID = absint($old_row['TXN_ID']);
43
+		if (! $TXN_ID) {
44
+			$this->add_error(
45
+				sprintf(
46
+					esc_html__('Invalid transaction with ID=%1$d. Error: "%2$s"', 'event_espresso'),
47
+					$TXN_ID,
48
+					$wpdb->last_error
49
+				)
50
+			);
51
+			return;
52
+		}
53
+		// get all payments for the TXN
54
+		$payments = $this->_get_payments($TXN_ID);
55
+		if (empty($payments)) {
56
+			return;
57
+		}
58
+		// then the registrants
59
+		$registrations = $this->_get_registrations($TXN_ID);
60
+		if (empty($registrations)) {
61
+			return;
62
+		}
63
+		// now loop thru each payment and apply it to each of the registrations
64
+		foreach ($payments as $payment) {
65
+			if ($payment->STS_ID === 'PAP' && $payment->PAY_amount > 0) {
66
+				$this->_process_registration_payments($payment, $registrations);
67
+			}
68
+		}
69
+	}
70
+
71
+
72
+	/**
73
+	 * _get_registrations
74
+	 *
75
+	 * @param int $TXN_ID
76
+	 * @return array
77
+	 */
78
+	protected function _get_registrations(int $TXN_ID): array
79
+	{
80
+		/** @type WPDB $wpdb */ global $wpdb;
81
+		$SQL = "SELECT * FROM $this->_registration_table WHERE TXN_ID = %d AND STS_ID IN ( 'RPP', 'RAP' )";
82
+		return $wpdb->get_results($wpdb->prepare($SQL, $TXN_ID), OBJECT_K);
83
+	}
84
+
85
+
86
+	/**
87
+	 * _get_payments
88
+	 *
89
+	 * @param int $TXN_ID
90
+	 * @return array
91
+	 */
92
+	protected function _get_payments(int $TXN_ID): array
93
+	{
94
+		/** @type WPDB $wpdb */ global $wpdb;
95
+		return $wpdb->get_results(
96
+			$wpdb->prepare("SELECT * FROM $this->_payment_table WHERE TXN_ID = %d", $TXN_ID),
97
+			OBJECT_K
98
+		);
99
+	}
100
+
101
+
102
+	/**
103
+	 * _get_possibly_updated_REG_paid
104
+	 *
105
+	 * @param int $REG_ID
106
+	 * @return float
107
+	 */
108
+	protected function _get_possibly_updated_REG_paid(int $REG_ID): float
109
+	{
110
+		/** @type WPDB $wpdb */ global $wpdb;
111
+		return (float) $wpdb->get_var(
112
+			$wpdb->prepare("SELECT REG_paid FROM $this->_registration_table WHERE REG_ID = %d", $REG_ID)
113
+		);
114
+	}
115
+
116
+
117
+	/**
118
+	 * _process_registration_payments
119
+	 * basically a copy of the "Sequential Registration Payment Application Strategy"  logic
120
+	 * currently in EE_Payment_Processor::process_registration_payments()
121
+	 *
122
+	 * @param stdClass $payment
123
+	 * @param array    $registrations
124
+	 * @return void
125
+	 */
126
+	protected function _process_registration_payments(stdClass $payment, array $registrations = [])
127
+	{
128
+		// how much is available to apply to registrations?
129
+		$available_payment_amount = $payment->PAY_amount;
130
+		foreach ($registrations as $REG_ID => $registration) {
131
+			// nothing left, then we are done here?
132
+			if (! $available_payment_amount > 0) {
133
+				break;
134
+			}
135
+			// ensure REG_final_price has a valid value, and skip if it turns out to be zero
136
+			$registration->REG_final_price =
137
+				! empty($registration->REG_final_price) ? (float) $registration->REG_final_price : 0.00;
138
+			if (! $registration->REG_final_price > 0) {
139
+				continue;
140
+			}
141
+			// because REG_paid may have been updated by a previous payment, we need to retrieve the value from the db
142
+			$registration->REG_paid = $this->_get_possibly_updated_REG_paid($REG_ID);
143
+			// calculate amount owing, and skip if it turns out to be zero
144
+			$owing = $registration->REG_final_price - $registration->REG_paid;
145
+			if (! $owing > 0) {
146
+				continue;
147
+			}
148
+			// don't allow payment amount to exceed the available payment amount, OR the amount owing
149
+			$payment_amount = min($available_payment_amount, $owing);
150
+			// update $available_payment_amount
151
+			$available_payment_amount = $available_payment_amount - $payment_amount;
152
+			// add relation between registration and payment and set amount
153
+			if ($this->_insert_registration_payment($registration->REG_ID, $payment->PAY_ID, (float) $payment_amount)) {
154
+				// calculate and set new REG_paid
155
+				$registration->REG_paid = $registration->REG_paid + $payment_amount;
156
+				$this->_update_registration_paid($registration->REG_ID, (float) $registration->REG_paid);
157
+			}
158
+		}
159
+	}
160
+
161
+
162
+	/**
163
+	 * _insert_registration_payment
164
+	 *
165
+	 * @param int   $REG_ID
166
+	 * @param int   $PAY_ID
167
+	 * @param float $PAY_amount
168
+	 * @return bool
169
+	 */
170
+	protected function _insert_registration_payment(int $REG_ID = 0, int $PAY_ID = 0, float $PAY_amount = 0.00): bool
171
+	{
172
+		global $wpdb;
173
+		$success = $wpdb->insert(
174
+			$this->_registration_payment_table,
175
+			['REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount,],  // data
176
+			['%f']                                                                     // data format
177
+		);
178
+		if ($success === false) {
179
+			$this->add_error(
180
+				sprintf(
181
+					esc_html__(
182
+						'Could not update registration paid value for registration ID=%1$d because "%2$s"',
183
+						'event_espresso'
184
+					),
185
+					$REG_ID,
186
+					$wpdb->last_error
187
+				)
188
+			);
189
+			return false;
190
+		}
191
+		return true;
192
+	}
193
+
194
+
195
+	/**
196
+	 * _update_registration_paid
197
+	 *
198
+	 * @param int   $REG_ID
199
+	 * @param float $REG_paid
200
+	 * @return bool
201
+	 */
202
+	protected function _update_registration_paid(int $REG_ID = 0, float $REG_paid = 0.00): bool
203
+	{
204
+		/** @type WPDB $wpdb */ global $wpdb;
205
+		$success = $wpdb->update(
206
+			$this->_registration_table,
207
+			['REG_paid' => $REG_paid],  // data
208
+			['REG_ID' => $REG_ID],      // where
209
+			['%f'],                     // data format
210
+			['%d']                      // where format
211
+		);
212
+		if ($success === false) {
213
+			$this->add_error(
214
+				sprintf(
215
+					esc_html__(
216
+						'Could not update registration paid value for registration ID=%1$d because "%2$s"',
217
+						'event_espresso'
218
+					),
219
+					$REG_ID,
220
+					$wpdb->last_error
221
+				)
222
+			);
223
+			return false;
224
+		}
225
+		return true;
226
+	}
227 227
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -22,10 +22,10 @@  discard block
 block discarded – undo
22 22
         /** @type WPDB $wpdb */ global $wpdb;
23 23
         $this->_pretty_name = esc_html__('Registration Payment Record Generation', 'event_espresso');
24 24
         // define tables
25
-        $this->_old_table                  = $wpdb->prefix . 'esp_transaction';
26
-        $this->_payment_table              = $wpdb->prefix . 'esp_payment';
27
-        $this->_registration_table         = $wpdb->prefix . 'esp_registration';
28
-        $this->_registration_payment_table = $wpdb->prefix . 'esp_registration_payment';
25
+        $this->_old_table                  = $wpdb->prefix.'esp_transaction';
26
+        $this->_payment_table              = $wpdb->prefix.'esp_payment';
27
+        $this->_registration_table         = $wpdb->prefix.'esp_registration';
28
+        $this->_registration_payment_table = $wpdb->prefix.'esp_registration_payment';
29 29
         // build SQL WHERE clauses
30 30
         $this->_extra_where_sql = "WHERE STS_ID IN ( 'TIN', 'TCM' ) AND TXN_Total != '0.000'";
31 31
         parent::__construct();
@@ -40,7 +40,7 @@  discard block
 block discarded – undo
40 40
     {
41 41
         /** @type WPDB $wpdb */ global $wpdb;
42 42
         $TXN_ID = absint($old_row['TXN_ID']);
43
-        if (! $TXN_ID) {
43
+        if ( ! $TXN_ID) {
44 44
             $this->add_error(
45 45
                 sprintf(
46 46
                     esc_html__('Invalid transaction with ID=%1$d. Error: "%2$s"', 'event_espresso'),
@@ -129,20 +129,20 @@  discard block
 block discarded – undo
129 129
         $available_payment_amount = $payment->PAY_amount;
130 130
         foreach ($registrations as $REG_ID => $registration) {
131 131
             // nothing left, then we are done here?
132
-            if (! $available_payment_amount > 0) {
132
+            if ( ! $available_payment_amount > 0) {
133 133
                 break;
134 134
             }
135 135
             // ensure REG_final_price has a valid value, and skip if it turns out to be zero
136 136
             $registration->REG_final_price =
137 137
                 ! empty($registration->REG_final_price) ? (float) $registration->REG_final_price : 0.00;
138
-            if (! $registration->REG_final_price > 0) {
138
+            if ( ! $registration->REG_final_price > 0) {
139 139
                 continue;
140 140
             }
141 141
             // because REG_paid may have been updated by a previous payment, we need to retrieve the value from the db
142 142
             $registration->REG_paid = $this->_get_possibly_updated_REG_paid($REG_ID);
143 143
             // calculate amount owing, and skip if it turns out to be zero
144 144
             $owing = $registration->REG_final_price - $registration->REG_paid;
145
-            if (! $owing > 0) {
145
+            if ( ! $owing > 0) {
146 146
                 continue;
147 147
             }
148 148
             // don't allow payment amount to exceed the available payment amount, OR the amount owing
@@ -172,7 +172,7 @@  discard block
 block discarded – undo
172 172
         global $wpdb;
173 173
         $success = $wpdb->insert(
174 174
             $this->_registration_payment_table,
175
-            ['REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount,],  // data
175
+            ['REG_ID' => $REG_ID, 'PAY_ID' => $PAY_ID, 'RPY_amount' => $PAY_amount, ], // data
176 176
             ['%f']                                                                     // data format
177 177
         );
178 178
         if ($success === false) {
@@ -204,9 +204,9 @@  discard block
 block discarded – undo
204 204
         /** @type WPDB $wpdb */ global $wpdb;
205 205
         $success = $wpdb->update(
206 206
             $this->_registration_table,
207
-            ['REG_paid' => $REG_paid],  // data
208
-            ['REG_ID' => $REG_ID],      // where
209
-            ['%f'],                     // data format
207
+            ['REG_paid' => $REG_paid], // data
208
+            ['REG_ID' => $REG_ID], // where
209
+            ['%f'], // data format
210 210
             ['%d']                      // where format
211 211
         );
212 212
         if ($success === false) {
Please login to merge, or discard this patch.
4_7_0_stages/EE_DMS_4_7_0_Add_Taxes_To_REG_Final_Price.dmsstage.php 2 patches
Indentation   +193 added lines, -193 removed lines patch added patch discarded remove patch
@@ -10,69 +10,69 @@  discard block
 block discarded – undo
10 10
 class EE_DMS_4_7_0_Add_Taxes_To_REG_Final_Price extends EE_Data_Migration_Script_Stage_Table
11 11
 {
12 12
 
13
-    protected string $_ticket_table;
14
-
15
-    protected string $_line_item_table;
16
-
17
-
18
-    public function __construct()
19
-    {
20
-        /** @type WPDB $wpdb */ global $wpdb;
21
-        $this->_pretty_name = esc_html__('Registration Final Price Tax Calculations', 'event_espresso');
22
-        // define tables
23
-        $this->_old_table       = $wpdb->prefix . 'esp_registration';
24
-        $this->_ticket_table    = $wpdb->prefix . 'esp_ticket';
25
-        $this->_line_item_table = $wpdb->prefix . 'esp_line_item';
26
-        parent::__construct();
27
-    }
28
-
29
-
30
-    /**
31
-     * @return string
32
-     */
33
-    protected function _get_rest_of_sql_for_query(): string
34
-    {
35
-        $SQL = "FROM $this->_old_table AS reg ";
36
-        $SQL .= "JOIN $this->_ticket_table as tkt ON reg.TKT_ID = tkt.TKT_ID ";
37
-        $SQL .= "JOIN $this->_line_item_table as line ON reg.TXN_ID = line.TXN_ID ";
38
-        $SQL .= "WHERE tkt.TKT_taxable = 1 ";
39
-        $SQL .= "AND line.LIN_code = 'total' ";
40
-        $SQL .= "AND reg.REG_final_price > 0 ";
41
-        return $SQL;
42
-    }
43
-
44
-
45
-    /**
46
-     * Counts the records to migrate; the public version may cache it
47
-     *
48
-     * @return int
49
-     */
50
-    public function _count_records_to_migrate(): int
51
-    {
52
-        /** @type WPDB $wpdb */ global $wpdb;
53
-        $SQL   = "SELECT count( reg.REG_ID ) ";
54
-        $SQL   .= $this->_get_rest_of_sql_for_query();
55
-        return (int) $wpdb->get_var($SQL);
56
-    }
57
-
58
-
59
-    /**
60
-     * Gets data for all registrations with taxable tickets in the esp_line_item table
61
-     *
62
-     * @param int   $limit
63
-     * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
64
-     * @global wpdb $wpdb
65
-     */
66
-    protected function _get_rows($limit): array
67
-    {
68
-        /** @type WPDB $wpdb */ global $wpdb;
69
-        $start_at_record = $this->count_records_migrated();
70
-        $SQL             = "SELECT reg.REG_ID,  reg.REG_final_price, line.LIN_ID ";
71
-        $SQL             .= $this->_get_rest_of_sql_for_query();
72
-        $SQL             .= $wpdb->prepare("LIMIT %d, %d", $start_at_record, $limit);
73
-
74
-        // produces something like:
75
-        /*
13
+	protected string $_ticket_table;
14
+
15
+	protected string $_line_item_table;
16
+
17
+
18
+	public function __construct()
19
+	{
20
+		/** @type WPDB $wpdb */ global $wpdb;
21
+		$this->_pretty_name = esc_html__('Registration Final Price Tax Calculations', 'event_espresso');
22
+		// define tables
23
+		$this->_old_table       = $wpdb->prefix . 'esp_registration';
24
+		$this->_ticket_table    = $wpdb->prefix . 'esp_ticket';
25
+		$this->_line_item_table = $wpdb->prefix . 'esp_line_item';
26
+		parent::__construct();
27
+	}
28
+
29
+
30
+	/**
31
+	 * @return string
32
+	 */
33
+	protected function _get_rest_of_sql_for_query(): string
34
+	{
35
+		$SQL = "FROM $this->_old_table AS reg ";
36
+		$SQL .= "JOIN $this->_ticket_table as tkt ON reg.TKT_ID = tkt.TKT_ID ";
37
+		$SQL .= "JOIN $this->_line_item_table as line ON reg.TXN_ID = line.TXN_ID ";
38
+		$SQL .= "WHERE tkt.TKT_taxable = 1 ";
39
+		$SQL .= "AND line.LIN_code = 'total' ";
40
+		$SQL .= "AND reg.REG_final_price > 0 ";
41
+		return $SQL;
42
+	}
43
+
44
+
45
+	/**
46
+	 * Counts the records to migrate; the public version may cache it
47
+	 *
48
+	 * @return int
49
+	 */
50
+	public function _count_records_to_migrate(): int
51
+	{
52
+		/** @type WPDB $wpdb */ global $wpdb;
53
+		$SQL   = "SELECT count( reg.REG_ID ) ";
54
+		$SQL   .= $this->_get_rest_of_sql_for_query();
55
+		return (int) $wpdb->get_var($SQL);
56
+	}
57
+
58
+
59
+	/**
60
+	 * Gets data for all registrations with taxable tickets in the esp_line_item table
61
+	 *
62
+	 * @param int   $limit
63
+	 * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
64
+	 * @global wpdb $wpdb
65
+	 */
66
+	protected function _get_rows($limit): array
67
+	{
68
+		/** @type WPDB $wpdb */ global $wpdb;
69
+		$start_at_record = $this->count_records_migrated();
70
+		$SQL             = "SELECT reg.REG_ID,  reg.REG_final_price, line.LIN_ID ";
71
+		$SQL             .= $this->_get_rest_of_sql_for_query();
72
+		$SQL             .= $wpdb->prepare("LIMIT %d, %d", $start_at_record, $limit);
73
+
74
+		// produces something like:
75
+		/*
76 76
             SELECT
77 77
               reg.REG_ID,
78 78
               reg.REG_final_price,
@@ -89,134 +89,134 @@  discard block
 block discarded – undo
89 89
             LIMIT 1, 50
90 90
          */
91 91
 
92
-        return $wpdb->get_results($SQL, ARRAY_A);
93
-    }
94
-
95
-
96
-    /**
97
-     * @param array $old_row
98
-     * @return void
99
-     */
100
-    protected function _migrate_old_row($old_row)
101
-    {
102
-        /** @type WPDB $wpdb */ global $wpdb;
103
-        // ensure all required values are present
104
-        if (! isset($old_row['REG_ID'], $old_row['REG_final_price'], $old_row['LIN_ID'])) {
105
-            $this->add_error(
106
-                sprintf(
107
-                    esc_html__(
108
-                        'Invalid query results returned with the following data:%1$s REG_ID=%2$d, REG_final_price=%3$d, LIN_ID=%4$f. Error: "%5$s"',
109
-                        'event_espresso'
110
-                    ),
111
-                    '<br />',
112
-                    $old_row['REG_ID'] ?? '',
113
-                    $old_row['REG_final_price'] ?? '',
114
-                    $old_row['LIN_ID'] ?? '',
115
-                    $wpdb->last_error
116
-                )
117
-            );
118
-            return;
119
-        }
120
-        // get tax subtotal
121
-        $tax_subtotal_line_item_ID = $this->_get_line_item_ID_for_tax_subtotal($old_row['LIN_ID']);
122
-        if (! $tax_subtotal_line_item_ID) {
123
-            $this->add_error(
124
-                sprintf(
125
-                    esc_html__('Invalid line item ID returned. Error: "%1$s"', 'event_espresso'),
126
-                    $wpdb->last_error
127
-                )
128
-            );
129
-            return;
130
-        }
131
-        // now get taxes
132
-        $taxes = $this->_get_tax_amounts($tax_subtotal_line_item_ID);
133
-        // apply taxes to registration final price
134
-        $this->_apply_taxes($old_row['REG_ID'], (float) $old_row['REG_final_price'], $taxes);
135
-    }
136
-
137
-
138
-    /**
139
-     * _get_tax_subtotal
140
-     *
141
-     * @param int $LIN_ID
142
-     * @return int
143
-     */
144
-    protected function _get_line_item_ID_for_tax_subtotal(int $LIN_ID): int
145
-    {
146
-        /** @type WPDB $wpdb */ global $wpdb;
147
-        $SQL = "SELECT LIN_ID ";
148
-        $SQL .= "FROM $this->_line_item_table ";
149
-        $SQL .= "WHERE LIN_parent = %d ";
150
-        $SQL .= "AND LIN_code = 'taxes'";
151
-        return (int) $wpdb->get_var($wpdb->prepare($SQL, $LIN_ID));
152
-    }
153
-
154
-
155
-    /**
156
-     * _get_tax_subtotal
157
-     *
158
-     * @param int $LIN_ID
159
-     * @return array
160
-     */
161
-    protected function _get_tax_amounts(int $LIN_ID): array
162
-    {
163
-        /** @type WPDB $wpdb */ global $wpdb;
164
-        $SQL = "SELECT LIN_percent ";
165
-        $SQL .= "FROM $this->_line_item_table ";
166
-        $SQL .= "WHERE LIN_parent = %d";
167
-        return $wpdb->get_results($wpdb->prepare($SQL, $LIN_ID), OBJECT_K);
168
-    }
169
-
170
-
171
-    /**
172
-     * _apply_taxes
173
-     *
174
-     * @param int   $REG_ID
175
-     * @param float $final_price
176
-     * @param array $taxes
177
-     * @return void
178
-     */
179
-    protected function _apply_taxes(int $REG_ID = 0, float $final_price = 0.00, array $taxes = [])
180
-    {
181
-        if (is_array($taxes) && ! empty($taxes)) {
182
-            $total_taxes = 0;
183
-            foreach ($taxes as $tax) {
184
-                $total_taxes += $final_price * ($tax->LIN_percent / 100);
185
-            }
186
-            $final_price += $total_taxes;
187
-            $this->_update_registration_final_price($REG_ID, (float) $final_price);
188
-        }
189
-    }
190
-
191
-
192
-    /**
193
-     * _update_registration_final_price
194
-     *
195
-     * @param int   $REG_ID
196
-     * @param float $REG_final_price
197
-     * @return void
198
-     */
199
-    protected function _update_registration_final_price(int $REG_ID = 0, float $REG_final_price = 0.00)
200
-    {
201
-        /** @type WPDB $wpdb */ global $wpdb;
202
-        $success = $wpdb->update(
203
-            $this->_old_table,
204
-            ['REG_final_price' => $REG_final_price],  // data
205
-            ['REG_ID' => $REG_ID],                    // where
206
-            ['%f'],                                   // data format
207
-            ['%d']                                    // where format
208
-        );
209
-        if ($success === false) {
210
-            $this->add_error(
211
-                sprintf(
212
-                    esc_html__(
213
-                        'Could not update registration final price value for registration ID=%1$d because "%2$s"',
214
-                        'event_espresso'
215
-                    ),
216
-                    $REG_ID,
217
-                    $wpdb->last_error
218
-                )
219
-            );
220
-        }
221
-    }
92
+		return $wpdb->get_results($SQL, ARRAY_A);
93
+	}
94
+
95
+
96
+	/**
97
+	 * @param array $old_row
98
+	 * @return void
99
+	 */
100
+	protected function _migrate_old_row($old_row)
101
+	{
102
+		/** @type WPDB $wpdb */ global $wpdb;
103
+		// ensure all required values are present
104
+		if (! isset($old_row['REG_ID'], $old_row['REG_final_price'], $old_row['LIN_ID'])) {
105
+			$this->add_error(
106
+				sprintf(
107
+					esc_html__(
108
+						'Invalid query results returned with the following data:%1$s REG_ID=%2$d, REG_final_price=%3$d, LIN_ID=%4$f. Error: "%5$s"',
109
+						'event_espresso'
110
+					),
111
+					'<br />',
112
+					$old_row['REG_ID'] ?? '',
113
+					$old_row['REG_final_price'] ?? '',
114
+					$old_row['LIN_ID'] ?? '',
115
+					$wpdb->last_error
116
+				)
117
+			);
118
+			return;
119
+		}
120
+		// get tax subtotal
121
+		$tax_subtotal_line_item_ID = $this->_get_line_item_ID_for_tax_subtotal($old_row['LIN_ID']);
122
+		if (! $tax_subtotal_line_item_ID) {
123
+			$this->add_error(
124
+				sprintf(
125
+					esc_html__('Invalid line item ID returned. Error: "%1$s"', 'event_espresso'),
126
+					$wpdb->last_error
127
+				)
128
+			);
129
+			return;
130
+		}
131
+		// now get taxes
132
+		$taxes = $this->_get_tax_amounts($tax_subtotal_line_item_ID);
133
+		// apply taxes to registration final price
134
+		$this->_apply_taxes($old_row['REG_ID'], (float) $old_row['REG_final_price'], $taxes);
135
+	}
136
+
137
+
138
+	/**
139
+	 * _get_tax_subtotal
140
+	 *
141
+	 * @param int $LIN_ID
142
+	 * @return int
143
+	 */
144
+	protected function _get_line_item_ID_for_tax_subtotal(int $LIN_ID): int
145
+	{
146
+		/** @type WPDB $wpdb */ global $wpdb;
147
+		$SQL = "SELECT LIN_ID ";
148
+		$SQL .= "FROM $this->_line_item_table ";
149
+		$SQL .= "WHERE LIN_parent = %d ";
150
+		$SQL .= "AND LIN_code = 'taxes'";
151
+		return (int) $wpdb->get_var($wpdb->prepare($SQL, $LIN_ID));
152
+	}
153
+
154
+
155
+	/**
156
+	 * _get_tax_subtotal
157
+	 *
158
+	 * @param int $LIN_ID
159
+	 * @return array
160
+	 */
161
+	protected function _get_tax_amounts(int $LIN_ID): array
162
+	{
163
+		/** @type WPDB $wpdb */ global $wpdb;
164
+		$SQL = "SELECT LIN_percent ";
165
+		$SQL .= "FROM $this->_line_item_table ";
166
+		$SQL .= "WHERE LIN_parent = %d";
167
+		return $wpdb->get_results($wpdb->prepare($SQL, $LIN_ID), OBJECT_K);
168
+	}
169
+
170
+
171
+	/**
172
+	 * _apply_taxes
173
+	 *
174
+	 * @param int   $REG_ID
175
+	 * @param float $final_price
176
+	 * @param array $taxes
177
+	 * @return void
178
+	 */
179
+	protected function _apply_taxes(int $REG_ID = 0, float $final_price = 0.00, array $taxes = [])
180
+	{
181
+		if (is_array($taxes) && ! empty($taxes)) {
182
+			$total_taxes = 0;
183
+			foreach ($taxes as $tax) {
184
+				$total_taxes += $final_price * ($tax->LIN_percent / 100);
185
+			}
186
+			$final_price += $total_taxes;
187
+			$this->_update_registration_final_price($REG_ID, (float) $final_price);
188
+		}
189
+	}
190
+
191
+
192
+	/**
193
+	 * _update_registration_final_price
194
+	 *
195
+	 * @param int   $REG_ID
196
+	 * @param float $REG_final_price
197
+	 * @return void
198
+	 */
199
+	protected function _update_registration_final_price(int $REG_ID = 0, float $REG_final_price = 0.00)
200
+	{
201
+		/** @type WPDB $wpdb */ global $wpdb;
202
+		$success = $wpdb->update(
203
+			$this->_old_table,
204
+			['REG_final_price' => $REG_final_price],  // data
205
+			['REG_ID' => $REG_ID],                    // where
206
+			['%f'],                                   // data format
207
+			['%d']                                    // where format
208
+		);
209
+		if ($success === false) {
210
+			$this->add_error(
211
+				sprintf(
212
+					esc_html__(
213
+						'Could not update registration final price value for registration ID=%1$d because "%2$s"',
214
+						'event_espresso'
215
+					),
216
+					$REG_ID,
217
+					$wpdb->last_error
218
+				)
219
+			);
220
+		}
221
+	}
222 222
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -20,9 +20,9 @@  discard block
 block discarded – undo
20 20
         /** @type WPDB $wpdb */ global $wpdb;
21 21
         $this->_pretty_name = esc_html__('Registration Final Price Tax Calculations', 'event_espresso');
22 22
         // define tables
23
-        $this->_old_table       = $wpdb->prefix . 'esp_registration';
24
-        $this->_ticket_table    = $wpdb->prefix . 'esp_ticket';
25
-        $this->_line_item_table = $wpdb->prefix . 'esp_line_item';
23
+        $this->_old_table       = $wpdb->prefix.'esp_registration';
24
+        $this->_ticket_table    = $wpdb->prefix.'esp_ticket';
25
+        $this->_line_item_table = $wpdb->prefix.'esp_line_item';
26 26
         parent::__construct();
27 27
     }
28 28
 
@@ -50,8 +50,8 @@  discard block
 block discarded – undo
50 50
     public function _count_records_to_migrate(): int
51 51
     {
52 52
         /** @type WPDB $wpdb */ global $wpdb;
53
-        $SQL   = "SELECT count( reg.REG_ID ) ";
54
-        $SQL   .= $this->_get_rest_of_sql_for_query();
53
+        $SQL = "SELECT count( reg.REG_ID ) ";
54
+        $SQL .= $this->_get_rest_of_sql_for_query();
55 55
         return (int) $wpdb->get_var($SQL);
56 56
     }
57 57
 
@@ -101,7 +101,7 @@  discard block
 block discarded – undo
101 101
     {
102 102
         /** @type WPDB $wpdb */ global $wpdb;
103 103
         // ensure all required values are present
104
-        if (! isset($old_row['REG_ID'], $old_row['REG_final_price'], $old_row['LIN_ID'])) {
104
+        if ( ! isset($old_row['REG_ID'], $old_row['REG_final_price'], $old_row['LIN_ID'])) {
105 105
             $this->add_error(
106 106
                 sprintf(
107 107
                     esc_html__(
@@ -119,7 +119,7 @@  discard block
 block discarded – undo
119 119
         }
120 120
         // get tax subtotal
121 121
         $tax_subtotal_line_item_ID = $this->_get_line_item_ID_for_tax_subtotal($old_row['LIN_ID']);
122
-        if (! $tax_subtotal_line_item_ID) {
122
+        if ( ! $tax_subtotal_line_item_ID) {
123 123
             $this->add_error(
124 124
                 sprintf(
125 125
                     esc_html__('Invalid line item ID returned. Error: "%1$s"', 'event_espresso'),
@@ -201,9 +201,9 @@  discard block
 block discarded – undo
201 201
         /** @type WPDB $wpdb */ global $wpdb;
202 202
         $success = $wpdb->update(
203 203
             $this->_old_table,
204
-            ['REG_final_price' => $REG_final_price],  // data
205
-            ['REG_ID' => $REG_ID],                    // where
206
-            ['%f'],                                   // data format
204
+            ['REG_final_price' => $REG_final_price], // data
205
+            ['REG_ID' => $REG_ID], // where
206
+            ['%f'], // data format
207 207
             ['%d']                                    // where format
208 208
         );
209 209
         if ($success === false) {
Please login to merge, or discard this patch.
core/data_migration_scripts/EE_DMS_Core_5_0_0.dms.php 1 patch
Indentation   +244 added lines, -244 removed lines patch added patch discarded remove patch
@@ -11,9 +11,9 @@  discard block
 block discarded – undo
11 11
 $stages            = glob(EE_CORE . 'data_migration_scripts/5_0_0_stages/*');
12 12
 $class_to_filepath = [];
13 13
 foreach ($stages as $filepath) {
14
-    $matches = [];
15
-    preg_match('~5_0_0_stages/(.*).dmsstage.php~', $filepath, $matches);
16
-    $class_to_filepath[ $matches[1] ] = $filepath;
14
+	$matches = [];
15
+	preg_match('~5_0_0_stages/(.*).dmsstage.php~', $filepath, $matches);
16
+	$class_to_filepath[ $matches[1] ] = $filepath;
17 17
 }
18 18
 // give addons a chance to autoload their stages too
19 19
 $class_to_filepath = apply_filters('FHEE__EE_DMS_5_0_0__autoloaded_stages', $class_to_filepath);
@@ -29,65 +29,65 @@  discard block
 block discarded – undo
29 29
  */
30 30
 class EE_DMS_Core_5_0_0 extends EE_Data_Migration_Script_Base
31 31
 {
32
-    /**
33
-     * @param EE_DMS_Core_4_10_0 $dms_4_10
34
-     * @param TableManager|null  $table_manager
35
-     * @param TableAnalysis|null $table_analysis
36
-     */
37
-    public function __construct(
38
-        EE_DMS_Core_4_10_0 $dms_4_10,
39
-        TableManager $table_manager = null,
40
-        TableAnalysis $table_analysis = null
41
-    ) {
42
-        $this->previous_dms      = $dms_4_10;
43
-        $this->_pretty_name      = esc_html__("Data Update to Event Espresso 5.0.0", "event_espresso");
44
-        $this->_priority         = 10;
45
-        $this->_migration_stages = [
46
-            new EE_DMS_5_0_0_Event_Venues(),
47
-        ];
48
-        parent::__construct($table_manager, $table_analysis);
49
-    }
50
-
51
-
52
-    /**
53
-     * Whether to migrate or not.
54
-     *
55
-     * @param array $version_array
56
-     * @return bool
57
-     */
58
-    public function can_migrate_from_version($version_array): bool
59
-    {
60
-        $version_string = $version_array['Core'];
61
-        return $version_string &&
62
-               version_compare($version_string, '5.0.0.decaf', '<') &&
63
-               version_compare($version_string, '4.10.0.decaf', '>=');
64
-    }
65
-
66
-
67
-    /**
68
-     * @return bool
69
-     * @throws EE_Error
70
-     * @throws ReflectionException
71
-     */
72
-    public function schema_changes_before_migration(): bool
73
-    {
74
-        require_once EE_HELPERS . 'EEH_Activation.helper.php';
75
-
76
-        $this->_table_has_not_changed_since_previous(
77
-            'esp_answer',
78
-            ' ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
32
+	/**
33
+	 * @param EE_DMS_Core_4_10_0 $dms_4_10
34
+	 * @param TableManager|null  $table_manager
35
+	 * @param TableAnalysis|null $table_analysis
36
+	 */
37
+	public function __construct(
38
+		EE_DMS_Core_4_10_0 $dms_4_10,
39
+		TableManager $table_manager = null,
40
+		TableAnalysis $table_analysis = null
41
+	) {
42
+		$this->previous_dms      = $dms_4_10;
43
+		$this->_pretty_name      = esc_html__("Data Update to Event Espresso 5.0.0", "event_espresso");
44
+		$this->_priority         = 10;
45
+		$this->_migration_stages = [
46
+			new EE_DMS_5_0_0_Event_Venues(),
47
+		];
48
+		parent::__construct($table_manager, $table_analysis);
49
+	}
50
+
51
+
52
+	/**
53
+	 * Whether to migrate or not.
54
+	 *
55
+	 * @param array $version_array
56
+	 * @return bool
57
+	 */
58
+	public function can_migrate_from_version($version_array): bool
59
+	{
60
+		$version_string = $version_array['Core'];
61
+		return $version_string &&
62
+			   version_compare($version_string, '5.0.0.decaf', '<') &&
63
+			   version_compare($version_string, '4.10.0.decaf', '>=');
64
+	}
65
+
66
+
67
+	/**
68
+	 * @return bool
69
+	 * @throws EE_Error
70
+	 * @throws ReflectionException
71
+	 */
72
+	public function schema_changes_before_migration(): bool
73
+	{
74
+		require_once EE_HELPERS . 'EEH_Activation.helper.php';
75
+
76
+		$this->_table_has_not_changed_since_previous(
77
+			'esp_answer',
78
+			' ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
79 79
             REG_ID int(10) unsigned NOT NULL,
80 80
             QST_ID int(10) unsigned NOT NULL,
81 81
             ANS_value text NOT NULL,
82 82
             PRIMARY KEY  (ANS_ID),
83 83
             KEY REG_ID (REG_ID),
84 84
             KEY QST_ID (QST_ID)'
85
-        );
85
+		);
86 86
 
87 87
 
88
-        $this->_table_has_not_changed_since_previous(
89
-            'esp_attendee_meta',
90
-            "ATTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
88
+		$this->_table_has_not_changed_since_previous(
89
+			'esp_attendee_meta',
90
+			"ATTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
91 91
 				ATT_ID bigint(20) unsigned NOT NULL,
92 92
 				ATT_fname varchar(45) NOT NULL,
93 93
 				ATT_lname varchar(45) NOT NULL,
@@ -104,12 +104,12 @@  discard block
 block discarded – undo
104 104
 				KEY ATT_email (ATT_email(191)),
105 105
 				KEY ATT_lname (ATT_lname),
106 106
 				KEY ATT_fname (ATT_fname)"
107
-        );
107
+		);
108 108
 
109 109
 
110
-        $this->_table_has_not_changed_since_previous(
111
-            'esp_checkin',
112
-            "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
110
+		$this->_table_has_not_changed_since_previous(
111
+			'esp_checkin',
112
+			"CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
113 113
 				REG_ID int(10) unsigned NOT NULL,
114 114
 				DTT_ID int(10) unsigned NOT NULL,
115 115
 				CHK_in tinyint(1) unsigned NOT NULL DEFAULT 1,
@@ -117,12 +117,12 @@  discard block
 block discarded – undo
117 117
 				PRIMARY KEY  (CHK_ID),
118 118
 				KEY REG_ID (REG_ID),
119 119
 				KEY DTT_ID (DTT_ID)"
120
-        );
120
+		);
121 121
 
122 122
 
123
-        $this->_table_has_not_changed_since_previous(
124
-            'esp_country',
125
-            "CNT_ISO varchar(2) NOT NULL,
123
+		$this->_table_has_not_changed_since_previous(
124
+			'esp_country',
125
+			"CNT_ISO varchar(2) NOT NULL,
126 126
 				CNT_ISO3 varchar(3) NOT NULL,
127 127
 				RGN_ID tinyint(3) unsigned DEFAULT NULL,
128 128
 				CNT_name varchar(45) NOT NULL,
@@ -138,38 +138,38 @@  discard block
 block discarded – undo
138 138
 				CNT_is_EU tinyint(1) DEFAULT '0',
139 139
 				CNT_active tinyint(1) DEFAULT '0',
140 140
 				PRIMARY KEY  (CNT_ISO)"
141
-        );
141
+		);
142 142
 
143 143
 
144
-        $this->_table_has_not_changed_since_previous(
145
-            'esp_currency',
146
-            "CUR_code varchar(6) NOT NULL,
144
+		$this->_table_has_not_changed_since_previous(
145
+			'esp_currency',
146
+			"CUR_code varchar(6) NOT NULL,
147 147
 				CUR_single varchar(45) DEFAULT 'dollar',
148 148
 				CUR_plural varchar(45) DEFAULT 'dollars',
149 149
 				CUR_sign varchar(45) DEFAULT '$',
150 150
 				CUR_dec_plc varchar(1) NOT NULL DEFAULT '2',
151 151
 				CUR_active tinyint(1) DEFAULT '0',
152 152
 				PRIMARY KEY  (CUR_code)"
153
-        );
153
+		);
154 154
 
155 155
 
156
-        // note: although this table is no longer in use,
157
-        // it hasn't been removed because then queries to the model will have errors.
158
-        // but you should expect this table and its corresponding model to be removed in
159
-        // the next few months
160
-        $this->_table_is_new_in_this_version(
161
-            'esp_currency_payment_method',
162
-            "CPM_ID int(11) NOT NULL AUTO_INCREMENT,
156
+		// note: although this table is no longer in use,
157
+		// it hasn't been removed because then queries to the model will have errors.
158
+		// but you should expect this table and its corresponding model to be removed in
159
+		// the next few months
160
+		$this->_table_is_new_in_this_version(
161
+			'esp_currency_payment_method',
162
+			"CPM_ID int(11) NOT NULL AUTO_INCREMENT,
163 163
 				CUR_code varchar(6) NOT NULL,
164 164
 				PMD_ID int(11) NOT NULL,
165 165
 				PRIMARY KEY  (CPM_ID),
166 166
 				KEY PMD_ID (PMD_ID)"
167
-        );
167
+		);
168 168
 
169 169
 
170
-        $this->_table_is_changed_in_this_version(
171
-            'esp_datetime',
172
-            "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
170
+		$this->_table_is_changed_in_this_version(
171
+			'esp_datetime',
172
+			"DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
173 173
 				EVT_ID bigint(20) unsigned NOT NULL,
174 174
 			    VNU_ID bigint(20) unsigned NOT NULL DEFAULT 0,
175 175
 				DTT_name varchar(255) NOT NULL DEFAULT '',
@@ -188,35 +188,35 @@  discard block
 block discarded – undo
188 188
 				KEY DTT_EVT_end (DTT_EVT_end),
189 189
 				KEY EVT_ID (EVT_ID),
190 190
 				KEY VNU_ID (VNU_ID)"
191
-        );
192
-        $this->_get_table_manager()->dropIndex('esp_datetime', 'DTT_is_primary');
191
+		);
192
+		$this->_get_table_manager()->dropIndex('esp_datetime', 'DTT_is_primary');
193 193
 
194 194
 
195
-        $this->_table_has_not_changed_since_previous(
196
-            'esp_datetime_ticket',
197
-            "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
195
+		$this->_table_has_not_changed_since_previous(
196
+			'esp_datetime_ticket',
197
+			"DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
198 198
 				DTT_ID int(10) unsigned NOT NULL,
199 199
 				TKT_ID int(10) unsigned NOT NULL,
200 200
 				PRIMARY KEY  (DTK_ID),
201 201
 				KEY DTT_ID (DTT_ID),
202 202
 				KEY TKT_ID (TKT_ID)"
203
-        );
203
+		);
204 204
 
205 205
 
206
-        $this->_table_has_not_changed_since_previous(
207
-            'esp_event_message_template',
208
-            "EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
206
+		$this->_table_has_not_changed_since_previous(
207
+			'esp_event_message_template',
208
+			"EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
209 209
 				EVT_ID bigint(20) unsigned NOT NULL DEFAULT 0,
210 210
 				GRP_ID int(10) unsigned NOT NULL DEFAULT 0,
211 211
 				PRIMARY KEY  (EMT_ID),
212 212
 				KEY EVT_ID (EVT_ID),
213 213
 				KEY GRP_ID (GRP_ID)"
214
-        );
214
+		);
215 215
 
216 216
 
217
-        $this->_table_is_changed_in_this_version(
218
-            'esp_event_meta',
219
-            "EVTM_ID int(10) NOT NULL AUTO_INCREMENT,
217
+		$this->_table_is_changed_in_this_version(
218
+			'esp_event_meta',
219
+			"EVTM_ID int(10) NOT NULL AUTO_INCREMENT,
220 220
 				EVT_ID bigint(20) unsigned NOT NULL,
221 221
 			    VNU_ID bigint(20) unsigned NOT NULL DEFAULT 0,
222 222
 				EVT_display_desc tinyint(1) unsigned NOT NULL DEFAULT 1,
@@ -234,12 +234,12 @@  discard block
 block discarded – undo
234 234
 				PRIMARY KEY  (EVTM_ID),
235 235
 				KEY EVT_ID (EVT_ID),
236 236
 				KEY VNU_ID (VNU_ID)"
237
-        );
237
+		);
238 238
 
239 239
 
240
-        $this->_table_has_not_changed_since_previous(
241
-            'esp_event_question_group',
242
-            "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
240
+		$this->_table_has_not_changed_since_previous(
241
+			'esp_event_question_group',
242
+			"EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
243 243
 				EVT_ID bigint(20) unsigned NOT NULL,
244 244
 				QSG_ID int(10) unsigned NOT NULL,
245 245
 				EQG_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
@@ -247,34 +247,34 @@  discard block
 block discarded – undo
247 247
 				PRIMARY KEY  (EQG_ID),
248 248
 				KEY EVT_ID (EVT_ID),
249 249
 				KEY QSG_ID (QSG_ID)"
250
-        );
250
+		);
251 251
 
252 252
 
253
-        $this->_table_has_not_changed_since_previous(
254
-            'esp_event_venue',
255
-            "EVV_ID int(11) NOT NULL AUTO_INCREMENT,
253
+		$this->_table_has_not_changed_since_previous(
254
+			'esp_event_venue',
255
+			"EVV_ID int(11) NOT NULL AUTO_INCREMENT,
256 256
 				EVT_ID bigint(20) unsigned NOT NULL,
257 257
 				VNU_ID bigint(20) unsigned NOT NULL,
258 258
 				EVV_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
259 259
 				PRIMARY KEY  (EVV_ID)"
260
-        );
260
+		);
261 261
 
262 262
 
263
-        $this->_table_has_not_changed_since_previous(
264
-            'esp_extra_meta',
265
-            "EXM_ID int(11) NOT NULL AUTO_INCREMENT,
263
+		$this->_table_has_not_changed_since_previous(
264
+			'esp_extra_meta',
265
+			"EXM_ID int(11) NOT NULL AUTO_INCREMENT,
266 266
 				OBJ_ID int(11) DEFAULT NULL,
267 267
 				EXM_type varchar(45) DEFAULT NULL,
268 268
 				EXM_key varchar(45) DEFAULT NULL,
269 269
 				EXM_value text,
270 270
 				PRIMARY KEY  (EXM_ID),
271 271
 				KEY EXM_type (EXM_type,OBJ_ID,EXM_key)"
272
-        );
272
+		);
273 273
 
274 274
 
275
-        $this->_table_has_not_changed_since_previous(
276
-            'esp_extra_join',
277
-            "EXJ_ID int(11) NOT NULL AUTO_INCREMENT,
275
+		$this->_table_has_not_changed_since_previous(
276
+			'esp_extra_join',
277
+			"EXJ_ID int(11) NOT NULL AUTO_INCREMENT,
278 278
 				EXJ_first_model_id varchar(12) NOT NULL,
279 279
 				EXJ_first_model_name varchar(20) NOT NULL,
280 280
 				EXJ_second_model_id varchar(12) NOT NULL,
@@ -282,12 +282,12 @@  discard block
 block discarded – undo
282 282
 				PRIMARY KEY  (EXJ_ID),
283 283
 				KEY first_model (EXJ_first_model_name,EXJ_first_model_id),
284 284
 				KEY second_model (EXJ_second_model_name,EXJ_second_model_id)"
285
-        );
285
+		);
286 286
 
287 287
 
288
-        $this->_table_is_new_in_this_version(
289
-            'esp_form_element',
290
-            "FIN_UUID varchar(25) NOT NULL,
288
+		$this->_table_is_new_in_this_version(
289
+			'esp_form_element',
290
+			"FIN_UUID varchar(25) NOT NULL,
291 291
 				FSC_UUID varchar(25) NOT NULL,
292 292
 				FIN_adminOnly tinyint(1) unsigned NOT NULL DEFAULT 0,
293 293
 				FIN_attributes text DEFAULT NULL,
@@ -302,14 +302,14 @@  discard block
 block discarded – undo
302 302
 				FIN_wpUser bigint(20) unsigned DEFAULT NULL,
303 303
 				PRIMARY KEY  (FIN_UUID),
304 304
 				KEY FSC_UUID (FSC_UUID)"
305
-        );
306
-        $this->_get_table_manager()->dropIndex('esp_form_element', 'FIN_order');
307
-        $this->_get_table_manager()->dropIndex('esp_form_element', 'FIN_status');
305
+		);
306
+		$this->_get_table_manager()->dropIndex('esp_form_element', 'FIN_order');
307
+		$this->_get_table_manager()->dropIndex('esp_form_element', 'FIN_status');
308 308
 
309 309
 
310
-        $this->_table_is_new_in_this_version(
311
-            'esp_form_section',
312
-            "FSC_UUID varchar(25) NOT NULL,
310
+		$this->_table_is_new_in_this_version(
311
+			'esp_form_section',
312
+			"FSC_UUID varchar(25) NOT NULL,
313 313
 				FSC_appliesTo tinytext NOT NULL,
314 314
 				FSC_attributes text DEFAULT NULL,
315 315
 				FSC_belongsTo varchar(25) DEFAULT NULL,
@@ -319,14 +319,14 @@  discard block
 block discarded – undo
319 319
 				FSC_wpUser bigint(20) unsigned DEFAULT NULL,
320 320
 				PRIMARY KEY  (FSC_UUID),
321 321
 				KEY FSC_belongsTo (FSC_belongsTo)"
322
-        );
323
-        $this->_get_table_manager()->dropIndex('esp_form_section', 'FSC_order');
324
-        $this->_get_table_manager()->dropIndex('esp_form_section', 'FSC_status');
322
+		);
323
+		$this->_get_table_manager()->dropIndex('esp_form_section', 'FSC_order');
324
+		$this->_get_table_manager()->dropIndex('esp_form_section', 'FSC_status');
325 325
 
326 326
 
327
-        $this->_table_is_new_in_this_version(
328
-            'esp_form_submission',
329
-            "FSB_UUID varchar(25) NOT NULL,
327
+		$this->_table_is_new_in_this_version(
328
+			'esp_form_submission',
329
+			"FSB_UUID varchar(25) NOT NULL,
330 330
                 FSC_UUID varchar(25) NOT NULL,
331 331
 				TXN_ID int(10) DEFAULT NULL,
332 332
 				FSB_data mediumtext DEFAULT NULL,
@@ -334,12 +334,12 @@  discard block
 block discarded – undo
334 334
 				PRIMARY KEY  (FSB_UUID),
335 335
 				KEY FSC_UUID (FSC_UUID),
336 336
 				KEY TXN_ID (TXN_ID)"
337
-        );
337
+		);
338 338
 
339 339
 
340
-        $this->_table_is_changed_in_this_version(
341
-            'esp_line_item',
342
-            "LIN_ID int(11) NOT NULL AUTO_INCREMENT,
340
+		$this->_table_is_changed_in_this_version(
341
+			'esp_line_item',
342
+			"LIN_ID int(11) NOT NULL AUTO_INCREMENT,
343 343
 				LIN_code varchar(245) NOT NULL DEFAULT '',
344 344
 				TXN_ID int(10) DEFAULT NULL,
345 345
 				LIN_name varchar(245) NOT NULL DEFAULT '',
@@ -361,12 +361,12 @@  discard block
 block discarded – undo
361 361
 				KEY txn_type_timestamp (TXN_ID,LIN_type,LIN_timestamp),
362 362
 				KEY txn_obj_id_obj_type (TXN_ID,OBJ_ID,OBJ_type),
363 363
 				KEY obj_id_obj_type (OBJ_ID,OBJ_type)"
364
-        );
364
+		);
365 365
 
366 366
 
367
-        $this->_table_has_not_changed_since_previous(
368
-            'esp_log',
369
-            "LOG_ID int(11) NOT NULL AUTO_INCREMENT,
367
+		$this->_table_has_not_changed_since_previous(
368
+			'esp_log',
369
+			"LOG_ID int(11) NOT NULL AUTO_INCREMENT,
370 370
 				LOG_time datetime DEFAULT NULL,
371 371
 				OBJ_ID varchar(45) DEFAULT NULL,
372 372
 				OBJ_type varchar(45) DEFAULT NULL,
@@ -377,12 +377,12 @@  discard block
 block discarded – undo
377 377
 				KEY LOG_time (LOG_time),
378 378
 				KEY OBJ (OBJ_type,OBJ_ID),
379 379
 				KEY LOG_type (LOG_type)"
380
-        );
380
+		);
381 381
 
382 382
 
383
-        $this->_table_has_not_changed_since_previous(
384
-            'esp_message',
385
-            "MSG_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
383
+		$this->_table_has_not_changed_since_previous(
384
+			'esp_message',
385
+			"MSG_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
386 386
 				GRP_ID int(10) unsigned NULL,
387 387
 				MSG_token varchar(255) NULL,
388 388
 				TXN_ID int(10) unsigned NULL,
@@ -406,32 +406,32 @@  discard block
 block discarded – undo
406 406
 				KEY MSG_to (MSG_to(191)),
407 407
 				KEY MSG_from (MSG_from(191)),
408 408
 				KEY MSG_modified (MSG_modified)"
409
-        );
410
-        $this->_get_table_manager()->dropIndex('esp_message', 'MSG_messenger');
411
-        $this->_get_table_manager()->dropIndex('esp_message', 'MSG_message_type');
412
-        $this->_get_table_manager()->dropIndex('esp_message', 'MSG_context');
413
-        $this->_get_table_manager()->dropIndex('esp_message', 'MSG_recipient_type');
414
-        $this->_get_table_manager()->dropIndex('esp_message', 'MSG_subject');
415
-        $this->_get_table_manager()->dropIndex('esp_message', 'MSG_created');
416
-        $this->_get_table_manager()->dropIndex('esp_message', 'MSG_priority');
417
-        $this->_get_table_manager()->dropIndex('esp_message', 'STS_ID');
418
-
419
-
420
-        $this->_table_has_not_changed_since_previous(
421
-            'esp_message_template',
422
-            "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
409
+		);
410
+		$this->_get_table_manager()->dropIndex('esp_message', 'MSG_messenger');
411
+		$this->_get_table_manager()->dropIndex('esp_message', 'MSG_message_type');
412
+		$this->_get_table_manager()->dropIndex('esp_message', 'MSG_context');
413
+		$this->_get_table_manager()->dropIndex('esp_message', 'MSG_recipient_type');
414
+		$this->_get_table_manager()->dropIndex('esp_message', 'MSG_subject');
415
+		$this->_get_table_manager()->dropIndex('esp_message', 'MSG_created');
416
+		$this->_get_table_manager()->dropIndex('esp_message', 'MSG_priority');
417
+		$this->_get_table_manager()->dropIndex('esp_message', 'STS_ID');
418
+
419
+
420
+		$this->_table_has_not_changed_since_previous(
421
+			'esp_message_template',
422
+			"MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
423 423
 				GRP_ID int(10) unsigned NOT NULL,
424 424
 				MTP_context varchar(50) NOT NULL,
425 425
 				MTP_template_field varchar(30) NOT NULL,
426 426
 				MTP_content text NOT NULL,
427 427
 				PRIMARY KEY  (MTP_ID),
428 428
 				KEY GRP_ID (GRP_ID)"
429
-        );
429
+		);
430 430
 
431 431
 
432
-        $this->_table_has_not_changed_since_previous(
433
-            'esp_message_template_group',
434
-            "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
432
+		$this->_table_has_not_changed_since_previous(
433
+			'esp_message_template_group',
434
+			"GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
435 435
 				MTP_user_id int(10) NOT NULL DEFAULT '1',
436 436
 				MTP_name varchar(245) NOT NULL DEFAULT '',
437 437
 				MTP_description varchar(245) NOT NULL DEFAULT '',
@@ -443,12 +443,12 @@  discard block
 block discarded – undo
443 443
 				MTP_is_active tinyint(1) NOT NULL DEFAULT '1',
444 444
 				PRIMARY KEY  (GRP_ID),
445 445
 				KEY MTP_user_id (MTP_user_id)"
446
-        );
446
+		);
447 447
 
448 448
 
449
-        $this->_table_has_not_changed_since_previous(
450
-            'esp_payment',
451
-            "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
449
+		$this->_table_has_not_changed_since_previous(
450
+			'esp_payment',
451
+			"PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
452 452
 				TXN_ID int(10) unsigned DEFAULT NULL,
453 453
 				STS_ID varchar(3) DEFAULT NULL,
454 454
 				PAY_timestamp datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
@@ -465,12 +465,12 @@  discard block
 block discarded – undo
465 465
 				PRIMARY KEY  (PAY_ID),
466 466
 				KEY PAY_timestamp (PAY_timestamp),
467 467
 				KEY TXN_ID (TXN_ID)"
468
-        );
468
+		);
469 469
 
470 470
 
471
-        $this->_table_has_not_changed_since_previous(
472
-            'esp_payment_method',
473
-            "PMD_ID int(11) NOT NULL AUTO_INCREMENT,
471
+		$this->_table_has_not_changed_since_previous(
472
+			'esp_payment_method',
473
+			"PMD_ID int(11) NOT NULL AUTO_INCREMENT,
474 474
 				PMD_type varchar(124) DEFAULT NULL,
475 475
 				PMD_name varchar(255) DEFAULT NULL,
476 476
 				PMD_desc text,
@@ -485,13 +485,13 @@  discard block
 block discarded – undo
485 485
 				PMD_scope varchar(255) NULL DEFAULT 'frontend',
486 486
 				PRIMARY KEY  (PMD_ID),
487 487
 				UNIQUE KEY PMD_slug_UNIQUE (PMD_slug)"
488
-        );
489
-        $this->_get_table_manager()->dropIndex('esp_payment_method', 'PMD_type');
488
+		);
489
+		$this->_get_table_manager()->dropIndex('esp_payment_method', 'PMD_type');
490 490
 
491 491
 
492
-        $this->_table_is_changed_in_this_version(
493
-            'esp_price',
494
-            "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
492
+		$this->_table_is_changed_in_this_version(
493
+			'esp_price',
494
+			"PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
495 495
 				PRT_ID tinyint(3) unsigned NOT NULL,
496 496
 				PRC_amount decimal(12,6) NOT NULL DEFAULT '0.00',
497 497
 				PRC_name varchar(245) NOT NULL,
@@ -504,12 +504,12 @@  discard block
 block discarded – undo
504 504
 				PRC_parent int(10) unsigned DEFAULT 0,
505 505
 				PRIMARY KEY  (PRC_ID),
506 506
 				KEY PRT_ID (PRT_ID)"
507
-        );
507
+		);
508 508
 
509 509
 
510
-        $this->_table_has_not_changed_since_previous(
511
-            'esp_price_type',
512
-            "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
510
+		$this->_table_has_not_changed_since_previous(
511
+			'esp_price_type',
512
+			"PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
513 513
 				PRT_name varchar(45) NOT NULL,
514 514
 				PBT_ID tinyint(3) unsigned NOT NULL DEFAULT '1',
515 515
 				PRT_is_percent tinyint(1) NOT NULL DEFAULT '0',
@@ -518,33 +518,33 @@  discard block
 block discarded – undo
518 518
 				PRT_deleted tinyint(1) NOT NULL DEFAULT '0',
519 519
 				UNIQUE KEY PRT_name_UNIQUE (PRT_name),
520 520
 				PRIMARY KEY  (PRT_ID)"
521
-        );
521
+		);
522 522
 
523 523
 
524
-        $this->_table_has_not_changed_since_previous(
525
-            'esp_ticket_price',
526
-            "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
524
+		$this->_table_has_not_changed_since_previous(
525
+			'esp_ticket_price',
526
+			"TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
527 527
 				TKT_ID int(10) unsigned NOT NULL,
528 528
 				PRC_ID int(10) unsigned NOT NULL,
529 529
 				PRIMARY KEY  (TKP_ID),
530 530
 				KEY TKT_ID (TKT_ID),
531 531
 				KEY PRC_ID (PRC_ID)"
532
-        );
532
+		);
533 533
 
534 534
 
535
-        $this->_table_has_not_changed_since_previous(
536
-            'esp_ticket_template',
537
-            "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
535
+		$this->_table_has_not_changed_since_previous(
536
+			'esp_ticket_template',
537
+			"TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
538 538
 				TTM_name varchar(45) NOT NULL,
539 539
 				TTM_description text,
540 540
 				TTM_file varchar(45),
541 541
 				PRIMARY KEY  (TTM_ID)"
542
-        );
542
+		);
543 543
 
544 544
 
545
-        $this->_table_has_not_changed_since_previous(
546
-            'esp_question',
547
-            "QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
545
+		$this->_table_has_not_changed_since_previous(
546
+			'esp_question',
547
+			"QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
548 548
 				QST_display_text text NOT NULL,
549 549
 				QST_admin_label varchar(255) NOT NULL,
550 550
 				QST_system varchar(25) DEFAULT NULL,
@@ -557,13 +557,13 @@  discard block
 block discarded – undo
557 557
 				QST_wp_user bigint(20) unsigned NULL,
558 558
 				QST_deleted tinyint(2) unsigned NOT NULL DEFAULT 0,
559 559
 				PRIMARY KEY  (QST_ID)"
560
-        );
561
-        $this->_get_table_manager()->dropIndex('esp_question', 'QST_order');
560
+		);
561
+		$this->_get_table_manager()->dropIndex('esp_question', 'QST_order');
562 562
 
563 563
 
564
-        $this->_table_has_not_changed_since_previous(
565
-            'esp_question_group',
566
-            "QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
564
+		$this->_table_has_not_changed_since_previous(
565
+			'esp_question_group',
566
+			"QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
567 567
 				QSG_name varchar(255) NOT NULL,
568 568
 				QSG_identifier varchar(100) NOT NULL,
569 569
 				QSG_desc text NULL,
@@ -576,25 +576,25 @@  discard block
 block discarded – undo
576 576
 				PRIMARY KEY  (QSG_ID),
577 577
 				UNIQUE KEY QSG_identifier_UNIQUE (QSG_identifier),
578 578
 				KEY QSG_order (QSG_order)"
579
-        );
580
-        $this->_get_table_manager()->dropIndex('esp_question_group', 'QSG_order');
579
+		);
580
+		$this->_get_table_manager()->dropIndex('esp_question_group', 'QSG_order');
581 581
 
582 582
 
583
-        $this->_table_has_not_changed_since_previous(
584
-            'esp_question_group_question',
585
-            "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
583
+		$this->_table_has_not_changed_since_previous(
584
+			'esp_question_group_question',
585
+			"QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
586 586
 				QSG_ID int(10) unsigned NOT NULL,
587 587
 				QST_ID int(10) unsigned NOT NULL,
588 588
 				QGQ_order int(10) unsigned NOT NULL DEFAULT 0,
589 589
 				PRIMARY KEY  (QGQ_ID),
590 590
 				KEY QST_ID (QST_ID)"
591
-        );
592
-        $this->_get_table_manager()->dropIndex('esp_question_group_question', 'QSG_ID_order');
591
+		);
592
+		$this->_get_table_manager()->dropIndex('esp_question_group_question', 'QSG_ID_order');
593 593
 
594 594
 
595
-        $this->_table_has_not_changed_since_previous(
596
-            'esp_question_option',
597
-            "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
595
+		$this->_table_has_not_changed_since_previous(
596
+			'esp_question_option',
597
+			"QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
598 598
 				QSO_value varchar(255) NOT NULL,
599 599
 				QSO_desc text NOT NULL,
600 600
 				QST_ID int(10) unsigned NOT NULL,
@@ -603,13 +603,13 @@  discard block
 block discarded – undo
603 603
 				QSO_deleted tinyint(1) unsigned NOT NULL DEFAULT 0,
604 604
 				PRIMARY KEY  (QSO_ID),
605 605
 				KEY QST_ID (QST_ID)"
606
-        );
607
-        $this->_get_table_manager()->dropIndex('esp_question_option', 'QSO_order');
606
+		);
607
+		$this->_get_table_manager()->dropIndex('esp_question_option', 'QSO_order');
608 608
 
609 609
 
610
-        $this->_table_has_not_changed_since_previous(
611
-            'esp_registration',
612
-            "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
610
+		$this->_table_has_not_changed_since_previous(
611
+			'esp_registration',
612
+			"REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
613 613
 				EVT_ID bigint(20) unsigned NOT NULL,
614 614
 				ATT_ID bigint(20) unsigned NOT NULL,
615 615
 				TXN_ID int(10) unsigned NOT NULL,
@@ -633,25 +633,25 @@  discard block
 block discarded – undo
633 633
 				KEY ATT_ID (ATT_ID),
634 634
 				KEY TKT_ID (TKT_ID),
635 635
 				KEY EVT_ID (EVT_ID)"
636
-        );
637
-        $this->_get_table_manager()->dropIndex('esp_registration', 'STS_ID');
636
+		);
637
+		$this->_get_table_manager()->dropIndex('esp_registration', 'STS_ID');
638 638
 
639 639
 
640
-        $this->_table_has_not_changed_since_previous(
641
-            'esp_registration_payment',
642
-            "RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
640
+		$this->_table_has_not_changed_since_previous(
641
+			'esp_registration_payment',
642
+			"RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
643 643
 					  REG_ID int(10) unsigned NOT NULL,
644 644
 					  PAY_ID int(10) unsigned NULL,
645 645
 					  RPY_amount decimal(12,3) NOT NULL DEFAULT '0.00',
646 646
 					  PRIMARY KEY  (RPY_ID),
647 647
 					  KEY REG_ID (REG_ID),
648 648
 					  KEY PAY_ID (PAY_ID)"
649
-        );
649
+		);
650 650
 
651 651
 
652
-        $this->_table_has_not_changed_since_previous(
653
-            'esp_state',
654
-            "STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT,
652
+		$this->_table_has_not_changed_since_previous(
653
+			'esp_state',
654
+			"STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT,
655 655
 				CNT_ISO varchar(2) NOT NULL,
656 656
 				STA_abbrev varchar(24) NOT NULL,
657 657
 				STA_name varchar(100) NOT NULL,
@@ -659,12 +659,12 @@  discard block
 block discarded – undo
659 659
 				PRIMARY KEY  (STA_ID),
660 660
 				KEY STA_abbrev (STA_abbrev),
661 661
 				KEY CNT_ISO (CNT_ISO)"
662
-        );
662
+		);
663 663
 
664 664
 
665
-        $this->_table_has_not_changed_since_previous(
666
-            'esp_status',
667
-            "STS_ID varchar(3) NOT NULL,
665
+		$this->_table_has_not_changed_since_previous(
666
+			'esp_status',
667
+			"STS_ID varchar(3) NOT NULL,
668 668
 				STS_code varchar(45) NOT NULL,
669 669
 				STS_type varchar(45) NOT NULL,
670 670
 				STS_can_edit tinyint(1) NOT NULL DEFAULT 0,
@@ -672,12 +672,12 @@  discard block
 block discarded – undo
672 672
 				STS_open tinyint(1) NOT NULL DEFAULT 1,
673 673
 				UNIQUE KEY STS_ID_UNIQUE (STS_ID),
674 674
 				KEY STS_type (STS_type)"
675
-        );
675
+		);
676 676
 
677 677
 
678
-        $this->_table_is_changed_in_this_version(
679
-            'esp_ticket',
680
-            "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
678
+		$this->_table_is_changed_in_this_version(
679
+			'esp_ticket',
680
+			"TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
681 681
 				TTM_ID int(10) unsigned NOT NULL,
682 682
 				TKT_name varchar(245) NOT NULL DEFAULT '',
683 683
 				TKT_description text NOT NULL,
@@ -703,12 +703,12 @@  discard block
 block discarded – undo
703 703
 				PRIMARY KEY  (TKT_ID),
704 704
 				KEY TKT_start_date (TKT_start_date),
705 705
 				KEY TKT_end_date (TKT_end_date)"
706
-        );
706
+		);
707 707
 
708 708
 
709
-        $this->_table_has_not_changed_since_previous(
710
-            'esp_transaction',
711
-            "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
709
+		$this->_table_has_not_changed_since_previous(
710
+			'esp_transaction',
711
+			"TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
712 712
 				TXN_timestamp datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
713 713
 				TXN_total decimal(12,3) DEFAULT '0.00',
714 714
 				TXN_paid decimal(12,3) NOT NULL DEFAULT '0.00',
@@ -719,13 +719,13 @@  discard block
 block discarded – undo
719 719
 				TXN_reg_steps text,
720 720
 				PRIMARY KEY  (TXN_ID),
721 721
 				KEY TXN_timestamp (TXN_timestamp)"
722
-        );
723
-        $this->_get_table_manager()->dropIndex('esp_transaction', 'STS_ID');
722
+		);
723
+		$this->_get_table_manager()->dropIndex('esp_transaction', 'STS_ID');
724 724
 
725 725
 
726
-        $this->_table_has_not_changed_since_previous(
727
-            'esp_venue_meta',
728
-            'VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
726
+		$this->_table_has_not_changed_since_previous(
727
+			'esp_venue_meta',
728
+			'VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
729 729
 			VNU_ID bigint(20) unsigned NOT NULL DEFAULT 0,
730 730
 			VNU_address varchar(255) DEFAULT NULL,
731 731
 			VNU_address2 varchar(255) DEFAULT NULL,
@@ -744,26 +744,26 @@  discard block
 block discarded – undo
744 744
 			KEY VNU_ID (VNU_ID),
745 745
 			KEY STA_ID (STA_ID),
746 746
 			KEY CNT_ISO (CNT_ISO)'
747
-        );
747
+		);
748 748
 
749 749
 
750
-        $this->previous_dms->insert_default_data();
751
-        $LegacyTextDomainOptions = new LegacyTextDomainOptions();
752
-        $LegacyTextDomainOptions->convertToConsolidatedFormat();
753
-        return true;
754
-    }
750
+		$this->previous_dms->insert_default_data();
751
+		$LegacyTextDomainOptions = new LegacyTextDomainOptions();
752
+		$LegacyTextDomainOptions->convertToConsolidatedFormat();
753
+		return true;
754
+	}
755 755
 
756 756
 
757
-    /**
758
-     * @return bool
759
-     */
760
-    public function schema_changes_after_migration(): bool
761
-    {
762
-        return true;
763
-    }
757
+	/**
758
+	 * @return bool
759
+	 */
760
+	public function schema_changes_after_migration(): bool
761
+	{
762
+		return true;
763
+	}
764 764
 
765 765
 
766
-    public function migration_page_hooks()
767
-    {
768
-    }
766
+	public function migration_page_hooks()
767
+	{
768
+	}
769 769
 }
Please login to merge, or discard this patch.
core/data_migration_scripts/EE_Data_Migration_Script_Base.core.php 1 patch
Indentation   +914 added lines, -914 removed lines patch added patch discarded remove patch
@@ -15,918 +15,918 @@
 block discarded – undo
15 15
  */
16 16
 abstract class EE_Data_Migration_Script_Base extends EE_Data_Migration_Class_Base
17 17
 {
18
-    /**
19
-     * Set by client code to indicate this DMS is being ran as part of a proper migration,
20
-     * instead of being used to merely setup (or verify) the database structure.
21
-     * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration
22
-     * should call EE_Data_Migration_Script_Base::set_migrating( FALSE )
23
-     *
24
-     * @var boolean
25
-     */
26
-    protected bool $_migrating = true;
27
-
28
-    /**
29
-     * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object
30
-     *
31
-     * @var EE_Data_Migration_Script_Stage[] $migration_functions
32
-     */
33
-    protected array $_migration_stages = array();
34
-
35
-    /**
36
-     * Indicates we've already run the schema changes that needed to happen BEFORE the data migration
37
-     *
38
-     * @var boolean
39
-     */
40
-    protected ?bool $_schema_changes_before_migration_ran = null;
41
-
42
-    /**
43
-     * Indicates we've already run the schema changes that needed to happen AFTER the data migration
44
-     *
45
-     * @var boolean
46
-     */
47
-    protected ?bool $_schema_changes_after_migration_ran = null;
48
-
49
-    /**
50
-     * String which describes what's currently happening in this migration
51
-     *
52
-     * @var string|null
53
-     */
54
-    protected ?string $_feedback_message = '';
55
-
56
-    /**
57
-     * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers
58
-     * correspond to earlier execution
59
-     *
60
-     * @var int
61
-     */
62
-    protected int $_priority = 5;
63
-
64
-    /**
65
-     * Multidimensional array that defines the mapping from OLD table Primary Keys
66
-     * to NEW table Primary Keys.
67
-     * Top-level array keys are OLD table names (minus the "wp_" part),
68
-     * 2nd-level array keys are NEW table names (again, minus the "wp_" part),
69
-     * 3rd-level array keys are the OLD table primary keys
70
-     * and 3rd-level array values are the NEW table primary keys
71
-     *
72
-     * @var array
73
-     */
74
-    protected array $_mappings = array();
75
-
76
-    /**
77
-     * @var EE_Data_Migration_Script_Base
78
-     */
79
-    protected EE_Data_Migration_Script_Base $previous_dms;
80
-
81
-
82
-    /**
83
-     * Returns whether this data migration script can operate on the given version of the database.
84
-     * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and
85
-     * it's passed a string like '3.1.38B', it should return true.
86
-     * If this DMS is to migrate data from an EE3 addon, you will probably want to use
87
-     * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and
88
-     * EE_Data_Migration_Manager::get_migration_ran() to check that core was already
89
-     * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated
90
-     * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want
91
-     * to run this DMS).
92
-     * If this DMS migrates data from a previous version of this EE4 addon, just
93
-     * comparing $current_database_state_of[ $this->slug() ] will probably suffice.
94
-     * If this DMS should never migrate data, because it's only used to define the initial
95
-     * database state, just return FALSE (and core's activation process will take care
96
-     * of calling its schema_changes_before_migration() and
97
-     * schema_changes_after_migration() for you. )
98
-     *
99
-     * @param array $version_array keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc)
100
-     * @return boolean
101
-     */
102
-    abstract public function can_migrate_from_version($version_array);
103
-
104
-
105
-    /**
106
-     * Performs database schema changes that need to occur BEFORE the data is migrated.
107
-     * Eg, if we were going to change user passwords from plaintext to encoded versions
108
-     * during this migration, this would probably add a new column called something like
109
-     * "encoded_password".
110
-     *
111
-     * @return boolean of success
112
-     */
113
-    abstract public function schema_changes_before_migration();
114
-
115
-
116
-    /**
117
-     * Performs the database schema changes that need to occur AFTER the data has been migrated.
118
-     * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords
119
-     * from plaintext to encoded versions, and we had added a column called "encoded_password",
120
-     * this function would probably remove the old column "password" (which still holds the plaintext password)
121
-     * and possibly rename "encoded_password" to "password"
122
-     *
123
-     * @return boolean of success
124
-     */
125
-    abstract public function schema_changes_after_migration();
126
-
127
-
128
-    /**
129
-     * All children of this must call parent::__construct()
130
-     * at the end of their constructor or suffer the consequences!
131
-     *
132
-     * @param TableManager|null  $table_manager
133
-     * @param TableAnalysis|null $table_analysis
134
-     */
135
-    public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
136
-    {
137
-        $this->_migration_stages = (array) apply_filters(
138
-            'FHEE__' . get_class($this) . '__construct__migration_stages',
139
-            $this->_migration_stages
140
-        );
141
-        foreach ($this->_migration_stages as $migration_stage) {
142
-            if ($migration_stage instanceof EE_Data_Migration_Script_Stage) {
143
-                $migration_stage->_construct_finalize($this);
144
-            }
145
-        }
146
-        parent::__construct($table_manager, $table_analysis);
147
-    }
148
-
149
-
150
-    /**
151
-     * Place to add hooks and filters for tweaking the migrations page, in order
152
-     * to customize it
153
-     */
154
-    public function migration_page_hooks()
155
-    {
156
-        // by default none are added because we normally like the default look of the migration page
157
-    }
158
-
159
-
160
-    /**
161
-     * Sets the mapping from old table primary keys to new table primary keys.
162
-     * This mapping is automatically persisted as a property on the migration
163
-     *
164
-     * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
165
-     * @param int|string $old_pk    old primary key. Eg events_detail.id's value
166
-     * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
167
-     * @param array|int|string $new_pk    eg posts.ID
168
-     * @return void
169
-     * @throws EE_Error
170
-     */
171
-    public function set_mapping($old_table, $old_pk, $new_table, $new_pk)
172
-    {
173
-        // make sure it has the needed keys
174
-        if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) {
175
-            $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
176
-        }
177
-        $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk;
178
-    }
179
-
180
-
181
-    /**
182
-     * Gets the new primary key, if provided with the OLD table and the primary key
183
-     * of an item in the old table, and the new table
184
-     *
185
-     * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
186
-     * @param int|string $old_pk    old primary key. Eg events_detail.id's value
187
-     * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
188
-     * @return mixed the primary key on the new table
189
-     * @throws EE_Error
190
-     */
191
-    public function get_mapping_new_pk($old_table, $old_pk, $new_table)
192
-    {
193
-        if (
194
-            ! isset($this->_mappings[ $old_table ]) ||
195
-            ! isset($this->_mappings[ $old_table ][ $new_table ])
196
-        ) {
197
-            // try fetching the option
198
-            $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
199
-        }
200
-        return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ])
201
-            ? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null;
202
-    }
203
-
204
-
205
-    /**
206
-     * Gets the old primary key, if provided with the OLD table,
207
-     * and the new table and the primary key of an item in the new table
208
-     *
209
-     * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail
210
-     * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts
211
-     * @param mixed  $new_pk
212
-     * @return mixed
213
-     * @throws EE_Error
214
-     */
215
-    public function get_mapping_old_pk($old_table, $new_table, $new_pk)
216
-    {
217
-        if (
218
-            ! isset($this->_mappings[ $old_table ]) ||
219
-            ! isset($this->_mappings[ $old_table ][ $new_table ])
220
-        ) {
221
-            // try fetching the option
222
-            $this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
223
-        }
224
-        if (isset($this->_mappings[ $old_table ][ $new_table ])) {
225
-            $new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]);
226
-            if (isset($new_pk_to_old_pk[ $new_pk ])) {
227
-                return $new_pk_to_old_pk[ $new_pk ];
228
-            }
229
-        }
230
-        return null;
231
-    }
232
-
233
-
234
-    /**
235
-     * Gets the mapping array option specified by the table names
236
-     *
237
-     * @param string $old_table_name
238
-     * @param string $new_table_name
239
-     * @return array
240
-     * @throws EE_Error
241
-     */
242
-    protected function _get_mapping_option($old_table_name, $new_table_name)
243
-    {
244
-        return get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array());
245
-    }
246
-
247
-
248
-    /**
249
-     * Updates the mapping option specified by the table names with the array provided
250
-     *
251
-     * @param string $old_table_name
252
-     * @param string $new_table_name
253
-     * @param array  $mapping_array
254
-     * @return boolean success of updating option
255
-     * @throws EE_Error
256
-     */
257
-    protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array)
258
-    {
259
-        $success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false);
260
-        return $success;
261
-    }
262
-
263
-
264
-    /**
265
-     * Gets the option name for this script to map from $old_table_name to $new_table_name
266
-     *
267
-     * @param string $old_table_name
268
-     * @param string $new_table_name
269
-     * @return string
270
-     * @throws EE_Error
271
-     */
272
-    protected function _get_mapping_option_name($old_table_name, $new_table_name)
273
-    {
274
-        global $wpdb;
275
-        $old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name);
276
-        $new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name);
277
-        $migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
278
-        return substr(
279
-            EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp,
280
-            0,
281
-            64
282
-        );
283
-    }
284
-
285
-
286
-    /**
287
-     * Counts all the records that will be migrated during this data migration.
288
-     * For example, if we were changing old user passwords from plaintext to encoded versions,
289
-     * this would be a count of all users who have passwords. If we were going to also split
290
-     * attendee records into transactions, registrations, and attendee records, this would include
291
-     * the count of all attendees currently in existence in the DB (ie, users + attendees).
292
-     * If you can't determine how many records there are to migrate, just provide a guess: this
293
-     * number will only be used in calculating the percent complete. If you estimate there to be
294
-     * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at
295
-     * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete.
296
-     *
297
-     * @return int
298
-     */
299
-    protected function _count_records_to_migrate()
300
-    {
301
-        $count = 0;
302
-        foreach ($this->stages() as $stage) {
303
-            $count += $stage->count_records_to_migrate();
304
-        }
305
-        return $count;
306
-    }
307
-
308
-
309
-    /**
310
-     * Returns the number of records updated so far. Usually this is easiest to do
311
-     * by just setting a transient and updating it after each migration_step
312
-     *
313
-     * @return int
314
-     */
315
-    public function count_records_migrated()
316
-    {
317
-        $count = 0;
318
-        foreach ($this->stages() as $stage) {
319
-            $count += $stage->count_records_migrated();
320
-        }
321
-        $this->_records_migrated = $count;
322
-        return $count;
323
-    }
324
-
325
-
326
-    /**
327
-     * @param int $num_records_to_migrate_limit
328
-     * @return int
329
-     * @throws EE_Error
330
-     * @throws Exception
331
-     */
332
-    public function migration_step($num_records_to_migrate_limit)
333
-    {
334
-        // reset the feedback message
335
-        $this->_feedback_message = '';
336
-        // if we haven't yet done the 1st schema changes, do them now. buffer any output
337
-        $this->_maybe_do_schema_changes(true);
338
-
339
-        $num_records_actually_migrated = 0;
340
-        $records_migrated_per_stage = array();
341
-        // setup the 'stage' variable, which should hold the last run stage of the migration  (or none at all if nothing runs)
342
-        $stage = null;
343
-        // get the next stage that isn't complete
344
-        foreach ($this->stages() as $stage) {
345
-            if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) {
346
-                try {
347
-                    $records_migrated_during_stage = $stage->migration_step(
348
-                        $num_records_to_migrate_limit - $num_records_actually_migrated
349
-                    );
350
-                    $num_records_actually_migrated += $records_migrated_during_stage;
351
-                    $records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage;
352
-                } catch (Exception $e) {
353
-                    // yes if we catch an exception here, we consider that migration stage borked.
354
-                    $stage->set_status(EE_Data_Migration_Manager::status_fatal_error);
355
-                    $this->set_status(EE_Data_Migration_Manager::status_fatal_error);
356
-                    $stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString());
357
-                    throw $e;
358
-                }
359
-                // check that the migration stage didn't mark itself as having a fatal error
360
-                if ($stage->is_broken()) {
361
-                    $this->set_broken();
362
-                    throw new EE_Error($stage->get_last_error());
363
-                }
364
-            }
365
-            // once we've migrated all the number we intended to (possibly from different stages), stop migrating
366
-            // or if we had a fatal error
367
-            // or if the current script stopped early- its not done, but it's done all it thinks we should do on this step
368
-            if (
369
-                $num_records_actually_migrated >= $num_records_to_migrate_limit
370
-                || $stage->is_broken()
371
-                || $stage->has_more_to_do()
372
-            ) {
373
-                break;
374
-            }
375
-        }
376
-        // check if we're all done this data migration...
377
-        // which is indicated by being done early AND the last stage claims to be done
378
-        if ($stage == null) {
379
-            // this migration script apparently has NO stages... which is super weird, but whatever
380
-            $this->set_completed();
381
-            $this->_maybe_do_schema_changes(false);
382
-        } elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) {
383
-            // apparently we're done, because we couldn't migrate the number we intended to
384
-            $this->set_completed();
385
-            $this->_update_feedback_message(array_reverse($records_migrated_per_stage));
386
-            // do schema changes for after the migration now
387
-            // first double-check we haven't already done this
388
-            $this->_maybe_do_schema_changes(false);
389
-        } else {
390
-            // update feedback message, keeping in mind that we show them with the most recent at the top
391
-            $this->_update_feedback_message(array_reverse($records_migrated_per_stage));
392
-        }
393
-        return $num_records_actually_migrated;
394
-    }
395
-
396
-
397
-    /**
398
-     * Updates the feedback message according to what was done during this migration stage.
399
-     *
400
-     * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records
401
-     *                                          migrated from that stage
402
-     * @return void
403
-     */
404
-    private function _update_feedback_message($records_migrated_per_stage)
405
-    {
406
-        $feedback_message_array = array();
407
-        foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) {
408
-            $feedback_message_array[] = sprintf(
409
-                esc_html__("Migrated %d records successfully during %s", "event_espresso"),
410
-                $num_records_migrated,
411
-                $migration_stage_name
412
-            );
413
-        }
414
-        $this->_feedback_message .= implode("<br>", $feedback_message_array);
415
-    }
416
-
417
-
418
-    /**
419
-     * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration
420
-     * (if $before==false). Buffers their outputs and stores them on the class.
421
-     *
422
-     * @param boolean $before
423
-     * @throws Exception
424
-     * @return void
425
-     */
426
-    private function _maybe_do_schema_changes($before = true)
427
-    {
428
-        // so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran
429
-        $property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran';
430
-        if (! $this->{$property_name}) {
431
-            try {
432
-                ob_start();
433
-                if ($before) {
434
-                    $this->schema_changes_before_migration();
435
-                } else {
436
-                    $this->schema_changes_after_migration();
437
-                }
438
-                $output = ob_get_contents();
439
-                ob_end_clean();
440
-            } catch (Exception $e) {
441
-                $this->set_status(EE_Data_Migration_Manager::status_fatal_error);
442
-                throw $e;
443
-            }
444
-            // record that we've done these schema changes
445
-            $this->{$property_name} = true;
446
-            // if there were any warnings etc, record them as non-fatal errors
447
-            if ($output) {
448
-                // there were some warnings
449
-                $this->_errors[] = $output;
450
-            }
451
-        }
452
-    }
453
-
454
-
455
-    /**
456
-     * Wrapper for EEH_Activation::create_table. However, takes into account the request type when
457
-     * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead
458
-     * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to
459
-     * or
460
-     * activated currently. If this is a brand new activation or a migration, and we're indicating this table should
461
-     * not
462
-     * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this
463
-     * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table
464
-     * should be nuked.
465
-     *
466
-     * Just for a bit of context, the migration script's db_schema_changes_* methods
467
-     * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the
468
-     * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was
469
-     * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should
470
-     * be dropped), and during a migration when we're moving the DB to the state of the migration script
471
-     *
472
-     * @param string $table_name
473
-     * @param string $table_definition_sql
474
-     * @param string $engine_string
475
-     * @throws EE_Error
476
-     * @throws ReflectionException
477
-     */
478
-    protected function _table_is_new_in_this_version(
479
-        $table_name,
480
-        $table_definition_sql,
481
-        $engine_string = 'ENGINE=InnoDB'
482
-    ) {
483
-        $this->_create_table_and_catch_errors(
484
-            $table_name,
485
-            $table_definition_sql,
486
-            $engine_string,
487
-            $this->_pre_existing_table_should_be_dropped(true)
488
-        );
489
-    }
490
-
491
-
492
-    /**
493
-     * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table
494
-     * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not
495
-     * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is
496
-     * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as
497
-     * _table_should_exist_previously.
498
-     * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the
499
-     * table shouldn't exist).
500
-     *
501
-     * @param string $table_name
502
-     * @param string $table_definition_sql
503
-     * @param string $engine_string
504
-     * @throws EE_Error
505
-     * @throws ReflectionException
506
-     */
507
-    protected function _table_is_changed_in_this_version(
508
-        $table_name,
509
-        $table_definition_sql,
510
-        $engine_string = 'ENGINE=InnoDB'
511
-    ) {
512
-        $this->_create_table_and_catch_errors(
513
-            $table_name,
514
-            $table_definition_sql,
515
-            $engine_string,
516
-            $this->_pre_existing_table_should_be_dropped(false)
517
-        );
518
-    }
519
-
520
-
521
-    /**
522
-     * _old_table_exists
523
-     * returns TRUE if the requested table exists in the current database
524
-     *
525
-     * @param string $table_name
526
-     * @return boolean
527
-     * @throws EE_Error
528
-     */
529
-    protected function _old_table_exists($table_name)
530
-    {
531
-        return $this->_get_table_analysis()->tableExists($table_name);
532
-    }
533
-
534
-
535
-    /**
536
-     * _delete_table_if_empty
537
-     * returns TRUE if the requested table was empty and successfully empty
538
-     *
539
-     * @param string $table_name
540
-     * @return boolean
541
-     * @throws EE_Error
542
-     * @throws ReflectionException
543
-     */
544
-    protected function _delete_table_if_empty($table_name)
545
-    {
546
-        return EEH_Activation::delete_db_table_if_empty($table_name);
547
-    }
548
-
549
-
550
-    /**
551
-     * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version
552
-     * as these are significantly more efficient or explicit.
553
-     * Please see description of _table_is_new_in_this_version. This function will only set
554
-     * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand
555
-     * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin"
556
-     * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually
557
-     * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table
558
-     * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then
559
-     * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations
560
-     * because apparently it hasn't changed since the previous one, right?
561
-     *
562
-     * @param string $table_name
563
-     * @param string $table_definition_sql
564
-     * @param string $engine_string
565
-     * @throws EE_Error
566
-     * @throws ReflectionException
567
-     */
568
-    protected function _table_should_exist_previously(
569
-        $table_name,
570
-        $table_definition_sql,
571
-        $engine_string = 'ENGINE=InnoDB'
572
-    ) {
573
-        $this->_create_table_and_catch_errors(
574
-            $table_name,
575
-            $table_definition_sql,
576
-            $engine_string,
577
-            $this->_pre_existing_table_should_be_dropped(false)
578
-        );
579
-    }
580
-
581
-
582
-    /**
583
-     * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing
584
-     * a migration, we skip checking this table's structure in the database and just assume it's correct.
585
-     * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs,
586
-     * but important for multisite where migrations can take a very long time otherwise).
587
-     * If the table is known to have changed since previous version, use _table_is_changed_in_this_version().
588
-     * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table
589
-     * shouldn't exist).
590
-     *
591
-     * @param string $table_name
592
-     * @param string $table_definition_sql
593
-     * @param string $engine_string
594
-     * @throws EE_Error
595
-     * @throws ReflectionException
596
-     */
597
-    protected function _table_has_not_changed_since_previous(
598
-        $table_name,
599
-        $table_definition_sql,
600
-        $engine_string = 'ENGINE=InnoDB'
601
-    ) {
602
-        if ($this->_currently_migrating()) {
603
-            // if we're doing a migration, and this table apparently already exists, then we don't need do anything right?
604
-            return;
605
-        }
606
-        $this->_create_table_and_catch_errors(
607
-            $table_name,
608
-            $table_definition_sql,
609
-            $engine_string,
610
-            $this->_pre_existing_table_should_be_dropped(false)
611
-        );
612
-    }
613
-
614
-    /**
615
-     * Returns whether this migration script is being used as part of an actual migration
616
-     *
617
-     * @return boolean
618
-     */
619
-    protected function _currently_migrating()
620
-    {
621
-        // we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM)
622
-        return $this->_migrating
623
-            && DbStatus::isOffline()
624
-            && $this->can_migrate_from_version(
625
-                EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set()
626
-            );
627
-    }
628
-
629
-
630
-    /**
631
-     * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new,
632
-     * and the plugin's request type.
633
-     * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table
634
-     * shouldn't exist no matter what).
635
-     *
636
-     * @param boolean $table_is_new
637
-     * @return boolean
638
-     * @throws EE_Error
639
-     */
640
-    protected function _pre_existing_table_should_be_dropped($table_is_new)
641
-    {
642
-        if ($table_is_new) {
643
-            if (
644
-                $this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation
645
-                || $this->_currently_migrating()
646
-            ) {
647
-                return true;
648
-            } else {
649
-                return false;
650
-            }
651
-        } else {
652
-            if (
653
-                in_array(
654
-                    $this->_get_req_type_for_plugin_corresponding_to_this_dms(),
655
-                    array(EE_System::req_type_new_activation)
656
-                )
657
-            ) {
658
-                return true;
659
-            } else {
660
-                return false;
661
-            }
662
-        }
663
-    }
664
-
665
-
666
-    /**
667
-     * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS
668
-     *
669
-     * @param string  $table_name
670
-     * @param string  $table_definition_sql
671
-     * @param string  $engine_string
672
-     * @param boolean $drop_pre_existing_tables
673
-     * @throws ReflectionException
674
-     */
675
-    private function _create_table_and_catch_errors(
676
-        $table_name,
677
-        $table_definition_sql,
678
-        $engine_string = 'ENGINE=InnoDB',
679
-        $drop_pre_existing_tables = false
680
-    ) {
681
-        try {
682
-            EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables);
683
-        } catch (EE_Error $e) {
684
-            $message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString();
685
-            $this->add_error($message);
686
-            $this->_feedback_message .= $message;
687
-        }
688
-    }
689
-
690
-
691
-    /**
692
-     * Gets the request type for the plugin (core or addon) that corresponds to this DMS
693
-     *
694
-     * @return int one of EE_System::_req_type_* constants
695
-     * @throws EE_Error
696
-     */
697
-    private function _get_req_type_for_plugin_corresponding_to_this_dms()
698
-    {
699
-        if ($this->slug() == 'Core') {
700
-            return EE_System::instance()->detect_req_type();
701
-        } else {// it must be for an addon
702
-            $addon_name = $this->slug();
703
-            if (EE_Registry::instance()->get_addon_by_name($addon_name)) {
704
-                return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type();
705
-            } else {
706
-                throw new EE_Error(
707
-                    sprintf(
708
-                        esc_html__(
709
-                            "The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s",
710
-                            "event_espresso"
711
-                        ),
712
-                        $this->slug(),
713
-                        $addon_name,
714
-                        implode(",", array_keys(EE_Registry::instance()->get_addons_by_name()))
715
-                    )
716
-                );
717
-            }
718
-        }
719
-    }
720
-
721
-
722
-    /**
723
-     * returns an array of strings describing errors by all the script's stages
724
-     *
725
-     * @return array
726
-     */
727
-    public function get_errors()
728
-    {
729
-        $all_errors = $this->_errors;
730
-        if (! is_array($all_errors)) {
731
-            $all_errors = array();
732
-        }
733
-        foreach ($this->stages() as $stage) {
734
-            $all_errors = array_merge($stage->get_errors(), $all_errors);
735
-        }
736
-        return $all_errors;
737
-    }
738
-
739
-
740
-    /**
741
-     * Indicates whether this migration script should continue
742
-     *
743
-     * @return boolean
744
-     */
745
-    public function can_continue()
746
-    {
747
-        return in_array(
748
-            $this->get_status(),
749
-            EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script
750
-        );
751
-    }
752
-
753
-
754
-    /**
755
-     * Gets all the data migration stages associated with this script. Note:
756
-     * addons can filter this list to add their own stages, and because the list is
757
-     * numerically-indexed, they can insert their stage wherever they like and it will
758
-     * get ordered by the indexes
759
-     *
760
-     * @return EE_Data_Migration_Script_Stage[]
761
-     */
762
-    protected function stages()
763
-    {
764
-        $stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages);
765
-        ksort($stages);
766
-        return $stages;
767
-    }
768
-
769
-
770
-    /**
771
-     * Gets a string which should describe what's going on currently with this migration, which
772
-     * can be displayed to the user
773
-     *
774
-     * @return string
775
-     */
776
-    public function get_feedback_message()
777
-    {
778
-        return $this->_feedback_message;
779
-    }
780
-
781
-
782
-    /**
783
-     * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class'
784
-     * properties to the DB. However, we don't want to use __sleep() because its quite
785
-     * possible that this class is defined when it goes to sleep, but NOT available when it
786
-     * awakes (eg, this class is part of an addon that is deactivated at some point).
787
-     */
788
-    public function properties_as_array()
789
-    {
790
-        $properties = parent::properties_as_array();
791
-        $properties['_migration_stages'] = array();
792
-        foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) {
793
-            $properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array(
794
-            );
795
-        }
796
-        unset($properties['_mappings']);
797
-        unset($properties['previous_dms']);
798
-
799
-        foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) {
800
-            foreach ($mapping_to_new_table as $new_table_name => $mapping) {
801
-                $this->_set_mapping_option($old_table_name, $new_table_name, $mapping);
802
-            }
803
-        }
804
-        return $properties;
805
-    }
806
-
807
-
808
-    /**
809
-     * Sets all of the properties of this script stage to match what's in the array, which is assumed
810
-     * to have been made from the properties_as_array() function.
811
-     *
812
-     * @param array $array_of_properties like what's produced from properties_as_array() method
813
-     * @return void
814
-     */
815
-    public function instantiate_from_array_of_properties($array_of_properties)
816
-    {
817
-        $stages_properties_arrays = $array_of_properties['_migration_stages'];
818
-        unset($array_of_properties['_migration_stages']);
819
-        unset($array_of_properties['class']);
820
-        foreach ($array_of_properties as $property_name => $property_value) {
821
-            $this->{$property_name} = $property_value;
822
-        }
823
-        // _migration_stages are already instantiated, but have only default data
824
-        foreach ($this->_migration_stages as $stage) {
825
-            $stage_data = $this->_find_migration_stage_data_with_classname(
826
-                get_class($stage),
827
-                $stages_properties_arrays
828
-            );
829
-            // SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by
830
-            // an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet)
831
-            if ($stage_data) {
832
-                $stage->instantiate_from_array_of_properties($stage_data);
833
-            }
834
-        }
835
-    }
836
-
837
-
838
-    /**
839
-     * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which
840
-     * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes)
841
-     * for the given classname
842
-     *
843
-     * @param string $classname
844
-     * @param array  $migration_stage_data_arrays
845
-     * @return null
846
-     */
847
-    private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays)
848
-    {
849
-        foreach ($migration_stage_data_arrays as $migration_stage_data_array) {
850
-            if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) {
851
-                return $migration_stage_data_array;
852
-            }
853
-        }
854
-        return null;
855
-    }
856
-
857
-
858
-    /**
859
-     * Returns the version that this script migrates to, based on the script's name.
860
-     * Cannot be overwritten because lots of code needs to know which version a script
861
-     * migrates to knowing only its name.
862
-     *
863
-     * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin
864
-     * that will be updated to. Eg array('Core','4.1.0')
865
-     * @throws EE_Error
866
-     */
867
-    final public function migrates_to_version()
868
-    {
869
-        return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
870
-    }
871
-
872
-
873
-    /**
874
-     * Gets this addon's slug as it would appear in the current_db_state wp option,
875
-     * and if this migration script is for an addon, it SHOULD match the addon's slug
876
-     * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon.
877
-     * Or 'Core' for core (non-addon).
878
-     *
879
-     * @return string
880
-     * @throws EE_Error
881
-     */
882
-    public function slug()
883
-    {
884
-        $migrates_to_version_info = $this->migrates_to_version();
885
-        // the slug is the first part of the array
886
-        return $migrates_to_version_info['slug'];
887
-    }
888
-
889
-
890
-    /**
891
-     * Returns the script's priority relative to DMSs from other addons. However, when
892
-     * two DMSs from the same addon/core apply, this is ignored (and instead the version that
893
-     * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs
894
-     * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons,
895
-     * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set
896
-     * the database up so it can run), then you can set "A" to priority 3 or something.
897
-     *
898
-     * @return int
899
-     */
900
-    public function priority()
901
-    {
902
-        return $this->_priority;
903
-    }
904
-
905
-
906
-    /**
907
-     * Sets whether this DMS is being ran as part of a migration, instead of
908
-     * just being used to setup (or verify) the current database structure matches
909
-     * what the latest DMS indicates it should be
910
-     *
911
-     * @param boolean $migrating
912
-     * @return void
913
-     */
914
-    public function set_migrating($migrating = true)
915
-    {
916
-        $this->_migrating = $migrating;
917
-    }
918
-
919
-    /**
920
-     * Marks that we think this migration class can continue to migrate
921
-     */
922
-    public function reattempt()
923
-    {
924
-        parent::reattempt();
925
-        // also, we want to reattempt any stages that were marked as borked
926
-        foreach ($this->stages() as $stage) {
927
-            if ($stage->is_broken()) {
928
-                $stage->reattempt();
929
-            }
930
-        }
931
-    }
18
+	/**
19
+	 * Set by client code to indicate this DMS is being ran as part of a proper migration,
20
+	 * instead of being used to merely setup (or verify) the database structure.
21
+	 * Defaults to TRUE, so client code that's NOT using this DMS as part of a proper migration
22
+	 * should call EE_Data_Migration_Script_Base::set_migrating( FALSE )
23
+	 *
24
+	 * @var boolean
25
+	 */
26
+	protected bool $_migrating = true;
27
+
28
+	/**
29
+	 * numerically-indexed array where each value is EE_Data_Migration_Script_Stage object
30
+	 *
31
+	 * @var EE_Data_Migration_Script_Stage[] $migration_functions
32
+	 */
33
+	protected array $_migration_stages = array();
34
+
35
+	/**
36
+	 * Indicates we've already run the schema changes that needed to happen BEFORE the data migration
37
+	 *
38
+	 * @var boolean
39
+	 */
40
+	protected ?bool $_schema_changes_before_migration_ran = null;
41
+
42
+	/**
43
+	 * Indicates we've already run the schema changes that needed to happen AFTER the data migration
44
+	 *
45
+	 * @var boolean
46
+	 */
47
+	protected ?bool $_schema_changes_after_migration_ran = null;
48
+
49
+	/**
50
+	 * String which describes what's currently happening in this migration
51
+	 *
52
+	 * @var string|null
53
+	 */
54
+	protected ?string $_feedback_message = '';
55
+
56
+	/**
57
+	 * Indicates the script's priority. Like wp's add_action and add_filter, lower numbers
58
+	 * correspond to earlier execution
59
+	 *
60
+	 * @var int
61
+	 */
62
+	protected int $_priority = 5;
63
+
64
+	/**
65
+	 * Multidimensional array that defines the mapping from OLD table Primary Keys
66
+	 * to NEW table Primary Keys.
67
+	 * Top-level array keys are OLD table names (minus the "wp_" part),
68
+	 * 2nd-level array keys are NEW table names (again, minus the "wp_" part),
69
+	 * 3rd-level array keys are the OLD table primary keys
70
+	 * and 3rd-level array values are the NEW table primary keys
71
+	 *
72
+	 * @var array
73
+	 */
74
+	protected array $_mappings = array();
75
+
76
+	/**
77
+	 * @var EE_Data_Migration_Script_Base
78
+	 */
79
+	protected EE_Data_Migration_Script_Base $previous_dms;
80
+
81
+
82
+	/**
83
+	 * Returns whether this data migration script can operate on the given version of the database.
84
+	 * Eg, if this migration script can migrate from 3.1.26 or higher (but not anything after 4.0.0), and
85
+	 * it's passed a string like '3.1.38B', it should return true.
86
+	 * If this DMS is to migrate data from an EE3 addon, you will probably want to use
87
+	 * EventEspresso\core\services\database\TableAnalysis::tableExists() to check for old EE3 tables, and
88
+	 * EE_Data_Migration_Manager::get_migration_ran() to check that core was already
89
+	 * migrated from EE3 to EE4 (ie, this DMS probably relies on some migration data generated
90
+	 * during the Core 4.1.0 DMS. If core didn't run that DMS, you probably don't want
91
+	 * to run this DMS).
92
+	 * If this DMS migrates data from a previous version of this EE4 addon, just
93
+	 * comparing $current_database_state_of[ $this->slug() ] will probably suffice.
94
+	 * If this DMS should never migrate data, because it's only used to define the initial
95
+	 * database state, just return FALSE (and core's activation process will take care
96
+	 * of calling its schema_changes_before_migration() and
97
+	 * schema_changes_after_migration() for you. )
98
+	 *
99
+	 * @param array $version_array keys are EE plugin slugs (eg 'Core', 'Calendar', 'Mailchimp', etc)
100
+	 * @return boolean
101
+	 */
102
+	abstract public function can_migrate_from_version($version_array);
103
+
104
+
105
+	/**
106
+	 * Performs database schema changes that need to occur BEFORE the data is migrated.
107
+	 * Eg, if we were going to change user passwords from plaintext to encoded versions
108
+	 * during this migration, this would probably add a new column called something like
109
+	 * "encoded_password".
110
+	 *
111
+	 * @return boolean of success
112
+	 */
113
+	abstract public function schema_changes_before_migration();
114
+
115
+
116
+	/**
117
+	 * Performs the database schema changes that need to occur AFTER the data has been migrated.
118
+	 * Usually this will mean we'll be removing old columns. Eg, if we were changing passwords
119
+	 * from plaintext to encoded versions, and we had added a column called "encoded_password",
120
+	 * this function would probably remove the old column "password" (which still holds the plaintext password)
121
+	 * and possibly rename "encoded_password" to "password"
122
+	 *
123
+	 * @return boolean of success
124
+	 */
125
+	abstract public function schema_changes_after_migration();
126
+
127
+
128
+	/**
129
+	 * All children of this must call parent::__construct()
130
+	 * at the end of their constructor or suffer the consequences!
131
+	 *
132
+	 * @param TableManager|null  $table_manager
133
+	 * @param TableAnalysis|null $table_analysis
134
+	 */
135
+	public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
136
+	{
137
+		$this->_migration_stages = (array) apply_filters(
138
+			'FHEE__' . get_class($this) . '__construct__migration_stages',
139
+			$this->_migration_stages
140
+		);
141
+		foreach ($this->_migration_stages as $migration_stage) {
142
+			if ($migration_stage instanceof EE_Data_Migration_Script_Stage) {
143
+				$migration_stage->_construct_finalize($this);
144
+			}
145
+		}
146
+		parent::__construct($table_manager, $table_analysis);
147
+	}
148
+
149
+
150
+	/**
151
+	 * Place to add hooks and filters for tweaking the migrations page, in order
152
+	 * to customize it
153
+	 */
154
+	public function migration_page_hooks()
155
+	{
156
+		// by default none are added because we normally like the default look of the migration page
157
+	}
158
+
159
+
160
+	/**
161
+	 * Sets the mapping from old table primary keys to new table primary keys.
162
+	 * This mapping is automatically persisted as a property on the migration
163
+	 *
164
+	 * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
165
+	 * @param int|string $old_pk    old primary key. Eg events_detail.id's value
166
+	 * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
167
+	 * @param array|int|string $new_pk    eg posts.ID
168
+	 * @return void
169
+	 * @throws EE_Error
170
+	 */
171
+	public function set_mapping($old_table, $old_pk, $new_table, $new_pk)
172
+	{
173
+		// make sure it has the needed keys
174
+		if (! isset($this->_mappings[ $old_table ]) || ! isset($this->_mappings[ $old_table ][ $new_table ])) {
175
+			$this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
176
+		}
177
+		$this->_mappings[ $old_table ][ $new_table ][ $old_pk ] = $new_pk;
178
+	}
179
+
180
+
181
+	/**
182
+	 * Gets the new primary key, if provided with the OLD table and the primary key
183
+	 * of an item in the old table, and the new table
184
+	 *
185
+	 * @param string     $old_table with wpdb prefix (wp_). Eg: wp_events_detail
186
+	 * @param int|string $old_pk    old primary key. Eg events_detail.id's value
187
+	 * @param string     $new_table with wpdb prefix (wp_). Eg: wp_posts
188
+	 * @return mixed the primary key on the new table
189
+	 * @throws EE_Error
190
+	 */
191
+	public function get_mapping_new_pk($old_table, $old_pk, $new_table)
192
+	{
193
+		if (
194
+			! isset($this->_mappings[ $old_table ]) ||
195
+			! isset($this->_mappings[ $old_table ][ $new_table ])
196
+		) {
197
+			// try fetching the option
198
+			$this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
199
+		}
200
+		return isset($this->_mappings[ $old_table ][ $new_table ][ $old_pk ])
201
+			? $this->_mappings[ $old_table ][ $new_table ][ $old_pk ] : null;
202
+	}
203
+
204
+
205
+	/**
206
+	 * Gets the old primary key, if provided with the OLD table,
207
+	 * and the new table and the primary key of an item in the new table
208
+	 *
209
+	 * @param string $old_table with wpdb prefix (wp_). Eg: wp_events_detail
210
+	 * @param string $new_table with wpdb prefix (wp_). Eg: wp_posts
211
+	 * @param mixed  $new_pk
212
+	 * @return mixed
213
+	 * @throws EE_Error
214
+	 */
215
+	public function get_mapping_old_pk($old_table, $new_table, $new_pk)
216
+	{
217
+		if (
218
+			! isset($this->_mappings[ $old_table ]) ||
219
+			! isset($this->_mappings[ $old_table ][ $new_table ])
220
+		) {
221
+			// try fetching the option
222
+			$this->_mappings[ $old_table ][ $new_table ] = $this->_get_mapping_option($old_table, $new_table);
223
+		}
224
+		if (isset($this->_mappings[ $old_table ][ $new_table ])) {
225
+			$new_pk_to_old_pk = array_flip($this->_mappings[ $old_table ][ $new_table ]);
226
+			if (isset($new_pk_to_old_pk[ $new_pk ])) {
227
+				return $new_pk_to_old_pk[ $new_pk ];
228
+			}
229
+		}
230
+		return null;
231
+	}
232
+
233
+
234
+	/**
235
+	 * Gets the mapping array option specified by the table names
236
+	 *
237
+	 * @param string $old_table_name
238
+	 * @param string $new_table_name
239
+	 * @return array
240
+	 * @throws EE_Error
241
+	 */
242
+	protected function _get_mapping_option($old_table_name, $new_table_name)
243
+	{
244
+		return get_option($this->_get_mapping_option_name($old_table_name, $new_table_name), array());
245
+	}
246
+
247
+
248
+	/**
249
+	 * Updates the mapping option specified by the table names with the array provided
250
+	 *
251
+	 * @param string $old_table_name
252
+	 * @param string $new_table_name
253
+	 * @param array  $mapping_array
254
+	 * @return boolean success of updating option
255
+	 * @throws EE_Error
256
+	 */
257
+	protected function _set_mapping_option($old_table_name, $new_table_name, $mapping_array)
258
+	{
259
+		$success = update_option($this->_get_mapping_option_name($old_table_name, $new_table_name), $mapping_array, false);
260
+		return $success;
261
+	}
262
+
263
+
264
+	/**
265
+	 * Gets the option name for this script to map from $old_table_name to $new_table_name
266
+	 *
267
+	 * @param string $old_table_name
268
+	 * @param string $new_table_name
269
+	 * @return string
270
+	 * @throws EE_Error
271
+	 */
272
+	protected function _get_mapping_option_name($old_table_name, $new_table_name)
273
+	{
274
+		global $wpdb;
275
+		$old_table_name_sans_wp = str_replace($wpdb->prefix, "", $old_table_name);
276
+		$new_table_name_sans_wp = str_replace($wpdb->prefix, "", $new_table_name);
277
+		$migrates_to = EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
278
+		return substr(
279
+			EE_Data_Migration_Manager::data_migration_script_mapping_option_prefix . $migrates_to ['slug'] . '_' . $migrates_to['version'] . '_' . $old_table_name_sans_wp . '_' . $new_table_name_sans_wp,
280
+			0,
281
+			64
282
+		);
283
+	}
284
+
285
+
286
+	/**
287
+	 * Counts all the records that will be migrated during this data migration.
288
+	 * For example, if we were changing old user passwords from plaintext to encoded versions,
289
+	 * this would be a count of all users who have passwords. If we were going to also split
290
+	 * attendee records into transactions, registrations, and attendee records, this would include
291
+	 * the count of all attendees currently in existence in the DB (ie, users + attendees).
292
+	 * If you can't determine how many records there are to migrate, just provide a guess: this
293
+	 * number will only be used in calculating the percent complete. If you estimate there to be
294
+	 * 100 records to migrate, and it turns out there's 120, we'll just show the migration as being at
295
+	 * 99% until the function "migration_step" returns EE_Data_Migration_Script_Base::status_complete.
296
+	 *
297
+	 * @return int
298
+	 */
299
+	protected function _count_records_to_migrate()
300
+	{
301
+		$count = 0;
302
+		foreach ($this->stages() as $stage) {
303
+			$count += $stage->count_records_to_migrate();
304
+		}
305
+		return $count;
306
+	}
307
+
308
+
309
+	/**
310
+	 * Returns the number of records updated so far. Usually this is easiest to do
311
+	 * by just setting a transient and updating it after each migration_step
312
+	 *
313
+	 * @return int
314
+	 */
315
+	public function count_records_migrated()
316
+	{
317
+		$count = 0;
318
+		foreach ($this->stages() as $stage) {
319
+			$count += $stage->count_records_migrated();
320
+		}
321
+		$this->_records_migrated = $count;
322
+		return $count;
323
+	}
324
+
325
+
326
+	/**
327
+	 * @param int $num_records_to_migrate_limit
328
+	 * @return int
329
+	 * @throws EE_Error
330
+	 * @throws Exception
331
+	 */
332
+	public function migration_step($num_records_to_migrate_limit)
333
+	{
334
+		// reset the feedback message
335
+		$this->_feedback_message = '';
336
+		// if we haven't yet done the 1st schema changes, do them now. buffer any output
337
+		$this->_maybe_do_schema_changes(true);
338
+
339
+		$num_records_actually_migrated = 0;
340
+		$records_migrated_per_stage = array();
341
+		// setup the 'stage' variable, which should hold the last run stage of the migration  (or none at all if nothing runs)
342
+		$stage = null;
343
+		// get the next stage that isn't complete
344
+		foreach ($this->stages() as $stage) {
345
+			if ($stage->get_status() == EE_Data_Migration_Manager::status_continue) {
346
+				try {
347
+					$records_migrated_during_stage = $stage->migration_step(
348
+						$num_records_to_migrate_limit - $num_records_actually_migrated
349
+					);
350
+					$num_records_actually_migrated += $records_migrated_during_stage;
351
+					$records_migrated_per_stage[ $stage->pretty_name() ] = $records_migrated_during_stage;
352
+				} catch (Exception $e) {
353
+					// yes if we catch an exception here, we consider that migration stage borked.
354
+					$stage->set_status(EE_Data_Migration_Manager::status_fatal_error);
355
+					$this->set_status(EE_Data_Migration_Manager::status_fatal_error);
356
+					$stage->add_error($e->getMessage() . ". Stack-trace:" . $e->getTraceAsString());
357
+					throw $e;
358
+				}
359
+				// check that the migration stage didn't mark itself as having a fatal error
360
+				if ($stage->is_broken()) {
361
+					$this->set_broken();
362
+					throw new EE_Error($stage->get_last_error());
363
+				}
364
+			}
365
+			// once we've migrated all the number we intended to (possibly from different stages), stop migrating
366
+			// or if we had a fatal error
367
+			// or if the current script stopped early- its not done, but it's done all it thinks we should do on this step
368
+			if (
369
+				$num_records_actually_migrated >= $num_records_to_migrate_limit
370
+				|| $stage->is_broken()
371
+				|| $stage->has_more_to_do()
372
+			) {
373
+				break;
374
+			}
375
+		}
376
+		// check if we're all done this data migration...
377
+		// which is indicated by being done early AND the last stage claims to be done
378
+		if ($stage == null) {
379
+			// this migration script apparently has NO stages... which is super weird, but whatever
380
+			$this->set_completed();
381
+			$this->_maybe_do_schema_changes(false);
382
+		} elseif ($num_records_actually_migrated < $num_records_to_migrate_limit && ! $stage->has_more_to_do()) {
383
+			// apparently we're done, because we couldn't migrate the number we intended to
384
+			$this->set_completed();
385
+			$this->_update_feedback_message(array_reverse($records_migrated_per_stage));
386
+			// do schema changes for after the migration now
387
+			// first double-check we haven't already done this
388
+			$this->_maybe_do_schema_changes(false);
389
+		} else {
390
+			// update feedback message, keeping in mind that we show them with the most recent at the top
391
+			$this->_update_feedback_message(array_reverse($records_migrated_per_stage));
392
+		}
393
+		return $num_records_actually_migrated;
394
+	}
395
+
396
+
397
+	/**
398
+	 * Updates the feedback message according to what was done during this migration stage.
399
+	 *
400
+	 * @param array $records_migrated_per_stage KEYS are pretty names for each stage; values are the count of records
401
+	 *                                          migrated from that stage
402
+	 * @return void
403
+	 */
404
+	private function _update_feedback_message($records_migrated_per_stage)
405
+	{
406
+		$feedback_message_array = array();
407
+		foreach ($records_migrated_per_stage as $migration_stage_name => $num_records_migrated) {
408
+			$feedback_message_array[] = sprintf(
409
+				esc_html__("Migrated %d records successfully during %s", "event_espresso"),
410
+				$num_records_migrated,
411
+				$migration_stage_name
412
+			);
413
+		}
414
+		$this->_feedback_message .= implode("<br>", $feedback_message_array);
415
+	}
416
+
417
+
418
+	/**
419
+	 * Calls either schema_changes_before_migration() (if $before==true) or schema_changes_after_migration
420
+	 * (if $before==false). Buffers their outputs and stores them on the class.
421
+	 *
422
+	 * @param boolean $before
423
+	 * @throws Exception
424
+	 * @return void
425
+	 */
426
+	private function _maybe_do_schema_changes($before = true)
427
+	{
428
+		// so this property will be either _schema_changes_after_migration_ran or _schema_changes_before_migration_ran
429
+		$property_name = '_schema_changes_' . ($before ? 'before' : 'after') . '_migration_ran';
430
+		if (! $this->{$property_name}) {
431
+			try {
432
+				ob_start();
433
+				if ($before) {
434
+					$this->schema_changes_before_migration();
435
+				} else {
436
+					$this->schema_changes_after_migration();
437
+				}
438
+				$output = ob_get_contents();
439
+				ob_end_clean();
440
+			} catch (Exception $e) {
441
+				$this->set_status(EE_Data_Migration_Manager::status_fatal_error);
442
+				throw $e;
443
+			}
444
+			// record that we've done these schema changes
445
+			$this->{$property_name} = true;
446
+			// if there were any warnings etc, record them as non-fatal errors
447
+			if ($output) {
448
+				// there were some warnings
449
+				$this->_errors[] = $output;
450
+			}
451
+		}
452
+	}
453
+
454
+
455
+	/**
456
+	 * Wrapper for EEH_Activation::create_table. However, takes into account the request type when
457
+	 * deciding what to pass for its 4th arg, $drop_pre_existing_tables. Using this function, instead
458
+	 * of _table_should_exist_previously, indicates that this table should be new to the EE version being migrated to
459
+	 * or
460
+	 * activated currently. If this is a brand new activation or a migration, and we're indicating this table should
461
+	 * not
462
+	 * previously exist, then we want to set $drop_pre_existing_tables to TRUE (ie, we shouldn't discover that this
463
+	 * table exists in the DB in EEH_Activation::create_table- if it DOES exist, something's wrong and the old table
464
+	 * should be nuked.
465
+	 *
466
+	 * Just for a bit of context, the migration script's db_schema_changes_* methods
467
+	 * are called basically in 3 cases: on brand new activation of EE4 (ie no previous version of EE existed and the
468
+	 * plugin is being activated and we want to add all the brand new tables), upon reactivation of EE4 (it was
469
+	 * deactivated and then reactivated, in which case we want to just verify the DB structure is ok) that table should
470
+	 * be dropped), and during a migration when we're moving the DB to the state of the migration script
471
+	 *
472
+	 * @param string $table_name
473
+	 * @param string $table_definition_sql
474
+	 * @param string $engine_string
475
+	 * @throws EE_Error
476
+	 * @throws ReflectionException
477
+	 */
478
+	protected function _table_is_new_in_this_version(
479
+		$table_name,
480
+		$table_definition_sql,
481
+		$engine_string = 'ENGINE=InnoDB'
482
+	) {
483
+		$this->_create_table_and_catch_errors(
484
+			$table_name,
485
+			$table_definition_sql,
486
+			$engine_string,
487
+			$this->_pre_existing_table_should_be_dropped(true)
488
+		);
489
+	}
490
+
491
+
492
+	/**
493
+	 * Like _table_is_new_in_this_version and _table_should_exist_previously, this function verifies the given table
494
+	 * exists. But we understand that this table has CHANGED in this version since the previous version. So it's not
495
+	 * completely new, but it's different. So we need to treat it like a new table in terms of verifying it's schema is
496
+	 * correct on activations, migrations, upgrades; but if it exists when it shouldn't, we need to be as lenient as
497
+	 * _table_should_exist_previously.
498
+	 * 8656]{Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the
499
+	 * table shouldn't exist).
500
+	 *
501
+	 * @param string $table_name
502
+	 * @param string $table_definition_sql
503
+	 * @param string $engine_string
504
+	 * @throws EE_Error
505
+	 * @throws ReflectionException
506
+	 */
507
+	protected function _table_is_changed_in_this_version(
508
+		$table_name,
509
+		$table_definition_sql,
510
+		$engine_string = 'ENGINE=InnoDB'
511
+	) {
512
+		$this->_create_table_and_catch_errors(
513
+			$table_name,
514
+			$table_definition_sql,
515
+			$engine_string,
516
+			$this->_pre_existing_table_should_be_dropped(false)
517
+		);
518
+	}
519
+
520
+
521
+	/**
522
+	 * _old_table_exists
523
+	 * returns TRUE if the requested table exists in the current database
524
+	 *
525
+	 * @param string $table_name
526
+	 * @return boolean
527
+	 * @throws EE_Error
528
+	 */
529
+	protected function _old_table_exists($table_name)
530
+	{
531
+		return $this->_get_table_analysis()->tableExists($table_name);
532
+	}
533
+
534
+
535
+	/**
536
+	 * _delete_table_if_empty
537
+	 * returns TRUE if the requested table was empty and successfully empty
538
+	 *
539
+	 * @param string $table_name
540
+	 * @return boolean
541
+	 * @throws EE_Error
542
+	 * @throws ReflectionException
543
+	 */
544
+	protected function _delete_table_if_empty($table_name)
545
+	{
546
+		return EEH_Activation::delete_db_table_if_empty($table_name);
547
+	}
548
+
549
+
550
+	/**
551
+	 * It is preferred to use _table_has_not_changed_since_previous or _table_is_changed_in_this_version
552
+	 * as these are significantly more efficient or explicit.
553
+	 * Please see description of _table_is_new_in_this_version. This function will only set
554
+	 * EEH_Activation::create_table's $drop_pre_existing_tables to TRUE if it's a brand
555
+	 * new activation. ie, a more accurate name for this method would be "_table_added_previously_by_this_plugin"
556
+	 * because the table will be cleared out if this is a new activation (ie, if its a new activation, it actually
557
+	 * should exist previously). Otherwise, we'll always set $drop_pre_existing_tables to FALSE because the table
558
+	 * should have existed. Note, if the table is being MODIFIED in this version being activated or migrated to, then
559
+	 * you want _table_is_changed_in_this_version NOT this one. We don't check this table's structure during migrations
560
+	 * because apparently it hasn't changed since the previous one, right?
561
+	 *
562
+	 * @param string $table_name
563
+	 * @param string $table_definition_sql
564
+	 * @param string $engine_string
565
+	 * @throws EE_Error
566
+	 * @throws ReflectionException
567
+	 */
568
+	protected function _table_should_exist_previously(
569
+		$table_name,
570
+		$table_definition_sql,
571
+		$engine_string = 'ENGINE=InnoDB'
572
+	) {
573
+		$this->_create_table_and_catch_errors(
574
+			$table_name,
575
+			$table_definition_sql,
576
+			$engine_string,
577
+			$this->_pre_existing_table_should_be_dropped(false)
578
+		);
579
+	}
580
+
581
+
582
+	/**
583
+	 * Exactly the same as _table_should_exist_previously(), except if this migration script is currently doing
584
+	 * a migration, we skip checking this table's structure in the database and just assume it's correct.
585
+	 * So this is useful only to improve efficiency when doing migrations (not a big deal for single site installs,
586
+	 * but important for multisite where migrations can take a very long time otherwise).
587
+	 * If the table is known to have changed since previous version, use _table_is_changed_in_this_version().
588
+	 * Assumes only this plugin could have added this table (ie, if its a new activation of this plugin, the table
589
+	 * shouldn't exist).
590
+	 *
591
+	 * @param string $table_name
592
+	 * @param string $table_definition_sql
593
+	 * @param string $engine_string
594
+	 * @throws EE_Error
595
+	 * @throws ReflectionException
596
+	 */
597
+	protected function _table_has_not_changed_since_previous(
598
+		$table_name,
599
+		$table_definition_sql,
600
+		$engine_string = 'ENGINE=InnoDB'
601
+	) {
602
+		if ($this->_currently_migrating()) {
603
+			// if we're doing a migration, and this table apparently already exists, then we don't need do anything right?
604
+			return;
605
+		}
606
+		$this->_create_table_and_catch_errors(
607
+			$table_name,
608
+			$table_definition_sql,
609
+			$engine_string,
610
+			$this->_pre_existing_table_should_be_dropped(false)
611
+		);
612
+	}
613
+
614
+	/**
615
+	 * Returns whether this migration script is being used as part of an actual migration
616
+	 *
617
+	 * @return boolean
618
+	 */
619
+	protected function _currently_migrating()
620
+	{
621
+		// we want to know if we are currently performing a migration. We could just believe what was set on the _migrating property, but let's double-check (ie the script should apply and we should be in MM)
622
+		return $this->_migrating
623
+			&& DbStatus::isOffline()
624
+			&& $this->can_migrate_from_version(
625
+				EE_Data_Migration_Manager::instance()->ensure_current_database_state_is_set()
626
+			);
627
+	}
628
+
629
+
630
+	/**
631
+	 * Determines if a table should be dropped, based on whether it's reported to be new in $table_is_new,
632
+	 * and the plugin's request type.
633
+	 * Assumes only this plugin could have added the table (ie, if its a new activation of this plugin, the table
634
+	 * shouldn't exist no matter what).
635
+	 *
636
+	 * @param boolean $table_is_new
637
+	 * @return boolean
638
+	 * @throws EE_Error
639
+	 */
640
+	protected function _pre_existing_table_should_be_dropped($table_is_new)
641
+	{
642
+		if ($table_is_new) {
643
+			if (
644
+				$this->_get_req_type_for_plugin_corresponding_to_this_dms() == EE_System::req_type_new_activation
645
+				|| $this->_currently_migrating()
646
+			) {
647
+				return true;
648
+			} else {
649
+				return false;
650
+			}
651
+		} else {
652
+			if (
653
+				in_array(
654
+					$this->_get_req_type_for_plugin_corresponding_to_this_dms(),
655
+					array(EE_System::req_type_new_activation)
656
+				)
657
+			) {
658
+				return true;
659
+			} else {
660
+				return false;
661
+			}
662
+		}
663
+	}
664
+
665
+
666
+	/**
667
+	 * Just wraps EEH_Activation::create_table, but catches any errors it may throw and adds them as errors on the DMS
668
+	 *
669
+	 * @param string  $table_name
670
+	 * @param string  $table_definition_sql
671
+	 * @param string  $engine_string
672
+	 * @param boolean $drop_pre_existing_tables
673
+	 * @throws ReflectionException
674
+	 */
675
+	private function _create_table_and_catch_errors(
676
+		$table_name,
677
+		$table_definition_sql,
678
+		$engine_string = 'ENGINE=InnoDB',
679
+		$drop_pre_existing_tables = false
680
+	) {
681
+		try {
682
+			EEH_Activation::create_table($table_name, $table_definition_sql, $engine_string, $drop_pre_existing_tables);
683
+		} catch (EE_Error $e) {
684
+			$message = $e->getMessage() . '<br>Stack Trace:' . $e->getTraceAsString();
685
+			$this->add_error($message);
686
+			$this->_feedback_message .= $message;
687
+		}
688
+	}
689
+
690
+
691
+	/**
692
+	 * Gets the request type for the plugin (core or addon) that corresponds to this DMS
693
+	 *
694
+	 * @return int one of EE_System::_req_type_* constants
695
+	 * @throws EE_Error
696
+	 */
697
+	private function _get_req_type_for_plugin_corresponding_to_this_dms()
698
+	{
699
+		if ($this->slug() == 'Core') {
700
+			return EE_System::instance()->detect_req_type();
701
+		} else {// it must be for an addon
702
+			$addon_name = $this->slug();
703
+			if (EE_Registry::instance()->get_addon_by_name($addon_name)) {
704
+				return EE_Registry::instance()->get_addon_by_name($addon_name)->detect_req_type();
705
+			} else {
706
+				throw new EE_Error(
707
+					sprintf(
708
+						esc_html__(
709
+							"The DMS slug '%s' should correspond to the addon's name, which should also be '%s', but no such addon was registered. These are the registered addons' names: %s",
710
+							"event_espresso"
711
+						),
712
+						$this->slug(),
713
+						$addon_name,
714
+						implode(",", array_keys(EE_Registry::instance()->get_addons_by_name()))
715
+					)
716
+				);
717
+			}
718
+		}
719
+	}
720
+
721
+
722
+	/**
723
+	 * returns an array of strings describing errors by all the script's stages
724
+	 *
725
+	 * @return array
726
+	 */
727
+	public function get_errors()
728
+	{
729
+		$all_errors = $this->_errors;
730
+		if (! is_array($all_errors)) {
731
+			$all_errors = array();
732
+		}
733
+		foreach ($this->stages() as $stage) {
734
+			$all_errors = array_merge($stage->get_errors(), $all_errors);
735
+		}
736
+		return $all_errors;
737
+	}
738
+
739
+
740
+	/**
741
+	 * Indicates whether this migration script should continue
742
+	 *
743
+	 * @return boolean
744
+	 */
745
+	public function can_continue()
746
+	{
747
+		return in_array(
748
+			$this->get_status(),
749
+			EE_Data_Migration_Manager::instance()->stati_that_indicate_to_continue_single_migration_script
750
+		);
751
+	}
752
+
753
+
754
+	/**
755
+	 * Gets all the data migration stages associated with this script. Note:
756
+	 * addons can filter this list to add their own stages, and because the list is
757
+	 * numerically-indexed, they can insert their stage wherever they like and it will
758
+	 * get ordered by the indexes
759
+	 *
760
+	 * @return EE_Data_Migration_Script_Stage[]
761
+	 */
762
+	protected function stages()
763
+	{
764
+		$stages = apply_filters('FHEE__' . get_class($this) . '__stages', $this->_migration_stages);
765
+		ksort($stages);
766
+		return $stages;
767
+	}
768
+
769
+
770
+	/**
771
+	 * Gets a string which should describe what's going on currently with this migration, which
772
+	 * can be displayed to the user
773
+	 *
774
+	 * @return string
775
+	 */
776
+	public function get_feedback_message()
777
+	{
778
+		return $this->_feedback_message;
779
+	}
780
+
781
+
782
+	/**
783
+	 * A lot like "__sleep()" magic method in purpose, this is meant for persisting this class'
784
+	 * properties to the DB. However, we don't want to use __sleep() because its quite
785
+	 * possible that this class is defined when it goes to sleep, but NOT available when it
786
+	 * awakes (eg, this class is part of an addon that is deactivated at some point).
787
+	 */
788
+	public function properties_as_array()
789
+	{
790
+		$properties = parent::properties_as_array();
791
+		$properties['_migration_stages'] = array();
792
+		foreach ($this->_migration_stages as $migration_stage_priority => $migration_stage_class) {
793
+			$properties['_migration_stages'][ $migration_stage_priority ] = $migration_stage_class->properties_as_array(
794
+			);
795
+		}
796
+		unset($properties['_mappings']);
797
+		unset($properties['previous_dms']);
798
+
799
+		foreach ($this->_mappings as $old_table_name => $mapping_to_new_table) {
800
+			foreach ($mapping_to_new_table as $new_table_name => $mapping) {
801
+				$this->_set_mapping_option($old_table_name, $new_table_name, $mapping);
802
+			}
803
+		}
804
+		return $properties;
805
+	}
806
+
807
+
808
+	/**
809
+	 * Sets all of the properties of this script stage to match what's in the array, which is assumed
810
+	 * to have been made from the properties_as_array() function.
811
+	 *
812
+	 * @param array $array_of_properties like what's produced from properties_as_array() method
813
+	 * @return void
814
+	 */
815
+	public function instantiate_from_array_of_properties($array_of_properties)
816
+	{
817
+		$stages_properties_arrays = $array_of_properties['_migration_stages'];
818
+		unset($array_of_properties['_migration_stages']);
819
+		unset($array_of_properties['class']);
820
+		foreach ($array_of_properties as $property_name => $property_value) {
821
+			$this->{$property_name} = $property_value;
822
+		}
823
+		// _migration_stages are already instantiated, but have only default data
824
+		foreach ($this->_migration_stages as $stage) {
825
+			$stage_data = $this->_find_migration_stage_data_with_classname(
826
+				get_class($stage),
827
+				$stages_properties_arrays
828
+			);
829
+			// SO, if we found the stage data that was saved, use it. Otherwise, I guess the stage is new? (maybe added by
830
+			// an addon? Unlikely... not sure why it wouldn't exist, but if it doesn't just treat it like it was never started yet)
831
+			if ($stage_data) {
832
+				$stage->instantiate_from_array_of_properties($stage_data);
833
+			}
834
+		}
835
+	}
836
+
837
+
838
+	/**
839
+	 * Gets the migration data from the array $migration_stage_data_arrays (which is an array of arrays, each of which
840
+	 * is pretty well identical to EE_Data_Migration_Stage objects except all their properties are array indexes)
841
+	 * for the given classname
842
+	 *
843
+	 * @param string $classname
844
+	 * @param array  $migration_stage_data_arrays
845
+	 * @return null
846
+	 */
847
+	private function _find_migration_stage_data_with_classname($classname, $migration_stage_data_arrays)
848
+	{
849
+		foreach ($migration_stage_data_arrays as $migration_stage_data_array) {
850
+			if (isset($migration_stage_data_array['class']) && $migration_stage_data_array['class'] == $classname) {
851
+				return $migration_stage_data_array;
852
+			}
853
+		}
854
+		return null;
855
+	}
856
+
857
+
858
+	/**
859
+	 * Returns the version that this script migrates to, based on the script's name.
860
+	 * Cannot be overwritten because lots of code needs to know which version a script
861
+	 * migrates to knowing only its name.
862
+	 *
863
+	 * @return array where the first key is the plugin's slug, the 2nd is the version of that plugin
864
+	 * that will be updated to. Eg array('Core','4.1.0')
865
+	 * @throws EE_Error
866
+	 */
867
+	final public function migrates_to_version()
868
+	{
869
+		return EE_Data_Migration_Manager::instance()->script_migrates_to_version(get_class($this));
870
+	}
871
+
872
+
873
+	/**
874
+	 * Gets this addon's slug as it would appear in the current_db_state wp option,
875
+	 * and if this migration script is for an addon, it SHOULD match the addon's slug
876
+	 * (and also the addon's classname, minus the 'EE_' prefix.). Eg, 'Calendar' for the EE_Calendar addon.
877
+	 * Or 'Core' for core (non-addon).
878
+	 *
879
+	 * @return string
880
+	 * @throws EE_Error
881
+	 */
882
+	public function slug()
883
+	{
884
+		$migrates_to_version_info = $this->migrates_to_version();
885
+		// the slug is the first part of the array
886
+		return $migrates_to_version_info['slug'];
887
+	}
888
+
889
+
890
+	/**
891
+	 * Returns the script's priority relative to DMSs from other addons. However, when
892
+	 * two DMSs from the same addon/core apply, this is ignored (and instead the version that
893
+	 * the script migrates to is used to determine which to run first). The default is 5, but all core DMSs
894
+	 * normally have priority 10. (So if you want a DMS "A" to run before DMS "B", both of which are from addons,
895
+	 * and both of which CAN run at the same time (ie, "B" doesn't depend on "A" to set
896
+	 * the database up so it can run), then you can set "A" to priority 3 or something.
897
+	 *
898
+	 * @return int
899
+	 */
900
+	public function priority()
901
+	{
902
+		return $this->_priority;
903
+	}
904
+
905
+
906
+	/**
907
+	 * Sets whether this DMS is being ran as part of a migration, instead of
908
+	 * just being used to setup (or verify) the current database structure matches
909
+	 * what the latest DMS indicates it should be
910
+	 *
911
+	 * @param boolean $migrating
912
+	 * @return void
913
+	 */
914
+	public function set_migrating($migrating = true)
915
+	{
916
+		$this->_migrating = $migrating;
917
+	}
918
+
919
+	/**
920
+	 * Marks that we think this migration class can continue to migrate
921
+	 */
922
+	public function reattempt()
923
+	{
924
+		parent::reattempt();
925
+		// also, we want to reattempt any stages that were marked as borked
926
+		foreach ($this->stages() as $stage) {
927
+			if ($stage->is_broken()) {
928
+				$stage->reattempt();
929
+			}
930
+		}
931
+	}
932 932
 }
Please login to merge, or discard this patch.
core/data_migration_scripts/EE_Data_Migration_Script_Stage_Table.core.php 1 patch
Indentation   +75 added lines, -75 removed lines patch added patch discarded remove patch
@@ -15,86 +15,86 @@
 block discarded – undo
15 15
  */
16 16
 abstract class EE_Data_Migration_Script_Stage_Table extends EE_Data_Migration_Script_Stage
17 17
 {
18
-    /**
19
-     * @var string The columns to select. May be overridden for children.
20
-     */
21
-    protected string $select_expression = '*';
18
+	/**
19
+	 * @var string The columns to select. May be overridden for children.
20
+	 */
21
+	protected string $select_expression = '*';
22 22
 
23
-    /**
24
-     * Set in the constructor to add this sql to both the counting query in
25
-     * EE_Data_Migration_Script_Stage_Table::_count_records_to_migrate() and
26
-     * EE_Data_Migration_Script_Stage_Table::_get_rows().
27
-     * Eg "where column_name like '%some_value%'"
28
-     *
29
-     * @var string|null
30
-     */
31
-    protected ?string $_extra_where_sql;
23
+	/**
24
+	 * Set in the constructor to add this sql to both the counting query in
25
+	 * EE_Data_Migration_Script_Stage_Table::_count_records_to_migrate() and
26
+	 * EE_Data_Migration_Script_Stage_Table::_get_rows().
27
+	 * Eg "where column_name like '%some_value%'"
28
+	 *
29
+	 * @var string|null
30
+	 */
31
+	protected ?string $_extra_where_sql;
32 32
 
33 33
 
34
-    /**
35
-     * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
36
-     * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
37
-     * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
38
-     * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
39
-     * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
40
-     * very least we MUST report/return 50 items migrated)
41
-     *
42
-     * @param int $num_items_to_migrate
43
-     * @return int number of items ACTUALLY migrated
44
-     */
45
-    public function _migration_step($num_items_to_migrate = 50)
46
-    {
47
-        $rows = $this->_get_rows($num_items_to_migrate);
48
-        $items_actually_migrated = 0;
49
-        foreach ($rows as $old_row) {
50
-            $this->_migrate_old_row($old_row);
51
-            $items_actually_migrated++;
52
-        }
53
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
54
-            $this->set_completed();
55
-        }
56
-        return $items_actually_migrated;
57
-    }
34
+	/**
35
+	 * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
36
+	 * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
37
+	 * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
38
+	 * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
39
+	 * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
40
+	 * very least we MUST report/return 50 items migrated)
41
+	 *
42
+	 * @param int $num_items_to_migrate
43
+	 * @return int number of items ACTUALLY migrated
44
+	 */
45
+	public function _migration_step($num_items_to_migrate = 50)
46
+	{
47
+		$rows = $this->_get_rows($num_items_to_migrate);
48
+		$items_actually_migrated = 0;
49
+		foreach ($rows as $old_row) {
50
+			$this->_migrate_old_row($old_row);
51
+			$items_actually_migrated++;
52
+		}
53
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
54
+			$this->set_completed();
55
+		}
56
+		return $items_actually_migrated;
57
+	}
58 58
 
59
-    /**
60
-     * Gets the rows for each migration stage from the old table
61
-     *
62
-     * @global wpdb $wpdb
63
-     * @param int   $limit
64
-     * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
65
-     */
66
-    protected function _get_rows($limit)
67
-    {
68
-        global $wpdb;
69
-        $start_at_record = $this->count_records_migrated();
70
-        $query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare(
71
-            "LIMIT %d, %d",
72
-            $start_at_record,
73
-            $limit
74
-        );
75
-        return $wpdb->get_results($query, ARRAY_A);
76
-    }
59
+	/**
60
+	 * Gets the rows for each migration stage from the old table
61
+	 *
62
+	 * @global wpdb $wpdb
63
+	 * @param int   $limit
64
+	 * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
65
+	 */
66
+	protected function _get_rows($limit)
67
+	{
68
+		global $wpdb;
69
+		$start_at_record = $this->count_records_migrated();
70
+		$query = "SELECT {$this->select_expression} FROM {$this->_old_table} {$this->_extra_where_sql} " . $wpdb->prepare(
71
+			"LIMIT %d, %d",
72
+			$start_at_record,
73
+			$limit
74
+		);
75
+		return $wpdb->get_results($query, ARRAY_A);
76
+	}
77 77
 
78 78
 
79
-    /**
80
-     * Counts the records to migrate; the public version may cache it
81
-     *
82
-     * @return int
83
-     */
84
-    public function _count_records_to_migrate()
85
-    {
86
-        global $wpdb;
87
-        $query = "SELECT COUNT(*) FROM {$this->_old_table} {$this->_extra_where_sql}";
88
-        return (int) $wpdb->get_var($query);
89
-    }
79
+	/**
80
+	 * Counts the records to migrate; the public version may cache it
81
+	 *
82
+	 * @return int
83
+	 */
84
+	public function _count_records_to_migrate()
85
+	{
86
+		global $wpdb;
87
+		$query = "SELECT COUNT(*) FROM {$this->_old_table} {$this->_extra_where_sql}";
88
+		return (int) $wpdb->get_var($query);
89
+	}
90 90
 
91
-    /**
92
-     * takes care of migrating this particular row from the OLD table to whatever its
93
-     * representation is in the new database. If there are errors, use $this->add_error to log them. If there is a
94
-     * fatal error which prevents all future migrations, throw an exception describing it
95
-     *
96
-     * @param array $old_row an associative array where keys are column names and values are their values.
97
-     * @return void
98
-     */
99
-    abstract protected function _migrate_old_row($old_row);
91
+	/**
92
+	 * takes care of migrating this particular row from the OLD table to whatever its
93
+	 * representation is in the new database. If there are errors, use $this->add_error to log them. If there is a
94
+	 * fatal error which prevents all future migrations, throw an exception describing it
95
+	 *
96
+	 * @param array $old_row an associative array where keys are column names and values are their values.
97
+	 * @return void
98
+	 */
99
+	abstract protected function _migrate_old_row($old_row);
100 100
 }
Please login to merge, or discard this patch.
4_5_0_stages/EE_DMS_4_5_0_update_wp_user_for_price_types.dmsstage.php 2 patches
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -11,44 +11,44 @@
 block discarded – undo
11 11
 class EE_DMS_4_5_0_update_wp_user_for_price_types extends EE_Data_Migration_Script_Stage_Table
12 12
 {
13 13
 
14
-    public function __construct()
15
-    {
16
-        global $wpdb;
17
-        $this->_pretty_name = esc_html__("Price Types", "event_espresso");
18
-        $this->_old_table   = $wpdb->prefix . "esp_price_type";
19
-        parent::__construct();
20
-    }
14
+	public function __construct()
15
+	{
16
+		global $wpdb;
17
+		$this->_pretty_name = esc_html__("Price Types", "event_espresso");
18
+		$this->_old_table   = $wpdb->prefix . "esp_price_type";
19
+		parent::__construct();
20
+	}
21 21
 
22 22
 
23
-    /**
24
-     * @throws EE_Error
25
-     * @throws ReflectionException
26
-     */
27
-    protected function _migrate_old_row($old_row)
28
-    {
29
-        // foreach ticket row we add the id for the current logged in user.
30
-        global $wpdb;
31
-        $user_id = EEH_Activation::get_default_creator_id();
32
-        $user_id = $user_id ?: 0;
33
-        $updated = $wpdb->update(
34
-            $this->_old_table,
35
-            ['PRT_wp_user' => $user_id],
36
-            ['PRT_ID' => $old_row['PRT_ID']],
37
-            ['%d'], // PRT_wp_user
38
-            ['%d']  // PRT_ID
39
-        );
40
-        if (false === $updated) {
41
-            $this->add_error(
42
-                sprintf(
43
-                    esc_html__(
44
-                        "Error in updating table %s setting PRT_wp_user = %d where PRT_ID = %d",
45
-                        'event_espresso'
46
-                    ),
47
-                    $this->_old_table,
48
-                    $user_id,
49
-                    $old_row['PRT_ID']
50
-                )
51
-            );
52
-        }
53
-    }
23
+	/**
24
+	 * @throws EE_Error
25
+	 * @throws ReflectionException
26
+	 */
27
+	protected function _migrate_old_row($old_row)
28
+	{
29
+		// foreach ticket row we add the id for the current logged in user.
30
+		global $wpdb;
31
+		$user_id = EEH_Activation::get_default_creator_id();
32
+		$user_id = $user_id ?: 0;
33
+		$updated = $wpdb->update(
34
+			$this->_old_table,
35
+			['PRT_wp_user' => $user_id],
36
+			['PRT_ID' => $old_row['PRT_ID']],
37
+			['%d'], // PRT_wp_user
38
+			['%d']  // PRT_ID
39
+		);
40
+		if (false === $updated) {
41
+			$this->add_error(
42
+				sprintf(
43
+					esc_html__(
44
+						"Error in updating table %s setting PRT_wp_user = %d where PRT_ID = %d",
45
+						'event_espresso'
46
+					),
47
+					$this->_old_table,
48
+					$user_id,
49
+					$old_row['PRT_ID']
50
+				)
51
+			);
52
+		}
53
+	}
54 54
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@
 block discarded – undo
15 15
     {
16 16
         global $wpdb;
17 17
         $this->_pretty_name = esc_html__("Price Types", "event_espresso");
18
-        $this->_old_table   = $wpdb->prefix . "esp_price_type";
18
+        $this->_old_table   = $wpdb->prefix."esp_price_type";
19 19
         parent::__construct();
20 20
     }
21 21
 
Please login to merge, or discard this patch.
4_5_0_stages/EE_DMS_4_5_0_update_wp_user_for_prices.dmsstage.php 2 patches
Indentation   +38 added lines, -38 removed lines patch added patch discarded remove patch
@@ -11,44 +11,44 @@
 block discarded – undo
11 11
 class EE_DMS_4_5_0_update_wp_user_for_prices extends EE_Data_Migration_Script_Stage_Table
12 12
 {
13 13
 
14
-    public function __construct()
15
-    {
16
-        global $wpdb;
17
-        $this->_pretty_name = esc_html__("Prices", "event_espresso");
18
-        $this->_old_table   = $wpdb->prefix . "esp_price";
19
-        parent::__construct();
20
-    }
14
+	public function __construct()
15
+	{
16
+		global $wpdb;
17
+		$this->_pretty_name = esc_html__("Prices", "event_espresso");
18
+		$this->_old_table   = $wpdb->prefix . "esp_price";
19
+		parent::__construct();
20
+	}
21 21
 
22 22
 
23
-    /**
24
-     * @throws EE_Error
25
-     * @throws ReflectionException
26
-     */
27
-    protected function _migrate_old_row($old_row)
28
-    {
29
-        // foreach ticket row we add the id for the current logged in user.
30
-        global $wpdb;
31
-        $user_id = EEH_Activation::get_default_creator_id();
32
-        $user_id = $user_id ?: 0;
33
-        $updated = $wpdb->update(
34
-            $this->_old_table,
35
-            ['PRC_wp_user' => $user_id],
36
-            ['PRC_ID' => $old_row['PRC_ID']],
37
-            ['%d'],// PRC_wp_user
38
-            ['%d'] // PRC_ID
39
-        );
40
-        if (false === $updated) {
41
-            $this->add_error(
42
-                sprintf(
43
-                    esc_html__(
44
-                        "Error in updating table %s setting PRC_wp_user = %d where PRC_ID = %d",
45
-                        'event_espresso'
46
-                    ),
47
-                    $this->_old_table,
48
-                    $user_id,
49
-                    $old_row['PRC_ID']
50
-                )
51
-            );
52
-        }
53
-    }
23
+	/**
24
+	 * @throws EE_Error
25
+	 * @throws ReflectionException
26
+	 */
27
+	protected function _migrate_old_row($old_row)
28
+	{
29
+		// foreach ticket row we add the id for the current logged in user.
30
+		global $wpdb;
31
+		$user_id = EEH_Activation::get_default_creator_id();
32
+		$user_id = $user_id ?: 0;
33
+		$updated = $wpdb->update(
34
+			$this->_old_table,
35
+			['PRC_wp_user' => $user_id],
36
+			['PRC_ID' => $old_row['PRC_ID']],
37
+			['%d'],// PRC_wp_user
38
+			['%d'] // PRC_ID
39
+		);
40
+		if (false === $updated) {
41
+			$this->add_error(
42
+				sprintf(
43
+					esc_html__(
44
+						"Error in updating table %s setting PRC_wp_user = %d where PRC_ID = %d",
45
+						'event_espresso'
46
+					),
47
+					$this->_old_table,
48
+					$user_id,
49
+					$old_row['PRC_ID']
50
+				)
51
+			);
52
+		}
53
+	}
54 54
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -15,7 +15,7 @@  discard block
 block discarded – undo
15 15
     {
16 16
         global $wpdb;
17 17
         $this->_pretty_name = esc_html__("Prices", "event_espresso");
18
-        $this->_old_table   = $wpdb->prefix . "esp_price";
18
+        $this->_old_table   = $wpdb->prefix."esp_price";
19 19
         parent::__construct();
20 20
     }
21 21
 
@@ -34,7 +34,7 @@  discard block
 block discarded – undo
34 34
             $this->_old_table,
35 35
             ['PRC_wp_user' => $user_id],
36 36
             ['PRC_ID' => $old_row['PRC_ID']],
37
-            ['%d'],// PRC_wp_user
37
+            ['%d'], // PRC_wp_user
38 38
             ['%d'] // PRC_ID
39 39
         );
40 40
         if (false === $updated) {
Please login to merge, or discard this patch.
4_5_0_stages/EE_DMS_4_5_0_update_wp_user_for_tickets.dmsstage.php 1 patch
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -10,48 +10,48 @@
 block discarded – undo
10 10
  */
11 11
 class EE_DMS_4_5_0_update_wp_user_for_tickets extends EE_Data_Migration_Script_Stage_Table
12 12
 {
13
-    public function __construct()
14
-    {
15
-        global $wpdb;
16
-        $this->_pretty_name = esc_html__("Tickets", "event_espresso");
17
-        $this->_old_table   = $wpdb->prefix . "esp_ticket";
18
-        parent::__construct();
19
-    }
13
+	public function __construct()
14
+	{
15
+		global $wpdb;
16
+		$this->_pretty_name = esc_html__("Tickets", "event_espresso");
17
+		$this->_old_table   = $wpdb->prefix . "esp_ticket";
18
+		parent::__construct();
19
+	}
20 20
 
21 21
 
22
-    /**
23
-     * @throws ReflectionException
24
-     * @throws EE_Error
25
-     */
26
-    protected function _migrate_old_row($old_row)
27
-    {
28
-        // foreach ticket row we add the id for the current logged in user.
29
-        global $wpdb;
30
-        $user_id = EEH_Activation::get_default_creator_id();
31
-        $user_id = $user_id ?: 0;
32
-        $updated = $wpdb->update(
33
-            $this->_old_table,
34
-            ['TKT_wp_user' => $user_id],
35
-            ['TKT_ID' => $old_row['TKT_ID']],
36
-            [
37
-                '%d',// TKT_wp_user
38
-            ],
39
-            [
40
-                '%d',// TKT_ID
41
-            ]
42
-        );
43
-        if (false === $updated) {
44
-            $this->add_error(
45
-                sprintf(
46
-                    esc_html__(
47
-                        "Error in updating table %s setting TKT_wp_user = %d where TKT_ID = %d",
48
-                        'event_espresso'
49
-                    ),
50
-                    $this->_old_table,
51
-                    $user_id,
52
-                    $old_row['TKT_ID']
53
-                )
54
-            );
55
-        }
56
-    }
22
+	/**
23
+	 * @throws ReflectionException
24
+	 * @throws EE_Error
25
+	 */
26
+	protected function _migrate_old_row($old_row)
27
+	{
28
+		// foreach ticket row we add the id for the current logged in user.
29
+		global $wpdb;
30
+		$user_id = EEH_Activation::get_default_creator_id();
31
+		$user_id = $user_id ?: 0;
32
+		$updated = $wpdb->update(
33
+			$this->_old_table,
34
+			['TKT_wp_user' => $user_id],
35
+			['TKT_ID' => $old_row['TKT_ID']],
36
+			[
37
+				'%d',// TKT_wp_user
38
+			],
39
+			[
40
+				'%d',// TKT_ID
41
+			]
42
+		);
43
+		if (false === $updated) {
44
+			$this->add_error(
45
+				sprintf(
46
+					esc_html__(
47
+						"Error in updating table %s setting TKT_wp_user = %d where TKT_ID = %d",
48
+						'event_espresso'
49
+					),
50
+					$this->_old_table,
51
+					$user_id,
52
+					$old_row['TKT_ID']
53
+				)
54
+			);
55
+		}
56
+	}
57 57
 }
Please login to merge, or discard this patch.