Completed
Branch master (465a38)
by
unknown
27:45 queued 22:40
created
4_5_0_stages/EE_DMS_4_5_0_invoice_settings.dmsstage.php 2 patches
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -15,114 +15,114 @@
 block discarded – undo
15 15
 class EE_DMS_4_5_0_invoice_settings extends EE_Data_Migration_Script_Stage
16 16
 {
17 17
 
18
-    /**
19
-     * Just initializes the status of the migration
20
-     */
21
-    public function __construct()
22
-    {
23
-        $this->_pretty_name = esc_html__('Update Invoice Gateway Settings', 'event_espresso');
24
-        parent::__construct();
25
-    }
18
+	/**
19
+	 * Just initializes the status of the migration
20
+	 */
21
+	public function __construct()
22
+	{
23
+		$this->_pretty_name = esc_html__('Update Invoice Gateway Settings', 'event_espresso');
24
+		parent::__construct();
25
+	}
26 26
 
27 27
 
28
-    /**
29
-     * _count_records_to_migrate
30
-     * Counts the records to migrate; the public version may cache it
31
-     *
32
-     * @access protected
33
-     * @return int
34
-     */
35
-    protected function _count_records_to_migrate(): int
36
-    {
37
-        return 1;
38
-    }
28
+	/**
29
+	 * _count_records_to_migrate
30
+	 * Counts the records to migrate; the public version may cache it
31
+	 *
32
+	 * @access protected
33
+	 * @return int
34
+	 */
35
+	protected function _count_records_to_migrate(): int
36
+	{
37
+		return 1;
38
+	}
39 39
 
40 40
 
41
-    /**
42
-     *    _migration_step
43
-     *
44
-     * @access protected
45
-     * @param int $num_items_to_migrate
46
-     * @return int number of items ACTUALLY migrated
47
-     * @throws EE_Error
48
-     * @throws InvalidDataTypeException
49
-     */
50
-    protected function _migration_step($num_items_to_migrate = 1)
51
-    {
52
-        // if this isn't set then something is really wrong
53
-        if (! EE_Config::instance()->gateway instanceof EE_Gateway_Config) {
54
-            throw new EE_Error(
55
-                esc_html__('It appears the Event Espresso Core Configuration is not setup correctly.', 'event_espresso')
56
-            );
57
-        }
58
-        $invoice_settings = isset(EE_Config::instance()->gateway->payment_settings['Invoice'])
59
-            ? EE_Config::instance()->gateway->payment_settings['Invoice']
60
-            : null;
61
-        if (! $invoice_settings) {
62
-            $this->add_error(
63
-                esc_html__(
64
-                    'Could not migrate EE4.4 invoice settings to EE4.5 because they didnt exist',
65
-                    'event_espresso'
66
-                )
67
-            );
68
-        } else {
69
-            $invoice_settings['template_payment_instructions'] = $invoice_settings['pdf_instructions'];
70
-            $invoice_settings['template_invoice_payee_name']   = $invoice_settings['payable_to'];
71
-            $invoice_settings['template_invoice_address']      = $invoice_settings['payment_address'];
72
-            $invoice_settings['template_invoice_email']        = '';
73
-            $invoice_settings['template_invoice_tax_number']   = '';
74
-            unset($invoice_settings['pdf_instructions']);
75
-            unset($invoice_settings['payable_to']);
76
-            unset($invoice_settings['payment_address']);
77
-            EE_Config::instance()->gateway->payment_settings['Invoice'] = $invoice_settings;
78
-            EE_Config::instance()->update_espresso_config(false, false);
41
+	/**
42
+	 *    _migration_step
43
+	 *
44
+	 * @access protected
45
+	 * @param int $num_items_to_migrate
46
+	 * @return int number of items ACTUALLY migrated
47
+	 * @throws EE_Error
48
+	 * @throws InvalidDataTypeException
49
+	 */
50
+	protected function _migration_step($num_items_to_migrate = 1)
51
+	{
52
+		// if this isn't set then something is really wrong
53
+		if (! EE_Config::instance()->gateway instanceof EE_Gateway_Config) {
54
+			throw new EE_Error(
55
+				esc_html__('It appears the Event Espresso Core Configuration is not setup correctly.', 'event_espresso')
56
+			);
57
+		}
58
+		$invoice_settings = isset(EE_Config::instance()->gateway->payment_settings['Invoice'])
59
+			? EE_Config::instance()->gateway->payment_settings['Invoice']
60
+			: null;
61
+		if (! $invoice_settings) {
62
+			$this->add_error(
63
+				esc_html__(
64
+					'Could not migrate EE4.4 invoice settings to EE4.5 because they didnt exist',
65
+					'event_espresso'
66
+				)
67
+			);
68
+		} else {
69
+			$invoice_settings['template_payment_instructions'] = $invoice_settings['pdf_instructions'];
70
+			$invoice_settings['template_invoice_payee_name']   = $invoice_settings['payable_to'];
71
+			$invoice_settings['template_invoice_address']      = $invoice_settings['payment_address'];
72
+			$invoice_settings['template_invoice_email']        = '';
73
+			$invoice_settings['template_invoice_tax_number']   = '';
74
+			unset($invoice_settings['pdf_instructions']);
75
+			unset($invoice_settings['payable_to']);
76
+			unset($invoice_settings['payment_address']);
77
+			EE_Config::instance()->gateway->payment_settings['Invoice'] = $invoice_settings;
78
+			EE_Config::instance()->update_espresso_config(false, false);
79 79
 
80
-            // @todo: check 'invoice_css' too because we can't easily affect that so we might need to set a persistent notice
81
-            // (why is it tough to change? because we want to update the receipt and invoice message template, but
82
-            // message templates are only initialized AFTER migrations and those two are new in 4.5. So if we wanted to
83
-            // update them from a DMS, we'd need to have the DMS create the message templates which is quite a lot of code;
84
-            // also we don't want to build a dependency on the messages code because it is likely to change soon
85
-            if (
86
-                isset($invoice_settings['invoice_css']) &&
87
-                ! in_array($invoice_settings['invoice_css'], ['', 'simple.css'])
88
-            ) {
89
-                new PersistentAdminNotice(
90
-                    'invoice_css_not_updated',
91
-                    sprintf(
92
-                        esc_html__(
93
-                            'You had previously set your Invoice Payment Method\'s stylesheet to be %1$s, but that setting has moved. PDF and HTML Invoices and Receipts are now Messages, which means you can easily modify them from your Wordpress Dashboard instead of using filters or uploading template files. Please visit Messages -> Receipt and Messages -> Invoice to change their stylesheets.',
94
-                            'event_espresso'
95
-                        ),
96
-                        $invoice_settings['invoice_css']
97
-                    )
98
-                );
99
-            }
100
-            $templates_relative_path = 'modules/gateways/Invoice/lib/templates/';
101
-            $overridden_invoice_body = EEH_Template::locate_template(
102
-                $templates_relative_path . 'invoice_body.template.php',
103
-                null,
104
-                false,
105
-                false,
106
-                true
107
-            );
108
-            $overridden_receipt_body = EEH_Template::locate_template(
109
-                $templates_relative_path . 'receipt_body.template.php',
110
-                null,
111
-                false,
112
-                false,
113
-                true
114
-            );
115
-            if ($overridden_invoice_body || $overridden_receipt_body) {
116
-                new PersistentAdminNotice(
117
-                    'invoice_overriding_templates', esc_html__(
118
-                    'Note: in this version of Event Espresso, PDF and HTML Invoices and Receipts are now Messages and can be changed just like any other messages; however we noticed you had previously overridden the old default Invoice/Receipt templates. Because of this, your old Invoice/Receipt templates will continue to be used INSTEAD of the new Invoice/Receipt message equivalents. We recommend deleting your old Invoice/Receipt templates and modifying the new Invoice and Receipt messages\'s content in Messages -> Invoice and Messages -> Receipt.',
119
-                    'event_espresso'
120
-                ),  true
121
-                );
122
-            }
123
-        }
124
-        // regardless of whether it worked or not, we ought to continue the migration
125
-        $this->set_completed();
126
-        return 1;
127
-    }
80
+			// @todo: check 'invoice_css' too because we can't easily affect that so we might need to set a persistent notice
81
+			// (why is it tough to change? because we want to update the receipt and invoice message template, but
82
+			// message templates are only initialized AFTER migrations and those two are new in 4.5. So if we wanted to
83
+			// update them from a DMS, we'd need to have the DMS create the message templates which is quite a lot of code;
84
+			// also we don't want to build a dependency on the messages code because it is likely to change soon
85
+			if (
86
+				isset($invoice_settings['invoice_css']) &&
87
+				! in_array($invoice_settings['invoice_css'], ['', 'simple.css'])
88
+			) {
89
+				new PersistentAdminNotice(
90
+					'invoice_css_not_updated',
91
+					sprintf(
92
+						esc_html__(
93
+							'You had previously set your Invoice Payment Method\'s stylesheet to be %1$s, but that setting has moved. PDF and HTML Invoices and Receipts are now Messages, which means you can easily modify them from your Wordpress Dashboard instead of using filters or uploading template files. Please visit Messages -> Receipt and Messages -> Invoice to change their stylesheets.',
94
+							'event_espresso'
95
+						),
96
+						$invoice_settings['invoice_css']
97
+					)
98
+				);
99
+			}
100
+			$templates_relative_path = 'modules/gateways/Invoice/lib/templates/';
101
+			$overridden_invoice_body = EEH_Template::locate_template(
102
+				$templates_relative_path . 'invoice_body.template.php',
103
+				null,
104
+				false,
105
+				false,
106
+				true
107
+			);
108
+			$overridden_receipt_body = EEH_Template::locate_template(
109
+				$templates_relative_path . 'receipt_body.template.php',
110
+				null,
111
+				false,
112
+				false,
113
+				true
114
+			);
115
+			if ($overridden_invoice_body || $overridden_receipt_body) {
116
+				new PersistentAdminNotice(
117
+					'invoice_overriding_templates', esc_html__(
118
+					'Note: in this version of Event Espresso, PDF and HTML Invoices and Receipts are now Messages and can be changed just like any other messages; however we noticed you had previously overridden the old default Invoice/Receipt templates. Because of this, your old Invoice/Receipt templates will continue to be used INSTEAD of the new Invoice/Receipt message equivalents. We recommend deleting your old Invoice/Receipt templates and modifying the new Invoice and Receipt messages\'s content in Messages -> Invoice and Messages -> Receipt.',
119
+					'event_espresso'
120
+				),  true
121
+				);
122
+			}
123
+		}
124
+		// regardless of whether it worked or not, we ought to continue the migration
125
+		$this->set_completed();
126
+		return 1;
127
+	}
128 128
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -50,7 +50,7 @@  discard block
 block discarded – undo
50 50
     protected function _migration_step($num_items_to_migrate = 1)
51 51
     {
52 52
         // if this isn't set then something is really wrong
53
-        if (! EE_Config::instance()->gateway instanceof EE_Gateway_Config) {
53
+        if ( ! EE_Config::instance()->gateway instanceof EE_Gateway_Config) {
54 54
             throw new EE_Error(
55 55
                 esc_html__('It appears the Event Espresso Core Configuration is not setup correctly.', 'event_espresso')
56 56
             );
@@ -58,7 +58,7 @@  discard block
 block discarded – undo
58 58
         $invoice_settings = isset(EE_Config::instance()->gateway->payment_settings['Invoice'])
59 59
             ? EE_Config::instance()->gateway->payment_settings['Invoice']
60 60
             : null;
61
-        if (! $invoice_settings) {
61
+        if ( ! $invoice_settings) {
62 62
             $this->add_error(
63 63
                 esc_html__(
64 64
                     'Could not migrate EE4.4 invoice settings to EE4.5 because they didnt exist',
@@ -99,14 +99,14 @@  discard block
 block discarded – undo
99 99
             }
100 100
             $templates_relative_path = 'modules/gateways/Invoice/lib/templates/';
101 101
             $overridden_invoice_body = EEH_Template::locate_template(
102
-                $templates_relative_path . 'invoice_body.template.php',
102
+                $templates_relative_path.'invoice_body.template.php',
103 103
                 null,
104 104
                 false,
105 105
                 false,
106 106
                 true
107 107
             );
108 108
             $overridden_receipt_body = EEH_Template::locate_template(
109
-                $templates_relative_path . 'receipt_body.template.php',
109
+                $templates_relative_path.'receipt_body.template.php',
110 110
                 null,
111 111
                 false,
112 112
                 false,
@@ -117,7 +117,7 @@  discard block
 block discarded – undo
117 117
                     'invoice_overriding_templates', esc_html__(
118 118
                     'Note: in this version of Event Espresso, PDF and HTML Invoices and Receipts are now Messages and can be changed just like any other messages; however we noticed you had previously overridden the old default Invoice/Receipt templates. Because of this, your old Invoice/Receipt templates will continue to be used INSTEAD of the new Invoice/Receipt message equivalents. We recommend deleting your old Invoice/Receipt templates and modifying the new Invoice and Receipt messages\'s content in Messages -> Invoice and Messages -> Receipt.',
119 119
                     'event_espresso'
120
-                ),  true
120
+                ), true
121 121
                 );
122 122
             }
123 123
         }
Please login to merge, or discard this patch.
4_10_0_stages/EE_DMS_4_10_0_Event_Question_Group.dmsstage.php 2 patches
Indentation   +134 added lines, -134 removed lines patch added patch discarded remove patch
@@ -10,144 +10,144 @@
 block discarded – undo
10 10
 class EE_DMS_4_10_0_Event_Question_Group extends EE_Data_Migration_Script_Stage_Table
11 11
 {
12 12
 
13
-    /**
14
-     * Just initializes the status of the migration
15
-     */
16
-    public function __construct()
17
-    {
18
-        global $wpdb;
19
-        $this->_pretty_name     = esc_html__('Event-Question Group Relations', 'event_espresso');
20
-        $this->_old_table       = $wpdb->prefix . 'esp_event_question_group';
21
-        $this->_extra_where_sql = "WHERE EQG_primary = 0 AND EQG_additional=0";
22
-        parent::__construct();
23
-    }
13
+	/**
14
+	 * Just initializes the status of the migration
15
+	 */
16
+	public function __construct()
17
+	{
18
+		global $wpdb;
19
+		$this->_pretty_name     = esc_html__('Event-Question Group Relations', 'event_espresso');
20
+		$this->_old_table       = $wpdb->prefix . 'esp_event_question_group';
21
+		$this->_extra_where_sql = "WHERE EQG_primary = 0 AND EQG_additional=0";
22
+		parent::__construct();
23
+	}
24 24
 
25 25
 
26
-    /**
27
-     * Removes the duplicate event_question_group rows that only had EQG_primary=0. Now we just have one row
28
-     * joining event-to-question-groups with two columns: EQG_primary and EQG_additional, indicating which question
29
-     * groups apply to which category of registrant.
30
-     *
31
-     * @param array $old_row an associative array where keys are column names and values are their values.
32
-     * @return void
33
-     */
34
-    protected function _migrate_old_row($old_row)
35
-    {
36
-        if (isset($old_row['EVT_ID'], $old_row['QSG_ID'])) {
37
-            global $wpdb;
38
-            // If the question group was also for primary attendees, we should just update that row.
39
-            // And we delete this row.
40
-            // Updating all the rows could be slow on massive DBs, so do the slow selection first, then a quick update
41
-            // in order to avoid locking the table for too long.
42
-            $ids_to_update = $wpdb->get_col(
43
-                $wpdb->prepare(
44
-                    'SELECT EQG_ID FROM ' . $this->_old_table . ' WHERE EQG_primary=1 AND EVT_ID=%d AND QSG_ID=%d',
45
-                    $old_row['EVT_ID'],
46
-                    $old_row['QSG_ID']
47
-                )
48
-            );
49
-            $success       = false;
50
-            if ($ids_to_update) {
51
-                $success = $wpdb->query(
52
-                    'UPDATE ' .
53
-                    $this->_old_table .
54
-                    ' SET EQG_additional=1 WHERE EQG_ID IN (' .
55
-                    implode(',', array_map('intval', $ids_to_update)) .
56
-                    ') LIMIT ' .
57
-                    count($ids_to_update)
58
-                );
59
-            }
60
-            if ($success) {
61
-                // Ok it's confirmed: the question group WAS for the primary attendee group too. So
62
-                // now we just need to delete this row.
63
-                $successful_delete = $wpdb->delete(
64
-                    $this->_old_table,
65
-                    [
66
-                        'EQG_ID' => $old_row['EQG_ID'],
67
-                    ],
68
-                    ['%d']
69
-                );
70
-                if (! $successful_delete) {
71
-                    $this->add_error(
72
-                        sprintf(
73
-                            esc_html__(
74
-                                'Could not delete old event-question group relation row "%1$s" because "%2$s"',
75
-                                'event_espresso'
76
-                            ),
77
-                            wp_json_encode($old_row),
78
-                            $wpdb->last_error
79
-                        )
80
-                    );
81
-                }
82
-            } else {
83
-                // Oh, the question group actually was NOT for the primary attendee. So we just need to update this row
84
-                // Let's do the selection separately from the deletion, this way we don't lock big tables for too long.
85
-                $ids_to_update2 = $wpdb->get_col(
86
-                    $wpdb->prepare(
87
-                        'SELECT EQG_ID FROM ' . $this->_old_table . ' WHERE EVT_ID=%d AND QSG_ID=%d',
88
-                        $old_row['EVT_ID'],
89
-                        $old_row['QSG_ID']
90
-                    )
91
-                );
92
-                if ($ids_to_update2) {
93
-                    $wpdb->query(
94
-                        'UPDATE ' .
95
-                        $this->_old_table .
96
-                        ' SET EQG_additional=1 WHERE EQG_ID IN (' .
97
-                        implode(',', array_map('intval', $ids_to_update2)) .
98
-                        ') LIMIT ' .
99
-                        count($ids_to_update2)
100
-                    );
101
-                }
102
-            }
103
-        }
104
-    }
26
+	/**
27
+	 * Removes the duplicate event_question_group rows that only had EQG_primary=0. Now we just have one row
28
+	 * joining event-to-question-groups with two columns: EQG_primary and EQG_additional, indicating which question
29
+	 * groups apply to which category of registrant.
30
+	 *
31
+	 * @param array $old_row an associative array where keys are column names and values are their values.
32
+	 * @return void
33
+	 */
34
+	protected function _migrate_old_row($old_row)
35
+	{
36
+		if (isset($old_row['EVT_ID'], $old_row['QSG_ID'])) {
37
+			global $wpdb;
38
+			// If the question group was also for primary attendees, we should just update that row.
39
+			// And we delete this row.
40
+			// Updating all the rows could be slow on massive DBs, so do the slow selection first, then a quick update
41
+			// in order to avoid locking the table for too long.
42
+			$ids_to_update = $wpdb->get_col(
43
+				$wpdb->prepare(
44
+					'SELECT EQG_ID FROM ' . $this->_old_table . ' WHERE EQG_primary=1 AND EVT_ID=%d AND QSG_ID=%d',
45
+					$old_row['EVT_ID'],
46
+					$old_row['QSG_ID']
47
+				)
48
+			);
49
+			$success       = false;
50
+			if ($ids_to_update) {
51
+				$success = $wpdb->query(
52
+					'UPDATE ' .
53
+					$this->_old_table .
54
+					' SET EQG_additional=1 WHERE EQG_ID IN (' .
55
+					implode(',', array_map('intval', $ids_to_update)) .
56
+					') LIMIT ' .
57
+					count($ids_to_update)
58
+				);
59
+			}
60
+			if ($success) {
61
+				// Ok it's confirmed: the question group WAS for the primary attendee group too. So
62
+				// now we just need to delete this row.
63
+				$successful_delete = $wpdb->delete(
64
+					$this->_old_table,
65
+					[
66
+						'EQG_ID' => $old_row['EQG_ID'],
67
+					],
68
+					['%d']
69
+				);
70
+				if (! $successful_delete) {
71
+					$this->add_error(
72
+						sprintf(
73
+							esc_html__(
74
+								'Could not delete old event-question group relation row "%1$s" because "%2$s"',
75
+								'event_espresso'
76
+							),
77
+							wp_json_encode($old_row),
78
+							$wpdb->last_error
79
+						)
80
+					);
81
+				}
82
+			} else {
83
+				// Oh, the question group actually was NOT for the primary attendee. So we just need to update this row
84
+				// Let's do the selection separately from the deletion, this way we don't lock big tables for too long.
85
+				$ids_to_update2 = $wpdb->get_col(
86
+					$wpdb->prepare(
87
+						'SELECT EQG_ID FROM ' . $this->_old_table . ' WHERE EVT_ID=%d AND QSG_ID=%d',
88
+						$old_row['EVT_ID'],
89
+						$old_row['QSG_ID']
90
+					)
91
+				);
92
+				if ($ids_to_update2) {
93
+					$wpdb->query(
94
+						'UPDATE ' .
95
+						$this->_old_table .
96
+						' SET EQG_additional=1 WHERE EQG_ID IN (' .
97
+						implode(',', array_map('intval', $ids_to_update2)) .
98
+						') LIMIT ' .
99
+						count($ids_to_update2)
100
+					);
101
+				}
102
+			}
103
+		}
104
+	}
105 105
 
106 106
 
107
-    /**
108
-     * Gets the rows for the existing table that shouldn't exist in 4.10.
109
-     * Specifically the rows where EQG_primary=false and EQG_additional=false.
110
-     * Gotcha: because the migration is REMOVING rows as it goes, we shouldn't use the offset.
111
-     *
112
-     * @param int   $limit
113
-     * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
114
-     * @global wpdb $wpdb
115
-     */
116
-    protected function _get_rows($limit)
117
-    {
118
-        global $wpdb;
119
-        $query = "SELECT * FROM $this->_old_table $this->_extra_where_sql " . $wpdb->prepare(
120
-                "LIMIT %d",
121
-                $limit
122
-            );
123
-        return $wpdb->get_results($query, ARRAY_A);
124
-    }
107
+	/**
108
+	 * Gets the rows for the existing table that shouldn't exist in 4.10.
109
+	 * Specifically the rows where EQG_primary=false and EQG_additional=false.
110
+	 * Gotcha: because the migration is REMOVING rows as it goes, we shouldn't use the offset.
111
+	 *
112
+	 * @param int   $limit
113
+	 * @return array of arrays like $wpdb->get_results($sql, ARRAY_A)
114
+	 * @global wpdb $wpdb
115
+	 */
116
+	protected function _get_rows($limit)
117
+	{
118
+		global $wpdb;
119
+		$query = "SELECT * FROM $this->_old_table $this->_extra_where_sql " . $wpdb->prepare(
120
+				"LIMIT %d",
121
+				$limit
122
+			);
123
+		return $wpdb->get_results($query, ARRAY_A);
124
+	}
125 125
 
126 126
 
127
-    /**
128
-     * Slight departure from the normal procedure here: because this removes rows from the DB, we need to ensure
129
-     * we count the records first, then do the migration.
130
-     *
131
-     * @param int $num_items_to_migrate
132
-     * @return int number of items ACTUALLY migrated
133
-     */
134
-    public function _migration_step($num_items_to_migrate = 50)
135
-    {
136
-        // Count the items right away. This migration step will be removing those rows, so we need to count them
137
-        // right away to get an accurate count.
138
-        $this->count_records_to_migrate();
139
-        $rows                    = $this->_get_rows($num_items_to_migrate);
140
-        $items_actually_migrated = 0;
141
-        foreach ($rows as $old_row) {
142
-            $this->_migrate_old_row($old_row);
143
-            $items_actually_migrated++;
144
-        }
145
-        if (
146
-            empty($rows) ||
147
-            ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate())
148
-        ) {
149
-            $this->set_completed();
150
-        }
151
-        return $items_actually_migrated;
152
-    }
127
+	/**
128
+	 * Slight departure from the normal procedure here: because this removes rows from the DB, we need to ensure
129
+	 * we count the records first, then do the migration.
130
+	 *
131
+	 * @param int $num_items_to_migrate
132
+	 * @return int number of items ACTUALLY migrated
133
+	 */
134
+	public function _migration_step($num_items_to_migrate = 50)
135
+	{
136
+		// Count the items right away. This migration step will be removing those rows, so we need to count them
137
+		// right away to get an accurate count.
138
+		$this->count_records_to_migrate();
139
+		$rows                    = $this->_get_rows($num_items_to_migrate);
140
+		$items_actually_migrated = 0;
141
+		foreach ($rows as $old_row) {
142
+			$this->_migrate_old_row($old_row);
143
+			$items_actually_migrated++;
144
+		}
145
+		if (
146
+			empty($rows) ||
147
+			($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate())
148
+		) {
149
+			$this->set_completed();
150
+		}
151
+		return $items_actually_migrated;
152
+	}
153 153
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
     {
18 18
         global $wpdb;
19 19
         $this->_pretty_name     = esc_html__('Event-Question Group Relations', 'event_espresso');
20
-        $this->_old_table       = $wpdb->prefix . 'esp_event_question_group';
20
+        $this->_old_table       = $wpdb->prefix.'esp_event_question_group';
21 21
         $this->_extra_where_sql = "WHERE EQG_primary = 0 AND EQG_additional=0";
22 22
         parent::__construct();
23 23
     }
@@ -41,19 +41,19 @@  discard block
 block discarded – undo
41 41
             // in order to avoid locking the table for too long.
42 42
             $ids_to_update = $wpdb->get_col(
43 43
                 $wpdb->prepare(
44
-                    'SELECT EQG_ID FROM ' . $this->_old_table . ' WHERE EQG_primary=1 AND EVT_ID=%d AND QSG_ID=%d',
44
+                    'SELECT EQG_ID FROM '.$this->_old_table.' WHERE EQG_primary=1 AND EVT_ID=%d AND QSG_ID=%d',
45 45
                     $old_row['EVT_ID'],
46 46
                     $old_row['QSG_ID']
47 47
                 )
48 48
             );
49
-            $success       = false;
49
+            $success = false;
50 50
             if ($ids_to_update) {
51 51
                 $success = $wpdb->query(
52
-                    'UPDATE ' .
53
-                    $this->_old_table .
54
-                    ' SET EQG_additional=1 WHERE EQG_ID IN (' .
55
-                    implode(',', array_map('intval', $ids_to_update)) .
56
-                    ') LIMIT ' .
52
+                    'UPDATE '.
53
+                    $this->_old_table.
54
+                    ' SET EQG_additional=1 WHERE EQG_ID IN ('.
55
+                    implode(',', array_map('intval', $ids_to_update)).
56
+                    ') LIMIT '.
57 57
                     count($ids_to_update)
58 58
                 );
59 59
             }
@@ -67,7 +67,7 @@  discard block
 block discarded – undo
67 67
                     ],
68 68
                     ['%d']
69 69
                 );
70
-                if (! $successful_delete) {
70
+                if ( ! $successful_delete) {
71 71
                     $this->add_error(
72 72
                         sprintf(
73 73
                             esc_html__(
@@ -84,18 +84,18 @@  discard block
 block discarded – undo
84 84
                 // Let's do the selection separately from the deletion, this way we don't lock big tables for too long.
85 85
                 $ids_to_update2 = $wpdb->get_col(
86 86
                     $wpdb->prepare(
87
-                        'SELECT EQG_ID FROM ' . $this->_old_table . ' WHERE EVT_ID=%d AND QSG_ID=%d',
87
+                        'SELECT EQG_ID FROM '.$this->_old_table.' WHERE EVT_ID=%d AND QSG_ID=%d',
88 88
                         $old_row['EVT_ID'],
89 89
                         $old_row['QSG_ID']
90 90
                     )
91 91
                 );
92 92
                 if ($ids_to_update2) {
93 93
                     $wpdb->query(
94
-                        'UPDATE ' .
95
-                        $this->_old_table .
96
-                        ' SET EQG_additional=1 WHERE EQG_ID IN (' .
97
-                        implode(',', array_map('intval', $ids_to_update2)) .
98
-                        ') LIMIT ' .
94
+                        'UPDATE '.
95
+                        $this->_old_table.
96
+                        ' SET EQG_additional=1 WHERE EQG_ID IN ('.
97
+                        implode(',', array_map('intval', $ids_to_update2)).
98
+                        ') LIMIT '.
99 99
                         count($ids_to_update2)
100 100
                     );
101 101
                 }
@@ -116,7 +116,7 @@  discard block
 block discarded – undo
116 116
     protected function _get_rows($limit)
117 117
     {
118 118
         global $wpdb;
119
-        $query = "SELECT * FROM $this->_old_table $this->_extra_where_sql " . $wpdb->prepare(
119
+        $query = "SELECT * FROM $this->_old_table $this->_extra_where_sql ".$wpdb->prepare(
120 120
                 "LIMIT %d",
121 121
                 $limit
122 122
             );
Please login to merge, or discard this patch.
core/data_migration_scripts/EE_DMS_Core_4_8_0.dms.php 2 patches
Indentation   +386 added lines, -386 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_8_0_stages/*');
11 11
 $class_to_filepath = [];
12 12
 foreach ($stages as $filepath) {
13
-    $matches = [];
14
-    preg_match('~4_8_0_stages/(.*).dmsstage.php~', $filepath, $matches);
15
-    $class_to_filepath[ $matches[1] ] = $filepath;
13
+	$matches = [];
14
+	preg_match('~4_8_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_8_0__autoloaded_stages', $class_to_filepath);
@@ -35,76 +35,76 @@  discard block
 block discarded – undo
35 35
 class EE_DMS_Core_4_8_0 extends EE_Data_Migration_Script_Base
36 36
 {
37 37
 
38
-    /**
39
-     * return EE_DMS_Core_4_8_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.8.0", "event_espresso");
47
-        $this->_priority         = 10;
48
-        $this->_migration_stages = [
49
-            new EE_DMS_4_8_0_pretax_totals(),
50
-            new EE_DMS_4_8_0_event_subtotals(),
51
-        ];
52
-        parent::__construct($table_manager, $table_analysis);
53
-    }
54
-
55
-
56
-    /**
57
-     * Because this is being done at basically the same time as the MER-ready branch
58
-     * of core, it's possible people might have installed MEr-ready branch first,
59
-     * and then this one, in which case we still want to perform this migration,
60
-     * even though the version might not have increased
61
-     *
62
-     * @param array $version_array
63
-     * @return bool
64
-     */
65
-    public function can_migrate_from_version($version_array)
66
-    {
67
-        $version_string = $version_array['Core'];
68
-        if (
69
-            version_compare($version_string, '4.8.0.decaf', '<') &&
70
-            version_compare($version_string, '4.7.0.decaf', '>=')
71
-        ) {
72
-            //          echo "$version_string can be migrated from";
73
-            return true;
74
-        } elseif (! $version_string) {
75
-            //          echo "no version string provided: $version_string";
76
-            // no version string provided... this must be pre 4.3
77
-            return false;// changed mind. dont want people thinking they should migrate yet because they cant
78
-        } else {
79
-            //          echo "$version_string doesnt apply";
80
-            return false;
81
-        }
82
-    }
83
-
84
-
85
-    /**
86
-     * @return bool
87
-     * @throws EE_Error
88
-     * @throws ReflectionException
89
-     */
90
-    public function schema_changes_before_migration()
91
-    {
92
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
93
-        $now_in_mysql = current_time('mysql', true);
94
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
95
-
96
-        $table_name = 'esp_answer';
97
-        $sql        = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
38
+	/**
39
+	 * return EE_DMS_Core_4_8_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.8.0", "event_espresso");
47
+		$this->_priority         = 10;
48
+		$this->_migration_stages = [
49
+			new EE_DMS_4_8_0_pretax_totals(),
50
+			new EE_DMS_4_8_0_event_subtotals(),
51
+		];
52
+		parent::__construct($table_manager, $table_analysis);
53
+	}
54
+
55
+
56
+	/**
57
+	 * Because this is being done at basically the same time as the MER-ready branch
58
+	 * of core, it's possible people might have installed MEr-ready branch first,
59
+	 * and then this one, in which case we still want to perform this migration,
60
+	 * even though the version might not have increased
61
+	 *
62
+	 * @param array $version_array
63
+	 * @return bool
64
+	 */
65
+	public function can_migrate_from_version($version_array)
66
+	{
67
+		$version_string = $version_array['Core'];
68
+		if (
69
+			version_compare($version_string, '4.8.0.decaf', '<') &&
70
+			version_compare($version_string, '4.7.0.decaf', '>=')
71
+		) {
72
+			//          echo "$version_string can be migrated from";
73
+			return true;
74
+		} elseif (! $version_string) {
75
+			//          echo "no version string provided: $version_string";
76
+			// no version string provided... this must be pre 4.3
77
+			return false;// changed mind. dont want people thinking they should migrate yet because they cant
78
+		} else {
79
+			//          echo "$version_string doesnt apply";
80
+			return false;
81
+		}
82
+	}
83
+
84
+
85
+	/**
86
+	 * @return bool
87
+	 * @throws EE_Error
88
+	 * @throws ReflectionException
89
+	 */
90
+	public function schema_changes_before_migration()
91
+	{
92
+		require_once(EE_HELPERS . 'EEH_Activation.helper.php');
93
+		$now_in_mysql = current_time('mysql', true);
94
+		require_once(EE_HELPERS . 'EEH_Activation.helper.php');
95
+
96
+		$table_name = 'esp_answer';
97
+		$sql        = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
98 98
 					REG_ID int(10) unsigned NOT NULL,
99 99
 					QST_ID int(10) unsigned NOT NULL,
100 100
 					ANS_value text NOT NULL,
101 101
 					PRIMARY KEY  (ANS_ID),
102 102
 					KEY REG_ID (REG_ID),
103 103
 					KEY QST_ID (QST_ID)";
104
-        $this->_table_is_changed_in_this_version($table_name, $sql);
104
+		$this->_table_is_changed_in_this_version($table_name, $sql);
105 105
 
106
-        $table_name = 'esp_attendee_meta';
107
-        $sql        = "ATTM_ID int(10) unsigned NOT	NULL AUTO_INCREMENT,
106
+		$table_name = 'esp_attendee_meta';
107
+		$sql        = "ATTM_ID int(10) unsigned NOT	NULL AUTO_INCREMENT,
108 108
 						ATT_ID bigint(20) unsigned NOT NULL,
109 109
 						ATT_fname varchar(45) NOT NULL,
110 110
 						ATT_lname varchar(45) NOT	NULL,
@@ -119,10 +119,10 @@  discard block
 block discarded – undo
119 119
 							PRIMARY KEY  (ATTM_ID),
120 120
 								KEY ATT_ID (ATT_ID),
121 121
 								KEY ATT_email (ATT_email(191))";
122
-        $this->_table_is_changed_in_this_version($table_name, $sql);
122
+		$this->_table_is_changed_in_this_version($table_name, $sql);
123 123
 
124
-        $table_name = 'esp_country';
125
-        $sql        = "CNT_ISO varchar(2) collate utf8_bin NOT NULL,
124
+		$table_name = 'esp_country';
125
+		$sql        = "CNT_ISO varchar(2) collate utf8_bin NOT NULL,
126 126
 					  CNT_ISO3 varchar(3) collate utf8_bin NOT NULL,
127 127
 					  RGN_ID tinyint(3) unsigned DEFAULT NULL,
128 128
 					  CNT_name varchar(45) collate utf8_bin NOT NULL,
@@ -138,20 +138,20 @@  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
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
141
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
142 142
 
143
-        $table_name = 'esp_currency';
144
-        $sql        = "CUR_code varchar(6) collate utf8_bin NOT NULL,
143
+		$table_name = 'esp_currency';
144
+		$sql        = "CUR_code varchar(6) collate utf8_bin NOT NULL,
145 145
 				CUR_single varchar(45) collate utf8_bin DEFAULT 'dollar',
146 146
 				CUR_plural varchar(45) collate utf8_bin DEFAULT 'dollars',
147 147
 				CUR_sign varchar(45) collate utf8_bin DEFAULT '$',
148 148
 				CUR_dec_plc varchar(1) collate utf8_bin NOT NULL DEFAULT '2',
149 149
 				CUR_active tinyint(1) DEFAULT '0',
150 150
 				PRIMARY KEY  (CUR_code)";
151
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
151
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
152 152
 
153
-        $table_name = 'esp_datetime';
154
-        $sql        = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
153
+		$table_name = 'esp_datetime';
154
+		$sql        = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
155 155
 				  EVT_ID bigint(20) unsigned NOT NULL,
156 156
 				  DTT_name varchar(255) NOT NULL DEFAULT '',
157 157
 				  DTT_description text NOT NULL,
@@ -167,10 +167,10 @@  discard block
 block discarded – undo
167 167
 						KEY DTT_EVT_start (DTT_EVT_start),
168 168
 						KEY EVT_ID (EVT_ID),
169 169
 						KEY DTT_is_primary (DTT_is_primary)";
170
-        $this->_table_is_changed_in_this_version($table_name, $sql);
170
+		$this->_table_is_changed_in_this_version($table_name, $sql);
171 171
 
172
-        $table_name = 'esp_event_meta';
173
-        $sql        = "
172
+		$table_name = 'esp_event_meta';
173
+		$sql        = "
174 174
 			EVTM_ID int(10) NOT NULL AUTO_INCREMENT,
175 175
 			EVT_ID bigint(20) unsigned NOT NULL,
176 176
 			EVT_display_desc tinyint(1) unsigned NOT NULL DEFAULT 1,
@@ -186,38 +186,38 @@  discard block
 block discarded – undo
186 186
 			EVT_donations tinyint(1) NULL,
187 187
 			PRIMARY KEY  (EVTM_ID),
188 188
 			KEY EVT_ID (EVT_ID)";
189
-        $this->_table_is_changed_in_this_version($table_name, $sql);
189
+		$this->_table_is_changed_in_this_version($table_name, $sql);
190 190
 
191
-        $table_name = 'esp_event_question_group';
192
-        $sql        = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
191
+		$table_name = 'esp_event_question_group';
192
+		$sql        = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
193 193
 					EVT_ID bigint(20) unsigned NOT NULL,
194 194
 					QSG_ID int(10) unsigned NOT NULL,
195 195
 					EQG_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
196 196
 					PRIMARY KEY  (EQG_ID),
197 197
 					KEY EVT_ID (EVT_ID),
198 198
 					KEY QSG_ID (QSG_ID)";
199
-        $this->_table_is_changed_in_this_version($table_name, $sql);
199
+		$this->_table_is_changed_in_this_version($table_name, $sql);
200 200
 
201
-        $table_name = 'esp_event_venue';
202
-        $sql        = "EVV_ID int(11) NOT NULL AUTO_INCREMENT,
201
+		$table_name = 'esp_event_venue';
202
+		$sql        = "EVV_ID int(11) NOT NULL AUTO_INCREMENT,
203 203
 				EVT_ID bigint(20) unsigned NOT NULL,
204 204
 				VNU_ID bigint(20) unsigned NOT NULL,
205 205
 				EVV_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
206 206
 				PRIMARY KEY  (EVV_ID)";
207
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
207
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
208 208
 
209
-        $table_name = 'esp_extra_meta';
210
-        $sql        = "EXM_ID int(11) NOT NULL AUTO_INCREMENT,
209
+		$table_name = 'esp_extra_meta';
210
+		$sql        = "EXM_ID int(11) NOT NULL AUTO_INCREMENT,
211 211
 				OBJ_ID int(11) DEFAULT NULL,
212 212
 				EXM_type varchar(45) DEFAULT NULL,
213 213
 				EXM_key varchar(45) DEFAULT NULL,
214 214
 				EXM_value text,
215 215
 				PRIMARY KEY  (EXM_ID),
216 216
 				KEY EXM_type (EXM_type,OBJ_ID,EXM_key)";
217
-        $this->_table_is_changed_in_this_version($table_name, $sql);
217
+		$this->_table_is_changed_in_this_version($table_name, $sql);
218 218
 
219
-        $table_name = 'esp_extra_join';
220
-        $sql        = "EXJ_ID int(11) NOT NULL AUTO_INCREMENT,
219
+		$table_name = 'esp_extra_join';
220
+		$sql        = "EXJ_ID int(11) NOT NULL AUTO_INCREMENT,
221 221
 				EXJ_first_model_id varchar(6) NOT NULL,
222 222
 				EXJ_first_model_name varchar(20) NOT NULL,
223 223
 				EXJ_second_model_id varchar(6) NOT NULL,
@@ -225,10 +225,10 @@  discard block
 block discarded – undo
225 225
 				PRIMARY KEY  (EXJ_ID),
226 226
 				KEY first_model (EXJ_first_model_name,EXJ_first_model_id),
227 227
 				KEY second_model (EXJ_second_model_name,EXJ_second_model_id)";
228
-        $this->_table_is_new_in_this_version($table_name, $sql);
228
+		$this->_table_is_new_in_this_version($table_name, $sql);
229 229
 
230
-        $table_name = 'esp_line_item';
231
-        $sql        = "LIN_ID int(11) NOT NULL AUTO_INCREMENT,
230
+		$table_name = 'esp_line_item';
231
+		$sql        = "LIN_ID int(11) NOT NULL AUTO_INCREMENT,
232 232
 				LIN_code varchar(245) NOT NULL DEFAULT '',
233 233
 				TXN_ID int(11) DEFAULT NULL,
234 234
 				LIN_name varchar(245) NOT NULL DEFAULT '',
@@ -247,10 +247,10 @@  discard block
 block discarded – undo
247 247
 				PRIMARY KEY  (LIN_ID),
248 248
 				KEY LIN_code (LIN_code(191)),
249 249
 				KEY TXN_ID (TXN_ID)";
250
-        $this->_table_is_changed_in_this_version($table_name, $sql);
250
+		$this->_table_is_changed_in_this_version($table_name, $sql);
251 251
 
252
-        $table_name = 'esp_log';
253
-        $sql        = "LOG_ID int(11) NOT NULL AUTO_INCREMENT,
252
+		$table_name = 'esp_log';
253
+		$sql        = "LOG_ID int(11) NOT NULL AUTO_INCREMENT,
254 254
 				LOG_time datetime DEFAULT NULL,
255 255
 				OBJ_ID varchar(45) DEFAULT NULL,
256 256
 				OBJ_type varchar(45) DEFAULT NULL,
@@ -261,20 +261,20 @@  discard block
 block discarded – undo
261 261
 				KEY LOG_time (LOG_time),
262 262
 				KEY OBJ (OBJ_type,OBJ_ID),
263 263
 				KEY LOG_type (LOG_type)";
264
-        $this->_table_is_changed_in_this_version($table_name, $sql);
264
+		$this->_table_is_changed_in_this_version($table_name, $sql);
265 265
 
266
-        $table_name = 'esp_message_template';
267
-        $sql        = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
266
+		$table_name = 'esp_message_template';
267
+		$sql        = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
268 268
 					GRP_ID int(10) unsigned NOT NULL,
269 269
 					MTP_context varchar(50) NOT NULL,
270 270
 					MTP_template_field varchar(30) NOT NULL,
271 271
 					MTP_content text NOT NULL,
272 272
 					PRIMARY KEY  (MTP_ID),
273 273
 					KEY GRP_ID (GRP_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_message_template_group';
277
-        $sql        = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
276
+		$table_name = 'esp_message_template_group';
277
+		$sql        = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
278 278
 					MTP_user_id int(10) NOT NULL DEFAULT '1',
279 279
 					MTP_name varchar(245) NOT NULL DEFAULT '',
280 280
 					MTP_description varchar(245) NOT NULL DEFAULT '',
@@ -286,19 +286,19 @@  discard block
 block discarded – undo
286 286
 					MTP_is_active tinyint(1) NOT NULL DEFAULT '1',
287 287
 					PRIMARY KEY  (GRP_ID),
288 288
 					KEY MTP_user_id (MTP_user_id)";
289
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
289
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
290 290
 
291
-        $table_name = 'esp_event_message_template';
292
-        $sql        = "EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
291
+		$table_name = 'esp_event_message_template';
292
+		$sql        = "EMT_ID bigint(20) unsigned NOT NULL AUTO_INCREMENT,
293 293
 					EVT_ID bigint(20) unsigned NOT NULL DEFAULT 0,
294 294
 					GRP_ID int(10) unsigned NOT NULL DEFAULT 0,
295 295
 					PRIMARY KEY  (EMT_ID),
296 296
 					KEY EVT_ID (EVT_ID),
297 297
 					KEY GRP_ID (GRP_ID)";
298
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
298
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
299 299
 
300
-        $table_name = 'esp_payment';
301
-        $sql        = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
300
+		$table_name = 'esp_payment';
301
+		$sql        = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
302 302
 					TXN_ID int(10) unsigned DEFAULT NULL,
303 303
 					STS_ID varchar(3) collate utf8_bin DEFAULT NULL,
304 304
 					PAY_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
@@ -315,10 +315,10 @@  discard block
 block discarded – undo
315 315
 					PRIMARY KEY  (PAY_ID),
316 316
 					KEY PAY_timestamp (PAY_timestamp),
317 317
 					KEY TXN_ID (TXN_ID)";
318
-        $this->_table_is_changed_in_this_version($table_name, $sql);
318
+		$this->_table_is_changed_in_this_version($table_name, $sql);
319 319
 
320
-        $table_name = 'esp_payment_method';
321
-        $sql        = "PMD_ID int(11) NOT NULL AUTO_INCREMENT,
320
+		$table_name = 'esp_payment_method';
321
+		$sql        = "PMD_ID int(11) NOT NULL AUTO_INCREMENT,
322 322
 				PMD_type varchar(124) DEFAULT NULL,
323 323
 				PMD_name varchar(255) DEFAULT NULL,
324 324
 				PMD_desc text,
@@ -334,36 +334,36 @@  discard block
 block discarded – undo
334 334
 				PRIMARY KEY  (PMD_ID),
335 335
 				UNIQUE KEY PMD_slug_UNIQUE (PMD_slug),
336 336
 				KEY PMD_type (PMD_type)";
337
-        $this->_table_is_changed_in_this_version($table_name, $sql);
337
+		$this->_table_is_changed_in_this_version($table_name, $sql);
338 338
 
339
-        $table_name = "esp_ticket_price";
340
-        $sql        = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
339
+		$table_name = "esp_ticket_price";
340
+		$sql        = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
341 341
 					  TKT_ID int(10) unsigned NOT NULL,
342 342
 					  PRC_ID int(10) unsigned NOT NULL,
343 343
 					  PRIMARY KEY  (TKP_ID),
344 344
 					  KEY TKT_ID (TKT_ID),
345 345
 					  KEY PRC_ID (PRC_ID)";
346
-        $this->_table_is_changed_in_this_version($table_name, $sql);
346
+		$this->_table_is_changed_in_this_version($table_name, $sql);
347 347
 
348
-        $table_name = "esp_datetime_ticket";
349
-        $sql        = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
348
+		$table_name = "esp_datetime_ticket";
349
+		$sql        = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
350 350
 					  DTT_ID int(10) unsigned NOT NULL,
351 351
 					  TKT_ID int(10) unsigned NOT NULL,
352 352
 					  PRIMARY KEY  (DTK_ID),
353 353
 					  KEY DTT_ID (DTT_ID),
354 354
 					  KEY TKT_ID (TKT_ID)";
355
-        $this->_table_is_changed_in_this_version($table_name, $sql);
355
+		$this->_table_is_changed_in_this_version($table_name, $sql);
356 356
 
357
-        $table_name = "esp_ticket_template";
358
-        $sql        = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
357
+		$table_name = "esp_ticket_template";
358
+		$sql        = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
359 359
 					  TTM_name varchar(45) NOT NULL,
360 360
 					  TTM_description text,
361 361
 					  TTM_file varchar(45),
362 362
 					  PRIMARY KEY  (TTM_ID)";
363
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
363
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
364 364
 
365
-        $table_name = 'esp_question';
366
-        $sql        = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
365
+		$table_name = 'esp_question';
366
+		$sql        = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
367 367
 					QST_display_text text NOT NULL,
368 368
 					QST_admin_label varchar(255) NOT NULL,
369 369
 					QST_system varchar(25) NOT NULL DEFAULT "",
@@ -377,20 +377,20 @@  discard block
 block discarded – undo
377 377
 					QST_deleted tinyint(2) unsigned NOT NULL DEFAULT 0,
378 378
 					PRIMARY KEY  (QST_ID),
379 379
 					KEY QST_order (QST_order)';
380
-        $this->_table_is_changed_in_this_version($table_name, $sql);
380
+		$this->_table_is_changed_in_this_version($table_name, $sql);
381 381
 
382
-        $table_name = 'esp_question_group_question';
383
-        $sql        = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
382
+		$table_name = 'esp_question_group_question';
383
+		$sql        = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
384 384
 					QSG_ID int(10) unsigned NOT NULL,
385 385
 					QST_ID int(10) unsigned NOT NULL,
386 386
 					QGQ_order int(10) unsigned NOT NULL DEFAULT 0,
387 387
 					PRIMARY KEY  (QGQ_ID),
388 388
 					KEY QST_ID (QST_ID),
389 389
 					KEY QSG_ID_order (QSG_ID,QGQ_order)";
390
-        $this->_table_is_changed_in_this_version($table_name, $sql);
390
+		$this->_table_is_changed_in_this_version($table_name, $sql);
391 391
 
392
-        $table_name = 'esp_question_option';
393
-        $sql        = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
392
+		$table_name = 'esp_question_option';
393
+		$sql        = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
394 394
 					QSO_value varchar(255) NOT NULL,
395 395
 					QSO_desc text NOT NULL,
396 396
 					QST_ID int(10) unsigned NOT NULL,
@@ -400,10 +400,10 @@  discard block
 block discarded – undo
400 400
 					PRIMARY KEY  (QSO_ID),
401 401
 					KEY QST_ID (QST_ID),
402 402
 					KEY QSO_order (QSO_order)";
403
-        $this->_table_is_changed_in_this_version($table_name, $sql);
403
+		$this->_table_is_changed_in_this_version($table_name, $sql);
404 404
 
405
-        $table_name = 'esp_registration';
406
-        $sql        = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
405
+		$table_name = 'esp_registration';
406
+		$sql        = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
407 407
 					  EVT_ID bigint(20) unsigned NOT NULL,
408 408
 					  ATT_ID bigint(20) unsigned NOT NULL,
409 409
 					  TXN_ID int(10) unsigned NOT NULL,
@@ -427,20 +427,20 @@  discard block
 block discarded – undo
427 427
 					  KEY TKT_ID (TKT_ID),
428 428
 					  KEY EVT_ID (EVT_ID),
429 429
 					  KEY STS_ID (STS_ID)";
430
-        $this->_table_is_changed_in_this_version($table_name, $sql);
430
+		$this->_table_is_changed_in_this_version($table_name, $sql);
431 431
 
432
-        $table_name = 'esp_registration_payment';
433
-        $sql        = "RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
432
+		$table_name = 'esp_registration_payment';
433
+		$sql        = "RPY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
434 434
 					  REG_ID int(10) unsigned NOT NULL,
435 435
 					  PAY_ID int(10) unsigned NULL,
436 436
 					  RPY_amount decimal(10,3) NOT NULL DEFAULT '0.00',
437 437
 					  PRIMARY KEY  (RPY_ID),
438 438
 					  KEY REG_ID (REG_ID),
439 439
 					  KEY PAY_ID (PAY_ID)";
440
-        $this->_table_is_changed_in_this_version($table_name, $sql);
440
+		$this->_table_is_changed_in_this_version($table_name, $sql);
441 441
 
442
-        $table_name = 'esp_checkin';
443
-        $sql        = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
442
+		$table_name = 'esp_checkin';
443
+		$sql        = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
444 444
 					REG_ID int(10) unsigned NOT NULL,
445 445
 					DTT_ID int(10) unsigned NOT NULL,
446 446
 					CHK_in tinyint(1) unsigned NOT NULL DEFAULT 1,
@@ -448,10 +448,10 @@  discard block
 block discarded – undo
448 448
 					PRIMARY KEY  (CHK_ID),
449 449
 					KEY REG_ID (REG_ID),
450 450
 					KEY DTT_ID (DTT_ID)";
451
-        $this->_table_is_changed_in_this_version($table_name, $sql);
451
+		$this->_table_is_changed_in_this_version($table_name, $sql);
452 452
 
453
-        $table_name = 'esp_state';
454
-        $sql        = "STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT,
453
+		$table_name = 'esp_state';
454
+		$sql        = "STA_ID smallint(5) unsigned NOT NULL AUTO_INCREMENT,
455 455
 					  CNT_ISO varchar(2) collate utf8_bin NOT NULL,
456 456
 					  STA_abbrev varchar(24) collate utf8_bin NOT NULL,
457 457
 					  STA_name varchar(100) collate utf8_bin NOT NULL,
@@ -459,10 +459,10 @@  discard block
 block discarded – undo
459 459
 					  PRIMARY KEY  (STA_ID),
460 460
 					  KEY STA_abbrev (STA_abbrev),
461 461
 					  KEY CNT_ISO (CNT_ISO)";
462
-        $this->_table_is_changed_in_this_version($table_name, $sql);
462
+		$this->_table_is_changed_in_this_version($table_name, $sql);
463 463
 
464
-        $table_name = 'esp_status';
465
-        $sql        = "STS_ID varchar(3) NOT NULL,
464
+		$table_name = 'esp_status';
465
+		$sql        = "STS_ID varchar(3) NOT NULL,
466 466
 					  STS_code varchar(45) NOT NULL,
467 467
 					  STS_type varchar(45) NOT NULL,
468 468
 					  STS_can_edit tinyint(1) NOT NULL DEFAULT 0,
@@ -470,10 +470,10 @@  discard block
 block discarded – undo
470 470
 					  STS_open tinyint(1) NOT NULL DEFAULT 1,
471 471
 					  UNIQUE KEY STS_ID_UNIQUE (STS_ID),
472 472
 					  KEY STS_type (STS_type)";
473
-        $this->_table_is_changed_in_this_version($table_name, $sql);
473
+		$this->_table_is_changed_in_this_version($table_name, $sql);
474 474
 
475
-        $table_name = 'esp_transaction';
476
-        $sql        = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
475
+		$table_name = 'esp_transaction';
476
+		$sql        = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
477 477
 					  TXN_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
478 478
 					  TXN_total decimal(10,3) DEFAULT '0.00',
479 479
 					  TXN_paid decimal(10,3) NOT NULL DEFAULT '0.00',
@@ -485,10 +485,10 @@  discard block
 block discarded – undo
485 485
 					  PRIMARY KEY  (TXN_ID),
486 486
 					  KEY TXN_timestamp (TXN_timestamp),
487 487
 					  KEY STS_ID (STS_ID)";
488
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
488
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
489 489
 
490
-        $table_name = 'esp_venue_meta';
491
-        $sql        = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
490
+		$table_name = 'esp_venue_meta';
491
+		$sql        = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
492 492
 			VNU_ID bigint(20) unsigned NOT NULL DEFAULT 0,
493 493
 			VNU_address varchar(255) DEFAULT NULL,
494 494
 			VNU_address2 varchar(255) DEFAULT NULL,
@@ -507,10 +507,10 @@  discard block
 block discarded – undo
507 507
 			KEY VNU_ID (VNU_ID),
508 508
 			KEY STA_ID (STA_ID),
509 509
 			KEY CNT_ISO (CNT_ISO)";
510
-        $this->_table_is_changed_in_this_version($table_name, $sql);
511
-        // modified tables
512
-        $table_name = "esp_price";
513
-        $sql        = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
510
+		$this->_table_is_changed_in_this_version($table_name, $sql);
511
+		// modified tables
512
+		$table_name = "esp_price";
513
+		$sql        = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
514 514
 					  PRT_ID tinyint(3) unsigned NOT NULL,
515 515
 					  PRC_amount decimal(10,3) NOT NULL DEFAULT '0.00',
516 516
 					  PRC_name varchar(245) NOT NULL,
@@ -523,10 +523,10 @@  discard block
 block discarded – undo
523 523
 					  PRC_parent int(10) unsigned DEFAULT 0,
524 524
 					  PRIMARY KEY  (PRC_ID),
525 525
 					  KEY PRT_ID (PRT_ID)";
526
-        $this->_table_is_changed_in_this_version($table_name, $sql);
526
+		$this->_table_is_changed_in_this_version($table_name, $sql);
527 527
 
528
-        $table_name = "esp_price_type";
529
-        $sql        = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
528
+		$table_name = "esp_price_type";
529
+		$sql        = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
530 530
 				  PRT_name varchar(45) NOT NULL,
531 531
 				  PBT_ID tinyint(3) unsigned NOT NULL DEFAULT '1',
532 532
 				  PRT_is_percent tinyint(1) NOT NULL DEFAULT '0',
@@ -535,10 +535,10 @@  discard block
 block discarded – undo
535 535
 				  PRT_deleted tinyint(1) NOT NULL DEFAULT '0',
536 536
 				  UNIQUE KEY PRT_name_UNIQUE (PRT_name),
537 537
 				  PRIMARY KEY  (PRT_ID)";
538
-        $this->_table_has_not_changed_since_previous($table_name, $sql);
538
+		$this->_table_has_not_changed_since_previous($table_name, $sql);
539 539
 
540
-        $table_name = "esp_ticket";
541
-        $sql        = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
540
+		$table_name = "esp_ticket";
541
+		$sql        = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
542 542
 					  TTM_ID int(10) unsigned NOT NULL,
543 543
 					  TKT_name varchar(245) NOT NULL DEFAULT '',
544 544
 					  TKT_description text NOT NULL,
@@ -560,10 +560,10 @@  discard block
 block discarded – undo
560 560
 					  TKT_deleted tinyint(1) NOT NULL DEFAULT '0',
561 561
 					  PRIMARY KEY  (TKT_ID),
562 562
 					  KEY TKT_start_date (TKT_start_date)";
563
-        $this->_table_is_changed_in_this_version($table_name, $sql);
563
+		$this->_table_is_changed_in_this_version($table_name, $sql);
564 564
 
565
-        $table_name = 'esp_question_group';
566
-        $sql        = 'QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
565
+		$table_name = 'esp_question_group';
566
+		$sql        = '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,225 +576,225 @@  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
-        $this->_table_is_changed_in_this_version($table_name, $sql);
580
-        /** @var EE_DMS_Core_4_1_0 $script_4_1_defaults */
581
-        $script_4_1_defaults = EE_Registry::instance()->load_dms('Core_4_1_0');
582
-        // (because many need to convert old string states to foreign keys into the states table)
583
-        $script_4_1_defaults->insert_default_states();
584
-        $script_4_1_defaults->insert_default_countries();
585
-        /** @var EE_DMS_Core_4_5_0 $script_4_5_defaults */
586
-        $script_4_5_defaults = EE_Registry::instance()->load_dms('Core_4_5_0');
587
-        $script_4_5_defaults->insert_default_price_types();
588
-        $script_4_5_defaults->insert_default_prices();
589
-        $script_4_5_defaults->insert_default_tickets();
590
-        /** @var EE_DMS_Core_4_6_0 $script_4_6_defaults */
591
-        $script_4_6_defaults = EE_Registry::instance()->load_dms('Core_4_6_0');
592
-        $script_4_6_defaults->add_default_admin_only_payments();
593
-        $script_4_6_defaults->insert_default_currencies();
594
-        $this->verify_new_countries();
595
-        $this->verify_new_currencies();
596
-        return true;
597
-    }
598
-
599
-
600
-    /**
601
-     * @return boolean
602
-     * @throws EE_Error
603
-     * @throws ReflectionException
604
-     */
605
-    public function schema_changes_after_migration()
606
-    {
607
-        $this->fix_non_default_taxes();
608
-        // this is actually the same as the last DMS
609
-        /** @var EE_DMS_Core_4_7_0 $script_4_7_defaults */
610
-        $script_4_7_defaults = EE_Registry::instance()->load_dms('Core_4_7_0');
611
-        return $script_4_7_defaults->schema_changes_after_migration();
612
-    }
613
-
614
-
615
-    public function migration_page_hooks()
616
-    {
617
-    }
618
-
619
-
620
-    /**
621
-     * verifies each of the new countries exists that somehow we missed in 4.1
622
-     *
623
-     * @throws EE_Error
624
-     */
625
-    public function verify_new_countries()
626
-    {
627
-        // a list of countries (and specifically some which were missed in another list):https://gist.github.com/adhipg/1600028
628
-        // how many decimal places? https://en.wikipedia.org/wiki/ISO_4217
629
-        // currency symbols: http://www.xe.com/symbols.php
630
-        // CNT_ISO, CNT_ISO3, RGN_ID, CNT_name, CNT_cur_code, CNT_cur_single, CNT_cur_plural, CNT_cur_sign, CNT_cur_sign_b4, CNT_cur_dec_plc, CNT_tel_code, CNT_is_EU, CNT_active
631
-        // ('AD', 'AND', 0, 'Andorra', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+376', 0, 0),
632
-        $newer_countries = [
633
-            ['AX', 'ALA', 0, 'Åland Islands', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+358', 1, 0],
634
-            ['BL', 'BLM', 0, 'Saint Barthelemy', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+590', 1, 0],
635
-            ['CW', 'CUW', 0, 'Curacao', 'ANG', 'Guilder', 'Guilders', 'ƒ', 1, 2, '+599', 1, 0],
636
-            ['GG', 'GGY', 0, 'Guernsey', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+44', 0, 0],
637
-            ['IM', 'IMN', 0, 'Isle of Man', 'GBP', 'Pound', 'Pounds', '£', 1, 2, '+44', 0, 0],
638
-            ['JE', 'JEY', 0, 'Jersey', 'GBP', 'Pound', 'Pounds', '£', 1, 2, '+44', 0, 0],
639
-            ['MF', 'MAF', 0, 'Saint Martin', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+590', 1, 0],
640
-            ['ME', 'MNE', 0, 'Montenegro', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+382', 0, 0],
641
-            ['RS', 'SRB', 0, 'Serbia', 'RSD', 'Dinar', 'Dinars', '', 0, 2, '+381', 1, 0],
642
-            ['SS', 'SSD', 0, 'South Sudan', 'SSP', 'Pound', 'Pounds', '£', 1, 2, '+211', 0, 0],
643
-            ['SX', 'SXM', 0, 'Sint Maarten', 'ANG', 'Guilder', 'Guilders', 'ƒ', 1, 2, '+1', 1, 0],
644
-            ['XK', 'XKX', 0, 'Kosovo', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+383', 0, 0],
645
-            ['YT', 'MYT', 0, 'Mayotte', 'EUR', 'Euro', 'Euros', '€', 0, 2, '+262', 1, 0],
646
-            [
647
-                'BQ',
648
-                'BES',
649
-                0,
650
-                'Bonaire, Saint Eustatius and Saba',
651
-                'USD',
652
-                'Dollar',
653
-                'Dollars',
654
-                '$',
655
-                1,
656
-                2,
657
-                '+599',
658
-                0,
659
-                0,
660
-            ],
661
-            ['BV', 'BVT', 0, 'Bouvet Island', 'NOK', 'Krone', 'Krones', 'kr', 1, 2, '+47', 0, 0],
662
-            ['IO', 'IOT', 0, 'British Indian Ocean Territory', 'GBP', 'Pound', 'Pounds', '£', 1, 2, '+246', 0, 0],
663
-            ['CX', 'CXR', 0, 'Christmas Island', 'AUD', 'Dollar', 'Dollars', '$', 1, 2, '+61', 0, 0],
664
-            ['CC', 'CCK', 0, 'Cocos (Keeling) Islands', 'AUD', 'Dollar', 'Dollars', '$', 1, 2, '+891', 0, 0],
665
-            [
666
-                'HM',
667
-                'HMD',
668
-                0,
669
-                'Heard Island and McDonald Islands',
670
-                'AUD',
671
-                'Dollar',
672
-                'Dollars',
673
-                '$',
674
-                1,
675
-                2,
676
-                '+891',
677
-                0,
678
-                0,
679
-            ],
680
-            ['PS', 'PSE', 0, 'Palestinian Territory', 'ILS', 'Shekel', 'Shekels', '₪', 1, 2, '+970', 0, 0],
681
-            [
682
-                'GS',
683
-                'SGS',
684
-                0,
685
-                'South Georgia and the South Sandwich Islands',
686
-                'GBP',
687
-                'Pound',
688
-                'Pounds',
689
-                '£',
690
-                1,
691
-                2,
692
-                '+500',
693
-                0,
694
-                0,
695
-            ],
696
-            ['TL', 'TLS', 0, 'Timor-Leste', 'USD', 'Dollar', 'Dollars', '$', 1, 2, '+670', 0, 0],
697
-            ['TF', 'ATF', 0, 'French Southern Territories', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+262', 0, 0],
698
-            [
699
-                'UM',
700
-                'UMI',
701
-                0,
702
-                'United States Minor Outlying Islands',
703
-                'USD',
704
-                'Dollar',
705
-                'Dollars',
706
-                '$',
707
-                1,
708
-                2,
709
-                '+1',
710
-                0,
711
-                0,
712
-            ],
713
-        ];
714
-        global $wpdb;
715
-        $country_table  = $wpdb->prefix . "esp_country";
716
-        $country_format = [
717
-            "CNT_ISO"         => '%s',
718
-            "CNT_ISO3"        => '%s',
719
-            "RGN_ID"          => '%d',
720
-            "CNT_name"        => '%s',
721
-            "CNT_cur_code"    => '%s',
722
-            "CNT_cur_single"  => '%s',
723
-            "CNT_cur_plural"  => '%s',
724
-            "CNT_cur_sign"    => '%s',
725
-            "CNT_cur_sign_b4" => '%d',
726
-            "CNT_cur_dec_plc" => '%d',
727
-            "CNT_tel_code"    => '%s',
728
-            "CNT_is_EU"       => '%d',
729
-            "CNT_active"      => '%d',
730
-        ];
731
-        if ($this->_get_table_analysis()->tableExists($country_table)) {
732
-            foreach ($newer_countries as $country) {
733
-                $SQL       = "SELECT COUNT('CNT_ISO') FROM $country_table WHERE CNT_ISO='$country[0]' LIMIT 1";
734
-                $countries = $wpdb->get_var($SQL);
735
-                if (! $countries) {
736
-                    $wpdb->insert(
737
-                        $country_table,
738
-                        array_combine(array_keys($country_format), $country),
739
-                        $country_format
740
-                    );
741
-                }
742
-            }
743
-        }
744
-    }
745
-
746
-
747
-    /**
748
-     * verifies each of the new currencies exists that somehow we missed in 4.6
749
-     *
750
-     * @throws EE_Error
751
-     */
752
-    public function verify_new_currencies()
753
-    {
754
-        // a list of countries (and specifically some which were missed in another list):https://gist.github.com/adhipg/1600028
755
-        // how many decimal places? https://en.wikipedia.org/wiki/ISO_4217
756
-        // currency symbols: http://www.xe.com/symbols.php
757
-        // CUR_code, CUR_single, CUR_plural, CUR_sign, CUR_dec_plc, CUR_active
758
-        // ( 'EUR',  'Euro',  'Euros',  '€',  2,1),
759
-        $newer_currencies = [
760
-            ['RSD', 'Dinar', 'Dinars', '', 3, 1],
761
-        ];
762
-        global $wpdb;
763
-        $currency_table  = $wpdb->prefix . "esp_currency";
764
-        $currency_format = [
765
-            "CUR_code"    => '%s',
766
-            "CUR_single"  => '%s',
767
-            "CUR_plural"  => '%s',
768
-            "CUR_sign"    => '%s',
769
-            "CUR_dec_plc" => '%d',
770
-            "CUR_active"  => '%d',
771
-        ];
772
-        if ($this->_get_table_analysis()->tableExists($currency_table)) {
773
-            foreach ($newer_currencies as $currency) {
774
-                $SQL       = "SELECT COUNT('CUR_code') FROM $currency_table WHERE CUR_code='$currency[0]' LIMIT 1";
775
-                $countries = $wpdb->get_var($SQL);
776
-                if (! $countries) {
777
-                    $wpdb->insert(
778
-                        $currency_table,
779
-                        array_combine(array_keys($currency_format), $currency),
780
-                        $currency_format
781
-                    );
782
-                }
783
-            }
784
-        }
785
-    }
786
-
787
-
788
-    /**
789
-     * addresses https://events.codebasehq.com/projects/event-espresso/tickets/8731
790
-     * which should just be a temporary issue for folks who installed 4.8.0-4.8.5;
791
-     * we should be able to stop doing this in 4.9
792
-     */
793
-    public function fix_non_default_taxes()
794
-    {
795
-        global $wpdb;
796
-        $query = $wpdb->prepare(
797
-            "UPDATE
579
+		$this->_table_is_changed_in_this_version($table_name, $sql);
580
+		/** @var EE_DMS_Core_4_1_0 $script_4_1_defaults */
581
+		$script_4_1_defaults = EE_Registry::instance()->load_dms('Core_4_1_0');
582
+		// (because many need to convert old string states to foreign keys into the states table)
583
+		$script_4_1_defaults->insert_default_states();
584
+		$script_4_1_defaults->insert_default_countries();
585
+		/** @var EE_DMS_Core_4_5_0 $script_4_5_defaults */
586
+		$script_4_5_defaults = EE_Registry::instance()->load_dms('Core_4_5_0');
587
+		$script_4_5_defaults->insert_default_price_types();
588
+		$script_4_5_defaults->insert_default_prices();
589
+		$script_4_5_defaults->insert_default_tickets();
590
+		/** @var EE_DMS_Core_4_6_0 $script_4_6_defaults */
591
+		$script_4_6_defaults = EE_Registry::instance()->load_dms('Core_4_6_0');
592
+		$script_4_6_defaults->add_default_admin_only_payments();
593
+		$script_4_6_defaults->insert_default_currencies();
594
+		$this->verify_new_countries();
595
+		$this->verify_new_currencies();
596
+		return true;
597
+	}
598
+
599
+
600
+	/**
601
+	 * @return boolean
602
+	 * @throws EE_Error
603
+	 * @throws ReflectionException
604
+	 */
605
+	public function schema_changes_after_migration()
606
+	{
607
+		$this->fix_non_default_taxes();
608
+		// this is actually the same as the last DMS
609
+		/** @var EE_DMS_Core_4_7_0 $script_4_7_defaults */
610
+		$script_4_7_defaults = EE_Registry::instance()->load_dms('Core_4_7_0');
611
+		return $script_4_7_defaults->schema_changes_after_migration();
612
+	}
613
+
614
+
615
+	public function migration_page_hooks()
616
+	{
617
+	}
618
+
619
+
620
+	/**
621
+	 * verifies each of the new countries exists that somehow we missed in 4.1
622
+	 *
623
+	 * @throws EE_Error
624
+	 */
625
+	public function verify_new_countries()
626
+	{
627
+		// a list of countries (and specifically some which were missed in another list):https://gist.github.com/adhipg/1600028
628
+		// how many decimal places? https://en.wikipedia.org/wiki/ISO_4217
629
+		// currency symbols: http://www.xe.com/symbols.php
630
+		// CNT_ISO, CNT_ISO3, RGN_ID, CNT_name, CNT_cur_code, CNT_cur_single, CNT_cur_plural, CNT_cur_sign, CNT_cur_sign_b4, CNT_cur_dec_plc, CNT_tel_code, CNT_is_EU, CNT_active
631
+		// ('AD', 'AND', 0, 'Andorra', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+376', 0, 0),
632
+		$newer_countries = [
633
+			['AX', 'ALA', 0, 'Åland Islands', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+358', 1, 0],
634
+			['BL', 'BLM', 0, 'Saint Barthelemy', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+590', 1, 0],
635
+			['CW', 'CUW', 0, 'Curacao', 'ANG', 'Guilder', 'Guilders', 'ƒ', 1, 2, '+599', 1, 0],
636
+			['GG', 'GGY', 0, 'Guernsey', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+44', 0, 0],
637
+			['IM', 'IMN', 0, 'Isle of Man', 'GBP', 'Pound', 'Pounds', '£', 1, 2, '+44', 0, 0],
638
+			['JE', 'JEY', 0, 'Jersey', 'GBP', 'Pound', 'Pounds', '£', 1, 2, '+44', 0, 0],
639
+			['MF', 'MAF', 0, 'Saint Martin', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+590', 1, 0],
640
+			['ME', 'MNE', 0, 'Montenegro', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+382', 0, 0],
641
+			['RS', 'SRB', 0, 'Serbia', 'RSD', 'Dinar', 'Dinars', '', 0, 2, '+381', 1, 0],
642
+			['SS', 'SSD', 0, 'South Sudan', 'SSP', 'Pound', 'Pounds', '£', 1, 2, '+211', 0, 0],
643
+			['SX', 'SXM', 0, 'Sint Maarten', 'ANG', 'Guilder', 'Guilders', 'ƒ', 1, 2, '+1', 1, 0],
644
+			['XK', 'XKX', 0, 'Kosovo', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+383', 0, 0],
645
+			['YT', 'MYT', 0, 'Mayotte', 'EUR', 'Euro', 'Euros', '€', 0, 2, '+262', 1, 0],
646
+			[
647
+				'BQ',
648
+				'BES',
649
+				0,
650
+				'Bonaire, Saint Eustatius and Saba',
651
+				'USD',
652
+				'Dollar',
653
+				'Dollars',
654
+				'$',
655
+				1,
656
+				2,
657
+				'+599',
658
+				0,
659
+				0,
660
+			],
661
+			['BV', 'BVT', 0, 'Bouvet Island', 'NOK', 'Krone', 'Krones', 'kr', 1, 2, '+47', 0, 0],
662
+			['IO', 'IOT', 0, 'British Indian Ocean Territory', 'GBP', 'Pound', 'Pounds', '£', 1, 2, '+246', 0, 0],
663
+			['CX', 'CXR', 0, 'Christmas Island', 'AUD', 'Dollar', 'Dollars', '$', 1, 2, '+61', 0, 0],
664
+			['CC', 'CCK', 0, 'Cocos (Keeling) Islands', 'AUD', 'Dollar', 'Dollars', '$', 1, 2, '+891', 0, 0],
665
+			[
666
+				'HM',
667
+				'HMD',
668
+				0,
669
+				'Heard Island and McDonald Islands',
670
+				'AUD',
671
+				'Dollar',
672
+				'Dollars',
673
+				'$',
674
+				1,
675
+				2,
676
+				'+891',
677
+				0,
678
+				0,
679
+			],
680
+			['PS', 'PSE', 0, 'Palestinian Territory', 'ILS', 'Shekel', 'Shekels', '₪', 1, 2, '+970', 0, 0],
681
+			[
682
+				'GS',
683
+				'SGS',
684
+				0,
685
+				'South Georgia and the South Sandwich Islands',
686
+				'GBP',
687
+				'Pound',
688
+				'Pounds',
689
+				'£',
690
+				1,
691
+				2,
692
+				'+500',
693
+				0,
694
+				0,
695
+			],
696
+			['TL', 'TLS', 0, 'Timor-Leste', 'USD', 'Dollar', 'Dollars', '$', 1, 2, '+670', 0, 0],
697
+			['TF', 'ATF', 0, 'French Southern Territories', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+262', 0, 0],
698
+			[
699
+				'UM',
700
+				'UMI',
701
+				0,
702
+				'United States Minor Outlying Islands',
703
+				'USD',
704
+				'Dollar',
705
+				'Dollars',
706
+				'$',
707
+				1,
708
+				2,
709
+				'+1',
710
+				0,
711
+				0,
712
+			],
713
+		];
714
+		global $wpdb;
715
+		$country_table  = $wpdb->prefix . "esp_country";
716
+		$country_format = [
717
+			"CNT_ISO"         => '%s',
718
+			"CNT_ISO3"        => '%s',
719
+			"RGN_ID"          => '%d',
720
+			"CNT_name"        => '%s',
721
+			"CNT_cur_code"    => '%s',
722
+			"CNT_cur_single"  => '%s',
723
+			"CNT_cur_plural"  => '%s',
724
+			"CNT_cur_sign"    => '%s',
725
+			"CNT_cur_sign_b4" => '%d',
726
+			"CNT_cur_dec_plc" => '%d',
727
+			"CNT_tel_code"    => '%s',
728
+			"CNT_is_EU"       => '%d',
729
+			"CNT_active"      => '%d',
730
+		];
731
+		if ($this->_get_table_analysis()->tableExists($country_table)) {
732
+			foreach ($newer_countries as $country) {
733
+				$SQL       = "SELECT COUNT('CNT_ISO') FROM $country_table WHERE CNT_ISO='$country[0]' LIMIT 1";
734
+				$countries = $wpdb->get_var($SQL);
735
+				if (! $countries) {
736
+					$wpdb->insert(
737
+						$country_table,
738
+						array_combine(array_keys($country_format), $country),
739
+						$country_format
740
+					);
741
+				}
742
+			}
743
+		}
744
+	}
745
+
746
+
747
+	/**
748
+	 * verifies each of the new currencies exists that somehow we missed in 4.6
749
+	 *
750
+	 * @throws EE_Error
751
+	 */
752
+	public function verify_new_currencies()
753
+	{
754
+		// a list of countries (and specifically some which were missed in another list):https://gist.github.com/adhipg/1600028
755
+		// how many decimal places? https://en.wikipedia.org/wiki/ISO_4217
756
+		// currency symbols: http://www.xe.com/symbols.php
757
+		// CUR_code, CUR_single, CUR_plural, CUR_sign, CUR_dec_plc, CUR_active
758
+		// ( 'EUR',  'Euro',  'Euros',  '€',  2,1),
759
+		$newer_currencies = [
760
+			['RSD', 'Dinar', 'Dinars', '', 3, 1],
761
+		];
762
+		global $wpdb;
763
+		$currency_table  = $wpdb->prefix . "esp_currency";
764
+		$currency_format = [
765
+			"CUR_code"    => '%s',
766
+			"CUR_single"  => '%s',
767
+			"CUR_plural"  => '%s',
768
+			"CUR_sign"    => '%s',
769
+			"CUR_dec_plc" => '%d',
770
+			"CUR_active"  => '%d',
771
+		];
772
+		if ($this->_get_table_analysis()->tableExists($currency_table)) {
773
+			foreach ($newer_currencies as $currency) {
774
+				$SQL       = "SELECT COUNT('CUR_code') FROM $currency_table WHERE CUR_code='$currency[0]' LIMIT 1";
775
+				$countries = $wpdb->get_var($SQL);
776
+				if (! $countries) {
777
+					$wpdb->insert(
778
+						$currency_table,
779
+						array_combine(array_keys($currency_format), $currency),
780
+						$currency_format
781
+					);
782
+				}
783
+			}
784
+		}
785
+	}
786
+
787
+
788
+	/**
789
+	 * addresses https://events.codebasehq.com/projects/event-espresso/tickets/8731
790
+	 * which should just be a temporary issue for folks who installed 4.8.0-4.8.5;
791
+	 * we should be able to stop doing this in 4.9
792
+	 */
793
+	public function fix_non_default_taxes()
794
+	{
795
+		global $wpdb;
796
+		$query = $wpdb->prepare(
797
+			"UPDATE
798 798
 				{$wpdb->prefix}esp_price p INNER JOIN
799 799
 				{$wpdb->prefix}esp_price_type pt ON p.PRT_ID = pt.PRT_ID
800 800
 			SET
@@ -803,8 +803,8 @@  discard block
 block discarded – undo
803 803
 				p.PRC_is_default = 0 AND
804 804
 				pt.PBT_ID = %d
805 805
 					",
806
-            EEM_Price_Type::base_type_tax
807
-        );
808
-        $wpdb->query($query);
809
-    }
806
+			EEM_Price_Type::base_type_tax
807
+		);
808
+		$wpdb->query($query);
809
+	}
810 810
 }
Please login to merge, or discard this patch.
Spacing   +10 added lines, -10 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_8_0_stages/*');
10
+$stages            = glob(EE_CORE.'data_migration_scripts/4_8_0_stages/*');
11 11
 $class_to_filepath = [];
12 12
 foreach ($stages as $filepath) {
13 13
     $matches = [];
14 14
     preg_match('~4_8_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_8_0__autoloaded_stages', $class_to_filepath);
@@ -71,10 +71,10 @@  discard block
 block discarded – undo
71 71
         ) {
72 72
             //          echo "$version_string can be migrated from";
73 73
             return true;
74
-        } elseif (! $version_string) {
74
+        } elseif ( ! $version_string) {
75 75
             //          echo "no version string provided: $version_string";
76 76
             // no version string provided... this must be pre 4.3
77
-            return false;// changed mind. dont want people thinking they should migrate yet because they cant
77
+            return false; // changed mind. dont want people thinking they should migrate yet because they cant
78 78
         } else {
79 79
             //          echo "$version_string doesnt apply";
80 80
             return false;
@@ -89,9 +89,9 @@  discard block
 block discarded – undo
89 89
      */
90 90
     public function schema_changes_before_migration()
91 91
     {
92
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
92
+        require_once(EE_HELPERS.'EEH_Activation.helper.php');
93 93
         $now_in_mysql = current_time('mysql', true);
94
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
94
+        require_once(EE_HELPERS.'EEH_Activation.helper.php');
95 95
 
96 96
         $table_name = 'esp_answer';
97 97
         $sql        = " ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
@@ -712,7 +712,7 @@  discard block
 block discarded – undo
712 712
             ],
713 713
         ];
714 714
         global $wpdb;
715
-        $country_table  = $wpdb->prefix . "esp_country";
715
+        $country_table  = $wpdb->prefix."esp_country";
716 716
         $country_format = [
717 717
             "CNT_ISO"         => '%s',
718 718
             "CNT_ISO3"        => '%s',
@@ -732,7 +732,7 @@  discard block
 block discarded – undo
732 732
             foreach ($newer_countries as $country) {
733 733
                 $SQL       = "SELECT COUNT('CNT_ISO') FROM $country_table WHERE CNT_ISO='$country[0]' LIMIT 1";
734 734
                 $countries = $wpdb->get_var($SQL);
735
-                if (! $countries) {
735
+                if ( ! $countries) {
736 736
                     $wpdb->insert(
737 737
                         $country_table,
738 738
                         array_combine(array_keys($country_format), $country),
@@ -760,7 +760,7 @@  discard block
 block discarded – undo
760 760
             ['RSD', 'Dinar', 'Dinars', '', 3, 1],
761 761
         ];
762 762
         global $wpdb;
763
-        $currency_table  = $wpdb->prefix . "esp_currency";
763
+        $currency_table  = $wpdb->prefix."esp_currency";
764 764
         $currency_format = [
765 765
             "CUR_code"    => '%s',
766 766
             "CUR_single"  => '%s',
@@ -773,7 +773,7 @@  discard block
 block discarded – undo
773 773
             foreach ($newer_currencies as $currency) {
774 774
                 $SQL       = "SELECT COUNT('CUR_code') FROM $currency_table WHERE CUR_code='$currency[0]' LIMIT 1";
775 775
                 $countries = $wpdb->get_var($SQL);
776
-                if (! $countries) {
776
+                if ( ! $countries) {
777 777
                     $wpdb->insert(
778 778
                         $currency_table,
779 779
                         array_combine(array_keys($currency_format), $currency),
Please login to merge, or discard this patch.
core/data_migration_scripts/EE_DMS_Core_4_1_0.dms.php 2 patches
Indentation   +1256 added lines, -1256 removed lines patch added patch discarded remove patch
@@ -10,11 +10,11 @@  discard block
 block discarded – undo
10 10
 $stages            = glob(EE_CORE . 'data_migration_scripts/4_1_0_stages/*');
11 11
 $class_to_filepath = [];
12 12
 if (! empty($stages)) {
13
-    foreach ($stages as $filepath) {
14
-        $matches = [];
15
-        preg_match('~4_1_0_stages/(.*).dmsstage.php~', $filepath, $matches);
16
-        $class_to_filepath[ $matches[1] ] = $filepath;
17
-    }
13
+	foreach ($stages as $filepath) {
14
+		$matches = [];
15
+		preg_match('~4_1_0_stages/(.*).dmsstage.php~', $filepath, $matches);
16
+		$class_to_filepath[ $matches[1] ] = $filepath;
17
+	}
18 18
 }
19 19
 // give addons a chance to autoload their stages too
20 20
 $class_to_filepath = apply_filters('FHEE__EE_DMS_4_1_0__autoloaded_stages', $class_to_filepath);
@@ -39,92 +39,92 @@  discard block
 block discarded – undo
39 39
 class EE_DMS_Core_4_1_0 extends EE_Data_Migration_Script_Base
40 40
 {
41 41
 
42
-    /**
43
-     * EE_DMS_Core_4_1_0 constructor.
44
-     *
45
-     * @param TableManager|null  $table_manager
46
-     * @param TableAnalysis|null $table_analysis
47
-     */
48
-    public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
49
-    {
50
-        $this->_pretty_name      =
51
-            esc_html__("Data Migration from Event Espresso 3 to Event Espresso 4.1.0", "event_espresso");
52
-        $this->_priority         = 10;
53
-        $this->_migration_stages = [
54
-            new EE_DMS_4_1_0_org_options(),
55
-            new EE_DMS_4_1_0_shortcodes(),
56
-            new EE_DMS_4_1_0_gateways(),
57
-            new EE_DMS_4_1_0_events(),
58
-            new EE_DMS_4_1_0_prices(),
59
-            new EE_DMS_4_1_0_category_details(),
60
-            new EE_DMS_4_1_0_event_category(),
61
-            new EE_DMS_4_1_0_venues(),
62
-            new EE_DMS_4_1_0_event_venue(),
63
-            new EE_DMS_4_1_0_question_groups(),
64
-            new EE_DMS_4_1_0_questions(),
65
-            new EE_DMS_4_1_0_question_group_question(),
66
-            new EE_DMS_4_1_0_event_question_group(),
67
-            new EE_DMS_4_1_0_attendees(),
68
-            new EE_DMS_4_1_0_line_items(),
69
-            new EE_DMS_4_1_0_answers(),
70
-            new EE_DMS_4_1_0_checkins(),
71
-        ];
72
-        parent::__construct($table_manager, $table_analysis);
73
-    }
74
-
75
-
76
-    /**
77
-     * Checks if this 3.1 Check-in table exists. If it doesn't we can't migrate Check-ins
78
-     *
79
-     * @return boolean
80
-     * @global wpdb $wpdb
81
-     */
82
-    private function _checkin_table_exists(): bool
83
-    {
84
-        global $wpdb;
85
-        return (bool) $wpdb->get_results(
86
-            "SHOW TABLES LIKE '" . $wpdb->prefix . "events_attendee_checkin" . "'"
87
-        );
88
-    }
89
-
90
-
91
-    public function can_migrate_from_version($version_array)
92
-    {
93
-        $version_string = $version_array['Core'];
94
-        if (version_compare($version_string, '4.0.0.decaf', '<') && version_compare($version_string, '3.1.26', '>=')) {
95
-            //          echo "$version_string can be migrated fro";
96
-            return true;
97
-        } elseif (! $version_string) {
98
-            //          echo "no version string provided: $version_string";
99
-            // no version string provided... this must be pre 4.1
100
-            // because since 4.1 we're
101
-            return false;// changed mind. dont want people thinking they should migrate yet because they cant
102
-        } else {
103
-            //          echo "$version_string doesnt apply";
104
-            return false;
105
-        }
106
-    }
107
-
108
-
109
-    /**
110
-     * @throws EE_Error
111
-     * @throws ReflectionException
112
-     */
113
-    public function schema_changes_before_migration()
114
-    {
115
-        // relies on 4.1's EEH_Activation::create_table
116
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
117
-
118
-        $table_name = 'esp_answer';
119
-        $sql        = "ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
42
+	/**
43
+	 * EE_DMS_Core_4_1_0 constructor.
44
+	 *
45
+	 * @param TableManager|null  $table_manager
46
+	 * @param TableAnalysis|null $table_analysis
47
+	 */
48
+	public function __construct(TableManager $table_manager = null, TableAnalysis $table_analysis = null)
49
+	{
50
+		$this->_pretty_name      =
51
+			esc_html__("Data Migration from Event Espresso 3 to Event Espresso 4.1.0", "event_espresso");
52
+		$this->_priority         = 10;
53
+		$this->_migration_stages = [
54
+			new EE_DMS_4_1_0_org_options(),
55
+			new EE_DMS_4_1_0_shortcodes(),
56
+			new EE_DMS_4_1_0_gateways(),
57
+			new EE_DMS_4_1_0_events(),
58
+			new EE_DMS_4_1_0_prices(),
59
+			new EE_DMS_4_1_0_category_details(),
60
+			new EE_DMS_4_1_0_event_category(),
61
+			new EE_DMS_4_1_0_venues(),
62
+			new EE_DMS_4_1_0_event_venue(),
63
+			new EE_DMS_4_1_0_question_groups(),
64
+			new EE_DMS_4_1_0_questions(),
65
+			new EE_DMS_4_1_0_question_group_question(),
66
+			new EE_DMS_4_1_0_event_question_group(),
67
+			new EE_DMS_4_1_0_attendees(),
68
+			new EE_DMS_4_1_0_line_items(),
69
+			new EE_DMS_4_1_0_answers(),
70
+			new EE_DMS_4_1_0_checkins(),
71
+		];
72
+		parent::__construct($table_manager, $table_analysis);
73
+	}
74
+
75
+
76
+	/**
77
+	 * Checks if this 3.1 Check-in table exists. If it doesn't we can't migrate Check-ins
78
+	 *
79
+	 * @return boolean
80
+	 * @global wpdb $wpdb
81
+	 */
82
+	private function _checkin_table_exists(): bool
83
+	{
84
+		global $wpdb;
85
+		return (bool) $wpdb->get_results(
86
+			"SHOW TABLES LIKE '" . $wpdb->prefix . "events_attendee_checkin" . "'"
87
+		);
88
+	}
89
+
90
+
91
+	public function can_migrate_from_version($version_array)
92
+	{
93
+		$version_string = $version_array['Core'];
94
+		if (version_compare($version_string, '4.0.0.decaf', '<') && version_compare($version_string, '3.1.26', '>=')) {
95
+			//          echo "$version_string can be migrated fro";
96
+			return true;
97
+		} elseif (! $version_string) {
98
+			//          echo "no version string provided: $version_string";
99
+			// no version string provided... this must be pre 4.1
100
+			// because since 4.1 we're
101
+			return false;// changed mind. dont want people thinking they should migrate yet because they cant
102
+		} else {
103
+			//          echo "$version_string doesnt apply";
104
+			return false;
105
+		}
106
+	}
107
+
108
+
109
+	/**
110
+	 * @throws EE_Error
111
+	 * @throws ReflectionException
112
+	 */
113
+	public function schema_changes_before_migration()
114
+	{
115
+		// relies on 4.1's EEH_Activation::create_table
116
+		require_once(EE_HELPERS . 'EEH_Activation.helper.php');
117
+
118
+		$table_name = 'esp_answer';
119
+		$sql        = "ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
120 120
 					REG_ID int(10) unsigned NOT NULL,
121 121
 					QST_ID int(10) unsigned NOT NULL,
122 122
 					ANS_value text NOT NULL,
123 123
 					PRIMARY KEY  (ANS_ID)";
124
-        $this->_table_is_new_in_this_version($table_name, $sql);
124
+		$this->_table_is_new_in_this_version($table_name, $sql);
125 125
 
126
-        $table_name = 'esp_attendee_meta';
127
-        $sql        = "ATTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
126
+		$table_name = 'esp_attendee_meta';
127
+		$sql        = "ATTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
128 128
 						ATT_ID bigint(20) unsigned NOT NULL,
129 129
 						ATT_fname varchar(45) NOT NULL,
130 130
 						ATT_lname varchar(45) NOT NULL,
@@ -140,10 +140,10 @@  discard block
 block discarded – undo
140 140
 								KEY ATT_fname (ATT_fname),
141 141
 								KEY ATT_lname (ATT_lname),
142 142
 								KEY ATT_email (ATT_email(191))";
143
-        $this->_table_is_new_in_this_version($table_name, $sql);
143
+		$this->_table_is_new_in_this_version($table_name, $sql);
144 144
 
145
-        $table_name = 'esp_country';
146
-        $sql        = "CNT_ISO varchar(2) COLLATE utf8_bin NOT NULL,
145
+		$table_name = 'esp_country';
146
+		$sql        = "CNT_ISO varchar(2) COLLATE utf8_bin NOT NULL,
147 147
 					  CNT_ISO3 varchar(3) COLLATE utf8_bin NOT NULL,
148 148
 					  RGN_ID tinyint(3) unsigned DEFAULT NULL,
149 149
 					  CNT_name varchar(45) COLLATE utf8_bin NOT NULL,
@@ -159,10 +159,10 @@  discard block
 block discarded – undo
159 159
 					  CNT_is_EU tinyint(1) DEFAULT '0',
160 160
 					  CNT_active tinyint(1) DEFAULT '0',
161 161
 					  PRIMARY KEY  (CNT_ISO)";
162
-        $this->_table_is_new_in_this_version($table_name, $sql);
162
+		$this->_table_is_new_in_this_version($table_name, $sql);
163 163
 
164
-        $table_name = 'esp_datetime';
165
-        $sql        = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
164
+		$table_name = 'esp_datetime';
165
+		$sql        = "DTT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
166 166
 				  EVT_ID bigint(20) unsigned NOT NULL,
167 167
 				  DTT_EVT_start datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
168 168
 				  DTT_EVT_end datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
@@ -175,10 +175,10 @@  discard block
 block discarded – undo
175 175
 						PRIMARY KEY  (DTT_ID),
176 176
 						KEY EVT_ID (EVT_ID),
177 177
 						KEY DTT_is_primary (DTT_is_primary)";
178
-        $this->_table_is_new_in_this_version($table_name, $sql);
178
+		$this->_table_is_new_in_this_version($table_name, $sql);
179 179
 
180
-        $table_name = 'esp_event_meta';
181
-        $sql        = "
180
+		$table_name = 'esp_event_meta';
181
+		$sql        = "
182 182
 			EVTM_ID int(10) NOT NULL AUTO_INCREMENT,
183 183
 			EVT_ID bigint(20) unsigned NOT NULL,
184 184
 			EVT_display_desc tinyint(1) unsigned NOT NULL DEFAULT 1,
@@ -193,35 +193,35 @@  discard block
 block discarded – undo
193 193
 			EVT_external_URL varchar(200) NULL,
194 194
 			EVT_donations tinyint(1) NULL,
195 195
 			PRIMARY KEY  (EVTM_ID)";
196
-        $this->_table_is_new_in_this_version($table_name, $sql);
196
+		$this->_table_is_new_in_this_version($table_name, $sql);
197 197
 
198
-        $table_name = 'esp_event_question_group';
199
-        $sql        = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
198
+		$table_name = 'esp_event_question_group';
199
+		$sql        = "EQG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
200 200
 					EVT_ID bigint(20) unsigned NOT NULL,
201 201
 					QSG_ID int(10) unsigned NOT NULL,
202 202
 					EQG_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
203 203
 					PRIMARY KEY  (EQG_ID)";
204
-        $this->_table_is_new_in_this_version($table_name, $sql);
204
+		$this->_table_is_new_in_this_version($table_name, $sql);
205 205
 
206
-        $table_name = 'esp_event_venue';
207
-        $sql        = "EVV_ID int(11) NOT NULL AUTO_INCREMENT,
206
+		$table_name = 'esp_event_venue';
207
+		$sql        = "EVV_ID int(11) NOT NULL AUTO_INCREMENT,
208 208
 				EVT_ID bigint(20) unsigned NOT NULL,
209 209
 				VNU_ID bigint(20) unsigned NOT NULL,
210 210
 				EVV_primary tinyint(1) unsigned NOT NULL DEFAULT 0,
211 211
 				PRIMARY KEY  (EVV_ID)";
212
-        $this->_table_is_new_in_this_version($table_name, $sql);
212
+		$this->_table_is_new_in_this_version($table_name, $sql);
213 213
 
214
-        $table_name = 'esp_extra_meta';
215
-        $sql        = "EXM_ID int(11) NOT NULL AUTO_INCREMENT,
214
+		$table_name = 'esp_extra_meta';
215
+		$sql        = "EXM_ID int(11) NOT NULL AUTO_INCREMENT,
216 216
 				OBJ_ID int(11) DEFAULT NULL,
217 217
 				EXM_type varchar(45) DEFAULT NULL,
218 218
 				EXM_key varchar(45) DEFAULT NULL,
219 219
 				EXM_value text,
220 220
 				PRIMARY KEY  (EXM_ID)";
221
-        $this->_table_is_new_in_this_version($table_name, $sql);
221
+		$this->_table_is_new_in_this_version($table_name, $sql);
222 222
 
223
-        $table_name = 'esp_line_item';
224
-        $sql        = "LIN_ID int(11) NOT NULL AUTO_INCREMENT,
223
+		$table_name = 'esp_line_item';
224
+		$sql        = "LIN_ID int(11) NOT NULL AUTO_INCREMENT,
225 225
 				LIN_code varchar(245) NOT NULL DEFAULT '',
226 226
 				TXN_ID int(11) DEFAULT NULL,
227 227
 				LIN_name varchar(245) NOT NULL DEFAULT '',
@@ -237,20 +237,20 @@  discard block
 block discarded – undo
237 237
 				OBJ_ID int(11) DEFAULT NULL,
238 238
 				OBJ_type varchar(45)DEFAULT NULL,
239 239
 				PRIMARY KEY  (LIN_ID)";
240
-        $this->_table_is_new_in_this_version($table_name, $sql);
240
+		$this->_table_is_new_in_this_version($table_name, $sql);
241 241
 
242
-        $table_name = 'esp_message_template';
243
-        $sql        = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
242
+		$table_name = 'esp_message_template';
243
+		$sql        = "MTP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
244 244
 					GRP_ID int(10) unsigned NOT NULL,
245 245
 					MTP_context varchar(50) NOT NULL,
246 246
 					MTP_template_field varchar(30) NOT NULL,
247 247
 					MTP_content text NOT NULL,
248 248
 					PRIMARY KEY  (MTP_ID),
249 249
 					KEY GRP_ID (GRP_ID)";
250
-        $this->_table_is_new_in_this_version($table_name, $sql);
250
+		$this->_table_is_new_in_this_version($table_name, $sql);
251 251
 
252
-        $table_name = 'esp_message_template_group';
253
-        $sql        = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
252
+		$table_name = 'esp_message_template_group';
253
+		$sql        = "GRP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
254 254
 					EVT_ID bigint(20) unsigned DEFAULT NULL,
255 255
 					MTP_user_id int(10) NOT NULL DEFAULT '1',
256 256
 					MTP_messenger varchar(30) NOT NULL,
@@ -262,10 +262,10 @@  discard block
 block discarded – undo
262 262
 					PRIMARY KEY  (GRP_ID),
263 263
 					KEY EVT_ID (EVT_ID),
264 264
 					KEY MTP_user_id (MTP_user_id)";
265
-        $this->_table_is_new_in_this_version($table_name, $sql);
265
+		$this->_table_is_new_in_this_version($table_name, $sql);
266 266
 
267
-        $table_name = 'esp_payment';
268
-        $sql        = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
267
+		$table_name = 'esp_payment';
268
+		$sql        = "PAY_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
269 269
 					TXN_ID int(10) unsigned DEFAULT NULL,
270 270
 					STS_ID varchar(3) COLLATE utf8_bin DEFAULT NULL,
271 271
 					PAY_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
@@ -281,10 +281,10 @@  discard block
 block discarded – undo
281 281
 					PRIMARY KEY  (PAY_ID),
282 282
 					KEY TXN_ID (TXN_ID),
283 283
 					KEY PAY_timestamp (PAY_timestamp)";
284
-        $this->_table_is_new_in_this_version($table_name, $sql);
284
+		$this->_table_is_new_in_this_version($table_name, $sql);
285 285
 
286
-        $table_name = "esp_ticket";
287
-        $sql        = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
286
+		$table_name = "esp_ticket";
287
+		$sql        = "TKT_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
288 288
 					  TTM_ID int(10) unsigned NOT NULL,
289 289
 					  TKT_name varchar(245) NOT NULL DEFAULT '',
290 290
 					  TKT_description text NOT NULL,
@@ -303,32 +303,32 @@  discard block
 block discarded – undo
303 303
 					  TKT_parent int(10) unsigned DEFAULT '0',
304 304
 					  TKT_deleted tinyint(1) NOT NULL DEFAULT '0',
305 305
 					  PRIMARY KEY  (TKT_ID)";
306
-        $this->_table_is_new_in_this_version($table_name, $sql);
306
+		$this->_table_is_new_in_this_version($table_name, $sql);
307 307
 
308
-        $table_name = "esp_ticket_price";
309
-        $sql        = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
308
+		$table_name = "esp_ticket_price";
309
+		$sql        = "TKP_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
310 310
 					  TKT_ID int(10) unsigned NOT NULL,
311 311
 					  PRC_ID int(10) unsigned NOT NULL,
312 312
 					  PRIMARY KEY  (TKP_ID)";
313
-        $this->_table_is_new_in_this_version($table_name, $sql);
313
+		$this->_table_is_new_in_this_version($table_name, $sql);
314 314
 
315
-        $table_name = "esp_datetime_ticket";
316
-        $sql        = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
315
+		$table_name = "esp_datetime_ticket";
316
+		$sql        = "DTK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
317 317
 					  DTT_ID int(10) unsigned NOT NULL,
318 318
 					  TKT_ID int(10) unsigned NOT NULL,
319 319
 					  PRIMARY KEY  (DTK_ID)";
320
-        $this->_table_is_new_in_this_version($table_name, $sql);
320
+		$this->_table_is_new_in_this_version($table_name, $sql);
321 321
 
322
-        $table_name = "esp_ticket_template";
323
-        $sql        = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
322
+		$table_name = "esp_ticket_template";
323
+		$sql        = "TTM_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
324 324
 					  TTM_name varchar(45) NOT NULL,
325 325
 					  TTM_description text,
326 326
 					  TTM_file varchar(45),
327 327
 					  PRIMARY KEY  (TTM_ID)";
328
-        $this->_table_is_new_in_this_version($table_name, $sql);
328
+		$this->_table_is_new_in_this_version($table_name, $sql);
329 329
 
330
-        $table_name = "esp_price";
331
-        $sql        = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
330
+		$table_name = "esp_price";
331
+		$sql        = "PRC_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
332 332
 					  PRT_ID tinyint(3) unsigned NOT NULL,
333 333
 					  PRC_amount decimal(10,3) NOT NULL DEFAULT '0.00',
334 334
 					  PRC_name varchar(245) NOT NULL,
@@ -339,10 +339,10 @@  discard block
 block discarded – undo
339 339
 					  PRC_order tinyint(3) unsigned NOT NULL DEFAULT '0',
340 340
 					  PRC_parent int(10) unsigned DEFAULT 0,
341 341
 					  PRIMARY KEY  (PRC_ID)";
342
-        $this->_table_is_new_in_this_version($table_name, $sql);
342
+		$this->_table_is_new_in_this_version($table_name, $sql);
343 343
 
344
-        $table_name = "esp_price_type";
345
-        $sql        = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
344
+		$table_name = "esp_price_type";
345
+		$sql        = "PRT_ID tinyint(3) unsigned NOT NULL AUTO_INCREMENT,
346 346
 				  PRT_name varchar(45) NOT NULL,
347 347
 				  PBT_ID tinyint(3) unsigned NOT NULL DEFAULT '1',
348 348
 				  PRT_is_percent tinyint(1) NOT NULL DEFAULT '0',
@@ -350,10 +350,10 @@  discard block
 block discarded – undo
350 350
 				  PRT_deleted tinyint(1) NOT NULL DEFAULT '0',
351 351
 				  UNIQUE KEY PRT_name_UNIQUE (PRT_name),
352 352
 				  PRIMARY KEY  (PRT_ID)";
353
-        $this->_table_is_new_in_this_version($table_name, $sql);
353
+		$this->_table_is_new_in_this_version($table_name, $sql);
354 354
 
355
-        $table_name = 'esp_question';
356
-        $sql        = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
355
+		$table_name = 'esp_question';
356
+		$sql        = 'QST_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
357 357
 					QST_display_text text NOT NULL,
358 358
 					QST_admin_label varchar(255) NOT NULL,
359 359
 					QST_system varchar(25) DEFAULT NULL,
@@ -365,11 +365,11 @@  discard block
 block discarded – undo
365 365
 					QST_wp_user bigint(20) unsigned NULL,
366 366
 					QST_deleted tinyint(1) unsigned NOT NULL DEFAULT 0,
367 367
 					PRIMARY KEY  (QST_ID)';
368
-        $this->_table_is_new_in_this_version($table_name, $sql);
369
-        $this->_get_table_manager()->dropIndex('esp_question_group', 'QSG_identifier_UNIQUE');
368
+		$this->_table_is_new_in_this_version($table_name, $sql);
369
+		$this->_get_table_manager()->dropIndex('esp_question_group', 'QSG_identifier_UNIQUE');
370 370
 
371
-        $table_name = 'esp_question_group';
372
-        $sql        = 'QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
371
+		$table_name = 'esp_question_group';
372
+		$sql        = 'QSG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
373 373
 					QSG_name varchar(255) NOT NULL,
374 374
 					QSG_identifier varchar(100) NOT NULL,
375 375
 					QSG_desc text NULL,
@@ -380,26 +380,26 @@  discard block
 block discarded – undo
380 380
 					QSG_deleted tinyint(1) unsigned NOT NULL DEFAULT 0,
381 381
 					PRIMARY KEY  (QSG_ID),
382 382
 					UNIQUE KEY QSG_identifier_UNIQUE (QSG_identifier ASC)';
383
-        $this->_table_is_new_in_this_version($table_name, $sql);
383
+		$this->_table_is_new_in_this_version($table_name, $sql);
384 384
 
385
-        $table_name = 'esp_question_group_question';
386
-        $sql        = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
385
+		$table_name = 'esp_question_group_question';
386
+		$sql        = "QGQ_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
387 387
 					QSG_ID int(10) unsigned NOT NULL,
388 388
 					QST_ID int(10) unsigned NOT NULL,
389 389
 					PRIMARY KEY  (QGQ_ID) ";
390
-        $this->_table_is_new_in_this_version($table_name, $sql);
390
+		$this->_table_is_new_in_this_version($table_name, $sql);
391 391
 
392
-        $table_name = 'esp_question_option';
393
-        $sql        = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
392
+		$table_name = 'esp_question_option';
393
+		$sql        = "QSO_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
394 394
 					QSO_value varchar(255) NOT NULL,
395 395
 					QSO_desc text NOT NULL,
396 396
 					QST_ID int(10) unsigned NOT NULL,
397 397
 					QSO_deleted tinyint(1) unsigned NOT NULL DEFAULT 0,
398 398
 					PRIMARY KEY  (QSO_ID)";
399
-        $this->_table_is_new_in_this_version($table_name, $sql);
399
+		$this->_table_is_new_in_this_version($table_name, $sql);
400 400
 
401
-        $table_name = 'esp_registration';
402
-        $sql        = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
401
+		$table_name = 'esp_registration';
402
+		$sql        = "REG_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
403 403
 					  EVT_ID bigint(20) unsigned NOT NULL,
404 404
 					  ATT_ID bigint(20) unsigned NOT NULL,
405 405
 					  TXN_ID int(10) unsigned NOT NULL,
@@ -422,28 +422,28 @@  discard block
 block discarded – undo
422 422
 					  KEY STS_ID (STS_ID),
423 423
 					  KEY REG_url_link (REG_url_link),
424 424
 					  KEY REG_code (REG_code)";
425
-        $this->_table_is_new_in_this_version($table_name, $sql);
425
+		$this->_table_is_new_in_this_version($table_name, $sql);
426 426
 
427
-        $table_name = 'esp_checkin';
428
-        $sql        = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
427
+		$table_name = 'esp_checkin';
428
+		$sql        = "CHK_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
429 429
 					REG_ID int(10) unsigned NOT NULL,
430 430
 					DTT_ID int(10) unsigned NOT NULL,
431 431
 					CHK_in tinyint(1) unsigned NOT NULL DEFAULT 1,
432 432
 					CHK_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
433 433
 					PRIMARY KEY  (CHK_ID)";
434
-        $this->_table_is_new_in_this_version($table_name, $sql);
434
+		$this->_table_is_new_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(6) COLLATE utf8_bin NOT NULL,
440 440
 					  STA_name varchar(100) COLLATE utf8_bin NOT NULL,
441 441
 					  STA_active tinyint(1) DEFAULT '1',
442 442
 					  PRIMARY KEY  (STA_ID)";
443
-        $this->_table_is_new_in_this_version($table_name, $sql);
443
+		$this->_table_is_new_in_this_version($table_name, $sql);
444 444
 
445
-        $table_name = 'esp_status';
446
-        $sql        = "STS_ID varchar(3) COLLATE utf8_bin NOT NULL,
445
+		$table_name = 'esp_status';
446
+		$sql        = "STS_ID varchar(3) COLLATE utf8_bin NOT NULL,
447 447
 					  STS_code varchar(45) COLLATE utf8_bin NOT NULL,
448 448
 					  STS_type set('event','registration','transaction','payment','email') COLLATE utf8_bin NOT NULL,
449 449
 					  STS_can_edit tinyint(1) NOT NULL DEFAULT 0,
@@ -451,10 +451,10 @@  discard block
 block discarded – undo
451 451
 					  STS_open tinyint(1) NOT NULL DEFAULT 1,
452 452
 					  UNIQUE KEY STS_ID_UNIQUE (STS_ID),
453 453
 					  KEY STS_type (STS_type)";
454
-        $this->_table_is_new_in_this_version($table_name, $sql);
454
+		$this->_table_is_new_in_this_version($table_name, $sql);
455 455
 
456
-        $table_name = 'esp_transaction';
457
-        $sql        = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
456
+		$table_name = 'esp_transaction';
457
+		$sql        = "TXN_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
458 458
 					  TXN_timestamp datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
459 459
 					  TXN_total decimal(10,3) DEFAULT '0.00',
460 460
 					  TXN_paid decimal(10,3) NOT NULL DEFAULT '0.00',
@@ -464,10 +464,10 @@  discard block
 block discarded – undo
464 464
 					  PRIMARY KEY  (TXN_ID),
465 465
 					  KEY TXN_timestamp (TXN_timestamp),
466 466
 					  KEY STS_ID (STS_ID)";
467
-        $this->_table_is_new_in_this_version($table_name, $sql);
467
+		$this->_table_is_new_in_this_version($table_name, $sql);
468 468
 
469
-        $table_name = 'esp_venue_meta';
470
-        $sql        = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
469
+		$table_name = 'esp_venue_meta';
470
+		$sql        = "VNUM_ID int(11) NOT NULL AUTO_INCREMENT,
471 471
 			VNU_ID bigint(20) unsigned NOT NULL DEFAULT 0,
472 472
 			VNU_address varchar(255) DEFAULT NULL,
473 473
 			VNU_address2 varchar(255) DEFAULT NULL,
@@ -485,51 +485,51 @@  discard block
 block discarded – undo
485 485
 			PRIMARY KEY  (VNUM_ID),
486 486
 			KEY STA_ID (STA_ID),
487 487
 			KEY CNT_ISO (CNT_ISO)";
488
-        $this->_table_is_new_in_this_version($table_name, $sql);
489
-        // setting up the default stats and countries is also essential for the data migrations to run
490
-        // (because many need to convert old string states to foreign keys into the states table)
491
-        $this->insert_default_states();
492
-        $this->insert_default_countries();
493
-        // setting up default prices, price types, and tickets is also essential for the price migrations
494
-        $this->insert_default_price_types();
495
-        $this->insert_default_prices();
496
-        $this->insert_default_tickets();
497
-        // setting up the config wp option pretty well counts as a 'schema change', or at least should happen here
498
-        EE_Config::instance()->update_espresso_config();
499
-        return true;
500
-    }
501
-
502
-
503
-    /**
504
-     * Yes we could have cleaned up the ee3 tables here. But just in case someone
505
-     * didn't backup their DB, and decides they want ot keep using EE3, we'll
506
-     * leave them for now. Maybe remove them in 4.5 or something.
507
-     *
508
-     * @return boolean
509
-     */
510
-    public function schema_changes_after_migration()
511
-    {
512
-        return true;
513
-    }
514
-
515
-
516
-    /**
517
-     * insert_default_states
518
-     *
519
-     * @access public
520
-     * @static
521
-     * @return void
522
-     * @throws EE_Error
523
-     */
524
-    public function insert_default_states()
525
-    {
526
-        global $wpdb;
527
-        $state_table = $wpdb->prefix . "esp_state";
528
-        if ($this->_get_table_analysis()->tableExists($state_table)) {
529
-            $SQL    = "SELECT COUNT('STA_ID') FROM " . $state_table;
530
-            $states = $wpdb->get_var($SQL);
531
-            if (! $states) {
532
-                $SQL = "INSERT INTO " . $state_table . "
488
+		$this->_table_is_new_in_this_version($table_name, $sql);
489
+		// setting up the default stats and countries is also essential for the data migrations to run
490
+		// (because many need to convert old string states to foreign keys into the states table)
491
+		$this->insert_default_states();
492
+		$this->insert_default_countries();
493
+		// setting up default prices, price types, and tickets is also essential for the price migrations
494
+		$this->insert_default_price_types();
495
+		$this->insert_default_prices();
496
+		$this->insert_default_tickets();
497
+		// setting up the config wp option pretty well counts as a 'schema change', or at least should happen here
498
+		EE_Config::instance()->update_espresso_config();
499
+		return true;
500
+	}
501
+
502
+
503
+	/**
504
+	 * Yes we could have cleaned up the ee3 tables here. But just in case someone
505
+	 * didn't backup their DB, and decides they want ot keep using EE3, we'll
506
+	 * leave them for now. Maybe remove them in 4.5 or something.
507
+	 *
508
+	 * @return boolean
509
+	 */
510
+	public function schema_changes_after_migration()
511
+	{
512
+		return true;
513
+	}
514
+
515
+
516
+	/**
517
+	 * insert_default_states
518
+	 *
519
+	 * @access public
520
+	 * @static
521
+	 * @return void
522
+	 * @throws EE_Error
523
+	 */
524
+	public function insert_default_states()
525
+	{
526
+		global $wpdb;
527
+		$state_table = $wpdb->prefix . "esp_state";
528
+		if ($this->_get_table_analysis()->tableExists($state_table)) {
529
+			$SQL    = "SELECT COUNT('STA_ID') FROM " . $state_table;
530
+			$states = $wpdb->get_var($SQL);
531
+			if (! $states) {
532
+				$SQL = "INSERT INTO " . $state_table . "
533 533
 				(STA_ID, CNT_ISO, STA_abbrev, STA_name, STA_active) VALUES
534 534
 				(1, 'US', 'AK', 'Alaska', 1),
535 535
 				(2, 'US', 'AL', 'Alabama', 1),
@@ -603,29 +603,29 @@  discard block
 block discarded – undo
603 603
 				(70, 'CA', 'NT', 'Northwest Territories', 1),
604 604
 				(71, 'CA', 'NU', 'Nunavut', 1),
605 605
 				(72, 'CA', 'YT', 'Yukon', 1);";
606
-                $wpdb->query($SQL);
607
-            }
608
-        }
609
-    }
610
-
611
-
612
-    /**
613
-     * insert_default_countries
614
-     *
615
-     * @access public
616
-     * @static
617
-     * @return void
618
-     * @throws EE_Error
619
-     */
620
-    public function insert_default_countries()
621
-    {
622
-        global $wpdb;
623
-        $country_table = $wpdb->prefix . "esp_country";
624
-        if ($this->_get_table_analysis()->tableExists($country_table)) {
625
-            $SQL       = "SELECT COUNT('CNT_ISO') FROM " . $country_table;
626
-            $countries = $wpdb->get_var($SQL);
627
-            if (! $countries) {
628
-                $SQL = "INSERT INTO " . $country_table . "
606
+				$wpdb->query($SQL);
607
+			}
608
+		}
609
+	}
610
+
611
+
612
+	/**
613
+	 * insert_default_countries
614
+	 *
615
+	 * @access public
616
+	 * @static
617
+	 * @return void
618
+	 * @throws EE_Error
619
+	 */
620
+	public function insert_default_countries()
621
+	{
622
+		global $wpdb;
623
+		$country_table = $wpdb->prefix . "esp_country";
624
+		if ($this->_get_table_analysis()->tableExists($country_table)) {
625
+			$SQL       = "SELECT COUNT('CNT_ISO') FROM " . $country_table;
626
+			$countries = $wpdb->get_var($SQL);
627
+			if (! $countries) {
628
+				$SQL = "INSERT INTO " . $country_table . "
629 629
 				(CNT_ISO, CNT_ISO3, RGN_ID, CNT_name, CNT_cur_code, CNT_cur_single, CNT_cur_plural, CNT_cur_sign, CNT_cur_sign_b4, CNT_cur_dec_plc, CNT_tel_code, CNT_is_EU, CNT_active) VALUES
630 630
 				('AD', 'AND', 0, 'Andorra', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+376', 0, 0),
631 631
 				('AE', 'ARE', 0, 'United Arab Emirates', 'AED', 'Dirham', 'Dirhams', 'د.إ', 1, 2, '+971', 0, 0),
@@ -853,1051 +853,1051 @@  discard block
 block discarded – undo
853 853
 				('ZA', 'ZAF', 0, 'South Africa', 'ZAR', 'Rand', 'Rands', 'R', 1, 2, '+27', 0, 0),
854 854
 				('ZM', 'ZMB', 0, 'Zambia', 'ZMK', 'Kwacha', 'Kwachas', '', 1, 2, '+260', 0, 0),
855 855
 				('ZW', 'ZWE', 0, 'Zimbabwe', 'ZWD', 'Dollar', 'Dollars', 'Z$', 1, 2, '+263', 0, 0);";
856
-                $wpdb->query($SQL);
857
-            }
858
-        }
859
-    }
860
-
861
-
862
-    /**
863
-     * insert_default_price_types
864
-     *
865
-     * @access public
866
-     * @static
867
-     * @return void
868
-     * @throws EE_Error
869
-     */
870
-    public function insert_default_price_types()
871
-    {
872
-        global $wpdb;
873
-        $price_type_table = $wpdb->prefix . "esp_price_type";
874
-        if ($this->_get_table_analysis()->tableExists($price_type_table)) {
875
-            $SQL               = 'SELECT COUNT(PRT_ID) FROM ' . $price_type_table;
876
-            $price_types_exist = $wpdb->get_var($SQL);
877
-            if (! $price_types_exist) {
878
-                $SQL = "INSERT INTO $price_type_table ( PRT_ID, PRT_name, PBT_ID, PRT_is_percent, PRT_order, PRT_deleted ) VALUES
856
+				$wpdb->query($SQL);
857
+			}
858
+		}
859
+	}
860
+
861
+
862
+	/**
863
+	 * insert_default_price_types
864
+	 *
865
+	 * @access public
866
+	 * @static
867
+	 * @return void
868
+	 * @throws EE_Error
869
+	 */
870
+	public function insert_default_price_types()
871
+	{
872
+		global $wpdb;
873
+		$price_type_table = $wpdb->prefix . "esp_price_type";
874
+		if ($this->_get_table_analysis()->tableExists($price_type_table)) {
875
+			$SQL               = 'SELECT COUNT(PRT_ID) FROM ' . $price_type_table;
876
+			$price_types_exist = $wpdb->get_var($SQL);
877
+			if (! $price_types_exist) {
878
+				$SQL = "INSERT INTO $price_type_table ( PRT_ID, PRT_name, PBT_ID, PRT_is_percent, PRT_order, PRT_deleted ) VALUES
879 879
 							(1, '" . esc_html__('Base Price', 'event_espresso') . "', 1,  0, 0, 0),
880 880
 							(2, '" . esc_html__('Percent Discount', 'event_espresso') . "', 2,  1, 20, 0),
881 881
 							(3, '" . esc_html__('Fixed Discount', 'event_espresso') . "', 2,  0, 30, 0),
882 882
 							(4, '" . esc_html__('Percent Surcharge', 'event_espresso') . "', 3,  1, 40, 0),
883 883
 							(5, '" . esc_html__('Fixed Surcharge', 'event_espresso') . "', 3,  0, 50, 0);";
884
-                $SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_price_types__SQL', $SQL);
885
-                $wpdb->query($SQL);
886
-            }
887
-        }
888
-    }
889
-
890
-
891
-    /**
892
-     * insert_default_prices. We assume we're upgrading to regular here.
893
-     * If we're INSTALLING 4.1 CAF, then we add a few extra default prices
894
-     * when EEH_Activation's initialize_db_content is called via a hook in
895
-     * EE_Brewing_regular
896
-     *
897
-     * @access public
898
-     * @static
899
-     * @return void
900
-     * @throws EE_Error
901
-     */
902
-    public function insert_default_prices()
903
-    {
904
-        global $wpdb;
905
-        $price_table = $wpdb->prefix . "esp_price";
906
-        if ($this->_get_table_analysis()->tableExists($price_table)) {
907
-            $SQL          = 'SELECT COUNT(PRC_ID) FROM ' . $price_table;
908
-            $prices_exist = $wpdb->get_var($SQL);
909
-            if (! $prices_exist) {
910
-                $SQL = "INSERT INTO $price_table
884
+				$SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_price_types__SQL', $SQL);
885
+				$wpdb->query($SQL);
886
+			}
887
+		}
888
+	}
889
+
890
+
891
+	/**
892
+	 * insert_default_prices. We assume we're upgrading to regular here.
893
+	 * If we're INSTALLING 4.1 CAF, then we add a few extra default prices
894
+	 * when EEH_Activation's initialize_db_content is called via a hook in
895
+	 * EE_Brewing_regular
896
+	 *
897
+	 * @access public
898
+	 * @static
899
+	 * @return void
900
+	 * @throws EE_Error
901
+	 */
902
+	public function insert_default_prices()
903
+	{
904
+		global $wpdb;
905
+		$price_table = $wpdb->prefix . "esp_price";
906
+		if ($this->_get_table_analysis()->tableExists($price_table)) {
907
+			$SQL          = 'SELECT COUNT(PRC_ID) FROM ' . $price_table;
908
+			$prices_exist = $wpdb->get_var($SQL);
909
+			if (! $prices_exist) {
910
+				$SQL = "INSERT INTO $price_table
911 911
 							(PRC_ID, PRT_ID, PRC_amount, PRC_name, PRC_desc,  PRC_is_default, PRC_overrides, PRC_order, PRC_deleted, PRC_parent ) VALUES
912 912
 							(1, 1, '0.00', 'Admission', '', 1, null, 0, 0, 0);";
913
-                $SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_prices__SQL', $SQL);
914
-                $wpdb->query($SQL);
915
-            }
916
-        }
917
-    }
918
-
919
-
920
-    /**
921
-     * insert default ticket
922
-     *
923
-     * @access public
924
-     * @static
925
-     * @return void
926
-     * @throws EE_Error
927
-     * @throws EE_Error
928
-     */
929
-    public function insert_default_tickets()
930
-    {
931
-        global $wpdb;
932
-        $ticket_table = $wpdb->prefix . "esp_ticket";
933
-        if ($this->_get_table_analysis()->tableExists($ticket_table)) {
934
-            $SQL           = 'SELECT COUNT(TKT_ID) FROM ' . $ticket_table;
935
-            $tickets_exist = $wpdb->get_var($SQL);
936
-            if (! $tickets_exist) {
937
-                $SQL = "INSERT INTO $ticket_table
913
+				$SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_prices__SQL', $SQL);
914
+				$wpdb->query($SQL);
915
+			}
916
+		}
917
+	}
918
+
919
+
920
+	/**
921
+	 * insert default ticket
922
+	 *
923
+	 * @access public
924
+	 * @static
925
+	 * @return void
926
+	 * @throws EE_Error
927
+	 * @throws EE_Error
928
+	 */
929
+	public function insert_default_tickets()
930
+	{
931
+		global $wpdb;
932
+		$ticket_table = $wpdb->prefix . "esp_ticket";
933
+		if ($this->_get_table_analysis()->tableExists($ticket_table)) {
934
+			$SQL           = 'SELECT COUNT(TKT_ID) FROM ' . $ticket_table;
935
+			$tickets_exist = $wpdb->get_var($SQL);
936
+			if (! $tickets_exist) {
937
+				$SQL = "INSERT INTO $ticket_table
938 938
 					( TKT_ID, TTM_ID, TKT_name, TKT_description, TKT_qty, TKT_sold, TKT_uses, TKT_min, TKT_max, TKT_price, TKT_start_date, TKT_end_date, TKT_taxable, TKT_order, TKT_row, TKT_is_default, TKT_parent, TKT_deleted ) VALUES
939 939
 					( 1, 0, '" .
940
-                       esc_html__("Free Ticket", "event_espresso") .
941
-                       "', '', 100, 0, -1, 0, -1, 0.00, '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0, 0, 1, 1, 0, 0);";
942
-                $SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_tickets__SQL', $SQL);
943
-                $wpdb->query($SQL);
944
-            }
945
-        }
946
-        $ticket_price_table = $wpdb->prefix . "esp_ticket_price";
947
-        if ($this->_get_table_analysis()->tableExists($ticket_price_table)) {
948
-            $SQL              = 'SELECT COUNT(TKP_ID) FROM ' . $ticket_price_table;
949
-            $ticket_prc_exist = $wpdb->get_var($SQL);
950
-            if (! $ticket_prc_exist) {
951
-                $SQL = "INSERT INTO $ticket_price_table
940
+					   esc_html__("Free Ticket", "event_espresso") .
941
+					   "', '', 100, 0, -1, 0, -1, 0.00, '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0, 0, 1, 1, 0, 0);";
942
+				$SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_tickets__SQL', $SQL);
943
+				$wpdb->query($SQL);
944
+			}
945
+		}
946
+		$ticket_price_table = $wpdb->prefix . "esp_ticket_price";
947
+		if ($this->_get_table_analysis()->tableExists($ticket_price_table)) {
948
+			$SQL              = 'SELECT COUNT(TKP_ID) FROM ' . $ticket_price_table;
949
+			$ticket_prc_exist = $wpdb->get_var($SQL);
950
+			if (! $ticket_prc_exist) {
951
+				$SQL = "INSERT INTO $ticket_price_table
952 952
 				( TKP_ID, TKT_ID, PRC_ID ) VALUES
953 953
 				( 1, 1, 1 )
954 954
 				";
955
-                $SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_tickets__SQL__ticket_price', $SQL);
956
-                $wpdb->query($SQL);
957
-            }
958
-        }
959
-    }
960
-
961
-
962
-    /**
963
-     * Gets a country entry as an array, or creates one if none is found. Much like EEM_Country::instance()->get_one(),
964
-     * but is independent of outside code which can change in future versions of EE. Also, $country_name CAN be a 3.1
965
-     * country ID (int), a 2-letter ISO, 3-letter ISO, or name
966
-     *
967
-     * @param int|string $country_name or country ID
968
-     * @return array where keys are columns, values are column values
969
-     * @throws EE_Error
970
-     * @throws EE_Error
971
-     * @global wpdb      $wpdb
972
-     */
973
-    public function get_or_create_country($country_name): array
974
-    {
975
-        if (! $country_name) {
976
-            throw new EE_Error(esc_html__("Could not get a country because country name is blank", "event_espresso"));
977
-        }
978
-        global $wpdb;
979
-        $country_table = $wpdb->prefix . "esp_country";
980
-        if (is_int($country_name)) {
981
-            $country_name = $this->get_iso_from_3_1_country_id($country_name);
982
-        }
983
-        $country = $wpdb->get_row(
984
-            $wpdb->prepare(
985
-                "SELECT * FROM $country_table WHERE
955
+				$SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_tickets__SQL__ticket_price', $SQL);
956
+				$wpdb->query($SQL);
957
+			}
958
+		}
959
+	}
960
+
961
+
962
+	/**
963
+	 * Gets a country entry as an array, or creates one if none is found. Much like EEM_Country::instance()->get_one(),
964
+	 * but is independent of outside code which can change in future versions of EE. Also, $country_name CAN be a 3.1
965
+	 * country ID (int), a 2-letter ISO, 3-letter ISO, or name
966
+	 *
967
+	 * @param int|string $country_name or country ID
968
+	 * @return array where keys are columns, values are column values
969
+	 * @throws EE_Error
970
+	 * @throws EE_Error
971
+	 * @global wpdb      $wpdb
972
+	 */
973
+	public function get_or_create_country($country_name): array
974
+	{
975
+		if (! $country_name) {
976
+			throw new EE_Error(esc_html__("Could not get a country because country name is blank", "event_espresso"));
977
+		}
978
+		global $wpdb;
979
+		$country_table = $wpdb->prefix . "esp_country";
980
+		if (is_int($country_name)) {
981
+			$country_name = $this->get_iso_from_3_1_country_id($country_name);
982
+		}
983
+		$country = $wpdb->get_row(
984
+			$wpdb->prepare(
985
+				"SELECT * FROM $country_table WHERE
986 986
 			CNT_ISO LIKE %s OR
987 987
 			CNT_ISO3 LIKE %s OR
988 988
 			CNT_name LIKE %s LIMIT 1",
989
-                $country_name,
990
-                $country_name,
991
-                $country_name
992
-            ),
993
-            ARRAY_A
994
-        );
995
-        if (! $country) {
996
-            // insert a new one then
997
-            $cols_n_values = [
998
-                'CNT_ISO'         => $this->_find_available_country_iso(),
999
-                'CNT_ISO3'        => $this->_find_available_country_iso(3),
1000
-                'RGN_ID'          => 0,
1001
-                'CNT_name'        => $country_name,
1002
-                'CNT_cur_code'    => 'USD',
1003
-                'CNT_cur_single'  => 'Dollar',
1004
-                'CNT_cur_plural'  => 'Dollars',
1005
-                'CNT_cur_sign'    => '&#36;',
1006
-                'CNT_cur_sign_b4' => true,
1007
-                'CNT_cur_dec_plc' => 2,
1008
-                'CNT_cur_dec_mrk' => '.',
1009
-                'CNT_cur_thsnds'  => ',',
1010
-                'CNT_tel_code'    => '+1',
1011
-                'CNT_is_EU'       => false,
1012
-                'CNT_active'      => true,
1013
-            ];
1014
-            $data_types    = [
1015
-                '%s',// CNT_ISO
1016
-                '%s',// CNT_ISO3
1017
-                '%d',// RGN_ID
1018
-                '%s',// CNT_name
1019
-                '%s',// CNT_cur_code
1020
-                '%s',// CNT_cur_single
1021
-                '%s',// CNT_cur_plural
1022
-                '%s',// CNT_cur_sign
1023
-                '%d',// CNT_cur_sign_b4
1024
-                '%d',// CNT_cur_dec_plc
1025
-                '%s',// CNT_cur_dec_mrk
1026
-                '%s',// CNT_cur_thsnds
1027
-                '%s',// CNT_tel_code
1028
-                '%d',// CNT_is_EU
1029
-                '%d',// CNT_active
1030
-            ];
1031
-            $success       = $wpdb->insert(
1032
-                $country_table,
1033
-                $cols_n_values,
1034
-                $data_types
1035
-            );
1036
-            if (! $success) {
1037
-                throw new EE_Error(
1038
-                    $this->_create_error_message_for_db_insertion(
1039
-                        'N/A',
1040
-                        ['country_id' => $country_name],
1041
-                        $country_table,
1042
-                        $cols_n_values,
1043
-                        $data_types
1044
-                    )
1045
-                );
1046
-            }
1047
-            $country = $cols_n_values;
1048
-        }
1049
-        return $country;
1050
-    }
1051
-
1052
-
1053
-    /**
1054
-     * finds a country iso which hasn't been used yet
1055
-     *
1056
-     * @param int   $num_letters
1057
-     * @return string
1058
-     * @global wpdb $wpdb
1059
-     */
1060
-    private function _find_available_country_iso(int $num_letters = 2): string
1061
-    {
1062
-        global $wpdb;
1063
-        $country_table = $wpdb->prefix . "esp_country";
1064
-        $attempts      = 0;
1065
-        do {
1066
-            $current_iso           = strtoupper(wp_generate_password($num_letters, false));
1067
-            $country_with_that_iso = $wpdb->get_var(
1068
-                $wpdb->prepare(
1069
-                    "SELECT count(CNT_ISO) FROM " . $country_table . " WHERE CNT_ISO=%s",
1070
-                    $current_iso
1071
-                )
1072
-            );
1073
-            $attempts++;
1074
-            // keep going until we find an available country code, or we arbitrarily
1075
-            // decide we've tried this enough. Somehow they have way too many countries
1076
-            // (probably because they're mis-using the EE3 country_id like a custom question)
1077
-        } while (intval($country_with_that_iso) && $attempts < 200);
1078
-        return $current_iso;
1079
-    }
1080
-
1081
-
1082
-    /**
1083
-     * Gets a state entry as an array, or creates one if none is found. Much like EEM_State::instance()->get_one(), but
1084
-     * is independent of outside code which can change in future versions of EE
1085
-     *
1086
-     * @param string     $state_name
1087
-     * @param int|string $country_name
1088
-     * @return array where keys are columns, values are column values
1089
-     * @throws EE_Error
1090
-     * @global wpdb      $wpdb
1091
-     */
1092
-    public function get_or_create_state(string $state_name, $country_name = ''): array
1093
-    {
1094
-        if (! $state_name) {
1095
-            throw new EE_Error(
1096
-                esc_html__(
1097
-                    "Could not get-or-create state because no state name was provided",
1098
-                    "event_espresso"
1099
-                )
1100
-            );
1101
-        }
1102
-        try {
1103
-            $country     = $this->get_or_create_country($country_name);
1104
-            $country_iso = $country['CNT_ISO'];
1105
-        } catch (EE_Error $e) {
1106
-            $country_iso = $this->get_default_country_iso();
1107
-        }
1108
-        global $wpdb;
1109
-        $state_table = $wpdb->prefix . "esp_state";
1110
-        $state       = $wpdb->get_row(
1111
-            $wpdb->prepare(
1112
-                "SELECT * FROM $state_table WHERE
989
+				$country_name,
990
+				$country_name,
991
+				$country_name
992
+			),
993
+			ARRAY_A
994
+		);
995
+		if (! $country) {
996
+			// insert a new one then
997
+			$cols_n_values = [
998
+				'CNT_ISO'         => $this->_find_available_country_iso(),
999
+				'CNT_ISO3'        => $this->_find_available_country_iso(3),
1000
+				'RGN_ID'          => 0,
1001
+				'CNT_name'        => $country_name,
1002
+				'CNT_cur_code'    => 'USD',
1003
+				'CNT_cur_single'  => 'Dollar',
1004
+				'CNT_cur_plural'  => 'Dollars',
1005
+				'CNT_cur_sign'    => '&#36;',
1006
+				'CNT_cur_sign_b4' => true,
1007
+				'CNT_cur_dec_plc' => 2,
1008
+				'CNT_cur_dec_mrk' => '.',
1009
+				'CNT_cur_thsnds'  => ',',
1010
+				'CNT_tel_code'    => '+1',
1011
+				'CNT_is_EU'       => false,
1012
+				'CNT_active'      => true,
1013
+			];
1014
+			$data_types    = [
1015
+				'%s',// CNT_ISO
1016
+				'%s',// CNT_ISO3
1017
+				'%d',// RGN_ID
1018
+				'%s',// CNT_name
1019
+				'%s',// CNT_cur_code
1020
+				'%s',// CNT_cur_single
1021
+				'%s',// CNT_cur_plural
1022
+				'%s',// CNT_cur_sign
1023
+				'%d',// CNT_cur_sign_b4
1024
+				'%d',// CNT_cur_dec_plc
1025
+				'%s',// CNT_cur_dec_mrk
1026
+				'%s',// CNT_cur_thsnds
1027
+				'%s',// CNT_tel_code
1028
+				'%d',// CNT_is_EU
1029
+				'%d',// CNT_active
1030
+			];
1031
+			$success       = $wpdb->insert(
1032
+				$country_table,
1033
+				$cols_n_values,
1034
+				$data_types
1035
+			);
1036
+			if (! $success) {
1037
+				throw new EE_Error(
1038
+					$this->_create_error_message_for_db_insertion(
1039
+						'N/A',
1040
+						['country_id' => $country_name],
1041
+						$country_table,
1042
+						$cols_n_values,
1043
+						$data_types
1044
+					)
1045
+				);
1046
+			}
1047
+			$country = $cols_n_values;
1048
+		}
1049
+		return $country;
1050
+	}
1051
+
1052
+
1053
+	/**
1054
+	 * finds a country iso which hasn't been used yet
1055
+	 *
1056
+	 * @param int   $num_letters
1057
+	 * @return string
1058
+	 * @global wpdb $wpdb
1059
+	 */
1060
+	private function _find_available_country_iso(int $num_letters = 2): string
1061
+	{
1062
+		global $wpdb;
1063
+		$country_table = $wpdb->prefix . "esp_country";
1064
+		$attempts      = 0;
1065
+		do {
1066
+			$current_iso           = strtoupper(wp_generate_password($num_letters, false));
1067
+			$country_with_that_iso = $wpdb->get_var(
1068
+				$wpdb->prepare(
1069
+					"SELECT count(CNT_ISO) FROM " . $country_table . " WHERE CNT_ISO=%s",
1070
+					$current_iso
1071
+				)
1072
+			);
1073
+			$attempts++;
1074
+			// keep going until we find an available country code, or we arbitrarily
1075
+			// decide we've tried this enough. Somehow they have way too many countries
1076
+			// (probably because they're mis-using the EE3 country_id like a custom question)
1077
+		} while (intval($country_with_that_iso) && $attempts < 200);
1078
+		return $current_iso;
1079
+	}
1080
+
1081
+
1082
+	/**
1083
+	 * Gets a state entry as an array, or creates one if none is found. Much like EEM_State::instance()->get_one(), but
1084
+	 * is independent of outside code which can change in future versions of EE
1085
+	 *
1086
+	 * @param string     $state_name
1087
+	 * @param int|string $country_name
1088
+	 * @return array where keys are columns, values are column values
1089
+	 * @throws EE_Error
1090
+	 * @global wpdb      $wpdb
1091
+	 */
1092
+	public function get_or_create_state(string $state_name, $country_name = ''): array
1093
+	{
1094
+		if (! $state_name) {
1095
+			throw new EE_Error(
1096
+				esc_html__(
1097
+					"Could not get-or-create state because no state name was provided",
1098
+					"event_espresso"
1099
+				)
1100
+			);
1101
+		}
1102
+		try {
1103
+			$country     = $this->get_or_create_country($country_name);
1104
+			$country_iso = $country['CNT_ISO'];
1105
+		} catch (EE_Error $e) {
1106
+			$country_iso = $this->get_default_country_iso();
1107
+		}
1108
+		global $wpdb;
1109
+		$state_table = $wpdb->prefix . "esp_state";
1110
+		$state       = $wpdb->get_row(
1111
+			$wpdb->prepare(
1112
+				"SELECT * FROM $state_table WHERE
1113 1113
 			(STA_abbrev LIKE %s OR
1114 1114
 			STA_name LIKE %s) AND
1115 1115
 			CNT_ISO LIKE %s LIMIT 1",
1116
-                $state_name,
1117
-                $state_name,
1118
-                $country_iso
1119
-            ),
1120
-            ARRAY_A
1121
-        );
1122
-        if (! $state) {
1123
-            // insert a new one then
1124
-            $cols_n_values = [
1125
-                'CNT_ISO'    => $country_iso,
1126
-                'STA_abbrev' => substr($state_name, 0, 6),
1127
-                'STA_name'   => $state_name,
1128
-                'STA_active' => true,
1129
-            ];
1130
-            $data_types    = [
1131
-                '%s',// CNT_ISO
1132
-                '%s',// STA_abbrev
1133
-                '%s',// STA_name
1134
-                '%d',// STA_active
1135
-            ];
1136
-            $success       = $wpdb->insert($state_table, $cols_n_values, $data_types);
1137
-            if (! $success) {
1138
-                throw new EE_Error(
1139
-                    $this->_create_error_message_for_db_insertion(
1140
-                        'N/A',
1141
-                        ['state' => $state_name, 'country_id' => $country_name],
1142
-                        $state_table,
1143
-                        $cols_n_values,
1144
-                        $data_types
1145
-                    )
1146
-                );
1147
-            }
1148
-            $state           = $cols_n_values;
1149
-            $state['STA_ID'] = $wpdb->insert_id;
1150
-        }
1151
-        return $state;
1152
-    }
1153
-
1154
-
1155
-    /**
1156
-     * Fixes times like "5:00 PM" into the expected 24-hour format "17:00".
1157
-     * THis is actually just copied from the 3.1 JSON API because it needed to do the exact same thing
1158
-     *
1159
-     * @param string|null $timeString
1160
-     * @return string in the php DATETIME format: "G:i" (24-hour format hour with leading zeros, a colon, and minutes
1161
-     *                with leading zeros)
1162
-     */
1163
-    public function convertTimeFromAMPM(?string $timeString): string
1164
-    {
1165
-        $matches = [];
1166
-        preg_match("~(\\d*):(\\d*)~", $timeString, $matches);
1167
-        if (! $matches || count($matches) < 3) {
1168
-            $hour    = '00';
1169
-            $minutes = '00';
1170
-        } else {
1171
-            $hour    = intval($matches[1]);
1172
-            $minutes = $matches[2];
1173
-        }
1174
-        if (strpos($timeString, 'PM') || strpos($timeString, 'pm')) {
1175
-            $hour = intval($hour) + 12;
1176
-        }
1177
-        $hour    = str_pad("$hour", 2, '0', STR_PAD_LEFT);
1178
-        $minutes = str_pad("$minutes", 2, '0', STR_PAD_LEFT);
1179
-        return "$hour:$minutes";
1180
-    }
1181
-
1182
-
1183
-    /**
1184
-     * Gets the ISO3 fora country given its 3.1 country ID.
1185
-     *
1186
-     * @param int $country_id
1187
-     * @return string the country's ISO3 code
1188
-     */
1189
-    public function get_iso_from_3_1_country_id(int $country_id): string
1190
-    {
1191
-        $old_countries = [
1192
-            [64, 'United States', 'US', 'USA', 1],
1193
-            [15, 'Australia', 'AU', 'AUS', 1],
1194
-            [39, 'Canada', 'CA', 'CAN', 1],
1195
-            [171, 'United Kingdom', 'GB', 'GBR', 1],
1196
-            [70, 'France', 'FR', 'FRA', 2],
1197
-            [111, 'Italy', 'IT', 'ITA', 2],
1198
-            [63, 'Spain', 'ES', 'ESP', 2],
1199
-            [1, 'Afghanistan', 'AF', 'AFG', 1],
1200
-            [2, 'Albania', 'AL', 'ALB', 1],
1201
-            [3, 'Germany', 'DE', 'DEU', 2],
1202
-            [198, 'Switzerland', 'CH', 'CHE', 1],
1203
-            [87, 'Netherlands', 'NL', 'NLD', 2],
1204
-            [197, 'Sweden', 'SE', 'SWE', 1],
1205
-            [230, 'Akrotiri and Dhekelia', 'CY', 'CYP', 2],
1206
-            [4, 'Andorra', 'AD', 'AND', 2],
1207
-            [5, 'Angola', 'AO', 'AGO', 1],
1208
-            [6, 'Anguilla', 'AI', 'AIA', 1],
1209
-            [7, 'Antarctica', 'AQ', 'ATA', 1],
1210
-            [8, 'Antigua and Barbuda', 'AG', 'ATG', 1],
1211
-            [10, 'Saudi Arabia', 'SA', 'SAU', 1],
1212
-            [11, 'Algeria', 'DZ', 'DZA', 1],
1213
-            [12, 'Argentina', 'AR', 'ARG', 1],
1214
-            [13, 'Armenia', 'AM', 'ARM', 1],
1215
-            [14, 'Aruba', 'AW', 'ABW', 1],
1216
-            [16, 'Austria', 'AT', 'AUT', 2],
1217
-            [17, 'Azerbaijan', 'AZ', 'AZE', 1],
1218
-            [18, 'Bahamas', 'BS', 'BHS', 1],
1219
-            [19, 'Bahrain', 'BH', 'BHR', 1],
1220
-            [20, 'Bangladesh', 'BD', 'BGD', 1],
1221
-            [21, 'Barbados', 'BB', 'BRB', 1],
1222
-            [22, 'Belgium ', 'BE', 'BEL', 2],
1223
-            [23, 'Belize', 'BZ', 'BLZ', 1],
1224
-            [24, 'Benin', 'BJ', 'BEN', 1],
1225
-            [25, 'Bermudas', 'BM', 'BMU', 1],
1226
-            [26, 'Belarus', 'BY', 'BLR', 1],
1227
-            [27, 'Bolivia', 'BO', 'BOL', 1],
1228
-            [28, 'Bosnia and Herzegovina', 'BA', 'BIH', 1],
1229
-            [29, 'Botswana', 'BW', 'BWA', 1],
1230
-            [96, 'Bouvet Island', 'BV', 'BVT', 1],
1231
-            [30, 'Brazil', 'BR', 'BRA', 1],
1232
-            [31, 'Brunei', 'BN', 'BRN', 1],
1233
-            [32, 'Bulgaria', 'BG', 'BGR', 1],
1234
-            [33, 'Burkina Faso', 'BF', 'BFA', 1],
1235
-            [34, 'Burundi', 'BI', 'BDI', 1],
1236
-            [35, 'Bhutan', 'BT', 'BTN', 1],
1237
-            [36, 'Cape Verde', 'CV', 'CPV', 1],
1238
-            [37, 'Cambodia', 'KH', 'KHM', 1],
1239
-            [38, 'Cameroon', 'CM', 'CMR', 1],
1240
-            [98, 'Cayman Islands', 'KY', 'CYM', 1],
1241
-            [172, 'Central African Republic', 'CF', 'CAF', 1],
1242
-            [40, 'Chad', 'TD', 'TCD', 1],
1243
-            [41, 'Chile', 'CL', 'CHL', 1],
1244
-            [42, 'China', 'CN', 'CHN', 1],
1245
-            [105, 'Christmas Island', 'CX', 'CXR', 1],
1246
-            [43, 'Cyprus', 'CY', 'CYP', 2],
1247
-            [99, 'Cocos Island', 'CC', 'CCK', 1],
1248
-            [100, 'Cook Islands', 'CK', 'COK', 1],
1249
-            [44, 'Colombia', 'CO', 'COL', 1],
1250
-            [45, 'Comoros', 'KM', 'COM', 1],
1251
-            [46, 'Congo', 'CG', 'COG', 1],
1252
-            [47, 'North Korea', 'KP', 'PRK', 1],
1253
-            [50, 'Costa Rica', 'CR', 'CRI', 1],
1254
-            [51, 'Croatia', 'HR', 'HRV', 1],
1255
-            [52, 'Cuba', 'CU', 'CUB', 1],
1256
-            [173, 'Czech Republic', 'CZ', 'CZE', 1],
1257
-            [53, 'Denmark', 'DK', 'DNK', 1],
1258
-            [54, 'Djibouti', 'DJ', 'DJI', 1],
1259
-            [55, 'Dominica', 'DM', 'DMA', 1],
1260
-            [174, 'Dominican Republic', 'DO', 'DOM', 1],
1261
-            [56, 'Ecuador', 'EC', 'ECU', 1],
1262
-            [57, 'Egypt', 'EG', 'EGY', 1],
1263
-            [58, 'El Salvador', 'SV', 'SLV', 1],
1264
-            [60, 'Eritrea', 'ER', 'ERI', 1],
1265
-            [61, 'Slovakia', 'SK', 'SVK', 2],
1266
-            [62, 'Slovenia', 'SI', 'SVN', 2],
1267
-            [65, 'Estonia', 'EE', 'EST', 2],
1268
-            [66, 'Ethiopia', 'ET', 'ETH', 1],
1269
-            [102, 'Faroe islands', 'FO', 'FRO', 1],
1270
-            [103, 'Falkland Islands', 'FK', 'FLK', 1],
1271
-            [67, 'Fiji', 'FJ', 'FJI', 1],
1272
-            [69, 'Finland', 'FI', 'FIN', 2],
1273
-            [71, 'Gabon', 'GA', 'GAB', 1],
1274
-            [72, 'Gambia', 'GM', 'GMB', 1],
1275
-            [73, 'Georgia', 'GE', 'GEO', 1],
1276
-            [74, 'Ghana', 'GH', 'GHA', 1],
1277
-            [75, 'Gibraltar', 'GI', 'GIB', 1],
1278
-            [76, 'Greece', 'GR', 'GRC', 2],
1279
-            [77, 'Grenada', 'GD', 'GRD', 1],
1280
-            [78, 'Greenland', 'GL', 'GRL', 1],
1281
-            [79, 'Guadeloupe', 'GP', 'GLP', 1],
1282
-            [80, 'Guam', 'GU', 'GUM', 1],
1283
-            [81, 'Guatemala', 'GT', 'GTM', 1],
1284
-            [82, 'Guinea', 'GN', 'GIN', 1],
1285
-            [83, 'Equatorial Guinea', 'GQ', 'GNQ', 1],
1286
-            [84, 'Guinea-Bissau', 'GW', 'GNB', 1],
1287
-            [85, 'Guyana', 'GY', 'GUY', 1],
1288
-            [86, 'Haiti', 'HT', 'HTI', 1],
1289
-            [88, 'Honduras', 'HN', 'HND', 1],
1290
-            [89, 'Hong Kong', 'HK', 'HKG', 1],
1291
-            [90, 'Hungary', 'HU', 'HUN', 1],
1292
-            [91, 'India', 'IN', 'IND', 1],
1293
-            [205, 'British Indian Ocean Territory', 'IO', 'IOT', 1],
1294
-            [92, 'Indonesia', 'ID', 'IDN', 1],
1295
-            [93, 'Iraq', 'IQ', 'IRQ', 1],
1296
-            [94, 'Iran', 'IR', 'IRN', 1],
1297
-            [95, 'Ireland', 'IE', 'IRL', 2],
1298
-            [97, 'Iceland', 'IS', 'ISL', 1],
1299
-            [110, 'Israel', 'IL', 'ISR', 1],
1300
-            [49, 'Ivory Coast ', 'CI', 'CIV', 1],
1301
-            [112, 'Jamaica', 'JM', 'JAM', 1],
1302
-            [113, 'Japan', 'JP', 'JPN', 1],
1303
-            [114, 'Jordan', 'JO', 'JOR', 1],
1304
-            [115, 'Kazakhstan', 'KZ', 'KAZ', 1],
1305
-            [116, 'Kenya', 'KE', 'KEN', 1],
1306
-            [117, 'Kyrgyzstan', 'KG', 'KGZ', 1],
1307
-            [118, 'Kiribati', 'KI', 'KIR', 1],
1308
-            [48, 'South Korea', 'KR', 'KOR', 1],
1309
-            [228, 'Kosovo', 'XK', 'XKV', 2],
1310
-            // there is no official ISO code for Kosovo yet (http://geonames.wordpress.com/2010/03/08/xk-country-code-for-kosovo/) so using a temporary country code and a modified 3 character code for ISO code -- this should be updated if/when Kosovo gets its own ISO code
1311
-            [119, 'Kuwait', 'KW', 'KWT', 1],
1312
-            [120, 'Laos', 'LA', 'LAO', 1],
1313
-            [121, 'Latvia', 'LV', 'LVA', 2],
1314
-            [122, 'Lesotho', 'LS', 'LSO', 1],
1315
-            [123, 'Lebanon', 'LB', 'LBN', 1],
1316
-            [124, 'Liberia', 'LR', 'LBR', 1],
1317
-            [125, 'Libya', 'LY', 'LBY', 1],
1318
-            [126, 'Liechtenstein', 'LI', 'LIE', 1],
1319
-            [127, 'Lithuania', 'LT', 'LTU', 2],
1320
-            [128, 'Luxemburg', 'LU', 'LUX', 2],
1321
-            [129, 'Macao', 'MO', 'MAC', 1],
1322
-            [130, 'Macedonia', 'MK', 'MKD', 1],
1323
-            [131, 'Madagascar', 'MG', 'MDG', 1],
1324
-            [132, 'Malaysia', 'MY', 'MYS', 1],
1325
-            [133, 'Malawi', 'MW', 'MWI', 1],
1326
-            [134, 'Maldivas', 'MV', 'MDV', 1],
1327
-            [135, 'Mali', 'ML', 'MLI', 1],
1328
-            [136, 'Malta', 'MT', 'MLT', 2],
1329
-            [101, 'Northern Marianas', 'MP', 'MNP', 1],
1330
-            [137, 'Morocco', 'MA', 'MAR', 1],
1331
-            [104, 'Marshall islands', 'MH', 'MHL', 1],
1332
-            [138, 'Martinique', 'MQ', 'MTQ', 1],
1333
-            [139, 'Mauritius', 'MU', 'MUS', 1],
1334
-            [140, 'Mauritania', 'MR', 'MRT', 1],
1335
-            [141, 'Mayote', 'YT', 'MYT', 2],
1336
-            [142, 'Mexico', 'MX', 'MEX', 1],
1337
-            [143, 'Micronesia', 'FM', 'FSM', 1],
1338
-            [144, 'Moldova', 'MD', 'MDA', 1],
1339
-            [145, 'Monaco', 'MC', 'MCO', 2],
1340
-            [146, 'Mongolia', 'MN', 'MNG', 1],
1341
-            [147, 'Montserrat', 'MS', 'MSR', 1],
1342
-            [227, 'Montenegro', 'ME', 'MNE', 2],
1343
-            [148, 'Mozambique', 'MZ', 'MOZ', 1],
1344
-            [149, 'Myanmar', 'MM', 'MMR', 1],
1345
-            [150, 'Namibia', 'NA', 'NAM', 1],
1346
-            [151, 'Nauru', 'NR', 'NRU', 1],
1347
-            [152, 'Nepal', 'NP', 'NPL', 1],
1348
-            [9, 'Netherlands Antilles', 'AN', 'ANT', 1],
1349
-            [153, 'Nicaragua', 'NI', 'NIC', 1],
1350
-            [154, 'Niger', 'NE', 'NER', 1],
1351
-            [155, 'Nigeria', 'NG', 'NGA', 1],
1352
-            [156, 'Niue', 'NU', 'NIU', 1],
1353
-            [157, 'Norway', 'NO', 'NOR', 1],
1354
-            [158, 'New Caledonia', 'NC', 'NCL', 1],
1355
-            [159, 'New Zealand', 'NZ', 'NZL', 1],
1356
-            [160, 'Oman', 'OM', 'OMN', 1],
1357
-            [161, 'Pakistan', 'PK', 'PAK', 1],
1358
-            [162, 'Palau', 'PW', 'PLW', 1],
1359
-            [163, 'Panama', 'PA', 'PAN', 1],
1360
-            [164, 'Papua New Guinea', 'PG', 'PNG', 1],
1361
-            [165, 'Paraguay', 'PY', 'PRY', 1],
1362
-            [166, 'Peru', 'PE', 'PER', 1],
1363
-            [68, 'Philippines', 'PH', 'PHL', 1],
1364
-            [167, 'Poland', 'PL', 'POL', 1],
1365
-            [168, 'Portugal', 'PT', 'PRT', 2],
1366
-            [169, 'Puerto Rico', 'PR', 'PRI', 1],
1367
-            [170, 'Qatar', 'QA', 'QAT', 1],
1368
-            [176, 'Rwanda', 'RW', 'RWA', 1],
1369
-            [177, 'Romania', 'RO', 'ROM', 2],
1370
-            [178, 'Russia', 'RU', 'RUS', 1],
1371
-            [229, 'Saint Pierre and Miquelon', 'PM', 'SPM', 2],
1372
-            [180, 'Samoa', 'WS', 'WSM', 1],
1373
-            [181, 'American Samoa', 'AS', 'ASM', 1],
1374
-            [183, 'San Marino', 'SM', 'SMR', 2],
1375
-            [184, 'Saint Vincent and the Grenadines', 'VC', 'VCT', 1],
1376
-            [185, 'Saint Helena', 'SH', 'SHN', 1],
1377
-            [186, 'Saint Lucia', 'LC', 'LCA', 1],
1378
-            [188, 'Senegal', 'SN', 'SEN', 1],
1379
-            [189, 'Seychelles', 'SC', 'SYC', 1],
1380
-            [190, 'Sierra Leona', 'SL', 'SLE', 1],
1381
-            [191, 'Singapore', 'SG', 'SGP', 1],
1382
-            [192, 'Syria', 'SY', 'SYR', 1],
1383
-            [193, 'Somalia', 'SO', 'SOM', 1],
1384
-            [194, 'Sri Lanka', 'LK', 'LKA', 1],
1385
-            [195, 'South Africa', 'ZA', 'ZAF', 1],
1386
-            [196, 'Sudan', 'SD', 'SDN', 1],
1387
-            [199, 'Suriname', 'SR', 'SUR', 1],
1388
-            [200, 'Swaziland', 'SZ', 'SWZ', 1],
1389
-            [201, 'Thailand', 'TH', 'THA', 1],
1390
-            [202, 'Taiwan', 'TW', 'TWN', 1],
1391
-            [203, 'Tanzania', 'TZ', 'TZA', 1],
1392
-            [204, 'Tajikistan', 'TJ', 'TJK', 1],
1393
-            [206, 'Timor-Leste', 'TL', 'TLS', 1],
1394
-            [207, 'Togo', 'TG', 'TGO', 1],
1395
-            [208, 'Tokelau', 'TK', 'TKL', 1],
1396
-            [209, 'Tonga', 'TO', 'TON', 1],
1397
-            [210, 'Trinidad and Tobago', 'TT', 'TTO', 1],
1398
-            [211, 'Tunisia', 'TN', 'TUN', 1],
1399
-            [212, 'Turkmenistan', 'TM', 'TKM', 1],
1400
-            [213, 'Turkey', 'TR', 'TUR', 1],
1401
-            [214, 'Tuvalu', 'TV', 'TUV', 1],
1402
-            [215, 'Ukraine', 'UA', 'UKR', 1],
1403
-            [216, 'Uganda', 'UG', 'UGA', 1],
1404
-            [59, 'United Arab Emirates', 'AE', 'ARE', 1],
1405
-            [217, 'Uruguay', 'UY', 'URY', 1],
1406
-            [218, 'Uzbekistan', 'UZ', 'UZB', 1],
1407
-            [219, 'Vanuatu', 'VU', 'VUT', 1],
1408
-            [220, 'Vatican City', 'VA', 'VAT', 2],
1409
-            [221, 'Venezuela', 'VE', 'VEN', 1],
1410
-            [222, 'Vietnam', 'VN', 'VNM', 1],
1411
-            [108, 'Virgin Islands', 'VI', 'VIR', 1],
1412
-            [223, 'Yemen', 'YE', 'YEM', 1],
1413
-            [225, 'Zambia', 'ZM', 'ZMB', 1],
1414
-            [226, 'Zimbabwe', 'ZW', 'ZWE', 1],
1415
-        ];
1416
-        $country_iso   = 'US';
1417
-        foreach ($old_countries as $country_array) {
1418
-            // note: index 0 is the 3.1 country ID
1419
-            if ($country_array[0] == $country_id) {
1420
-                // note: index 2 is the ISO
1421
-                $country_iso = $country_array[2];
1422
-                break;
1423
-            }
1424
-        }
1425
-        return $country_iso;
1426
-    }
1427
-
1428
-
1429
-    /**
1430
-     * Gets the ISO3 for the
1431
-     *
1432
-     * @return string
1433
-     */
1434
-    public function get_default_country_iso(): string
1435
-    {
1436
-        $old_org_options = get_option('events_organization_settings');
1437
-        return $this->get_iso_from_3_1_country_id($old_org_options['organization_country']);
1438
-    }
1439
-
1440
-
1441
-    /**
1442
-     * Converts a 3.1 payment status to its equivalent 4.1 registration status
1443
-     *
1444
-     * @param string  $payment_status                   possible value for 3.1's evens_attendee.payment_status
1445
-     * @param boolean $this_thing_required_pre_approval whether the thing we're considering (the general setting's
1446
-     *                                                  default payment status, the event's default payment status, or
1447
-     *                                                  the attendee's payment status) required pre-approval.
1448
-     * @return string STS_ID for use in 4.1
1449
-     */
1450
-    public function convert_3_1_payment_status_to_4_1_STS_ID(
1451
-        string $payment_status,
1452
-        bool $this_thing_required_pre_approval = false
1453
-    ): string {
1454
-        // EE team can read the related discussion: https://app.asana.com/0/2400967562914/9418495544455
1455
-        if ($this_thing_required_pre_approval) {
1456
-            return 'RNA';
1457
-        } else {
1458
-            $mapping = [
1459
-                'Completed'        => 'RAP',
1460
-                ''                 => 'RPP',
1461
-                'Incomplete'       => 'RPP',
1462
-                'Pending'          => 'RAP',
1463
-                // stati that only occurred on 3.1 attendees:
1464
-                'Payment Declined' => 'RPP',
1465
-                'Not Completed'    => 'RPP',
1466
-                'Cancelled'        => 'RPP',
1467
-                'Declined'         => 'RPP',
1468
-            ];
1469
-        }
1470
-        return $mapping[ $payment_status ] ?? 'RNA';
1471
-    }
1472
-
1473
-
1474
-    /**
1475
-     * Makes sure the 3.1's image url is converted to an image attachment post to the 4.1 CPT event
1476
-     * and sets it as the featured image on the CPT event
1477
-     *
1478
-     * @param string                         $guid
1479
-     * @param int                            $new_cpt_id
1480
-     * @param EE_Data_Migration_Script_Stage $migration_stage the stage which called this, where errors should be added
1481
-     * @return boolean whether we had to do the big job of creating an image attachment
1482
-     */
1483
-    public function convert_image_url_to_attachment_and_attach_to_post(
1484
-        string $guid,
1485
-        int $new_cpt_id,
1486
-        EE_Data_Migration_Script_Stage $migration_stage
1487
-    ): bool {
1488
-        $created_attachment_post = false;
1489
-        $guid                    = $this->_get_original_guid($guid);
1490
-        if ($guid) {
1491
-            // check for an existing attachment post with this guid
1492
-            $attachment_post_id = $this->_get_image_attachment_id_by_GUID($guid);
1493
-            if (! $attachment_post_id) {
1494
-                // post thumbnail with that GUID doesn't exist, we should create one
1495
-                $attachment_post_id      = $this->_create_image_attachment_from_GUID($guid, $migration_stage);
1496
-                $created_attachment_post = true;
1497
-            }
1498
-            // double-check we actually have an attachment post
1499
-            if ($attachment_post_id) {
1500
-                update_post_meta($new_cpt_id, '_thumbnail_id', $attachment_post_id);
1501
-            } else {
1502
-                $migration_stage->add_error(
1503
-                    sprintf(
1504
-                        esc_html__(
1505
-                            "Could not update event image %s for CPT with ID %d, but attachments post ID is %d",
1506
-                            "event_espresso"
1507
-                        ),
1508
-                        $guid,
1509
-                        $new_cpt_id,
1510
-                        $attachment_post_id
1511
-                    )
1512
-                );
1513
-            }
1514
-        }
1515
-        return $created_attachment_post;
1516
-    }
1517
-
1518
-
1519
-    /**
1520
-     * In 3.1, the event thumbnail image DOESN'T point to the original image, but instead
1521
-     * to a large thumbnail (which has nearly the same GUID, except it adds "-{width}x{height}" before the filetype,
1522
-     * or whatever dimensions it is. Eg 'http://mysite.com/image1-300x400.jpg' instead of
1523
-     * 'http://mysite.com/image1.jpg' ). This function attempts to strip that off and get the original file, if it
1524
-     * exists
1525
-     *
1526
-     * @param string $guid_in_old_event
1527
-     * @return string either the original guid, or $guid_in_old_event if we couldn't figure out what the original was
1528
-     */
1529
-    private function _get_original_guid(string $guid_in_old_event): string
1530
-    {
1531
-        $original_guid = preg_replace('~-\d*x\d*\.~', '.', $guid_in_old_event, 1);
1532
-        // do a head request to verify the file exists
1533
-        $head_response = wp_remote_head($original_guid);
1534
-        if (! $head_response instanceof WP_Error && $head_response['response']['message'] == 'OK') {
1535
-            return $original_guid;
1536
-        }
1537
-        return $guid_in_old_event;
1538
-    }
1539
-
1540
-
1541
-    /**
1542
-     * Creates an image attachment post for the GUID. If the GUID points to a remote image,
1543
-     * we download it to our uploads directory so that it can be properly processed (eg, creates different sizes of
1544
-     * thumbnails)
1545
-     *
1546
-     * @param string                         $guid
1547
-     * @param EE_Data_Migration_Script_Stage $migration_stage
1548
-     * @return int
1549
-     */
1550
-    private function _create_image_attachment_from_GUID(string $guid, EE_Data_Migration_Script_Stage $migration_stage)
1551
-    {
1552
-        if (! $guid) {
1553
-            $migration_stage->add_error(
1554
-                esc_html__(
1555
-                    "Cannot create image attachment for a blank GUID!",
1556
-                    "event_espresso"
1557
-                )
1558
-            );
1559
-            return 0;
1560
-        }
1561
-        $wp_filetype   = wp_check_filetype(basename($guid));
1562
-        $wp_upload_dir = wp_upload_dir();
1563
-        // if the file is located remotely, download it to our uploads DIR, because wp_generate_attachment_metadata
1564
-        // needs the file to be local
1565
-        if (strpos($guid, $wp_upload_dir['url']) === false) {
1566
-            // image is located remotely. download it and place it in the uploads directory
1567
-            if (! is_readable($guid)) {
1568
-                $migration_stage->add_error(
1569
-                    sprintf(
1570
-                        esc_html__(
1571
-                            "Could not create image attachment from non-existent file: %s",
1572
-                            "event_espresso"
1573
-                        ),
1574
-                        $guid
1575
-                    )
1576
-                );
1577
-                return 0;
1578
-            }
1579
-            $contents = file_get_contents($guid);
1580
-            if ($contents === false) {
1581
-                $migration_stage->add_error(
1582
-                    sprintf(
1583
-                        esc_html__(
1584
-                            "Could not read image at %s, and therefore couldn't create an attachment post for it.",
1585
-                            "event_espresso"
1586
-                        ),
1587
-                        $guid
1588
-                    )
1589
-                );
1590
-                return false;
1591
-            }
1592
-            $local_filepath = $wp_upload_dir['path'] . '/' . basename($guid);
1593
-            $save_file      = fopen($local_filepath, 'w');
1594
-            fwrite($save_file, $contents);
1595
-            fclose($save_file);
1596
-            $guid = str_replace($wp_upload_dir['path'], $wp_upload_dir['url'], $local_filepath);
1597
-        } else {
1598
-            $local_filepath = str_replace($wp_upload_dir['url'], $wp_upload_dir['path'], $guid);
1599
-        }
1600
-        $attachment = [
1601
-            'guid'           => $guid,
1602
-            'post_mime_type' => $wp_filetype['type'],
1603
-            'post_title'     => preg_replace('/\.[^.]+$/', '', basename($guid)),
1604
-            'post_content'   => '',
1605
-            'post_status'    => 'inherit',
1606
-        ];
1607
-        $attach_id  = wp_insert_attachment($attachment, $guid);
1608
-        if (! $attach_id) {
1609
-            $migration_stage->add_error(
1610
-                sprintf(
1611
-                    esc_html__(
1612
-                        "Could not create image attachment post from image '%s'. Attachment data was %s.",
1613
-                        "event_espresso"
1614
-                    ),
1615
-                    $guid,
1616
-                    $this->_json_encode($attachment)
1617
-                )
1618
-            );
1619
-            return $attach_id;
1620
-        }
1621
-        // you must first include the image.php file
1622
-        // for the function wp_generate_attachment_metadata() to work
1623
-        require_once(ABSPATH . 'wp-admin/includes/image.php');
1624
-        $attach_data = wp_generate_attachment_metadata($attach_id, $local_filepath);
1625
-        if (! $attach_data) {
1626
-            $migration_stage->add_error(
1627
-                sprintf(
1628
-                    esc_html__(
1629
-                        "Could not generate attachment metadata for attachment post %d with filepath %s and GUID %s. Please check the file was downloaded properly.",
1630
-                        "event_espresso"
1631
-                    ),
1632
-                    $attach_id,
1633
-                    $local_filepath,
1634
-                    $guid
1635
-                )
1636
-            );
1637
-            return $attach_id;
1638
-        }
1639
-        $metadata_save_result = wp_update_attachment_metadata($attach_id, $attach_data);
1640
-        if (! $metadata_save_result) {
1641
-            $migration_stage->add_error(
1642
-                sprintf(
1643
-                    esc_html__(
1644
-                        "Could not update attachment metadata for attachment %d with data %s",
1645
-                        "event_espresso"
1646
-                    ),
1647
-                    $attach_id,
1648
-                    $this->_json_encode($attach_data)
1649
-                )
1650
-            );
1651
-        }
1652
-        return $attach_id;
1653
-    }
1654
-
1655
-
1656
-    /**
1657
-     * Finds the attachment post containing info about an image attachment given the GUID (link to the image itself),
1658
-     * and returns its ID.
1659
-     *
1660
-     * @param string $guid
1661
-     * @return int
1662
-     * @global wpdb  $wpdb
1663
-     */
1664
-    private function _get_image_attachment_id_by_GUID(string $guid): int
1665
-    {
1666
-        global $wpdb;
1667
-        return (int) $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid=%s LIMIT 1", $guid));
1668
-    }
1669
-
1670
-
1671
-    /**
1672
-     * Returns a mysql-formatted DATETIME in UTC time, given a $DATETIME_string
1673
-     * (and optionally a timezone; if none is given, the wp default is used)
1674
-     *
1675
-     * @param EE_Data_Migration_Script_Stage $stage
1676
-     * @param array                          $row_of_data , the row from the DB (as an array) we're trying to find the
1677
-     *                                                    UTC time for
1678
-     * @param string                         $DATETIME_string
1679
-     * @param string|null                    $timezone
1680
-     * @return string
1681
-     */
1682
-    public function convert_date_string_to_utc(
1683
-        EE_Data_Migration_Script_Stage $stage,
1684
-        array $row_of_data,
1685
-        string $DATETIME_string,
1686
-        ?string $timezone = null
1687
-    ): string {
1688
-        $original_tz = $timezone;
1689
-        if (! $timezone) {
1690
-            $timezone = $this->_get_wp_timezone();
1691
-        }
1692
-        if (! $timezone) {
1693
-            $stage->add_error(
1694
-                sprintf(
1695
-                    esc_html__("Could not find timezone given %s for %s", "event_espresso"),
1696
-                    $original_tz,
1697
-                    $row_of_data
1698
-                )
1699
-            );
1700
-            $timezone = 'UTC';
1701
-        }
1702
-        try {
1703
-            $date_obj = new DateTime($DATETIME_string, new DateTimeZone($timezone));
1704
-            EEH_DTT_Helper::setTimezone($date_obj, new DateTimeZone('UTC'));
1705
-        } catch (Exception $e) {
1706
-            $stage->add_error(
1707
-                sprintf(
1708
-                    esc_html__(
1709
-                        "Could not convert time string '%s' using timezone '%s' into a proper DATETIME. Using current time instead.",
1710
-                        "event_espresso"
1711
-                    ),
1712
-                    $DATETIME_string,
1713
-                    $timezone
1714
-                )
1715
-            );
1716
-            $date_obj = new DateTime();
1717
-        }
1718
-        return $date_obj->format('Y-m-d H:i:s');
1719
-    }
1720
-
1721
-
1722
-    /**
1723
-     * Gets the default timezone string from wordpress (even if they set a gmt offset)
1724
-     *
1725
-     * @return string
1726
-     */
1727
-    private function _get_wp_timezone()
1728
-    {
1729
-        $timezone = empty($timezone) ? get_option('timezone_string') : $timezone;
1730
-        // if timezone is STILL empty then let's get the GMT offset and then set the timezone_string using our converter
1731
-        if (empty($timezone)) {
1732
-            // let's get a the WordPress UTC offset
1733
-            $offset   = (int) get_option('gmt_offset');
1734
-            $timezone = $this->timezone_convert_to_string_from_offset($offset);
1735
-        }
1736
-        return $timezone;
1737
-    }
1738
-
1739
-
1740
-    /**
1741
-     * Gets the wordpress timezone string from a UTC offset
1742
-     *
1743
-     * @param int $offset
1744
-     * @return boolean
1745
-     */
1746
-    private function timezone_convert_to_string_from_offset(int $offset): bool
1747
-    {
1748
-        // shamelessly taken from bottom comment at http://ca1.php.net/manual/en/function.timezone-name-from-abbr.php because timezone_name_from_abbr() did not work as expected - its not reliable
1749
-        $offset        *= 3600; // convert hour offset to seconds
1750
-        $abbreviations = timezone_abbreviations_list();
1751
-        foreach ($abbreviations as $abbreviation) {
1752
-            foreach ($abbreviation as $city) {
1753
-                if ($city['offset'] == $offset) {
1754
-                    return $city['timezone_id'];
1755
-                }
1756
-            }
1757
-        }
1758
-        return false;
1759
-    }
1760
-
1761
-
1762
-    public function migration_page_hooks()
1763
-    {
1764
-        add_filter(
1765
-            'FHEE__ee_migration_page__header',
1766
-            [$this, '_migrate_page_hook_simplify_version_strings'],
1767
-            10,
1768
-            3
1769
-        );
1770
-        add_filter(
1771
-            'FHEE__ee_migration_page__p_after_header',
1772
-            [$this, '_migration_page_hook_simplify_next_db_state'],
1773
-            10,
1774
-            2
1775
-        );
1776
-        add_filter(
1777
-            'FHEE__ee_migration_page__option_1_main',
1778
-            [$this, '_migrate_page_hook_simplify_version_strings'],
1779
-            10,
1780
-            3
1781
-        );
1782
-        add_filter(
1783
-            'FHEE__ee_migration_page__option_1_button_text',
1784
-            [$this, '_migrate_page_hook_simplify_version_strings'],
1785
-            10,
1786
-            3
1787
-        );
1788
-        add_action(
1789
-            'AHEE__ee_migration_page__option_1_extra_details',
1790
-            [$this, '_migration_page_hook_option_1_extra_details'],
1791
-            10,
1792
-            3
1793
-        );
1794
-        add_filter(
1795
-            'FHEE__ee_migration_page__option_2_main',
1796
-            [$this, '_migrate_page_hook_simplify_version_strings'],
1797
-            10,
1798
-            4
1799
-        );
1800
-        add_filter(
1801
-            'FHEE__ee_migration_page__option_2_button_text',
1802
-            [$this, '_migration_page_hook_simplify_next_db_state'],
1803
-            10,
1804
-            2
1805
-        );
1806
-        add_filter(
1807
-            'FHEE__ee_migration_page__option_2_details',
1808
-            [$this, '_migration_page_hook_simplify_next_db_state'],
1809
-            10,
1810
-            2
1811
-        );
1812
-        add_action(
1813
-            'AHEE__ee_migration_page__after_migration_options_table',
1814
-            [$this, '_migration_page_hook_after_migration_options_table']
1815
-        );
1816
-        add_filter(
1817
-            'FHEE__ee_migration_page__done_migration_header',
1818
-            [$this, '_migration_page_hook_simplify_next_db_state'],
1819
-            10,
1820
-            2
1821
-        );
1822
-        add_filter(
1823
-            'FHEE__ee_migration_page__p_after_done_migration_header',
1824
-            [$this, '_migration_page_hook_simplify_next_db_state'],
1825
-            10,
1826
-            2
1827
-        );
1828
-        add_filter(
1829
-            'FHEE__ee_migration_page__migration_options_template',
1830
-            [$this, 'use_migration_options_from_ee3_template']
1831
-        );
1832
-    }
1833
-
1834
-
1835
-    public function _migrate_page_hook_simplify_version_strings(
1836
-        $old_content,
1837
-        $current_db_state,
1838
-        $next_db_state,
1839
-        $ultimate_db_state = null
1840
-    ) {
1841
-        return str_replace(
1842
-            [$current_db_state, $next_db_state, $ultimate_db_state],
1843
-            [
1844
-                esc_html__('EE3', 'event_espresso'),
1845
-                esc_html__('EE4', 'event_espresso'),
1846
-                esc_html__("EE4", 'event_espresso'),
1847
-            ],
1848
-            $old_content
1849
-        );
1850
-    }
1851
-
1852
-
1853
-    public function _migration_page_hook_simplify_next_db_state($old_content, $next_db_state)
1854
-    {
1855
-        return str_replace($next_db_state, esc_html__("EE4", 'event_espresso'), $old_content);
1856
-    }
1857
-
1858
-
1859
-    public function _migration_page_hook_option_1_extra_details()
1860
-    {
1861
-        ?>
1116
+				$state_name,
1117
+				$state_name,
1118
+				$country_iso
1119
+			),
1120
+			ARRAY_A
1121
+		);
1122
+		if (! $state) {
1123
+			// insert a new one then
1124
+			$cols_n_values = [
1125
+				'CNT_ISO'    => $country_iso,
1126
+				'STA_abbrev' => substr($state_name, 0, 6),
1127
+				'STA_name'   => $state_name,
1128
+				'STA_active' => true,
1129
+			];
1130
+			$data_types    = [
1131
+				'%s',// CNT_ISO
1132
+				'%s',// STA_abbrev
1133
+				'%s',// STA_name
1134
+				'%d',// STA_active
1135
+			];
1136
+			$success       = $wpdb->insert($state_table, $cols_n_values, $data_types);
1137
+			if (! $success) {
1138
+				throw new EE_Error(
1139
+					$this->_create_error_message_for_db_insertion(
1140
+						'N/A',
1141
+						['state' => $state_name, 'country_id' => $country_name],
1142
+						$state_table,
1143
+						$cols_n_values,
1144
+						$data_types
1145
+					)
1146
+				);
1147
+			}
1148
+			$state           = $cols_n_values;
1149
+			$state['STA_ID'] = $wpdb->insert_id;
1150
+		}
1151
+		return $state;
1152
+	}
1153
+
1154
+
1155
+	/**
1156
+	 * Fixes times like "5:00 PM" into the expected 24-hour format "17:00".
1157
+	 * THis is actually just copied from the 3.1 JSON API because it needed to do the exact same thing
1158
+	 *
1159
+	 * @param string|null $timeString
1160
+	 * @return string in the php DATETIME format: "G:i" (24-hour format hour with leading zeros, a colon, and minutes
1161
+	 *                with leading zeros)
1162
+	 */
1163
+	public function convertTimeFromAMPM(?string $timeString): string
1164
+	{
1165
+		$matches = [];
1166
+		preg_match("~(\\d*):(\\d*)~", $timeString, $matches);
1167
+		if (! $matches || count($matches) < 3) {
1168
+			$hour    = '00';
1169
+			$minutes = '00';
1170
+		} else {
1171
+			$hour    = intval($matches[1]);
1172
+			$minutes = $matches[2];
1173
+		}
1174
+		if (strpos($timeString, 'PM') || strpos($timeString, 'pm')) {
1175
+			$hour = intval($hour) + 12;
1176
+		}
1177
+		$hour    = str_pad("$hour", 2, '0', STR_PAD_LEFT);
1178
+		$minutes = str_pad("$minutes", 2, '0', STR_PAD_LEFT);
1179
+		return "$hour:$minutes";
1180
+	}
1181
+
1182
+
1183
+	/**
1184
+	 * Gets the ISO3 fora country given its 3.1 country ID.
1185
+	 *
1186
+	 * @param int $country_id
1187
+	 * @return string the country's ISO3 code
1188
+	 */
1189
+	public function get_iso_from_3_1_country_id(int $country_id): string
1190
+	{
1191
+		$old_countries = [
1192
+			[64, 'United States', 'US', 'USA', 1],
1193
+			[15, 'Australia', 'AU', 'AUS', 1],
1194
+			[39, 'Canada', 'CA', 'CAN', 1],
1195
+			[171, 'United Kingdom', 'GB', 'GBR', 1],
1196
+			[70, 'France', 'FR', 'FRA', 2],
1197
+			[111, 'Italy', 'IT', 'ITA', 2],
1198
+			[63, 'Spain', 'ES', 'ESP', 2],
1199
+			[1, 'Afghanistan', 'AF', 'AFG', 1],
1200
+			[2, 'Albania', 'AL', 'ALB', 1],
1201
+			[3, 'Germany', 'DE', 'DEU', 2],
1202
+			[198, 'Switzerland', 'CH', 'CHE', 1],
1203
+			[87, 'Netherlands', 'NL', 'NLD', 2],
1204
+			[197, 'Sweden', 'SE', 'SWE', 1],
1205
+			[230, 'Akrotiri and Dhekelia', 'CY', 'CYP', 2],
1206
+			[4, 'Andorra', 'AD', 'AND', 2],
1207
+			[5, 'Angola', 'AO', 'AGO', 1],
1208
+			[6, 'Anguilla', 'AI', 'AIA', 1],
1209
+			[7, 'Antarctica', 'AQ', 'ATA', 1],
1210
+			[8, 'Antigua and Barbuda', 'AG', 'ATG', 1],
1211
+			[10, 'Saudi Arabia', 'SA', 'SAU', 1],
1212
+			[11, 'Algeria', 'DZ', 'DZA', 1],
1213
+			[12, 'Argentina', 'AR', 'ARG', 1],
1214
+			[13, 'Armenia', 'AM', 'ARM', 1],
1215
+			[14, 'Aruba', 'AW', 'ABW', 1],
1216
+			[16, 'Austria', 'AT', 'AUT', 2],
1217
+			[17, 'Azerbaijan', 'AZ', 'AZE', 1],
1218
+			[18, 'Bahamas', 'BS', 'BHS', 1],
1219
+			[19, 'Bahrain', 'BH', 'BHR', 1],
1220
+			[20, 'Bangladesh', 'BD', 'BGD', 1],
1221
+			[21, 'Barbados', 'BB', 'BRB', 1],
1222
+			[22, 'Belgium ', 'BE', 'BEL', 2],
1223
+			[23, 'Belize', 'BZ', 'BLZ', 1],
1224
+			[24, 'Benin', 'BJ', 'BEN', 1],
1225
+			[25, 'Bermudas', 'BM', 'BMU', 1],
1226
+			[26, 'Belarus', 'BY', 'BLR', 1],
1227
+			[27, 'Bolivia', 'BO', 'BOL', 1],
1228
+			[28, 'Bosnia and Herzegovina', 'BA', 'BIH', 1],
1229
+			[29, 'Botswana', 'BW', 'BWA', 1],
1230
+			[96, 'Bouvet Island', 'BV', 'BVT', 1],
1231
+			[30, 'Brazil', 'BR', 'BRA', 1],
1232
+			[31, 'Brunei', 'BN', 'BRN', 1],
1233
+			[32, 'Bulgaria', 'BG', 'BGR', 1],
1234
+			[33, 'Burkina Faso', 'BF', 'BFA', 1],
1235
+			[34, 'Burundi', 'BI', 'BDI', 1],
1236
+			[35, 'Bhutan', 'BT', 'BTN', 1],
1237
+			[36, 'Cape Verde', 'CV', 'CPV', 1],
1238
+			[37, 'Cambodia', 'KH', 'KHM', 1],
1239
+			[38, 'Cameroon', 'CM', 'CMR', 1],
1240
+			[98, 'Cayman Islands', 'KY', 'CYM', 1],
1241
+			[172, 'Central African Republic', 'CF', 'CAF', 1],
1242
+			[40, 'Chad', 'TD', 'TCD', 1],
1243
+			[41, 'Chile', 'CL', 'CHL', 1],
1244
+			[42, 'China', 'CN', 'CHN', 1],
1245
+			[105, 'Christmas Island', 'CX', 'CXR', 1],
1246
+			[43, 'Cyprus', 'CY', 'CYP', 2],
1247
+			[99, 'Cocos Island', 'CC', 'CCK', 1],
1248
+			[100, 'Cook Islands', 'CK', 'COK', 1],
1249
+			[44, 'Colombia', 'CO', 'COL', 1],
1250
+			[45, 'Comoros', 'KM', 'COM', 1],
1251
+			[46, 'Congo', 'CG', 'COG', 1],
1252
+			[47, 'North Korea', 'KP', 'PRK', 1],
1253
+			[50, 'Costa Rica', 'CR', 'CRI', 1],
1254
+			[51, 'Croatia', 'HR', 'HRV', 1],
1255
+			[52, 'Cuba', 'CU', 'CUB', 1],
1256
+			[173, 'Czech Republic', 'CZ', 'CZE', 1],
1257
+			[53, 'Denmark', 'DK', 'DNK', 1],
1258
+			[54, 'Djibouti', 'DJ', 'DJI', 1],
1259
+			[55, 'Dominica', 'DM', 'DMA', 1],
1260
+			[174, 'Dominican Republic', 'DO', 'DOM', 1],
1261
+			[56, 'Ecuador', 'EC', 'ECU', 1],
1262
+			[57, 'Egypt', 'EG', 'EGY', 1],
1263
+			[58, 'El Salvador', 'SV', 'SLV', 1],
1264
+			[60, 'Eritrea', 'ER', 'ERI', 1],
1265
+			[61, 'Slovakia', 'SK', 'SVK', 2],
1266
+			[62, 'Slovenia', 'SI', 'SVN', 2],
1267
+			[65, 'Estonia', 'EE', 'EST', 2],
1268
+			[66, 'Ethiopia', 'ET', 'ETH', 1],
1269
+			[102, 'Faroe islands', 'FO', 'FRO', 1],
1270
+			[103, 'Falkland Islands', 'FK', 'FLK', 1],
1271
+			[67, 'Fiji', 'FJ', 'FJI', 1],
1272
+			[69, 'Finland', 'FI', 'FIN', 2],
1273
+			[71, 'Gabon', 'GA', 'GAB', 1],
1274
+			[72, 'Gambia', 'GM', 'GMB', 1],
1275
+			[73, 'Georgia', 'GE', 'GEO', 1],
1276
+			[74, 'Ghana', 'GH', 'GHA', 1],
1277
+			[75, 'Gibraltar', 'GI', 'GIB', 1],
1278
+			[76, 'Greece', 'GR', 'GRC', 2],
1279
+			[77, 'Grenada', 'GD', 'GRD', 1],
1280
+			[78, 'Greenland', 'GL', 'GRL', 1],
1281
+			[79, 'Guadeloupe', 'GP', 'GLP', 1],
1282
+			[80, 'Guam', 'GU', 'GUM', 1],
1283
+			[81, 'Guatemala', 'GT', 'GTM', 1],
1284
+			[82, 'Guinea', 'GN', 'GIN', 1],
1285
+			[83, 'Equatorial Guinea', 'GQ', 'GNQ', 1],
1286
+			[84, 'Guinea-Bissau', 'GW', 'GNB', 1],
1287
+			[85, 'Guyana', 'GY', 'GUY', 1],
1288
+			[86, 'Haiti', 'HT', 'HTI', 1],
1289
+			[88, 'Honduras', 'HN', 'HND', 1],
1290
+			[89, 'Hong Kong', 'HK', 'HKG', 1],
1291
+			[90, 'Hungary', 'HU', 'HUN', 1],
1292
+			[91, 'India', 'IN', 'IND', 1],
1293
+			[205, 'British Indian Ocean Territory', 'IO', 'IOT', 1],
1294
+			[92, 'Indonesia', 'ID', 'IDN', 1],
1295
+			[93, 'Iraq', 'IQ', 'IRQ', 1],
1296
+			[94, 'Iran', 'IR', 'IRN', 1],
1297
+			[95, 'Ireland', 'IE', 'IRL', 2],
1298
+			[97, 'Iceland', 'IS', 'ISL', 1],
1299
+			[110, 'Israel', 'IL', 'ISR', 1],
1300
+			[49, 'Ivory Coast ', 'CI', 'CIV', 1],
1301
+			[112, 'Jamaica', 'JM', 'JAM', 1],
1302
+			[113, 'Japan', 'JP', 'JPN', 1],
1303
+			[114, 'Jordan', 'JO', 'JOR', 1],
1304
+			[115, 'Kazakhstan', 'KZ', 'KAZ', 1],
1305
+			[116, 'Kenya', 'KE', 'KEN', 1],
1306
+			[117, 'Kyrgyzstan', 'KG', 'KGZ', 1],
1307
+			[118, 'Kiribati', 'KI', 'KIR', 1],
1308
+			[48, 'South Korea', 'KR', 'KOR', 1],
1309
+			[228, 'Kosovo', 'XK', 'XKV', 2],
1310
+			// there is no official ISO code for Kosovo yet (http://geonames.wordpress.com/2010/03/08/xk-country-code-for-kosovo/) so using a temporary country code and a modified 3 character code for ISO code -- this should be updated if/when Kosovo gets its own ISO code
1311
+			[119, 'Kuwait', 'KW', 'KWT', 1],
1312
+			[120, 'Laos', 'LA', 'LAO', 1],
1313
+			[121, 'Latvia', 'LV', 'LVA', 2],
1314
+			[122, 'Lesotho', 'LS', 'LSO', 1],
1315
+			[123, 'Lebanon', 'LB', 'LBN', 1],
1316
+			[124, 'Liberia', 'LR', 'LBR', 1],
1317
+			[125, 'Libya', 'LY', 'LBY', 1],
1318
+			[126, 'Liechtenstein', 'LI', 'LIE', 1],
1319
+			[127, 'Lithuania', 'LT', 'LTU', 2],
1320
+			[128, 'Luxemburg', 'LU', 'LUX', 2],
1321
+			[129, 'Macao', 'MO', 'MAC', 1],
1322
+			[130, 'Macedonia', 'MK', 'MKD', 1],
1323
+			[131, 'Madagascar', 'MG', 'MDG', 1],
1324
+			[132, 'Malaysia', 'MY', 'MYS', 1],
1325
+			[133, 'Malawi', 'MW', 'MWI', 1],
1326
+			[134, 'Maldivas', 'MV', 'MDV', 1],
1327
+			[135, 'Mali', 'ML', 'MLI', 1],
1328
+			[136, 'Malta', 'MT', 'MLT', 2],
1329
+			[101, 'Northern Marianas', 'MP', 'MNP', 1],
1330
+			[137, 'Morocco', 'MA', 'MAR', 1],
1331
+			[104, 'Marshall islands', 'MH', 'MHL', 1],
1332
+			[138, 'Martinique', 'MQ', 'MTQ', 1],
1333
+			[139, 'Mauritius', 'MU', 'MUS', 1],
1334
+			[140, 'Mauritania', 'MR', 'MRT', 1],
1335
+			[141, 'Mayote', 'YT', 'MYT', 2],
1336
+			[142, 'Mexico', 'MX', 'MEX', 1],
1337
+			[143, 'Micronesia', 'FM', 'FSM', 1],
1338
+			[144, 'Moldova', 'MD', 'MDA', 1],
1339
+			[145, 'Monaco', 'MC', 'MCO', 2],
1340
+			[146, 'Mongolia', 'MN', 'MNG', 1],
1341
+			[147, 'Montserrat', 'MS', 'MSR', 1],
1342
+			[227, 'Montenegro', 'ME', 'MNE', 2],
1343
+			[148, 'Mozambique', 'MZ', 'MOZ', 1],
1344
+			[149, 'Myanmar', 'MM', 'MMR', 1],
1345
+			[150, 'Namibia', 'NA', 'NAM', 1],
1346
+			[151, 'Nauru', 'NR', 'NRU', 1],
1347
+			[152, 'Nepal', 'NP', 'NPL', 1],
1348
+			[9, 'Netherlands Antilles', 'AN', 'ANT', 1],
1349
+			[153, 'Nicaragua', 'NI', 'NIC', 1],
1350
+			[154, 'Niger', 'NE', 'NER', 1],
1351
+			[155, 'Nigeria', 'NG', 'NGA', 1],
1352
+			[156, 'Niue', 'NU', 'NIU', 1],
1353
+			[157, 'Norway', 'NO', 'NOR', 1],
1354
+			[158, 'New Caledonia', 'NC', 'NCL', 1],
1355
+			[159, 'New Zealand', 'NZ', 'NZL', 1],
1356
+			[160, 'Oman', 'OM', 'OMN', 1],
1357
+			[161, 'Pakistan', 'PK', 'PAK', 1],
1358
+			[162, 'Palau', 'PW', 'PLW', 1],
1359
+			[163, 'Panama', 'PA', 'PAN', 1],
1360
+			[164, 'Papua New Guinea', 'PG', 'PNG', 1],
1361
+			[165, 'Paraguay', 'PY', 'PRY', 1],
1362
+			[166, 'Peru', 'PE', 'PER', 1],
1363
+			[68, 'Philippines', 'PH', 'PHL', 1],
1364
+			[167, 'Poland', 'PL', 'POL', 1],
1365
+			[168, 'Portugal', 'PT', 'PRT', 2],
1366
+			[169, 'Puerto Rico', 'PR', 'PRI', 1],
1367
+			[170, 'Qatar', 'QA', 'QAT', 1],
1368
+			[176, 'Rwanda', 'RW', 'RWA', 1],
1369
+			[177, 'Romania', 'RO', 'ROM', 2],
1370
+			[178, 'Russia', 'RU', 'RUS', 1],
1371
+			[229, 'Saint Pierre and Miquelon', 'PM', 'SPM', 2],
1372
+			[180, 'Samoa', 'WS', 'WSM', 1],
1373
+			[181, 'American Samoa', 'AS', 'ASM', 1],
1374
+			[183, 'San Marino', 'SM', 'SMR', 2],
1375
+			[184, 'Saint Vincent and the Grenadines', 'VC', 'VCT', 1],
1376
+			[185, 'Saint Helena', 'SH', 'SHN', 1],
1377
+			[186, 'Saint Lucia', 'LC', 'LCA', 1],
1378
+			[188, 'Senegal', 'SN', 'SEN', 1],
1379
+			[189, 'Seychelles', 'SC', 'SYC', 1],
1380
+			[190, 'Sierra Leona', 'SL', 'SLE', 1],
1381
+			[191, 'Singapore', 'SG', 'SGP', 1],
1382
+			[192, 'Syria', 'SY', 'SYR', 1],
1383
+			[193, 'Somalia', 'SO', 'SOM', 1],
1384
+			[194, 'Sri Lanka', 'LK', 'LKA', 1],
1385
+			[195, 'South Africa', 'ZA', 'ZAF', 1],
1386
+			[196, 'Sudan', 'SD', 'SDN', 1],
1387
+			[199, 'Suriname', 'SR', 'SUR', 1],
1388
+			[200, 'Swaziland', 'SZ', 'SWZ', 1],
1389
+			[201, 'Thailand', 'TH', 'THA', 1],
1390
+			[202, 'Taiwan', 'TW', 'TWN', 1],
1391
+			[203, 'Tanzania', 'TZ', 'TZA', 1],
1392
+			[204, 'Tajikistan', 'TJ', 'TJK', 1],
1393
+			[206, 'Timor-Leste', 'TL', 'TLS', 1],
1394
+			[207, 'Togo', 'TG', 'TGO', 1],
1395
+			[208, 'Tokelau', 'TK', 'TKL', 1],
1396
+			[209, 'Tonga', 'TO', 'TON', 1],
1397
+			[210, 'Trinidad and Tobago', 'TT', 'TTO', 1],
1398
+			[211, 'Tunisia', 'TN', 'TUN', 1],
1399
+			[212, 'Turkmenistan', 'TM', 'TKM', 1],
1400
+			[213, 'Turkey', 'TR', 'TUR', 1],
1401
+			[214, 'Tuvalu', 'TV', 'TUV', 1],
1402
+			[215, 'Ukraine', 'UA', 'UKR', 1],
1403
+			[216, 'Uganda', 'UG', 'UGA', 1],
1404
+			[59, 'United Arab Emirates', 'AE', 'ARE', 1],
1405
+			[217, 'Uruguay', 'UY', 'URY', 1],
1406
+			[218, 'Uzbekistan', 'UZ', 'UZB', 1],
1407
+			[219, 'Vanuatu', 'VU', 'VUT', 1],
1408
+			[220, 'Vatican City', 'VA', 'VAT', 2],
1409
+			[221, 'Venezuela', 'VE', 'VEN', 1],
1410
+			[222, 'Vietnam', 'VN', 'VNM', 1],
1411
+			[108, 'Virgin Islands', 'VI', 'VIR', 1],
1412
+			[223, 'Yemen', 'YE', 'YEM', 1],
1413
+			[225, 'Zambia', 'ZM', 'ZMB', 1],
1414
+			[226, 'Zimbabwe', 'ZW', 'ZWE', 1],
1415
+		];
1416
+		$country_iso   = 'US';
1417
+		foreach ($old_countries as $country_array) {
1418
+			// note: index 0 is the 3.1 country ID
1419
+			if ($country_array[0] == $country_id) {
1420
+				// note: index 2 is the ISO
1421
+				$country_iso = $country_array[2];
1422
+				break;
1423
+			}
1424
+		}
1425
+		return $country_iso;
1426
+	}
1427
+
1428
+
1429
+	/**
1430
+	 * Gets the ISO3 for the
1431
+	 *
1432
+	 * @return string
1433
+	 */
1434
+	public function get_default_country_iso(): string
1435
+	{
1436
+		$old_org_options = get_option('events_organization_settings');
1437
+		return $this->get_iso_from_3_1_country_id($old_org_options['organization_country']);
1438
+	}
1439
+
1440
+
1441
+	/**
1442
+	 * Converts a 3.1 payment status to its equivalent 4.1 registration status
1443
+	 *
1444
+	 * @param string  $payment_status                   possible value for 3.1's evens_attendee.payment_status
1445
+	 * @param boolean $this_thing_required_pre_approval whether the thing we're considering (the general setting's
1446
+	 *                                                  default payment status, the event's default payment status, or
1447
+	 *                                                  the attendee's payment status) required pre-approval.
1448
+	 * @return string STS_ID for use in 4.1
1449
+	 */
1450
+	public function convert_3_1_payment_status_to_4_1_STS_ID(
1451
+		string $payment_status,
1452
+		bool $this_thing_required_pre_approval = false
1453
+	): string {
1454
+		// EE team can read the related discussion: https://app.asana.com/0/2400967562914/9418495544455
1455
+		if ($this_thing_required_pre_approval) {
1456
+			return 'RNA';
1457
+		} else {
1458
+			$mapping = [
1459
+				'Completed'        => 'RAP',
1460
+				''                 => 'RPP',
1461
+				'Incomplete'       => 'RPP',
1462
+				'Pending'          => 'RAP',
1463
+				// stati that only occurred on 3.1 attendees:
1464
+				'Payment Declined' => 'RPP',
1465
+				'Not Completed'    => 'RPP',
1466
+				'Cancelled'        => 'RPP',
1467
+				'Declined'         => 'RPP',
1468
+			];
1469
+		}
1470
+		return $mapping[ $payment_status ] ?? 'RNA';
1471
+	}
1472
+
1473
+
1474
+	/**
1475
+	 * Makes sure the 3.1's image url is converted to an image attachment post to the 4.1 CPT event
1476
+	 * and sets it as the featured image on the CPT event
1477
+	 *
1478
+	 * @param string                         $guid
1479
+	 * @param int                            $new_cpt_id
1480
+	 * @param EE_Data_Migration_Script_Stage $migration_stage the stage which called this, where errors should be added
1481
+	 * @return boolean whether we had to do the big job of creating an image attachment
1482
+	 */
1483
+	public function convert_image_url_to_attachment_and_attach_to_post(
1484
+		string $guid,
1485
+		int $new_cpt_id,
1486
+		EE_Data_Migration_Script_Stage $migration_stage
1487
+	): bool {
1488
+		$created_attachment_post = false;
1489
+		$guid                    = $this->_get_original_guid($guid);
1490
+		if ($guid) {
1491
+			// check for an existing attachment post with this guid
1492
+			$attachment_post_id = $this->_get_image_attachment_id_by_GUID($guid);
1493
+			if (! $attachment_post_id) {
1494
+				// post thumbnail with that GUID doesn't exist, we should create one
1495
+				$attachment_post_id      = $this->_create_image_attachment_from_GUID($guid, $migration_stage);
1496
+				$created_attachment_post = true;
1497
+			}
1498
+			// double-check we actually have an attachment post
1499
+			if ($attachment_post_id) {
1500
+				update_post_meta($new_cpt_id, '_thumbnail_id', $attachment_post_id);
1501
+			} else {
1502
+				$migration_stage->add_error(
1503
+					sprintf(
1504
+						esc_html__(
1505
+							"Could not update event image %s for CPT with ID %d, but attachments post ID is %d",
1506
+							"event_espresso"
1507
+						),
1508
+						$guid,
1509
+						$new_cpt_id,
1510
+						$attachment_post_id
1511
+					)
1512
+				);
1513
+			}
1514
+		}
1515
+		return $created_attachment_post;
1516
+	}
1517
+
1518
+
1519
+	/**
1520
+	 * In 3.1, the event thumbnail image DOESN'T point to the original image, but instead
1521
+	 * to a large thumbnail (which has nearly the same GUID, except it adds "-{width}x{height}" before the filetype,
1522
+	 * or whatever dimensions it is. Eg 'http://mysite.com/image1-300x400.jpg' instead of
1523
+	 * 'http://mysite.com/image1.jpg' ). This function attempts to strip that off and get the original file, if it
1524
+	 * exists
1525
+	 *
1526
+	 * @param string $guid_in_old_event
1527
+	 * @return string either the original guid, or $guid_in_old_event if we couldn't figure out what the original was
1528
+	 */
1529
+	private function _get_original_guid(string $guid_in_old_event): string
1530
+	{
1531
+		$original_guid = preg_replace('~-\d*x\d*\.~', '.', $guid_in_old_event, 1);
1532
+		// do a head request to verify the file exists
1533
+		$head_response = wp_remote_head($original_guid);
1534
+		if (! $head_response instanceof WP_Error && $head_response['response']['message'] == 'OK') {
1535
+			return $original_guid;
1536
+		}
1537
+		return $guid_in_old_event;
1538
+	}
1539
+
1540
+
1541
+	/**
1542
+	 * Creates an image attachment post for the GUID. If the GUID points to a remote image,
1543
+	 * we download it to our uploads directory so that it can be properly processed (eg, creates different sizes of
1544
+	 * thumbnails)
1545
+	 *
1546
+	 * @param string                         $guid
1547
+	 * @param EE_Data_Migration_Script_Stage $migration_stage
1548
+	 * @return int
1549
+	 */
1550
+	private function _create_image_attachment_from_GUID(string $guid, EE_Data_Migration_Script_Stage $migration_stage)
1551
+	{
1552
+		if (! $guid) {
1553
+			$migration_stage->add_error(
1554
+				esc_html__(
1555
+					"Cannot create image attachment for a blank GUID!",
1556
+					"event_espresso"
1557
+				)
1558
+			);
1559
+			return 0;
1560
+		}
1561
+		$wp_filetype   = wp_check_filetype(basename($guid));
1562
+		$wp_upload_dir = wp_upload_dir();
1563
+		// if the file is located remotely, download it to our uploads DIR, because wp_generate_attachment_metadata
1564
+		// needs the file to be local
1565
+		if (strpos($guid, $wp_upload_dir['url']) === false) {
1566
+			// image is located remotely. download it and place it in the uploads directory
1567
+			if (! is_readable($guid)) {
1568
+				$migration_stage->add_error(
1569
+					sprintf(
1570
+						esc_html__(
1571
+							"Could not create image attachment from non-existent file: %s",
1572
+							"event_espresso"
1573
+						),
1574
+						$guid
1575
+					)
1576
+				);
1577
+				return 0;
1578
+			}
1579
+			$contents = file_get_contents($guid);
1580
+			if ($contents === false) {
1581
+				$migration_stage->add_error(
1582
+					sprintf(
1583
+						esc_html__(
1584
+							"Could not read image at %s, and therefore couldn't create an attachment post for it.",
1585
+							"event_espresso"
1586
+						),
1587
+						$guid
1588
+					)
1589
+				);
1590
+				return false;
1591
+			}
1592
+			$local_filepath = $wp_upload_dir['path'] . '/' . basename($guid);
1593
+			$save_file      = fopen($local_filepath, 'w');
1594
+			fwrite($save_file, $contents);
1595
+			fclose($save_file);
1596
+			$guid = str_replace($wp_upload_dir['path'], $wp_upload_dir['url'], $local_filepath);
1597
+		} else {
1598
+			$local_filepath = str_replace($wp_upload_dir['url'], $wp_upload_dir['path'], $guid);
1599
+		}
1600
+		$attachment = [
1601
+			'guid'           => $guid,
1602
+			'post_mime_type' => $wp_filetype['type'],
1603
+			'post_title'     => preg_replace('/\.[^.]+$/', '', basename($guid)),
1604
+			'post_content'   => '',
1605
+			'post_status'    => 'inherit',
1606
+		];
1607
+		$attach_id  = wp_insert_attachment($attachment, $guid);
1608
+		if (! $attach_id) {
1609
+			$migration_stage->add_error(
1610
+				sprintf(
1611
+					esc_html__(
1612
+						"Could not create image attachment post from image '%s'. Attachment data was %s.",
1613
+						"event_espresso"
1614
+					),
1615
+					$guid,
1616
+					$this->_json_encode($attachment)
1617
+				)
1618
+			);
1619
+			return $attach_id;
1620
+		}
1621
+		// you must first include the image.php file
1622
+		// for the function wp_generate_attachment_metadata() to work
1623
+		require_once(ABSPATH . 'wp-admin/includes/image.php');
1624
+		$attach_data = wp_generate_attachment_metadata($attach_id, $local_filepath);
1625
+		if (! $attach_data) {
1626
+			$migration_stage->add_error(
1627
+				sprintf(
1628
+					esc_html__(
1629
+						"Could not generate attachment metadata for attachment post %d with filepath %s and GUID %s. Please check the file was downloaded properly.",
1630
+						"event_espresso"
1631
+					),
1632
+					$attach_id,
1633
+					$local_filepath,
1634
+					$guid
1635
+				)
1636
+			);
1637
+			return $attach_id;
1638
+		}
1639
+		$metadata_save_result = wp_update_attachment_metadata($attach_id, $attach_data);
1640
+		if (! $metadata_save_result) {
1641
+			$migration_stage->add_error(
1642
+				sprintf(
1643
+					esc_html__(
1644
+						"Could not update attachment metadata for attachment %d with data %s",
1645
+						"event_espresso"
1646
+					),
1647
+					$attach_id,
1648
+					$this->_json_encode($attach_data)
1649
+				)
1650
+			);
1651
+		}
1652
+		return $attach_id;
1653
+	}
1654
+
1655
+
1656
+	/**
1657
+	 * Finds the attachment post containing info about an image attachment given the GUID (link to the image itself),
1658
+	 * and returns its ID.
1659
+	 *
1660
+	 * @param string $guid
1661
+	 * @return int
1662
+	 * @global wpdb  $wpdb
1663
+	 */
1664
+	private function _get_image_attachment_id_by_GUID(string $guid): int
1665
+	{
1666
+		global $wpdb;
1667
+		return (int) $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE guid=%s LIMIT 1", $guid));
1668
+	}
1669
+
1670
+
1671
+	/**
1672
+	 * Returns a mysql-formatted DATETIME in UTC time, given a $DATETIME_string
1673
+	 * (and optionally a timezone; if none is given, the wp default is used)
1674
+	 *
1675
+	 * @param EE_Data_Migration_Script_Stage $stage
1676
+	 * @param array                          $row_of_data , the row from the DB (as an array) we're trying to find the
1677
+	 *                                                    UTC time for
1678
+	 * @param string                         $DATETIME_string
1679
+	 * @param string|null                    $timezone
1680
+	 * @return string
1681
+	 */
1682
+	public function convert_date_string_to_utc(
1683
+		EE_Data_Migration_Script_Stage $stage,
1684
+		array $row_of_data,
1685
+		string $DATETIME_string,
1686
+		?string $timezone = null
1687
+	): string {
1688
+		$original_tz = $timezone;
1689
+		if (! $timezone) {
1690
+			$timezone = $this->_get_wp_timezone();
1691
+		}
1692
+		if (! $timezone) {
1693
+			$stage->add_error(
1694
+				sprintf(
1695
+					esc_html__("Could not find timezone given %s for %s", "event_espresso"),
1696
+					$original_tz,
1697
+					$row_of_data
1698
+				)
1699
+			);
1700
+			$timezone = 'UTC';
1701
+		}
1702
+		try {
1703
+			$date_obj = new DateTime($DATETIME_string, new DateTimeZone($timezone));
1704
+			EEH_DTT_Helper::setTimezone($date_obj, new DateTimeZone('UTC'));
1705
+		} catch (Exception $e) {
1706
+			$stage->add_error(
1707
+				sprintf(
1708
+					esc_html__(
1709
+						"Could not convert time string '%s' using timezone '%s' into a proper DATETIME. Using current time instead.",
1710
+						"event_espresso"
1711
+					),
1712
+					$DATETIME_string,
1713
+					$timezone
1714
+				)
1715
+			);
1716
+			$date_obj = new DateTime();
1717
+		}
1718
+		return $date_obj->format('Y-m-d H:i:s');
1719
+	}
1720
+
1721
+
1722
+	/**
1723
+	 * Gets the default timezone string from wordpress (even if they set a gmt offset)
1724
+	 *
1725
+	 * @return string
1726
+	 */
1727
+	private function _get_wp_timezone()
1728
+	{
1729
+		$timezone = empty($timezone) ? get_option('timezone_string') : $timezone;
1730
+		// if timezone is STILL empty then let's get the GMT offset and then set the timezone_string using our converter
1731
+		if (empty($timezone)) {
1732
+			// let's get a the WordPress UTC offset
1733
+			$offset   = (int) get_option('gmt_offset');
1734
+			$timezone = $this->timezone_convert_to_string_from_offset($offset);
1735
+		}
1736
+		return $timezone;
1737
+	}
1738
+
1739
+
1740
+	/**
1741
+	 * Gets the wordpress timezone string from a UTC offset
1742
+	 *
1743
+	 * @param int $offset
1744
+	 * @return boolean
1745
+	 */
1746
+	private function timezone_convert_to_string_from_offset(int $offset): bool
1747
+	{
1748
+		// shamelessly taken from bottom comment at http://ca1.php.net/manual/en/function.timezone-name-from-abbr.php because timezone_name_from_abbr() did not work as expected - its not reliable
1749
+		$offset        *= 3600; // convert hour offset to seconds
1750
+		$abbreviations = timezone_abbreviations_list();
1751
+		foreach ($abbreviations as $abbreviation) {
1752
+			foreach ($abbreviation as $city) {
1753
+				if ($city['offset'] == $offset) {
1754
+					return $city['timezone_id'];
1755
+				}
1756
+			}
1757
+		}
1758
+		return false;
1759
+	}
1760
+
1761
+
1762
+	public function migration_page_hooks()
1763
+	{
1764
+		add_filter(
1765
+			'FHEE__ee_migration_page__header',
1766
+			[$this, '_migrate_page_hook_simplify_version_strings'],
1767
+			10,
1768
+			3
1769
+		);
1770
+		add_filter(
1771
+			'FHEE__ee_migration_page__p_after_header',
1772
+			[$this, '_migration_page_hook_simplify_next_db_state'],
1773
+			10,
1774
+			2
1775
+		);
1776
+		add_filter(
1777
+			'FHEE__ee_migration_page__option_1_main',
1778
+			[$this, '_migrate_page_hook_simplify_version_strings'],
1779
+			10,
1780
+			3
1781
+		);
1782
+		add_filter(
1783
+			'FHEE__ee_migration_page__option_1_button_text',
1784
+			[$this, '_migrate_page_hook_simplify_version_strings'],
1785
+			10,
1786
+			3
1787
+		);
1788
+		add_action(
1789
+			'AHEE__ee_migration_page__option_1_extra_details',
1790
+			[$this, '_migration_page_hook_option_1_extra_details'],
1791
+			10,
1792
+			3
1793
+		);
1794
+		add_filter(
1795
+			'FHEE__ee_migration_page__option_2_main',
1796
+			[$this, '_migrate_page_hook_simplify_version_strings'],
1797
+			10,
1798
+			4
1799
+		);
1800
+		add_filter(
1801
+			'FHEE__ee_migration_page__option_2_button_text',
1802
+			[$this, '_migration_page_hook_simplify_next_db_state'],
1803
+			10,
1804
+			2
1805
+		);
1806
+		add_filter(
1807
+			'FHEE__ee_migration_page__option_2_details',
1808
+			[$this, '_migration_page_hook_simplify_next_db_state'],
1809
+			10,
1810
+			2
1811
+		);
1812
+		add_action(
1813
+			'AHEE__ee_migration_page__after_migration_options_table',
1814
+			[$this, '_migration_page_hook_after_migration_options_table']
1815
+		);
1816
+		add_filter(
1817
+			'FHEE__ee_migration_page__done_migration_header',
1818
+			[$this, '_migration_page_hook_simplify_next_db_state'],
1819
+			10,
1820
+			2
1821
+		);
1822
+		add_filter(
1823
+			'FHEE__ee_migration_page__p_after_done_migration_header',
1824
+			[$this, '_migration_page_hook_simplify_next_db_state'],
1825
+			10,
1826
+			2
1827
+		);
1828
+		add_filter(
1829
+			'FHEE__ee_migration_page__migration_options_template',
1830
+			[$this, 'use_migration_options_from_ee3_template']
1831
+		);
1832
+	}
1833
+
1834
+
1835
+	public function _migrate_page_hook_simplify_version_strings(
1836
+		$old_content,
1837
+		$current_db_state,
1838
+		$next_db_state,
1839
+		$ultimate_db_state = null
1840
+	) {
1841
+		return str_replace(
1842
+			[$current_db_state, $next_db_state, $ultimate_db_state],
1843
+			[
1844
+				esc_html__('EE3', 'event_espresso'),
1845
+				esc_html__('EE4', 'event_espresso'),
1846
+				esc_html__("EE4", 'event_espresso'),
1847
+			],
1848
+			$old_content
1849
+		);
1850
+	}
1851
+
1852
+
1853
+	public function _migration_page_hook_simplify_next_db_state($old_content, $next_db_state)
1854
+	{
1855
+		return str_replace($next_db_state, esc_html__("EE4", 'event_espresso'), $old_content);
1856
+	}
1857
+
1858
+
1859
+	public function _migration_page_hook_option_1_extra_details()
1860
+	{
1861
+		?>
1862 1862
         <p><?php
1863
-        printf(
1864
-            esc_html__(
1865
-                "Note: many of your EE3 shortcodes will be changed to EE4 shortcodes during this migration (among many other things). Should you revert to EE3, then you should restore to your backup or manually change the EE4 shortcodes back to their EE3 equivalents",
1866
-                "event_espresso"
1867
-            )
1868
-        ); ?></p><?php
1869
-    }
1870
-
1871
-
1872
-    public function _migration_page_hook_after_migration_options_table()
1873
-    {
1874
-        ?><p class="ee-attention">
1863
+		printf(
1864
+			esc_html__(
1865
+				"Note: many of your EE3 shortcodes will be changed to EE4 shortcodes during this migration (among many other things). Should you revert to EE3, then you should restore to your backup or manually change the EE4 shortcodes back to their EE3 equivalents",
1866
+				"event_espresso"
1867
+			)
1868
+		); ?></p><?php
1869
+	}
1870
+
1871
+
1872
+	public function _migration_page_hook_after_migration_options_table()
1873
+	{
1874
+		?><p class="ee-attention">
1875 1875
         <strong><span class="reminder-spn">
1876 1876
                 <?php
1877
-                esc_html_e(
1878
-                    "Important note to those using Event Espresso 3 addons: ",
1879
-                    "event_espresso"
1880
-                ); ?></span></strong>
1877
+				esc_html_e(
1878
+					"Important note to those using Event Espresso 3 addons: ",
1879
+					"event_espresso"
1880
+				); ?></span></strong>
1881 1881
         <br/>
1882 1882
         <?php
1883
-        esc_html_e(
1884
-            "Unless an addon's description on our website explicitly states that it is compatible with EE4, you should consider it incompatible and know that it WILL NOT WORK correctly with this new version of Event Espresso 4 (EE4). As well, any data for incompatible addons will NOT BE MIGRATED until an updated EE4 compatible version of the addon is available. If you want, or need to keep using your EE3 addons, you should simply continue using EE3 until EE4 compatible versions of your addons become available. To continue using EE3 for now, just deactivate EE4 and reactivate EE3.",
1885
-            "event_espresso"
1886
-        ); ?>
1883
+		esc_html_e(
1884
+			"Unless an addon's description on our website explicitly states that it is compatible with EE4, you should consider it incompatible and know that it WILL NOT WORK correctly with this new version of Event Espresso 4 (EE4). As well, any data for incompatible addons will NOT BE MIGRATED until an updated EE4 compatible version of the addon is available. If you want, or need to keep using your EE3 addons, you should simply continue using EE3 until EE4 compatible versions of your addons become available. To continue using EE3 for now, just deactivate EE4 and reactivate EE3.",
1885
+			"event_espresso"
1886
+		); ?>
1887 1887
         </p><?php
1888
-    }
1889
-
1890
-
1891
-    /**
1892
-     * When showing the migration options, show more options and info than normal (ie, give folks the option
1893
-     * to start using EE4 without migrating. From EE3 that's fine, because it doesn't actually remove any data, because
1894
-     * EE4 doesn't have any yet. But when migrating from EE4 it would remove old data, so its not a great idea).
1895
-     *
1896
-     * @param $template_filepath
1897
-     * @return string
1898
-     */
1899
-    public function use_migration_options_from_ee3_template($template_filepath): string
1900
-    {
1901
-        return EE_MAINTENANCE_TEMPLATE_PATH . 'migration_options_from_ee3.template.php';
1902
-    }
1888
+	}
1889
+
1890
+
1891
+	/**
1892
+	 * When showing the migration options, show more options and info than normal (ie, give folks the option
1893
+	 * to start using EE4 without migrating. From EE3 that's fine, because it doesn't actually remove any data, because
1894
+	 * EE4 doesn't have any yet. But when migrating from EE4 it would remove old data, so its not a great idea).
1895
+	 *
1896
+	 * @param $template_filepath
1897
+	 * @return string
1898
+	 */
1899
+	public function use_migration_options_from_ee3_template($template_filepath): string
1900
+	{
1901
+		return EE_MAINTENANCE_TEMPLATE_PATH . 'migration_options_from_ee3.template.php';
1902
+	}
1903 1903
 }
Please login to merge, or discard this patch.
Spacing   +85 added lines, -85 removed lines patch added patch discarded remove patch
@@ -7,13 +7,13 @@  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_1_0_stages/*');
10
+$stages            = glob(EE_CORE.'data_migration_scripts/4_1_0_stages/*');
11 11
 $class_to_filepath = [];
12
-if (! empty($stages)) {
12
+if ( ! empty($stages)) {
13 13
     foreach ($stages as $filepath) {
14 14
         $matches = [];
15 15
         preg_match('~4_1_0_stages/(.*).dmsstage.php~', $filepath, $matches);
16
-        $class_to_filepath[ $matches[1] ] = $filepath;
16
+        $class_to_filepath[$matches[1]] = $filepath;
17 17
     }
18 18
 }
19 19
 // give addons a chance to autoload their stages too
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
     {
84 84
         global $wpdb;
85 85
         return (bool) $wpdb->get_results(
86
-            "SHOW TABLES LIKE '" . $wpdb->prefix . "events_attendee_checkin" . "'"
86
+            "SHOW TABLES LIKE '".$wpdb->prefix."events_attendee_checkin"."'"
87 87
         );
88 88
     }
89 89
 
@@ -94,11 +94,11 @@  discard block
 block discarded – undo
94 94
         if (version_compare($version_string, '4.0.0.decaf', '<') && version_compare($version_string, '3.1.26', '>=')) {
95 95
             //          echo "$version_string can be migrated fro";
96 96
             return true;
97
-        } elseif (! $version_string) {
97
+        } elseif ( ! $version_string) {
98 98
             //          echo "no version string provided: $version_string";
99 99
             // no version string provided... this must be pre 4.1
100 100
             // because since 4.1 we're
101
-            return false;// changed mind. dont want people thinking they should migrate yet because they cant
101
+            return false; // changed mind. dont want people thinking they should migrate yet because they cant
102 102
         } else {
103 103
             //          echo "$version_string doesnt apply";
104 104
             return false;
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
     public function schema_changes_before_migration()
114 114
     {
115 115
         // relies on 4.1's EEH_Activation::create_table
116
-        require_once(EE_HELPERS . 'EEH_Activation.helper.php');
116
+        require_once(EE_HELPERS.'EEH_Activation.helper.php');
117 117
 
118 118
         $table_name = 'esp_answer';
119 119
         $sql        = "ANS_ID int(10) unsigned NOT NULL AUTO_INCREMENT,
@@ -524,12 +524,12 @@  discard block
 block discarded – undo
524 524
     public function insert_default_states()
525 525
     {
526 526
         global $wpdb;
527
-        $state_table = $wpdb->prefix . "esp_state";
527
+        $state_table = $wpdb->prefix."esp_state";
528 528
         if ($this->_get_table_analysis()->tableExists($state_table)) {
529
-            $SQL    = "SELECT COUNT('STA_ID') FROM " . $state_table;
529
+            $SQL    = "SELECT COUNT('STA_ID') FROM ".$state_table;
530 530
             $states = $wpdb->get_var($SQL);
531
-            if (! $states) {
532
-                $SQL = "INSERT INTO " . $state_table . "
531
+            if ( ! $states) {
532
+                $SQL = "INSERT INTO ".$state_table."
533 533
 				(STA_ID, CNT_ISO, STA_abbrev, STA_name, STA_active) VALUES
534 534
 				(1, 'US', 'AK', 'Alaska', 1),
535 535
 				(2, 'US', 'AL', 'Alabama', 1),
@@ -620,12 +620,12 @@  discard block
 block discarded – undo
620 620
     public function insert_default_countries()
621 621
     {
622 622
         global $wpdb;
623
-        $country_table = $wpdb->prefix . "esp_country";
623
+        $country_table = $wpdb->prefix."esp_country";
624 624
         if ($this->_get_table_analysis()->tableExists($country_table)) {
625
-            $SQL       = "SELECT COUNT('CNT_ISO') FROM " . $country_table;
625
+            $SQL       = "SELECT COUNT('CNT_ISO') FROM ".$country_table;
626 626
             $countries = $wpdb->get_var($SQL);
627
-            if (! $countries) {
628
-                $SQL = "INSERT INTO " . $country_table . "
627
+            if ( ! $countries) {
628
+                $SQL = "INSERT INTO ".$country_table."
629 629
 				(CNT_ISO, CNT_ISO3, RGN_ID, CNT_name, CNT_cur_code, CNT_cur_single, CNT_cur_plural, CNT_cur_sign, CNT_cur_sign_b4, CNT_cur_dec_plc, CNT_tel_code, CNT_is_EU, CNT_active) VALUES
630 630
 				('AD', 'AND', 0, 'Andorra', 'EUR', 'Euro', 'Euros', '€', 1, 2, '+376', 0, 0),
631 631
 				('AE', 'ARE', 0, 'United Arab Emirates', 'AED', 'Dirham', 'Dirhams', 'د.إ', 1, 2, '+971', 0, 0),
@@ -870,17 +870,17 @@  discard block
 block discarded – undo
870 870
     public function insert_default_price_types()
871 871
     {
872 872
         global $wpdb;
873
-        $price_type_table = $wpdb->prefix . "esp_price_type";
873
+        $price_type_table = $wpdb->prefix."esp_price_type";
874 874
         if ($this->_get_table_analysis()->tableExists($price_type_table)) {
875
-            $SQL               = 'SELECT COUNT(PRT_ID) FROM ' . $price_type_table;
875
+            $SQL               = 'SELECT COUNT(PRT_ID) FROM '.$price_type_table;
876 876
             $price_types_exist = $wpdb->get_var($SQL);
877
-            if (! $price_types_exist) {
877
+            if ( ! $price_types_exist) {
878 878
                 $SQL = "INSERT INTO $price_type_table ( PRT_ID, PRT_name, PBT_ID, PRT_is_percent, PRT_order, PRT_deleted ) VALUES
879
-							(1, '" . esc_html__('Base Price', 'event_espresso') . "', 1,  0, 0, 0),
880
-							(2, '" . esc_html__('Percent Discount', 'event_espresso') . "', 2,  1, 20, 0),
881
-							(3, '" . esc_html__('Fixed Discount', 'event_espresso') . "', 2,  0, 30, 0),
882
-							(4, '" . esc_html__('Percent Surcharge', 'event_espresso') . "', 3,  1, 40, 0),
883
-							(5, '" . esc_html__('Fixed Surcharge', 'event_espresso') . "', 3,  0, 50, 0);";
879
+							(1, '".esc_html__('Base Price', 'event_espresso')."', 1,  0, 0, 0),
880
+							(2, '" . esc_html__('Percent Discount', 'event_espresso')."', 2,  1, 20, 0),
881
+							(3, '" . esc_html__('Fixed Discount', 'event_espresso')."', 2,  0, 30, 0),
882
+							(4, '" . esc_html__('Percent Surcharge', 'event_espresso')."', 3,  1, 40, 0),
883
+							(5, '" . esc_html__('Fixed Surcharge', 'event_espresso')."', 3,  0, 50, 0);";
884 884
                 $SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_price_types__SQL', $SQL);
885 885
                 $wpdb->query($SQL);
886 886
             }
@@ -902,11 +902,11 @@  discard block
 block discarded – undo
902 902
     public function insert_default_prices()
903 903
     {
904 904
         global $wpdb;
905
-        $price_table = $wpdb->prefix . "esp_price";
905
+        $price_table = $wpdb->prefix."esp_price";
906 906
         if ($this->_get_table_analysis()->tableExists($price_table)) {
907
-            $SQL          = 'SELECT COUNT(PRC_ID) FROM ' . $price_table;
907
+            $SQL          = 'SELECT COUNT(PRC_ID) FROM '.$price_table;
908 908
             $prices_exist = $wpdb->get_var($SQL);
909
-            if (! $prices_exist) {
909
+            if ( ! $prices_exist) {
910 910
                 $SQL = "INSERT INTO $price_table
911 911
 							(PRC_ID, PRT_ID, PRC_amount, PRC_name, PRC_desc,  PRC_is_default, PRC_overrides, PRC_order, PRC_deleted, PRC_parent ) VALUES
912 912
 							(1, 1, '0.00', 'Admission', '', 1, null, 0, 0, 0);";
@@ -929,25 +929,25 @@  discard block
 block discarded – undo
929 929
     public function insert_default_tickets()
930 930
     {
931 931
         global $wpdb;
932
-        $ticket_table = $wpdb->prefix . "esp_ticket";
932
+        $ticket_table = $wpdb->prefix."esp_ticket";
933 933
         if ($this->_get_table_analysis()->tableExists($ticket_table)) {
934
-            $SQL           = 'SELECT COUNT(TKT_ID) FROM ' . $ticket_table;
934
+            $SQL           = 'SELECT COUNT(TKT_ID) FROM '.$ticket_table;
935 935
             $tickets_exist = $wpdb->get_var($SQL);
936
-            if (! $tickets_exist) {
936
+            if ( ! $tickets_exist) {
937 937
                 $SQL = "INSERT INTO $ticket_table
938 938
 					( TKT_ID, TTM_ID, TKT_name, TKT_description, TKT_qty, TKT_sold, TKT_uses, TKT_min, TKT_max, TKT_price, TKT_start_date, TKT_end_date, TKT_taxable, TKT_order, TKT_row, TKT_is_default, TKT_parent, TKT_deleted ) VALUES
939
-					( 1, 0, '" .
940
-                       esc_html__("Free Ticket", "event_espresso") .
939
+					( 1, 0, '".
940
+                       esc_html__("Free Ticket", "event_espresso").
941 941
                        "', '', 100, 0, -1, 0, -1, 0.00, '0000-00-00 00:00:00', '0000-00-00 00:00:00', 0, 0, 1, 1, 0, 0);";
942 942
                 $SQL = apply_filters('FHEE__EE_DMS_4_1_0__insert_default_tickets__SQL', $SQL);
943 943
                 $wpdb->query($SQL);
944 944
             }
945 945
         }
946
-        $ticket_price_table = $wpdb->prefix . "esp_ticket_price";
946
+        $ticket_price_table = $wpdb->prefix."esp_ticket_price";
947 947
         if ($this->_get_table_analysis()->tableExists($ticket_price_table)) {
948
-            $SQL              = 'SELECT COUNT(TKP_ID) FROM ' . $ticket_price_table;
948
+            $SQL              = 'SELECT COUNT(TKP_ID) FROM '.$ticket_price_table;
949 949
             $ticket_prc_exist = $wpdb->get_var($SQL);
950
-            if (! $ticket_prc_exist) {
950
+            if ( ! $ticket_prc_exist) {
951 951
                 $SQL = "INSERT INTO $ticket_price_table
952 952
 				( TKP_ID, TKT_ID, PRC_ID ) VALUES
953 953
 				( 1, 1, 1 )
@@ -972,11 +972,11 @@  discard block
 block discarded – undo
972 972
      */
973 973
     public function get_or_create_country($country_name): array
974 974
     {
975
-        if (! $country_name) {
975
+        if ( ! $country_name) {
976 976
             throw new EE_Error(esc_html__("Could not get a country because country name is blank", "event_espresso"));
977 977
         }
978 978
         global $wpdb;
979
-        $country_table = $wpdb->prefix . "esp_country";
979
+        $country_table = $wpdb->prefix."esp_country";
980 980
         if (is_int($country_name)) {
981 981
             $country_name = $this->get_iso_from_3_1_country_id($country_name);
982 982
         }
@@ -992,7 +992,7 @@  discard block
 block discarded – undo
992 992
             ),
993 993
             ARRAY_A
994 994
         );
995
-        if (! $country) {
995
+        if ( ! $country) {
996 996
             // insert a new one then
997 997
             $cols_n_values = [
998 998
                 'CNT_ISO'         => $this->_find_available_country_iso(),
@@ -1011,29 +1011,29 @@  discard block
 block discarded – undo
1011 1011
                 'CNT_is_EU'       => false,
1012 1012
                 'CNT_active'      => true,
1013 1013
             ];
1014
-            $data_types    = [
1015
-                '%s',// CNT_ISO
1016
-                '%s',// CNT_ISO3
1017
-                '%d',// RGN_ID
1018
-                '%s',// CNT_name
1019
-                '%s',// CNT_cur_code
1020
-                '%s',// CNT_cur_single
1021
-                '%s',// CNT_cur_plural
1022
-                '%s',// CNT_cur_sign
1023
-                '%d',// CNT_cur_sign_b4
1024
-                '%d',// CNT_cur_dec_plc
1025
-                '%s',// CNT_cur_dec_mrk
1026
-                '%s',// CNT_cur_thsnds
1027
-                '%s',// CNT_tel_code
1028
-                '%d',// CNT_is_EU
1029
-                '%d',// CNT_active
1014
+            $data_types = [
1015
+                '%s', // CNT_ISO
1016
+                '%s', // CNT_ISO3
1017
+                '%d', // RGN_ID
1018
+                '%s', // CNT_name
1019
+                '%s', // CNT_cur_code
1020
+                '%s', // CNT_cur_single
1021
+                '%s', // CNT_cur_plural
1022
+                '%s', // CNT_cur_sign
1023
+                '%d', // CNT_cur_sign_b4
1024
+                '%d', // CNT_cur_dec_plc
1025
+                '%s', // CNT_cur_dec_mrk
1026
+                '%s', // CNT_cur_thsnds
1027
+                '%s', // CNT_tel_code
1028
+                '%d', // CNT_is_EU
1029
+                '%d', // CNT_active
1030 1030
             ];
1031
-            $success       = $wpdb->insert(
1031
+            $success = $wpdb->insert(
1032 1032
                 $country_table,
1033 1033
                 $cols_n_values,
1034 1034
                 $data_types
1035 1035
             );
1036
-            if (! $success) {
1036
+            if ( ! $success) {
1037 1037
                 throw new EE_Error(
1038 1038
                     $this->_create_error_message_for_db_insertion(
1039 1039
                         'N/A',
@@ -1060,13 +1060,13 @@  discard block
 block discarded – undo
1060 1060
     private function _find_available_country_iso(int $num_letters = 2): string
1061 1061
     {
1062 1062
         global $wpdb;
1063
-        $country_table = $wpdb->prefix . "esp_country";
1063
+        $country_table = $wpdb->prefix."esp_country";
1064 1064
         $attempts      = 0;
1065 1065
         do {
1066 1066
             $current_iso           = strtoupper(wp_generate_password($num_letters, false));
1067 1067
             $country_with_that_iso = $wpdb->get_var(
1068 1068
                 $wpdb->prepare(
1069
-                    "SELECT count(CNT_ISO) FROM " . $country_table . " WHERE CNT_ISO=%s",
1069
+                    "SELECT count(CNT_ISO) FROM ".$country_table." WHERE CNT_ISO=%s",
1070 1070
                     $current_iso
1071 1071
                 )
1072 1072
             );
@@ -1074,7 +1074,7 @@  discard block
 block discarded – undo
1074 1074
             // keep going until we find an available country code, or we arbitrarily
1075 1075
             // decide we've tried this enough. Somehow they have way too many countries
1076 1076
             // (probably because they're mis-using the EE3 country_id like a custom question)
1077
-        } while (intval($country_with_that_iso) && $attempts < 200);
1077
+        }while (intval($country_with_that_iso) && $attempts < 200);
1078 1078
         return $current_iso;
1079 1079
     }
1080 1080
 
@@ -1091,7 +1091,7 @@  discard block
 block discarded – undo
1091 1091
      */
1092 1092
     public function get_or_create_state(string $state_name, $country_name = ''): array
1093 1093
     {
1094
-        if (! $state_name) {
1094
+        if ( ! $state_name) {
1095 1095
             throw new EE_Error(
1096 1096
                 esc_html__(
1097 1097
                     "Could not get-or-create state because no state name was provided",
@@ -1106,7 +1106,7 @@  discard block
 block discarded – undo
1106 1106
             $country_iso = $this->get_default_country_iso();
1107 1107
         }
1108 1108
         global $wpdb;
1109
-        $state_table = $wpdb->prefix . "esp_state";
1109
+        $state_table = $wpdb->prefix."esp_state";
1110 1110
         $state       = $wpdb->get_row(
1111 1111
             $wpdb->prepare(
1112 1112
                 "SELECT * FROM $state_table WHERE
@@ -1119,7 +1119,7 @@  discard block
 block discarded – undo
1119 1119
             ),
1120 1120
             ARRAY_A
1121 1121
         );
1122
-        if (! $state) {
1122
+        if ( ! $state) {
1123 1123
             // insert a new one then
1124 1124
             $cols_n_values = [
1125 1125
                 'CNT_ISO'    => $country_iso,
@@ -1127,14 +1127,14 @@  discard block
 block discarded – undo
1127 1127
                 'STA_name'   => $state_name,
1128 1128
                 'STA_active' => true,
1129 1129
             ];
1130
-            $data_types    = [
1131
-                '%s',// CNT_ISO
1132
-                '%s',// STA_abbrev
1133
-                '%s',// STA_name
1134
-                '%d',// STA_active
1130
+            $data_types = [
1131
+                '%s', // CNT_ISO
1132
+                '%s', // STA_abbrev
1133
+                '%s', // STA_name
1134
+                '%d', // STA_active
1135 1135
             ];
1136
-            $success       = $wpdb->insert($state_table, $cols_n_values, $data_types);
1137
-            if (! $success) {
1136
+            $success = $wpdb->insert($state_table, $cols_n_values, $data_types);
1137
+            if ( ! $success) {
1138 1138
                 throw new EE_Error(
1139 1139
                     $this->_create_error_message_for_db_insertion(
1140 1140
                         'N/A',
@@ -1164,7 +1164,7 @@  discard block
 block discarded – undo
1164 1164
     {
1165 1165
         $matches = [];
1166 1166
         preg_match("~(\\d*):(\\d*)~", $timeString, $matches);
1167
-        if (! $matches || count($matches) < 3) {
1167
+        if ( ! $matches || count($matches) < 3) {
1168 1168
             $hour    = '00';
1169 1169
             $minutes = '00';
1170 1170
         } else {
@@ -1413,7 +1413,7 @@  discard block
 block discarded – undo
1413 1413
             [225, 'Zambia', 'ZM', 'ZMB', 1],
1414 1414
             [226, 'Zimbabwe', 'ZW', 'ZWE', 1],
1415 1415
         ];
1416
-        $country_iso   = 'US';
1416
+        $country_iso = 'US';
1417 1417
         foreach ($old_countries as $country_array) {
1418 1418
             // note: index 0 is the 3.1 country ID
1419 1419
             if ($country_array[0] == $country_id) {
@@ -1467,7 +1467,7 @@  discard block
 block discarded – undo
1467 1467
                 'Declined'         => 'RPP',
1468 1468
             ];
1469 1469
         }
1470
-        return $mapping[ $payment_status ] ?? 'RNA';
1470
+        return $mapping[$payment_status] ?? 'RNA';
1471 1471
     }
1472 1472
 
1473 1473
 
@@ -1490,7 +1490,7 @@  discard block
 block discarded – undo
1490 1490
         if ($guid) {
1491 1491
             // check for an existing attachment post with this guid
1492 1492
             $attachment_post_id = $this->_get_image_attachment_id_by_GUID($guid);
1493
-            if (! $attachment_post_id) {
1493
+            if ( ! $attachment_post_id) {
1494 1494
                 // post thumbnail with that GUID doesn't exist, we should create one
1495 1495
                 $attachment_post_id      = $this->_create_image_attachment_from_GUID($guid, $migration_stage);
1496 1496
                 $created_attachment_post = true;
@@ -1531,7 +1531,7 @@  discard block
 block discarded – undo
1531 1531
         $original_guid = preg_replace('~-\d*x\d*\.~', '.', $guid_in_old_event, 1);
1532 1532
         // do a head request to verify the file exists
1533 1533
         $head_response = wp_remote_head($original_guid);
1534
-        if (! $head_response instanceof WP_Error && $head_response['response']['message'] == 'OK') {
1534
+        if ( ! $head_response instanceof WP_Error && $head_response['response']['message'] == 'OK') {
1535 1535
             return $original_guid;
1536 1536
         }
1537 1537
         return $guid_in_old_event;
@@ -1549,7 +1549,7 @@  discard block
 block discarded – undo
1549 1549
      */
1550 1550
     private function _create_image_attachment_from_GUID(string $guid, EE_Data_Migration_Script_Stage $migration_stage)
1551 1551
     {
1552
-        if (! $guid) {
1552
+        if ( ! $guid) {
1553 1553
             $migration_stage->add_error(
1554 1554
                 esc_html__(
1555 1555
                     "Cannot create image attachment for a blank GUID!",
@@ -1564,7 +1564,7 @@  discard block
 block discarded – undo
1564 1564
         // needs the file to be local
1565 1565
         if (strpos($guid, $wp_upload_dir['url']) === false) {
1566 1566
             // image is located remotely. download it and place it in the uploads directory
1567
-            if (! is_readable($guid)) {
1567
+            if ( ! is_readable($guid)) {
1568 1568
                 $migration_stage->add_error(
1569 1569
                     sprintf(
1570 1570
                         esc_html__(
@@ -1589,7 +1589,7 @@  discard block
 block discarded – undo
1589 1589
                 );
1590 1590
                 return false;
1591 1591
             }
1592
-            $local_filepath = $wp_upload_dir['path'] . '/' . basename($guid);
1592
+            $local_filepath = $wp_upload_dir['path'].'/'.basename($guid);
1593 1593
             $save_file      = fopen($local_filepath, 'w');
1594 1594
             fwrite($save_file, $contents);
1595 1595
             fclose($save_file);
@@ -1604,8 +1604,8 @@  discard block
 block discarded – undo
1604 1604
             'post_content'   => '',
1605 1605
             'post_status'    => 'inherit',
1606 1606
         ];
1607
-        $attach_id  = wp_insert_attachment($attachment, $guid);
1608
-        if (! $attach_id) {
1607
+        $attach_id = wp_insert_attachment($attachment, $guid);
1608
+        if ( ! $attach_id) {
1609 1609
             $migration_stage->add_error(
1610 1610
                 sprintf(
1611 1611
                     esc_html__(
@@ -1620,9 +1620,9 @@  discard block
 block discarded – undo
1620 1620
         }
1621 1621
         // you must first include the image.php file
1622 1622
         // for the function wp_generate_attachment_metadata() to work
1623
-        require_once(ABSPATH . 'wp-admin/includes/image.php');
1623
+        require_once(ABSPATH.'wp-admin/includes/image.php');
1624 1624
         $attach_data = wp_generate_attachment_metadata($attach_id, $local_filepath);
1625
-        if (! $attach_data) {
1625
+        if ( ! $attach_data) {
1626 1626
             $migration_stage->add_error(
1627 1627
                 sprintf(
1628 1628
                     esc_html__(
@@ -1637,7 +1637,7 @@  discard block
 block discarded – undo
1637 1637
             return $attach_id;
1638 1638
         }
1639 1639
         $metadata_save_result = wp_update_attachment_metadata($attach_id, $attach_data);
1640
-        if (! $metadata_save_result) {
1640
+        if ( ! $metadata_save_result) {
1641 1641
             $migration_stage->add_error(
1642 1642
                 sprintf(
1643 1643
                     esc_html__(
@@ -1686,10 +1686,10 @@  discard block
 block discarded – undo
1686 1686
         ?string $timezone = null
1687 1687
     ): string {
1688 1688
         $original_tz = $timezone;
1689
-        if (! $timezone) {
1689
+        if ( ! $timezone) {
1690 1690
             $timezone = $this->_get_wp_timezone();
1691 1691
         }
1692
-        if (! $timezone) {
1692
+        if ( ! $timezone) {
1693 1693
             $stage->add_error(
1694 1694
                 sprintf(
1695 1695
                     esc_html__("Could not find timezone given %s for %s", "event_espresso"),
@@ -1746,7 +1746,7 @@  discard block
 block discarded – undo
1746 1746
     private function timezone_convert_to_string_from_offset(int $offset): bool
1747 1747
     {
1748 1748
         // shamelessly taken from bottom comment at http://ca1.php.net/manual/en/function.timezone-name-from-abbr.php because timezone_name_from_abbr() did not work as expected - its not reliable
1749
-        $offset        *= 3600; // convert hour offset to seconds
1749
+        $offset *= 3600; // convert hour offset to seconds
1750 1750
         $abbreviations = timezone_abbreviations_list();
1751 1751
         foreach ($abbreviations as $abbreviation) {
1752 1752
             foreach ($abbreviation as $city) {
@@ -1898,6 +1898,6 @@  discard block
 block discarded – undo
1898 1898
      */
1899 1899
     public function use_migration_options_from_ee3_template($template_filepath): string
1900 1900
     {
1901
-        return EE_MAINTENANCE_TEMPLATE_PATH . 'migration_options_from_ee3.template.php';
1901
+        return EE_MAINTENANCE_TEMPLATE_PATH.'migration_options_from_ee3.template.php';
1902 1902
     }
1903 1903
 }
Please login to merge, or discard this patch.
core/data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_answers.dmsstage.php 2 patches
Indentation   +119 added lines, -119 removed lines patch added patch discarded remove patch
@@ -31,136 +31,136 @@
 block discarded – undo
31 31
 class EE_DMS_4_1_0_answers extends EE_Data_Migration_Script_Stage_Table
32 32
 {
33 33
 
34
-    private string $_new_answer_table;
34
+	private string $_new_answer_table;
35 35
 
36
-    private string $_new_question_table;
36
+	private string $_new_question_table;
37 37
 
38 38
 
39
-    public function __construct()
40
-    {
41
-        global $wpdb;
42
-        $this->_pretty_name = esc_html__("Answers", "event_espresso");
43
-        $this->_old_table   = $wpdb->prefix . "events_answer";
44
-        // join to attendee and then join to events table
45
-        $this->select_expression   = 'ans.*, e.event_status';
46
-        $this->_extra_where_sql    = ' AS ans
39
+	public function __construct()
40
+	{
41
+		global $wpdb;
42
+		$this->_pretty_name = esc_html__("Answers", "event_espresso");
43
+		$this->_old_table   = $wpdb->prefix . "events_answer";
44
+		// join to attendee and then join to events table
45
+		$this->select_expression   = 'ans.*, e.event_status';
46
+		$this->_extra_where_sql    = ' AS ans
47 47
             INNER JOIN ' . $wpdb->prefix . 'events_attendee AS att ON ans.attendee_id = att.id
48 48
             INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id = e.id
49 49
             WHERE e.event_status !="D"';
50
-        $this->_new_answer_table   = $wpdb->prefix . "esp_answer";
51
-        $this->_new_question_table = $wpdb->prefix . "esp_question";
52
-        parent::__construct();
53
-    }
50
+		$this->_new_answer_table   = $wpdb->prefix . "esp_answer";
51
+		$this->_new_question_table = $wpdb->prefix . "esp_question";
52
+		parent::__construct();
53
+	}
54 54
 
55 55
 
56
-    /**
57
-     * @throws EE_Error
58
-     */
59
-    protected function _migrate_old_row($old_row)
60
-    {
61
-        // get the new REGs for the old answer
62
-        global $wpdb;
63
-        $old_attendee_table = $wpdb->prefix . "events_attendee";
64
-        $new_reg_table      = $wpdb->prefix . "esp_registration";
65
-        $regs               =
66
-            $this->get_migration_script()->get_mapping_new_pk(
67
-                $old_attendee_table,
68
-                $old_row['attendee_id'],
69
-                $new_reg_table
70
-            );
71
-        if (! $regs) {
72
-            $this->add_error(
73
-                sprintf(
74
-                    esc_html__(
75
-                        "Could not find new registrations for old attendee %d when creating answer %s",
76
-                        "event_espresso"
77
-                    ),
78
-                    $old_row['attendee_id'],
79
-                    $this->_json_encode($old_row)
80
-                )
81
-            );
82
-            return;
83
-        }
84
-        // as inefficient as this sounds, we create an answer per REGISTRATION, (even if the registrations use the same attendee)
85
-        foreach ($regs as $new_reg_id) {
86
-            $this->_insert_new_answer($old_row, $new_reg_id);
87
-        }
88
-    }
56
+	/**
57
+	 * @throws EE_Error
58
+	 */
59
+	protected function _migrate_old_row($old_row)
60
+	{
61
+		// get the new REGs for the old answer
62
+		global $wpdb;
63
+		$old_attendee_table = $wpdb->prefix . "events_attendee";
64
+		$new_reg_table      = $wpdb->prefix . "esp_registration";
65
+		$regs               =
66
+			$this->get_migration_script()->get_mapping_new_pk(
67
+				$old_attendee_table,
68
+				$old_row['attendee_id'],
69
+				$new_reg_table
70
+			);
71
+		if (! $regs) {
72
+			$this->add_error(
73
+				sprintf(
74
+					esc_html__(
75
+						"Could not find new registrations for old attendee %d when creating answer %s",
76
+						"event_espresso"
77
+					),
78
+					$old_row['attendee_id'],
79
+					$this->_json_encode($old_row)
80
+				)
81
+			);
82
+			return;
83
+		}
84
+		// as inefficient as this sounds, we create an answer per REGISTRATION, (even if the registrations use the same attendee)
85
+		foreach ($regs as $new_reg_id) {
86
+			$this->_insert_new_answer($old_row, $new_reg_id);
87
+		}
88
+	}
89 89
 
90 90
 
91
-    /**
92
-     * @param array $old_answer
93
-     * @param int   $new_reg_id
94
-     * @return void
95
-     * @throws EE_Error
96
-     * @global wpdb $wpdb
97
-     */
98
-    private function _insert_new_answer(array $old_answer, int $new_reg_id): void
99
-    {
100
-        global $wpdb;
101
-        $old_question_table = $wpdb->prefix . "events_question";
102
-        $new_question_id    =
103
-            $this->get_migration_script()->get_mapping_new_pk(
104
-                $old_question_table,
105
-                $old_answer['question_id'],
106
-                $this->_new_question_table
107
-            );
91
+	/**
92
+	 * @param array $old_answer
93
+	 * @param int   $new_reg_id
94
+	 * @return void
95
+	 * @throws EE_Error
96
+	 * @global wpdb $wpdb
97
+	 */
98
+	private function _insert_new_answer(array $old_answer, int $new_reg_id): void
99
+	{
100
+		global $wpdb;
101
+		$old_question_table = $wpdb->prefix . "events_question";
102
+		$new_question_id    =
103
+			$this->get_migration_script()->get_mapping_new_pk(
104
+				$old_question_table,
105
+				$old_answer['question_id'],
106
+				$this->_new_question_table
107
+			);
108 108
 
109
-        $question_row = $this->_get_question_type_and_system($new_question_id);
110
-        if ($question_row['QST_system']) {
111
-            // It's an answer to a system question? EE3 used to store that on both the attendee and the answers column,
112
-            // but not EE4! It's just stored in the attendee meta table. The answers table is ONLY for answers to custom
113
-            // questions.
114
-            return;
115
-        }
116
-        if ($question_row['QST_type'] == 'MULTIPLE') {
117
-            $ans_value = serialize(explode(",", stripslashes($old_answer['answer'])));
118
-        } else {
119
-            $ans_value = stripslashes($old_answer['answer']);
120
-        }
121
-        $cols_n_values = [
122
-            'REG_ID'    => $new_reg_id,
123
-            'QST_ID'    => $new_question_id,
124
-            'ANS_value' => $ans_value,
125
-        ];
126
-        $data_types    = [
127
-            '%d',// REG_ID
128
-            '%d',// QST_ID
129
-            '%s',// ANS_value
130
-        ];
131
-        $success       = $wpdb->insert($this->_new_answer_table, $cols_n_values, $data_types);
132
-        if (! $success) {
133
-            $this->add_error(
134
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
135
-                    $this->_old_table,
136
-                    $old_answer,
137
-                    $this->_new_answer_table,
138
-                    $cols_n_values,
139
-                    $data_types
140
-                )
141
-            );
142
-        }
143
-    }
109
+		$question_row = $this->_get_question_type_and_system($new_question_id);
110
+		if ($question_row['QST_system']) {
111
+			// It's an answer to a system question? EE3 used to store that on both the attendee and the answers column,
112
+			// but not EE4! It's just stored in the attendee meta table. The answers table is ONLY for answers to custom
113
+			// questions.
114
+			return;
115
+		}
116
+		if ($question_row['QST_type'] == 'MULTIPLE') {
117
+			$ans_value = serialize(explode(",", stripslashes($old_answer['answer'])));
118
+		} else {
119
+			$ans_value = stripslashes($old_answer['answer']);
120
+		}
121
+		$cols_n_values = [
122
+			'REG_ID'    => $new_reg_id,
123
+			'QST_ID'    => $new_question_id,
124
+			'ANS_value' => $ans_value,
125
+		];
126
+		$data_types    = [
127
+			'%d',// REG_ID
128
+			'%d',// QST_ID
129
+			'%s',// ANS_value
130
+		];
131
+		$success       = $wpdb->insert($this->_new_answer_table, $cols_n_values, $data_types);
132
+		if (! $success) {
133
+			$this->add_error(
134
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
135
+					$this->_old_table,
136
+					$old_answer,
137
+					$this->_new_answer_table,
138
+					$cols_n_values,
139
+					$data_types
140
+				)
141
+			);
142
+		}
143
+	}
144 144
 
145 145
 
146
-    /**
147
-     * Gets the question's type
148
-     *
149
-     * @param int   $question_id
150
-     * @return array
151
-     *  - @type string $QST_type
152
-     *  - @type string $QST_system
153
-     * @global wpdb $wpdb
154
-     */
155
-    private function _get_question_type_and_system(int $question_id): array
156
-    {
157
-        global $wpdb;
158
-        return $wpdb->get_row(
159
-            $wpdb->prepare(
160
-                "SELECT QST_type, QST_system FROM " . $this->_new_question_table . " WHERE QST_ID=%d LIMIT 1",
161
-                $question_id
162
-            ),
163
-            ARRAY_A
164
-        );
165
-    }
146
+	/**
147
+	 * Gets the question's type
148
+	 *
149
+	 * @param int   $question_id
150
+	 * @return array
151
+	 *  - @type string $QST_type
152
+	 *  - @type string $QST_system
153
+	 * @global wpdb $wpdb
154
+	 */
155
+	private function _get_question_type_and_system(int $question_id): array
156
+	{
157
+		global $wpdb;
158
+		return $wpdb->get_row(
159
+			$wpdb->prepare(
160
+				"SELECT QST_type, QST_system FROM " . $this->_new_question_table . " WHERE QST_ID=%d LIMIT 1",
161
+				$question_id
162
+			),
163
+			ARRAY_A
164
+		);
165
+	}
166 166
 }
Please login to merge, or discard this patch.
Spacing   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -40,15 +40,15 @@  discard block
 block discarded – undo
40 40
     {
41 41
         global $wpdb;
42 42
         $this->_pretty_name = esc_html__("Answers", "event_espresso");
43
-        $this->_old_table   = $wpdb->prefix . "events_answer";
43
+        $this->_old_table   = $wpdb->prefix."events_answer";
44 44
         // join to attendee and then join to events table
45 45
         $this->select_expression   = 'ans.*, e.event_status';
46 46
         $this->_extra_where_sql    = ' AS ans
47
-            INNER JOIN ' . $wpdb->prefix . 'events_attendee AS att ON ans.attendee_id = att.id
48
-            INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id = e.id
47
+            INNER JOIN ' . $wpdb->prefix.'events_attendee AS att ON ans.attendee_id = att.id
48
+            INNER JOIN ' . $wpdb->prefix.'events_detail AS e ON att.event_id = e.id
49 49
             WHERE e.event_status !="D"';
50
-        $this->_new_answer_table   = $wpdb->prefix . "esp_answer";
51
-        $this->_new_question_table = $wpdb->prefix . "esp_question";
50
+        $this->_new_answer_table   = $wpdb->prefix."esp_answer";
51
+        $this->_new_question_table = $wpdb->prefix."esp_question";
52 52
         parent::__construct();
53 53
     }
54 54
 
@@ -60,15 +60,15 @@  discard block
 block discarded – undo
60 60
     {
61 61
         // get the new REGs for the old answer
62 62
         global $wpdb;
63
-        $old_attendee_table = $wpdb->prefix . "events_attendee";
64
-        $new_reg_table      = $wpdb->prefix . "esp_registration";
63
+        $old_attendee_table = $wpdb->prefix."events_attendee";
64
+        $new_reg_table      = $wpdb->prefix."esp_registration";
65 65
         $regs               =
66 66
             $this->get_migration_script()->get_mapping_new_pk(
67 67
                 $old_attendee_table,
68 68
                 $old_row['attendee_id'],
69 69
                 $new_reg_table
70 70
             );
71
-        if (! $regs) {
71
+        if ( ! $regs) {
72 72
             $this->add_error(
73 73
                 sprintf(
74 74
                     esc_html__(
@@ -98,7 +98,7 @@  discard block
 block discarded – undo
98 98
     private function _insert_new_answer(array $old_answer, int $new_reg_id): void
99 99
     {
100 100
         global $wpdb;
101
-        $old_question_table = $wpdb->prefix . "events_question";
101
+        $old_question_table = $wpdb->prefix."events_question";
102 102
         $new_question_id    =
103 103
             $this->get_migration_script()->get_mapping_new_pk(
104 104
                 $old_question_table,
@@ -123,13 +123,13 @@  discard block
 block discarded – undo
123 123
             'QST_ID'    => $new_question_id,
124 124
             'ANS_value' => $ans_value,
125 125
         ];
126
-        $data_types    = [
127
-            '%d',// REG_ID
128
-            '%d',// QST_ID
129
-            '%s',// ANS_value
126
+        $data_types = [
127
+            '%d', // REG_ID
128
+            '%d', // QST_ID
129
+            '%s', // ANS_value
130 130
         ];
131
-        $success       = $wpdb->insert($this->_new_answer_table, $cols_n_values, $data_types);
132
-        if (! $success) {
131
+        $success = $wpdb->insert($this->_new_answer_table, $cols_n_values, $data_types);
132
+        if ( ! $success) {
133 133
             $this->add_error(
134 134
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
135 135
                     $this->_old_table,
@@ -157,7 +157,7 @@  discard block
 block discarded – undo
157 157
         global $wpdb;
158 158
         return $wpdb->get_row(
159 159
             $wpdb->prepare(
160
-                "SELECT QST_type, QST_system FROM " . $this->_new_question_table . " WHERE QST_ID=%d LIMIT 1",
160
+                "SELECT QST_type, QST_system FROM ".$this->_new_question_table." WHERE QST_ID=%d LIMIT 1",
161 161
                 $question_id
162 162
             ),
163 163
             ARRAY_A
Please login to merge, or discard this patch.
data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_attendees.dmsstage.php 2 patches
Indentation   +912 added lines, -912 removed lines patch added patch discarded remove patch
@@ -223,937 +223,937 @@
 block discarded – undo
223 223
 class EE_DMS_4_1_0_attendees extends EE_Data_Migration_Script_Stage_Table
224 224
 {
225 225
 
226
-    private string $_new_attendee_cpt_table;
226
+	private string $_new_attendee_cpt_table;
227 227
 
228
-    private string $_new_attendee_meta_table;
228
+	private string $_new_attendee_meta_table;
229 229
 
230
-    private string $_new_reg_table;
230
+	private string $_new_reg_table;
231 231
 
232
-    private string $_new_transaction_table;
232
+	private string $_new_transaction_table;
233 233
 
234
-    private string $_new_payment_table;
234
+	private string $_new_payment_table;
235 235
 
236
-    private string $_old_mer_table;
236
+	private string $_old_mer_table;
237 237
 
238
-    private string $_new_ticket_table;
238
+	private string $_new_ticket_table;
239 239
 
240
-    private string $_new_ticket_datetime_table;
240
+	private string $_new_ticket_datetime_table;
241 241
 
242
-    private string $_new_datetime_table;
242
+	private string $_new_datetime_table;
243 243
 
244
-    private string $_new_datetime_ticket_table;
244
+	private string $_new_datetime_ticket_table;
245 245
 
246
-    private string $_new_price_table;
246
+	private string $_new_price_table;
247 247
 
248
-    private string $_new_ticket_price_table;
248
+	private string $_new_ticket_price_table;
249 249
 
250
-    /**
251
-     * Remembers whether the mer table exists
252
-     *
253
-     * @var boolean
254
-     */
255
-    private ?bool $_mer_tables_exist = null;
250
+	/**
251
+	 * Remembers whether the mer table exists
252
+	 *
253
+	 * @var boolean
254
+	 */
255
+	private ?bool $_mer_tables_exist = null;
256 256
 
257 257
 
258
-    public function __construct()
259
-    {
260
-        global $wpdb;
261
-        $this->_pretty_name = esc_html__("Attendees", "event_espresso");
262
-        $this->_old_table   = $wpdb->prefix . "events_attendee";
263
-        // Only select the event status column from the event table.
264
-        $this->select_expression = 'att.*, e.event_status';
265
-        // Only select attendees for events that aren't deleted.
266
-        $this->_extra_where_sql           = 'AS att
258
+	public function __construct()
259
+	{
260
+		global $wpdb;
261
+		$this->_pretty_name = esc_html__("Attendees", "event_espresso");
262
+		$this->_old_table   = $wpdb->prefix . "events_attendee";
263
+		// Only select the event status column from the event table.
264
+		$this->select_expression = 'att.*, e.event_status';
265
+		// Only select attendees for events that aren't deleted.
266
+		$this->_extra_where_sql           = 'AS att
267 267
             INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id
268 268
             WHERE e.event_status!="D"';
269
-        $this->_old_mer_table             = $wpdb->prefix . "events_multi_event_registration_id_group";
270
-        $this->_new_attendee_cpt_table    = $wpdb->posts;
271
-        $this->_new_attendee_meta_table   = $wpdb->prefix . "esp_attendee_meta";
272
-        $this->_new_reg_table             = $wpdb->prefix . "esp_registration";
273
-        $this->_new_transaction_table     = $wpdb->prefix . "esp_transaction";
274
-        $this->_new_payment_table         = $wpdb->prefix . "esp_payment";
275
-        $this->_new_ticket_table          = $wpdb->prefix . "esp_ticket";
276
-        $this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
277
-        $this->_new_datetime_table        = $wpdb->prefix . "esp_datetime";
278
-        $this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
279
-        $this->_new_price_table           = $wpdb->prefix . "esp_price";
280
-        $this->_new_ticket_price_table    = $wpdb->prefix . "esp_ticket_price";
281
-        parent::__construct();
282
-    }
283
-
284
-
285
-    /**
286
-     * @throws EE_Error
287
-     */
288
-    protected function _migrate_old_row($old_row)
289
-    {
290
-        // first check if there's already a new attendee with similar characteristics
291
-        $new_att_id = $this->_find_attendee_cpt_matching($old_row);
292
-        if (! $new_att_id) {
293
-            $new_att_id = $this->_insert_new_attendee_cpt($old_row);
294
-            if (! $new_att_id) {
295
-                // if we couldn't even make an attendee, abandon all hope
296
-                return;
297
-            }
298
-            $new_att_meta_id = $this->_insert_attendee_meta_row($old_row, $new_att_id);
299
-            if ($new_att_meta_id) {
300
-                $this->get_migration_script()->set_mapping(
301
-                    $this->_old_table,
302
-                    $old_row['id'],
303
-                    $this->_new_attendee_meta_table,
304
-                    $new_att_meta_id
305
-                );
306
-            }
307
-        }
308
-        $this->get_migration_script()->set_mapping(
309
-            $this->_old_table,
310
-            $old_row['id'],
311
-            $this->_new_attendee_cpt_table,
312
-            $new_att_id
313
-        );
314
-
315
-        $txn_id = $this->_insert_new_transaction($old_row);
316
-        if (! $txn_id) {
317
-            // if we couldn't make the transaction, also abandon all hope
318
-            return;
319
-        }
320
-        $this->get_migration_script()->set_mapping(
321
-            $this->_old_table,
322
-            $old_row['id'],
323
-            $this->_new_transaction_table,
324
-            $txn_id
325
-        );
326
-        $pay_id = $this->_insert_new_payment($old_row, $txn_id);
327
-        if ($pay_id) {
328
-            $this->get_migration_script()->set_mapping(
329
-                $this->_old_table,
330
-                $old_row['id'],
331
-                $this->_new_payment_table,
332
-                $pay_id
333
-            );
334
-        }
335
-
336
-
337
-        // even if there was no payment, we can go ahead with adding the reg
338
-        $new_regs = $this->_insert_new_registrations($old_row, $new_att_id, $txn_id);
339
-        if ($new_regs) {
340
-            $this->get_migration_script()->set_mapping(
341
-                $this->_old_table,
342
-                $old_row['id'],
343
-                $this->_new_reg_table,
344
-                $new_regs
345
-            );
346
-        }
347
-    }
348
-
349
-
350
-    /**
351
-     * Checks if there's already an attendee CPT in the db that has the same
352
-     * first and last name, and email. If so, returns its ID as an int.
353
-     *
354
-     * @param array $old_attendee
355
-     * @return int
356
-     * @global wpdb $wpdb
357
-     */
358
-    private function _find_attendee_cpt_matching(array $old_attendee): int
359
-    {
360
-        global $wpdb;
361
-        $existing_attendee_id =
362
-            $wpdb->get_var(
363
-                $wpdb->prepare(
364
-                    "SELECT id FROM " .
365
-                    $this->_new_attendee_cpt_table .
366
-                    " AS cpt INNER JOIN " .
367
-                    $this->_new_attendee_meta_table .
368
-                    " AS meta ON cpt.ID = meta.ATT_ID WHERE meta.ATT_fname = %s AND meta.ATT_lname = %s AND meta.ATT_email = %s LIMIT 1",
369
-                    $old_attendee['fname'],
370
-                    $old_attendee['lname'],
371
-                    $old_attendee['email']
372
-                )
373
-            );
374
-        return intval($existing_attendee_id);
375
-    }
376
-
377
-
378
-    private function _insert_new_attendee_cpt($old_attendee): int
379
-    {
380
-        global $wpdb;
381
-        $cols_n_values = [
382
-            'post_title'    => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
383
-            'post_content'  => '',// ATT_bio
384
-            'post_name'     => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
385
-            'post_date'     => $this->get_migration_script()->convert_date_string_to_utc(
386
-                $this,
387
-                $old_attendee,
388
-                $old_attendee['date']
389
-            ),// ATT_created
390
-            'post_excerpt'  => '',// ATT_short_bio
391
-            'post_modified' => $this->get_migration_script()->convert_date_string_to_utc(
392
-                $this,
393
-                $old_attendee,
394
-                $old_attendee['date']
395
-            ),// ATT_modified
396
-            'post_author'   => 0,// ATT_author
397
-            'post_parent'   => 0,// ATT_parent
398
-            'post_type'     => 'espresso_attendees',// post_type
399
-            'post_status'   => 'publish',// status
400
-        ];
401
-        $data_types    = [
402
-            '%s',// ATT_full_name
403
-            '%s',// ATT_bio
404
-            '%s',// ATT_slug
405
-            '%s',// ATT_created
406
-            '%s',// ATT_short_bio
407
-            '%s',// ATT_modified
408
-            '%d',// ATT_author
409
-            '%d',// ATT_parent
410
-            '%s',// post_type
411
-            '%s',// status
412
-        ];
413
-        $success       = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $data_types);
414
-        if (! $success) {
415
-            $this->add_error(
416
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
417
-                    $this->_old_table,
418
-                    $old_attendee,
419
-                    $this->_new_attendee_cpt_table,
420
-                    $cols_n_values,
421
-                    $data_types
422
-                )
423
-            );
424
-            return 0;
425
-        }
426
-        return $wpdb->insert_id;
427
-    }
428
-
429
-
430
-    private function _insert_attendee_meta_row($old_attendee, $new_attendee_cpt_id): int
431
-    {
432
-        global $wpdb;
433
-        // get the state and country ids from the old row
434
-        try {
435
-            $new_country     =
436
-                $this->get_migration_script()->get_or_create_country(stripslashes($old_attendee['country_id']));
437
-            $new_country_iso = $new_country['CNT_ISO'];
438
-        } catch (EE_Error $exception) {
439
-            $new_country_iso = $this->get_migration_script()->get_default_country_iso();
440
-        }
441
-        try {
442
-            $new_state    =
443
-                $this->get_migration_script()->get_or_create_state(
444
-                    stripslashes($old_attendee['state']),
445
-                    $new_country_iso
446
-                );
447
-            $new_state_id = $new_state['STA_ID'];
448
-        } catch (EE_Error $exception) {
449
-            $new_state_id = 0;
450
-        }
451
-        $cols_n_values = [
452
-            'ATT_ID'       => $new_attendee_cpt_id,
453
-            'ATT_fname'    => stripslashes($old_attendee['fname']),
454
-            'ATT_lname'    => stripslashes($old_attendee['lname']),
455
-            'ATT_address'  => stripslashes($old_attendee['address']),
456
-            'ATT_address2' => stripslashes($old_attendee['address2']),
457
-            'ATT_city'     => stripslashes($old_attendee['city']),
458
-            'STA_ID'       => $new_state_id,
459
-            'CNT_ISO'      => $new_country_iso,
460
-            'ATT_zip'      => stripslashes($old_attendee['zip']),
461
-            'ATT_email'    => stripslashes($old_attendee['email']),
462
-            'ATT_phone'    => stripslashes($old_attendee['phone']),
463
-        ];
464
-        $data_types    = [
465
-            '%d',// ATT_ID
466
-            '%s',// ATT_fname
467
-            '%s',// ATT_lname
468
-            '%s',// ATT_address
469
-            '%s',// ATT_address2
470
-            '%s',// ATT_city
471
-            '%d',// STA_ID
472
-            '%s',// CNT_ISO
473
-            '%s',// ATT_zip
474
-            '%s',// ATT_email
475
-            '%s',// ATT_phone
476
-        ];
477
-        $success       = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $data_types);
478
-        if (! $success) {
479
-            $this->add_error(
480
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
481
-                    $this->_old_table,
482
-                    $old_attendee,
483
-                    $this->_new_attendee_meta_table,
484
-                    $cols_n_values,
485
-                    $data_types
486
-                )
487
-            );
488
-            return 0;
489
-        }
490
-        return $wpdb->insert_id;
491
-    }
492
-
493
-
494
-    /**
495
-     * Note: we don't necessarily create a new transaction for each attendee row.
496
-     * Only if the old attendee 'is_primary' is true; otherwise we find the old attendee row that
497
-     * 'is_primary' and has the same 'txn_id', then we return ITS new transaction id
498
-     *
499
-     * @param array $old_attendee
500
-     * @return int new transaction id
501
-     * @throws EE_Error
502
-     * @global wpdb $wpdb
503
-     */
504
-    private function _insert_new_transaction(array $old_attendee): int
505
-    {
506
-        global $wpdb;
507
-
508
-        // first: let's check for an existing transaction for this old attendee
509
-        if (intval($old_attendee['is_primary'])) {// primary attendee, so create txn
510
-            $txn_id =
511
-                $this->get_migration_script()->get_mapping_new_pk(
512
-                    $this->_old_table,
513
-                    intval($old_attendee['id']),
514
-                    $this->_new_transaction_table
515
-                );
516
-        } else { // non-primary attendee, so find its primary attendee's transaction
517
-            $primary_attendee_old_id =
518
-                $wpdb->get_var(
519
-                    $wpdb->prepare(
520
-                        "SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s",
521
-                        $old_attendee['registration_id']
522
-                    )
523
-                );
524
-            if (! $primary_attendee_old_id) {
525
-                $primary_attendee        =
526
-                    $this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
527
-                $primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
528
-            }
529
-            $txn_id =
530
-                $this->get_migration_script()->get_mapping_new_pk(
531
-                    $this->_old_table,
532
-                    intval($primary_attendee_old_id),
533
-                    $this->_new_transaction_table
534
-                );
535
-            if (! $txn_id) {
536
-                $this->add_error(
537
-                    sprintf(
538
-                        esc_html__(
539
-                            "Could not find primary attendee's new transaction. Current attendee is: %s, we think the 3.1 primary attendee for it has id %d, but there's no 4.1 transaction for that primary attendee id.",
540
-                            "event_espresso"
541
-                        ),
542
-                        $this->_json_encode($old_attendee),
543
-                        $primary_attendee_old_id
544
-                    )
545
-                );
546
-                $txn_id = 0;
547
-            }
548
-        }
549
-        // if there isn't yet a transaction row for this, create one
550
-        // (so even if it was a non-primary attendee with no EE3 primary attendee,
551
-        // it ought to have SOME transaction, so we'll make one)
552
-        if (! $txn_id) {
553
-            // maps 3.1 payment stati onto 4.1 transaction stati
554
-            $txn_status_mapping = [
555
-                'Completed'        => 'TCM',
556
-                'Pending'          => 'TIN',
557
-                'Payment Declined' => 'TIN',
558
-                'Incomplete'       => 'TIN',
559
-                'Not Completed'    => 'TIN',
560
-                'Cancelled'        => 'TIN',
561
-                'Declined'         => 'TIN',
562
-            ];
563
-            $STS_ID             = $txn_status_mapping[ $old_attendee['payment_status'] ] ?? 'TIN';
564
-            $cols_n_values      = [
565
-                'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc(
566
-                    $this,
567
-                    $old_attendee,
568
-                    $old_attendee['date']
569
-                ),
570
-                'TXN_total'     => floatval($old_attendee['total_cost']),
571
-                'TXN_paid'      => floatval($old_attendee['amount_pd']),
572
-                'STS_ID'        => $STS_ID,
573
-                'TXN_hash_salt' => $old_attendee['hashSalt'],
574
-            ];
575
-            $data_types         = [
576
-                '%s',// TXN_timestamp
577
-                '%f',// TXN_total
578
-                '%f',// TXN_paid
579
-                '%s',// STS_ID
580
-                '%s',// TXN_hash_salt
581
-            ];
582
-            $success            = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $data_types);
583
-            if (! $success) {
584
-                $this->add_error(
585
-                    $this->get_migration_script()->_create_error_message_for_db_insertion(
586
-                        $this->_old_table,
587
-                        $old_attendee,
588
-                        $this->_new_transaction_table,
589
-                        $cols_n_values,
590
-                        $data_types
591
-                    )
592
-                );
593
-                return 0;
594
-            }
595
-            $txn_id = $wpdb->insert_id;
596
-        }
597
-
598
-        return $txn_id;
599
-    }
600
-
601
-
602
-    /**
603
-     * Detects if the MER tables exist
604
-     *
605
-     * @return boolean
606
-     * @global wpdb $wpdb
607
-     */
608
-    private function _mer_tables_exist(): ?bool
609
-    {
610
-        if ($this->_mer_tables_exist === null) {
611
-            global $wpdb;
612
-
613
-            if ($wpdb->get_var("SHOW TABLES LIKE '$this->_old_mer_table'") != $this->_old_mer_table) {
614
-                $this->_mer_tables_exist = false;
615
-            } else {
616
-                $this->_mer_tables_exist = true;
617
-            }
618
-        }
619
-        return $this->_mer_tables_exist;
620
-    }
621
-
622
-
623
-    /**
624
-     * Gets the 4.1 registration's status given the 3.1 attendee row. We consider
625
-     * whether the event required pre-approval or not,a dn the 4.1 payment status.
626
-     *
627
-     * @param array $old_attendee_row
628
-     * @return string
629
-     * @global wpdb $wpdb
630
-     */
631
-    private function _get_reg_status_for_old_payment_status(array $old_attendee_row): string
632
-    {
633
-        // need event default reg status and if pre_approval was required
634
-        global $wpdb;
635
-        $event_required_pre_approval =
636
-            $wpdb->get_var(
637
-                $wpdb->prepare(
638
-                    "SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d",
639
-                    $old_attendee_row['event_id']
640
-                )
641
-            );
642
-        return $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
643
-            $old_attendee_row['payment_status'],
644
-            intval($event_required_pre_approval) && intval($old_attendee_row['pre_approve'])
645
-        );
646
-    }
647
-
648
-
649
-    /**
650
-     * Adds however many registrations are indicated by the old attendee's QUANTITY field,
651
-     * and returns an array of their IDs
652
-     *
653
-     * @param array $old_attendee
654
-     * @param int   $new_attendee_id
655
-     * @param int   $new_txn_id
656
-     * @return array of new registration ids
657
-     * @throws EE_Error
658
-     * @global wpdb $wpdb
659
-     */
660
-    private function _insert_new_registrations(array $old_attendee, int $new_attendee_id, int $new_txn_id): array
661
-    {
662
-        global $wpdb;
663
-
664
-        $STS_ID       = $this->_get_reg_status_for_old_payment_status($old_attendee);
665
-        $new_event_id =
666
-            $this->get_migration_script()->get_mapping_new_pk(
667
-                $wpdb->prefix . 'events_detail',
668
-                $old_attendee['event_id'],
669
-                $wpdb->posts
670
-            );
671
-        if (! $new_event_id) {
672
-            $this->add_error(
673
-                sprintf(
674
-                    esc_html__(
675
-                        "Could not find NEW event CPT ID for old event '%d' on old attendee %s",
676
-                        "event_espresso"
677
-                    ),
678
-                    $old_attendee['event_id'],
679
-                    $this->_json_encode($old_attendee)
680
-                )
681
-            );
682
-        }
683
-
684
-        $ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
685
-        if (! $ticket_id) {
686
-            $ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
687
-            $this->add_error(
688
-                sprintf(
689
-                    esc_html__(
690
-                        'Could not find a ticket for old attendee with id %d for new event %d, so created a new ticket with id %d',
691
-                        'event_espresso'
692
-                    ),
693
-                    $old_attendee['id'],
694
-                    $new_event_id,
695
-                    $ticket_id
696
-                )
697
-            );
698
-        }
699
-        $regs_on_this_row = intval($old_attendee['quantity']);
700
-        $new_regs         = [];
701
-        // 4 cases we need to account for:
702
-        // 1 old attendee_details row with a quantity of X (no mer)
703
-        // Y old attendee_details rows with a quantity of 1 (no mer) joined by their common registration_id
704
-        // Y old attendee_details rows with a quantity of x (because of mer)
705
-        // Y old attendee_details rows with a quantity of 1 (because of mer) joined by wp_events_multi_event_registration_id_group
706
-        for ($count = 1; $count <= $regs_on_this_row; $count++) {
707
-            // sum regs on older rows
708
-            $regs_on_this_event_and_txn = $this->_sum_old_attendees_on_old_txn($old_attendee, true);
709
-            $cols_n_values              = [
710
-                'EVT_ID'           => $new_event_id,
711
-                'ATT_ID'           => $new_attendee_id,
712
-                'TXN_ID'           => $new_txn_id,
713
-                'TKT_ID'           => $ticket_id,
714
-                'STS_ID'           => $STS_ID,
715
-                'REG_date'         => $this->get_migration_script()->convert_date_string_to_utc(
716
-                    $this,
717
-                    $old_attendee,
718
-                    $old_attendee['date']
719
-                ),
720
-                'REG_final_price'  => $old_attendee['final_price'],
721
-                'REG_session'      => substr($old_attendee['attendee_session'], 0, 44),
722
-                'REG_code'         => sanitize_key($old_attendee['registration_id']),
723
-                'REG_url_link'     => sanitize_key($old_attendee['registration_id'] . '-' . $count),
724
-                'REG_count'        => $regs_on_this_event_and_txn + $count,
725
-                'REG_group_size'   => $this->_sum_old_attendees_on_old_txn($old_attendee),
726
-                'REG_att_is_going' => true,
727
-                'REG_deleted'      => false,
728
-            ];
729
-            $data_types                 = [
730
-                '%d',// EVT_ID
731
-                '%d',// ATT_ID
732
-                '%d',// TXN_ID
733
-                '%d',// TKT_ID
734
-                '%s',// STS_ID
735
-                '%s',// REG_date
736
-                '%f',// REG_final_price
737
-                '%s',// REG_session
738
-                '%s',// REG_code
739
-                '%s',// REG_url_link
740
-                '%d',// REG_count
741
-                '%d',// REG_group_size
742
-                '%d',// REG_att_is_going
743
-                '%d',// REG_deleted
744
-            ];
745
-            $success                    = $wpdb->insert($this->_new_reg_table, $cols_n_values, $data_types);
746
-            if (! $success) {
747
-                $this->add_error(
748
-                    $this->get_migration_script()->_create_error_message_for_db_insertion(
749
-                        $this->_old_table,
750
-                        $old_attendee,
751
-                        $this->_new_reg_table,
752
-                        $cols_n_values,
753
-                        $data_types
754
-                    )
755
-                );
756
-                return [];
757
-            }
758
-            $cols_n_values['REG_ID'] = $wpdb->insert_id;
759
-            $new_regs[]              = $wpdb->insert_id;
760
-        }
761
-        $this->_add_regs_to_ticket_and_datetimes($ticket_id, count($new_regs), $STS_ID);
762
-        return $new_regs;
763
-    }
764
-
765
-
766
-    /**
767
-     * Increments the sold values on the ticket and its related datetimes by the amount sold,
768
-     * which should be done directly after adding the rows. Yes this means we're constantly incrementing
769
-     * the sold amounts as we go, and is less efficient than a single big query,
770
-     * but it's safer because we KNOW these regs have been added, rather than inferring
771
-     * that they WILL be added (because the attendees stage runs nearly last during
772
-     * the migration script)
773
-     *
774
-     * @param int    $new_ticket_id
775
-     * @param int    $quantity_sold
776
-     * @param string $STS_ID
777
-     * @return boolean whether they were successfully updated or not
778
-     */
779
-    protected function _add_regs_to_ticket_and_datetimes(int $new_ticket_id, int $quantity_sold, string $STS_ID): bool
780
-    {
781
-        if ($STS_ID != 'RAP') {
782
-            return true;
783
-        }
784
-        global $wpdb;
785
-        $success =
786
-            $wpdb->query(
787
-                $wpdb->prepare(
788
-                    "UPDATE $this->_new_ticket_table SET TKT_sold=TKT_sold+%d WHERE TKT_ID=%d",
789
-                    $quantity_sold,
790
-                    $new_ticket_id
791
-                )
792
-            );
793
-        if ($success) {
794
-            // get the ticket's datetimes, and increment them too
795
-            $success_update_datetimes = $wpdb->query(
796
-                $wpdb->prepare(
797
-                    "UPDATE $this->_new_ticket_table TKT
269
+		$this->_old_mer_table             = $wpdb->prefix . "events_multi_event_registration_id_group";
270
+		$this->_new_attendee_cpt_table    = $wpdb->posts;
271
+		$this->_new_attendee_meta_table   = $wpdb->prefix . "esp_attendee_meta";
272
+		$this->_new_reg_table             = $wpdb->prefix . "esp_registration";
273
+		$this->_new_transaction_table     = $wpdb->prefix . "esp_transaction";
274
+		$this->_new_payment_table         = $wpdb->prefix . "esp_payment";
275
+		$this->_new_ticket_table          = $wpdb->prefix . "esp_ticket";
276
+		$this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
277
+		$this->_new_datetime_table        = $wpdb->prefix . "esp_datetime";
278
+		$this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
279
+		$this->_new_price_table           = $wpdb->prefix . "esp_price";
280
+		$this->_new_ticket_price_table    = $wpdb->prefix . "esp_ticket_price";
281
+		parent::__construct();
282
+	}
283
+
284
+
285
+	/**
286
+	 * @throws EE_Error
287
+	 */
288
+	protected function _migrate_old_row($old_row)
289
+	{
290
+		// first check if there's already a new attendee with similar characteristics
291
+		$new_att_id = $this->_find_attendee_cpt_matching($old_row);
292
+		if (! $new_att_id) {
293
+			$new_att_id = $this->_insert_new_attendee_cpt($old_row);
294
+			if (! $new_att_id) {
295
+				// if we couldn't even make an attendee, abandon all hope
296
+				return;
297
+			}
298
+			$new_att_meta_id = $this->_insert_attendee_meta_row($old_row, $new_att_id);
299
+			if ($new_att_meta_id) {
300
+				$this->get_migration_script()->set_mapping(
301
+					$this->_old_table,
302
+					$old_row['id'],
303
+					$this->_new_attendee_meta_table,
304
+					$new_att_meta_id
305
+				);
306
+			}
307
+		}
308
+		$this->get_migration_script()->set_mapping(
309
+			$this->_old_table,
310
+			$old_row['id'],
311
+			$this->_new_attendee_cpt_table,
312
+			$new_att_id
313
+		);
314
+
315
+		$txn_id = $this->_insert_new_transaction($old_row);
316
+		if (! $txn_id) {
317
+			// if we couldn't make the transaction, also abandon all hope
318
+			return;
319
+		}
320
+		$this->get_migration_script()->set_mapping(
321
+			$this->_old_table,
322
+			$old_row['id'],
323
+			$this->_new_transaction_table,
324
+			$txn_id
325
+		);
326
+		$pay_id = $this->_insert_new_payment($old_row, $txn_id);
327
+		if ($pay_id) {
328
+			$this->get_migration_script()->set_mapping(
329
+				$this->_old_table,
330
+				$old_row['id'],
331
+				$this->_new_payment_table,
332
+				$pay_id
333
+			);
334
+		}
335
+
336
+
337
+		// even if there was no payment, we can go ahead with adding the reg
338
+		$new_regs = $this->_insert_new_registrations($old_row, $new_att_id, $txn_id);
339
+		if ($new_regs) {
340
+			$this->get_migration_script()->set_mapping(
341
+				$this->_old_table,
342
+				$old_row['id'],
343
+				$this->_new_reg_table,
344
+				$new_regs
345
+			);
346
+		}
347
+	}
348
+
349
+
350
+	/**
351
+	 * Checks if there's already an attendee CPT in the db that has the same
352
+	 * first and last name, and email. If so, returns its ID as an int.
353
+	 *
354
+	 * @param array $old_attendee
355
+	 * @return int
356
+	 * @global wpdb $wpdb
357
+	 */
358
+	private function _find_attendee_cpt_matching(array $old_attendee): int
359
+	{
360
+		global $wpdb;
361
+		$existing_attendee_id =
362
+			$wpdb->get_var(
363
+				$wpdb->prepare(
364
+					"SELECT id FROM " .
365
+					$this->_new_attendee_cpt_table .
366
+					" AS cpt INNER JOIN " .
367
+					$this->_new_attendee_meta_table .
368
+					" AS meta ON cpt.ID = meta.ATT_ID WHERE meta.ATT_fname = %s AND meta.ATT_lname = %s AND meta.ATT_email = %s LIMIT 1",
369
+					$old_attendee['fname'],
370
+					$old_attendee['lname'],
371
+					$old_attendee['email']
372
+				)
373
+			);
374
+		return intval($existing_attendee_id);
375
+	}
376
+
377
+
378
+	private function _insert_new_attendee_cpt($old_attendee): int
379
+	{
380
+		global $wpdb;
381
+		$cols_n_values = [
382
+			'post_title'    => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
383
+			'post_content'  => '',// ATT_bio
384
+			'post_name'     => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
385
+			'post_date'     => $this->get_migration_script()->convert_date_string_to_utc(
386
+				$this,
387
+				$old_attendee,
388
+				$old_attendee['date']
389
+			),// ATT_created
390
+			'post_excerpt'  => '',// ATT_short_bio
391
+			'post_modified' => $this->get_migration_script()->convert_date_string_to_utc(
392
+				$this,
393
+				$old_attendee,
394
+				$old_attendee['date']
395
+			),// ATT_modified
396
+			'post_author'   => 0,// ATT_author
397
+			'post_parent'   => 0,// ATT_parent
398
+			'post_type'     => 'espresso_attendees',// post_type
399
+			'post_status'   => 'publish',// status
400
+		];
401
+		$data_types    = [
402
+			'%s',// ATT_full_name
403
+			'%s',// ATT_bio
404
+			'%s',// ATT_slug
405
+			'%s',// ATT_created
406
+			'%s',// ATT_short_bio
407
+			'%s',// ATT_modified
408
+			'%d',// ATT_author
409
+			'%d',// ATT_parent
410
+			'%s',// post_type
411
+			'%s',// status
412
+		];
413
+		$success       = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $data_types);
414
+		if (! $success) {
415
+			$this->add_error(
416
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
417
+					$this->_old_table,
418
+					$old_attendee,
419
+					$this->_new_attendee_cpt_table,
420
+					$cols_n_values,
421
+					$data_types
422
+				)
423
+			);
424
+			return 0;
425
+		}
426
+		return $wpdb->insert_id;
427
+	}
428
+
429
+
430
+	private function _insert_attendee_meta_row($old_attendee, $new_attendee_cpt_id): int
431
+	{
432
+		global $wpdb;
433
+		// get the state and country ids from the old row
434
+		try {
435
+			$new_country     =
436
+				$this->get_migration_script()->get_or_create_country(stripslashes($old_attendee['country_id']));
437
+			$new_country_iso = $new_country['CNT_ISO'];
438
+		} catch (EE_Error $exception) {
439
+			$new_country_iso = $this->get_migration_script()->get_default_country_iso();
440
+		}
441
+		try {
442
+			$new_state    =
443
+				$this->get_migration_script()->get_or_create_state(
444
+					stripslashes($old_attendee['state']),
445
+					$new_country_iso
446
+				);
447
+			$new_state_id = $new_state['STA_ID'];
448
+		} catch (EE_Error $exception) {
449
+			$new_state_id = 0;
450
+		}
451
+		$cols_n_values = [
452
+			'ATT_ID'       => $new_attendee_cpt_id,
453
+			'ATT_fname'    => stripslashes($old_attendee['fname']),
454
+			'ATT_lname'    => stripslashes($old_attendee['lname']),
455
+			'ATT_address'  => stripslashes($old_attendee['address']),
456
+			'ATT_address2' => stripslashes($old_attendee['address2']),
457
+			'ATT_city'     => stripslashes($old_attendee['city']),
458
+			'STA_ID'       => $new_state_id,
459
+			'CNT_ISO'      => $new_country_iso,
460
+			'ATT_zip'      => stripslashes($old_attendee['zip']),
461
+			'ATT_email'    => stripslashes($old_attendee['email']),
462
+			'ATT_phone'    => stripslashes($old_attendee['phone']),
463
+		];
464
+		$data_types    = [
465
+			'%d',// ATT_ID
466
+			'%s',// ATT_fname
467
+			'%s',// ATT_lname
468
+			'%s',// ATT_address
469
+			'%s',// ATT_address2
470
+			'%s',// ATT_city
471
+			'%d',// STA_ID
472
+			'%s',// CNT_ISO
473
+			'%s',// ATT_zip
474
+			'%s',// ATT_email
475
+			'%s',// ATT_phone
476
+		];
477
+		$success       = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $data_types);
478
+		if (! $success) {
479
+			$this->add_error(
480
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
481
+					$this->_old_table,
482
+					$old_attendee,
483
+					$this->_new_attendee_meta_table,
484
+					$cols_n_values,
485
+					$data_types
486
+				)
487
+			);
488
+			return 0;
489
+		}
490
+		return $wpdb->insert_id;
491
+	}
492
+
493
+
494
+	/**
495
+	 * Note: we don't necessarily create a new transaction for each attendee row.
496
+	 * Only if the old attendee 'is_primary' is true; otherwise we find the old attendee row that
497
+	 * 'is_primary' and has the same 'txn_id', then we return ITS new transaction id
498
+	 *
499
+	 * @param array $old_attendee
500
+	 * @return int new transaction id
501
+	 * @throws EE_Error
502
+	 * @global wpdb $wpdb
503
+	 */
504
+	private function _insert_new_transaction(array $old_attendee): int
505
+	{
506
+		global $wpdb;
507
+
508
+		// first: let's check for an existing transaction for this old attendee
509
+		if (intval($old_attendee['is_primary'])) {// primary attendee, so create txn
510
+			$txn_id =
511
+				$this->get_migration_script()->get_mapping_new_pk(
512
+					$this->_old_table,
513
+					intval($old_attendee['id']),
514
+					$this->_new_transaction_table
515
+				);
516
+		} else { // non-primary attendee, so find its primary attendee's transaction
517
+			$primary_attendee_old_id =
518
+				$wpdb->get_var(
519
+					$wpdb->prepare(
520
+						"SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s",
521
+						$old_attendee['registration_id']
522
+					)
523
+				);
524
+			if (! $primary_attendee_old_id) {
525
+				$primary_attendee        =
526
+					$this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
527
+				$primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
528
+			}
529
+			$txn_id =
530
+				$this->get_migration_script()->get_mapping_new_pk(
531
+					$this->_old_table,
532
+					intval($primary_attendee_old_id),
533
+					$this->_new_transaction_table
534
+				);
535
+			if (! $txn_id) {
536
+				$this->add_error(
537
+					sprintf(
538
+						esc_html__(
539
+							"Could not find primary attendee's new transaction. Current attendee is: %s, we think the 3.1 primary attendee for it has id %d, but there's no 4.1 transaction for that primary attendee id.",
540
+							"event_espresso"
541
+						),
542
+						$this->_json_encode($old_attendee),
543
+						$primary_attendee_old_id
544
+					)
545
+				);
546
+				$txn_id = 0;
547
+			}
548
+		}
549
+		// if there isn't yet a transaction row for this, create one
550
+		// (so even if it was a non-primary attendee with no EE3 primary attendee,
551
+		// it ought to have SOME transaction, so we'll make one)
552
+		if (! $txn_id) {
553
+			// maps 3.1 payment stati onto 4.1 transaction stati
554
+			$txn_status_mapping = [
555
+				'Completed'        => 'TCM',
556
+				'Pending'          => 'TIN',
557
+				'Payment Declined' => 'TIN',
558
+				'Incomplete'       => 'TIN',
559
+				'Not Completed'    => 'TIN',
560
+				'Cancelled'        => 'TIN',
561
+				'Declined'         => 'TIN',
562
+			];
563
+			$STS_ID             = $txn_status_mapping[ $old_attendee['payment_status'] ] ?? 'TIN';
564
+			$cols_n_values      = [
565
+				'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc(
566
+					$this,
567
+					$old_attendee,
568
+					$old_attendee['date']
569
+				),
570
+				'TXN_total'     => floatval($old_attendee['total_cost']),
571
+				'TXN_paid'      => floatval($old_attendee['amount_pd']),
572
+				'STS_ID'        => $STS_ID,
573
+				'TXN_hash_salt' => $old_attendee['hashSalt'],
574
+			];
575
+			$data_types         = [
576
+				'%s',// TXN_timestamp
577
+				'%f',// TXN_total
578
+				'%f',// TXN_paid
579
+				'%s',// STS_ID
580
+				'%s',// TXN_hash_salt
581
+			];
582
+			$success            = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $data_types);
583
+			if (! $success) {
584
+				$this->add_error(
585
+					$this->get_migration_script()->_create_error_message_for_db_insertion(
586
+						$this->_old_table,
587
+						$old_attendee,
588
+						$this->_new_transaction_table,
589
+						$cols_n_values,
590
+						$data_types
591
+					)
592
+				);
593
+				return 0;
594
+			}
595
+			$txn_id = $wpdb->insert_id;
596
+		}
597
+
598
+		return $txn_id;
599
+	}
600
+
601
+
602
+	/**
603
+	 * Detects if the MER tables exist
604
+	 *
605
+	 * @return boolean
606
+	 * @global wpdb $wpdb
607
+	 */
608
+	private function _mer_tables_exist(): ?bool
609
+	{
610
+		if ($this->_mer_tables_exist === null) {
611
+			global $wpdb;
612
+
613
+			if ($wpdb->get_var("SHOW TABLES LIKE '$this->_old_mer_table'") != $this->_old_mer_table) {
614
+				$this->_mer_tables_exist = false;
615
+			} else {
616
+				$this->_mer_tables_exist = true;
617
+			}
618
+		}
619
+		return $this->_mer_tables_exist;
620
+	}
621
+
622
+
623
+	/**
624
+	 * Gets the 4.1 registration's status given the 3.1 attendee row. We consider
625
+	 * whether the event required pre-approval or not,a dn the 4.1 payment status.
626
+	 *
627
+	 * @param array $old_attendee_row
628
+	 * @return string
629
+	 * @global wpdb $wpdb
630
+	 */
631
+	private function _get_reg_status_for_old_payment_status(array $old_attendee_row): string
632
+	{
633
+		// need event default reg status and if pre_approval was required
634
+		global $wpdb;
635
+		$event_required_pre_approval =
636
+			$wpdb->get_var(
637
+				$wpdb->prepare(
638
+					"SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d",
639
+					$old_attendee_row['event_id']
640
+				)
641
+			);
642
+		return $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
643
+			$old_attendee_row['payment_status'],
644
+			intval($event_required_pre_approval) && intval($old_attendee_row['pre_approve'])
645
+		);
646
+	}
647
+
648
+
649
+	/**
650
+	 * Adds however many registrations are indicated by the old attendee's QUANTITY field,
651
+	 * and returns an array of their IDs
652
+	 *
653
+	 * @param array $old_attendee
654
+	 * @param int   $new_attendee_id
655
+	 * @param int   $new_txn_id
656
+	 * @return array of new registration ids
657
+	 * @throws EE_Error
658
+	 * @global wpdb $wpdb
659
+	 */
660
+	private function _insert_new_registrations(array $old_attendee, int $new_attendee_id, int $new_txn_id): array
661
+	{
662
+		global $wpdb;
663
+
664
+		$STS_ID       = $this->_get_reg_status_for_old_payment_status($old_attendee);
665
+		$new_event_id =
666
+			$this->get_migration_script()->get_mapping_new_pk(
667
+				$wpdb->prefix . 'events_detail',
668
+				$old_attendee['event_id'],
669
+				$wpdb->posts
670
+			);
671
+		if (! $new_event_id) {
672
+			$this->add_error(
673
+				sprintf(
674
+					esc_html__(
675
+						"Could not find NEW event CPT ID for old event '%d' on old attendee %s",
676
+						"event_espresso"
677
+					),
678
+					$old_attendee['event_id'],
679
+					$this->_json_encode($old_attendee)
680
+				)
681
+			);
682
+		}
683
+
684
+		$ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
685
+		if (! $ticket_id) {
686
+			$ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
687
+			$this->add_error(
688
+				sprintf(
689
+					esc_html__(
690
+						'Could not find a ticket for old attendee with id %d for new event %d, so created a new ticket with id %d',
691
+						'event_espresso'
692
+					),
693
+					$old_attendee['id'],
694
+					$new_event_id,
695
+					$ticket_id
696
+				)
697
+			);
698
+		}
699
+		$regs_on_this_row = intval($old_attendee['quantity']);
700
+		$new_regs         = [];
701
+		// 4 cases we need to account for:
702
+		// 1 old attendee_details row with a quantity of X (no mer)
703
+		// Y old attendee_details rows with a quantity of 1 (no mer) joined by their common registration_id
704
+		// Y old attendee_details rows with a quantity of x (because of mer)
705
+		// Y old attendee_details rows with a quantity of 1 (because of mer) joined by wp_events_multi_event_registration_id_group
706
+		for ($count = 1; $count <= $regs_on_this_row; $count++) {
707
+			// sum regs on older rows
708
+			$regs_on_this_event_and_txn = $this->_sum_old_attendees_on_old_txn($old_attendee, true);
709
+			$cols_n_values              = [
710
+				'EVT_ID'           => $new_event_id,
711
+				'ATT_ID'           => $new_attendee_id,
712
+				'TXN_ID'           => $new_txn_id,
713
+				'TKT_ID'           => $ticket_id,
714
+				'STS_ID'           => $STS_ID,
715
+				'REG_date'         => $this->get_migration_script()->convert_date_string_to_utc(
716
+					$this,
717
+					$old_attendee,
718
+					$old_attendee['date']
719
+				),
720
+				'REG_final_price'  => $old_attendee['final_price'],
721
+				'REG_session'      => substr($old_attendee['attendee_session'], 0, 44),
722
+				'REG_code'         => sanitize_key($old_attendee['registration_id']),
723
+				'REG_url_link'     => sanitize_key($old_attendee['registration_id'] . '-' . $count),
724
+				'REG_count'        => $regs_on_this_event_and_txn + $count,
725
+				'REG_group_size'   => $this->_sum_old_attendees_on_old_txn($old_attendee),
726
+				'REG_att_is_going' => true,
727
+				'REG_deleted'      => false,
728
+			];
729
+			$data_types                 = [
730
+				'%d',// EVT_ID
731
+				'%d',// ATT_ID
732
+				'%d',// TXN_ID
733
+				'%d',// TKT_ID
734
+				'%s',// STS_ID
735
+				'%s',// REG_date
736
+				'%f',// REG_final_price
737
+				'%s',// REG_session
738
+				'%s',// REG_code
739
+				'%s',// REG_url_link
740
+				'%d',// REG_count
741
+				'%d',// REG_group_size
742
+				'%d',// REG_att_is_going
743
+				'%d',// REG_deleted
744
+			];
745
+			$success                    = $wpdb->insert($this->_new_reg_table, $cols_n_values, $data_types);
746
+			if (! $success) {
747
+				$this->add_error(
748
+					$this->get_migration_script()->_create_error_message_for_db_insertion(
749
+						$this->_old_table,
750
+						$old_attendee,
751
+						$this->_new_reg_table,
752
+						$cols_n_values,
753
+						$data_types
754
+					)
755
+				);
756
+				return [];
757
+			}
758
+			$cols_n_values['REG_ID'] = $wpdb->insert_id;
759
+			$new_regs[]              = $wpdb->insert_id;
760
+		}
761
+		$this->_add_regs_to_ticket_and_datetimes($ticket_id, count($new_regs), $STS_ID);
762
+		return $new_regs;
763
+	}
764
+
765
+
766
+	/**
767
+	 * Increments the sold values on the ticket and its related datetimes by the amount sold,
768
+	 * which should be done directly after adding the rows. Yes this means we're constantly incrementing
769
+	 * the sold amounts as we go, and is less efficient than a single big query,
770
+	 * but it's safer because we KNOW these regs have been added, rather than inferring
771
+	 * that they WILL be added (because the attendees stage runs nearly last during
772
+	 * the migration script)
773
+	 *
774
+	 * @param int    $new_ticket_id
775
+	 * @param int    $quantity_sold
776
+	 * @param string $STS_ID
777
+	 * @return boolean whether they were successfully updated or not
778
+	 */
779
+	protected function _add_regs_to_ticket_and_datetimes(int $new_ticket_id, int $quantity_sold, string $STS_ID): bool
780
+	{
781
+		if ($STS_ID != 'RAP') {
782
+			return true;
783
+		}
784
+		global $wpdb;
785
+		$success =
786
+			$wpdb->query(
787
+				$wpdb->prepare(
788
+					"UPDATE $this->_new_ticket_table SET TKT_sold=TKT_sold+%d WHERE TKT_ID=%d",
789
+					$quantity_sold,
790
+					$new_ticket_id
791
+				)
792
+			);
793
+		if ($success) {
794
+			// get the ticket's datetimes, and increment them too
795
+			$success_update_datetimes = $wpdb->query(
796
+				$wpdb->prepare(
797
+					"UPDATE $this->_new_ticket_table TKT
798 798
 				INNER JOIN $this->_new_ticket_datetime_table as DTK ON TKT.TKT_ID = DTK.TKT_ID
799 799
 				INNER JOIN $this->_new_datetime_table as DTT ON DTK.DTT_ID = DTT.DTT_ID
800 800
 				SET DTT.DTT_sold = DTT.DTT_sold + %d WHERE TKT.TKT_ID = %d",
801
-                    $quantity_sold,
802
-                    $new_ticket_id
803
-                )
804
-            );
805
-            if (! $success_update_datetimes) {
806
-                $this->add_error(
807
-                    sprintf(
808
-                        esc_html__(
809
-                            "Could not update datetimes related to ticket with ID %d's TKT_sold by %d because %s",
810
-                            "event_espresso"
811
-                        ),
812
-                        $new_ticket_id,
813
-                        $quantity_sold,
814
-                        $wpdb->last_error
815
-                    )
816
-                );
817
-            }
818
-        } else {
819
-            $this->add_error(
820
-                sprintf(
821
-                    esc_html__("Could not update ticket with ID %d's TKT_sold by %d because %s", "event_espresso"),
822
-                    $new_ticket_id,
823
-                    $quantity_sold,
824
-                    $wpdb->last_error
825
-                )
826
-            );
827
-        }
828
-        return true;
829
-    }
830
-
831
-
832
-    /**
833
-     * Makes a guess at which ticket is the one the attendee purchased.
834
-     * Obviously, the old attendee's event_id narrows it down quite a bit;
835
-     * then the old attendee's orig_price and event_time, and price_option can uniquely identify the ticket
836
-     * however, if we don't find an exact match, see if any of those conditions match;
837
-     * and lastly if none of that works, just use the first ticket for the event we find
838
-     *
839
-     * @param array $old_attendee
840
-     * @param       $new_event_id
841
-     * @return string|null
842
-     */
843
-    private function _try_to_find_new_ticket_id(array $old_attendee, $new_event_id): ?string
844
-    {
845
-        global $wpdb;
846
-        $tickets_table          = $this->_new_ticket_table;
847
-        $datetime_tickets_table = $this->_new_ticket_datetime_table;
848
-        $datetime_table         = $this->_new_datetime_table;
849
-
850
-        $old_att_price_option = $old_attendee['price_option'];
851
-        $old_att_price        = floatval($old_attendee['orig_price']);
852
-
853
-        $old_att_start_date = $old_attendee['start_date'];
854
-        $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
855
-        $old_att_datetime   =
856
-            $this->get_migration_script()->convert_date_string_to_utc(
857
-                $this,
858
-                $old_attendee,
859
-                "$old_att_start_date $old_att_start_time:00"
860
-            );
861
-        // add all conditions to an array from which we can SHIFT conditions off in order to widen our search
862
-        // the most important condition should be last, as it will be array_shift'ed off last
863
-        $conditions           = [
864
-            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
865
-            $wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
866
-            $wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
867
-            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
868
-        ];
869
-        $select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
801
+					$quantity_sold,
802
+					$new_ticket_id
803
+				)
804
+			);
805
+			if (! $success_update_datetimes) {
806
+				$this->add_error(
807
+					sprintf(
808
+						esc_html__(
809
+							"Could not update datetimes related to ticket with ID %d's TKT_sold by %d because %s",
810
+							"event_espresso"
811
+						),
812
+						$new_ticket_id,
813
+						$quantity_sold,
814
+						$wpdb->last_error
815
+					)
816
+				);
817
+			}
818
+		} else {
819
+			$this->add_error(
820
+				sprintf(
821
+					esc_html__("Could not update ticket with ID %d's TKT_sold by %d because %s", "event_espresso"),
822
+					$new_ticket_id,
823
+					$quantity_sold,
824
+					$wpdb->last_error
825
+				)
826
+			);
827
+		}
828
+		return true;
829
+	}
830
+
831
+
832
+	/**
833
+	 * Makes a guess at which ticket is the one the attendee purchased.
834
+	 * Obviously, the old attendee's event_id narrows it down quite a bit;
835
+	 * then the old attendee's orig_price and event_time, and price_option can uniquely identify the ticket
836
+	 * however, if we don't find an exact match, see if any of those conditions match;
837
+	 * and lastly if none of that works, just use the first ticket for the event we find
838
+	 *
839
+	 * @param array $old_attendee
840
+	 * @param       $new_event_id
841
+	 * @return string|null
842
+	 */
843
+	private function _try_to_find_new_ticket_id(array $old_attendee, $new_event_id): ?string
844
+	{
845
+		global $wpdb;
846
+		$tickets_table          = $this->_new_ticket_table;
847
+		$datetime_tickets_table = $this->_new_ticket_datetime_table;
848
+		$datetime_table         = $this->_new_datetime_table;
849
+
850
+		$old_att_price_option = $old_attendee['price_option'];
851
+		$old_att_price        = floatval($old_attendee['orig_price']);
852
+
853
+		$old_att_start_date = $old_attendee['start_date'];
854
+		$old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
855
+		$old_att_datetime   =
856
+			$this->get_migration_script()->convert_date_string_to_utc(
857
+				$this,
858
+				$old_attendee,
859
+				"$old_att_start_date $old_att_start_time:00"
860
+			);
861
+		// add all conditions to an array from which we can SHIFT conditions off in order to widen our search
862
+		// the most important condition should be last, as it will be array_shift'ed off last
863
+		$conditions           = [
864
+			$wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
865
+			$wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
866
+			$wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
867
+			$wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
868
+		];
869
+		$select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
870 870
 			$datetime_tickets_table ON $tickets_table.TKT_ID = $datetime_tickets_table.TKT_ID INNER JOIN
871 871
 			$datetime_table ON $datetime_tickets_table.DTT_ID = $datetime_table.DTT_ID";
872
-        // start running queries, widening search each time by removing a condition
873
-        do {
874
-            $full_query      = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
875
-            $ticket_id_found = $wpdb->get_var($full_query);
876
-            array_shift($conditions);
877
-        } while (! $ticket_id_found && $conditions);
878
-        return $ticket_id_found;
879
-    }
880
-
881
-
882
-    /**
883
-     * If we couldn't find a 4.1 ticket for a 3.1 attendee row, this function creates one;
884
-     * and it also tries to find a datetime that works, and inserts a price, and associates
885
-     * the new ticket to that datetime and price.
886
-     *
887
-     * @return int ticket id
888
-     */
889
-    private function _insert_new_ticket_because_none_found($old_attendee, $new_event_id): int
890
-    {
891
-        global $wpdb;
892
-        $old_att_price_option = $old_attendee['price_option'];
893
-        $old_att_price        = floatval($old_attendee['orig_price']);
894
-
895
-        $old_att_start_date     = $old_attendee['start_date'];
896
-        $old_att_start_time     = $this->get_migration_script()->convertTimeFromAMPM(
897
-            $old_attendee['event_time']
898
-        );
899
-        $old_att_start_datetime =
900
-            $this->get_migration_script()->convert_date_string_to_utc(
901
-                $this,
902
-                $old_attendee,
903
-                "$old_att_start_date $old_att_start_time:00"
904
-            );
905
-        $old_att_end_datetime   = null;
906
-
907
-
908
-        // insert new datetime unless we find one
909
-        $datetime_id =
910
-            $wpdb->get_var(
911
-                $wpdb->prepare(
912
-                    "SELECT DTT_ID FROM " .
913
-                    $this->_new_datetime_table .
914
-                    " WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1",
915
-                    $old_att_start_datetime,
916
-                    $new_event_id
917
-                ),
918
-                ARRAY_A
919
-            );
920
-        if (! $datetime_id) {
921
-            $old_att_end_date     = $old_attendee['start_date'];
922
-            $old_att_end_time     = $this->get_migration_script()->convertTimeFromAMPM(
923
-                $old_attendee['event_time']
924
-            );
925
-            $old_att_end_datetime = $this->get_migration_script()->convert_date_string_to_utc(
926
-                $this,
927
-                $old_attendee,
928
-                "$old_att_end_date $old_att_end_time:00"
929
-            );
930
-            $wpdb->insert(
931
-                $this->_new_datetime_table,
932
-                [
933
-                    'EVT_ID'        => $new_event_id,
934
-                    'DTT_EVT_start' => $old_att_start_datetime,
935
-                    'DTT_EVT_end'   => $old_att_end_datetime,
936
-                    'DTT_deleted'   => true,
937
-                ],
938
-                [
939
-                    '%d',// EVT_ID
940
-                    '%s',// DTT_EVT_start
941
-                    '%s',// DTT_EVT_end
942
-                    '%d',// DTT_deleted
943
-                ]
944
-            );
945
-            $datetime_id = $wpdb->insert_id;
946
-        }
947
-
948
-        // insert new ticket
949
-        $wpdb->insert(
950
-            $wpdb->prefix . 'esp_ticket',
951
-            [
952
-                'TKT_name'       => $old_att_price_option,
953
-                'TKT_qty'        => -1,
954
-                'TKT_price'      => $old_att_price,
955
-                'TKT_start_date' => $old_att_start_datetime,
956
-                // we really have no clue what the time should be, but at least it was available when they attended
957
-                'TKT_end_date'   => $old_att_end_datetime,
958
-
959
-            ],
960
-            [
961
-                '%s',// name
962
-                '%d',// qty
963
-                '%d',// price
964
-                '%s',// start_date
965
-                '%s',// end_date
966
-            ]
967
-        );
968
-        $ticket_id = $wpdb->insert_id;
969
-        // associate the ticket with the datetime we found earlier
970
-        $wpdb->insert(
971
-            $this->_new_datetime_ticket_table,
972
-            [
973
-                'DTT_ID' => $datetime_id,
974
-                'TKT_ID' => $ticket_id,
975
-            ],
976
-            [
977
-                '%d',// DTT_ID
978
-                '%d',// TKT_ID
979
-            ]
980
-        );
981
-        // insert new price
982
-        $wpdb->insert(
983
-            $this->_new_price_table,
984
-            [
985
-                'PRC_amount'  => $old_att_price,
986
-                'PRT_ID'      => EE_DMS_4_1_0_prices::price_type_base,
987
-                'PRC_name'    => $old_att_price_option,
988
-                'PRC_deleted' => true,
989
-            ],
990
-            [
991
-                '%d',// PRC_amount
992
-                '%d',// PRT_ID
993
-                '%s',// PRC_name
994
-                '%d',// PRC_deleted
995
-            ]
996
-        );
997
-        $price_id = $wpdb->insert_id;
998
-        // associate the price to the ticket
999
-        $wpdb->insert(
1000
-            $this->_new_ticket_price_table,
1001
-            [
1002
-                'TKT_ID' => $ticket_id,
1003
-                'PRC_ID' => $price_id,
1004
-            ],
1005
-            [
1006
-                '%d',// TKT_ID
1007
-                '%d',// PRC_ID
1008
-            ]
1009
-        );
1010
-        return $ticket_id;
1011
-    }
1012
-
1013
-
1014
-    /**
1015
-     * Counts all the registrations on this transaction. If $count_only_older is TRUE then returns the number added SO
1016
-     * FAR (ie, only considers attendee rows with an ID less than this one's), but if $count_only_older is FALSe
1017
-     * returns ALL
1018
-     *
1019
-     * @param array   $old_attendee_row
1020
-     * @param boolean $count_only_older true if you want the running count (ie, the total up to this row), and false if
1021
-     *                                  you want ALL
1022
-     * @return int
1023
-     * @global wpdb   $wpdb
1024
-     */
1025
-    private function _sum_old_attendees_on_old_txn(array $old_attendee_row, bool $count_only_older = false): int
1026
-    {
1027
-        global $wpdb;
1028
-        $count_only_older_sql = $count_only_older ? $wpdb->prepare(" AND id<%d", $old_attendee_row['id']) : '';
1029
-        $count                =
1030
-            intval(
1031
-                $wpdb->get_var(
1032
-                    $wpdb->prepare(
1033
-                        "SELECT SUM(quantity) FROM " .
1034
-                        $this->_old_table .
1035
-                        " WHERE registration_id=%s $count_only_older_sql",
1036
-                        $old_attendee_row['registration_id']
1037
-                    )
1038
-                )
1039
-            );
1040
-
1041
-        if ($this->_mer_tables_exist()) {
1042
-            // if MER exists, then it's a little tricky.
1043
-            // when users registered by adding items to the cart, and it was a
1044
-            // group registration requiring additional attendee INFO, then the attendee rows
1045
-            // DO NOT have the same registration_id (although they probably should have)
1046
-            // they are related just like MER attendee rows are related, through the MER group table
1047
-            // BUT we want to count all the MER attendee rows for the same registration
1048
-            $primary_attendee =
1049
-                $this->_find_mer_primary_attendee_using_mer_tables($old_attendee_row['registration_id']);
1050
-
1051
-            $count_using_mer_table =
1052
-                $wpdb->get_var(
1053
-                    $wpdb->prepare(
1054
-                        "SELECT SUM(quantity) FROM $this->_old_table att INNER JOIN $this->_old_mer_table mer ON att.registration_id = mer.registration_id WHERE att.event_id=%d AND mer.primary_registration_id = %s $count_only_older_sql",
1055
-                        $old_attendee_row['event_id'],
1056
-                        $primary_attendee['registration_id']
1057
-                    )
1058
-                );
1059
-            $count                 = max($count_using_mer_table, $count);
1060
-        }
1061
-        return $count;
1062
-    }
1063
-
1064
-
1065
-    private function _insert_new_payment($old_attendee, $new_txn_id): int
1066
-    {
1067
-        global $wpdb;
1068
-        // Only add a payment for primary attendees. Important note: partial payments were marked as "Pending".
1069
-        // Also, an offline payment had blank status (ie, "").
1070
-        $old_pay_stati_indicating_no_payment = ['', 'Incomplete', 'Not Completed'];
1071
-        // if this is for a primary 3.1 attendee which WASN'T free and has a completed, cancelled, or declined payment...
1072
-        if (
1073
-            intval($old_attendee['is_primary']) &&
1074
-            floatval($old_attendee['total_cost']) &&
1075
-            ! in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)
1076
-        ) {
1077
-            $pay_status_mapping = [
1078
-                'Completed'        => 'PAP',
1079
-                'Payment Declined' => 'PDC',
1080
-                'Cancelled'        => 'PCN',
1081
-                'Declined'         => 'PDC',
1082
-            ];
1083
-
1084
-            $by_admin = $old_attendee['payment'] == 'Admin';
1085
-
1086
-            // IE, if we don't recognize the status, assume payment failed
1087
-            $STS_ID = $pay_status_mapping[ $old_attendee['payment_status'] ] ?? 'PFL';
1088
-
1089
-            $cols_n_values = [
1090
-                'TXN_ID'               => $new_txn_id,
1091
-                'STS_ID'               => $STS_ID,
1092
-                'PAY_timestamp'        => $this->get_migration_script()->convert_date_string_to_utc(
1093
-                    $this,
1094
-                    $old_attendee,
1095
-                    $old_attendee['date']
1096
-                ),
1097
-                'PAY_method'           => 'CART',
1098
-                'PAY_amount'           => $old_attendee['amount_pd'],
1099
-                // 'txn_type' was used both for the gateway's name, and for other payment notes.
1100
-                'PAY_gateway'          => $old_attendee['txn_type'],
1101
-                'PAY_extra_accntng'    => $old_attendee['txn_type'],
1102
-                'PAY_gateway_response' => '',
1103
-                'PAY_txn_id_chq_nmbr'  => substr($old_attendee['txn_id'], 0, 32),
1104
-                'PAY_via_admin'        => $by_admin,
1105
-                'PAY_details'          => $old_attendee['transaction_details'],
1106
-            ];
1107
-            $data_types    = [
1108
-                '%d',// TXN_Id
1109
-                '%s',// STS_ID
1110
-                '%s',// PAY_timestamp
1111
-                '%s',// PAY_method
1112
-                '%f',// PAY_amount
1113
-                '%s',// PAY_gateway
1114
-                '%s',// PAY_gateway_response
1115
-                '%s',// PAY_txn_id_chq_nmbr
1116
-                '%d',// PAY_via_admin
1117
-                '%s',// PAY_details
1118
-            ];
1119
-            $success       = $wpdb->insert($this->_new_payment_table, $cols_n_values, $data_types);
1120
-            if (! $success) {
1121
-                $this->add_error(
1122
-                    $this->get_migration_script()->_create_error_message_for_db_insertion(
1123
-                        $this->_old_table,
1124
-                        $old_attendee,
1125
-                        $this->_new_attendee_cpt_table,
1126
-                        $cols_n_values,
1127
-                        $data_types
1128
-                    )
1129
-                );
1130
-                return 0;
1131
-            }
1132
-            return $wpdb->insert_id;
1133
-        }
1134
-        return 0;
1135
-    }
1136
-
1137
-
1138
-    /**
1139
-     * If MER is active, if you want ot find the other registrations on that attendee row
1140
-     *
1141
-     * @param int   $old_registration_id
1142
-     * @return array|null
1143
-     * @global wpdb $wpdb
1144
-     */
1145
-    private function _find_mer_primary_attendee_using_mer_tables(int $old_registration_id): ?array
1146
-    {
1147
-        if (! $this->_mer_tables_exist()) {
1148
-            return null;
1149
-        }
1150
-        global $wpdb;
1151
-        return $wpdb->get_row(
1152
-            $wpdb->prepare(
1153
-                "SELECT * FROM $this->_old_mer_table AS mer INNER JOIN $this->_old_table AS att ON mer.primary_registration_id = att.registration_id WHERE mer.registration_id=%s LIMIT 1",
1154
-                $old_registration_id
1155
-            ),
1156
-            ARRAY_A
1157
-        );
1158
-    }
872
+		// start running queries, widening search each time by removing a condition
873
+		do {
874
+			$full_query      = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
875
+			$ticket_id_found = $wpdb->get_var($full_query);
876
+			array_shift($conditions);
877
+		} while (! $ticket_id_found && $conditions);
878
+		return $ticket_id_found;
879
+	}
880
+
881
+
882
+	/**
883
+	 * If we couldn't find a 4.1 ticket for a 3.1 attendee row, this function creates one;
884
+	 * and it also tries to find a datetime that works, and inserts a price, and associates
885
+	 * the new ticket to that datetime and price.
886
+	 *
887
+	 * @return int ticket id
888
+	 */
889
+	private function _insert_new_ticket_because_none_found($old_attendee, $new_event_id): int
890
+	{
891
+		global $wpdb;
892
+		$old_att_price_option = $old_attendee['price_option'];
893
+		$old_att_price        = floatval($old_attendee['orig_price']);
894
+
895
+		$old_att_start_date     = $old_attendee['start_date'];
896
+		$old_att_start_time     = $this->get_migration_script()->convertTimeFromAMPM(
897
+			$old_attendee['event_time']
898
+		);
899
+		$old_att_start_datetime =
900
+			$this->get_migration_script()->convert_date_string_to_utc(
901
+				$this,
902
+				$old_attendee,
903
+				"$old_att_start_date $old_att_start_time:00"
904
+			);
905
+		$old_att_end_datetime   = null;
906
+
907
+
908
+		// insert new datetime unless we find one
909
+		$datetime_id =
910
+			$wpdb->get_var(
911
+				$wpdb->prepare(
912
+					"SELECT DTT_ID FROM " .
913
+					$this->_new_datetime_table .
914
+					" WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1",
915
+					$old_att_start_datetime,
916
+					$new_event_id
917
+				),
918
+				ARRAY_A
919
+			);
920
+		if (! $datetime_id) {
921
+			$old_att_end_date     = $old_attendee['start_date'];
922
+			$old_att_end_time     = $this->get_migration_script()->convertTimeFromAMPM(
923
+				$old_attendee['event_time']
924
+			);
925
+			$old_att_end_datetime = $this->get_migration_script()->convert_date_string_to_utc(
926
+				$this,
927
+				$old_attendee,
928
+				"$old_att_end_date $old_att_end_time:00"
929
+			);
930
+			$wpdb->insert(
931
+				$this->_new_datetime_table,
932
+				[
933
+					'EVT_ID'        => $new_event_id,
934
+					'DTT_EVT_start' => $old_att_start_datetime,
935
+					'DTT_EVT_end'   => $old_att_end_datetime,
936
+					'DTT_deleted'   => true,
937
+				],
938
+				[
939
+					'%d',// EVT_ID
940
+					'%s',// DTT_EVT_start
941
+					'%s',// DTT_EVT_end
942
+					'%d',// DTT_deleted
943
+				]
944
+			);
945
+			$datetime_id = $wpdb->insert_id;
946
+		}
947
+
948
+		// insert new ticket
949
+		$wpdb->insert(
950
+			$wpdb->prefix . 'esp_ticket',
951
+			[
952
+				'TKT_name'       => $old_att_price_option,
953
+				'TKT_qty'        => -1,
954
+				'TKT_price'      => $old_att_price,
955
+				'TKT_start_date' => $old_att_start_datetime,
956
+				// we really have no clue what the time should be, but at least it was available when they attended
957
+				'TKT_end_date'   => $old_att_end_datetime,
958
+
959
+			],
960
+			[
961
+				'%s',// name
962
+				'%d',// qty
963
+				'%d',// price
964
+				'%s',// start_date
965
+				'%s',// end_date
966
+			]
967
+		);
968
+		$ticket_id = $wpdb->insert_id;
969
+		// associate the ticket with the datetime we found earlier
970
+		$wpdb->insert(
971
+			$this->_new_datetime_ticket_table,
972
+			[
973
+				'DTT_ID' => $datetime_id,
974
+				'TKT_ID' => $ticket_id,
975
+			],
976
+			[
977
+				'%d',// DTT_ID
978
+				'%d',// TKT_ID
979
+			]
980
+		);
981
+		// insert new price
982
+		$wpdb->insert(
983
+			$this->_new_price_table,
984
+			[
985
+				'PRC_amount'  => $old_att_price,
986
+				'PRT_ID'      => EE_DMS_4_1_0_prices::price_type_base,
987
+				'PRC_name'    => $old_att_price_option,
988
+				'PRC_deleted' => true,
989
+			],
990
+			[
991
+				'%d',// PRC_amount
992
+				'%d',// PRT_ID
993
+				'%s',// PRC_name
994
+				'%d',// PRC_deleted
995
+			]
996
+		);
997
+		$price_id = $wpdb->insert_id;
998
+		// associate the price to the ticket
999
+		$wpdb->insert(
1000
+			$this->_new_ticket_price_table,
1001
+			[
1002
+				'TKT_ID' => $ticket_id,
1003
+				'PRC_ID' => $price_id,
1004
+			],
1005
+			[
1006
+				'%d',// TKT_ID
1007
+				'%d',// PRC_ID
1008
+			]
1009
+		);
1010
+		return $ticket_id;
1011
+	}
1012
+
1013
+
1014
+	/**
1015
+	 * Counts all the registrations on this transaction. If $count_only_older is TRUE then returns the number added SO
1016
+	 * FAR (ie, only considers attendee rows with an ID less than this one's), but if $count_only_older is FALSe
1017
+	 * returns ALL
1018
+	 *
1019
+	 * @param array   $old_attendee_row
1020
+	 * @param boolean $count_only_older true if you want the running count (ie, the total up to this row), and false if
1021
+	 *                                  you want ALL
1022
+	 * @return int
1023
+	 * @global wpdb   $wpdb
1024
+	 */
1025
+	private function _sum_old_attendees_on_old_txn(array $old_attendee_row, bool $count_only_older = false): int
1026
+	{
1027
+		global $wpdb;
1028
+		$count_only_older_sql = $count_only_older ? $wpdb->prepare(" AND id<%d", $old_attendee_row['id']) : '';
1029
+		$count                =
1030
+			intval(
1031
+				$wpdb->get_var(
1032
+					$wpdb->prepare(
1033
+						"SELECT SUM(quantity) FROM " .
1034
+						$this->_old_table .
1035
+						" WHERE registration_id=%s $count_only_older_sql",
1036
+						$old_attendee_row['registration_id']
1037
+					)
1038
+				)
1039
+			);
1040
+
1041
+		if ($this->_mer_tables_exist()) {
1042
+			// if MER exists, then it's a little tricky.
1043
+			// when users registered by adding items to the cart, and it was a
1044
+			// group registration requiring additional attendee INFO, then the attendee rows
1045
+			// DO NOT have the same registration_id (although they probably should have)
1046
+			// they are related just like MER attendee rows are related, through the MER group table
1047
+			// BUT we want to count all the MER attendee rows for the same registration
1048
+			$primary_attendee =
1049
+				$this->_find_mer_primary_attendee_using_mer_tables($old_attendee_row['registration_id']);
1050
+
1051
+			$count_using_mer_table =
1052
+				$wpdb->get_var(
1053
+					$wpdb->prepare(
1054
+						"SELECT SUM(quantity) FROM $this->_old_table att INNER JOIN $this->_old_mer_table mer ON att.registration_id = mer.registration_id WHERE att.event_id=%d AND mer.primary_registration_id = %s $count_only_older_sql",
1055
+						$old_attendee_row['event_id'],
1056
+						$primary_attendee['registration_id']
1057
+					)
1058
+				);
1059
+			$count                 = max($count_using_mer_table, $count);
1060
+		}
1061
+		return $count;
1062
+	}
1063
+
1064
+
1065
+	private function _insert_new_payment($old_attendee, $new_txn_id): int
1066
+	{
1067
+		global $wpdb;
1068
+		// Only add a payment for primary attendees. Important note: partial payments were marked as "Pending".
1069
+		// Also, an offline payment had blank status (ie, "").
1070
+		$old_pay_stati_indicating_no_payment = ['', 'Incomplete', 'Not Completed'];
1071
+		// if this is for a primary 3.1 attendee which WASN'T free and has a completed, cancelled, or declined payment...
1072
+		if (
1073
+			intval($old_attendee['is_primary']) &&
1074
+			floatval($old_attendee['total_cost']) &&
1075
+			! in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)
1076
+		) {
1077
+			$pay_status_mapping = [
1078
+				'Completed'        => 'PAP',
1079
+				'Payment Declined' => 'PDC',
1080
+				'Cancelled'        => 'PCN',
1081
+				'Declined'         => 'PDC',
1082
+			];
1083
+
1084
+			$by_admin = $old_attendee['payment'] == 'Admin';
1085
+
1086
+			// IE, if we don't recognize the status, assume payment failed
1087
+			$STS_ID = $pay_status_mapping[ $old_attendee['payment_status'] ] ?? 'PFL';
1088
+
1089
+			$cols_n_values = [
1090
+				'TXN_ID'               => $new_txn_id,
1091
+				'STS_ID'               => $STS_ID,
1092
+				'PAY_timestamp'        => $this->get_migration_script()->convert_date_string_to_utc(
1093
+					$this,
1094
+					$old_attendee,
1095
+					$old_attendee['date']
1096
+				),
1097
+				'PAY_method'           => 'CART',
1098
+				'PAY_amount'           => $old_attendee['amount_pd'],
1099
+				// 'txn_type' was used both for the gateway's name, and for other payment notes.
1100
+				'PAY_gateway'          => $old_attendee['txn_type'],
1101
+				'PAY_extra_accntng'    => $old_attendee['txn_type'],
1102
+				'PAY_gateway_response' => '',
1103
+				'PAY_txn_id_chq_nmbr'  => substr($old_attendee['txn_id'], 0, 32),
1104
+				'PAY_via_admin'        => $by_admin,
1105
+				'PAY_details'          => $old_attendee['transaction_details'],
1106
+			];
1107
+			$data_types    = [
1108
+				'%d',// TXN_Id
1109
+				'%s',// STS_ID
1110
+				'%s',// PAY_timestamp
1111
+				'%s',// PAY_method
1112
+				'%f',// PAY_amount
1113
+				'%s',// PAY_gateway
1114
+				'%s',// PAY_gateway_response
1115
+				'%s',// PAY_txn_id_chq_nmbr
1116
+				'%d',// PAY_via_admin
1117
+				'%s',// PAY_details
1118
+			];
1119
+			$success       = $wpdb->insert($this->_new_payment_table, $cols_n_values, $data_types);
1120
+			if (! $success) {
1121
+				$this->add_error(
1122
+					$this->get_migration_script()->_create_error_message_for_db_insertion(
1123
+						$this->_old_table,
1124
+						$old_attendee,
1125
+						$this->_new_attendee_cpt_table,
1126
+						$cols_n_values,
1127
+						$data_types
1128
+					)
1129
+				);
1130
+				return 0;
1131
+			}
1132
+			return $wpdb->insert_id;
1133
+		}
1134
+		return 0;
1135
+	}
1136
+
1137
+
1138
+	/**
1139
+	 * If MER is active, if you want ot find the other registrations on that attendee row
1140
+	 *
1141
+	 * @param int   $old_registration_id
1142
+	 * @return array|null
1143
+	 * @global wpdb $wpdb
1144
+	 */
1145
+	private function _find_mer_primary_attendee_using_mer_tables(int $old_registration_id): ?array
1146
+	{
1147
+		if (! $this->_mer_tables_exist()) {
1148
+			return null;
1149
+		}
1150
+		global $wpdb;
1151
+		return $wpdb->get_row(
1152
+			$wpdb->prepare(
1153
+				"SELECT * FROM $this->_old_mer_table AS mer INNER JOIN $this->_old_table AS att ON mer.primary_registration_id = att.registration_id WHERE mer.registration_id=%s LIMIT 1",
1154
+				$old_registration_id
1155
+			),
1156
+			ARRAY_A
1157
+		);
1158
+	}
1159 1159
 }
Please login to merge, or discard this patch.
Spacing   +141 added lines, -141 removed lines patch added patch discarded remove patch
@@ -259,25 +259,25 @@  discard block
 block discarded – undo
259 259
     {
260 260
         global $wpdb;
261 261
         $this->_pretty_name = esc_html__("Attendees", "event_espresso");
262
-        $this->_old_table   = $wpdb->prefix . "events_attendee";
262
+        $this->_old_table   = $wpdb->prefix."events_attendee";
263 263
         // Only select the event status column from the event table.
264 264
         $this->select_expression = 'att.*, e.event_status';
265 265
         // Only select attendees for events that aren't deleted.
266 266
         $this->_extra_where_sql           = 'AS att
267
-            INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id
267
+            INNER JOIN ' . $wpdb->prefix.'events_detail AS e ON att.event_id=e.id
268 268
             WHERE e.event_status!="D"';
269
-        $this->_old_mer_table             = $wpdb->prefix . "events_multi_event_registration_id_group";
269
+        $this->_old_mer_table             = $wpdb->prefix."events_multi_event_registration_id_group";
270 270
         $this->_new_attendee_cpt_table    = $wpdb->posts;
271
-        $this->_new_attendee_meta_table   = $wpdb->prefix . "esp_attendee_meta";
272
-        $this->_new_reg_table             = $wpdb->prefix . "esp_registration";
273
-        $this->_new_transaction_table     = $wpdb->prefix . "esp_transaction";
274
-        $this->_new_payment_table         = $wpdb->prefix . "esp_payment";
275
-        $this->_new_ticket_table          = $wpdb->prefix . "esp_ticket";
276
-        $this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
277
-        $this->_new_datetime_table        = $wpdb->prefix . "esp_datetime";
278
-        $this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
279
-        $this->_new_price_table           = $wpdb->prefix . "esp_price";
280
-        $this->_new_ticket_price_table    = $wpdb->prefix . "esp_ticket_price";
271
+        $this->_new_attendee_meta_table   = $wpdb->prefix."esp_attendee_meta";
272
+        $this->_new_reg_table             = $wpdb->prefix."esp_registration";
273
+        $this->_new_transaction_table     = $wpdb->prefix."esp_transaction";
274
+        $this->_new_payment_table         = $wpdb->prefix."esp_payment";
275
+        $this->_new_ticket_table          = $wpdb->prefix."esp_ticket";
276
+        $this->_new_ticket_datetime_table = $wpdb->prefix."esp_datetime_ticket";
277
+        $this->_new_datetime_table        = $wpdb->prefix."esp_datetime";
278
+        $this->_new_datetime_ticket_table = $wpdb->prefix."esp_datetime_ticket";
279
+        $this->_new_price_table           = $wpdb->prefix."esp_price";
280
+        $this->_new_ticket_price_table    = $wpdb->prefix."esp_ticket_price";
281 281
         parent::__construct();
282 282
     }
283 283
 
@@ -289,9 +289,9 @@  discard block
 block discarded – undo
289 289
     {
290 290
         // first check if there's already a new attendee with similar characteristics
291 291
         $new_att_id = $this->_find_attendee_cpt_matching($old_row);
292
-        if (! $new_att_id) {
292
+        if ( ! $new_att_id) {
293 293
             $new_att_id = $this->_insert_new_attendee_cpt($old_row);
294
-            if (! $new_att_id) {
294
+            if ( ! $new_att_id) {
295 295
                 // if we couldn't even make an attendee, abandon all hope
296 296
                 return;
297 297
             }
@@ -313,7 +313,7 @@  discard block
 block discarded – undo
313 313
         );
314 314
 
315 315
         $txn_id = $this->_insert_new_transaction($old_row);
316
-        if (! $txn_id) {
316
+        if ( ! $txn_id) {
317 317
             // if we couldn't make the transaction, also abandon all hope
318 318
             return;
319 319
         }
@@ -361,10 +361,10 @@  discard block
 block discarded – undo
361 361
         $existing_attendee_id =
362 362
             $wpdb->get_var(
363 363
                 $wpdb->prepare(
364
-                    "SELECT id FROM " .
365
-                    $this->_new_attendee_cpt_table .
366
-                    " AS cpt INNER JOIN " .
367
-                    $this->_new_attendee_meta_table .
364
+                    "SELECT id FROM ".
365
+                    $this->_new_attendee_cpt_table.
366
+                    " AS cpt INNER JOIN ".
367
+                    $this->_new_attendee_meta_table.
368 368
                     " AS meta ON cpt.ID = meta.ATT_ID WHERE meta.ATT_fname = %s AND meta.ATT_lname = %s AND meta.ATT_email = %s LIMIT 1",
369 369
                     $old_attendee['fname'],
370 370
                     $old_attendee['lname'],
@@ -379,39 +379,39 @@  discard block
 block discarded – undo
379 379
     {
380 380
         global $wpdb;
381 381
         $cols_n_values = [
382
-            'post_title'    => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
383
-            'post_content'  => '',// ATT_bio
384
-            'post_name'     => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
382
+            'post_title'    => stripslashes($old_attendee['fname']." ".$old_attendee['lname']), // ATT_full_name
383
+            'post_content'  => '', // ATT_bio
384
+            'post_name'     => sanitize_title($old_attendee['fname']."-".$old_attendee['lname']), // ATT_slug
385 385
             'post_date'     => $this->get_migration_script()->convert_date_string_to_utc(
386 386
                 $this,
387 387
                 $old_attendee,
388 388
                 $old_attendee['date']
389
-            ),// ATT_created
390
-            'post_excerpt'  => '',// ATT_short_bio
389
+            ), // ATT_created
390
+            'post_excerpt'  => '', // ATT_short_bio
391 391
             'post_modified' => $this->get_migration_script()->convert_date_string_to_utc(
392 392
                 $this,
393 393
                 $old_attendee,
394 394
                 $old_attendee['date']
395
-            ),// ATT_modified
396
-            'post_author'   => 0,// ATT_author
397
-            'post_parent'   => 0,// ATT_parent
398
-            'post_type'     => 'espresso_attendees',// post_type
399
-            'post_status'   => 'publish',// status
395
+            ), // ATT_modified
396
+            'post_author'   => 0, // ATT_author
397
+            'post_parent'   => 0, // ATT_parent
398
+            'post_type'     => 'espresso_attendees', // post_type
399
+            'post_status'   => 'publish', // status
400 400
         ];
401
-        $data_types    = [
402
-            '%s',// ATT_full_name
403
-            '%s',// ATT_bio
404
-            '%s',// ATT_slug
405
-            '%s',// ATT_created
406
-            '%s',// ATT_short_bio
407
-            '%s',// ATT_modified
408
-            '%d',// ATT_author
409
-            '%d',// ATT_parent
410
-            '%s',// post_type
411
-            '%s',// status
401
+        $data_types = [
402
+            '%s', // ATT_full_name
403
+            '%s', // ATT_bio
404
+            '%s', // ATT_slug
405
+            '%s', // ATT_created
406
+            '%s', // ATT_short_bio
407
+            '%s', // ATT_modified
408
+            '%d', // ATT_author
409
+            '%d', // ATT_parent
410
+            '%s', // post_type
411
+            '%s', // status
412 412
         ];
413
-        $success       = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $data_types);
414
-        if (! $success) {
413
+        $success = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $data_types);
414
+        if ( ! $success) {
415 415
             $this->add_error(
416 416
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
417 417
                     $this->_old_table,
@@ -439,7 +439,7 @@  discard block
 block discarded – undo
439 439
             $new_country_iso = $this->get_migration_script()->get_default_country_iso();
440 440
         }
441 441
         try {
442
-            $new_state    =
442
+            $new_state =
443 443
                 $this->get_migration_script()->get_or_create_state(
444 444
                     stripslashes($old_attendee['state']),
445 445
                     $new_country_iso
@@ -461,21 +461,21 @@  discard block
 block discarded – undo
461 461
             'ATT_email'    => stripslashes($old_attendee['email']),
462 462
             'ATT_phone'    => stripslashes($old_attendee['phone']),
463 463
         ];
464
-        $data_types    = [
465
-            '%d',// ATT_ID
466
-            '%s',// ATT_fname
467
-            '%s',// ATT_lname
468
-            '%s',// ATT_address
469
-            '%s',// ATT_address2
470
-            '%s',// ATT_city
471
-            '%d',// STA_ID
472
-            '%s',// CNT_ISO
473
-            '%s',// ATT_zip
474
-            '%s',// ATT_email
475
-            '%s',// ATT_phone
464
+        $data_types = [
465
+            '%d', // ATT_ID
466
+            '%s', // ATT_fname
467
+            '%s', // ATT_lname
468
+            '%s', // ATT_address
469
+            '%s', // ATT_address2
470
+            '%s', // ATT_city
471
+            '%d', // STA_ID
472
+            '%s', // CNT_ISO
473
+            '%s', // ATT_zip
474
+            '%s', // ATT_email
475
+            '%s', // ATT_phone
476 476
         ];
477
-        $success       = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $data_types);
478
-        if (! $success) {
477
+        $success = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $data_types);
478
+        if ( ! $success) {
479 479
             $this->add_error(
480 480
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
481 481
                     $this->_old_table,
@@ -517,11 +517,11 @@  discard block
 block discarded – undo
517 517
             $primary_attendee_old_id =
518 518
                 $wpdb->get_var(
519 519
                     $wpdb->prepare(
520
-                        "SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s",
520
+                        "SELECT id FROM ".$this->_old_table." WHERE is_primary=1 and registration_id=%s",
521 521
                         $old_attendee['registration_id']
522 522
                     )
523 523
                 );
524
-            if (! $primary_attendee_old_id) {
524
+            if ( ! $primary_attendee_old_id) {
525 525
                 $primary_attendee        =
526 526
                     $this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
527 527
                 $primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
@@ -532,7 +532,7 @@  discard block
 block discarded – undo
532 532
                     intval($primary_attendee_old_id),
533 533
                     $this->_new_transaction_table
534 534
                 );
535
-            if (! $txn_id) {
535
+            if ( ! $txn_id) {
536 536
                 $this->add_error(
537 537
                     sprintf(
538 538
                         esc_html__(
@@ -549,7 +549,7 @@  discard block
 block discarded – undo
549 549
         // if there isn't yet a transaction row for this, create one
550 550
         // (so even if it was a non-primary attendee with no EE3 primary attendee,
551 551
         // it ought to have SOME transaction, so we'll make one)
552
-        if (! $txn_id) {
552
+        if ( ! $txn_id) {
553 553
             // maps 3.1 payment stati onto 4.1 transaction stati
554 554
             $txn_status_mapping = [
555 555
                 'Completed'        => 'TCM',
@@ -560,7 +560,7 @@  discard block
 block discarded – undo
560 560
                 'Cancelled'        => 'TIN',
561 561
                 'Declined'         => 'TIN',
562 562
             ];
563
-            $STS_ID             = $txn_status_mapping[ $old_attendee['payment_status'] ] ?? 'TIN';
563
+            $STS_ID             = $txn_status_mapping[$old_attendee['payment_status']] ?? 'TIN';
564 564
             $cols_n_values      = [
565 565
                 'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc(
566 566
                     $this,
@@ -572,15 +572,15 @@  discard block
 block discarded – undo
572 572
                 'STS_ID'        => $STS_ID,
573 573
                 'TXN_hash_salt' => $old_attendee['hashSalt'],
574 574
             ];
575
-            $data_types         = [
576
-                '%s',// TXN_timestamp
577
-                '%f',// TXN_total
578
-                '%f',// TXN_paid
579
-                '%s',// STS_ID
580
-                '%s',// TXN_hash_salt
575
+            $data_types = [
576
+                '%s', // TXN_timestamp
577
+                '%f', // TXN_total
578
+                '%f', // TXN_paid
579
+                '%s', // STS_ID
580
+                '%s', // TXN_hash_salt
581 581
             ];
582
-            $success            = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $data_types);
583
-            if (! $success) {
582
+            $success = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $data_types);
583
+            if ( ! $success) {
584 584
                 $this->add_error(
585 585
                     $this->get_migration_script()->_create_error_message_for_db_insertion(
586 586
                         $this->_old_table,
@@ -635,7 +635,7 @@  discard block
 block discarded – undo
635 635
         $event_required_pre_approval =
636 636
             $wpdb->get_var(
637 637
                 $wpdb->prepare(
638
-                    "SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d",
638
+                    "SELECT require_pre_approval FROM ".$wpdb->prefix."events_detail WHERE id = %d",
639 639
                     $old_attendee_row['event_id']
640 640
                 )
641 641
             );
@@ -664,11 +664,11 @@  discard block
 block discarded – undo
664 664
         $STS_ID       = $this->_get_reg_status_for_old_payment_status($old_attendee);
665 665
         $new_event_id =
666 666
             $this->get_migration_script()->get_mapping_new_pk(
667
-                $wpdb->prefix . 'events_detail',
667
+                $wpdb->prefix.'events_detail',
668 668
                 $old_attendee['event_id'],
669 669
                 $wpdb->posts
670 670
             );
671
-        if (! $new_event_id) {
671
+        if ( ! $new_event_id) {
672 672
             $this->add_error(
673 673
                 sprintf(
674 674
                     esc_html__(
@@ -682,7 +682,7 @@  discard block
 block discarded – undo
682 682
         }
683 683
 
684 684
         $ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
685
-        if (! $ticket_id) {
685
+        if ( ! $ticket_id) {
686 686
             $ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
687 687
             $this->add_error(
688 688
                 sprintf(
@@ -720,30 +720,30 @@  discard block
 block discarded – undo
720 720
                 'REG_final_price'  => $old_attendee['final_price'],
721 721
                 'REG_session'      => substr($old_attendee['attendee_session'], 0, 44),
722 722
                 'REG_code'         => sanitize_key($old_attendee['registration_id']),
723
-                'REG_url_link'     => sanitize_key($old_attendee['registration_id'] . '-' . $count),
723
+                'REG_url_link'     => sanitize_key($old_attendee['registration_id'].'-'.$count),
724 724
                 'REG_count'        => $regs_on_this_event_and_txn + $count,
725 725
                 'REG_group_size'   => $this->_sum_old_attendees_on_old_txn($old_attendee),
726 726
                 'REG_att_is_going' => true,
727 727
                 'REG_deleted'      => false,
728 728
             ];
729
-            $data_types                 = [
730
-                '%d',// EVT_ID
731
-                '%d',// ATT_ID
732
-                '%d',// TXN_ID
733
-                '%d',// TKT_ID
734
-                '%s',// STS_ID
735
-                '%s',// REG_date
736
-                '%f',// REG_final_price
737
-                '%s',// REG_session
738
-                '%s',// REG_code
739
-                '%s',// REG_url_link
740
-                '%d',// REG_count
741
-                '%d',// REG_group_size
742
-                '%d',// REG_att_is_going
743
-                '%d',// REG_deleted
729
+            $data_types = [
730
+                '%d', // EVT_ID
731
+                '%d', // ATT_ID
732
+                '%d', // TXN_ID
733
+                '%d', // TKT_ID
734
+                '%s', // STS_ID
735
+                '%s', // REG_date
736
+                '%f', // REG_final_price
737
+                '%s', // REG_session
738
+                '%s', // REG_code
739
+                '%s', // REG_url_link
740
+                '%d', // REG_count
741
+                '%d', // REG_group_size
742
+                '%d', // REG_att_is_going
743
+                '%d', // REG_deleted
744 744
             ];
745
-            $success                    = $wpdb->insert($this->_new_reg_table, $cols_n_values, $data_types);
746
-            if (! $success) {
745
+            $success = $wpdb->insert($this->_new_reg_table, $cols_n_values, $data_types);
746
+            if ( ! $success) {
747 747
                 $this->add_error(
748 748
                     $this->get_migration_script()->_create_error_message_for_db_insertion(
749 749
                         $this->_old_table,
@@ -802,7 +802,7 @@  discard block
 block discarded – undo
802 802
                     $new_ticket_id
803 803
                 )
804 804
             );
805
-            if (! $success_update_datetimes) {
805
+            if ( ! $success_update_datetimes) {
806 806
                 $this->add_error(
807 807
                     sprintf(
808 808
                         esc_html__(
@@ -860,21 +860,21 @@  discard block
 block discarded – undo
860 860
             );
861 861
         // add all conditions to an array from which we can SHIFT conditions off in order to widen our search
862 862
         // the most important condition should be last, as it will be array_shift'ed off last
863
-        $conditions           = [
864
-            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
865
-            $wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
866
-            $wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
867
-            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
863
+        $conditions = [
864
+            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime), // times match?
865
+            $wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price), // prices match?
866
+            $wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option), // names match?
867
+            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id), // events match?
868 868
         ];
869 869
         $select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
870 870
 			$datetime_tickets_table ON $tickets_table.TKT_ID = $datetime_tickets_table.TKT_ID INNER JOIN
871 871
 			$datetime_table ON $datetime_tickets_table.DTT_ID = $datetime_table.DTT_ID";
872 872
         // start running queries, widening search each time by removing a condition
873 873
         do {
874
-            $full_query      = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
874
+            $full_query      = $select_and_join_part." WHERE ".implode(" AND ", $conditions)." LIMIT 1";
875 875
             $ticket_id_found = $wpdb->get_var($full_query);
876 876
             array_shift($conditions);
877
-        } while (! $ticket_id_found && $conditions);
877
+        }while ( ! $ticket_id_found && $conditions);
878 878
         return $ticket_id_found;
879 879
     }
880 880
 
@@ -902,22 +902,22 @@  discard block
 block discarded – undo
902 902
                 $old_attendee,
903 903
                 "$old_att_start_date $old_att_start_time:00"
904 904
             );
905
-        $old_att_end_datetime   = null;
905
+        $old_att_end_datetime = null;
906 906
 
907 907
 
908 908
         // insert new datetime unless we find one
909 909
         $datetime_id =
910 910
             $wpdb->get_var(
911 911
                 $wpdb->prepare(
912
-                    "SELECT DTT_ID FROM " .
913
-                    $this->_new_datetime_table .
912
+                    "SELECT DTT_ID FROM ".
913
+                    $this->_new_datetime_table.
914 914
                     " WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1",
915 915
                     $old_att_start_datetime,
916 916
                     $new_event_id
917 917
                 ),
918 918
                 ARRAY_A
919 919
             );
920
-        if (! $datetime_id) {
920
+        if ( ! $datetime_id) {
921 921
             $old_att_end_date     = $old_attendee['start_date'];
922 922
             $old_att_end_time     = $this->get_migration_script()->convertTimeFromAMPM(
923 923
                 $old_attendee['event_time']
@@ -936,10 +936,10 @@  discard block
 block discarded – undo
936 936
                     'DTT_deleted'   => true,
937 937
                 ],
938 938
                 [
939
-                    '%d',// EVT_ID
940
-                    '%s',// DTT_EVT_start
941
-                    '%s',// DTT_EVT_end
942
-                    '%d',// DTT_deleted
939
+                    '%d', // EVT_ID
940
+                    '%s', // DTT_EVT_start
941
+                    '%s', // DTT_EVT_end
942
+                    '%d', // DTT_deleted
943 943
                 ]
944 944
             );
945 945
             $datetime_id = $wpdb->insert_id;
@@ -947,7 +947,7 @@  discard block
 block discarded – undo
947 947
 
948 948
         // insert new ticket
949 949
         $wpdb->insert(
950
-            $wpdb->prefix . 'esp_ticket',
950
+            $wpdb->prefix.'esp_ticket',
951 951
             [
952 952
                 'TKT_name'       => $old_att_price_option,
953 953
                 'TKT_qty'        => -1,
@@ -958,11 +958,11 @@  discard block
 block discarded – undo
958 958
 
959 959
             ],
960 960
             [
961
-                '%s',// name
962
-                '%d',// qty
963
-                '%d',// price
964
-                '%s',// start_date
965
-                '%s',// end_date
961
+                '%s', // name
962
+                '%d', // qty
963
+                '%d', // price
964
+                '%s', // start_date
965
+                '%s', // end_date
966 966
             ]
967 967
         );
968 968
         $ticket_id = $wpdb->insert_id;
@@ -974,8 +974,8 @@  discard block
 block discarded – undo
974 974
                 'TKT_ID' => $ticket_id,
975 975
             ],
976 976
             [
977
-                '%d',// DTT_ID
978
-                '%d',// TKT_ID
977
+                '%d', // DTT_ID
978
+                '%d', // TKT_ID
979 979
             ]
980 980
         );
981 981
         // insert new price
@@ -988,10 +988,10 @@  discard block
 block discarded – undo
988 988
                 'PRC_deleted' => true,
989 989
             ],
990 990
             [
991
-                '%d',// PRC_amount
992
-                '%d',// PRT_ID
993
-                '%s',// PRC_name
994
-                '%d',// PRC_deleted
991
+                '%d', // PRC_amount
992
+                '%d', // PRT_ID
993
+                '%s', // PRC_name
994
+                '%d', // PRC_deleted
995 995
             ]
996 996
         );
997 997
         $price_id = $wpdb->insert_id;
@@ -1003,8 +1003,8 @@  discard block
 block discarded – undo
1003 1003
                 'PRC_ID' => $price_id,
1004 1004
             ],
1005 1005
             [
1006
-                '%d',// TKT_ID
1007
-                '%d',// PRC_ID
1006
+                '%d', // TKT_ID
1007
+                '%d', // PRC_ID
1008 1008
             ]
1009 1009
         );
1010 1010
         return $ticket_id;
@@ -1030,8 +1030,8 @@  discard block
 block discarded – undo
1030 1030
             intval(
1031 1031
                 $wpdb->get_var(
1032 1032
                     $wpdb->prepare(
1033
-                        "SELECT SUM(quantity) FROM " .
1034
-                        $this->_old_table .
1033
+                        "SELECT SUM(quantity) FROM ".
1034
+                        $this->_old_table.
1035 1035
                         " WHERE registration_id=%s $count_only_older_sql",
1036 1036
                         $old_attendee_row['registration_id']
1037 1037
                     )
@@ -1056,7 +1056,7 @@  discard block
 block discarded – undo
1056 1056
                         $primary_attendee['registration_id']
1057 1057
                     )
1058 1058
                 );
1059
-            $count                 = max($count_using_mer_table, $count);
1059
+            $count = max($count_using_mer_table, $count);
1060 1060
         }
1061 1061
         return $count;
1062 1062
     }
@@ -1084,7 +1084,7 @@  discard block
 block discarded – undo
1084 1084
             $by_admin = $old_attendee['payment'] == 'Admin';
1085 1085
 
1086 1086
             // IE, if we don't recognize the status, assume payment failed
1087
-            $STS_ID = $pay_status_mapping[ $old_attendee['payment_status'] ] ?? 'PFL';
1087
+            $STS_ID = $pay_status_mapping[$old_attendee['payment_status']] ?? 'PFL';
1088 1088
 
1089 1089
             $cols_n_values = [
1090 1090
                 'TXN_ID'               => $new_txn_id,
@@ -1104,20 +1104,20 @@  discard block
 block discarded – undo
1104 1104
                 'PAY_via_admin'        => $by_admin,
1105 1105
                 'PAY_details'          => $old_attendee['transaction_details'],
1106 1106
             ];
1107
-            $data_types    = [
1108
-                '%d',// TXN_Id
1109
-                '%s',// STS_ID
1110
-                '%s',// PAY_timestamp
1111
-                '%s',// PAY_method
1112
-                '%f',// PAY_amount
1113
-                '%s',// PAY_gateway
1114
-                '%s',// PAY_gateway_response
1115
-                '%s',// PAY_txn_id_chq_nmbr
1116
-                '%d',// PAY_via_admin
1117
-                '%s',// PAY_details
1107
+            $data_types = [
1108
+                '%d', // TXN_Id
1109
+                '%s', // STS_ID
1110
+                '%s', // PAY_timestamp
1111
+                '%s', // PAY_method
1112
+                '%f', // PAY_amount
1113
+                '%s', // PAY_gateway
1114
+                '%s', // PAY_gateway_response
1115
+                '%s', // PAY_txn_id_chq_nmbr
1116
+                '%d', // PAY_via_admin
1117
+                '%s', // PAY_details
1118 1118
             ];
1119
-            $success       = $wpdb->insert($this->_new_payment_table, $cols_n_values, $data_types);
1120
-            if (! $success) {
1119
+            $success = $wpdb->insert($this->_new_payment_table, $cols_n_values, $data_types);
1120
+            if ( ! $success) {
1121 1121
                 $this->add_error(
1122 1122
                     $this->get_migration_script()->_create_error_message_for_db_insertion(
1123 1123
                         $this->_old_table,
@@ -1144,7 +1144,7 @@  discard block
 block discarded – undo
1144 1144
      */
1145 1145
     private function _find_mer_primary_attendee_using_mer_tables(int $old_registration_id): ?array
1146 1146
     {
1147
-        if (! $this->_mer_tables_exist()) {
1147
+        if ( ! $this->_mer_tables_exist()) {
1148 1148
             return null;
1149 1149
         }
1150 1150
         global $wpdb;
Please login to merge, or discard this patch.
data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_event_venue.dmsstage.php 2 patches
Indentation   +114 added lines, -114 removed lines patch added patch discarded remove patch
@@ -27,128 +27,128 @@
 block discarded – undo
27 27
 class EE_DMS_4_1_0_event_venue extends EE_Data_Migration_Script_Stage
28 28
 {
29 29
 
30
-    /**
31
-     * @throws EE_Error
32
-     */
33
-    public function _migration_step($num_items_to_migrate = 50)
34
-    {
35
-        global $wpdb;
36
-        $start_at_record         = $this->count_records_migrated();
37
-        $rows                    = $wpdb->get_results(
38
-            $wpdb->prepare(
39
-                "SELECT * FROM $this->_old_table AS ev
30
+	/**
31
+	 * @throws EE_Error
32
+	 */
33
+	public function _migration_step($num_items_to_migrate = 50)
34
+	{
35
+		global $wpdb;
36
+		$start_at_record         = $this->count_records_migrated();
37
+		$rows                    = $wpdb->get_results(
38
+			$wpdb->prepare(
39
+				"SELECT * FROM $this->_old_table AS ev
40 40
                     INNER JOIN " . $wpdb->prefix . "events_detail AS e ON ev.event_id=e.id
41 41
                     WHERE e.event_status!='D' LIMIT %d,%d",
42
-                $start_at_record,
43
-                $num_items_to_migrate
44
-            ),
45
-            ARRAY_A
46
-        );
47
-        $items_actually_migrated = 0;
48
-        foreach ($rows as $event_venue_rel) {
49
-            $this->_insert_new_event_to_venue_rel($event_venue_rel);
50
-            $items_actually_migrated++;
51
-        }
52
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
53
-            $this->set_completed();
54
-        }
55
-        return $items_actually_migrated;
56
-    }
42
+				$start_at_record,
43
+				$num_items_to_migrate
44
+			),
45
+			ARRAY_A
46
+		);
47
+		$items_actually_migrated = 0;
48
+		foreach ($rows as $event_venue_rel) {
49
+			$this->_insert_new_event_to_venue_rel($event_venue_rel);
50
+			$items_actually_migrated++;
51
+		}
52
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
53
+			$this->set_completed();
54
+		}
55
+		return $items_actually_migrated;
56
+	}
57 57
 
58 58
 
59
-    public function _count_records_to_migrate()
60
-    {
61
-        global $wpdb;
62
-        return $wpdb->get_var(
63
-            "SELECT COUNT(ev.id) FROM " . $this->_old_table . " AS ev
59
+	public function _count_records_to_migrate()
60
+	{
61
+		global $wpdb;
62
+		return $wpdb->get_var(
63
+			"SELECT COUNT(ev.id) FROM " . $this->_old_table . " AS ev
64 64
             INNER JOIN " . $wpdb->prefix . 'events_detail AS e ON ev.event_id=e.id
65 65
             WHERE e.event_status!="D"'
66
-        );
67
-    }
66
+		);
67
+	}
68 68
 
69 69
 
70
-    public function __construct()
71
-    {
72
-        global $wpdb;
73
-        $this->_old_table   = $wpdb->prefix . "events_venue_rel";
74
-        $this->_new_table   = $wpdb->prefix . "esp_event_venue";
75
-        $this->_pretty_name = esc_html__("Event to Venue Relations", "event_espresso");
76
-        parent::__construct();
77
-    }
70
+	public function __construct()
71
+	{
72
+		global $wpdb;
73
+		$this->_old_table   = $wpdb->prefix . "events_venue_rel";
74
+		$this->_new_table   = $wpdb->prefix . "esp_event_venue";
75
+		$this->_pretty_name = esc_html__("Event to Venue Relations", "event_espresso");
76
+		parent::__construct();
77
+	}
78 78
 
79 79
 
80
-    /**
81
-     * Attempts to insert a new question group in the new format given an old one
82
-     *
83
-     * @param array $old_event_venue_rel
84
-     * @return void
85
-     * @throws EE_Error
86
-     * @global wpdb $wpdb
87
-     */
88
-    private function _insert_new_event_to_venue_rel(array $old_event_venue_rel): void
89
-    {
90
-        global $wpdb;
91
-        $new_event_id = $this->get_migration_script()->get_mapping_new_pk(
92
-            $wpdb->prefix . "events_detail",
93
-            $old_event_venue_rel['event_id'],
94
-            $wpdb->prefix . "posts"
95
-        );
96
-        $new_venue_id = $this->get_migration_script()->get_mapping_new_pk(
97
-            $wpdb->prefix . "events_venue",
98
-            $old_event_venue_rel['venue_id'],
99
-            $wpdb->prefix . "posts"
100
-        );
101
-        if (! $new_event_id) {
102
-            $this->add_error(
103
-                sprintf(
104
-                    esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"),
105
-                    $old_event_venue_rel['event_id']
106
-                )
107
-            );
108
-            return;
109
-        }
110
-        if (! $new_venue_id) {
111
-            $this->add_error(
112
-                sprintf(
113
-                    esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"),
114
-                    $old_event_venue_rel['venue_id']
115
-                )
116
-            );
117
-            return;
118
-        }
119
-        // first ensure there are no other relation entries for this event
120
-        // because although EE4 supports it, EE3 didn't really
121
-        $wpdb->delete(
122
-            $this->_new_table,
123
-            [
124
-                'EVT_ID' => $new_event_id,
125
-            ],
126
-            [
127
-                '%d',// EVT_ID
128
-            ]
129
-        );
130
-        //      echo "last query". $wpdb->last_query;die;
131
-        $cols_n_values = [
132
-            'EVT_ID'      => $new_event_id,
133
-            'VNU_ID'      => $new_venue_id,
134
-            'EVV_primary' => true,
135
-        ];
136
-        $data_types    = [
137
-            '%d',// EVT_ID
138
-            '%d',// VNU_ID
139
-            '%d',// EVT_primary
140
-        ];
141
-        $success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
142
-        if (! $success) {
143
-            $this->add_error(
144
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
145
-                    $this->_old_table,
146
-                    $old_event_venue_rel,
147
-                    $this->_new_table,
148
-                    $cols_n_values,
149
-                    $data_types
150
-                )
151
-            );
152
-        }
153
-    }
80
+	/**
81
+	 * Attempts to insert a new question group in the new format given an old one
82
+	 *
83
+	 * @param array $old_event_venue_rel
84
+	 * @return void
85
+	 * @throws EE_Error
86
+	 * @global wpdb $wpdb
87
+	 */
88
+	private function _insert_new_event_to_venue_rel(array $old_event_venue_rel): void
89
+	{
90
+		global $wpdb;
91
+		$new_event_id = $this->get_migration_script()->get_mapping_new_pk(
92
+			$wpdb->prefix . "events_detail",
93
+			$old_event_venue_rel['event_id'],
94
+			$wpdb->prefix . "posts"
95
+		);
96
+		$new_venue_id = $this->get_migration_script()->get_mapping_new_pk(
97
+			$wpdb->prefix . "events_venue",
98
+			$old_event_venue_rel['venue_id'],
99
+			$wpdb->prefix . "posts"
100
+		);
101
+		if (! $new_event_id) {
102
+			$this->add_error(
103
+				sprintf(
104
+					esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"),
105
+					$old_event_venue_rel['event_id']
106
+				)
107
+			);
108
+			return;
109
+		}
110
+		if (! $new_venue_id) {
111
+			$this->add_error(
112
+				sprintf(
113
+					esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"),
114
+					$old_event_venue_rel['venue_id']
115
+				)
116
+			);
117
+			return;
118
+		}
119
+		// first ensure there are no other relation entries for this event
120
+		// because although EE4 supports it, EE3 didn't really
121
+		$wpdb->delete(
122
+			$this->_new_table,
123
+			[
124
+				'EVT_ID' => $new_event_id,
125
+			],
126
+			[
127
+				'%d',// EVT_ID
128
+			]
129
+		);
130
+		//      echo "last query". $wpdb->last_query;die;
131
+		$cols_n_values = [
132
+			'EVT_ID'      => $new_event_id,
133
+			'VNU_ID'      => $new_venue_id,
134
+			'EVV_primary' => true,
135
+		];
136
+		$data_types    = [
137
+			'%d',// EVT_ID
138
+			'%d',// VNU_ID
139
+			'%d',// EVT_primary
140
+		];
141
+		$success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
142
+		if (! $success) {
143
+			$this->add_error(
144
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
145
+					$this->_old_table,
146
+					$old_event_venue_rel,
147
+					$this->_new_table,
148
+					$cols_n_values,
149
+					$data_types
150
+				)
151
+			);
152
+		}
153
+	}
154 154
 }
Please login to merge, or discard this patch.
Spacing   +18 added lines, -18 removed lines patch added patch discarded remove patch
@@ -37,7 +37,7 @@  discard block
 block discarded – undo
37 37
         $rows                    = $wpdb->get_results(
38 38
             $wpdb->prepare(
39 39
                 "SELECT * FROM $this->_old_table AS ev
40
-                    INNER JOIN " . $wpdb->prefix . "events_detail AS e ON ev.event_id=e.id
40
+                    INNER JOIN ".$wpdb->prefix."events_detail AS e ON ev.event_id=e.id
41 41
                     WHERE e.event_status!='D' LIMIT %d,%d",
42 42
                 $start_at_record,
43 43
                 $num_items_to_migrate
@@ -60,8 +60,8 @@  discard block
 block discarded – undo
60 60
     {
61 61
         global $wpdb;
62 62
         return $wpdb->get_var(
63
-            "SELECT COUNT(ev.id) FROM " . $this->_old_table . " AS ev
64
-            INNER JOIN " . $wpdb->prefix . 'events_detail AS e ON ev.event_id=e.id
63
+            "SELECT COUNT(ev.id) FROM ".$this->_old_table." AS ev
64
+            INNER JOIN " . $wpdb->prefix.'events_detail AS e ON ev.event_id=e.id
65 65
             WHERE e.event_status!="D"'
66 66
         );
67 67
     }
@@ -70,8 +70,8 @@  discard block
 block discarded – undo
70 70
     public function __construct()
71 71
     {
72 72
         global $wpdb;
73
-        $this->_old_table   = $wpdb->prefix . "events_venue_rel";
74
-        $this->_new_table   = $wpdb->prefix . "esp_event_venue";
73
+        $this->_old_table   = $wpdb->prefix."events_venue_rel";
74
+        $this->_new_table   = $wpdb->prefix."esp_event_venue";
75 75
         $this->_pretty_name = esc_html__("Event to Venue Relations", "event_espresso");
76 76
         parent::__construct();
77 77
     }
@@ -89,16 +89,16 @@  discard block
 block discarded – undo
89 89
     {
90 90
         global $wpdb;
91 91
         $new_event_id = $this->get_migration_script()->get_mapping_new_pk(
92
-            $wpdb->prefix . "events_detail",
92
+            $wpdb->prefix."events_detail",
93 93
             $old_event_venue_rel['event_id'],
94
-            $wpdb->prefix . "posts"
94
+            $wpdb->prefix."posts"
95 95
         );
96 96
         $new_venue_id = $this->get_migration_script()->get_mapping_new_pk(
97
-            $wpdb->prefix . "events_venue",
97
+            $wpdb->prefix."events_venue",
98 98
             $old_event_venue_rel['venue_id'],
99
-            $wpdb->prefix . "posts"
99
+            $wpdb->prefix."posts"
100 100
         );
101
-        if (! $new_event_id) {
101
+        if ( ! $new_event_id) {
102 102
             $this->add_error(
103 103
                 sprintf(
104 104
                     esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"),
@@ -107,7 +107,7 @@  discard block
 block discarded – undo
107 107
             );
108 108
             return;
109 109
         }
110
-        if (! $new_venue_id) {
110
+        if ( ! $new_venue_id) {
111 111
             $this->add_error(
112 112
                 sprintf(
113 113
                     esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"),
@@ -124,7 +124,7 @@  discard block
 block discarded – undo
124 124
                 'EVT_ID' => $new_event_id,
125 125
             ],
126 126
             [
127
-                '%d',// EVT_ID
127
+                '%d', // EVT_ID
128 128
             ]
129 129
         );
130 130
         //      echo "last query". $wpdb->last_query;die;
@@ -133,13 +133,13 @@  discard block
 block discarded – undo
133 133
             'VNU_ID'      => $new_venue_id,
134 134
             'EVV_primary' => true,
135 135
         ];
136
-        $data_types    = [
137
-            '%d',// EVT_ID
138
-            '%d',// VNU_ID
139
-            '%d',// EVT_primary
136
+        $data_types = [
137
+            '%d', // EVT_ID
138
+            '%d', // VNU_ID
139
+            '%d', // EVT_primary
140 140
         ];
141
-        $success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
142
-        if (! $success) {
141
+        $success = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
142
+        if ( ! $success) {
143 143
             $this->add_error(
144 144
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
145 145
                     $this->_old_table,
Please login to merge, or discard this patch.
core/data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_events.dmsstage.php 2 patches
Indentation   +814 added lines, -814 removed lines patch added patch discarded remove patch
@@ -175,822 +175,822 @@
 block discarded – undo
175 175
 class EE_DMS_4_1_0_events extends EE_Data_Migration_Script_Stage
176 176
 {
177 177
 
178
-    private string $_old_start_end_table;
179
-
180
-    private string $_new_meta_table;
181
-
182
-    private string $_new_datetime_table;
183
-
184
-
185
-    /**
186
-     * Just initializes the status of the migration
187
-     */
188
-    public function __construct()
189
-    {
190
-        global $wpdb;
191
-        $this->_old_table           = $wpdb->prefix . "events_detail";
192
-        $this->_old_start_end_table = $wpdb->prefix . "events_start_end";
193
-        $this->_new_table           = $wpdb->prefix . "posts";
194
-        $this->_new_meta_table      = $wpdb->prefix . "esp_event_meta";
195
-        $this->_new_datetime_table  = $wpdb->prefix . "esp_datetime";
196
-        $this->_pretty_name         = esc_html__("Events", "event_espresso");
197
-        parent::__construct();
198
-    }
199
-
200
-
201
-    /**
202
-     * Counts the records to migrate; the public version may cache it
203
-     *
204
-     * @return int
205
-     */
206
-    public function _count_records_to_migrate(): int
207
-    {
208
-        global $wpdb;
209
-        return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $this->_old_table . ' WHERE event_status !="D"');
210
-    }
211
-
212
-
213
-    /**
214
-     * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
215
-     * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
216
-     * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
217
-     * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
218
-     * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
219
-     * very least we MUST report/return 50 items migrated)
220
-     *
221
-     * @param int $num_items_to_migrate
222
-     * @return int number of items ACTUALLY migrated
223
-     * @throws EE_Error
224
-     */
225
-    protected function _migration_step($num_items_to_migrate = 50)
226
-    {
227
-        global $wpdb;
228
-        // because the migration of each event can be a LOT more work, make each step smaller
229
-        $num_items_to_migrate     = max(1, $num_items_to_migrate / 5);
230
-        $events                   =
231
-            $wpdb->get_results(
232
-                $wpdb->prepare(
233
-                    "SELECT * FROM $this->_old_table WHERE event_status!='D' LIMIT %d,%d",
234
-                    $this->count_records_migrated(),
235
-                    $num_items_to_migrate
236
-                ),
237
-                ARRAY_A
238
-            );
239
-        $items_migrated_this_step = 0;
240
-
241
-        foreach ($events as $event_row) {
242
-            $guid = null;
243
-            // insert new 4.1 Attendee object using $wpdb
244
-            $post_id = $this->_insert_cpt($event_row);
245
-            if ($post_id) {
246
-                $this->get_migration_script()->set_mapping(
247
-                    $this->_old_table,
248
-                    $event_row['id'],
249
-                    $this->_new_table,
250
-                    $post_id
251
-                );
252
-                $meta_id = $this->_insert_event_meta($event_row, $post_id);
253
-                if ($meta_id) {
254
-                    $this->get_migration_script()->set_mapping(
255
-                        $this->_old_table,
256
-                        $event_row['id'],
257
-                        $this->_new_meta_table,
258
-                        $meta_id
259
-                    );
260
-                }
261
-                $this->_convert_start_end_times($event_row, $post_id);
262
-                $event_meta = maybe_unserialize($event_row['event_meta']);
263
-                $guid       = $event_meta['event_thumbnail_url'] ?? null;
264
-                $this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
265
-                    $guid,
266
-                    $post_id,
267
-                    $this
268
-                );
269
-
270
-                // maybe create a venue from info on the event?
271
-                $new_venue_id = $this->_maybe_create_venue($event_row);
272
-                if ($new_venue_id) {
273
-                    $this->_insert_new_venue_to_event($post_id, $new_venue_id);
274
-                }
275
-                $this->_add_post_metas($event_row, $post_id);
276
-            }
277
-            $items_migrated_this_step++;
278
-            if ($guid) {
279
-                // if we had to check for an image attachment
280
-                // then let's call it a day (avoid timing out, because this took a long time)
281
-                break;
282
-            }
283
-        }
284
-        if ($this->count_records_migrated() + $items_migrated_this_step >= $this->count_records_to_migrate()) {
285
-            $this->set_status(EE_Data_Migration_Manager::status_completed);
286
-        }
287
-        return $items_migrated_this_step;
288
-    }
289
-
290
-
291
-    /**
292
-     * Stores any extra 3.1 "event_meta" column things as post meta
293
-     *
294
-     * @param array $old_event
295
-     * @param int   $post_id
296
-     * @return void
297
-     */
298
-    private function _add_post_metas(array $old_event, int $post_id)
299
-    {
300
-        $event_meta = maybe_unserialize($old_event['event_meta']);
301
-        if (! $event_meta || ! is_array($event_meta)) {
302
-            return;
303
-        }
304
-        unset($event_meta['date_submitted']);              // factored into CPT
305
-        unset($event_meta['additional_attendee_reg_info']);// factored into event meta table
306
-        unset($event_meta['default_payment_status']);      // dido
307
-        unset($event_meta['event_thumbnail_url']);         // used to find post featured image
308
-        foreach ($event_meta as $meta_key => $meta_value) {
309
-            if ($meta_key) {// if the meta key is just an empty string, ignore it
310
-                $success = add_post_meta($post_id, $meta_key, $meta_value, true);
311
-                if (! $success) {
312
-                    $this->add_error(
313
-                        sprintf(
314
-                            esc_html__(
315
-                                "Could not add post meta for CPT with ID #%d. Meta key: '%s',meta value:'%d' for 3.1 event: %s",
316
-                                "event_espresso"
317
-                            ),
318
-                            $post_id,
319
-                            $meta_key,
320
-                            $meta_value,
321
-                            implode(",", $old_event)
322
-                        )
323
-                    );
324
-                }
325
-            }
326
-        }
327
-        if ($old_event['alt_email']) {
328
-            add_post_meta($post_id, 'alt_email', $old_event['alt_email']);
329
-        }
330
-        if ($old_event['recurrence_id']) {
331
-            add_post_meta($post_id, 'recurrence_id', $old_event['recurrence_id']);
332
-        }
333
-    }
334
-
335
-
336
-    /**
337
-     * Finds a unique slug for this event, given its name (we could have simply used
338
-     * the old unique_identifier column, but it added a long string of seemingly random characters onto the end
339
-     * and really wasn't that pretty for a slug, so we decided we'd make our own slug again)
340
-     *
341
-     * @param string $event_name      (the name of the event for reading by humans)
342
-     * @param string $old_identifier  the old EE3 identifier (a long unique string)
343
-     * @param string $new_post_status a post status
344
-     * @return string
345
-     */
346
-    private function _find_unique_slug(
347
-        string $event_name,
348
-        string $old_identifier = '',
349
-        string $new_post_status = 'publish'
350
-    ): string {
351
-        $original_name = $event_name ? sanitize_title($event_name) : $old_identifier;
352
-        return wp_unique_post_slug($original_name, 0, $new_post_status, 'espresso_events', 0);
353
-    }
354
-
355
-
356
-    /**
357
-     * returns whether there is a post that has this same slug (post_title)
358
-     *
359
-     * @param string $slug
360
-     * @return boolean
361
-     * @global wpdb  $wpdb
362
-     */
363
-    private function _other_post_exists_with_that_slug(string $slug): bool
364
-    {
365
-        global $wpdb;
366
-        $query =
367
-            $wpdb->prepare(
368
-                "SELECT COUNT(ID) FROM $this->_new_table WHERE event_status != 'D' AND post_name = %s",
369
-                $slug
370
-            );
371
-        $count = $wpdb->get_var($query);
372
-        return (bool) intval($count);
373
-    }
374
-
375
-
376
-    /**
377
-     * @param $old_event
378
-     * @return int
379
-     */
380
-    private function _insert_cpt($old_event): int
381
-    {
382
-        global $wpdb;
383
-        // convert 3.1 event status to 4.1 CPT status
384
-        // for reference, 3.1 event stati available for setting are:
385
-        //      $status = array(array('id' => 'A', 'text' => esc_html__('Public', 'event_espresso')), array('id' => 'S', 'text' => esc_html__('Waitlist', 'event_espresso')), array('id' => 'O', 'text' => esc_html__('Ongoing', 'event_espresso')), array('id' => 'R', 'text' => esc_html__('Draft', 'event_espresso')), array('id' => 'D', 'text' => esc_html__('Deleted', 'event_espresso')));
386
-        //      and the json api uses the following to convert from 3.1 to 4.0
387
-        //      'S'=>'secondary/waitlist',
388
-        //      'A'=>'active',
389
-        //      'X'=>'denied',
390
-        //      'IA'=>'inactive',
391
-        //      'O'=>'ongoing',
392
-        //      'P'=>'pending',
393
-        //      'R'=>'draft',
394
-        //      'D'=>'deleted');
395
-        //      4.1 Event Post stati are the normal post statis
396
-        //      (publish,future,draft,pending,private,trash,auto-draft,inherit)
397
-        //      and 3 custom ones: cancelled,postponed,sold_out
398
-        $status_conversions = [
399
-            'R'  => 'draft',
400
-            'X'  => 'draft',
401
-            // 4.1 doesn't have a "not approved for publishing" status. this is what posts are set to that aren't approved
402
-            'P'  => 'pending',
403
-            'IA' => 'draft',
404
-            // draft and in the past
405
-            // IA=inactive in 3.1: events were switched to this when they expired. in 4.1 that's just calculated
406
-            'O'  => 'publish',
407
-            // @todo: will be an event type later; if this is the status, set the end date WAAAY later; and add term for 'ongoing'
408
-            'A'  => 'publish',
409
-            'S'  => 'draft',
410
-            // @todo: is it ok to just mark secondary/waitlist events as DRAFTS?
411
-            'D'  => 'trash',
412
-        ];
413
-        $post_status        = $status_conversions[ $old_event['event_status'] ];
414
-        // check if we've sold out
415
-        if (intval($old_event['reg_limit']) <= self::count_registrations($old_event['id'])) {
416
-            $post_status = 'sold_out';
417
-        }
418
-        //      FYI postponed and cancelled don't exist in 3.1
419
-        $cols_n_values                      = [
420
-            'post_title'        => stripslashes($old_event['event_name']),// EVT_name
421
-            'post_content'      => stripslashes($old_event['event_desc']),// EVT_desc
422
-            'post_name'         => $this->_find_unique_slug(
423
-                $old_event['event_name'],
424
-                $old_event['event_identifier'],
425
-                $post_status
426
-            ),// EVT_slug
427
-            'post_date'         => $old_event['submitted'],// EVT_created NOT
428
-            'post_date_gmt'     => get_gmt_from_date($old_event['submitted']),
429
-            'post_excerpt'      => '',// EVT_short_desc
430
-            'post_modified'     => $old_event['submitted'],// EVT_modified
431
-            'post_modified_gmt' => get_gmt_from_date($old_event['submitted']),
432
-            'post_author'       => $old_event['wp_user'],// EVT_wp_user
433
-            'post_parent'       => 0,// parent maybe get this from some REM field?
434
-            'menu_order'        => 0,// EVT_order
435
-            'post_type'         => 'espresso_events',// post_type
436
-            'post_status'       => $post_status,// status
437
-        ];
438
-        $cols_n_values_with_no_invalid_text = [];
439
-        foreach ($cols_n_values as $col => $value) {
440
-            $value_sans_invalid_chars = $wpdb->strip_invalid_text_for_column($this->_new_table, $col, $value);
441
-            if (! is_wp_error($value_sans_invalid_chars)) {
442
-                $cols_n_values_with_no_invalid_text[ $col ] = $value_sans_invalid_chars;
443
-            } else {
444
-                // otherwise leave it as-is. It will blow everything up and stop the migration
445
-                $cols_n_values_with_no_invalid_text[ $col ] = $value;
446
-            }
447
-        }
448
-        $cols_n_values = $cols_n_values_with_no_invalid_text;
449
-        $data_types    = [
450
-            '%s',// EVT_name
451
-            '%s',// EVT_desc
452
-            '%s',// EVT_slug
453
-            '%s',// EVT_created
454
-            '%s',
455
-            '%s',// EVT_short_desc
456
-            '%s',// EVT_modified
457
-            '%s',
458
-            '%s',// EVT_wp_user
459
-            '%d',// post_parent
460
-            '%d',// EVT_order
461
-            '%s',// post_type
462
-            '%s',// status
463
-        ];
464
-        $success       = $wpdb->insert(
465
-            $this->_new_table,
466
-            $cols_n_values,
467
-            $data_types
468
-        );
469
-        if (! $success) {
470
-            $this->add_error(
471
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
472
-                    $this->_old_table,
473
-                    $old_event,
474
-                    $this->_new_table,
475
-                    $cols_n_values,
476
-                    $data_types
477
-                )
478
-            );
479
-            return 0;
480
-        }
481
-        return $wpdb->insert_id;
482
-    }
483
-
484
-
485
-    /**
486
-     * Counts all the registrations for the event in the 3.1 DB. (takes into account attendee rows which represent
487
-     * various registrations)
488
-     *
489
-     * @param int   $event_id
490
-     * @return int
491
-     * @global wpdb $wpdb
492
-     */
493
-    public static function count_registrations(int $event_id): int
494
-    {
495
-        global $wpdb;
496
-        return (int) $wpdb->get_var(
497
-            $wpdb->prepare("SELECT sum(quantity) FROM {$wpdb->prefix}events_attendee WHERE event_id=%d", $event_id)
498
-        );
499
-    }
500
-
501
-
502
-    /**
503
-     * @param $old_event
504
-     * @param $new_cpt_id
505
-     * @return int
506
-     */
507
-    private function _insert_event_meta($old_event, $new_cpt_id): int
508
-    {
509
-        global $wpdb;
510
-        $event_meta = maybe_unserialize($old_event['event_meta']);
511
-        //      for reference, 3.1 'default_payment_status' are: $default_payment_status = array(
512
-        //  array('id' => "", 'text' => 'No Change'),
513
-        //  array('id' => 'Incomplete', 'text' => 'Incomplete'),
514
-        //  array('id' => 'Pending', 'text' => 'Pending'),
515
-        //  //array('id' => 'Completed', 'text' => 'Completed')
516
-        // );
517
-        $default_reg_status =
518
-            $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
519
-                $event_meta['default_payment_status'] ?? '',
520
-                intval($old_event['require_pre_approval'])
521
-            );
522
-        $cols_n_values      = [
523
-            'EVT_ID'                          => $new_cpt_id,// EVT_ID_fk
524
-            'EVT_display_desc'                => 'Y' == $old_event['display_desc'],
525
-            'EVT_display_ticket_selector'     => 'Y' == $old_event['display_reg_form'],
526
-            'EVT_visible_on'                  => $this->get_migration_script()->convert_date_string_to_utc(
527
-                $this,
528
-                $old_event,
529
-                current_time(
530
-                    'mysql'
531
-                ),
532
-                $old_event['timezone_string']
533
-            ),// don't use the old 'visible_on', as it wasn't ever used
534
-            'EVT_additional_limit'            => $old_event['allow_multiple'] == 'N' ? 1
535
-                : $old_event['additional_limit'],
536
-            'EVT_default_registration_status' => $default_reg_status,
537
-            'EVT_member_only'                 => $old_event['member_only'],
538
-            'EVT_phone'                       => $old_event['phone'],
539
-            'EVT_allow_overflow'              => 'Y' == $old_event['allow_overflow'],
540
-            'EVT_timezone_string'             => $old_event['timezone_string'],
541
-            'EVT_external_URL'                => $old_event['externalURL'],
542
-            'EVT_donations'                   => false,// doesn't exist in 3.1
543
-
544
-        ];
545
-        $data_types         = [
546
-            '%s',// EVT_ID
547
-            '%d',// EVT_display_desc
548
-            '%d',// EVT_display_ticket_selector
549
-            '%s',// EVT_visible_on
550
-            '%d',// EVT_additional_limit
551
-            '%s',// EVT_default_registration_status
552
-            '%d',// EVT_member_only
553
-            '%s',// EVT_phone
554
-            '%d',// EVT_allow_overflow
555
-            '%s',// EVT_timezone_string
556
-            '%s',// EVT_external_URL
557
-            '%d',// EVT_donations
558
-        ];
559
-        $success            = $wpdb->insert(
560
-            $this->_new_meta_table,
561
-            $cols_n_values,
562
-            $data_types
563
-        );
564
-        if (! $success) {
565
-            $this->add_error(
566
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
567
-                    $this->_old_table,
568
-                    $old_event,
569
-                    $this->_new_meta_table,
570
-                    $cols_n_values,
571
-                    $data_types
572
-                )
573
-            );
574
-            return 0;
575
-        }
576
-        return $wpdb->insert_id;
577
-    }
578
-
579
-
580
-    /**
581
-     * @param $old_event
582
-     * @return int
583
-     */
584
-    private function _maybe_create_venue($old_event): int
585
-    {
586
-        if (
587
-            $old_event['address'] ||
588
-            $old_event['address2'] ||
589
-            $old_event['city'] ||
590
-            $old_event['state'] ||
591
-            $old_event['zip'] ||
592
-            $old_event['venue_title'] ||
593
-            $old_event['venue_url'] ||
594
-            $old_event['venue_image'] ||
595
-            $old_event['venue_phone'] ||
596
-            $old_event['virtual_url'] ||
597
-            $old_event['virtual_phone']
598
-        ) {
599
-            $old_id = $this->_duplicate_venue_exists($old_event);
600
-            if ($old_id) {
601
-                return $old_id;
602
-            }
603
-            $new_id = $this->_insert_venue_into_posts($old_event);
604
-            if ($new_id) {
605
-                $this->_insert_venue_into_meta_table($new_id, $old_event);
606
-                $guid = $old_event['venue_image'] ?? null;
607
-                $this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
608
-                    $guid,
609
-                    $new_id,
610
-                    $this
611
-                );
612
-            }
613
-            // we don't bother recording the conversion from old events to venues as that
614
-            // will complicate finding the conversion from old venues to new events
615
-            return $new_id;
616
-        }
617
-        return 0;
618
-    }
619
-
620
-
621
-    /**
622
-     * Assuming there is venue data on this event, check if there is a duplicate venue already in the system for it.
623
-     * If so, return it. Otherwise, return NULL.
624
-     *
625
-     * @param array $old_event
626
-     * @return int duplicate venue id
627
-     */
628
-    private function _duplicate_venue_exists(array $old_event): int
629
-    {
630
-        global $wpdb;
631
-        $conditions     = [
632
-            'VNU_address'       => $old_event ['address'],
633
-            'VNU_address2'      => $old_event['address2'],
634
-            'VNU_city'          => $old_event['city'],
635
-            'VNU_zip'           => $old_event['zip'],
636
-            'post_title'        => $this->_get_venue_title_for_event($old_event),
637
-            'VNU_phone'         => $old_event['venue_phone'],// VNU_phone
638
-            'VNU_url'           => $old_event['venue_url'],// VNU_url
639
-            'VNU_virtual_phone' => $old_event['virtual_phone'],// VNU_virtual_phone
640
-            'VNU_virtual_url'   => $old_event['virtual_url'],// VNU_virtual_url
641
-        ];
642
-        $sql_conditions = [];
643
-        foreach ($conditions as $column => $value) {
644
-            $sql_conditions [] = $wpdb->prepare("$column = %s", $value);
645
-        }
646
-        $query = "SELECT VNU_ID
178
+	private string $_old_start_end_table;
179
+
180
+	private string $_new_meta_table;
181
+
182
+	private string $_new_datetime_table;
183
+
184
+
185
+	/**
186
+	 * Just initializes the status of the migration
187
+	 */
188
+	public function __construct()
189
+	{
190
+		global $wpdb;
191
+		$this->_old_table           = $wpdb->prefix . "events_detail";
192
+		$this->_old_start_end_table = $wpdb->prefix . "events_start_end";
193
+		$this->_new_table           = $wpdb->prefix . "posts";
194
+		$this->_new_meta_table      = $wpdb->prefix . "esp_event_meta";
195
+		$this->_new_datetime_table  = $wpdb->prefix . "esp_datetime";
196
+		$this->_pretty_name         = esc_html__("Events", "event_espresso");
197
+		parent::__construct();
198
+	}
199
+
200
+
201
+	/**
202
+	 * Counts the records to migrate; the public version may cache it
203
+	 *
204
+	 * @return int
205
+	 */
206
+	public function _count_records_to_migrate(): int
207
+	{
208
+		global $wpdb;
209
+		return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $this->_old_table . ' WHERE event_status !="D"');
210
+	}
211
+
212
+
213
+	/**
214
+	 * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
215
+	 * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
216
+	 * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
217
+	 * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
218
+	 * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
219
+	 * very least we MUST report/return 50 items migrated)
220
+	 *
221
+	 * @param int $num_items_to_migrate
222
+	 * @return int number of items ACTUALLY migrated
223
+	 * @throws EE_Error
224
+	 */
225
+	protected function _migration_step($num_items_to_migrate = 50)
226
+	{
227
+		global $wpdb;
228
+		// because the migration of each event can be a LOT more work, make each step smaller
229
+		$num_items_to_migrate     = max(1, $num_items_to_migrate / 5);
230
+		$events                   =
231
+			$wpdb->get_results(
232
+				$wpdb->prepare(
233
+					"SELECT * FROM $this->_old_table WHERE event_status!='D' LIMIT %d,%d",
234
+					$this->count_records_migrated(),
235
+					$num_items_to_migrate
236
+				),
237
+				ARRAY_A
238
+			);
239
+		$items_migrated_this_step = 0;
240
+
241
+		foreach ($events as $event_row) {
242
+			$guid = null;
243
+			// insert new 4.1 Attendee object using $wpdb
244
+			$post_id = $this->_insert_cpt($event_row);
245
+			if ($post_id) {
246
+				$this->get_migration_script()->set_mapping(
247
+					$this->_old_table,
248
+					$event_row['id'],
249
+					$this->_new_table,
250
+					$post_id
251
+				);
252
+				$meta_id = $this->_insert_event_meta($event_row, $post_id);
253
+				if ($meta_id) {
254
+					$this->get_migration_script()->set_mapping(
255
+						$this->_old_table,
256
+						$event_row['id'],
257
+						$this->_new_meta_table,
258
+						$meta_id
259
+					);
260
+				}
261
+				$this->_convert_start_end_times($event_row, $post_id);
262
+				$event_meta = maybe_unserialize($event_row['event_meta']);
263
+				$guid       = $event_meta['event_thumbnail_url'] ?? null;
264
+				$this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
265
+					$guid,
266
+					$post_id,
267
+					$this
268
+				);
269
+
270
+				// maybe create a venue from info on the event?
271
+				$new_venue_id = $this->_maybe_create_venue($event_row);
272
+				if ($new_venue_id) {
273
+					$this->_insert_new_venue_to_event($post_id, $new_venue_id);
274
+				}
275
+				$this->_add_post_metas($event_row, $post_id);
276
+			}
277
+			$items_migrated_this_step++;
278
+			if ($guid) {
279
+				// if we had to check for an image attachment
280
+				// then let's call it a day (avoid timing out, because this took a long time)
281
+				break;
282
+			}
283
+		}
284
+		if ($this->count_records_migrated() + $items_migrated_this_step >= $this->count_records_to_migrate()) {
285
+			$this->set_status(EE_Data_Migration_Manager::status_completed);
286
+		}
287
+		return $items_migrated_this_step;
288
+	}
289
+
290
+
291
+	/**
292
+	 * Stores any extra 3.1 "event_meta" column things as post meta
293
+	 *
294
+	 * @param array $old_event
295
+	 * @param int   $post_id
296
+	 * @return void
297
+	 */
298
+	private function _add_post_metas(array $old_event, int $post_id)
299
+	{
300
+		$event_meta = maybe_unserialize($old_event['event_meta']);
301
+		if (! $event_meta || ! is_array($event_meta)) {
302
+			return;
303
+		}
304
+		unset($event_meta['date_submitted']);              // factored into CPT
305
+		unset($event_meta['additional_attendee_reg_info']);// factored into event meta table
306
+		unset($event_meta['default_payment_status']);      // dido
307
+		unset($event_meta['event_thumbnail_url']);         // used to find post featured image
308
+		foreach ($event_meta as $meta_key => $meta_value) {
309
+			if ($meta_key) {// if the meta key is just an empty string, ignore it
310
+				$success = add_post_meta($post_id, $meta_key, $meta_value, true);
311
+				if (! $success) {
312
+					$this->add_error(
313
+						sprintf(
314
+							esc_html__(
315
+								"Could not add post meta for CPT with ID #%d. Meta key: '%s',meta value:'%d' for 3.1 event: %s",
316
+								"event_espresso"
317
+							),
318
+							$post_id,
319
+							$meta_key,
320
+							$meta_value,
321
+							implode(",", $old_event)
322
+						)
323
+					);
324
+				}
325
+			}
326
+		}
327
+		if ($old_event['alt_email']) {
328
+			add_post_meta($post_id, 'alt_email', $old_event['alt_email']);
329
+		}
330
+		if ($old_event['recurrence_id']) {
331
+			add_post_meta($post_id, 'recurrence_id', $old_event['recurrence_id']);
332
+		}
333
+	}
334
+
335
+
336
+	/**
337
+	 * Finds a unique slug for this event, given its name (we could have simply used
338
+	 * the old unique_identifier column, but it added a long string of seemingly random characters onto the end
339
+	 * and really wasn't that pretty for a slug, so we decided we'd make our own slug again)
340
+	 *
341
+	 * @param string $event_name      (the name of the event for reading by humans)
342
+	 * @param string $old_identifier  the old EE3 identifier (a long unique string)
343
+	 * @param string $new_post_status a post status
344
+	 * @return string
345
+	 */
346
+	private function _find_unique_slug(
347
+		string $event_name,
348
+		string $old_identifier = '',
349
+		string $new_post_status = 'publish'
350
+	): string {
351
+		$original_name = $event_name ? sanitize_title($event_name) : $old_identifier;
352
+		return wp_unique_post_slug($original_name, 0, $new_post_status, 'espresso_events', 0);
353
+	}
354
+
355
+
356
+	/**
357
+	 * returns whether there is a post that has this same slug (post_title)
358
+	 *
359
+	 * @param string $slug
360
+	 * @return boolean
361
+	 * @global wpdb  $wpdb
362
+	 */
363
+	private function _other_post_exists_with_that_slug(string $slug): bool
364
+	{
365
+		global $wpdb;
366
+		$query =
367
+			$wpdb->prepare(
368
+				"SELECT COUNT(ID) FROM $this->_new_table WHERE event_status != 'D' AND post_name = %s",
369
+				$slug
370
+			);
371
+		$count = $wpdb->get_var($query);
372
+		return (bool) intval($count);
373
+	}
374
+
375
+
376
+	/**
377
+	 * @param $old_event
378
+	 * @return int
379
+	 */
380
+	private function _insert_cpt($old_event): int
381
+	{
382
+		global $wpdb;
383
+		// convert 3.1 event status to 4.1 CPT status
384
+		// for reference, 3.1 event stati available for setting are:
385
+		//      $status = array(array('id' => 'A', 'text' => esc_html__('Public', 'event_espresso')), array('id' => 'S', 'text' => esc_html__('Waitlist', 'event_espresso')), array('id' => 'O', 'text' => esc_html__('Ongoing', 'event_espresso')), array('id' => 'R', 'text' => esc_html__('Draft', 'event_espresso')), array('id' => 'D', 'text' => esc_html__('Deleted', 'event_espresso')));
386
+		//      and the json api uses the following to convert from 3.1 to 4.0
387
+		//      'S'=>'secondary/waitlist',
388
+		//      'A'=>'active',
389
+		//      'X'=>'denied',
390
+		//      'IA'=>'inactive',
391
+		//      'O'=>'ongoing',
392
+		//      'P'=>'pending',
393
+		//      'R'=>'draft',
394
+		//      'D'=>'deleted');
395
+		//      4.1 Event Post stati are the normal post statis
396
+		//      (publish,future,draft,pending,private,trash,auto-draft,inherit)
397
+		//      and 3 custom ones: cancelled,postponed,sold_out
398
+		$status_conversions = [
399
+			'R'  => 'draft',
400
+			'X'  => 'draft',
401
+			// 4.1 doesn't have a "not approved for publishing" status. this is what posts are set to that aren't approved
402
+			'P'  => 'pending',
403
+			'IA' => 'draft',
404
+			// draft and in the past
405
+			// IA=inactive in 3.1: events were switched to this when they expired. in 4.1 that's just calculated
406
+			'O'  => 'publish',
407
+			// @todo: will be an event type later; if this is the status, set the end date WAAAY later; and add term for 'ongoing'
408
+			'A'  => 'publish',
409
+			'S'  => 'draft',
410
+			// @todo: is it ok to just mark secondary/waitlist events as DRAFTS?
411
+			'D'  => 'trash',
412
+		];
413
+		$post_status        = $status_conversions[ $old_event['event_status'] ];
414
+		// check if we've sold out
415
+		if (intval($old_event['reg_limit']) <= self::count_registrations($old_event['id'])) {
416
+			$post_status = 'sold_out';
417
+		}
418
+		//      FYI postponed and cancelled don't exist in 3.1
419
+		$cols_n_values                      = [
420
+			'post_title'        => stripslashes($old_event['event_name']),// EVT_name
421
+			'post_content'      => stripslashes($old_event['event_desc']),// EVT_desc
422
+			'post_name'         => $this->_find_unique_slug(
423
+				$old_event['event_name'],
424
+				$old_event['event_identifier'],
425
+				$post_status
426
+			),// EVT_slug
427
+			'post_date'         => $old_event['submitted'],// EVT_created NOT
428
+			'post_date_gmt'     => get_gmt_from_date($old_event['submitted']),
429
+			'post_excerpt'      => '',// EVT_short_desc
430
+			'post_modified'     => $old_event['submitted'],// EVT_modified
431
+			'post_modified_gmt' => get_gmt_from_date($old_event['submitted']),
432
+			'post_author'       => $old_event['wp_user'],// EVT_wp_user
433
+			'post_parent'       => 0,// parent maybe get this from some REM field?
434
+			'menu_order'        => 0,// EVT_order
435
+			'post_type'         => 'espresso_events',// post_type
436
+			'post_status'       => $post_status,// status
437
+		];
438
+		$cols_n_values_with_no_invalid_text = [];
439
+		foreach ($cols_n_values as $col => $value) {
440
+			$value_sans_invalid_chars = $wpdb->strip_invalid_text_for_column($this->_new_table, $col, $value);
441
+			if (! is_wp_error($value_sans_invalid_chars)) {
442
+				$cols_n_values_with_no_invalid_text[ $col ] = $value_sans_invalid_chars;
443
+			} else {
444
+				// otherwise leave it as-is. It will blow everything up and stop the migration
445
+				$cols_n_values_with_no_invalid_text[ $col ] = $value;
446
+			}
447
+		}
448
+		$cols_n_values = $cols_n_values_with_no_invalid_text;
449
+		$data_types    = [
450
+			'%s',// EVT_name
451
+			'%s',// EVT_desc
452
+			'%s',// EVT_slug
453
+			'%s',// EVT_created
454
+			'%s',
455
+			'%s',// EVT_short_desc
456
+			'%s',// EVT_modified
457
+			'%s',
458
+			'%s',// EVT_wp_user
459
+			'%d',// post_parent
460
+			'%d',// EVT_order
461
+			'%s',// post_type
462
+			'%s',// status
463
+		];
464
+		$success       = $wpdb->insert(
465
+			$this->_new_table,
466
+			$cols_n_values,
467
+			$data_types
468
+		);
469
+		if (! $success) {
470
+			$this->add_error(
471
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
472
+					$this->_old_table,
473
+					$old_event,
474
+					$this->_new_table,
475
+					$cols_n_values,
476
+					$data_types
477
+				)
478
+			);
479
+			return 0;
480
+		}
481
+		return $wpdb->insert_id;
482
+	}
483
+
484
+
485
+	/**
486
+	 * Counts all the registrations for the event in the 3.1 DB. (takes into account attendee rows which represent
487
+	 * various registrations)
488
+	 *
489
+	 * @param int   $event_id
490
+	 * @return int
491
+	 * @global wpdb $wpdb
492
+	 */
493
+	public static function count_registrations(int $event_id): int
494
+	{
495
+		global $wpdb;
496
+		return (int) $wpdb->get_var(
497
+			$wpdb->prepare("SELECT sum(quantity) FROM {$wpdb->prefix}events_attendee WHERE event_id=%d", $event_id)
498
+		);
499
+	}
500
+
501
+
502
+	/**
503
+	 * @param $old_event
504
+	 * @param $new_cpt_id
505
+	 * @return int
506
+	 */
507
+	private function _insert_event_meta($old_event, $new_cpt_id): int
508
+	{
509
+		global $wpdb;
510
+		$event_meta = maybe_unserialize($old_event['event_meta']);
511
+		//      for reference, 3.1 'default_payment_status' are: $default_payment_status = array(
512
+		//  array('id' => "", 'text' => 'No Change'),
513
+		//  array('id' => 'Incomplete', 'text' => 'Incomplete'),
514
+		//  array('id' => 'Pending', 'text' => 'Pending'),
515
+		//  //array('id' => 'Completed', 'text' => 'Completed')
516
+		// );
517
+		$default_reg_status =
518
+			$this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
519
+				$event_meta['default_payment_status'] ?? '',
520
+				intval($old_event['require_pre_approval'])
521
+			);
522
+		$cols_n_values      = [
523
+			'EVT_ID'                          => $new_cpt_id,// EVT_ID_fk
524
+			'EVT_display_desc'                => 'Y' == $old_event['display_desc'],
525
+			'EVT_display_ticket_selector'     => 'Y' == $old_event['display_reg_form'],
526
+			'EVT_visible_on'                  => $this->get_migration_script()->convert_date_string_to_utc(
527
+				$this,
528
+				$old_event,
529
+				current_time(
530
+					'mysql'
531
+				),
532
+				$old_event['timezone_string']
533
+			),// don't use the old 'visible_on', as it wasn't ever used
534
+			'EVT_additional_limit'            => $old_event['allow_multiple'] == 'N' ? 1
535
+				: $old_event['additional_limit'],
536
+			'EVT_default_registration_status' => $default_reg_status,
537
+			'EVT_member_only'                 => $old_event['member_only'],
538
+			'EVT_phone'                       => $old_event['phone'],
539
+			'EVT_allow_overflow'              => 'Y' == $old_event['allow_overflow'],
540
+			'EVT_timezone_string'             => $old_event['timezone_string'],
541
+			'EVT_external_URL'                => $old_event['externalURL'],
542
+			'EVT_donations'                   => false,// doesn't exist in 3.1
543
+
544
+		];
545
+		$data_types         = [
546
+			'%s',// EVT_ID
547
+			'%d',// EVT_display_desc
548
+			'%d',// EVT_display_ticket_selector
549
+			'%s',// EVT_visible_on
550
+			'%d',// EVT_additional_limit
551
+			'%s',// EVT_default_registration_status
552
+			'%d',// EVT_member_only
553
+			'%s',// EVT_phone
554
+			'%d',// EVT_allow_overflow
555
+			'%s',// EVT_timezone_string
556
+			'%s',// EVT_external_URL
557
+			'%d',// EVT_donations
558
+		];
559
+		$success            = $wpdb->insert(
560
+			$this->_new_meta_table,
561
+			$cols_n_values,
562
+			$data_types
563
+		);
564
+		if (! $success) {
565
+			$this->add_error(
566
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
567
+					$this->_old_table,
568
+					$old_event,
569
+					$this->_new_meta_table,
570
+					$cols_n_values,
571
+					$data_types
572
+				)
573
+			);
574
+			return 0;
575
+		}
576
+		return $wpdb->insert_id;
577
+	}
578
+
579
+
580
+	/**
581
+	 * @param $old_event
582
+	 * @return int
583
+	 */
584
+	private function _maybe_create_venue($old_event): int
585
+	{
586
+		if (
587
+			$old_event['address'] ||
588
+			$old_event['address2'] ||
589
+			$old_event['city'] ||
590
+			$old_event['state'] ||
591
+			$old_event['zip'] ||
592
+			$old_event['venue_title'] ||
593
+			$old_event['venue_url'] ||
594
+			$old_event['venue_image'] ||
595
+			$old_event['venue_phone'] ||
596
+			$old_event['virtual_url'] ||
597
+			$old_event['virtual_phone']
598
+		) {
599
+			$old_id = $this->_duplicate_venue_exists($old_event);
600
+			if ($old_id) {
601
+				return $old_id;
602
+			}
603
+			$new_id = $this->_insert_venue_into_posts($old_event);
604
+			if ($new_id) {
605
+				$this->_insert_venue_into_meta_table($new_id, $old_event);
606
+				$guid = $old_event['venue_image'] ?? null;
607
+				$this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
608
+					$guid,
609
+					$new_id,
610
+					$this
611
+				);
612
+			}
613
+			// we don't bother recording the conversion from old events to venues as that
614
+			// will complicate finding the conversion from old venues to new events
615
+			return $new_id;
616
+		}
617
+		return 0;
618
+	}
619
+
620
+
621
+	/**
622
+	 * Assuming there is venue data on this event, check if there is a duplicate venue already in the system for it.
623
+	 * If so, return it. Otherwise, return NULL.
624
+	 *
625
+	 * @param array $old_event
626
+	 * @return int duplicate venue id
627
+	 */
628
+	private function _duplicate_venue_exists(array $old_event): int
629
+	{
630
+		global $wpdb;
631
+		$conditions     = [
632
+			'VNU_address'       => $old_event ['address'],
633
+			'VNU_address2'      => $old_event['address2'],
634
+			'VNU_city'          => $old_event['city'],
635
+			'VNU_zip'           => $old_event['zip'],
636
+			'post_title'        => $this->_get_venue_title_for_event($old_event),
637
+			'VNU_phone'         => $old_event['venue_phone'],// VNU_phone
638
+			'VNU_url'           => $old_event['venue_url'],// VNU_url
639
+			'VNU_virtual_phone' => $old_event['virtual_phone'],// VNU_virtual_phone
640
+			'VNU_virtual_url'   => $old_event['virtual_url'],// VNU_virtual_url
641
+		];
642
+		$sql_conditions = [];
643
+		foreach ($conditions as $column => $value) {
644
+			$sql_conditions [] = $wpdb->prepare("$column = %s", $value);
645
+		}
646
+		$query = "SELECT VNU_ID
647 647
 					FROM
648 648
 		$wpdb->posts as p INNER JOIN
649 649
 		{$wpdb->prefix}esp_venue_meta as v ON p.ID = v.VNU_ID
650 650
 			WHERE " . implode(" AND ", $sql_conditions) . " LIMIT 1";
651
-        return (int) $wpdb->get_var($query);
652
-    }
653
-
654
-
655
-    /**
656
-     * Gets teh venue's title or makes one up if there is none
657
-     *
658
-     * @param array $event_data_array keys are events_details columns and values are their values
659
-     * @return string
660
-     */
661
-    protected function _get_venue_title_for_event(array $event_data_array): string
662
-    {
663
-        return $event_data_array['venue_title']
664
-            ? stripslashes($event_data_array['venue_title'])
665
-            : stripslashes(
666
-                sprintf(
667
-                    esc_html__('Venue of %s', 'event_espresso'),
668
-                    $event_data_array['event_name']
669
-                )
670
-            );
671
-    }
672
-
673
-
674
-    /**
675
-     * Inserts the CPT
676
-     *
677
-     * @param array $old_event keys are cols, values are col values
678
-     * @return int
679
-     */
680
-    private function _insert_venue_into_posts(array $old_event): int
681
-    {
682
-        global $wpdb;
683
-        $insertion_array  = [
684
-            'post_title'        => $this->_get_venue_title_for_event($old_event),// VNU_name
685
-            'post_content'      => '',// VNU_desc
686
-            'post_name'         => $this->_find_unique_slug(
687
-                $old_event['venue_title'],
688
-                sanitize_title('venue-of-' . $old_event['event_name'])
689
-            ),// VNU_identifier
690
-            'post_date'         => current_time('mysql'),// VNU_created
691
-            'post_date_gmt'     => get_gmt_from_date(current_time('mysql')),
692
-            'post_excerpt'      => '',// VNU_short_desc arbitrary only 50 characters
693
-            'post_modified'     => current_time('mysql'),// VNU_modified
694
-            'post_modified_gmt' => get_gmt_from_date(current_time('mysql')),
695
-            'post_author'       => $old_event['wp_user'],// VNU_wp_user
696
-            'post_parent'       => 0,// parent
697
-            'menu_order'        => 0,// VNU_order
698
-            'post_type'         => 'espresso_venues',// post_type
699
-        ];
700
-        $data_types_array = [
701
-            '%s',// VNU_name
702
-            '%s',// VNU_desc
703
-            '%s',// VNU_identifier
704
-            '%s',// VNU_created
705
-            '%s',
706
-            '%s',// VNU_short_desc
707
-            '%s',// VNU_modified
708
-            '%s',
709
-            '%d',// VNU_wp_user
710
-            '%d',// parent
711
-            '%d',// VNU_order
712
-            '%s',// post_type
713
-        ];
714
-        $success          = $wpdb->insert(
715
-            $wpdb->posts,
716
-            $insertion_array,
717
-            $data_types_array
718
-        );
719
-        if (! $success) {
720
-            $this->add_error(
721
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
722
-                    $this->_old_table,
723
-                    $old_event,
724
-                    $this->_new_table,
725
-                    $insertion_array,
726
-                    $data_types_array
727
-                )
728
-            );
729
-            return 0;
730
-        }
731
-        return $wpdb->insert_id;
732
-    }
733
-
734
-
735
-    /**
736
-     * Inserts into the venue_meta table
737
-     *
738
-     * @param int   $cpt_id
739
-     * @param array $old_event
740
-     * @return void
741
-     */
742
-    private function _insert_venue_into_meta_table(int $cpt_id, array $old_event): void
743
-    {
744
-        global $wpdb;
745
-
746
-        // assume the country is the same as the organization's old settings
747
-        $country_iso = $this->get_migration_script()->get_default_country_iso();
748
-        // find the state from the venue, or the organization, or just guess california
749
-        if (! $old_event['state']) {
750
-            $old_org_options = get_option('events_organization_settings');
751
-            $state_name      = stripslashes($old_org_options['organization_state']);
752
-        } else {
753
-            $state_name = $old_event['state'];
754
-        }
755
-        if (! $state_name) {
756
-            $state_name = 'CA';
757
-        }
758
-        // get a state ID with the same name, if possible
759
-        try {
760
-            $state    = $this->get_migration_script()->get_or_create_state($state_name, $country_iso);
761
-            $state_id = $state['STA_ID'];
762
-        } catch (EE_Error $e) {
763
-            $this->add_error($e->getMessage());
764
-            $state_id = 0;
765
-        }
766
-        // now insert into meta table
767
-        $insertion_array = [
768
-            'VNU_ID'              => $cpt_id,// VNU_ID_fk
769
-            'VNU_address'         => stripslashes($old_event['address']),// VNU_address
770
-            'VNU_address2'        => stripslashes($old_event['address2']),// VNU_address2
771
-            'VNU_city'            => stripslashes($old_event['city']),// VNU_city
772
-            'STA_ID'              => $state_id,// STA_ID
773
-            'CNT_ISO'             => $country_iso,// CNT_ISO
774
-            'VNU_zip'             => $old_event['zip'],// VNU_zip
775
-            'VNU_phone'           => $old_event['venue_phone'],// VNU_phone
776
-            'VNU_capacity'        => -1,// VNU_capacity
777
-            'VNU_url'             => $old_event['venue_url'],// VNU_url
778
-            'VNU_virtual_phone'   => $old_event['virtual_phone'],// VNU_virtual_phone
779
-            'VNU_virtual_url'     => $old_event['virtual_url'],// VNU_virtual_url
780
-            'VNU_google_map_link' => '',// VNU_google_map_link
781
-            'VNU_enable_for_gmap' => true, // VNU_enable_for_gmap
782
-        ];
783
-        $data_types      = [
784
-            '%d',// VNU_ID_fk
785
-            '%s',// VNU_address
786
-            '%s',// VNU_address2
787
-            '%s',// VNU_city
788
-            '%d',// STA_ID
789
-            '%s',// CNT_ISO
790
-            '%s',// VNU_zip
791
-            '%s',// VNU_phone
792
-            '%d',// VNU_capacity
793
-            '%s',// VNU_url
794
-            '%s',// VNU_virtual_phone
795
-            '%s',// VNU_virtual_url
796
-            '%s',// VNU_google_map_link
797
-            '%d',// VNU_enable_for_gmap
798
-        ];
799
-        $success         = $wpdb->insert($wpdb->prefix . "esp_venue_meta", $insertion_array, $data_types);
800
-        if (! $success) {
801
-            $this->add_error(
802
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
803
-                    $this->_old_table,
804
-                    $old_event,
805
-                    $this->_new_meta_table,
806
-                    $insertion_array,
807
-                    $data_types
808
-                )
809
-            );
810
-        }
811
-    }
812
-
813
-
814
-    /**
815
-     * @param $new_event_id
816
-     * @param $new_venue_id
817
-     * @return void
818
-     */
819
-    private function _insert_new_venue_to_event($new_event_id, $new_venue_id): void
820
-    {
821
-        global $wpdb;
822
-        if (! $new_event_id) {
823
-            $this->add_error(
824
-                sprintf(esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"), $new_event_id)
825
-            );
826
-            return;
827
-        }
828
-        if (! $new_venue_id) {
829
-            $this->add_error(
830
-                sprintf(esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"), $new_venue_id)
831
-            );
832
-            return;
833
-        }
834
-        $cols_n_values = [
835
-            'EVT_ID'      => $new_event_id,
836
-            'VNU_ID'      => $new_venue_id,
837
-            'EVV_primary' => true,
838
-        ];
839
-        $data_types    = [
840
-            '%d',// EVT_ID
841
-            '%d',// VNU_ID
842
-            '%d',// EVT_primary
843
-        ];
844
-        $success       = $wpdb->insert($wpdb->prefix . "esp_event_venue", $cols_n_values, $data_types);
845
-        if (! $success) {
846
-            $this->add_error(
847
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
848
-                    $this->_old_table,
849
-                    [],
850
-                    $this->_new_table,
851
-                    $cols_n_values,
852
-                    $data_types
853
-                )
854
-            );
855
-        }
856
-    }
857
-
858
-
859
-    /**
860
-     * Converts all the 3.1 start-end times for the event to 4.1 datetimes
861
-     *
862
-     * @param array $old_event  results of get_results(...,ARRAY_A)
863
-     * @param int   $new_cpt_id new post ID
864
-     * @return void (if there are errors though, adds them to the stage's error list
865
-     * @throws EE_Error
866
-     * @global wpdb $wpdb
867
-     */
868
-    private function _convert_start_end_times(array $old_event, int $new_cpt_id)
869
-    {
870
-        $start_end_times = $this->_get_old_start_end_times($old_event['id']);
871
-        foreach ($start_end_times as $start_end_time) {
872
-            $datetime_id = $this->_insert_new_datetime($start_end_time, $old_event, $new_cpt_id);
873
-            if ($datetime_id) {
874
-                $this->get_migration_script()->set_mapping(
875
-                    $this->_old_start_end_table,
876
-                    $start_end_time['id'],
877
-                    $this->_new_datetime_table,
878
-                    $datetime_id
879
-                );
880
-            }
881
-        }
882
-    }
883
-
884
-
885
-    /**
886
-     * Queries the 3.1 wp_events_start_end table to get all the start and end times for the event
887
-     *
888
-     * @param int   $old_event_id
889
-     * @return array
890
-     * @global wpdb $wpdb
891
-     */
892
-    private function _get_old_start_end_times(int $old_event_id): array
893
-    {
894
-        global $wpdb;
895
-        return $wpdb->get_results(
896
-            $wpdb->prepare("SELECT * FROM $this->_old_start_end_table WHERE event_id=%d", $old_event_id),
897
-            ARRAY_A
898
-        );
899
-    }
900
-
901
-
902
-    /**
903
-     * Inserts a 4.1 datetime given the 3.1 start_end db row and event_details row
904
-     *
905
-     * @param array $start_end_time_row
906
-     * @param array $old_event_row
907
-     * @param int   $new_cpt_id
908
-     * @return int ID of new datetime
909
-     */
910
-    private function _insert_new_datetime(array $start_end_time_row, array $old_event_row, int $new_cpt_id): int
911
-    {
912
-        global $wpdb;
913
-        $start_date         = $old_event_row['start_date'];
914
-        $start_time         = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['start_time']);
915
-        $end_date           = $old_event_row['end_date'];
916
-        $end_time           = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['end_time']);
917
-        $existing_datetimes = $this->_count_other_datetimes_exist_for_new_event($new_cpt_id);
918
-        $start_datetime_utc =
919
-            $this->get_migration_script()->convert_date_string_to_utc(
920
-                $this,
921
-                $start_end_time_row,
922
-                "$start_date $start_time:00",
923
-                $old_event_row['timezone_string']
924
-            );
925
-        $end_datetime_utc   =
926
-            $this->get_migration_script()->convert_date_string_to_utc(
927
-                $this,
928
-                $start_end_time_row,
929
-                "$end_date $end_time:00",
930
-                $old_event_row['timezone_string']
931
-            );
932
-        $cols_n_values      = [
933
-            'EVT_ID'        => $new_cpt_id,
934
-            // EVT_ID
935
-            'DTT_EVT_start' => $start_datetime_utc,
936
-            // DTT_EVT_start
937
-            'DTT_EVT_end'   => $end_datetime_utc,
938
-            // DTT_EVT_end
939
-            'DTT_reg_limit' => intval($start_end_time_row['reg_limit']) ? $start_end_time_row['reg_limit']
940
-                : $old_event_row['reg_limit'],
941
-            // DTT_reg_limit
942
-            'DTT_sold'      => 0,
943
-            // note: we will increment this as registrations are added during the migration
944
-            //          'DTT_is_primary'=> 0 == $existing_datetimes ,//DTT_is_primary... if count==0, then we'll call it the 'primary'
945
-            'DTT_order'     => $existing_datetimes,
946
-            // DTT_order, just give it the same order as the count of how many datetimes already exist
947
-            'DTT_parent'    => 0,
948
-            'DTT_deleted'   => false,
949
-        ];
950
-        $data_types         = [
951
-            '%d',// EVT_Id
952
-            '%s',// DTT_EVT_start
953
-            '%s',// DTT_EVT_end
954
-            '%d',// DTT_reg_limit
955
-            '%d',// DTT_sold
956
-            //          '%d',//DTT_is_primary
957
-            '%d',// DTT_order
958
-            '%d',// DTT_parent
959
-            '%d',// DTT_deleted
960
-        ];
961
-        $success            = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
962
-        if (! $success) {
963
-            $this->add_error(
964
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
965
-                    $this->_old_start_end_table,
966
-                    array_merge($old_event_row, $start_end_time_row),
967
-                    $this->_new_datetime_table,
968
-                    $cols_n_values,
969
-                    $data_types
970
-                )
971
-            );
972
-            return 0;
973
-        }
974
-        return $wpdb->insert_id;
975
-    }
976
-
977
-
978
-    /**
979
-     * Checks if there's a 4.1 datetime for this event already. This is mostly only handy
980
-     * when deciding whether a datetime we're about ot insert should be the 'primary' or not
981
-     *
982
-     * @param int   $cpt_event_id
983
-     * @return int
984
-     * @global wpdb $wpdb
985
-     */
986
-    private function _count_other_datetimes_exist_for_new_event(int $cpt_event_id): int
987
-    {
988
-        global $wpdb;
989
-        return (int) $wpdb->get_var(
990
-            $wpdb->prepare(
991
-                "SELECT COUNT(*) FROM $this->_new_datetime_table WHERE EVT_ID=%d",
992
-                $cpt_event_id
993
-            )
994
-        );
995
-    }
651
+		return (int) $wpdb->get_var($query);
652
+	}
653
+
654
+
655
+	/**
656
+	 * Gets teh venue's title or makes one up if there is none
657
+	 *
658
+	 * @param array $event_data_array keys are events_details columns and values are their values
659
+	 * @return string
660
+	 */
661
+	protected function _get_venue_title_for_event(array $event_data_array): string
662
+	{
663
+		return $event_data_array['venue_title']
664
+			? stripslashes($event_data_array['venue_title'])
665
+			: stripslashes(
666
+				sprintf(
667
+					esc_html__('Venue of %s', 'event_espresso'),
668
+					$event_data_array['event_name']
669
+				)
670
+			);
671
+	}
672
+
673
+
674
+	/**
675
+	 * Inserts the CPT
676
+	 *
677
+	 * @param array $old_event keys are cols, values are col values
678
+	 * @return int
679
+	 */
680
+	private function _insert_venue_into_posts(array $old_event): int
681
+	{
682
+		global $wpdb;
683
+		$insertion_array  = [
684
+			'post_title'        => $this->_get_venue_title_for_event($old_event),// VNU_name
685
+			'post_content'      => '',// VNU_desc
686
+			'post_name'         => $this->_find_unique_slug(
687
+				$old_event['venue_title'],
688
+				sanitize_title('venue-of-' . $old_event['event_name'])
689
+			),// VNU_identifier
690
+			'post_date'         => current_time('mysql'),// VNU_created
691
+			'post_date_gmt'     => get_gmt_from_date(current_time('mysql')),
692
+			'post_excerpt'      => '',// VNU_short_desc arbitrary only 50 characters
693
+			'post_modified'     => current_time('mysql'),// VNU_modified
694
+			'post_modified_gmt' => get_gmt_from_date(current_time('mysql')),
695
+			'post_author'       => $old_event['wp_user'],// VNU_wp_user
696
+			'post_parent'       => 0,// parent
697
+			'menu_order'        => 0,// VNU_order
698
+			'post_type'         => 'espresso_venues',// post_type
699
+		];
700
+		$data_types_array = [
701
+			'%s',// VNU_name
702
+			'%s',// VNU_desc
703
+			'%s',// VNU_identifier
704
+			'%s',// VNU_created
705
+			'%s',
706
+			'%s',// VNU_short_desc
707
+			'%s',// VNU_modified
708
+			'%s',
709
+			'%d',// VNU_wp_user
710
+			'%d',// parent
711
+			'%d',// VNU_order
712
+			'%s',// post_type
713
+		];
714
+		$success          = $wpdb->insert(
715
+			$wpdb->posts,
716
+			$insertion_array,
717
+			$data_types_array
718
+		);
719
+		if (! $success) {
720
+			$this->add_error(
721
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
722
+					$this->_old_table,
723
+					$old_event,
724
+					$this->_new_table,
725
+					$insertion_array,
726
+					$data_types_array
727
+				)
728
+			);
729
+			return 0;
730
+		}
731
+		return $wpdb->insert_id;
732
+	}
733
+
734
+
735
+	/**
736
+	 * Inserts into the venue_meta table
737
+	 *
738
+	 * @param int   $cpt_id
739
+	 * @param array $old_event
740
+	 * @return void
741
+	 */
742
+	private function _insert_venue_into_meta_table(int $cpt_id, array $old_event): void
743
+	{
744
+		global $wpdb;
745
+
746
+		// assume the country is the same as the organization's old settings
747
+		$country_iso = $this->get_migration_script()->get_default_country_iso();
748
+		// find the state from the venue, or the organization, or just guess california
749
+		if (! $old_event['state']) {
750
+			$old_org_options = get_option('events_organization_settings');
751
+			$state_name      = stripslashes($old_org_options['organization_state']);
752
+		} else {
753
+			$state_name = $old_event['state'];
754
+		}
755
+		if (! $state_name) {
756
+			$state_name = 'CA';
757
+		}
758
+		// get a state ID with the same name, if possible
759
+		try {
760
+			$state    = $this->get_migration_script()->get_or_create_state($state_name, $country_iso);
761
+			$state_id = $state['STA_ID'];
762
+		} catch (EE_Error $e) {
763
+			$this->add_error($e->getMessage());
764
+			$state_id = 0;
765
+		}
766
+		// now insert into meta table
767
+		$insertion_array = [
768
+			'VNU_ID'              => $cpt_id,// VNU_ID_fk
769
+			'VNU_address'         => stripslashes($old_event['address']),// VNU_address
770
+			'VNU_address2'        => stripslashes($old_event['address2']),// VNU_address2
771
+			'VNU_city'            => stripslashes($old_event['city']),// VNU_city
772
+			'STA_ID'              => $state_id,// STA_ID
773
+			'CNT_ISO'             => $country_iso,// CNT_ISO
774
+			'VNU_zip'             => $old_event['zip'],// VNU_zip
775
+			'VNU_phone'           => $old_event['venue_phone'],// VNU_phone
776
+			'VNU_capacity'        => -1,// VNU_capacity
777
+			'VNU_url'             => $old_event['venue_url'],// VNU_url
778
+			'VNU_virtual_phone'   => $old_event['virtual_phone'],// VNU_virtual_phone
779
+			'VNU_virtual_url'     => $old_event['virtual_url'],// VNU_virtual_url
780
+			'VNU_google_map_link' => '',// VNU_google_map_link
781
+			'VNU_enable_for_gmap' => true, // VNU_enable_for_gmap
782
+		];
783
+		$data_types      = [
784
+			'%d',// VNU_ID_fk
785
+			'%s',// VNU_address
786
+			'%s',// VNU_address2
787
+			'%s',// VNU_city
788
+			'%d',// STA_ID
789
+			'%s',// CNT_ISO
790
+			'%s',// VNU_zip
791
+			'%s',// VNU_phone
792
+			'%d',// VNU_capacity
793
+			'%s',// VNU_url
794
+			'%s',// VNU_virtual_phone
795
+			'%s',// VNU_virtual_url
796
+			'%s',// VNU_google_map_link
797
+			'%d',// VNU_enable_for_gmap
798
+		];
799
+		$success         = $wpdb->insert($wpdb->prefix . "esp_venue_meta", $insertion_array, $data_types);
800
+		if (! $success) {
801
+			$this->add_error(
802
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
803
+					$this->_old_table,
804
+					$old_event,
805
+					$this->_new_meta_table,
806
+					$insertion_array,
807
+					$data_types
808
+				)
809
+			);
810
+		}
811
+	}
812
+
813
+
814
+	/**
815
+	 * @param $new_event_id
816
+	 * @param $new_venue_id
817
+	 * @return void
818
+	 */
819
+	private function _insert_new_venue_to_event($new_event_id, $new_venue_id): void
820
+	{
821
+		global $wpdb;
822
+		if (! $new_event_id) {
823
+			$this->add_error(
824
+				sprintf(esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"), $new_event_id)
825
+			);
826
+			return;
827
+		}
828
+		if (! $new_venue_id) {
829
+			$this->add_error(
830
+				sprintf(esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"), $new_venue_id)
831
+			);
832
+			return;
833
+		}
834
+		$cols_n_values = [
835
+			'EVT_ID'      => $new_event_id,
836
+			'VNU_ID'      => $new_venue_id,
837
+			'EVV_primary' => true,
838
+		];
839
+		$data_types    = [
840
+			'%d',// EVT_ID
841
+			'%d',// VNU_ID
842
+			'%d',// EVT_primary
843
+		];
844
+		$success       = $wpdb->insert($wpdb->prefix . "esp_event_venue", $cols_n_values, $data_types);
845
+		if (! $success) {
846
+			$this->add_error(
847
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
848
+					$this->_old_table,
849
+					[],
850
+					$this->_new_table,
851
+					$cols_n_values,
852
+					$data_types
853
+				)
854
+			);
855
+		}
856
+	}
857
+
858
+
859
+	/**
860
+	 * Converts all the 3.1 start-end times for the event to 4.1 datetimes
861
+	 *
862
+	 * @param array $old_event  results of get_results(...,ARRAY_A)
863
+	 * @param int   $new_cpt_id new post ID
864
+	 * @return void (if there are errors though, adds them to the stage's error list
865
+	 * @throws EE_Error
866
+	 * @global wpdb $wpdb
867
+	 */
868
+	private function _convert_start_end_times(array $old_event, int $new_cpt_id)
869
+	{
870
+		$start_end_times = $this->_get_old_start_end_times($old_event['id']);
871
+		foreach ($start_end_times as $start_end_time) {
872
+			$datetime_id = $this->_insert_new_datetime($start_end_time, $old_event, $new_cpt_id);
873
+			if ($datetime_id) {
874
+				$this->get_migration_script()->set_mapping(
875
+					$this->_old_start_end_table,
876
+					$start_end_time['id'],
877
+					$this->_new_datetime_table,
878
+					$datetime_id
879
+				);
880
+			}
881
+		}
882
+	}
883
+
884
+
885
+	/**
886
+	 * Queries the 3.1 wp_events_start_end table to get all the start and end times for the event
887
+	 *
888
+	 * @param int   $old_event_id
889
+	 * @return array
890
+	 * @global wpdb $wpdb
891
+	 */
892
+	private function _get_old_start_end_times(int $old_event_id): array
893
+	{
894
+		global $wpdb;
895
+		return $wpdb->get_results(
896
+			$wpdb->prepare("SELECT * FROM $this->_old_start_end_table WHERE event_id=%d", $old_event_id),
897
+			ARRAY_A
898
+		);
899
+	}
900
+
901
+
902
+	/**
903
+	 * Inserts a 4.1 datetime given the 3.1 start_end db row and event_details row
904
+	 *
905
+	 * @param array $start_end_time_row
906
+	 * @param array $old_event_row
907
+	 * @param int   $new_cpt_id
908
+	 * @return int ID of new datetime
909
+	 */
910
+	private function _insert_new_datetime(array $start_end_time_row, array $old_event_row, int $new_cpt_id): int
911
+	{
912
+		global $wpdb;
913
+		$start_date         = $old_event_row['start_date'];
914
+		$start_time         = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['start_time']);
915
+		$end_date           = $old_event_row['end_date'];
916
+		$end_time           = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['end_time']);
917
+		$existing_datetimes = $this->_count_other_datetimes_exist_for_new_event($new_cpt_id);
918
+		$start_datetime_utc =
919
+			$this->get_migration_script()->convert_date_string_to_utc(
920
+				$this,
921
+				$start_end_time_row,
922
+				"$start_date $start_time:00",
923
+				$old_event_row['timezone_string']
924
+			);
925
+		$end_datetime_utc   =
926
+			$this->get_migration_script()->convert_date_string_to_utc(
927
+				$this,
928
+				$start_end_time_row,
929
+				"$end_date $end_time:00",
930
+				$old_event_row['timezone_string']
931
+			);
932
+		$cols_n_values      = [
933
+			'EVT_ID'        => $new_cpt_id,
934
+			// EVT_ID
935
+			'DTT_EVT_start' => $start_datetime_utc,
936
+			// DTT_EVT_start
937
+			'DTT_EVT_end'   => $end_datetime_utc,
938
+			// DTT_EVT_end
939
+			'DTT_reg_limit' => intval($start_end_time_row['reg_limit']) ? $start_end_time_row['reg_limit']
940
+				: $old_event_row['reg_limit'],
941
+			// DTT_reg_limit
942
+			'DTT_sold'      => 0,
943
+			// note: we will increment this as registrations are added during the migration
944
+			//          'DTT_is_primary'=> 0 == $existing_datetimes ,//DTT_is_primary... if count==0, then we'll call it the 'primary'
945
+			'DTT_order'     => $existing_datetimes,
946
+			// DTT_order, just give it the same order as the count of how many datetimes already exist
947
+			'DTT_parent'    => 0,
948
+			'DTT_deleted'   => false,
949
+		];
950
+		$data_types         = [
951
+			'%d',// EVT_Id
952
+			'%s',// DTT_EVT_start
953
+			'%s',// DTT_EVT_end
954
+			'%d',// DTT_reg_limit
955
+			'%d',// DTT_sold
956
+			//          '%d',//DTT_is_primary
957
+			'%d',// DTT_order
958
+			'%d',// DTT_parent
959
+			'%d',// DTT_deleted
960
+		];
961
+		$success            = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
962
+		if (! $success) {
963
+			$this->add_error(
964
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
965
+					$this->_old_start_end_table,
966
+					array_merge($old_event_row, $start_end_time_row),
967
+					$this->_new_datetime_table,
968
+					$cols_n_values,
969
+					$data_types
970
+				)
971
+			);
972
+			return 0;
973
+		}
974
+		return $wpdb->insert_id;
975
+	}
976
+
977
+
978
+	/**
979
+	 * Checks if there's a 4.1 datetime for this event already. This is mostly only handy
980
+	 * when deciding whether a datetime we're about ot insert should be the 'primary' or not
981
+	 *
982
+	 * @param int   $cpt_event_id
983
+	 * @return int
984
+	 * @global wpdb $wpdb
985
+	 */
986
+	private function _count_other_datetimes_exist_for_new_event(int $cpt_event_id): int
987
+	{
988
+		global $wpdb;
989
+		return (int) $wpdb->get_var(
990
+			$wpdb->prepare(
991
+				"SELECT COUNT(*) FROM $this->_new_datetime_table WHERE EVT_ID=%d",
992
+				$cpt_event_id
993
+			)
994
+		);
995
+	}
996 996
 }
Please login to merge, or discard this patch.
Spacing   +143 added lines, -143 removed lines patch added patch discarded remove patch
@@ -188,11 +188,11 @@  discard block
 block discarded – undo
188 188
     public function __construct()
189 189
     {
190 190
         global $wpdb;
191
-        $this->_old_table           = $wpdb->prefix . "events_detail";
192
-        $this->_old_start_end_table = $wpdb->prefix . "events_start_end";
193
-        $this->_new_table           = $wpdb->prefix . "posts";
194
-        $this->_new_meta_table      = $wpdb->prefix . "esp_event_meta";
195
-        $this->_new_datetime_table  = $wpdb->prefix . "esp_datetime";
191
+        $this->_old_table           = $wpdb->prefix."events_detail";
192
+        $this->_old_start_end_table = $wpdb->prefix."events_start_end";
193
+        $this->_new_table           = $wpdb->prefix."posts";
194
+        $this->_new_meta_table      = $wpdb->prefix."esp_event_meta";
195
+        $this->_new_datetime_table  = $wpdb->prefix."esp_datetime";
196 196
         $this->_pretty_name         = esc_html__("Events", "event_espresso");
197 197
         parent::__construct();
198 198
     }
@@ -206,7 +206,7 @@  discard block
 block discarded – undo
206 206
     public function _count_records_to_migrate(): int
207 207
     {
208 208
         global $wpdb;
209
-        return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $this->_old_table . ' WHERE event_status !="D"');
209
+        return (int) $wpdb->get_var("SELECT COUNT(*) FROM ".$this->_old_table.' WHERE event_status !="D"');
210 210
     }
211 211
 
212 212
 
@@ -298,17 +298,17 @@  discard block
 block discarded – undo
298 298
     private function _add_post_metas(array $old_event, int $post_id)
299 299
     {
300 300
         $event_meta = maybe_unserialize($old_event['event_meta']);
301
-        if (! $event_meta || ! is_array($event_meta)) {
301
+        if ( ! $event_meta || ! is_array($event_meta)) {
302 302
             return;
303 303
         }
304
-        unset($event_meta['date_submitted']);              // factored into CPT
305
-        unset($event_meta['additional_attendee_reg_info']);// factored into event meta table
306
-        unset($event_meta['default_payment_status']);      // dido
307
-        unset($event_meta['event_thumbnail_url']);         // used to find post featured image
304
+        unset($event_meta['date_submitted']); // factored into CPT
305
+        unset($event_meta['additional_attendee_reg_info']); // factored into event meta table
306
+        unset($event_meta['default_payment_status']); // dido
307
+        unset($event_meta['event_thumbnail_url']); // used to find post featured image
308 308
         foreach ($event_meta as $meta_key => $meta_value) {
309 309
             if ($meta_key) {// if the meta key is just an empty string, ignore it
310 310
                 $success = add_post_meta($post_id, $meta_key, $meta_value, true);
311
-                if (! $success) {
311
+                if ( ! $success) {
312 312
                     $this->add_error(
313 313
                         sprintf(
314 314
                             esc_html__(
@@ -410,63 +410,63 @@  discard block
 block discarded – undo
410 410
             // @todo: is it ok to just mark secondary/waitlist events as DRAFTS?
411 411
             'D'  => 'trash',
412 412
         ];
413
-        $post_status        = $status_conversions[ $old_event['event_status'] ];
413
+        $post_status = $status_conversions[$old_event['event_status']];
414 414
         // check if we've sold out
415 415
         if (intval($old_event['reg_limit']) <= self::count_registrations($old_event['id'])) {
416 416
             $post_status = 'sold_out';
417 417
         }
418 418
         //      FYI postponed and cancelled don't exist in 3.1
419
-        $cols_n_values                      = [
420
-            'post_title'        => stripslashes($old_event['event_name']),// EVT_name
421
-            'post_content'      => stripslashes($old_event['event_desc']),// EVT_desc
419
+        $cols_n_values = [
420
+            'post_title'        => stripslashes($old_event['event_name']), // EVT_name
421
+            'post_content'      => stripslashes($old_event['event_desc']), // EVT_desc
422 422
             'post_name'         => $this->_find_unique_slug(
423 423
                 $old_event['event_name'],
424 424
                 $old_event['event_identifier'],
425 425
                 $post_status
426
-            ),// EVT_slug
427
-            'post_date'         => $old_event['submitted'],// EVT_created NOT
426
+            ), // EVT_slug
427
+            'post_date'         => $old_event['submitted'], // EVT_created NOT
428 428
             'post_date_gmt'     => get_gmt_from_date($old_event['submitted']),
429
-            'post_excerpt'      => '',// EVT_short_desc
430
-            'post_modified'     => $old_event['submitted'],// EVT_modified
429
+            'post_excerpt'      => '', // EVT_short_desc
430
+            'post_modified'     => $old_event['submitted'], // EVT_modified
431 431
             'post_modified_gmt' => get_gmt_from_date($old_event['submitted']),
432
-            'post_author'       => $old_event['wp_user'],// EVT_wp_user
433
-            'post_parent'       => 0,// parent maybe get this from some REM field?
434
-            'menu_order'        => 0,// EVT_order
435
-            'post_type'         => 'espresso_events',// post_type
436
-            'post_status'       => $post_status,// status
432
+            'post_author'       => $old_event['wp_user'], // EVT_wp_user
433
+            'post_parent'       => 0, // parent maybe get this from some REM field?
434
+            'menu_order'        => 0, // EVT_order
435
+            'post_type'         => 'espresso_events', // post_type
436
+            'post_status'       => $post_status, // status
437 437
         ];
438 438
         $cols_n_values_with_no_invalid_text = [];
439 439
         foreach ($cols_n_values as $col => $value) {
440 440
             $value_sans_invalid_chars = $wpdb->strip_invalid_text_for_column($this->_new_table, $col, $value);
441
-            if (! is_wp_error($value_sans_invalid_chars)) {
442
-                $cols_n_values_with_no_invalid_text[ $col ] = $value_sans_invalid_chars;
441
+            if ( ! is_wp_error($value_sans_invalid_chars)) {
442
+                $cols_n_values_with_no_invalid_text[$col] = $value_sans_invalid_chars;
443 443
             } else {
444 444
                 // otherwise leave it as-is. It will blow everything up and stop the migration
445
-                $cols_n_values_with_no_invalid_text[ $col ] = $value;
445
+                $cols_n_values_with_no_invalid_text[$col] = $value;
446 446
             }
447 447
         }
448 448
         $cols_n_values = $cols_n_values_with_no_invalid_text;
449 449
         $data_types    = [
450
-            '%s',// EVT_name
451
-            '%s',// EVT_desc
452
-            '%s',// EVT_slug
453
-            '%s',// EVT_created
450
+            '%s', // EVT_name
451
+            '%s', // EVT_desc
452
+            '%s', // EVT_slug
453
+            '%s', // EVT_created
454 454
             '%s',
455
-            '%s',// EVT_short_desc
456
-            '%s',// EVT_modified
455
+            '%s', // EVT_short_desc
456
+            '%s', // EVT_modified
457 457
             '%s',
458
-            '%s',// EVT_wp_user
459
-            '%d',// post_parent
460
-            '%d',// EVT_order
461
-            '%s',// post_type
462
-            '%s',// status
458
+            '%s', // EVT_wp_user
459
+            '%d', // post_parent
460
+            '%d', // EVT_order
461
+            '%s', // post_type
462
+            '%s', // status
463 463
         ];
464
-        $success       = $wpdb->insert(
464
+        $success = $wpdb->insert(
465 465
             $this->_new_table,
466 466
             $cols_n_values,
467 467
             $data_types
468 468
         );
469
-        if (! $success) {
469
+        if ( ! $success) {
470 470
             $this->add_error(
471 471
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
472 472
                     $this->_old_table,
@@ -519,8 +519,8 @@  discard block
 block discarded – undo
519 519
                 $event_meta['default_payment_status'] ?? '',
520 520
                 intval($old_event['require_pre_approval'])
521 521
             );
522
-        $cols_n_values      = [
523
-            'EVT_ID'                          => $new_cpt_id,// EVT_ID_fk
522
+        $cols_n_values = [
523
+            'EVT_ID'                          => $new_cpt_id, // EVT_ID_fk
524 524
             'EVT_display_desc'                => 'Y' == $old_event['display_desc'],
525 525
             'EVT_display_ticket_selector'     => 'Y' == $old_event['display_reg_form'],
526 526
             'EVT_visible_on'                  => $this->get_migration_script()->convert_date_string_to_utc(
@@ -530,7 +530,7 @@  discard block
 block discarded – undo
530 530
                     'mysql'
531 531
                 ),
532 532
                 $old_event['timezone_string']
533
-            ),// don't use the old 'visible_on', as it wasn't ever used
533
+            ), // don't use the old 'visible_on', as it wasn't ever used
534 534
             'EVT_additional_limit'            => $old_event['allow_multiple'] == 'N' ? 1
535 535
                 : $old_event['additional_limit'],
536 536
             'EVT_default_registration_status' => $default_reg_status,
@@ -539,29 +539,29 @@  discard block
 block discarded – undo
539 539
             'EVT_allow_overflow'              => 'Y' == $old_event['allow_overflow'],
540 540
             'EVT_timezone_string'             => $old_event['timezone_string'],
541 541
             'EVT_external_URL'                => $old_event['externalURL'],
542
-            'EVT_donations'                   => false,// doesn't exist in 3.1
542
+            'EVT_donations'                   => false, // doesn't exist in 3.1
543 543
 
544 544
         ];
545
-        $data_types         = [
546
-            '%s',// EVT_ID
547
-            '%d',// EVT_display_desc
548
-            '%d',// EVT_display_ticket_selector
549
-            '%s',// EVT_visible_on
550
-            '%d',// EVT_additional_limit
551
-            '%s',// EVT_default_registration_status
552
-            '%d',// EVT_member_only
553
-            '%s',// EVT_phone
554
-            '%d',// EVT_allow_overflow
555
-            '%s',// EVT_timezone_string
556
-            '%s',// EVT_external_URL
557
-            '%d',// EVT_donations
545
+        $data_types = [
546
+            '%s', // EVT_ID
547
+            '%d', // EVT_display_desc
548
+            '%d', // EVT_display_ticket_selector
549
+            '%s', // EVT_visible_on
550
+            '%d', // EVT_additional_limit
551
+            '%s', // EVT_default_registration_status
552
+            '%d', // EVT_member_only
553
+            '%s', // EVT_phone
554
+            '%d', // EVT_allow_overflow
555
+            '%s', // EVT_timezone_string
556
+            '%s', // EVT_external_URL
557
+            '%d', // EVT_donations
558 558
         ];
559
-        $success            = $wpdb->insert(
559
+        $success = $wpdb->insert(
560 560
             $this->_new_meta_table,
561 561
             $cols_n_values,
562 562
             $data_types
563 563
         );
564
-        if (! $success) {
564
+        if ( ! $success) {
565 565
             $this->add_error(
566 566
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
567 567
                     $this->_old_table,
@@ -628,16 +628,16 @@  discard block
 block discarded – undo
628 628
     private function _duplicate_venue_exists(array $old_event): int
629 629
     {
630 630
         global $wpdb;
631
-        $conditions     = [
631
+        $conditions = [
632 632
             'VNU_address'       => $old_event ['address'],
633 633
             'VNU_address2'      => $old_event['address2'],
634 634
             'VNU_city'          => $old_event['city'],
635 635
             'VNU_zip'           => $old_event['zip'],
636 636
             'post_title'        => $this->_get_venue_title_for_event($old_event),
637
-            'VNU_phone'         => $old_event['venue_phone'],// VNU_phone
638
-            'VNU_url'           => $old_event['venue_url'],// VNU_url
639
-            'VNU_virtual_phone' => $old_event['virtual_phone'],// VNU_virtual_phone
640
-            'VNU_virtual_url'   => $old_event['virtual_url'],// VNU_virtual_url
637
+            'VNU_phone'         => $old_event['venue_phone'], // VNU_phone
638
+            'VNU_url'           => $old_event['venue_url'], // VNU_url
639
+            'VNU_virtual_phone' => $old_event['virtual_phone'], // VNU_virtual_phone
640
+            'VNU_virtual_url'   => $old_event['virtual_url'], // VNU_virtual_url
641 641
         ];
642 642
         $sql_conditions = [];
643 643
         foreach ($conditions as $column => $value) {
@@ -647,7 +647,7 @@  discard block
 block discarded – undo
647 647
 					FROM
648 648
 		$wpdb->posts as p INNER JOIN
649 649
 		{$wpdb->prefix}esp_venue_meta as v ON p.ID = v.VNU_ID
650
-			WHERE " . implode(" AND ", $sql_conditions) . " LIMIT 1";
650
+			WHERE ".implode(" AND ", $sql_conditions)." LIMIT 1";
651 651
         return (int) $wpdb->get_var($query);
652 652
     }
653 653
 
@@ -680,43 +680,43 @@  discard block
 block discarded – undo
680 680
     private function _insert_venue_into_posts(array $old_event): int
681 681
     {
682 682
         global $wpdb;
683
-        $insertion_array  = [
684
-            'post_title'        => $this->_get_venue_title_for_event($old_event),// VNU_name
685
-            'post_content'      => '',// VNU_desc
683
+        $insertion_array = [
684
+            'post_title'        => $this->_get_venue_title_for_event($old_event), // VNU_name
685
+            'post_content'      => '', // VNU_desc
686 686
             'post_name'         => $this->_find_unique_slug(
687 687
                 $old_event['venue_title'],
688
-                sanitize_title('venue-of-' . $old_event['event_name'])
689
-            ),// VNU_identifier
690
-            'post_date'         => current_time('mysql'),// VNU_created
688
+                sanitize_title('venue-of-'.$old_event['event_name'])
689
+            ), // VNU_identifier
690
+            'post_date'         => current_time('mysql'), // VNU_created
691 691
             'post_date_gmt'     => get_gmt_from_date(current_time('mysql')),
692
-            'post_excerpt'      => '',// VNU_short_desc arbitrary only 50 characters
693
-            'post_modified'     => current_time('mysql'),// VNU_modified
692
+            'post_excerpt'      => '', // VNU_short_desc arbitrary only 50 characters
693
+            'post_modified'     => current_time('mysql'), // VNU_modified
694 694
             'post_modified_gmt' => get_gmt_from_date(current_time('mysql')),
695
-            'post_author'       => $old_event['wp_user'],// VNU_wp_user
696
-            'post_parent'       => 0,// parent
697
-            'menu_order'        => 0,// VNU_order
698
-            'post_type'         => 'espresso_venues',// post_type
695
+            'post_author'       => $old_event['wp_user'], // VNU_wp_user
696
+            'post_parent'       => 0, // parent
697
+            'menu_order'        => 0, // VNU_order
698
+            'post_type'         => 'espresso_venues', // post_type
699 699
         ];
700 700
         $data_types_array = [
701
-            '%s',// VNU_name
702
-            '%s',// VNU_desc
703
-            '%s',// VNU_identifier
704
-            '%s',// VNU_created
701
+            '%s', // VNU_name
702
+            '%s', // VNU_desc
703
+            '%s', // VNU_identifier
704
+            '%s', // VNU_created
705 705
             '%s',
706
-            '%s',// VNU_short_desc
707
-            '%s',// VNU_modified
706
+            '%s', // VNU_short_desc
707
+            '%s', // VNU_modified
708 708
             '%s',
709
-            '%d',// VNU_wp_user
710
-            '%d',// parent
711
-            '%d',// VNU_order
712
-            '%s',// post_type
709
+            '%d', // VNU_wp_user
710
+            '%d', // parent
711
+            '%d', // VNU_order
712
+            '%s', // post_type
713 713
         ];
714
-        $success          = $wpdb->insert(
714
+        $success = $wpdb->insert(
715 715
             $wpdb->posts,
716 716
             $insertion_array,
717 717
             $data_types_array
718 718
         );
719
-        if (! $success) {
719
+        if ( ! $success) {
720 720
             $this->add_error(
721 721
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
722 722
                     $this->_old_table,
@@ -746,13 +746,13 @@  discard block
 block discarded – undo
746 746
         // assume the country is the same as the organization's old settings
747 747
         $country_iso = $this->get_migration_script()->get_default_country_iso();
748 748
         // find the state from the venue, or the organization, or just guess california
749
-        if (! $old_event['state']) {
749
+        if ( ! $old_event['state']) {
750 750
             $old_org_options = get_option('events_organization_settings');
751 751
             $state_name      = stripslashes($old_org_options['organization_state']);
752 752
         } else {
753 753
             $state_name = $old_event['state'];
754 754
         }
755
-        if (! $state_name) {
755
+        if ( ! $state_name) {
756 756
             $state_name = 'CA';
757 757
         }
758 758
         // get a state ID with the same name, if possible
@@ -765,39 +765,39 @@  discard block
 block discarded – undo
765 765
         }
766 766
         // now insert into meta table
767 767
         $insertion_array = [
768
-            'VNU_ID'              => $cpt_id,// VNU_ID_fk
769
-            'VNU_address'         => stripslashes($old_event['address']),// VNU_address
770
-            'VNU_address2'        => stripslashes($old_event['address2']),// VNU_address2
771
-            'VNU_city'            => stripslashes($old_event['city']),// VNU_city
772
-            'STA_ID'              => $state_id,// STA_ID
773
-            'CNT_ISO'             => $country_iso,// CNT_ISO
774
-            'VNU_zip'             => $old_event['zip'],// VNU_zip
775
-            'VNU_phone'           => $old_event['venue_phone'],// VNU_phone
776
-            'VNU_capacity'        => -1,// VNU_capacity
777
-            'VNU_url'             => $old_event['venue_url'],// VNU_url
778
-            'VNU_virtual_phone'   => $old_event['virtual_phone'],// VNU_virtual_phone
779
-            'VNU_virtual_url'     => $old_event['virtual_url'],// VNU_virtual_url
780
-            'VNU_google_map_link' => '',// VNU_google_map_link
768
+            'VNU_ID'              => $cpt_id, // VNU_ID_fk
769
+            'VNU_address'         => stripslashes($old_event['address']), // VNU_address
770
+            'VNU_address2'        => stripslashes($old_event['address2']), // VNU_address2
771
+            'VNU_city'            => stripslashes($old_event['city']), // VNU_city
772
+            'STA_ID'              => $state_id, // STA_ID
773
+            'CNT_ISO'             => $country_iso, // CNT_ISO
774
+            'VNU_zip'             => $old_event['zip'], // VNU_zip
775
+            'VNU_phone'           => $old_event['venue_phone'], // VNU_phone
776
+            'VNU_capacity'        => -1, // VNU_capacity
777
+            'VNU_url'             => $old_event['venue_url'], // VNU_url
778
+            'VNU_virtual_phone'   => $old_event['virtual_phone'], // VNU_virtual_phone
779
+            'VNU_virtual_url'     => $old_event['virtual_url'], // VNU_virtual_url
780
+            'VNU_google_map_link' => '', // VNU_google_map_link
781 781
             'VNU_enable_for_gmap' => true, // VNU_enable_for_gmap
782 782
         ];
783
-        $data_types      = [
784
-            '%d',// VNU_ID_fk
785
-            '%s',// VNU_address
786
-            '%s',// VNU_address2
787
-            '%s',// VNU_city
788
-            '%d',// STA_ID
789
-            '%s',// CNT_ISO
790
-            '%s',// VNU_zip
791
-            '%s',// VNU_phone
792
-            '%d',// VNU_capacity
793
-            '%s',// VNU_url
794
-            '%s',// VNU_virtual_phone
795
-            '%s',// VNU_virtual_url
796
-            '%s',// VNU_google_map_link
797
-            '%d',// VNU_enable_for_gmap
783
+        $data_types = [
784
+            '%d', // VNU_ID_fk
785
+            '%s', // VNU_address
786
+            '%s', // VNU_address2
787
+            '%s', // VNU_city
788
+            '%d', // STA_ID
789
+            '%s', // CNT_ISO
790
+            '%s', // VNU_zip
791
+            '%s', // VNU_phone
792
+            '%d', // VNU_capacity
793
+            '%s', // VNU_url
794
+            '%s', // VNU_virtual_phone
795
+            '%s', // VNU_virtual_url
796
+            '%s', // VNU_google_map_link
797
+            '%d', // VNU_enable_for_gmap
798 798
         ];
799
-        $success         = $wpdb->insert($wpdb->prefix . "esp_venue_meta", $insertion_array, $data_types);
800
-        if (! $success) {
799
+        $success = $wpdb->insert($wpdb->prefix."esp_venue_meta", $insertion_array, $data_types);
800
+        if ( ! $success) {
801 801
             $this->add_error(
802 802
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
803 803
                     $this->_old_table,
@@ -819,13 +819,13 @@  discard block
 block discarded – undo
819 819
     private function _insert_new_venue_to_event($new_event_id, $new_venue_id): void
820 820
     {
821 821
         global $wpdb;
822
-        if (! $new_event_id) {
822
+        if ( ! $new_event_id) {
823 823
             $this->add_error(
824 824
                 sprintf(esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"), $new_event_id)
825 825
             );
826 826
             return;
827 827
         }
828
-        if (! $new_venue_id) {
828
+        if ( ! $new_venue_id) {
829 829
             $this->add_error(
830 830
                 sprintf(esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"), $new_venue_id)
831 831
             );
@@ -836,13 +836,13 @@  discard block
 block discarded – undo
836 836
             'VNU_ID'      => $new_venue_id,
837 837
             'EVV_primary' => true,
838 838
         ];
839
-        $data_types    = [
840
-            '%d',// EVT_ID
841
-            '%d',// VNU_ID
842
-            '%d',// EVT_primary
839
+        $data_types = [
840
+            '%d', // EVT_ID
841
+            '%d', // VNU_ID
842
+            '%d', // EVT_primary
843 843
         ];
844
-        $success       = $wpdb->insert($wpdb->prefix . "esp_event_venue", $cols_n_values, $data_types);
845
-        if (! $success) {
844
+        $success = $wpdb->insert($wpdb->prefix."esp_event_venue", $cols_n_values, $data_types);
845
+        if ( ! $success) {
846 846
             $this->add_error(
847 847
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
848 848
                     $this->_old_table,
@@ -922,14 +922,14 @@  discard block
 block discarded – undo
922 922
                 "$start_date $start_time:00",
923 923
                 $old_event_row['timezone_string']
924 924
             );
925
-        $end_datetime_utc   =
925
+        $end_datetime_utc =
926 926
             $this->get_migration_script()->convert_date_string_to_utc(
927 927
                 $this,
928 928
                 $start_end_time_row,
929 929
                 "$end_date $end_time:00",
930 930
                 $old_event_row['timezone_string']
931 931
             );
932
-        $cols_n_values      = [
932
+        $cols_n_values = [
933 933
             'EVT_ID'        => $new_cpt_id,
934 934
             // EVT_ID
935 935
             'DTT_EVT_start' => $start_datetime_utc,
@@ -947,19 +947,19 @@  discard block
 block discarded – undo
947 947
             'DTT_parent'    => 0,
948 948
             'DTT_deleted'   => false,
949 949
         ];
950
-        $data_types         = [
951
-            '%d',// EVT_Id
952
-            '%s',// DTT_EVT_start
953
-            '%s',// DTT_EVT_end
954
-            '%d',// DTT_reg_limit
955
-            '%d',// DTT_sold
950
+        $data_types = [
951
+            '%d', // EVT_Id
952
+            '%s', // DTT_EVT_start
953
+            '%s', // DTT_EVT_end
954
+            '%d', // DTT_reg_limit
955
+            '%d', // DTT_sold
956 956
             //          '%d',//DTT_is_primary
957
-            '%d',// DTT_order
958
-            '%d',// DTT_parent
959
-            '%d',// DTT_deleted
957
+            '%d', // DTT_order
958
+            '%d', // DTT_parent
959
+            '%d', // DTT_deleted
960 960
         ];
961
-        $success            = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
962
-        if (! $success) {
961
+        $success = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
962
+        if ( ! $success) {
963 963
             $this->add_error(
964 964
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
965 965
                     $this->_old_start_end_table,
Please login to merge, or discard this patch.
data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_questions.dmsstage.php 2 patches
Indentation   +144 added lines, -144 removed lines patch added patch discarded remove patch
@@ -67,159 +67,159 @@
 block discarded – undo
67 67
 class EE_DMS_4_1_0_questions extends EE_Data_Migration_Script_Stage
68 68
 {
69 69
 
70
-    private string $_option_table;
70
+	private string $_option_table;
71 71
 
72 72
 
73
-    public function __construct()
74
-    {
75
-        global $wpdb;
76
-        $this->_pretty_name  = esc_html__("Questions", "event_espresso");
77
-        $this->_old_table    = $wpdb->prefix . "events_question";
78
-        $this->_new_table    = $wpdb->prefix . "esp_question";
79
-        $this->_option_table = $wpdb->prefix . "esp_question_option";
80
-        parent::__construct();
81
-    }
73
+	public function __construct()
74
+	{
75
+		global $wpdb;
76
+		$this->_pretty_name  = esc_html__("Questions", "event_espresso");
77
+		$this->_old_table    = $wpdb->prefix . "events_question";
78
+		$this->_new_table    = $wpdb->prefix . "esp_question";
79
+		$this->_option_table = $wpdb->prefix . "esp_question_option";
80
+		parent::__construct();
81
+	}
82 82
 
83 83
 
84
-    /**
85
-     * @throws EE_Error
86
-     */
87
-    public function _migration_step($num_items_to_migrate = 50)
88
-    {
89
-        global $wpdb;
90
-        $start_at_record         = $this->count_records_migrated();
91
-        $rows                    = $wpdb->get_results(
92
-            $wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items_to_migrate),
93
-            ARRAY_A
94
-        );
95
-        $items_actually_migrated = 0;
96
-        foreach ($rows as $question_row) {
97
-            $new_id = $this->_insert_new_question($question_row);
98
-            $this->get_migration_script()->set_mapping(
99
-                $this->_old_table,
100
-                $question_row['id'],
101
-                $this->_new_table,
102
-                $new_id
103
-            );
104
-            $items_actually_migrated++;
105
-        }
106
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
107
-            $this->set_completed();
108
-        }
109
-        return $items_actually_migrated;
110
-    }
84
+	/**
85
+	 * @throws EE_Error
86
+	 */
87
+	public function _migration_step($num_items_to_migrate = 50)
88
+	{
89
+		global $wpdb;
90
+		$start_at_record         = $this->count_records_migrated();
91
+		$rows                    = $wpdb->get_results(
92
+			$wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items_to_migrate),
93
+			ARRAY_A
94
+		);
95
+		$items_actually_migrated = 0;
96
+		foreach ($rows as $question_row) {
97
+			$new_id = $this->_insert_new_question($question_row);
98
+			$this->get_migration_script()->set_mapping(
99
+				$this->_old_table,
100
+				$question_row['id'],
101
+				$this->_new_table,
102
+				$new_id
103
+			);
104
+			$items_actually_migrated++;
105
+		}
106
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
107
+			$this->set_completed();
108
+		}
109
+		return $items_actually_migrated;
110
+	}
111 111
 
112 112
 
113
-    public function _count_records_to_migrate()
114
-    {
115
-        global $wpdb;
116
-        return $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
117
-    }
113
+	public function _count_records_to_migrate()
114
+	{
115
+		global $wpdb;
116
+		return $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
117
+	}
118 118
 
119 119
 
120
-    private function _insert_new_question($old_question): int
121
-    {
122
-        global $wpdb;
123
-        // if this pretends to be a 'system' question, check if we already have a
124
-        // system question for that string. If so, pretend THAT new question
125
-        // is what we just inserted
126
-        if ($old_question['system_name']) {
127
-            $id_of_new_system_question = intval(
128
-                $wpdb->get_var(
129
-                    $wpdb->prepare(
130
-                        "SELECT QST_ID FROM $this->_new_table WHERE QST_system = %s",
131
-                        $old_question['system_name']
132
-                    )
133
-                )
134
-            );
135
-            if ($id_of_new_system_question) {
136
-                return $id_of_new_system_question;
137
-            }
138
-            // ok so this must be the first one. Carry on.
139
-        }
120
+	private function _insert_new_question($old_question): int
121
+	{
122
+		global $wpdb;
123
+		// if this pretends to be a 'system' question, check if we already have a
124
+		// system question for that string. If so, pretend THAT new question
125
+		// is what we just inserted
126
+		if ($old_question['system_name']) {
127
+			$id_of_new_system_question = intval(
128
+				$wpdb->get_var(
129
+					$wpdb->prepare(
130
+						"SELECT QST_ID FROM $this->_new_table WHERE QST_system = %s",
131
+						$old_question['system_name']
132
+					)
133
+				)
134
+			);
135
+			if ($id_of_new_system_question) {
136
+				return $id_of_new_system_question;
137
+			}
138
+			// ok so this must be the first one. Carry on.
139
+		}
140 140
 
141
-        $cols_n_values = [
142
-            'QST_display_text'  => stripslashes($old_question['question']),
143
-            'QST_admin_label'   => $old_question['system_name'] ?: sanitize_title($old_question['question']),
144
-            'QST_system'        => (string) $old_question['system_name'],
145
-            'QST_type'          => $old_question['question_type'],
146
-            'QST_required'      => 'Y' == $old_question['required'],
147
-            'QST_required_text' => stripslashes($old_question['required_text']),
148
-            'QST_order'         => $old_question['sequence'],
149
-            'QST_admin_only'    => 'Y' == $old_question['admin_only'],
150
-            'QST_wp_user'       => $old_question['wp_user'],
151
-            'QST_deleted'       => false,
152
-        ];
153
-        $data_types    = [
154
-            '%s',// QST_display_text
155
-            '%s',// QST-admin_label
156
-            '%s',// QST_system
157
-            '%s',// QST_type
158
-            '%d',// QST_required
159
-            '%s',// QST_required_text
160
-            '%d',// QST_order
161
-            '%d',// QST_admin_only
162
-            '%d',// QST_wp_user
163
-            '%d',// QST_deleted
164
-        ];
165
-        $success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
166
-        if (! $success) {
167
-            $this->add_error(
168
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
169
-                    $this->_old_table,
170
-                    $old_question,
171
-                    $this->_new_table,
172
-                    $cols_n_values,
173
-                    $data_types
174
-                )
175
-            );
176
-            return 0;
177
-        }
178
-        $new_id = $wpdb->insert_id;
179
-        // now take care of possibly adding question options
180
-        if (in_array($old_question['question_type'], ['DROPDOWN', 'SINGLE', 'MULTIPLE'])) {
181
-            $options = explode(",", $old_question['response']);
182
-            foreach ($options as $option) {
183
-                $this->_insert_question_option($option, $new_id);
184
-            }
185
-        }
186
-        return $new_id;
187
-    }
141
+		$cols_n_values = [
142
+			'QST_display_text'  => stripslashes($old_question['question']),
143
+			'QST_admin_label'   => $old_question['system_name'] ?: sanitize_title($old_question['question']),
144
+			'QST_system'        => (string) $old_question['system_name'],
145
+			'QST_type'          => $old_question['question_type'],
146
+			'QST_required'      => 'Y' == $old_question['required'],
147
+			'QST_required_text' => stripslashes($old_question['required_text']),
148
+			'QST_order'         => $old_question['sequence'],
149
+			'QST_admin_only'    => 'Y' == $old_question['admin_only'],
150
+			'QST_wp_user'       => $old_question['wp_user'],
151
+			'QST_deleted'       => false,
152
+		];
153
+		$data_types    = [
154
+			'%s',// QST_display_text
155
+			'%s',// QST-admin_label
156
+			'%s',// QST_system
157
+			'%s',// QST_type
158
+			'%d',// QST_required
159
+			'%s',// QST_required_text
160
+			'%d',// QST_order
161
+			'%d',// QST_admin_only
162
+			'%d',// QST_wp_user
163
+			'%d',// QST_deleted
164
+		];
165
+		$success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
166
+		if (! $success) {
167
+			$this->add_error(
168
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
169
+					$this->_old_table,
170
+					$old_question,
171
+					$this->_new_table,
172
+					$cols_n_values,
173
+					$data_types
174
+				)
175
+			);
176
+			return 0;
177
+		}
178
+		$new_id = $wpdb->insert_id;
179
+		// now take care of possibly adding question options
180
+		if (in_array($old_question['question_type'], ['DROPDOWN', 'SINGLE', 'MULTIPLE'])) {
181
+			$options = explode(",", $old_question['response']);
182
+			foreach ($options as $option) {
183
+				$this->_insert_question_option($option, $new_id);
184
+			}
185
+		}
186
+		return $new_id;
187
+	}
188 188
 
189 189
 
190
-    /**
191
-     * Adds a question option to the db
192
-     *
193
-     * @param string $option
194
-     * @param int    $question_id
195
-     * @return void
196
-     * @global wpdb  $wpdb
197
-     */
198
-    private function _insert_question_option(string $option, int $question_id): void
199
-    {
200
-        $option = trim($option);
201
-        global $wpdb;
202
-        $cols_n_values = [
203
-            'QST_ID'      => $question_id,
204
-            'QSO_value'   => $option,
205
-            'QSO_deleted' => false,
206
-        ];
207
-        $data_types    = [
208
-            '%d',// QST_ID
209
-            '%s',// QSO_value
210
-            '%d',// QSO_deleted
211
-        ];
212
-        $success       = $wpdb->insert($this->_option_table, $cols_n_values, $data_types);
213
-        if (! $success) {
214
-            $this->add_error(
215
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
216
-                    $this->_old_table,
217
-                    ['option' => $option, 'new_question_id' => $question_id],
218
-                    $this->_option_table,
219
-                    $cols_n_values,
220
-                    $data_types
221
-                )
222
-            );
223
-        }
224
-    }
190
+	/**
191
+	 * Adds a question option to the db
192
+	 *
193
+	 * @param string $option
194
+	 * @param int    $question_id
195
+	 * @return void
196
+	 * @global wpdb  $wpdb
197
+	 */
198
+	private function _insert_question_option(string $option, int $question_id): void
199
+	{
200
+		$option = trim($option);
201
+		global $wpdb;
202
+		$cols_n_values = [
203
+			'QST_ID'      => $question_id,
204
+			'QSO_value'   => $option,
205
+			'QSO_deleted' => false,
206
+		];
207
+		$data_types    = [
208
+			'%d',// QST_ID
209
+			'%s',// QSO_value
210
+			'%d',// QSO_deleted
211
+		];
212
+		$success       = $wpdb->insert($this->_option_table, $cols_n_values, $data_types);
213
+		if (! $success) {
214
+			$this->add_error(
215
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
216
+					$this->_old_table,
217
+					['option' => $option, 'new_question_id' => $question_id],
218
+					$this->_option_table,
219
+					$cols_n_values,
220
+					$data_types
221
+				)
222
+			);
223
+		}
224
+	}
225 225
 }
Please login to merge, or discard this patch.
Spacing   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -74,9 +74,9 @@  discard block
 block discarded – undo
74 74
     {
75 75
         global $wpdb;
76 76
         $this->_pretty_name  = esc_html__("Questions", "event_espresso");
77
-        $this->_old_table    = $wpdb->prefix . "events_question";
78
-        $this->_new_table    = $wpdb->prefix . "esp_question";
79
-        $this->_option_table = $wpdb->prefix . "esp_question_option";
77
+        $this->_old_table    = $wpdb->prefix."events_question";
78
+        $this->_new_table    = $wpdb->prefix."esp_question";
79
+        $this->_option_table = $wpdb->prefix."esp_question_option";
80 80
         parent::__construct();
81 81
     }
82 82
 
@@ -113,7 +113,7 @@  discard block
 block discarded – undo
113 113
     public function _count_records_to_migrate()
114 114
     {
115 115
         global $wpdb;
116
-        return $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
116
+        return $wpdb->get_var("SELECT COUNT(id) FROM ".$this->_old_table);
117 117
     }
118 118
 
119 119
 
@@ -150,20 +150,20 @@  discard block
 block discarded – undo
150 150
             'QST_wp_user'       => $old_question['wp_user'],
151 151
             'QST_deleted'       => false,
152 152
         ];
153
-        $data_types    = [
154
-            '%s',// QST_display_text
155
-            '%s',// QST-admin_label
156
-            '%s',// QST_system
157
-            '%s',// QST_type
158
-            '%d',// QST_required
159
-            '%s',// QST_required_text
160
-            '%d',// QST_order
161
-            '%d',// QST_admin_only
162
-            '%d',// QST_wp_user
163
-            '%d',// QST_deleted
153
+        $data_types = [
154
+            '%s', // QST_display_text
155
+            '%s', // QST-admin_label
156
+            '%s', // QST_system
157
+            '%s', // QST_type
158
+            '%d', // QST_required
159
+            '%s', // QST_required_text
160
+            '%d', // QST_order
161
+            '%d', // QST_admin_only
162
+            '%d', // QST_wp_user
163
+            '%d', // QST_deleted
164 164
         ];
165
-        $success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
166
-        if (! $success) {
165
+        $success = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
166
+        if ( ! $success) {
167 167
             $this->add_error(
168 168
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
169 169
                     $this->_old_table,
@@ -204,13 +204,13 @@  discard block
 block discarded – undo
204 204
             'QSO_value'   => $option,
205 205
             'QSO_deleted' => false,
206 206
         ];
207
-        $data_types    = [
208
-            '%d',// QST_ID
209
-            '%s',// QSO_value
210
-            '%d',// QSO_deleted
207
+        $data_types = [
208
+            '%d', // QST_ID
209
+            '%s', // QSO_value
210
+            '%d', // QSO_deleted
211 211
         ];
212
-        $success       = $wpdb->insert($this->_option_table, $cols_n_values, $data_types);
213
-        if (! $success) {
212
+        $success = $wpdb->insert($this->_option_table, $cols_n_values, $data_types);
213
+        if ( ! $success) {
214 214
             $this->add_error(
215 215
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
216 216
                     $this->_old_table,
Please login to merge, or discard this patch.