Completed
Branch master (24c3eb)
by
unknown
34:49 queued 28:57
created
4_1_0_stages/EE_DMS_4_1_0_question_group_question.dmsstage.php 1 patch
Indentation   +148 added lines, -148 removed lines patch added patch discarded remove patch
@@ -27,162 +27,162 @@
 block discarded – undo
27 27
  */
28 28
 class EE_DMS_4_1_0_question_group_question extends EE_Data_Migration_Script_Stage
29 29
 {
30
-    public function __construct()
31
-    {
32
-        global $wpdb;
33
-        $this->_old_table   = $wpdb->prefix . "events_qst_group_rel";
34
-        $this->_new_table   = $wpdb->prefix . "esp_question_group_question";
35
-        $this->_pretty_name = esc_html__("Question Group to Question Relations", "event_espresso");
36
-        parent::__construct();
37
-    }
30
+	public function __construct()
31
+	{
32
+		global $wpdb;
33
+		$this->_old_table   = $wpdb->prefix . "events_qst_group_rel";
34
+		$this->_new_table   = $wpdb->prefix . "esp_question_group_question";
35
+		$this->_pretty_name = esc_html__("Question Group to Question Relations", "event_espresso");
36
+		parent::__construct();
37
+	}
38 38
 
39 39
 
40
-    /**
41
-     * @throws EE_Error
42
-     */
43
-    public function _migration_step($num_items_to_migrate = 50)
44
-    {
45
-        global $wpdb;
46
-        $start_at_record         = $this->count_records_migrated();
47
-        $rows                    = $wpdb->get_results(
48
-            $wpdb->prepare(
49
-                "SELECT * FROM $this->_old_table LIMIT %d,%d",
50
-                $start_at_record,
51
-                $num_items_to_migrate
52
-            ),
53
-            ARRAY_A
54
-        );
55
-        $items_actually_migrated = 0;
56
-        foreach ($rows as $question_group_question) {
57
-            $this->_insert_new_question_group_question($question_group_question);
58
-            $items_actually_migrated++;
59
-        }
60
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
61
-            $this->set_completed();
62
-        }
63
-        return $items_actually_migrated;
64
-    }
40
+	/**
41
+	 * @throws EE_Error
42
+	 */
43
+	public function _migration_step($num_items_to_migrate = 50)
44
+	{
45
+		global $wpdb;
46
+		$start_at_record         = $this->count_records_migrated();
47
+		$rows                    = $wpdb->get_results(
48
+			$wpdb->prepare(
49
+				"SELECT * FROM $this->_old_table LIMIT %d,%d",
50
+				$start_at_record,
51
+				$num_items_to_migrate
52
+			),
53
+			ARRAY_A
54
+		);
55
+		$items_actually_migrated = 0;
56
+		foreach ($rows as $question_group_question) {
57
+			$this->_insert_new_question_group_question($question_group_question);
58
+			$items_actually_migrated++;
59
+		}
60
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
61
+			$this->set_completed();
62
+		}
63
+		return $items_actually_migrated;
64
+	}
65 65
 
66 66
 
67
-    public function _count_records_to_migrate()
68
-    {
69
-        global $wpdb;
70
-        return $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
71
-    }
67
+	public function _count_records_to_migrate()
68
+	{
69
+		global $wpdb;
70
+		return $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
71
+	}
72 72
 
73 73
 
74
-    /**
75
-     * Attempts to insert a new question group in the new format given an old one
76
-     *
77
-     * @param array $old_question_group_question
78
-     * @return void
79
-     * @throws EE_Error
80
-     * @global wpdb $wpdb
81
-     */
82
-    private function _insert_new_question_group_question(array $old_question_group_question): void
83
-    {
84
-        global $wpdb;
85
-        $new_question_id       = $this->get_migration_script()->get_mapping_new_pk(
86
-            $wpdb->prefix . "events_question",
87
-            $old_question_group_question['question_id'],
88
-            $wpdb->prefix . "esp_question"
89
-        );
90
-        $new_question_group_id = $this->get_migration_script()->get_mapping_new_pk(
91
-            $wpdb->prefix . "events_qst_group",
92
-            $old_question_group_question['group_id'],
93
-            $wpdb->prefix . "esp_question_group"
94
-        );
95
-        if (! $new_question_id) {
96
-            $this->add_error(
97
-                sprintf(
98
-                    esc_html__("Could not find 4.1 question id for 3.1 question #%d.", "event_espresso"),
99
-                    $old_question_group_question['question_id']
100
-                )
101
-            );
102
-            return;
103
-        }
104
-        if (! $new_question_group_id) {
105
-            $this->add_error(
106
-                sprintf(
107
-                    esc_html__("Could not find 4.1 question group id for 3.1 question group #%d.", "event_espresso"),
108
-                    $old_question_group_question['group_id']
109
-                )
110
-            );
111
-            return;
112
-        }
113
-        // if it's a system question, it needs to be in the right system group. otherwise no dice!
114
-        if (
115
-            ($this->_is_system_question_group($new_question_group_id) ==
116
-             $this->_is_system_question_for_question_group($new_question_id)) ||
117
-            ! $this->_is_system_question_for_question_group($new_question_id)
118
-        ) {
119
-            $cols_n_values = [
120
-                'QSG_ID' => $new_question_group_id,
121
-                'QST_ID' => $new_question_id,
122
-            ];
123
-            $data_types    = [
124
-                '%d',// QSG_ID
125
-                '%d',// QST_ID
126
-            ];
127
-            $success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
128
-            if (! $success) {
129
-                $this->add_error(
130
-                    $this->get_migration_script()->_create_error_message_for_db_insertion(
131
-                        $this->_old_table,
132
-                        $old_question_group_question,
133
-                        $this->_new_table,
134
-                        $cols_n_values,
135
-                        $data_types
136
-                    )
137
-                );
138
-            }
139
-        }
140
-    }
74
+	/**
75
+	 * Attempts to insert a new question group in the new format given an old one
76
+	 *
77
+	 * @param array $old_question_group_question
78
+	 * @return void
79
+	 * @throws EE_Error
80
+	 * @global wpdb $wpdb
81
+	 */
82
+	private function _insert_new_question_group_question(array $old_question_group_question): void
83
+	{
84
+		global $wpdb;
85
+		$new_question_id       = $this->get_migration_script()->get_mapping_new_pk(
86
+			$wpdb->prefix . "events_question",
87
+			$old_question_group_question['question_id'],
88
+			$wpdb->prefix . "esp_question"
89
+		);
90
+		$new_question_group_id = $this->get_migration_script()->get_mapping_new_pk(
91
+			$wpdb->prefix . "events_qst_group",
92
+			$old_question_group_question['group_id'],
93
+			$wpdb->prefix . "esp_question_group"
94
+		);
95
+		if (! $new_question_id) {
96
+			$this->add_error(
97
+				sprintf(
98
+					esc_html__("Could not find 4.1 question id for 3.1 question #%d.", "event_espresso"),
99
+					$old_question_group_question['question_id']
100
+				)
101
+			);
102
+			return;
103
+		}
104
+		if (! $new_question_group_id) {
105
+			$this->add_error(
106
+				sprintf(
107
+					esc_html__("Could not find 4.1 question group id for 3.1 question group #%d.", "event_espresso"),
108
+					$old_question_group_question['group_id']
109
+				)
110
+			);
111
+			return;
112
+		}
113
+		// if it's a system question, it needs to be in the right system group. otherwise no dice!
114
+		if (
115
+			($this->_is_system_question_group($new_question_group_id) ==
116
+			 $this->_is_system_question_for_question_group($new_question_id)) ||
117
+			! $this->_is_system_question_for_question_group($new_question_id)
118
+		) {
119
+			$cols_n_values = [
120
+				'QSG_ID' => $new_question_group_id,
121
+				'QST_ID' => $new_question_id,
122
+			];
123
+			$data_types    = [
124
+				'%d',// QSG_ID
125
+				'%d',// QST_ID
126
+			];
127
+			$success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
128
+			if (! $success) {
129
+				$this->add_error(
130
+					$this->get_migration_script()->_create_error_message_for_db_insertion(
131
+						$this->_old_table,
132
+						$old_question_group_question,
133
+						$this->_new_table,
134
+						$cols_n_values,
135
+						$data_types
136
+					)
137
+				);
138
+			}
139
+		}
140
+	}
141 141
 
142 142
 
143
-    /**
144
-     * If this question is a system question, returns the QSG_system number that
145
-     * indicates the question group its permitted in.
146
-     *
147
-     * @param int   $new_question_id
148
-     * @return int
149
-     * @global wpdb $wpdb
150
-     */
151
-    private function _is_system_question_for_question_group(int $new_question_id): int
152
-    {
153
-        global $wpdb;
154
-        $system_id = $wpdb->get_var(
155
-            $wpdb->prepare(
156
-                "SELECT QST_system FROM " . $wpdb->prefix . "esp_question WHERE QST_ID=%d",
157
-                $new_question_id
158
-            )
159
-        );
160
-        if (in_array($system_id, ['fname', 'lname', 'email'])) {
161
-            return 1;
162
-        }
163
-        if ($system_id != '' && $system_id) {
164
-            return 2;
165
-        }
166
-        return 0;
167
-    }
143
+	/**
144
+	 * If this question is a system question, returns the QSG_system number that
145
+	 * indicates the question group its permitted in.
146
+	 *
147
+	 * @param int   $new_question_id
148
+	 * @return int
149
+	 * @global wpdb $wpdb
150
+	 */
151
+	private function _is_system_question_for_question_group(int $new_question_id): int
152
+	{
153
+		global $wpdb;
154
+		$system_id = $wpdb->get_var(
155
+			$wpdb->prepare(
156
+				"SELECT QST_system FROM " . $wpdb->prefix . "esp_question WHERE QST_ID=%d",
157
+				$new_question_id
158
+			)
159
+		);
160
+		if (in_array($system_id, ['fname', 'lname', 'email'])) {
161
+			return 1;
162
+		}
163
+		if ($system_id != '' && $system_id) {
164
+			return 2;
165
+		}
166
+		return 0;
167
+	}
168 168
 
169 169
 
170
-    /**
171
-     * Returns the question group's QSG_system value (1 meaning personal info, 2
172
-     * being address info, and 0 being neither)
173
-     *
174
-     * @param int   $new_question_group_id
175
-     * @return int
176
-     * @global wpdb $wpdb
177
-     */
178
-    private function _is_system_question_group(int $new_question_group_id): int
179
-    {
180
-        global $wpdb;
181
-        return (int) $wpdb->get_var(
182
-            $wpdb->prepare(
183
-                "SELECT QSG_system FROM " . $wpdb->prefix . "esp_question_group WHERE QSG_ID=%d",
184
-                $new_question_group_id
185
-            )
186
-        );
187
-    }
170
+	/**
171
+	 * Returns the question group's QSG_system value (1 meaning personal info, 2
172
+	 * being address info, and 0 being neither)
173
+	 *
174
+	 * @param int   $new_question_group_id
175
+	 * @return int
176
+	 * @global wpdb $wpdb
177
+	 */
178
+	private function _is_system_question_group(int $new_question_group_id): int
179
+	{
180
+		global $wpdb;
181
+		return (int) $wpdb->get_var(
182
+			$wpdb->prepare(
183
+				"SELECT QSG_system FROM " . $wpdb->prefix . "esp_question_group WHERE QSG_ID=%d",
184
+				$new_question_group_id
185
+			)
186
+		);
187
+	}
188 188
 }
Please login to merge, or discard this patch.
core/data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_gateways.dmsstage.php 1 patch
Indentation   +231 added lines, -231 removed lines patch added patch discarded remove patch
@@ -14,256 +14,256 @@
 block discarded – undo
14 14
  */
15 15
 class EE_DMS_4_1_0_gateways extends EE_Data_Migration_Script_Stage
16 16
 {
17
-    private bool $_converted_active_gateways = false;
17
+	private bool $_converted_active_gateways = false;
18 18
 
19
-    protected array $_gateways_we_know_how_to_migrate = [
20
-        'aim'        => 'Aim',
21
-        'bank'       => 'Bank',
22
-        'check'      => 'Check',
23
-        'invoice'    => 'Invoice',
24
-        'paypal'     => 'Paypal_Standard',
25
-        'paypal_pro' => 'Paypal_Pro',
19
+	protected array $_gateways_we_know_how_to_migrate = [
20
+		'aim'        => 'Aim',
21
+		'bank'       => 'Bank',
22
+		'check'      => 'Check',
23
+		'invoice'    => 'Invoice',
24
+		'paypal'     => 'Paypal_Standard',
25
+		'paypal_pro' => 'Paypal_Pro',
26 26
 
27
-    ];
27
+	];
28 28
 
29
-    protected array $_gateways_we_leave_alone = [
30
-        '2checkout'        => '2checkout',
31
-        'anz'              => 'Anz',
32
-        'atos'             => 'Atos',
33
-        'authnet'          => 'Authnet',
34
-        'beanstream'       => 'Beanstream',
35
-        'evertec'          => 'Evertec',
36
-        'eway'             => 'Eway',
37
-        'eway_rapid3'      => 'Eway_Rapid3',
38
-        'exact'            => 'Exact',
39
-        'firstdata'        => 'Firstdata',
40
-        'firstdat_e4'      => 'Firstdata_E4',
41
-        'ideal'            => 'Ideal',
42
-        'infusion_payment' => 'InfusionSoft',
43
-        'luottokunta'      => 'Luottokunta',
44
-        'megasoft'         => 'Megasoft',
45
-        'moneris_hpp'      => 'Moneris_HPP',
46
-        'mwarrior'         => 'Mwarrior',
47
-        'nab'              => 'NAB',
48
-        'paychoice'        => 'Paychoice',
49
-        'paytrace'         => 'Paytrace',
50
-        'psigate'          => 'Psigate',
51
-        'purchase_order'   => 'Purchase_Order',
52
-        'qbms'             => 'QBMS',
53
-        'quickpay'         => 'Quickpay',
54
-        'realauth'         => 'Realauth',
55
-        'securepay_aus'    => 'Securepay_Aus',
56
-        'stripe'           => 'Stripe',
57
-        'usaepay_offsite'  => 'USAePay_Offsite',
58
-        'usaepay_onsite'   => 'USAePay_Onsite',
59
-        'wepay'            => 'Wepay',
60
-        'worldpay'         => 'Worldpay',
61
-    ];
29
+	protected array $_gateways_we_leave_alone = [
30
+		'2checkout'        => '2checkout',
31
+		'anz'              => 'Anz',
32
+		'atos'             => 'Atos',
33
+		'authnet'          => 'Authnet',
34
+		'beanstream'       => 'Beanstream',
35
+		'evertec'          => 'Evertec',
36
+		'eway'             => 'Eway',
37
+		'eway_rapid3'      => 'Eway_Rapid3',
38
+		'exact'            => 'Exact',
39
+		'firstdata'        => 'Firstdata',
40
+		'firstdat_e4'      => 'Firstdata_E4',
41
+		'ideal'            => 'Ideal',
42
+		'infusion_payment' => 'InfusionSoft',
43
+		'luottokunta'      => 'Luottokunta',
44
+		'megasoft'         => 'Megasoft',
45
+		'moneris_hpp'      => 'Moneris_HPP',
46
+		'mwarrior'         => 'Mwarrior',
47
+		'nab'              => 'NAB',
48
+		'paychoice'        => 'Paychoice',
49
+		'paytrace'         => 'Paytrace',
50
+		'psigate'          => 'Psigate',
51
+		'purchase_order'   => 'Purchase_Order',
52
+		'qbms'             => 'QBMS',
53
+		'quickpay'         => 'Quickpay',
54
+		'realauth'         => 'Realauth',
55
+		'securepay_aus'    => 'Securepay_Aus',
56
+		'stripe'           => 'Stripe',
57
+		'usaepay_offsite'  => 'USAePay_Offsite',
58
+		'usaepay_onsite'   => 'USAePay_Onsite',
59
+		'wepay'            => 'Wepay',
60
+		'worldpay'         => 'Worldpay',
61
+	];
62 62
 
63 63
 
64
-    public function __construct()
65
-    {
66
-        $this->_pretty_name = esc_html__("Gateways", "event_espresso");
67
-        parent::__construct();
68
-    }
64
+	public function __construct()
65
+	{
66
+		$this->_pretty_name = esc_html__("Gateways", "event_espresso");
67
+		parent::__construct();
68
+	}
69 69
 
70 70
 
71
-    public function _migration_step($num_items_to_migrate = 50)
72
-    {
73
-        // loads existing EE_Config from DB, if it exists
74
-        $new_gateway_config_obj  = EE_Config::instance()->gateway;
75
-        $items_actually_migrated = 0;
76
-        // convert settings
77
-        $gateways_to_deal_with = array_merge(
78
-            $this->_gateways_we_know_how_to_migrate,
79
-            $this->_gateways_we_leave_alone
80
-        );
81
-        // just do a part of them on this request
82
-        $gateways_to_deal_with = array_slice(
83
-            $gateways_to_deal_with,
84
-            $this->count_records_migrated(),
85
-            $num_items_to_migrate
86
-        );// $this->_gateways_we_know_how_to_migrate;
87
-        foreach ($gateways_to_deal_with as $old_gateway_slug => $new_gateway_slug) {
88
-            $old_gateway_wp_option_name = $this->_get_old_gateway_option_name($new_gateway_slug);
89
-            if (isset($this->_gateways_we_know_how_to_migrate[ $old_gateway_slug ])) {
90
-                // determine the old option's name
91
-                $old_gateway_settings = $this->_get_old_gateway_option($new_gateway_slug);
92
-                if (! $old_gateway_settings) {
93
-                    // no settings existed for this gateway anyways... weird...
94
-                    $items_actually_migrated++;
95
-                    continue;
96
-                }
97
-                // now prepare the settings to make sure they're in the 4.1 format
98
-                $new_gateway_settings = $this->_convert_gateway_settings(
99
-                    (array) $old_gateway_settings,
100
-                    $new_gateway_slug
101
-                );
71
+	public function _migration_step($num_items_to_migrate = 50)
72
+	{
73
+		// loads existing EE_Config from DB, if it exists
74
+		$new_gateway_config_obj  = EE_Config::instance()->gateway;
75
+		$items_actually_migrated = 0;
76
+		// convert settings
77
+		$gateways_to_deal_with = array_merge(
78
+			$this->_gateways_we_know_how_to_migrate,
79
+			$this->_gateways_we_leave_alone
80
+		);
81
+		// just do a part of them on this request
82
+		$gateways_to_deal_with = array_slice(
83
+			$gateways_to_deal_with,
84
+			$this->count_records_migrated(),
85
+			$num_items_to_migrate
86
+		);// $this->_gateways_we_know_how_to_migrate;
87
+		foreach ($gateways_to_deal_with as $old_gateway_slug => $new_gateway_slug) {
88
+			$old_gateway_wp_option_name = $this->_get_old_gateway_option_name($new_gateway_slug);
89
+			if (isset($this->_gateways_we_know_how_to_migrate[ $old_gateway_slug ])) {
90
+				// determine the old option's name
91
+				$old_gateway_settings = $this->_get_old_gateway_option($new_gateway_slug);
92
+				if (! $old_gateway_settings) {
93
+					// no settings existed for this gateway anyways... weird...
94
+					$items_actually_migrated++;
95
+					continue;
96
+				}
97
+				// now prepare the settings to make sure they're in the 4.1 format
98
+				$new_gateway_settings = $this->_convert_gateway_settings(
99
+					(array) $old_gateway_settings,
100
+					$new_gateway_slug
101
+				);
102 102
 
103
-                $new_gateway_config_obj->payment_settings[ $new_gateway_slug ] = $new_gateway_settings;
104
-                // and when we're done, remove the old option. Sometimes we'd prefer to do this in a different stage, but
105
-                // I think it's ok to do right away this time (we wont need gateway settings elsewhere)
106
-                delete_option($old_gateway_wp_option_name);
107
-            } else {// it must be one of the ones we mostly leave alone
108
-                global $wpdb;
109
-                // yeah we could do this all in one query... and if you're reading this and would like to, go ahead. Although you'll
110
-                // only be saving users 50 milliseconds the one time this runs...
111
-                $wpdb->query(
112
-                    $wpdb->prepare(
113
-                        "UPDATE " . $wpdb->options . " SET autoload='no' WHERE option_name=%s",
114
-                        $old_gateway_wp_option_name
115
-                    )
116
-                );
117
-            }
103
+				$new_gateway_config_obj->payment_settings[ $new_gateway_slug ] = $new_gateway_settings;
104
+				// and when we're done, remove the old option. Sometimes we'd prefer to do this in a different stage, but
105
+				// I think it's ok to do right away this time (we wont need gateway settings elsewhere)
106
+				delete_option($old_gateway_wp_option_name);
107
+			} else {// it must be one of the ones we mostly leave alone
108
+				global $wpdb;
109
+				// yeah we could do this all in one query... and if you're reading this and would like to, go ahead. Although you'll
110
+				// only be saving users 50 milliseconds the one time this runs...
111
+				$wpdb->query(
112
+					$wpdb->prepare(
113
+						"UPDATE " . $wpdb->options . " SET autoload='no' WHERE option_name=%s",
114
+						$old_gateway_wp_option_name
115
+					)
116
+				);
117
+			}
118 118
 
119
-            $items_actually_migrated++;
120
-        }
121
-        // if we can keep going, and it hasn't been done yet, convert active gateways
122
-        if ($items_actually_migrated < $num_items_to_migrate && ! $this->_converted_active_gateways) {
123
-            $this->_convert_active_gateways();
124
-            $this->_converted_active_gateways = true;
125
-            $items_actually_migrated++;
126
-        }
119
+			$items_actually_migrated++;
120
+		}
121
+		// if we can keep going, and it hasn't been done yet, convert active gateways
122
+		if ($items_actually_migrated < $num_items_to_migrate && ! $this->_converted_active_gateways) {
123
+			$this->_convert_active_gateways();
124
+			$this->_converted_active_gateways = true;
125
+			$items_actually_migrated++;
126
+		}
127 127
 
128
-        EE_Config::instance()->update_espresso_config(false, false);
129
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
130
-            $this->set_completed();
131
-        }
132
-        return $items_actually_migrated;
133
-    }
128
+		EE_Config::instance()->update_espresso_config(false, false);
129
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
130
+			$this->set_completed();
131
+		}
132
+		return $items_actually_migrated;
133
+	}
134 134
 
135 135
 
136
-    public function _count_records_to_migrate(): int
137
-    {
138
-        $count_of_gateways_to_convert     = (int) count($this->_gateways_we_know_how_to_migrate);
139
-        $step_of_setting_active_gateways  = 1;
140
-        $count_of_gateways_to_leave_alone = (int) count($this->_gateways_we_leave_alone);
141
-        //  $button_images_to_update =
142
-        return $count_of_gateways_to_convert + $step_of_setting_active_gateways + $count_of_gateways_to_leave_alone;
143
-    }
136
+	public function _count_records_to_migrate(): int
137
+	{
138
+		$count_of_gateways_to_convert     = (int) count($this->_gateways_we_know_how_to_migrate);
139
+		$step_of_setting_active_gateways  = 1;
140
+		$count_of_gateways_to_leave_alone = (int) count($this->_gateways_we_leave_alone);
141
+		//  $button_images_to_update =
142
+		return $count_of_gateways_to_convert + $step_of_setting_active_gateways + $count_of_gateways_to_leave_alone;
143
+	}
144 144
 
145 145
 
146
-    /**
147
-     * Takes the old array of 3.1 gateway settings for this gateway and converts it
148
-     * into an array with all the 4.1 gateway setting array keys (often the keys were
149
-     * changed from 3.1 to 4.1)
150
-     *
151
-     * @param array  $old_gateway_settings
152
-     * @param string $new_gateway_slug
153
-     * @return array
154
-     */
155
-    private function _convert_gateway_settings(array $old_gateway_settings, string $new_gateway_slug): array
156
-    {
157
-        $new_gateway_settings = $old_gateway_settings;
158
-        switch ($new_gateway_slug) {
159
-            case 'Bank':
160
-                $new_gateway_settings['account_number'] = $old_gateway_settings['bank_account'];
161
-                $new_gateway_settings['page_title']     = $old_gateway_settings['bank_title'];
162
-                break;
163
-            case 'Invoice':
164
-                $new_gateway_settings['invoice_logo_url'] = $old_gateway_settings['image_url'];
165
-                // Dont' migrate the old invoice payee address. It was usually given dummy data. And in EE4
166
-                // it gets used in the receipts which are available to everyone. So usually it's best to not
167
-                // migrate this.
168
-                unset($new_gateway_settings['payment_address']);
169
-                break;
170
-            case 'Paypal_Pro':
171
-                $new_gateway_settings['email']        = $old_gateway_settings['paypal_pro_email'];
172
-                $new_gateway_settings['username']     = $old_gateway_settings['paypal_api_username'];
173
-                $new_gateway_settings['password']     = $old_gateway_settings['paypal_api_password'];
174
-                $new_gateway_settings['signature']    = $old_gateway_settings['paypal_api_signature'];
175
-                $new_gateway_settings['credit_cards'] = explode(",", $old_gateway_settings['paypal_api_credit_cards']);
176
-                $new_gateway_settings['use_sandbox']  = $old_gateway_settings['paypal_pro_use_sandbox'];
177
-                break;
178
-        }
179
-        return $new_gateway_settings;
180
-    }
146
+	/**
147
+	 * Takes the old array of 3.1 gateway settings for this gateway and converts it
148
+	 * into an array with all the 4.1 gateway setting array keys (often the keys were
149
+	 * changed from 3.1 to 4.1)
150
+	 *
151
+	 * @param array  $old_gateway_settings
152
+	 * @param string $new_gateway_slug
153
+	 * @return array
154
+	 */
155
+	private function _convert_gateway_settings(array $old_gateway_settings, string $new_gateway_slug): array
156
+	{
157
+		$new_gateway_settings = $old_gateway_settings;
158
+		switch ($new_gateway_slug) {
159
+			case 'Bank':
160
+				$new_gateway_settings['account_number'] = $old_gateway_settings['bank_account'];
161
+				$new_gateway_settings['page_title']     = $old_gateway_settings['bank_title'];
162
+				break;
163
+			case 'Invoice':
164
+				$new_gateway_settings['invoice_logo_url'] = $old_gateway_settings['image_url'];
165
+				// Dont' migrate the old invoice payee address. It was usually given dummy data. And in EE4
166
+				// it gets used in the receipts which are available to everyone. So usually it's best to not
167
+				// migrate this.
168
+				unset($new_gateway_settings['payment_address']);
169
+				break;
170
+			case 'Paypal_Pro':
171
+				$new_gateway_settings['email']        = $old_gateway_settings['paypal_pro_email'];
172
+				$new_gateway_settings['username']     = $old_gateway_settings['paypal_api_username'];
173
+				$new_gateway_settings['password']     = $old_gateway_settings['paypal_api_password'];
174
+				$new_gateway_settings['signature']    = $old_gateway_settings['paypal_api_signature'];
175
+				$new_gateway_settings['credit_cards'] = explode(",", $old_gateway_settings['paypal_api_credit_cards']);
176
+				$new_gateway_settings['use_sandbox']  = $old_gateway_settings['paypal_pro_use_sandbox'];
177
+				break;
178
+		}
179
+		return $new_gateway_settings;
180
+	}
181 181
 
182 182
 
183
-    /**
184
-     * Returns the 3.1 gateway settings for the given 4.1 gateway
185
-     *
186
-     * @param string $new_gateway_slug
187
-     * @return null|array
188
-     */
189
-    private function _get_old_gateway_option(string $new_gateway_slug): ?array
190
-    {
191
-        return get_option(
192
-            $this->_get_old_gateway_option_name($new_gateway_slug),
193
-            null
194
-        );
195
-    }
183
+	/**
184
+	 * Returns the 3.1 gateway settings for the given 4.1 gateway
185
+	 *
186
+	 * @param string $new_gateway_slug
187
+	 * @return null|array
188
+	 */
189
+	private function _get_old_gateway_option(string $new_gateway_slug): ?array
190
+	{
191
+		return get_option(
192
+			$this->_get_old_gateway_option_name($new_gateway_slug),
193
+			null
194
+		);
195
+	}
196 196
 
197 197
 
198
-    /**
199
-     * Figures out the correct 3.1 gateway settings option name for the given 4.1 gateway
200
-     *
201
-     * @param string $new_gateway_slug
202
-     * @return string
203
-     */
204
-    private function _get_old_gateway_option_name(string $new_gateway_slug): string
205
-    {
206
-        $new_gateway_slugs_to_new = array_flip(
207
-            array_merge(
208
-                $this->_gateways_we_know_how_to_migrate,
209
-                $this->_gateways_we_leave_alone
210
-            )
211
-        );
212
-        $old_gateway_slug         = $new_gateway_slugs_to_new[ $new_gateway_slug ];
213
-        $normal_option_prefix     = 'event_espresso_';
214
-        $normal_option_postfix    = '_settings';
215
-        switch ($new_gateway_slug) {
216
-            case 'Bank':
217
-                $option_name = $normal_option_prefix . 'bank_deposit' . $normal_option_postfix;
218
-                break;
219
-            case 'Aim':
220
-                $option_name = $normal_option_prefix . 'authnet_aim' . $normal_option_postfix;
221
-                break;
222
-            case 'Check':
223
-                $option_name = $normal_option_prefix . 'check_payment' . $normal_option_postfix;
224
-                break;
225
-            case 'Ideal':
226
-                $option_name = $normal_option_prefix . 'ideal_mollie' . $normal_option_postfix;
227
-                break;
228
-            case 'Invoice':
229
-                $option_name = $normal_option_prefix . 'invoice_payment' . $normal_option_postfix;
230
-                break;
231
-            case 'Purchase_Order':
232
-                $option_name = $normal_option_prefix . 'purchase_order_payment' . $normal_option_postfix;
233
-                break;
234
-            case 'USAePay_Offsite':
235
-                $option_name = 'espresso_usaepay_offsite' . $normal_option_postfix;
236
-                break;
237
-            case 'USAePay_Onsite':
238
-                $option_name = 'espresso_usaepay_onsite' . $normal_option_postfix;
239
-                break;
240
-            default:
241
-                $option_name = apply_filters(
242
-                    'FHEE__EE_DMS_4_1_0_gateways__get_old_gateway_option',
243
-                    $normal_option_prefix . $old_gateway_slug . $normal_option_postfix
244
-                );
245
-        }
246
-        return $option_name;
247
-    }
198
+	/**
199
+	 * Figures out the correct 3.1 gateway settings option name for the given 4.1 gateway
200
+	 *
201
+	 * @param string $new_gateway_slug
202
+	 * @return string
203
+	 */
204
+	private function _get_old_gateway_option_name(string $new_gateway_slug): string
205
+	{
206
+		$new_gateway_slugs_to_new = array_flip(
207
+			array_merge(
208
+				$this->_gateways_we_know_how_to_migrate,
209
+				$this->_gateways_we_leave_alone
210
+			)
211
+		);
212
+		$old_gateway_slug         = $new_gateway_slugs_to_new[ $new_gateway_slug ];
213
+		$normal_option_prefix     = 'event_espresso_';
214
+		$normal_option_postfix    = '_settings';
215
+		switch ($new_gateway_slug) {
216
+			case 'Bank':
217
+				$option_name = $normal_option_prefix . 'bank_deposit' . $normal_option_postfix;
218
+				break;
219
+			case 'Aim':
220
+				$option_name = $normal_option_prefix . 'authnet_aim' . $normal_option_postfix;
221
+				break;
222
+			case 'Check':
223
+				$option_name = $normal_option_prefix . 'check_payment' . $normal_option_postfix;
224
+				break;
225
+			case 'Ideal':
226
+				$option_name = $normal_option_prefix . 'ideal_mollie' . $normal_option_postfix;
227
+				break;
228
+			case 'Invoice':
229
+				$option_name = $normal_option_prefix . 'invoice_payment' . $normal_option_postfix;
230
+				break;
231
+			case 'Purchase_Order':
232
+				$option_name = $normal_option_prefix . 'purchase_order_payment' . $normal_option_postfix;
233
+				break;
234
+			case 'USAePay_Offsite':
235
+				$option_name = 'espresso_usaepay_offsite' . $normal_option_postfix;
236
+				break;
237
+			case 'USAePay_Onsite':
238
+				$option_name = 'espresso_usaepay_onsite' . $normal_option_postfix;
239
+				break;
240
+			default:
241
+				$option_name = apply_filters(
242
+					'FHEE__EE_DMS_4_1_0_gateways__get_old_gateway_option',
243
+					$normal_option_prefix . $old_gateway_slug . $normal_option_postfix
244
+				);
245
+		}
246
+		return $option_name;
247
+	}
248 248
 
249 249
 
250
-    private function _convert_active_gateways()
251
-    {
252
-        // just does it all one big swoop
253
-        $old_active_gateways = get_option('event_espresso_active_gateways');
254
-        $new_active_gateways = EE_Config::instance()->gateway->active_gateways;
255
-        foreach ($old_active_gateways as $old_gateway_slug => $filepath) {
256
-            if (! isset($this->_gateways_we_know_how_to_migrate[ $old_gateway_slug ])) {
257
-                $this->add_error(
258
-                    sprintf(esc_html__("The %s gateway does not exist in EE 4.1", "event_espresso"), $old_gateway_slug)
259
-                );
260
-                continue;
261
-            }
262
-            $new_gateway_slug = $this->_gateways_we_know_how_to_migrate[ $old_gateway_slug ];
250
+	private function _convert_active_gateways()
251
+	{
252
+		// just does it all one big swoop
253
+		$old_active_gateways = get_option('event_espresso_active_gateways');
254
+		$new_active_gateways = EE_Config::instance()->gateway->active_gateways;
255
+		foreach ($old_active_gateways as $old_gateway_slug => $filepath) {
256
+			if (! isset($this->_gateways_we_know_how_to_migrate[ $old_gateway_slug ])) {
257
+				$this->add_error(
258
+					sprintf(esc_html__("The %s gateway does not exist in EE 4.1", "event_espresso"), $old_gateway_slug)
259
+				);
260
+				continue;
261
+			}
262
+			$new_gateway_slug = $this->_gateways_we_know_how_to_migrate[ $old_gateway_slug ];
263 263
 
264
-            // in the new format we can also upload gateways to the uploads directory and
265
-            $new_active_gateways[ $new_gateway_slug ] = false;
266
-        }
267
-        EE_Config::instance()->gateway->active_gateways = $new_active_gateways;
268
-    }
264
+			// in the new format we can also upload gateways to the uploads directory and
265
+			$new_active_gateways[ $new_gateway_slug ] = false;
266
+		}
267
+		EE_Config::instance()->gateway->active_gateways = $new_active_gateways;
268
+	}
269 269
 }
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   +817 added lines, -817 removed lines patch added patch discarded remove patch
@@ -176,825 +176,825 @@
 block discarded – undo
176 176
  */
177 177
 class EE_DMS_4_1_0_events extends EE_Data_Migration_Script_Stage
178 178
 {
179
-    private string $_old_start_end_table;
180
-
181
-    private string $_new_meta_table;
182
-
183
-    private string $_new_datetime_table;
184
-
185
-
186
-    /**
187
-     * Just initializes the status of the migration
188
-     */
189
-    public function __construct()
190
-    {
191
-        global $wpdb;
192
-        $this->_old_table           = $wpdb->prefix . "events_detail";
193
-        $this->_old_start_end_table = $wpdb->prefix . "events_start_end";
194
-        $this->_new_table           = $wpdb->prefix . "posts";
195
-        $this->_new_meta_table      = $wpdb->prefix . "esp_event_meta";
196
-        $this->_new_datetime_table  = $wpdb->prefix . "esp_datetime";
197
-        $this->_pretty_name         = esc_html__("Events", "event_espresso");
198
-        parent::__construct();
199
-    }
200
-
201
-
202
-    /**
203
-     * Counts the records to migrate; the public version may cache it
204
-     *
205
-     * @return int
206
-     */
207
-    public function _count_records_to_migrate(): int
208
-    {
209
-        global $wpdb;
210
-        return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $this->_old_table . ' WHERE event_status !="D"');
211
-    }
212
-
213
-
214
-    /**
215
-     * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
216
-     * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
217
-     * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
218
-     * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
219
-     * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
220
-     * very least we MUST report/return 50 items migrated)
221
-     *
222
-     * @param int $num_items_to_migrate
223
-     * @return int number of items ACTUALLY migrated
224
-     * @throws EE_Error
225
-     */
226
-    protected function _migration_step($num_items_to_migrate = 50)
227
-    {
228
-        global $wpdb;
229
-        // because the migration of each event can be a LOT more work, make each step smaller
230
-        $num_items_to_migrate     = max(1, $num_items_to_migrate / 5);
231
-        $events                   =
232
-            $wpdb->get_results(
233
-                $wpdb->prepare(
234
-                    "SELECT * FROM $this->_old_table WHERE event_status!='D' LIMIT %d,%d",
235
-                    $this->count_records_migrated(),
236
-                    $num_items_to_migrate
237
-                ),
238
-                ARRAY_A
239
-            );
240
-        $items_migrated_this_step = 0;
241
-
242
-        foreach ($events as $event_row) {
243
-            $guid = null;
244
-            // insert new 4.1 Attendee object using $wpdb
245
-            $post_id = $this->_insert_cpt($event_row);
246
-            if ($post_id) {
247
-                $this->get_migration_script()->set_mapping(
248
-                    $this->_old_table,
249
-                    $event_row['id'],
250
-                    $this->_new_table,
251
-                    $post_id
252
-                );
253
-                $meta_id = $this->_insert_event_meta($event_row, $post_id);
254
-                if ($meta_id) {
255
-                    $this->get_migration_script()->set_mapping(
256
-                        $this->_old_table,
257
-                        $event_row['id'],
258
-                        $this->_new_meta_table,
259
-                        $meta_id
260
-                    );
261
-                }
262
-                $this->_convert_start_end_times($event_row, $post_id);
263
-                $event_meta = maybe_unserialize($event_row['event_meta']);
264
-                $guid       = $event_meta['event_thumbnail_url'] ?? null;
265
-                if ($guid) {
266
-                    $this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
267
-                        $guid,
268
-                        $post_id,
269
-                        $this
270
-                    );
271
-                }
272
-                // maybe create a venue from info on the event?
273
-                $new_venue_id = $this->_maybe_create_venue($event_row);
274
-                if ($new_venue_id) {
275
-                    $this->_insert_new_venue_to_event($post_id, $new_venue_id);
276
-                }
277
-                $this->_add_post_metas($event_row, $post_id);
278
-            }
279
-            $items_migrated_this_step++;
280
-            if ($guid) {
281
-                // if we had to check for an image attachment
282
-                // then let's call it a day (avoid timing out, because this took a long time)
283
-                break;
284
-            }
285
-        }
286
-        if ($this->count_records_migrated() + $items_migrated_this_step >= $this->count_records_to_migrate()) {
287
-            $this->set_status(EE_Data_Migration_Manager::status_completed);
288
-        }
289
-        return $items_migrated_this_step;
290
-    }
291
-
292
-
293
-    /**
294
-     * Stores any extra 3.1 "event_meta" column things as post meta
295
-     *
296
-     * @param array $old_event
297
-     * @param int   $post_id
298
-     * @return void
299
-     */
300
-    private function _add_post_metas(array $old_event, int $post_id)
301
-    {
302
-        $event_meta = maybe_unserialize($old_event['event_meta']);
303
-        if (! $event_meta || ! is_array($event_meta)) {
304
-            return;
305
-        }
306
-        unset($event_meta['date_submitted']);              // factored into CPT
307
-        unset($event_meta['additional_attendee_reg_info']);// factored into event meta table
308
-        unset($event_meta['default_payment_status']);      // dido
309
-        unset($event_meta['event_thumbnail_url']);         // used to find post featured image
310
-        foreach ($event_meta as $meta_key => $meta_value) {
311
-            if ($meta_key) {// if the meta key is just an empty string, ignore it
312
-                $success = add_post_meta($post_id, $meta_key, $meta_value, true);
313
-                if (! $success) {
314
-                    $this->add_error(
315
-                        sprintf(
316
-                            esc_html__(
317
-                                "Could not add post meta for CPT with ID #%d. Meta key: '%s',meta value:'%d' for 3.1 event: %s",
318
-                                "event_espresso"
319
-                            ),
320
-                            $post_id,
321
-                            $meta_key,
322
-                            $meta_value,
323
-                            implode(",", $old_event)
324
-                        )
325
-                    );
326
-                }
327
-            }
328
-        }
329
-        if ($old_event['alt_email']) {
330
-            add_post_meta($post_id, 'alt_email', $old_event['alt_email']);
331
-        }
332
-        if ($old_event['recurrence_id']) {
333
-            add_post_meta($post_id, 'recurrence_id', $old_event['recurrence_id']);
334
-        }
335
-    }
336
-
337
-
338
-    /**
339
-     * Finds a unique slug for this event, given its name (we could have simply used
340
-     * the old unique_identifier column, but it added a long string of seemingly random characters onto the end
341
-     * and really wasn't that pretty for a slug, so we decided we'd make our own slug again)
342
-     *
343
-     * @param string $event_name      (the name of the event for reading by humans)
344
-     * @param string $old_identifier  the old EE3 identifier (a long unique string)
345
-     * @param string $new_post_status a post status
346
-     * @return string
347
-     */
348
-    private function _find_unique_slug(
349
-        string $event_name,
350
-        string $old_identifier = '',
351
-        string $new_post_status = 'publish'
352
-    ): string {
353
-        $original_name = $event_name ? sanitize_title($event_name) : $old_identifier;
354
-        return wp_unique_post_slug($original_name, 0, $new_post_status, EspressoPostType::EVENTS, 0);
355
-    }
356
-
357
-
358
-    /**
359
-     * returns whether there is a post that has this same slug (post_title)
360
-     *
361
-     * @param string $slug
362
-     * @return boolean
363
-     * @global wpdb  $wpdb
364
-     */
365
-    private function _other_post_exists_with_that_slug(string $slug): bool
366
-    {
367
-        global $wpdb;
368
-        $query =
369
-            $wpdb->prepare(
370
-                "SELECT COUNT(ID) FROM $this->_new_table WHERE event_status != 'D' AND post_name = %s",
371
-                $slug
372
-            );
373
-        $count = $wpdb->get_var($query);
374
-        return (bool) intval($count);
375
-    }
376
-
377
-
378
-    /**
379
-     * @param $old_event
380
-     * @return int
381
-     */
382
-    private function _insert_cpt($old_event): int
383
-    {
384
-        global $wpdb;
385
-        // convert 3.1 event status to 4.1 CPT status
386
-        // for reference, 3.1 event stati available for setting are:
387
-        //      $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')));
388
-        //      and the json api uses the following to convert from 3.1 to 4.0
389
-        //      'S'=>'secondary/waitlist',
390
-        //      'A'=>'active',
391
-        //      'X'=>'denied',
392
-        //      'IA'=>'inactive',
393
-        //      'O'=>'ongoing',
394
-        //      'P'=>'pending',
395
-        //      'R'=>'draft',
396
-        //      'D'=>'deleted');
397
-        //      4.1 Event Post stati are the normal post statis
398
-        //      (publish,future,draft,pending,private,trash,auto-draft,inherit)
399
-        //      and 3 custom ones: cancelled,postponed,sold_out
400
-        $status_conversions = [
401
-            'R'  => 'draft',
402
-            'X'  => 'draft',
403
-            // 4.1 doesn't have a "not approved for publishing" status. this is what posts are set to that aren't approved
404
-            'P'  => 'pending',
405
-            'IA' => 'draft',
406
-            // draft and in the past
407
-            // IA=inactive in 3.1: events were switched to this when they expired. in 4.1 that's just calculated
408
-            'O'  => 'publish',
409
-            // @todo: will be an event type later; if this is the status, set the end date WAAAY later; and add term for 'ongoing'
410
-            'A'  => 'publish',
411
-            'S'  => 'draft',
412
-            // @todo: is it ok to just mark secondary/waitlist events as DRAFTS?
413
-            'D'  => 'trash',
414
-        ];
415
-        $post_status        = $status_conversions[ $old_event['event_status'] ];
416
-        // check if we've sold out
417
-        if (intval($old_event['reg_limit']) <= self::count_registrations($old_event['id'])) {
418
-            $post_status = 'sold_out';
419
-        }
420
-        //      FYI postponed and cancelled don't exist in 3.1
421
-        $cols_n_values                      = [
422
-            'post_title'        => stripslashes($old_event['event_name']),// EVT_name
423
-            'post_content'      => stripslashes($old_event['event_desc']),// EVT_desc
424
-            'post_name'         => $this->_find_unique_slug(
425
-                $old_event['event_name'],
426
-                $old_event['event_identifier'],
427
-                $post_status
428
-            ),// EVT_slug
429
-            'post_date'         => $old_event['submitted'],// EVT_created NOT
430
-            'post_date_gmt'     => get_gmt_from_date($old_event['submitted']),
431
-            'post_excerpt'      => '',// EVT_short_desc
432
-            'post_modified'     => $old_event['submitted'],// EVT_modified
433
-            'post_modified_gmt' => get_gmt_from_date($old_event['submitted']),
434
-            'post_author'       => $old_event['wp_user'],// EVT_wp_user
435
-            'post_parent'       => 0,// parent maybe get this from some REM field?
436
-            'menu_order'        => 0,// EVT_order
437
-            'post_type'         => EspressoPostType::EVENTS,// post_type
438
-            'post_status'       => $post_status,// status
439
-        ];
440
-        $cols_n_values_with_no_invalid_text = [];
441
-        foreach ($cols_n_values as $col => $value) {
442
-            $value_sans_invalid_chars = $wpdb->strip_invalid_text_for_column($this->_new_table, $col, $value);
443
-            if (! is_wp_error($value_sans_invalid_chars)) {
444
-                $cols_n_values_with_no_invalid_text[ $col ] = $value_sans_invalid_chars;
445
-            } else {
446
-                // otherwise leave it as-is. It will blow everything up and stop the migration
447
-                $cols_n_values_with_no_invalid_text[ $col ] = $value;
448
-            }
449
-        }
450
-        $cols_n_values = $cols_n_values_with_no_invalid_text;
451
-        $data_types    = [
452
-            '%s',// EVT_name
453
-            '%s',// EVT_desc
454
-            '%s',// EVT_slug
455
-            '%s',// EVT_created
456
-            '%s',
457
-            '%s',// EVT_short_desc
458
-            '%s',// EVT_modified
459
-            '%s',
460
-            '%s',// EVT_wp_user
461
-            '%d',// post_parent
462
-            '%d',// EVT_order
463
-            '%s',// post_type
464
-            '%s',// status
465
-        ];
466
-        $success       = $wpdb->insert(
467
-            $this->_new_table,
468
-            $cols_n_values,
469
-            $data_types
470
-        );
471
-        if (! $success) {
472
-            $this->add_error(
473
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
474
-                    $this->_old_table,
475
-                    $old_event,
476
-                    $this->_new_table,
477
-                    $cols_n_values,
478
-                    $data_types
479
-                )
480
-            );
481
-            return 0;
482
-        }
483
-        return $wpdb->insert_id;
484
-    }
485
-
486
-
487
-    /**
488
-     * Counts all the registrations for the event in the 3.1 DB. (takes into account attendee rows which represent
489
-     * various registrations)
490
-     *
491
-     * @param int   $event_id
492
-     * @return int
493
-     * @global wpdb $wpdb
494
-     */
495
-    public static function count_registrations(int $event_id): int
496
-    {
497
-        global $wpdb;
498
-        return (int) $wpdb->get_var(
499
-            $wpdb->prepare("SELECT sum(quantity) FROM {$wpdb->prefix}events_attendee WHERE event_id=%d", $event_id)
500
-        );
501
-    }
502
-
503
-
504
-    /**
505
-     * @param $old_event
506
-     * @param $new_cpt_id
507
-     * @return int
508
-     */
509
-    private function _insert_event_meta($old_event, $new_cpt_id): int
510
-    {
511
-        global $wpdb;
512
-        $event_meta = maybe_unserialize($old_event['event_meta']);
513
-        //      for reference, 3.1 'default_payment_status' are: $default_payment_status = array(
514
-        //  array('id' => "", 'text' => 'No Change'),
515
-        //  array('id' => 'Incomplete', 'text' => 'Incomplete'),
516
-        //  array('id' => 'Pending', 'text' => 'Pending'),
517
-        //  //array('id' => 'Completed', 'text' => 'Completed')
518
-        // );
519
-        $default_reg_status =
520
-            $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
521
-                $event_meta['default_payment_status'] ?? '',
522
-                intval($old_event['require_pre_approval'])
523
-            );
524
-        $cols_n_values      = [
525
-            'EVT_ID'                          => $new_cpt_id,// EVT_ID_fk
526
-            'EVT_display_desc'                => 'Y' == $old_event['display_desc'],
527
-            'EVT_display_ticket_selector'     => 'Y' == $old_event['display_reg_form'],
528
-            'EVT_visible_on'                  => $this->get_migration_script()->convert_date_string_to_utc(
529
-                $this,
530
-                $old_event,
531
-                current_time(
532
-                    'mysql'
533
-                ),
534
-                $old_event['timezone_string']
535
-            ),// don't use the old 'visible_on', as it wasn't ever used
536
-            'EVT_additional_limit'            => $old_event['allow_multiple'] == 'N' ? 1
537
-                : $old_event['additional_limit'],
538
-            'EVT_default_registration_status' => $default_reg_status,
539
-            'EVT_member_only'                 => $old_event['member_only'],
540
-            'EVT_phone'                       => $old_event['phone'],
541
-            'EVT_allow_overflow'              => 'Y' == $old_event['allow_overflow'],
542
-            'EVT_timezone_string'             => $old_event['timezone_string'],
543
-            'EVT_external_URL'                => $old_event['externalURL'],
544
-            'EVT_donations'                   => false,// doesn't exist in 3.1
545
-
546
-        ];
547
-        $data_types         = [
548
-            '%s',// EVT_ID
549
-            '%d',// EVT_display_desc
550
-            '%d',// EVT_display_ticket_selector
551
-            '%s',// EVT_visible_on
552
-            '%d',// EVT_additional_limit
553
-            '%s',// EVT_default_registration_status
554
-            '%d',// EVT_member_only
555
-            '%s',// EVT_phone
556
-            '%d',// EVT_allow_overflow
557
-            '%s',// EVT_timezone_string
558
-            '%s',// EVT_external_URL
559
-            '%d',// EVT_donations
560
-        ];
561
-        $success            = $wpdb->insert(
562
-            $this->_new_meta_table,
563
-            $cols_n_values,
564
-            $data_types
565
-        );
566
-        if (! $success) {
567
-            $this->add_error(
568
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
569
-                    $this->_old_table,
570
-                    $old_event,
571
-                    $this->_new_meta_table,
572
-                    $cols_n_values,
573
-                    $data_types
574
-                )
575
-            );
576
-            return 0;
577
-        }
578
-        return $wpdb->insert_id;
579
-    }
580
-
581
-
582
-    /**
583
-     * @param $old_event
584
-     * @return int
585
-     */
586
-    private function _maybe_create_venue($old_event): int
587
-    {
588
-        if (
589
-            $old_event['address'] ||
590
-            $old_event['address2'] ||
591
-            $old_event['city'] ||
592
-            $old_event['state'] ||
593
-            $old_event['zip'] ||
594
-            $old_event['venue_title'] ||
595
-            $old_event['venue_url'] ||
596
-            $old_event['venue_image'] ||
597
-            $old_event['venue_phone'] ||
598
-            $old_event['virtual_url'] ||
599
-            $old_event['virtual_phone']
600
-        ) {
601
-            $old_id = $this->_duplicate_venue_exists($old_event);
602
-            if ($old_id) {
603
-                return $old_id;
604
-            }
605
-            $new_id = $this->_insert_venue_into_posts($old_event);
606
-            if ($new_id) {
607
-                $this->_insert_venue_into_meta_table($new_id, $old_event);
608
-                $guid = $old_event['venue_image'] ?? null;
609
-                if ($guid) {
610
-                    $this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
611
-                        $guid,
612
-                        $new_id,
613
-                        $this
614
-                    );
615
-                }
616
-            }
617
-            // we don't bother recording the conversion from old events to venues as that
618
-            // will complicate finding the conversion from old venues to new events
619
-            return $new_id;
620
-        }
621
-        return 0;
622
-    }
623
-
624
-
625
-    /**
626
-     * Assuming there is venue data on this event, check if there is a duplicate venue already in the system for it.
627
-     * If so, return it. Otherwise, return NULL.
628
-     *
629
-     * @param array $old_event
630
-     * @return int duplicate venue id
631
-     */
632
-    private function _duplicate_venue_exists(array $old_event): int
633
-    {
634
-        global $wpdb;
635
-        $conditions     = [
636
-            'VNU_address'       => $old_event ['address'],
637
-            'VNU_address2'      => $old_event['address2'],
638
-            'VNU_city'          => $old_event['city'],
639
-            'VNU_zip'           => $old_event['zip'],
640
-            'post_title'        => $this->_get_venue_title_for_event($old_event),
641
-            'VNU_phone'         => $old_event['venue_phone'],// VNU_phone
642
-            'VNU_url'           => $old_event['venue_url'],// VNU_url
643
-            'VNU_virtual_phone' => $old_event['virtual_phone'],// VNU_virtual_phone
644
-            'VNU_virtual_url'   => $old_event['virtual_url'],// VNU_virtual_url
645
-        ];
646
-        $sql_conditions = [];
647
-        foreach ($conditions as $column => $value) {
648
-            $sql_conditions [] = $wpdb->prepare("$column = %s", $value);
649
-        }
650
-        $query = "SELECT VNU_ID
179
+	private string $_old_start_end_table;
180
+
181
+	private string $_new_meta_table;
182
+
183
+	private string $_new_datetime_table;
184
+
185
+
186
+	/**
187
+	 * Just initializes the status of the migration
188
+	 */
189
+	public function __construct()
190
+	{
191
+		global $wpdb;
192
+		$this->_old_table           = $wpdb->prefix . "events_detail";
193
+		$this->_old_start_end_table = $wpdb->prefix . "events_start_end";
194
+		$this->_new_table           = $wpdb->prefix . "posts";
195
+		$this->_new_meta_table      = $wpdb->prefix . "esp_event_meta";
196
+		$this->_new_datetime_table  = $wpdb->prefix . "esp_datetime";
197
+		$this->_pretty_name         = esc_html__("Events", "event_espresso");
198
+		parent::__construct();
199
+	}
200
+
201
+
202
+	/**
203
+	 * Counts the records to migrate; the public version may cache it
204
+	 *
205
+	 * @return int
206
+	 */
207
+	public function _count_records_to_migrate(): int
208
+	{
209
+		global $wpdb;
210
+		return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $this->_old_table . ' WHERE event_status !="D"');
211
+	}
212
+
213
+
214
+	/**
215
+	 * IMPORTANT: if an error is encountered, or everything is finished, this stage should update its status property
216
+	 * accordingly. Note: it should not alter the count of items migrated. That is done in the public function that
217
+	 * calls this. IMPORTANT: The count of items migrated should ONLY be less than $num_items_to_migrate when it's the
218
+	 * last migration step, otherwise it should always return $num_items_to_migrate. (Eg, if we're migrating attendees
219
+	 * rows from the database, and $num_items_to_migrate is set to 50, then we SHOULD actually migrate 50 rows,but at
220
+	 * very least we MUST report/return 50 items migrated)
221
+	 *
222
+	 * @param int $num_items_to_migrate
223
+	 * @return int number of items ACTUALLY migrated
224
+	 * @throws EE_Error
225
+	 */
226
+	protected function _migration_step($num_items_to_migrate = 50)
227
+	{
228
+		global $wpdb;
229
+		// because the migration of each event can be a LOT more work, make each step smaller
230
+		$num_items_to_migrate     = max(1, $num_items_to_migrate / 5);
231
+		$events                   =
232
+			$wpdb->get_results(
233
+				$wpdb->prepare(
234
+					"SELECT * FROM $this->_old_table WHERE event_status!='D' LIMIT %d,%d",
235
+					$this->count_records_migrated(),
236
+					$num_items_to_migrate
237
+				),
238
+				ARRAY_A
239
+			);
240
+		$items_migrated_this_step = 0;
241
+
242
+		foreach ($events as $event_row) {
243
+			$guid = null;
244
+			// insert new 4.1 Attendee object using $wpdb
245
+			$post_id = $this->_insert_cpt($event_row);
246
+			if ($post_id) {
247
+				$this->get_migration_script()->set_mapping(
248
+					$this->_old_table,
249
+					$event_row['id'],
250
+					$this->_new_table,
251
+					$post_id
252
+				);
253
+				$meta_id = $this->_insert_event_meta($event_row, $post_id);
254
+				if ($meta_id) {
255
+					$this->get_migration_script()->set_mapping(
256
+						$this->_old_table,
257
+						$event_row['id'],
258
+						$this->_new_meta_table,
259
+						$meta_id
260
+					);
261
+				}
262
+				$this->_convert_start_end_times($event_row, $post_id);
263
+				$event_meta = maybe_unserialize($event_row['event_meta']);
264
+				$guid       = $event_meta['event_thumbnail_url'] ?? null;
265
+				if ($guid) {
266
+					$this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
267
+						$guid,
268
+						$post_id,
269
+						$this
270
+					);
271
+				}
272
+				// maybe create a venue from info on the event?
273
+				$new_venue_id = $this->_maybe_create_venue($event_row);
274
+				if ($new_venue_id) {
275
+					$this->_insert_new_venue_to_event($post_id, $new_venue_id);
276
+				}
277
+				$this->_add_post_metas($event_row, $post_id);
278
+			}
279
+			$items_migrated_this_step++;
280
+			if ($guid) {
281
+				// if we had to check for an image attachment
282
+				// then let's call it a day (avoid timing out, because this took a long time)
283
+				break;
284
+			}
285
+		}
286
+		if ($this->count_records_migrated() + $items_migrated_this_step >= $this->count_records_to_migrate()) {
287
+			$this->set_status(EE_Data_Migration_Manager::status_completed);
288
+		}
289
+		return $items_migrated_this_step;
290
+	}
291
+
292
+
293
+	/**
294
+	 * Stores any extra 3.1 "event_meta" column things as post meta
295
+	 *
296
+	 * @param array $old_event
297
+	 * @param int   $post_id
298
+	 * @return void
299
+	 */
300
+	private function _add_post_metas(array $old_event, int $post_id)
301
+	{
302
+		$event_meta = maybe_unserialize($old_event['event_meta']);
303
+		if (! $event_meta || ! is_array($event_meta)) {
304
+			return;
305
+		}
306
+		unset($event_meta['date_submitted']);              // factored into CPT
307
+		unset($event_meta['additional_attendee_reg_info']);// factored into event meta table
308
+		unset($event_meta['default_payment_status']);      // dido
309
+		unset($event_meta['event_thumbnail_url']);         // used to find post featured image
310
+		foreach ($event_meta as $meta_key => $meta_value) {
311
+			if ($meta_key) {// if the meta key is just an empty string, ignore it
312
+				$success = add_post_meta($post_id, $meta_key, $meta_value, true);
313
+				if (! $success) {
314
+					$this->add_error(
315
+						sprintf(
316
+							esc_html__(
317
+								"Could not add post meta for CPT with ID #%d. Meta key: '%s',meta value:'%d' for 3.1 event: %s",
318
+								"event_espresso"
319
+							),
320
+							$post_id,
321
+							$meta_key,
322
+							$meta_value,
323
+							implode(",", $old_event)
324
+						)
325
+					);
326
+				}
327
+			}
328
+		}
329
+		if ($old_event['alt_email']) {
330
+			add_post_meta($post_id, 'alt_email', $old_event['alt_email']);
331
+		}
332
+		if ($old_event['recurrence_id']) {
333
+			add_post_meta($post_id, 'recurrence_id', $old_event['recurrence_id']);
334
+		}
335
+	}
336
+
337
+
338
+	/**
339
+	 * Finds a unique slug for this event, given its name (we could have simply used
340
+	 * the old unique_identifier column, but it added a long string of seemingly random characters onto the end
341
+	 * and really wasn't that pretty for a slug, so we decided we'd make our own slug again)
342
+	 *
343
+	 * @param string $event_name      (the name of the event for reading by humans)
344
+	 * @param string $old_identifier  the old EE3 identifier (a long unique string)
345
+	 * @param string $new_post_status a post status
346
+	 * @return string
347
+	 */
348
+	private function _find_unique_slug(
349
+		string $event_name,
350
+		string $old_identifier = '',
351
+		string $new_post_status = 'publish'
352
+	): string {
353
+		$original_name = $event_name ? sanitize_title($event_name) : $old_identifier;
354
+		return wp_unique_post_slug($original_name, 0, $new_post_status, EspressoPostType::EVENTS, 0);
355
+	}
356
+
357
+
358
+	/**
359
+	 * returns whether there is a post that has this same slug (post_title)
360
+	 *
361
+	 * @param string $slug
362
+	 * @return boolean
363
+	 * @global wpdb  $wpdb
364
+	 */
365
+	private function _other_post_exists_with_that_slug(string $slug): bool
366
+	{
367
+		global $wpdb;
368
+		$query =
369
+			$wpdb->prepare(
370
+				"SELECT COUNT(ID) FROM $this->_new_table WHERE event_status != 'D' AND post_name = %s",
371
+				$slug
372
+			);
373
+		$count = $wpdb->get_var($query);
374
+		return (bool) intval($count);
375
+	}
376
+
377
+
378
+	/**
379
+	 * @param $old_event
380
+	 * @return int
381
+	 */
382
+	private function _insert_cpt($old_event): int
383
+	{
384
+		global $wpdb;
385
+		// convert 3.1 event status to 4.1 CPT status
386
+		// for reference, 3.1 event stati available for setting are:
387
+		//      $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')));
388
+		//      and the json api uses the following to convert from 3.1 to 4.0
389
+		//      'S'=>'secondary/waitlist',
390
+		//      'A'=>'active',
391
+		//      'X'=>'denied',
392
+		//      'IA'=>'inactive',
393
+		//      'O'=>'ongoing',
394
+		//      'P'=>'pending',
395
+		//      'R'=>'draft',
396
+		//      'D'=>'deleted');
397
+		//      4.1 Event Post stati are the normal post statis
398
+		//      (publish,future,draft,pending,private,trash,auto-draft,inherit)
399
+		//      and 3 custom ones: cancelled,postponed,sold_out
400
+		$status_conversions = [
401
+			'R'  => 'draft',
402
+			'X'  => 'draft',
403
+			// 4.1 doesn't have a "not approved for publishing" status. this is what posts are set to that aren't approved
404
+			'P'  => 'pending',
405
+			'IA' => 'draft',
406
+			// draft and in the past
407
+			// IA=inactive in 3.1: events were switched to this when they expired. in 4.1 that's just calculated
408
+			'O'  => 'publish',
409
+			// @todo: will be an event type later; if this is the status, set the end date WAAAY later; and add term for 'ongoing'
410
+			'A'  => 'publish',
411
+			'S'  => 'draft',
412
+			// @todo: is it ok to just mark secondary/waitlist events as DRAFTS?
413
+			'D'  => 'trash',
414
+		];
415
+		$post_status        = $status_conversions[ $old_event['event_status'] ];
416
+		// check if we've sold out
417
+		if (intval($old_event['reg_limit']) <= self::count_registrations($old_event['id'])) {
418
+			$post_status = 'sold_out';
419
+		}
420
+		//      FYI postponed and cancelled don't exist in 3.1
421
+		$cols_n_values                      = [
422
+			'post_title'        => stripslashes($old_event['event_name']),// EVT_name
423
+			'post_content'      => stripslashes($old_event['event_desc']),// EVT_desc
424
+			'post_name'         => $this->_find_unique_slug(
425
+				$old_event['event_name'],
426
+				$old_event['event_identifier'],
427
+				$post_status
428
+			),// EVT_slug
429
+			'post_date'         => $old_event['submitted'],// EVT_created NOT
430
+			'post_date_gmt'     => get_gmt_from_date($old_event['submitted']),
431
+			'post_excerpt'      => '',// EVT_short_desc
432
+			'post_modified'     => $old_event['submitted'],// EVT_modified
433
+			'post_modified_gmt' => get_gmt_from_date($old_event['submitted']),
434
+			'post_author'       => $old_event['wp_user'],// EVT_wp_user
435
+			'post_parent'       => 0,// parent maybe get this from some REM field?
436
+			'menu_order'        => 0,// EVT_order
437
+			'post_type'         => EspressoPostType::EVENTS,// post_type
438
+			'post_status'       => $post_status,// status
439
+		];
440
+		$cols_n_values_with_no_invalid_text = [];
441
+		foreach ($cols_n_values as $col => $value) {
442
+			$value_sans_invalid_chars = $wpdb->strip_invalid_text_for_column($this->_new_table, $col, $value);
443
+			if (! is_wp_error($value_sans_invalid_chars)) {
444
+				$cols_n_values_with_no_invalid_text[ $col ] = $value_sans_invalid_chars;
445
+			} else {
446
+				// otherwise leave it as-is. It will blow everything up and stop the migration
447
+				$cols_n_values_with_no_invalid_text[ $col ] = $value;
448
+			}
449
+		}
450
+		$cols_n_values = $cols_n_values_with_no_invalid_text;
451
+		$data_types    = [
452
+			'%s',// EVT_name
453
+			'%s',// EVT_desc
454
+			'%s',// EVT_slug
455
+			'%s',// EVT_created
456
+			'%s',
457
+			'%s',// EVT_short_desc
458
+			'%s',// EVT_modified
459
+			'%s',
460
+			'%s',// EVT_wp_user
461
+			'%d',// post_parent
462
+			'%d',// EVT_order
463
+			'%s',// post_type
464
+			'%s',// status
465
+		];
466
+		$success       = $wpdb->insert(
467
+			$this->_new_table,
468
+			$cols_n_values,
469
+			$data_types
470
+		);
471
+		if (! $success) {
472
+			$this->add_error(
473
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
474
+					$this->_old_table,
475
+					$old_event,
476
+					$this->_new_table,
477
+					$cols_n_values,
478
+					$data_types
479
+				)
480
+			);
481
+			return 0;
482
+		}
483
+		return $wpdb->insert_id;
484
+	}
485
+
486
+
487
+	/**
488
+	 * Counts all the registrations for the event in the 3.1 DB. (takes into account attendee rows which represent
489
+	 * various registrations)
490
+	 *
491
+	 * @param int   $event_id
492
+	 * @return int
493
+	 * @global wpdb $wpdb
494
+	 */
495
+	public static function count_registrations(int $event_id): int
496
+	{
497
+		global $wpdb;
498
+		return (int) $wpdb->get_var(
499
+			$wpdb->prepare("SELECT sum(quantity) FROM {$wpdb->prefix}events_attendee WHERE event_id=%d", $event_id)
500
+		);
501
+	}
502
+
503
+
504
+	/**
505
+	 * @param $old_event
506
+	 * @param $new_cpt_id
507
+	 * @return int
508
+	 */
509
+	private function _insert_event_meta($old_event, $new_cpt_id): int
510
+	{
511
+		global $wpdb;
512
+		$event_meta = maybe_unserialize($old_event['event_meta']);
513
+		//      for reference, 3.1 'default_payment_status' are: $default_payment_status = array(
514
+		//  array('id' => "", 'text' => 'No Change'),
515
+		//  array('id' => 'Incomplete', 'text' => 'Incomplete'),
516
+		//  array('id' => 'Pending', 'text' => 'Pending'),
517
+		//  //array('id' => 'Completed', 'text' => 'Completed')
518
+		// );
519
+		$default_reg_status =
520
+			$this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
521
+				$event_meta['default_payment_status'] ?? '',
522
+				intval($old_event['require_pre_approval'])
523
+			);
524
+		$cols_n_values      = [
525
+			'EVT_ID'                          => $new_cpt_id,// EVT_ID_fk
526
+			'EVT_display_desc'                => 'Y' == $old_event['display_desc'],
527
+			'EVT_display_ticket_selector'     => 'Y' == $old_event['display_reg_form'],
528
+			'EVT_visible_on'                  => $this->get_migration_script()->convert_date_string_to_utc(
529
+				$this,
530
+				$old_event,
531
+				current_time(
532
+					'mysql'
533
+				),
534
+				$old_event['timezone_string']
535
+			),// don't use the old 'visible_on', as it wasn't ever used
536
+			'EVT_additional_limit'            => $old_event['allow_multiple'] == 'N' ? 1
537
+				: $old_event['additional_limit'],
538
+			'EVT_default_registration_status' => $default_reg_status,
539
+			'EVT_member_only'                 => $old_event['member_only'],
540
+			'EVT_phone'                       => $old_event['phone'],
541
+			'EVT_allow_overflow'              => 'Y' == $old_event['allow_overflow'],
542
+			'EVT_timezone_string'             => $old_event['timezone_string'],
543
+			'EVT_external_URL'                => $old_event['externalURL'],
544
+			'EVT_donations'                   => false,// doesn't exist in 3.1
545
+
546
+		];
547
+		$data_types         = [
548
+			'%s',// EVT_ID
549
+			'%d',// EVT_display_desc
550
+			'%d',// EVT_display_ticket_selector
551
+			'%s',// EVT_visible_on
552
+			'%d',// EVT_additional_limit
553
+			'%s',// EVT_default_registration_status
554
+			'%d',// EVT_member_only
555
+			'%s',// EVT_phone
556
+			'%d',// EVT_allow_overflow
557
+			'%s',// EVT_timezone_string
558
+			'%s',// EVT_external_URL
559
+			'%d',// EVT_donations
560
+		];
561
+		$success            = $wpdb->insert(
562
+			$this->_new_meta_table,
563
+			$cols_n_values,
564
+			$data_types
565
+		);
566
+		if (! $success) {
567
+			$this->add_error(
568
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
569
+					$this->_old_table,
570
+					$old_event,
571
+					$this->_new_meta_table,
572
+					$cols_n_values,
573
+					$data_types
574
+				)
575
+			);
576
+			return 0;
577
+		}
578
+		return $wpdb->insert_id;
579
+	}
580
+
581
+
582
+	/**
583
+	 * @param $old_event
584
+	 * @return int
585
+	 */
586
+	private function _maybe_create_venue($old_event): int
587
+	{
588
+		if (
589
+			$old_event['address'] ||
590
+			$old_event['address2'] ||
591
+			$old_event['city'] ||
592
+			$old_event['state'] ||
593
+			$old_event['zip'] ||
594
+			$old_event['venue_title'] ||
595
+			$old_event['venue_url'] ||
596
+			$old_event['venue_image'] ||
597
+			$old_event['venue_phone'] ||
598
+			$old_event['virtual_url'] ||
599
+			$old_event['virtual_phone']
600
+		) {
601
+			$old_id = $this->_duplicate_venue_exists($old_event);
602
+			if ($old_id) {
603
+				return $old_id;
604
+			}
605
+			$new_id = $this->_insert_venue_into_posts($old_event);
606
+			if ($new_id) {
607
+				$this->_insert_venue_into_meta_table($new_id, $old_event);
608
+				$guid = $old_event['venue_image'] ?? null;
609
+				if ($guid) {
610
+					$this->get_migration_script()->convert_image_url_to_attachment_and_attach_to_post(
611
+						$guid,
612
+						$new_id,
613
+						$this
614
+					);
615
+				}
616
+			}
617
+			// we don't bother recording the conversion from old events to venues as that
618
+			// will complicate finding the conversion from old venues to new events
619
+			return $new_id;
620
+		}
621
+		return 0;
622
+	}
623
+
624
+
625
+	/**
626
+	 * Assuming there is venue data on this event, check if there is a duplicate venue already in the system for it.
627
+	 * If so, return it. Otherwise, return NULL.
628
+	 *
629
+	 * @param array $old_event
630
+	 * @return int duplicate venue id
631
+	 */
632
+	private function _duplicate_venue_exists(array $old_event): int
633
+	{
634
+		global $wpdb;
635
+		$conditions     = [
636
+			'VNU_address'       => $old_event ['address'],
637
+			'VNU_address2'      => $old_event['address2'],
638
+			'VNU_city'          => $old_event['city'],
639
+			'VNU_zip'           => $old_event['zip'],
640
+			'post_title'        => $this->_get_venue_title_for_event($old_event),
641
+			'VNU_phone'         => $old_event['venue_phone'],// VNU_phone
642
+			'VNU_url'           => $old_event['venue_url'],// VNU_url
643
+			'VNU_virtual_phone' => $old_event['virtual_phone'],// VNU_virtual_phone
644
+			'VNU_virtual_url'   => $old_event['virtual_url'],// VNU_virtual_url
645
+		];
646
+		$sql_conditions = [];
647
+		foreach ($conditions as $column => $value) {
648
+			$sql_conditions [] = $wpdb->prepare("$column = %s", $value);
649
+		}
650
+		$query = "SELECT VNU_ID
651 651
 					FROM
652 652
 		$wpdb->posts as p INNER JOIN
653 653
 		{$wpdb->prefix}esp_venue_meta as v ON p.ID = v.VNU_ID
654 654
 			WHERE " . implode(" AND ", $sql_conditions) . " LIMIT 1";
655
-        return (int) $wpdb->get_var($query);
656
-    }
657
-
658
-
659
-    /**
660
-     * Gets teh venue's title or makes one up if there is none
661
-     *
662
-     * @param array $event_data_array keys are events_details columns and values are their values
663
-     * @return string
664
-     */
665
-    protected function _get_venue_title_for_event(array $event_data_array): string
666
-    {
667
-        return $event_data_array['venue_title']
668
-            ? stripslashes($event_data_array['venue_title'])
669
-            : stripslashes(
670
-                sprintf(
671
-                    esc_html__('Venue of %s', 'event_espresso'),
672
-                    $event_data_array['event_name']
673
-                )
674
-            );
675
-    }
676
-
677
-
678
-    /**
679
-     * Inserts the CPT
680
-     *
681
-     * @param array $old_event keys are cols, values are col values
682
-     * @return int
683
-     */
684
-    private function _insert_venue_into_posts(array $old_event): int
685
-    {
686
-        global $wpdb;
687
-        $insertion_array  = [
688
-            'post_title'        => $this->_get_venue_title_for_event($old_event),// VNU_name
689
-            'post_content'      => '',// VNU_desc
690
-            'post_name'         => $this->_find_unique_slug(
691
-                $old_event['venue_title'],
692
-                sanitize_title('venue-of-' . $old_event['event_name'])
693
-            ),// VNU_identifier
694
-            'post_date'         => current_time('mysql'),// VNU_created
695
-            'post_date_gmt'     => get_gmt_from_date(current_time('mysql')),
696
-            'post_excerpt'      => '',// VNU_short_desc arbitrary only 50 characters
697
-            'post_modified'     => current_time('mysql'),// VNU_modified
698
-            'post_modified_gmt' => get_gmt_from_date(current_time('mysql')),
699
-            'post_author'       => $old_event['wp_user'],// VNU_wp_user
700
-            'post_parent'       => 0,// parent
701
-            'menu_order'        => 0,// VNU_order
702
-            'post_type'         => EspressoPostType::VENUES,// post_type
703
-        ];
704
-        $data_types_array = [
705
-            '%s',// VNU_name
706
-            '%s',// VNU_desc
707
-            '%s',// VNU_identifier
708
-            '%s',// VNU_created
709
-            '%s',
710
-            '%s',// VNU_short_desc
711
-            '%s',// VNU_modified
712
-            '%s',
713
-            '%d',// VNU_wp_user
714
-            '%d',// parent
715
-            '%d',// VNU_order
716
-            '%s',// post_type
717
-        ];
718
-        $success          = $wpdb->insert(
719
-            $wpdb->posts,
720
-            $insertion_array,
721
-            $data_types_array
722
-        );
723
-        if (! $success) {
724
-            $this->add_error(
725
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
726
-                    $this->_old_table,
727
-                    $old_event,
728
-                    $this->_new_table,
729
-                    $insertion_array,
730
-                    $data_types_array
731
-                )
732
-            );
733
-            return 0;
734
-        }
735
-        return $wpdb->insert_id;
736
-    }
737
-
738
-
739
-    /**
740
-     * Inserts into the venue_meta table
741
-     *
742
-     * @param int   $cpt_id
743
-     * @param array $old_event
744
-     * @return void
745
-     */
746
-    private function _insert_venue_into_meta_table(int $cpt_id, array $old_event): void
747
-    {
748
-        global $wpdb;
749
-
750
-        // assume the country is the same as the organization's old settings
751
-        $country_iso = $this->get_migration_script()->get_default_country_iso();
752
-        // find the state from the venue, or the organization, or just guess california
753
-        if (! $old_event['state']) {
754
-            $old_org_options = get_option('events_organization_settings');
755
-            $state_name      = stripslashes($old_org_options['organization_state']);
756
-        } else {
757
-            $state_name = $old_event['state'];
758
-        }
759
-        if (! $state_name) {
760
-            $state_name = 'CA';
761
-        }
762
-        // get a state ID with the same name, if possible
763
-        try {
764
-            $state    = $this->get_migration_script()->get_or_create_state($state_name, $country_iso);
765
-            $state_id = $state['STA_ID'];
766
-        } catch (EE_Error $e) {
767
-            $this->add_error($e->getMessage());
768
-            $state_id = 0;
769
-        }
770
-        // now insert into meta table
771
-        $insertion_array = [
772
-            'VNU_ID'              => $cpt_id,// VNU_ID_fk
773
-            'VNU_address'         => stripslashes($old_event['address']),// VNU_address
774
-            'VNU_address2'        => stripslashes($old_event['address2']),// VNU_address2
775
-            'VNU_city'            => stripslashes($old_event['city']),// VNU_city
776
-            'STA_ID'              => $state_id,// STA_ID
777
-            'CNT_ISO'             => $country_iso,// CNT_ISO
778
-            'VNU_zip'             => $old_event['zip'],// VNU_zip
779
-            'VNU_phone'           => $old_event['venue_phone'],// VNU_phone
780
-            'VNU_capacity'        => -1,// VNU_capacity
781
-            'VNU_url'             => $old_event['venue_url'],// VNU_url
782
-            'VNU_virtual_phone'   => $old_event['virtual_phone'],// VNU_virtual_phone
783
-            'VNU_virtual_url'     => $old_event['virtual_url'],// VNU_virtual_url
784
-            'VNU_google_map_link' => '',// VNU_google_map_link
785
-            'VNU_enable_for_gmap' => true, // VNU_enable_for_gmap
786
-        ];
787
-        $data_types      = [
788
-            '%d',// VNU_ID_fk
789
-            '%s',// VNU_address
790
-            '%s',// VNU_address2
791
-            '%s',// VNU_city
792
-            '%d',// STA_ID
793
-            '%s',// CNT_ISO
794
-            '%s',// VNU_zip
795
-            '%s',// VNU_phone
796
-            '%d',// VNU_capacity
797
-            '%s',// VNU_url
798
-            '%s',// VNU_virtual_phone
799
-            '%s',// VNU_virtual_url
800
-            '%s',// VNU_google_map_link
801
-            '%d',// VNU_enable_for_gmap
802
-        ];
803
-        $success         = $wpdb->insert($wpdb->prefix . "esp_venue_meta", $insertion_array, $data_types);
804
-        if (! $success) {
805
-            $this->add_error(
806
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
807
-                    $this->_old_table,
808
-                    $old_event,
809
-                    $this->_new_meta_table,
810
-                    $insertion_array,
811
-                    $data_types
812
-                )
813
-            );
814
-        }
815
-    }
816
-
817
-
818
-    /**
819
-     * @param $new_event_id
820
-     * @param $new_venue_id
821
-     * @return void
822
-     */
823
-    private function _insert_new_venue_to_event($new_event_id, $new_venue_id): void
824
-    {
825
-        global $wpdb;
826
-        if (! $new_event_id) {
827
-            $this->add_error(
828
-                sprintf(esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"), $new_event_id)
829
-            );
830
-            return;
831
-        }
832
-        if (! $new_venue_id) {
833
-            $this->add_error(
834
-                sprintf(esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"), $new_venue_id)
835
-            );
836
-            return;
837
-        }
838
-        $cols_n_values = [
839
-            'EVT_ID'      => $new_event_id,
840
-            'VNU_ID'      => $new_venue_id,
841
-            'EVV_primary' => true,
842
-        ];
843
-        $data_types    = [
844
-            '%d',// EVT_ID
845
-            '%d',// VNU_ID
846
-            '%d',// EVT_primary
847
-        ];
848
-        $success       = $wpdb->insert($wpdb->prefix . "esp_event_venue", $cols_n_values, $data_types);
849
-        if (! $success) {
850
-            $this->add_error(
851
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
852
-                    $this->_old_table,
853
-                    [],
854
-                    $this->_new_table,
855
-                    $cols_n_values,
856
-                    $data_types
857
-                )
858
-            );
859
-        }
860
-    }
861
-
862
-
863
-    /**
864
-     * Converts all the 3.1 start-end times for the event to 4.1 datetimes
865
-     *
866
-     * @param array $old_event  results of get_results(...,ARRAY_A)
867
-     * @param int   $new_cpt_id new post ID
868
-     * @return void (if there are errors though, adds them to the stage's error list
869
-     * @throws EE_Error
870
-     * @global wpdb $wpdb
871
-     */
872
-    private function _convert_start_end_times(array $old_event, int $new_cpt_id)
873
-    {
874
-        $start_end_times = $this->_get_old_start_end_times($old_event['id']);
875
-        foreach ($start_end_times as $start_end_time) {
876
-            $datetime_id = $this->_insert_new_datetime($start_end_time, $old_event, $new_cpt_id);
877
-            if ($datetime_id) {
878
-                $this->get_migration_script()->set_mapping(
879
-                    $this->_old_start_end_table,
880
-                    $start_end_time['id'],
881
-                    $this->_new_datetime_table,
882
-                    $datetime_id
883
-                );
884
-            }
885
-        }
886
-    }
887
-
888
-
889
-    /**
890
-     * Queries the 3.1 wp_events_start_end table to get all the start and end times for the event
891
-     *
892
-     * @param int   $old_event_id
893
-     * @return array
894
-     * @global wpdb $wpdb
895
-     */
896
-    private function _get_old_start_end_times(int $old_event_id): array
897
-    {
898
-        global $wpdb;
899
-        return $wpdb->get_results(
900
-            $wpdb->prepare("SELECT * FROM $this->_old_start_end_table WHERE event_id=%d", $old_event_id),
901
-            ARRAY_A
902
-        );
903
-    }
904
-
905
-
906
-    /**
907
-     * Inserts a 4.1 datetime given the 3.1 start_end db row and event_details row
908
-     *
909
-     * @param array $start_end_time_row
910
-     * @param array $old_event_row
911
-     * @param int   $new_cpt_id
912
-     * @return int ID of new datetime
913
-     */
914
-    private function _insert_new_datetime(array $start_end_time_row, array $old_event_row, int $new_cpt_id): int
915
-    {
916
-        global $wpdb;
917
-        $start_date         = $old_event_row['start_date'];
918
-        $start_time         = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['start_time']);
919
-        $end_date           = $old_event_row['end_date'];
920
-        $end_time           = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['end_time']);
921
-        $existing_datetimes = $this->_count_other_datetimes_exist_for_new_event($new_cpt_id);
922
-        $start_datetime_utc =
923
-            $this->get_migration_script()->convert_date_string_to_utc(
924
-                $this,
925
-                $start_end_time_row,
926
-                "$start_date $start_time:00",
927
-                $old_event_row['timezone_string']
928
-            );
929
-        $end_datetime_utc   =
930
-            $this->get_migration_script()->convert_date_string_to_utc(
931
-                $this,
932
-                $start_end_time_row,
933
-                "$end_date $end_time:00",
934
-                $old_event_row['timezone_string']
935
-            );
936
-        $cols_n_values      = [
937
-            'EVT_ID'        => $new_cpt_id,
938
-            // EVT_ID
939
-            'DTT_EVT_start' => $start_datetime_utc,
940
-            // DTT_EVT_start
941
-            'DTT_EVT_end'   => $end_datetime_utc,
942
-            // DTT_EVT_end
943
-            'DTT_reg_limit' => intval($start_end_time_row['reg_limit']) ? $start_end_time_row['reg_limit']
944
-                : $old_event_row['reg_limit'],
945
-            // DTT_reg_limit
946
-            'DTT_sold'      => 0,
947
-            // note: we will increment this as registrations are added during the migration
948
-            //          'DTT_is_primary'=> 0 == $existing_datetimes ,//DTT_is_primary... if count==0, then we'll call it the 'primary'
949
-            'DTT_order'     => $existing_datetimes,
950
-            // DTT_order, just give it the same order as the count of how many datetimes already exist
951
-            'DTT_parent'    => 0,
952
-            'DTT_deleted'   => false,
953
-        ];
954
-        $data_types         = [
955
-            '%d',// EVT_Id
956
-            '%s',// DTT_EVT_start
957
-            '%s',// DTT_EVT_end
958
-            '%d',// DTT_reg_limit
959
-            '%d',// DTT_sold
960
-            //          '%d',//DTT_is_primary
961
-            '%d',// DTT_order
962
-            '%d',// DTT_parent
963
-            '%d',// DTT_deleted
964
-        ];
965
-        $success            = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
966
-        if (! $success) {
967
-            $this->add_error(
968
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
969
-                    $this->_old_start_end_table,
970
-                    array_merge($old_event_row, $start_end_time_row),
971
-                    $this->_new_datetime_table,
972
-                    $cols_n_values,
973
-                    $data_types
974
-                )
975
-            );
976
-            return 0;
977
-        }
978
-        return $wpdb->insert_id;
979
-    }
980
-
981
-
982
-    /**
983
-     * Checks if there's a 4.1 datetime for this event already. This is mostly only handy
984
-     * when deciding whether a datetime we're about ot insert should be the 'primary' or not
985
-     *
986
-     * @param int   $cpt_event_id
987
-     * @return int
988
-     * @global wpdb $wpdb
989
-     */
990
-    private function _count_other_datetimes_exist_for_new_event(int $cpt_event_id): int
991
-    {
992
-        global $wpdb;
993
-        return (int) $wpdb->get_var(
994
-            $wpdb->prepare(
995
-                "SELECT COUNT(*) FROM $this->_new_datetime_table WHERE EVT_ID=%d",
996
-                $cpt_event_id
997
-            )
998
-        );
999
-    }
655
+		return (int) $wpdb->get_var($query);
656
+	}
657
+
658
+
659
+	/**
660
+	 * Gets teh venue's title or makes one up if there is none
661
+	 *
662
+	 * @param array $event_data_array keys are events_details columns and values are their values
663
+	 * @return string
664
+	 */
665
+	protected function _get_venue_title_for_event(array $event_data_array): string
666
+	{
667
+		return $event_data_array['venue_title']
668
+			? stripslashes($event_data_array['venue_title'])
669
+			: stripslashes(
670
+				sprintf(
671
+					esc_html__('Venue of %s', 'event_espresso'),
672
+					$event_data_array['event_name']
673
+				)
674
+			);
675
+	}
676
+
677
+
678
+	/**
679
+	 * Inserts the CPT
680
+	 *
681
+	 * @param array $old_event keys are cols, values are col values
682
+	 * @return int
683
+	 */
684
+	private function _insert_venue_into_posts(array $old_event): int
685
+	{
686
+		global $wpdb;
687
+		$insertion_array  = [
688
+			'post_title'        => $this->_get_venue_title_for_event($old_event),// VNU_name
689
+			'post_content'      => '',// VNU_desc
690
+			'post_name'         => $this->_find_unique_slug(
691
+				$old_event['venue_title'],
692
+				sanitize_title('venue-of-' . $old_event['event_name'])
693
+			),// VNU_identifier
694
+			'post_date'         => current_time('mysql'),// VNU_created
695
+			'post_date_gmt'     => get_gmt_from_date(current_time('mysql')),
696
+			'post_excerpt'      => '',// VNU_short_desc arbitrary only 50 characters
697
+			'post_modified'     => current_time('mysql'),// VNU_modified
698
+			'post_modified_gmt' => get_gmt_from_date(current_time('mysql')),
699
+			'post_author'       => $old_event['wp_user'],// VNU_wp_user
700
+			'post_parent'       => 0,// parent
701
+			'menu_order'        => 0,// VNU_order
702
+			'post_type'         => EspressoPostType::VENUES,// post_type
703
+		];
704
+		$data_types_array = [
705
+			'%s',// VNU_name
706
+			'%s',// VNU_desc
707
+			'%s',// VNU_identifier
708
+			'%s',// VNU_created
709
+			'%s',
710
+			'%s',// VNU_short_desc
711
+			'%s',// VNU_modified
712
+			'%s',
713
+			'%d',// VNU_wp_user
714
+			'%d',// parent
715
+			'%d',// VNU_order
716
+			'%s',// post_type
717
+		];
718
+		$success          = $wpdb->insert(
719
+			$wpdb->posts,
720
+			$insertion_array,
721
+			$data_types_array
722
+		);
723
+		if (! $success) {
724
+			$this->add_error(
725
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
726
+					$this->_old_table,
727
+					$old_event,
728
+					$this->_new_table,
729
+					$insertion_array,
730
+					$data_types_array
731
+				)
732
+			);
733
+			return 0;
734
+		}
735
+		return $wpdb->insert_id;
736
+	}
737
+
738
+
739
+	/**
740
+	 * Inserts into the venue_meta table
741
+	 *
742
+	 * @param int   $cpt_id
743
+	 * @param array $old_event
744
+	 * @return void
745
+	 */
746
+	private function _insert_venue_into_meta_table(int $cpt_id, array $old_event): void
747
+	{
748
+		global $wpdb;
749
+
750
+		// assume the country is the same as the organization's old settings
751
+		$country_iso = $this->get_migration_script()->get_default_country_iso();
752
+		// find the state from the venue, or the organization, or just guess california
753
+		if (! $old_event['state']) {
754
+			$old_org_options = get_option('events_organization_settings');
755
+			$state_name      = stripslashes($old_org_options['organization_state']);
756
+		} else {
757
+			$state_name = $old_event['state'];
758
+		}
759
+		if (! $state_name) {
760
+			$state_name = 'CA';
761
+		}
762
+		// get a state ID with the same name, if possible
763
+		try {
764
+			$state    = $this->get_migration_script()->get_or_create_state($state_name, $country_iso);
765
+			$state_id = $state['STA_ID'];
766
+		} catch (EE_Error $e) {
767
+			$this->add_error($e->getMessage());
768
+			$state_id = 0;
769
+		}
770
+		// now insert into meta table
771
+		$insertion_array = [
772
+			'VNU_ID'              => $cpt_id,// VNU_ID_fk
773
+			'VNU_address'         => stripslashes($old_event['address']),// VNU_address
774
+			'VNU_address2'        => stripslashes($old_event['address2']),// VNU_address2
775
+			'VNU_city'            => stripslashes($old_event['city']),// VNU_city
776
+			'STA_ID'              => $state_id,// STA_ID
777
+			'CNT_ISO'             => $country_iso,// CNT_ISO
778
+			'VNU_zip'             => $old_event['zip'],// VNU_zip
779
+			'VNU_phone'           => $old_event['venue_phone'],// VNU_phone
780
+			'VNU_capacity'        => -1,// VNU_capacity
781
+			'VNU_url'             => $old_event['venue_url'],// VNU_url
782
+			'VNU_virtual_phone'   => $old_event['virtual_phone'],// VNU_virtual_phone
783
+			'VNU_virtual_url'     => $old_event['virtual_url'],// VNU_virtual_url
784
+			'VNU_google_map_link' => '',// VNU_google_map_link
785
+			'VNU_enable_for_gmap' => true, // VNU_enable_for_gmap
786
+		];
787
+		$data_types      = [
788
+			'%d',// VNU_ID_fk
789
+			'%s',// VNU_address
790
+			'%s',// VNU_address2
791
+			'%s',// VNU_city
792
+			'%d',// STA_ID
793
+			'%s',// CNT_ISO
794
+			'%s',// VNU_zip
795
+			'%s',// VNU_phone
796
+			'%d',// VNU_capacity
797
+			'%s',// VNU_url
798
+			'%s',// VNU_virtual_phone
799
+			'%s',// VNU_virtual_url
800
+			'%s',// VNU_google_map_link
801
+			'%d',// VNU_enable_for_gmap
802
+		];
803
+		$success         = $wpdb->insert($wpdb->prefix . "esp_venue_meta", $insertion_array, $data_types);
804
+		if (! $success) {
805
+			$this->add_error(
806
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
807
+					$this->_old_table,
808
+					$old_event,
809
+					$this->_new_meta_table,
810
+					$insertion_array,
811
+					$data_types
812
+				)
813
+			);
814
+		}
815
+	}
816
+
817
+
818
+	/**
819
+	 * @param $new_event_id
820
+	 * @param $new_venue_id
821
+	 * @return void
822
+	 */
823
+	private function _insert_new_venue_to_event($new_event_id, $new_venue_id): void
824
+	{
825
+		global $wpdb;
826
+		if (! $new_event_id) {
827
+			$this->add_error(
828
+				sprintf(esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"), $new_event_id)
829
+			);
830
+			return;
831
+		}
832
+		if (! $new_venue_id) {
833
+			$this->add_error(
834
+				sprintf(esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"), $new_venue_id)
835
+			);
836
+			return;
837
+		}
838
+		$cols_n_values = [
839
+			'EVT_ID'      => $new_event_id,
840
+			'VNU_ID'      => $new_venue_id,
841
+			'EVV_primary' => true,
842
+		];
843
+		$data_types    = [
844
+			'%d',// EVT_ID
845
+			'%d',// VNU_ID
846
+			'%d',// EVT_primary
847
+		];
848
+		$success       = $wpdb->insert($wpdb->prefix . "esp_event_venue", $cols_n_values, $data_types);
849
+		if (! $success) {
850
+			$this->add_error(
851
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
852
+					$this->_old_table,
853
+					[],
854
+					$this->_new_table,
855
+					$cols_n_values,
856
+					$data_types
857
+				)
858
+			);
859
+		}
860
+	}
861
+
862
+
863
+	/**
864
+	 * Converts all the 3.1 start-end times for the event to 4.1 datetimes
865
+	 *
866
+	 * @param array $old_event  results of get_results(...,ARRAY_A)
867
+	 * @param int   $new_cpt_id new post ID
868
+	 * @return void (if there are errors though, adds them to the stage's error list
869
+	 * @throws EE_Error
870
+	 * @global wpdb $wpdb
871
+	 */
872
+	private function _convert_start_end_times(array $old_event, int $new_cpt_id)
873
+	{
874
+		$start_end_times = $this->_get_old_start_end_times($old_event['id']);
875
+		foreach ($start_end_times as $start_end_time) {
876
+			$datetime_id = $this->_insert_new_datetime($start_end_time, $old_event, $new_cpt_id);
877
+			if ($datetime_id) {
878
+				$this->get_migration_script()->set_mapping(
879
+					$this->_old_start_end_table,
880
+					$start_end_time['id'],
881
+					$this->_new_datetime_table,
882
+					$datetime_id
883
+				);
884
+			}
885
+		}
886
+	}
887
+
888
+
889
+	/**
890
+	 * Queries the 3.1 wp_events_start_end table to get all the start and end times for the event
891
+	 *
892
+	 * @param int   $old_event_id
893
+	 * @return array
894
+	 * @global wpdb $wpdb
895
+	 */
896
+	private function _get_old_start_end_times(int $old_event_id): array
897
+	{
898
+		global $wpdb;
899
+		return $wpdb->get_results(
900
+			$wpdb->prepare("SELECT * FROM $this->_old_start_end_table WHERE event_id=%d", $old_event_id),
901
+			ARRAY_A
902
+		);
903
+	}
904
+
905
+
906
+	/**
907
+	 * Inserts a 4.1 datetime given the 3.1 start_end db row and event_details row
908
+	 *
909
+	 * @param array $start_end_time_row
910
+	 * @param array $old_event_row
911
+	 * @param int   $new_cpt_id
912
+	 * @return int ID of new datetime
913
+	 */
914
+	private function _insert_new_datetime(array $start_end_time_row, array $old_event_row, int $new_cpt_id): int
915
+	{
916
+		global $wpdb;
917
+		$start_date         = $old_event_row['start_date'];
918
+		$start_time         = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['start_time']);
919
+		$end_date           = $old_event_row['end_date'];
920
+		$end_time           = $this->get_migration_script()->convertTimeFromAMPM($start_end_time_row['end_time']);
921
+		$existing_datetimes = $this->_count_other_datetimes_exist_for_new_event($new_cpt_id);
922
+		$start_datetime_utc =
923
+			$this->get_migration_script()->convert_date_string_to_utc(
924
+				$this,
925
+				$start_end_time_row,
926
+				"$start_date $start_time:00",
927
+				$old_event_row['timezone_string']
928
+			);
929
+		$end_datetime_utc   =
930
+			$this->get_migration_script()->convert_date_string_to_utc(
931
+				$this,
932
+				$start_end_time_row,
933
+				"$end_date $end_time:00",
934
+				$old_event_row['timezone_string']
935
+			);
936
+		$cols_n_values      = [
937
+			'EVT_ID'        => $new_cpt_id,
938
+			// EVT_ID
939
+			'DTT_EVT_start' => $start_datetime_utc,
940
+			// DTT_EVT_start
941
+			'DTT_EVT_end'   => $end_datetime_utc,
942
+			// DTT_EVT_end
943
+			'DTT_reg_limit' => intval($start_end_time_row['reg_limit']) ? $start_end_time_row['reg_limit']
944
+				: $old_event_row['reg_limit'],
945
+			// DTT_reg_limit
946
+			'DTT_sold'      => 0,
947
+			// note: we will increment this as registrations are added during the migration
948
+			//          'DTT_is_primary'=> 0 == $existing_datetimes ,//DTT_is_primary... if count==0, then we'll call it the 'primary'
949
+			'DTT_order'     => $existing_datetimes,
950
+			// DTT_order, just give it the same order as the count of how many datetimes already exist
951
+			'DTT_parent'    => 0,
952
+			'DTT_deleted'   => false,
953
+		];
954
+		$data_types         = [
955
+			'%d',// EVT_Id
956
+			'%s',// DTT_EVT_start
957
+			'%s',// DTT_EVT_end
958
+			'%d',// DTT_reg_limit
959
+			'%d',// DTT_sold
960
+			//          '%d',//DTT_is_primary
961
+			'%d',// DTT_order
962
+			'%d',// DTT_parent
963
+			'%d',// DTT_deleted
964
+		];
965
+		$success            = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
966
+		if (! $success) {
967
+			$this->add_error(
968
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
969
+					$this->_old_start_end_table,
970
+					array_merge($old_event_row, $start_end_time_row),
971
+					$this->_new_datetime_table,
972
+					$cols_n_values,
973
+					$data_types
974
+				)
975
+			);
976
+			return 0;
977
+		}
978
+		return $wpdb->insert_id;
979
+	}
980
+
981
+
982
+	/**
983
+	 * Checks if there's a 4.1 datetime for this event already. This is mostly only handy
984
+	 * when deciding whether a datetime we're about ot insert should be the 'primary' or not
985
+	 *
986
+	 * @param int   $cpt_event_id
987
+	 * @return int
988
+	 * @global wpdb $wpdb
989
+	 */
990
+	private function _count_other_datetimes_exist_for_new_event(int $cpt_event_id): int
991
+	{
992
+		global $wpdb;
993
+		return (int) $wpdb->get_var(
994
+			$wpdb->prepare(
995
+				"SELECT COUNT(*) FROM $this->_new_datetime_table WHERE EVT_ID=%d",
996
+				$cpt_event_id
997
+			)
998
+		);
999
+	}
1000 1000
 }
Please login to merge, or discard this patch.
Spacing   +143 added lines, -143 removed lines patch added patch discarded remove patch
@@ -189,11 +189,11 @@  discard block
 block discarded – undo
189 189
     public function __construct()
190 190
     {
191 191
         global $wpdb;
192
-        $this->_old_table           = $wpdb->prefix . "events_detail";
193
-        $this->_old_start_end_table = $wpdb->prefix . "events_start_end";
194
-        $this->_new_table           = $wpdb->prefix . "posts";
195
-        $this->_new_meta_table      = $wpdb->prefix . "esp_event_meta";
196
-        $this->_new_datetime_table  = $wpdb->prefix . "esp_datetime";
192
+        $this->_old_table           = $wpdb->prefix."events_detail";
193
+        $this->_old_start_end_table = $wpdb->prefix."events_start_end";
194
+        $this->_new_table           = $wpdb->prefix."posts";
195
+        $this->_new_meta_table      = $wpdb->prefix."esp_event_meta";
196
+        $this->_new_datetime_table  = $wpdb->prefix."esp_datetime";
197 197
         $this->_pretty_name         = esc_html__("Events", "event_espresso");
198 198
         parent::__construct();
199 199
     }
@@ -207,7 +207,7 @@  discard block
 block discarded – undo
207 207
     public function _count_records_to_migrate(): int
208 208
     {
209 209
         global $wpdb;
210
-        return (int) $wpdb->get_var("SELECT COUNT(*) FROM " . $this->_old_table . ' WHERE event_status !="D"');
210
+        return (int) $wpdb->get_var("SELECT COUNT(*) FROM ".$this->_old_table.' WHERE event_status !="D"');
211 211
     }
212 212
 
213 213
 
@@ -300,17 +300,17 @@  discard block
 block discarded – undo
300 300
     private function _add_post_metas(array $old_event, int $post_id)
301 301
     {
302 302
         $event_meta = maybe_unserialize($old_event['event_meta']);
303
-        if (! $event_meta || ! is_array($event_meta)) {
303
+        if ( ! $event_meta || ! is_array($event_meta)) {
304 304
             return;
305 305
         }
306
-        unset($event_meta['date_submitted']);              // factored into CPT
307
-        unset($event_meta['additional_attendee_reg_info']);// factored into event meta table
308
-        unset($event_meta['default_payment_status']);      // dido
309
-        unset($event_meta['event_thumbnail_url']);         // used to find post featured image
306
+        unset($event_meta['date_submitted']); // factored into CPT
307
+        unset($event_meta['additional_attendee_reg_info']); // factored into event meta table
308
+        unset($event_meta['default_payment_status']); // dido
309
+        unset($event_meta['event_thumbnail_url']); // used to find post featured image
310 310
         foreach ($event_meta as $meta_key => $meta_value) {
311 311
             if ($meta_key) {// if the meta key is just an empty string, ignore it
312 312
                 $success = add_post_meta($post_id, $meta_key, $meta_value, true);
313
-                if (! $success) {
313
+                if ( ! $success) {
314 314
                     $this->add_error(
315 315
                         sprintf(
316 316
                             esc_html__(
@@ -412,63 +412,63 @@  discard block
 block discarded – undo
412 412
             // @todo: is it ok to just mark secondary/waitlist events as DRAFTS?
413 413
             'D'  => 'trash',
414 414
         ];
415
-        $post_status        = $status_conversions[ $old_event['event_status'] ];
415
+        $post_status = $status_conversions[$old_event['event_status']];
416 416
         // check if we've sold out
417 417
         if (intval($old_event['reg_limit']) <= self::count_registrations($old_event['id'])) {
418 418
             $post_status = 'sold_out';
419 419
         }
420 420
         //      FYI postponed and cancelled don't exist in 3.1
421
-        $cols_n_values                      = [
422
-            'post_title'        => stripslashes($old_event['event_name']),// EVT_name
423
-            'post_content'      => stripslashes($old_event['event_desc']),// EVT_desc
421
+        $cols_n_values = [
422
+            'post_title'        => stripslashes($old_event['event_name']), // EVT_name
423
+            'post_content'      => stripslashes($old_event['event_desc']), // EVT_desc
424 424
             'post_name'         => $this->_find_unique_slug(
425 425
                 $old_event['event_name'],
426 426
                 $old_event['event_identifier'],
427 427
                 $post_status
428
-            ),// EVT_slug
429
-            'post_date'         => $old_event['submitted'],// EVT_created NOT
428
+            ), // EVT_slug
429
+            'post_date'         => $old_event['submitted'], // EVT_created NOT
430 430
             'post_date_gmt'     => get_gmt_from_date($old_event['submitted']),
431
-            'post_excerpt'      => '',// EVT_short_desc
432
-            'post_modified'     => $old_event['submitted'],// EVT_modified
431
+            'post_excerpt'      => '', // EVT_short_desc
432
+            'post_modified'     => $old_event['submitted'], // EVT_modified
433 433
             'post_modified_gmt' => get_gmt_from_date($old_event['submitted']),
434
-            'post_author'       => $old_event['wp_user'],// EVT_wp_user
435
-            'post_parent'       => 0,// parent maybe get this from some REM field?
436
-            'menu_order'        => 0,// EVT_order
437
-            'post_type'         => EspressoPostType::EVENTS,// post_type
438
-            'post_status'       => $post_status,// status
434
+            'post_author'       => $old_event['wp_user'], // EVT_wp_user
435
+            'post_parent'       => 0, // parent maybe get this from some REM field?
436
+            'menu_order'        => 0, // EVT_order
437
+            'post_type'         => EspressoPostType::EVENTS, // post_type
438
+            'post_status'       => $post_status, // status
439 439
         ];
440 440
         $cols_n_values_with_no_invalid_text = [];
441 441
         foreach ($cols_n_values as $col => $value) {
442 442
             $value_sans_invalid_chars = $wpdb->strip_invalid_text_for_column($this->_new_table, $col, $value);
443
-            if (! is_wp_error($value_sans_invalid_chars)) {
444
-                $cols_n_values_with_no_invalid_text[ $col ] = $value_sans_invalid_chars;
443
+            if ( ! is_wp_error($value_sans_invalid_chars)) {
444
+                $cols_n_values_with_no_invalid_text[$col] = $value_sans_invalid_chars;
445 445
             } else {
446 446
                 // otherwise leave it as-is. It will blow everything up and stop the migration
447
-                $cols_n_values_with_no_invalid_text[ $col ] = $value;
447
+                $cols_n_values_with_no_invalid_text[$col] = $value;
448 448
             }
449 449
         }
450 450
         $cols_n_values = $cols_n_values_with_no_invalid_text;
451 451
         $data_types    = [
452
-            '%s',// EVT_name
453
-            '%s',// EVT_desc
454
-            '%s',// EVT_slug
455
-            '%s',// EVT_created
452
+            '%s', // EVT_name
453
+            '%s', // EVT_desc
454
+            '%s', // EVT_slug
455
+            '%s', // EVT_created
456 456
             '%s',
457
-            '%s',// EVT_short_desc
458
-            '%s',// EVT_modified
457
+            '%s', // EVT_short_desc
458
+            '%s', // EVT_modified
459 459
             '%s',
460
-            '%s',// EVT_wp_user
461
-            '%d',// post_parent
462
-            '%d',// EVT_order
463
-            '%s',// post_type
464
-            '%s',// status
460
+            '%s', // EVT_wp_user
461
+            '%d', // post_parent
462
+            '%d', // EVT_order
463
+            '%s', // post_type
464
+            '%s', // status
465 465
         ];
466
-        $success       = $wpdb->insert(
466
+        $success = $wpdb->insert(
467 467
             $this->_new_table,
468 468
             $cols_n_values,
469 469
             $data_types
470 470
         );
471
-        if (! $success) {
471
+        if ( ! $success) {
472 472
             $this->add_error(
473 473
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
474 474
                     $this->_old_table,
@@ -521,8 +521,8 @@  discard block
 block discarded – undo
521 521
                 $event_meta['default_payment_status'] ?? '',
522 522
                 intval($old_event['require_pre_approval'])
523 523
             );
524
-        $cols_n_values      = [
525
-            'EVT_ID'                          => $new_cpt_id,// EVT_ID_fk
524
+        $cols_n_values = [
525
+            'EVT_ID'                          => $new_cpt_id, // EVT_ID_fk
526 526
             'EVT_display_desc'                => 'Y' == $old_event['display_desc'],
527 527
             'EVT_display_ticket_selector'     => 'Y' == $old_event['display_reg_form'],
528 528
             'EVT_visible_on'                  => $this->get_migration_script()->convert_date_string_to_utc(
@@ -532,7 +532,7 @@  discard block
 block discarded – undo
532 532
                     'mysql'
533 533
                 ),
534 534
                 $old_event['timezone_string']
535
-            ),// don't use the old 'visible_on', as it wasn't ever used
535
+            ), // don't use the old 'visible_on', as it wasn't ever used
536 536
             'EVT_additional_limit'            => $old_event['allow_multiple'] == 'N' ? 1
537 537
                 : $old_event['additional_limit'],
538 538
             'EVT_default_registration_status' => $default_reg_status,
@@ -541,29 +541,29 @@  discard block
 block discarded – undo
541 541
             'EVT_allow_overflow'              => 'Y' == $old_event['allow_overflow'],
542 542
             'EVT_timezone_string'             => $old_event['timezone_string'],
543 543
             'EVT_external_URL'                => $old_event['externalURL'],
544
-            'EVT_donations'                   => false,// doesn't exist in 3.1
544
+            'EVT_donations'                   => false, // doesn't exist in 3.1
545 545
 
546 546
         ];
547
-        $data_types         = [
548
-            '%s',// EVT_ID
549
-            '%d',// EVT_display_desc
550
-            '%d',// EVT_display_ticket_selector
551
-            '%s',// EVT_visible_on
552
-            '%d',// EVT_additional_limit
553
-            '%s',// EVT_default_registration_status
554
-            '%d',// EVT_member_only
555
-            '%s',// EVT_phone
556
-            '%d',// EVT_allow_overflow
557
-            '%s',// EVT_timezone_string
558
-            '%s',// EVT_external_URL
559
-            '%d',// EVT_donations
547
+        $data_types = [
548
+            '%s', // EVT_ID
549
+            '%d', // EVT_display_desc
550
+            '%d', // EVT_display_ticket_selector
551
+            '%s', // EVT_visible_on
552
+            '%d', // EVT_additional_limit
553
+            '%s', // EVT_default_registration_status
554
+            '%d', // EVT_member_only
555
+            '%s', // EVT_phone
556
+            '%d', // EVT_allow_overflow
557
+            '%s', // EVT_timezone_string
558
+            '%s', // EVT_external_URL
559
+            '%d', // EVT_donations
560 560
         ];
561
-        $success            = $wpdb->insert(
561
+        $success = $wpdb->insert(
562 562
             $this->_new_meta_table,
563 563
             $cols_n_values,
564 564
             $data_types
565 565
         );
566
-        if (! $success) {
566
+        if ( ! $success) {
567 567
             $this->add_error(
568 568
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
569 569
                     $this->_old_table,
@@ -632,16 +632,16 @@  discard block
 block discarded – undo
632 632
     private function _duplicate_venue_exists(array $old_event): int
633 633
     {
634 634
         global $wpdb;
635
-        $conditions     = [
635
+        $conditions = [
636 636
             'VNU_address'       => $old_event ['address'],
637 637
             'VNU_address2'      => $old_event['address2'],
638 638
             'VNU_city'          => $old_event['city'],
639 639
             'VNU_zip'           => $old_event['zip'],
640 640
             'post_title'        => $this->_get_venue_title_for_event($old_event),
641
-            'VNU_phone'         => $old_event['venue_phone'],// VNU_phone
642
-            'VNU_url'           => $old_event['venue_url'],// VNU_url
643
-            'VNU_virtual_phone' => $old_event['virtual_phone'],// VNU_virtual_phone
644
-            'VNU_virtual_url'   => $old_event['virtual_url'],// VNU_virtual_url
641
+            'VNU_phone'         => $old_event['venue_phone'], // VNU_phone
642
+            'VNU_url'           => $old_event['venue_url'], // VNU_url
643
+            'VNU_virtual_phone' => $old_event['virtual_phone'], // VNU_virtual_phone
644
+            'VNU_virtual_url'   => $old_event['virtual_url'], // VNU_virtual_url
645 645
         ];
646 646
         $sql_conditions = [];
647 647
         foreach ($conditions as $column => $value) {
@@ -651,7 +651,7 @@  discard block
 block discarded – undo
651 651
 					FROM
652 652
 		$wpdb->posts as p INNER JOIN
653 653
 		{$wpdb->prefix}esp_venue_meta as v ON p.ID = v.VNU_ID
654
-			WHERE " . implode(" AND ", $sql_conditions) . " LIMIT 1";
654
+			WHERE ".implode(" AND ", $sql_conditions)." LIMIT 1";
655 655
         return (int) $wpdb->get_var($query);
656 656
     }
657 657
 
@@ -684,43 +684,43 @@  discard block
 block discarded – undo
684 684
     private function _insert_venue_into_posts(array $old_event): int
685 685
     {
686 686
         global $wpdb;
687
-        $insertion_array  = [
688
-            'post_title'        => $this->_get_venue_title_for_event($old_event),// VNU_name
689
-            'post_content'      => '',// VNU_desc
687
+        $insertion_array = [
688
+            'post_title'        => $this->_get_venue_title_for_event($old_event), // VNU_name
689
+            'post_content'      => '', // VNU_desc
690 690
             'post_name'         => $this->_find_unique_slug(
691 691
                 $old_event['venue_title'],
692
-                sanitize_title('venue-of-' . $old_event['event_name'])
693
-            ),// VNU_identifier
694
-            'post_date'         => current_time('mysql'),// VNU_created
692
+                sanitize_title('venue-of-'.$old_event['event_name'])
693
+            ), // VNU_identifier
694
+            'post_date'         => current_time('mysql'), // VNU_created
695 695
             'post_date_gmt'     => get_gmt_from_date(current_time('mysql')),
696
-            'post_excerpt'      => '',// VNU_short_desc arbitrary only 50 characters
697
-            'post_modified'     => current_time('mysql'),// VNU_modified
696
+            'post_excerpt'      => '', // VNU_short_desc arbitrary only 50 characters
697
+            'post_modified'     => current_time('mysql'), // VNU_modified
698 698
             'post_modified_gmt' => get_gmt_from_date(current_time('mysql')),
699
-            'post_author'       => $old_event['wp_user'],// VNU_wp_user
700
-            'post_parent'       => 0,// parent
701
-            'menu_order'        => 0,// VNU_order
702
-            'post_type'         => EspressoPostType::VENUES,// post_type
699
+            'post_author'       => $old_event['wp_user'], // VNU_wp_user
700
+            'post_parent'       => 0, // parent
701
+            'menu_order'        => 0, // VNU_order
702
+            'post_type'         => EspressoPostType::VENUES, // post_type
703 703
         ];
704 704
         $data_types_array = [
705
-            '%s',// VNU_name
706
-            '%s',// VNU_desc
707
-            '%s',// VNU_identifier
708
-            '%s',// VNU_created
705
+            '%s', // VNU_name
706
+            '%s', // VNU_desc
707
+            '%s', // VNU_identifier
708
+            '%s', // VNU_created
709 709
             '%s',
710
-            '%s',// VNU_short_desc
711
-            '%s',// VNU_modified
710
+            '%s', // VNU_short_desc
711
+            '%s', // VNU_modified
712 712
             '%s',
713
-            '%d',// VNU_wp_user
714
-            '%d',// parent
715
-            '%d',// VNU_order
716
-            '%s',// post_type
713
+            '%d', // VNU_wp_user
714
+            '%d', // parent
715
+            '%d', // VNU_order
716
+            '%s', // post_type
717 717
         ];
718
-        $success          = $wpdb->insert(
718
+        $success = $wpdb->insert(
719 719
             $wpdb->posts,
720 720
             $insertion_array,
721 721
             $data_types_array
722 722
         );
723
-        if (! $success) {
723
+        if ( ! $success) {
724 724
             $this->add_error(
725 725
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
726 726
                     $this->_old_table,
@@ -750,13 +750,13 @@  discard block
 block discarded – undo
750 750
         // assume the country is the same as the organization's old settings
751 751
         $country_iso = $this->get_migration_script()->get_default_country_iso();
752 752
         // find the state from the venue, or the organization, or just guess california
753
-        if (! $old_event['state']) {
753
+        if ( ! $old_event['state']) {
754 754
             $old_org_options = get_option('events_organization_settings');
755 755
             $state_name      = stripslashes($old_org_options['organization_state']);
756 756
         } else {
757 757
             $state_name = $old_event['state'];
758 758
         }
759
-        if (! $state_name) {
759
+        if ( ! $state_name) {
760 760
             $state_name = 'CA';
761 761
         }
762 762
         // get a state ID with the same name, if possible
@@ -769,39 +769,39 @@  discard block
 block discarded – undo
769 769
         }
770 770
         // now insert into meta table
771 771
         $insertion_array = [
772
-            'VNU_ID'              => $cpt_id,// VNU_ID_fk
773
-            'VNU_address'         => stripslashes($old_event['address']),// VNU_address
774
-            'VNU_address2'        => stripslashes($old_event['address2']),// VNU_address2
775
-            'VNU_city'            => stripslashes($old_event['city']),// VNU_city
776
-            'STA_ID'              => $state_id,// STA_ID
777
-            'CNT_ISO'             => $country_iso,// CNT_ISO
778
-            'VNU_zip'             => $old_event['zip'],// VNU_zip
779
-            'VNU_phone'           => $old_event['venue_phone'],// VNU_phone
780
-            'VNU_capacity'        => -1,// VNU_capacity
781
-            'VNU_url'             => $old_event['venue_url'],// VNU_url
782
-            'VNU_virtual_phone'   => $old_event['virtual_phone'],// VNU_virtual_phone
783
-            'VNU_virtual_url'     => $old_event['virtual_url'],// VNU_virtual_url
784
-            'VNU_google_map_link' => '',// VNU_google_map_link
772
+            'VNU_ID'              => $cpt_id, // VNU_ID_fk
773
+            'VNU_address'         => stripslashes($old_event['address']), // VNU_address
774
+            'VNU_address2'        => stripslashes($old_event['address2']), // VNU_address2
775
+            'VNU_city'            => stripslashes($old_event['city']), // VNU_city
776
+            'STA_ID'              => $state_id, // STA_ID
777
+            'CNT_ISO'             => $country_iso, // CNT_ISO
778
+            'VNU_zip'             => $old_event['zip'], // VNU_zip
779
+            'VNU_phone'           => $old_event['venue_phone'], // VNU_phone
780
+            'VNU_capacity'        => -1, // VNU_capacity
781
+            'VNU_url'             => $old_event['venue_url'], // VNU_url
782
+            'VNU_virtual_phone'   => $old_event['virtual_phone'], // VNU_virtual_phone
783
+            'VNU_virtual_url'     => $old_event['virtual_url'], // VNU_virtual_url
784
+            'VNU_google_map_link' => '', // VNU_google_map_link
785 785
             'VNU_enable_for_gmap' => true, // VNU_enable_for_gmap
786 786
         ];
787
-        $data_types      = [
788
-            '%d',// VNU_ID_fk
789
-            '%s',// VNU_address
790
-            '%s',// VNU_address2
791
-            '%s',// VNU_city
792
-            '%d',// STA_ID
793
-            '%s',// CNT_ISO
794
-            '%s',// VNU_zip
795
-            '%s',// VNU_phone
796
-            '%d',// VNU_capacity
797
-            '%s',// VNU_url
798
-            '%s',// VNU_virtual_phone
799
-            '%s',// VNU_virtual_url
800
-            '%s',// VNU_google_map_link
801
-            '%d',// VNU_enable_for_gmap
787
+        $data_types = [
788
+            '%d', // VNU_ID_fk
789
+            '%s', // VNU_address
790
+            '%s', // VNU_address2
791
+            '%s', // VNU_city
792
+            '%d', // STA_ID
793
+            '%s', // CNT_ISO
794
+            '%s', // VNU_zip
795
+            '%s', // VNU_phone
796
+            '%d', // VNU_capacity
797
+            '%s', // VNU_url
798
+            '%s', // VNU_virtual_phone
799
+            '%s', // VNU_virtual_url
800
+            '%s', // VNU_google_map_link
801
+            '%d', // VNU_enable_for_gmap
802 802
         ];
803
-        $success         = $wpdb->insert($wpdb->prefix . "esp_venue_meta", $insertion_array, $data_types);
804
-        if (! $success) {
803
+        $success = $wpdb->insert($wpdb->prefix."esp_venue_meta", $insertion_array, $data_types);
804
+        if ( ! $success) {
805 805
             $this->add_error(
806 806
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
807 807
                     $this->_old_table,
@@ -823,13 +823,13 @@  discard block
 block discarded – undo
823 823
     private function _insert_new_venue_to_event($new_event_id, $new_venue_id): void
824 824
     {
825 825
         global $wpdb;
826
-        if (! $new_event_id) {
826
+        if ( ! $new_event_id) {
827 827
             $this->add_error(
828 828
                 sprintf(esc_html__("Could not find 4.1 event id for 3.1 event #%d.", "event_espresso"), $new_event_id)
829 829
             );
830 830
             return;
831 831
         }
832
-        if (! $new_venue_id) {
832
+        if ( ! $new_venue_id) {
833 833
             $this->add_error(
834 834
                 sprintf(esc_html__("Could not find 4.1 venue id for 3.1 venue #%d.", "event_espresso"), $new_venue_id)
835 835
             );
@@ -840,13 +840,13 @@  discard block
 block discarded – undo
840 840
             'VNU_ID'      => $new_venue_id,
841 841
             'EVV_primary' => true,
842 842
         ];
843
-        $data_types    = [
844
-            '%d',// EVT_ID
845
-            '%d',// VNU_ID
846
-            '%d',// EVT_primary
843
+        $data_types = [
844
+            '%d', // EVT_ID
845
+            '%d', // VNU_ID
846
+            '%d', // EVT_primary
847 847
         ];
848
-        $success       = $wpdb->insert($wpdb->prefix . "esp_event_venue", $cols_n_values, $data_types);
849
-        if (! $success) {
848
+        $success = $wpdb->insert($wpdb->prefix."esp_event_venue", $cols_n_values, $data_types);
849
+        if ( ! $success) {
850 850
             $this->add_error(
851 851
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
852 852
                     $this->_old_table,
@@ -926,14 +926,14 @@  discard block
 block discarded – undo
926 926
                 "$start_date $start_time:00",
927 927
                 $old_event_row['timezone_string']
928 928
             );
929
-        $end_datetime_utc   =
929
+        $end_datetime_utc =
930 930
             $this->get_migration_script()->convert_date_string_to_utc(
931 931
                 $this,
932 932
                 $start_end_time_row,
933 933
                 "$end_date $end_time:00",
934 934
                 $old_event_row['timezone_string']
935 935
             );
936
-        $cols_n_values      = [
936
+        $cols_n_values = [
937 937
             'EVT_ID'        => $new_cpt_id,
938 938
             // EVT_ID
939 939
             'DTT_EVT_start' => $start_datetime_utc,
@@ -951,19 +951,19 @@  discard block
 block discarded – undo
951 951
             'DTT_parent'    => 0,
952 952
             'DTT_deleted'   => false,
953 953
         ];
954
-        $data_types         = [
955
-            '%d',// EVT_Id
956
-            '%s',// DTT_EVT_start
957
-            '%s',// DTT_EVT_end
958
-            '%d',// DTT_reg_limit
959
-            '%d',// DTT_sold
954
+        $data_types = [
955
+            '%d', // EVT_Id
956
+            '%s', // DTT_EVT_start
957
+            '%s', // DTT_EVT_end
958
+            '%d', // DTT_reg_limit
959
+            '%d', // DTT_sold
960 960
             //          '%d',//DTT_is_primary
961
-            '%d',// DTT_order
962
-            '%d',// DTT_parent
963
-            '%d',// DTT_deleted
961
+            '%d', // DTT_order
962
+            '%d', // DTT_parent
963
+            '%d', // DTT_deleted
964 964
         ];
965
-        $success            = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
966
-        if (! $success) {
965
+        $success = $wpdb->insert($this->_new_datetime_table, $cols_n_values, $data_types);
966
+        if ( ! $success) {
967 967
             $this->add_error(
968 968
                 $this->get_migration_script()->_create_error_message_for_db_insertion(
969 969
                     $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 1 patch
Indentation   +144 added lines, -144 removed lines patch added patch discarded remove patch
@@ -66,159 +66,159 @@
 block discarded – undo
66 66
  */
67 67
 class EE_DMS_4_1_0_questions extends EE_Data_Migration_Script_Stage
68 68
 {
69
-    private string $_option_table;
69
+	private string $_option_table;
70 70
 
71 71
 
72
-    public function __construct()
73
-    {
74
-        global $wpdb;
75
-        $this->_pretty_name  = esc_html__("Questions", "event_espresso");
76
-        $this->_old_table    = $wpdb->prefix . "events_question";
77
-        $this->_new_table    = $wpdb->prefix . "esp_question";
78
-        $this->_option_table = $wpdb->prefix . "esp_question_option";
79
-        parent::__construct();
80
-    }
72
+	public function __construct()
73
+	{
74
+		global $wpdb;
75
+		$this->_pretty_name  = esc_html__("Questions", "event_espresso");
76
+		$this->_old_table    = $wpdb->prefix . "events_question";
77
+		$this->_new_table    = $wpdb->prefix . "esp_question";
78
+		$this->_option_table = $wpdb->prefix . "esp_question_option";
79
+		parent::__construct();
80
+	}
81 81
 
82 82
 
83
-    /**
84
-     * @throws EE_Error
85
-     */
86
-    public function _migration_step($num_items_to_migrate = 50)
87
-    {
88
-        global $wpdb;
89
-        $start_at_record         = $this->count_records_migrated();
90
-        $rows                    = $wpdb->get_results(
91
-            $wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items_to_migrate),
92
-            ARRAY_A
93
-        );
94
-        $items_actually_migrated = 0;
95
-        foreach ($rows as $question_row) {
96
-            $new_id = $this->_insert_new_question($question_row);
97
-            $this->get_migration_script()->set_mapping(
98
-                $this->_old_table,
99
-                $question_row['id'],
100
-                $this->_new_table,
101
-                $new_id
102
-            );
103
-            $items_actually_migrated++;
104
-        }
105
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
106
-            $this->set_completed();
107
-        }
108
-        return $items_actually_migrated;
109
-    }
83
+	/**
84
+	 * @throws EE_Error
85
+	 */
86
+	public function _migration_step($num_items_to_migrate = 50)
87
+	{
88
+		global $wpdb;
89
+		$start_at_record         = $this->count_records_migrated();
90
+		$rows                    = $wpdb->get_results(
91
+			$wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d", $start_at_record, $num_items_to_migrate),
92
+			ARRAY_A
93
+		);
94
+		$items_actually_migrated = 0;
95
+		foreach ($rows as $question_row) {
96
+			$new_id = $this->_insert_new_question($question_row);
97
+			$this->get_migration_script()->set_mapping(
98
+				$this->_old_table,
99
+				$question_row['id'],
100
+				$this->_new_table,
101
+				$new_id
102
+			);
103
+			$items_actually_migrated++;
104
+		}
105
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
106
+			$this->set_completed();
107
+		}
108
+		return $items_actually_migrated;
109
+	}
110 110
 
111 111
 
112
-    public function _count_records_to_migrate()
113
-    {
114
-        global $wpdb;
115
-        return $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
116
-    }
112
+	public function _count_records_to_migrate()
113
+	{
114
+		global $wpdb;
115
+		return $wpdb->get_var("SELECT COUNT(id) FROM " . $this->_old_table);
116
+	}
117 117
 
118 118
 
119
-    private function _insert_new_question($old_question): int
120
-    {
121
-        global $wpdb;
122
-        // if this pretends to be a 'system' question, check if we already have a
123
-        // system question for that string. If so, pretend THAT new question
124
-        // is what we just inserted
125
-        if ($old_question['system_name']) {
126
-            $id_of_new_system_question = intval(
127
-                $wpdb->get_var(
128
-                    $wpdb->prepare(
129
-                        "SELECT QST_ID FROM $this->_new_table WHERE QST_system = %s",
130
-                        $old_question['system_name']
131
-                    )
132
-                )
133
-            );
134
-            if ($id_of_new_system_question) {
135
-                return $id_of_new_system_question;
136
-            }
137
-            // ok so this must be the first one. Carry on.
138
-        }
119
+	private function _insert_new_question($old_question): int
120
+	{
121
+		global $wpdb;
122
+		// if this pretends to be a 'system' question, check if we already have a
123
+		// system question for that string. If so, pretend THAT new question
124
+		// is what we just inserted
125
+		if ($old_question['system_name']) {
126
+			$id_of_new_system_question = intval(
127
+				$wpdb->get_var(
128
+					$wpdb->prepare(
129
+						"SELECT QST_ID FROM $this->_new_table WHERE QST_system = %s",
130
+						$old_question['system_name']
131
+					)
132
+				)
133
+			);
134
+			if ($id_of_new_system_question) {
135
+				return $id_of_new_system_question;
136
+			}
137
+			// ok so this must be the first one. Carry on.
138
+		}
139 139
 
140
-        $cols_n_values = [
141
-            'QST_display_text'  => stripslashes($old_question['question']),
142
-            'QST_admin_label'   => sanitize_title($old_question['question'] ?: ''),
143
-            'QST_system'        => (string) $old_question['system_name'],
144
-            'QST_type'          => $old_question['question_type'],
145
-            'QST_required'      => 'Y' == $old_question['required'],
146
-            'QST_required_text' => stripslashes($old_question['required_text'] ?: ''),
147
-            'QST_order'         => $old_question['sequence'],
148
-            'QST_admin_only'    => 'Y' == $old_question['admin_only'],
149
-            'QST_wp_user'       => $old_question['wp_user'],
150
-            'QST_deleted'       => false,
151
-        ];
152
-        $data_types    = [
153
-            '%s',// QST_display_text
154
-            '%s',// QST-admin_label
155
-            '%s',// QST_system
156
-            '%s',// QST_type
157
-            '%d',// QST_required
158
-            '%s',// QST_required_text
159
-            '%d',// QST_order
160
-            '%d',// QST_admin_only
161
-            '%d',// QST_wp_user
162
-            '%d',// QST_deleted
163
-        ];
164
-        $success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
165
-        if (! $success) {
166
-            $this->add_error(
167
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
168
-                    $this->_old_table,
169
-                    $old_question,
170
-                    $this->_new_table,
171
-                    $cols_n_values,
172
-                    $data_types
173
-                )
174
-            );
175
-            return 0;
176
-        }
177
-        $new_id = $wpdb->insert_id;
178
-        // now take care of possibly adding question options
179
-        if (in_array($old_question['question_type'], ['DROPDOWN', 'SINGLE', 'MULTIPLE'])) {
180
-            $options = explode(",", $old_question['response']);
181
-            foreach ($options as $option) {
182
-                $this->_insert_question_option($option, $new_id);
183
-            }
184
-        }
185
-        return $new_id;
186
-    }
140
+		$cols_n_values = [
141
+			'QST_display_text'  => stripslashes($old_question['question']),
142
+			'QST_admin_label'   => sanitize_title($old_question['question'] ?: ''),
143
+			'QST_system'        => (string) $old_question['system_name'],
144
+			'QST_type'          => $old_question['question_type'],
145
+			'QST_required'      => 'Y' == $old_question['required'],
146
+			'QST_required_text' => stripslashes($old_question['required_text'] ?: ''),
147
+			'QST_order'         => $old_question['sequence'],
148
+			'QST_admin_only'    => 'Y' == $old_question['admin_only'],
149
+			'QST_wp_user'       => $old_question['wp_user'],
150
+			'QST_deleted'       => false,
151
+		];
152
+		$data_types    = [
153
+			'%s',// QST_display_text
154
+			'%s',// QST-admin_label
155
+			'%s',// QST_system
156
+			'%s',// QST_type
157
+			'%d',// QST_required
158
+			'%s',// QST_required_text
159
+			'%d',// QST_order
160
+			'%d',// QST_admin_only
161
+			'%d',// QST_wp_user
162
+			'%d',// QST_deleted
163
+		];
164
+		$success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
165
+		if (! $success) {
166
+			$this->add_error(
167
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
168
+					$this->_old_table,
169
+					$old_question,
170
+					$this->_new_table,
171
+					$cols_n_values,
172
+					$data_types
173
+				)
174
+			);
175
+			return 0;
176
+		}
177
+		$new_id = $wpdb->insert_id;
178
+		// now take care of possibly adding question options
179
+		if (in_array($old_question['question_type'], ['DROPDOWN', 'SINGLE', 'MULTIPLE'])) {
180
+			$options = explode(",", $old_question['response']);
181
+			foreach ($options as $option) {
182
+				$this->_insert_question_option($option, $new_id);
183
+			}
184
+		}
185
+		return $new_id;
186
+	}
187 187
 
188 188
 
189
-    /**
190
-     * Adds a question option to the db
191
-     *
192
-     * @param string $option
193
-     * @param int    $question_id
194
-     * @return void
195
-     * @global wpdb  $wpdb
196
-     */
197
-    private function _insert_question_option(string $option, int $question_id): void
198
-    {
199
-        $option = trim($option);
200
-        global $wpdb;
201
-        $cols_n_values = [
202
-            'QST_ID'      => $question_id,
203
-            'QSO_value'   => $option,
204
-            'QSO_deleted' => false,
205
-        ];
206
-        $data_types    = [
207
-            '%d',// QST_ID
208
-            '%s',// QSO_value
209
-            '%d',// QSO_deleted
210
-        ];
211
-        $success       = $wpdb->insert($this->_option_table, $cols_n_values, $data_types);
212
-        if (! $success) {
213
-            $this->add_error(
214
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
215
-                    $this->_old_table,
216
-                    ['option' => $option, 'new_question_id' => $question_id],
217
-                    $this->_option_table,
218
-                    $cols_n_values,
219
-                    $data_types
220
-                )
221
-            );
222
-        }
223
-    }
189
+	/**
190
+	 * Adds a question option to the db
191
+	 *
192
+	 * @param string $option
193
+	 * @param int    $question_id
194
+	 * @return void
195
+	 * @global wpdb  $wpdb
196
+	 */
197
+	private function _insert_question_option(string $option, int $question_id): void
198
+	{
199
+		$option = trim($option);
200
+		global $wpdb;
201
+		$cols_n_values = [
202
+			'QST_ID'      => $question_id,
203
+			'QSO_value'   => $option,
204
+			'QSO_deleted' => false,
205
+		];
206
+		$data_types    = [
207
+			'%d',// QST_ID
208
+			'%s',// QSO_value
209
+			'%d',// QSO_deleted
210
+		];
211
+		$success       = $wpdb->insert($this->_option_table, $cols_n_values, $data_types);
212
+		if (! $success) {
213
+			$this->add_error(
214
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
215
+					$this->_old_table,
216
+					['option' => $option, 'new_question_id' => $question_id],
217
+					$this->_option_table,
218
+					$cols_n_values,
219
+					$data_types
220
+				)
221
+			);
222
+		}
223
+	}
224 224
 }
Please login to merge, or discard this patch.
core/data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_checkins.dmsstage.php 1 patch
Indentation   +165 added lines, -165 removed lines patch added patch discarded remove patch
@@ -26,186 +26,186 @@
 block discarded – undo
26 26
 
27 27
 class EE_DMS_4_1_0_checkins extends EE_Data_Migration_Script_Stage_Table
28 28
 {
29
-    public function __construct()
30
-    {
31
-        global $wpdb;
32
-        $this->_pretty_name      = esc_html__('Check-ins', 'event_espresso');
33
-        $this->_old_table        = $wpdb->prefix . "events_attendee";
34
-        $this->select_expression = 'att.*, e.event_status';
35
-        $this->_extra_where_sql  = 'AS att
29
+	public function __construct()
30
+	{
31
+		global $wpdb;
32
+		$this->_pretty_name      = esc_html__('Check-ins', 'event_espresso');
33
+		$this->_old_table        = $wpdb->prefix . "events_attendee";
34
+		$this->select_expression = 'att.*, e.event_status';
35
+		$this->_extra_where_sql  = 'AS att
36 36
             INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id
37 37
             WHERE e.event_status!="D"';
38
-        $this->_new_table        = $wpdb->prefix . "esp_checkin";
39
-        parent::__construct();
40
-    }
38
+		$this->_new_table        = $wpdb->prefix . "esp_checkin";
39
+		parent::__construct();
40
+	}
41 41
 
42 42
 
43
-    /**
44
-     * @throws EE_Error
45
-     */
46
-    protected function _migrate_old_row($old_row)
47
-    {
48
-        global $wpdb;
49
-        $new_reg_table = $wpdb->prefix . "esp_registration";
43
+	/**
44
+	 * @throws EE_Error
45
+	 */
46
+	protected function _migrate_old_row($old_row)
47
+	{
48
+		global $wpdb;
49
+		$new_reg_table = $wpdb->prefix . "esp_registration";
50 50
 
51
-        $num_to_checkin_at_this_time = max([intval($old_row['checked_in_quantity']), intval($old_row['checked_in'])]);
51
+		$num_to_checkin_at_this_time = max([intval($old_row['checked_in_quantity']), intval($old_row['checked_in'])]);
52 52
 
53
-        $new_registrations_for_attendee =
54
-            $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table);
55
-        if (! $new_registrations_for_attendee) {
56
-            $new_registrations_for_attendee = [];
57
-        }
58
-        $new_datetime = $this->_try_to_find_datetime($old_row);
53
+		$new_registrations_for_attendee =
54
+			$this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $new_reg_table);
55
+		if (! $new_registrations_for_attendee) {
56
+			$new_registrations_for_attendee = [];
57
+		}
58
+		$new_datetime = $this->_try_to_find_datetime($old_row);
59 59
 
60
-        // make sure registrations array is numerically indexed starting at 0 (it probably already is)
61
-        $new_registrations_for_attendee = array_values($new_registrations_for_attendee);
62
-        $new_checkin_ids                = [];
63
-        for ($i = 0; $i < abs($num_to_checkin_at_this_time); $i++) {
64
-            $new_reg_id = $new_registrations_for_attendee[ $i ];
65
-            if (! $new_reg_id) {
66
-                $this->add_error(
67
-                    sprintf(
68
-                        esc_html__(
69
-                        /* translators: %1$s database row represented in JSON, %2$s number of registrations to check in
60
+		// make sure registrations array is numerically indexed starting at 0 (it probably already is)
61
+		$new_registrations_for_attendee = array_values($new_registrations_for_attendee);
62
+		$new_checkin_ids                = [];
63
+		for ($i = 0; $i < abs($num_to_checkin_at_this_time); $i++) {
64
+			$new_reg_id = $new_registrations_for_attendee[ $i ];
65
+			if (! $new_reg_id) {
66
+				$this->add_error(
67
+					sprintf(
68
+						esc_html__(
69
+						/* translators: %1$s database row represented in JSON, %2$s number of registrations to check in
70 70
                         *  %3$s number of registrations for the attendee, %4$s new registration rows represented in JSON
71 71
                         */
72
-                        // @codingStandardsIgnoreStart
73
-                            'It appears we wanted to check-in more registrations than actually exist. The old attendee record (%1$s) indicated we should check-in %2$d registrations, but there are only %3$d registrations for that attendee (%4$s)',
74
-                            // @codingStandardsIgnoreEnd
75
-                            'event_espresso'
76
-                        ),
77
-                        $this->_json_encode($old_row),
78
-                        abs($num_to_checkin_at_this_time),
79
-                        count($new_registrations_for_attendee),
80
-                        $this->_json_encode($new_registrations_for_attendee)
81
-                    )
82
-                );
83
-                break;
84
-            }
85
-            $existing_checkin_record = $wpdb->get_var(
86
-                $wpdb->prepare(
87
-                    "SELECT CHK_ID FROM $this->_new_table WHERE REG_ID = %d ORDER BY CHK_ID DESC LIMIT 1",
88
-                    $new_reg_id
89
-                )
90
-            );
91
-            if (! $existing_checkin_record) {
92
-                $new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime);
93
-                if ($new_id) {
94
-                    $new_checkin_ids[] = $new_id;
95
-                }
96
-            }
97
-        }
98
-        if ($new_checkin_ids) {
99
-            $this->get_migration_script()->set_mapping(
100
-                $this->_old_table,
101
-                $old_row['id'],
102
-                $this->_new_table,
103
-                $new_checkin_ids
104
-            );
105
-        }
106
-    }
72
+						// @codingStandardsIgnoreStart
73
+							'It appears we wanted to check-in more registrations than actually exist. The old attendee record (%1$s) indicated we should check-in %2$d registrations, but there are only %3$d registrations for that attendee (%4$s)',
74
+							// @codingStandardsIgnoreEnd
75
+							'event_espresso'
76
+						),
77
+						$this->_json_encode($old_row),
78
+						abs($num_to_checkin_at_this_time),
79
+						count($new_registrations_for_attendee),
80
+						$this->_json_encode($new_registrations_for_attendee)
81
+					)
82
+				);
83
+				break;
84
+			}
85
+			$existing_checkin_record = $wpdb->get_var(
86
+				$wpdb->prepare(
87
+					"SELECT CHK_ID FROM $this->_new_table WHERE REG_ID = %d ORDER BY CHK_ID DESC LIMIT 1",
88
+					$new_reg_id
89
+				)
90
+			);
91
+			if (! $existing_checkin_record) {
92
+				$new_id = $this->_insert_checkin_record($new_reg_id, $new_datetime);
93
+				if ($new_id) {
94
+					$new_checkin_ids[] = $new_id;
95
+				}
96
+			}
97
+		}
98
+		if ($new_checkin_ids) {
99
+			$this->get_migration_script()->set_mapping(
100
+				$this->_old_table,
101
+				$old_row['id'],
102
+				$this->_new_table,
103
+				$new_checkin_ids
104
+			);
105
+		}
106
+	}
107 107
 
108 108
 
109
-    /**
110
-     * Tries to find the new datetime the Check-in was for, based on the attendee row
111
-     * (because we know the attendee was for an event as a specific time, and we know
112
-     * the event's OLD ID...)
113
-     *
114
-     * @param array $old_attendee
115
-     * @return array row of datetime from DB
116
-     * @throws EE_Error
117
-     * @global wpdb $wpdb
118
-     */
119
-    private function _try_to_find_datetime(array $old_attendee): array
120
-    {
121
-        global $wpdb;
109
+	/**
110
+	 * Tries to find the new datetime the Check-in was for, based on the attendee row
111
+	 * (because we know the attendee was for an event as a specific time, and we know
112
+	 * the event's OLD ID...)
113
+	 *
114
+	 * @param array $old_attendee
115
+	 * @return array row of datetime from DB
116
+	 * @throws EE_Error
117
+	 * @global wpdb $wpdb
118
+	 */
119
+	private function _try_to_find_datetime(array $old_attendee): array
120
+	{
121
+		global $wpdb;
122 122
 
123
-        $new_event_id =
124
-            $this->get_migration_script()->get_mapping_new_pk(
125
-                $wpdb->prefix . "events_detail",
126
-                $old_attendee['event_id'],
127
-                $wpdb->posts
128
-            );
129
-        if (! $new_event_id) {
130
-            $this->add_error(
131
-                sprintf(
132
-                    esc_html__(
133
-                    /* translators: 1: original event ID, 2: original attendee database row */
134
-                    // @codingStandardsIgnoreStart
135
-                        'Could not find new event ID with old event ID %1$d, on attendee row %2$s; and because of that couldn\'t find the correct datetime for Check-in',
136
-                        // @codingStandardsIgnoreEnd
137
-                        'event_espresso'
138
-                    ),
139
-                    $old_attendee['event_id'],
140
-                    $this->_json_encode($old_attendee)
141
-                )
142
-            );
143
-            return [];
144
-        }
145
-        $old_att_start_date = $old_attendee['start_date'];
146
-        $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
147
-        $old_att_datetime   =
148
-            $this->get_migration_script()->convert_date_string_to_utc(
149
-                $this,
150
-                $old_attendee,
151
-                "$old_att_start_date $old_att_start_time:00"
152
-            );
123
+		$new_event_id =
124
+			$this->get_migration_script()->get_mapping_new_pk(
125
+				$wpdb->prefix . "events_detail",
126
+				$old_attendee['event_id'],
127
+				$wpdb->posts
128
+			);
129
+		if (! $new_event_id) {
130
+			$this->add_error(
131
+				sprintf(
132
+					esc_html__(
133
+					/* translators: 1: original event ID, 2: original attendee database row */
134
+					// @codingStandardsIgnoreStart
135
+						'Could not find new event ID with old event ID %1$d, on attendee row %2$s; and because of that couldn\'t find the correct datetime for Check-in',
136
+						// @codingStandardsIgnoreEnd
137
+						'event_espresso'
138
+					),
139
+					$old_attendee['event_id'],
140
+					$this->_json_encode($old_attendee)
141
+				)
142
+			);
143
+			return [];
144
+		}
145
+		$old_att_start_date = $old_attendee['start_date'];
146
+		$old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
147
+		$old_att_datetime   =
148
+			$this->get_migration_script()->convert_date_string_to_utc(
149
+				$this,
150
+				$old_attendee,
151
+				"$old_att_start_date $old_att_start_time:00"
152
+			);
153 153
 
154
-        $datetime_table = $wpdb->prefix . "esp_datetime";
155
-        // add all conditions to an array from which we can SHIFT conditions off in order to widen our search
156
-        // the most important condition should be last, as it will be array_shift'ed off last
157
-        $conditions = [
158
-            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
159
-            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
160
-        ];
161
-        // start running queries, widening search each time by removing a condition
162
-        do {
163
-            $full_query     = "SELECT * FROM $datetime_table WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
164
-            $datetime_found = $wpdb->get_row($full_query, ARRAY_A);
165
-            array_shift($conditions);
166
-        } while (! $datetime_found && $conditions);
167
-        return $datetime_found;
168
-    }
154
+		$datetime_table = $wpdb->prefix . "esp_datetime";
155
+		// add all conditions to an array from which we can SHIFT conditions off in order to widen our search
156
+		// the most important condition should be last, as it will be array_shift'ed off last
157
+		$conditions = [
158
+			$wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
159
+			$wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
160
+		];
161
+		// start running queries, widening search each time by removing a condition
162
+		do {
163
+			$full_query     = "SELECT * FROM $datetime_table WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
164
+			$datetime_found = $wpdb->get_row($full_query, ARRAY_A);
165
+			array_shift($conditions);
166
+		} while (! $datetime_found && $conditions);
167
+		return $datetime_found;
168
+	}
169 169
 
170 170
 
171
-    /**
172
-     * Adds a new Check-in/checkout record according to $new_reg_id,$new_datetime_id,$checking_in, and $timestamp
173
-     *
174
-     * @param int   $new_reg_id
175
-     * @param array $new_datetime
176
-     * @return int new Check-in id
177
-     */
178
-    private function _insert_checkin_record(int $new_reg_id, array $new_datetime): int
179
-    {
180
-        global $wpdb;
171
+	/**
172
+	 * Adds a new Check-in/checkout record according to $new_reg_id,$new_datetime_id,$checking_in, and $timestamp
173
+	 *
174
+	 * @param int   $new_reg_id
175
+	 * @param array $new_datetime
176
+	 * @return int new Check-in id
177
+	 */
178
+	private function _insert_checkin_record(int $new_reg_id, array $new_datetime): int
179
+	{
180
+		global $wpdb;
181 181
 
182 182
 
183
-        // ok we can actually do what we set out to do: add a checkin/checkout record
184
-        $cols_n_values = [
185
-            'REG_ID'        => $new_reg_id,
186
-            'DTT_ID'        => $new_datetime['DTT_ID'] ?? 0,
187
-            'CHK_in'        => true,
188
-            'CHK_timestamp' => $new_datetime['DTT_EVT_start'] ?? null,
189
-        ];
190
-        $data_types    = [
191
-            '%d',// REG_ID
192
-            '%d',// DTT_ID
193
-            '%d',// CHK_in
194
-            '%s',// CHK_timestamp
195
-        ];
196
-        $success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
197
-        if (! $success) {
198
-            $this->add_error(
199
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
200
-                    $this->_old_table,
201
-                    [],
202
-                    $this->_new_table,
203
-                    $cols_n_values,
204
-                    $data_types
205
-                )
206
-            );
207
-            return 0;
208
-        }
209
-        return $wpdb->insert_id;
210
-    }
183
+		// ok we can actually do what we set out to do: add a checkin/checkout record
184
+		$cols_n_values = [
185
+			'REG_ID'        => $new_reg_id,
186
+			'DTT_ID'        => $new_datetime['DTT_ID'] ?? 0,
187
+			'CHK_in'        => true,
188
+			'CHK_timestamp' => $new_datetime['DTT_EVT_start'] ?? null,
189
+		];
190
+		$data_types    = [
191
+			'%d',// REG_ID
192
+			'%d',// DTT_ID
193
+			'%d',// CHK_in
194
+			'%s',// CHK_timestamp
195
+		];
196
+		$success       = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
197
+		if (! $success) {
198
+			$this->add_error(
199
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
200
+					$this->_old_table,
201
+					[],
202
+					$this->_new_table,
203
+					$cols_n_values,
204
+					$data_types
205
+				)
206
+			);
207
+			return 0;
208
+		}
209
+		return $wpdb->insert_id;
210
+	}
211 211
 }
Please login to merge, or discard this patch.
4_1_0_stages/EE_DMS_4_1_0_event_question_group.dmsstage.php 1 patch
Indentation   +150 added lines, -150 removed lines patch added patch discarded remove patch
@@ -20,163 +20,163 @@
 block discarded – undo
20 20
  */
21 21
 class EE_DMS_4_1_0_event_question_group extends EE_Data_Migration_Script_Stage_Table
22 22
 {
23
-    /**
24
-     * @throws EE_Error
25
-     */
26
-    public function _migrate_old_row($old_row)
27
-    {
28
-        //      $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $this->_new_transaction_table);
29
-        //          if ( ! $txn_id ){
30
-        //              $this->add_error(sprintf(esc_html__("Could not find the transaction for the 3.1 attendee %d from row %s", "event_espresso"),$old_row['id'],$this->_json_encode($old_row)));
31
-        //              return;
32
-        //          }
33
-        //          $txn = $this->_get_txn($txn_id);
34
-        //          $new_line_items = $this->_insert_new_line_items($txn,$old_row);
35
-        //          $this->get_migration_script()->set_mapping($this->_old_table,$old_row['id'],$this->_new_line_table,$new_line_items);
23
+	/**
24
+	 * @throws EE_Error
25
+	 */
26
+	public function _migrate_old_row($old_row)
27
+	{
28
+		//      $txn_id = $this->get_migration_script()->get_mapping_new_pk($this->_old_table, $old_row['id'], $this->_new_transaction_table);
29
+		//          if ( ! $txn_id ){
30
+		//              $this->add_error(sprintf(esc_html__("Could not find the transaction for the 3.1 attendee %d from row %s", "event_espresso"),$old_row['id'],$this->_json_encode($old_row)));
31
+		//              return;
32
+		//          }
33
+		//          $txn = $this->_get_txn($txn_id);
34
+		//          $new_line_items = $this->_insert_new_line_items($txn,$old_row);
35
+		//          $this->get_migration_script()->set_mapping($this->_old_table,$old_row['id'],$this->_new_line_table,$new_line_items);
36 36
 
37
-        $this->_insert_new_event_question_groups($old_row);
38
-    }
39
-    //  function _migration_step($num_items=50){
40
-    //      global $wpdb;
41
-    //      $start_at_record = $this->count_records_migrated();
42
-    //      $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d",$start_at_record,$num_items),ARRAY_A);
43
-    //      $items_actually_migrated = 0;
44
-    //      foreach($rows as $old_event){
45
-    //          $this->_insert_new_event_question_groups($old_event);
46
-    //          $items_actually_migrated++;
47
-    //      }
48
-    //      if($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()){
49
-    //          $this->set_completed();
50
-    //      }
51
-    //      return $items_actually_migrated;
52
-    //  }
53
-    //  function _count_records_to_migrate() {
54
-    //      global $wpdb;
55
-    //      $count = $wpdb->get_var("SELECT COUNT(id) FROM ".$this->_old_table);
56
-    //      return $count;
57
-    //  }
58
-    public function __construct()
59
-    {
60
-        global $wpdb;
61
-        $this->_old_table       = $wpdb->prefix . "events_detail";
62
-        $this->_extra_where_sql = 'WHERE event_status!="D"';
63
-        $this->_new_table       = $wpdb->prefix . "esp_event_question_group";
64
-        $this->_pretty_name     = esc_html__("Question Groups in each Event", "event_espresso");
65
-        parent::__construct();
66
-    }
37
+		$this->_insert_new_event_question_groups($old_row);
38
+	}
39
+	//  function _migration_step($num_items=50){
40
+	//      global $wpdb;
41
+	//      $start_at_record = $this->count_records_migrated();
42
+	//      $rows = $wpdb->get_results($wpdb->prepare("SELECT * FROM $this->_old_table LIMIT %d,%d",$start_at_record,$num_items),ARRAY_A);
43
+	//      $items_actually_migrated = 0;
44
+	//      foreach($rows as $old_event){
45
+	//          $this->_insert_new_event_question_groups($old_event);
46
+	//          $items_actually_migrated++;
47
+	//      }
48
+	//      if($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()){
49
+	//          $this->set_completed();
50
+	//      }
51
+	//      return $items_actually_migrated;
52
+	//  }
53
+	//  function _count_records_to_migrate() {
54
+	//      global $wpdb;
55
+	//      $count = $wpdb->get_var("SELECT COUNT(id) FROM ".$this->_old_table);
56
+	//      return $count;
57
+	//  }
58
+	public function __construct()
59
+	{
60
+		global $wpdb;
61
+		$this->_old_table       = $wpdb->prefix . "events_detail";
62
+		$this->_extra_where_sql = 'WHERE event_status!="D"';
63
+		$this->_new_table       = $wpdb->prefix . "esp_event_question_group";
64
+		$this->_pretty_name     = esc_html__("Question Groups in each Event", "event_espresso");
65
+		parent::__construct();
66
+	}
67 67
 
68 68
 
69
-    /**
70
-     * Attempts to insert a new question group in the new format given an old one
71
-     *
72
-     * @param array $old_event
73
-     * @return void
74
-     * @throws EE_Error
75
-     * @global wpdb $wpdb
76
-     */
77
-    private function _insert_new_event_question_groups(array $old_event)
78
-    {
79
-        $new_event_question_group_ids = [];
80
-        $question_groups_for_primary  = maybe_unserialize($old_event['question_groups']);
81
-        if (is_array($question_groups_for_primary)) {
82
-            foreach ($question_groups_for_primary as $old_question_group_id) {
83
-                $new_id = $this->_insert_event_question_group(
84
-                    $old_event,
85
-                    (int) $old_question_group_id,
86
-                    true
87
-                );
88
-                if ($new_id) {
89
-                    $new_event_question_group_ids[] = $new_id;
90
-                }
91
-            }
92
-        }
93
-        $event_meta = maybe_unserialize($old_event['event_meta']);
94
-        if (isset($event_meta['add_attendee_question_groups'])) {
95
-            if (is_array($event_meta['add_attendee_question_groups'])) {
96
-                foreach ($event_meta['add_attendee_question_groups'] as $old_question_group_id) {
97
-                    $new_id = $this->_insert_event_question_group($old_event, $old_question_group_id, false);
98
-                    if ($new_id) {
99
-                        $new_event_question_group_ids[] = $new_id;
100
-                    }
101
-                }
102
-            }
103
-        }
69
+	/**
70
+	 * Attempts to insert a new question group in the new format given an old one
71
+	 *
72
+	 * @param array $old_event
73
+	 * @return void
74
+	 * @throws EE_Error
75
+	 * @global wpdb $wpdb
76
+	 */
77
+	private function _insert_new_event_question_groups(array $old_event)
78
+	{
79
+		$new_event_question_group_ids = [];
80
+		$question_groups_for_primary  = maybe_unserialize($old_event['question_groups']);
81
+		if (is_array($question_groups_for_primary)) {
82
+			foreach ($question_groups_for_primary as $old_question_group_id) {
83
+				$new_id = $this->_insert_event_question_group(
84
+					$old_event,
85
+					(int) $old_question_group_id,
86
+					true
87
+				);
88
+				if ($new_id) {
89
+					$new_event_question_group_ids[] = $new_id;
90
+				}
91
+			}
92
+		}
93
+		$event_meta = maybe_unserialize($old_event['event_meta']);
94
+		if (isset($event_meta['add_attendee_question_groups'])) {
95
+			if (is_array($event_meta['add_attendee_question_groups'])) {
96
+				foreach ($event_meta['add_attendee_question_groups'] as $old_question_group_id) {
97
+					$new_id = $this->_insert_event_question_group($old_event, $old_question_group_id, false);
98
+					if ($new_id) {
99
+						$new_event_question_group_ids[] = $new_id;
100
+					}
101
+				}
102
+			}
103
+		}
104 104
 
105 105
 
106
-        $this->get_migration_script()->set_mapping(
107
-            $this->_old_table,
108
-            $old_event['id'],
109
-            $this->_new_table,
110
-            $new_event_question_group_ids
111
-        );
112
-    }
106
+		$this->get_migration_script()->set_mapping(
107
+			$this->_old_table,
108
+			$old_event['id'],
109
+			$this->_new_table,
110
+			$new_event_question_group_ids
111
+		);
112
+	}
113 113
 
114 114
 
115
-    /**
116
-     * @throws EE_Error
117
-     */
118
-    private function _insert_event_question_group(
119
-        array $old_event,
120
-        int $old_question_group_id,
121
-        bool $primary
122
-    ): int {
123
-        global $wpdb;
124
-        $new_question_group_id = $this->get_migration_script()->get_mapping_new_pk(
125
-            $wpdb->prefix . "events_qst_group",
126
-            $old_question_group_id,
127
-            $wpdb->prefix . "esp_question_group"
128
-        );
115
+	/**
116
+	 * @throws EE_Error
117
+	 */
118
+	private function _insert_event_question_group(
119
+		array $old_event,
120
+		int $old_question_group_id,
121
+		bool $primary
122
+	): int {
123
+		global $wpdb;
124
+		$new_question_group_id = $this->get_migration_script()->get_mapping_new_pk(
125
+			$wpdb->prefix . "events_qst_group",
126
+			$old_question_group_id,
127
+			$wpdb->prefix . "esp_question_group"
128
+		);
129 129
 
130
-        if (! $new_question_group_id) {
131
-            $this->add_error(
132
-                sprintf(
133
-                // translators: %s question ID, %s event ID
134
-                    esc_html__("Could not find 4.1 question ID for 3.1 question id #%s on event $%s", "event_espresso"),
135
-                    $old_question_group_id,
136
-                    $old_event['id']
137
-                )
138
-            );
139
-            return 0;
140
-        }
141
-        $new_event_id = $this->get_migration_script()->get_mapping_new_pk(
142
-            $wpdb->prefix . "events_detail",
143
-            intval($old_event['id']),
144
-            $wpdb->posts
145
-        );
146
-        if (! $new_event_id) {
147
-            $this->add_error(
148
-                sprintf(
149
-                // translators: %s event ID
150
-                    esc_html__("Could not find 4.1 event 3.1 event id #%s", "event_espresso"),
151
-                    $old_event['id']
152
-                )
153
-            );
154
-            return 0;
155
-        }
156
-        $cols_n_values = [
157
-            'EVT_ID'      => $new_event_id,
158
-            'QSG_ID'      => $new_question_group_id,
159
-            'EQG_primary' => $primary,
160
-        ];
130
+		if (! $new_question_group_id) {
131
+			$this->add_error(
132
+				sprintf(
133
+				// translators: %s question ID, %s event ID
134
+					esc_html__("Could not find 4.1 question ID for 3.1 question id #%s on event $%s", "event_espresso"),
135
+					$old_question_group_id,
136
+					$old_event['id']
137
+				)
138
+			);
139
+			return 0;
140
+		}
141
+		$new_event_id = $this->get_migration_script()->get_mapping_new_pk(
142
+			$wpdb->prefix . "events_detail",
143
+			intval($old_event['id']),
144
+			$wpdb->posts
145
+		);
146
+		if (! $new_event_id) {
147
+			$this->add_error(
148
+				sprintf(
149
+				// translators: %s event ID
150
+					esc_html__("Could not find 4.1 event 3.1 event id #%s", "event_espresso"),
151
+					$old_event['id']
152
+				)
153
+			);
154
+			return 0;
155
+		}
156
+		$cols_n_values = [
157
+			'EVT_ID'      => $new_event_id,
158
+			'QSG_ID'      => $new_question_group_id,
159
+			'EQG_primary' => $primary,
160
+		];
161 161
 
162
-        $data_types = [
163
-            '%d',// EVT_ID
164
-            '%d',// QSG_ID
165
-            '%d',// EQG_primary
166
-        ];
167
-        $success    = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
168
-        if (! $success) {
169
-            $this->add_error(
170
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
171
-                    $this->_old_table,
172
-                    $old_event,
173
-                    $this->_new_table,
174
-                    $cols_n_values,
175
-                    $data_types
176
-                )
177
-            );
178
-            return 0;
179
-        }
180
-        return $wpdb->insert_id;
181
-    }
162
+		$data_types = [
163
+			'%d',// EVT_ID
164
+			'%d',// QSG_ID
165
+			'%d',// EQG_primary
166
+		];
167
+		$success    = $wpdb->insert($this->_new_table, $cols_n_values, $data_types);
168
+		if (! $success) {
169
+			$this->add_error(
170
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
171
+					$this->_old_table,
172
+					$old_event,
173
+					$this->_new_table,
174
+					$cols_n_values,
175
+					$data_types
176
+				)
177
+			);
178
+			return 0;
179
+		}
180
+		return $wpdb->insert_id;
181
+	}
182 182
 }
Please login to merge, or discard this patch.
data_migration_scripts/4_1_0_stages/EE_DMS_4_1_0_attendees.dmsstage.php 1 patch
Indentation   +917 added lines, -917 removed lines patch added patch discarded remove patch
@@ -224,942 +224,942 @@
 block discarded – undo
224 224
  */
225 225
 class EE_DMS_4_1_0_attendees extends EE_Data_Migration_Script_Stage_Table
226 226
 {
227
-    private string $_new_attendee_cpt_table;
227
+	private string $_new_attendee_cpt_table;
228 228
 
229
-    private string $_new_attendee_meta_table;
229
+	private string $_new_attendee_meta_table;
230 230
 
231
-    private string $_new_reg_table;
231
+	private string $_new_reg_table;
232 232
 
233
-    private string $_new_transaction_table;
233
+	private string $_new_transaction_table;
234 234
 
235
-    private string $_new_payment_table;
235
+	private string $_new_payment_table;
236 236
 
237
-    private string $_old_mer_table;
237
+	private string $_old_mer_table;
238 238
 
239
-    private string $_new_ticket_table;
239
+	private string $_new_ticket_table;
240 240
 
241
-    private string $_new_ticket_datetime_table;
241
+	private string $_new_ticket_datetime_table;
242 242
 
243
-    private string $_new_datetime_table;
243
+	private string $_new_datetime_table;
244 244
 
245
-    private string $_new_datetime_ticket_table;
245
+	private string $_new_datetime_ticket_table;
246 246
 
247
-    private string $_new_price_table;
247
+	private string $_new_price_table;
248 248
 
249
-    private string $_new_ticket_price_table;
249
+	private string $_new_ticket_price_table;
250 250
 
251
-    /**
252
-     * Remembers whether the mer table exists
253
-     *
254
-     * @var boolean
255
-     */
256
-    private ?bool $_mer_tables_exist = null;
251
+	/**
252
+	 * Remembers whether the mer table exists
253
+	 *
254
+	 * @var boolean
255
+	 */
256
+	private ?bool $_mer_tables_exist = null;
257 257
 
258 258
 
259
-    public function __construct()
260
-    {
261
-        global $wpdb;
262
-        $this->_pretty_name = esc_html__("Attendees", "event_espresso");
263
-        $this->_old_table   = $wpdb->prefix . "events_attendee";
264
-        // Only select the event status column from the event table.
265
-        $this->select_expression = 'att.*, e.event_status';
266
-        // Only select attendees for events that aren't deleted.
267
-        $this->_extra_where_sql           = 'AS att
259
+	public function __construct()
260
+	{
261
+		global $wpdb;
262
+		$this->_pretty_name = esc_html__("Attendees", "event_espresso");
263
+		$this->_old_table   = $wpdb->prefix . "events_attendee";
264
+		// Only select the event status column from the event table.
265
+		$this->select_expression = 'att.*, e.event_status';
266
+		// Only select attendees for events that aren't deleted.
267
+		$this->_extra_where_sql           = 'AS att
268 268
             INNER JOIN ' . $wpdb->prefix . 'events_detail AS e ON att.event_id=e.id
269 269
             WHERE e.event_status!="D"';
270
-        $this->_old_mer_table             = $wpdb->prefix . "events_multi_event_registration_id_group";
271
-        $this->_new_attendee_cpt_table    = $wpdb->posts;
272
-        $this->_new_attendee_meta_table   = $wpdb->prefix . "esp_attendee_meta";
273
-        $this->_new_reg_table             = $wpdb->prefix . "esp_registration";
274
-        $this->_new_transaction_table     = $wpdb->prefix . "esp_transaction";
275
-        $this->_new_payment_table         = $wpdb->prefix . "esp_payment";
276
-        $this->_new_ticket_table          = $wpdb->prefix . "esp_ticket";
277
-        $this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
278
-        $this->_new_datetime_table        = $wpdb->prefix . "esp_datetime";
279
-        $this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
280
-        $this->_new_price_table           = $wpdb->prefix . "esp_price";
281
-        $this->_new_ticket_price_table    = $wpdb->prefix . "esp_ticket_price";
282
-        parent::__construct();
283
-    }
284
-
285
-
286
-    /**
287
-     * @throws EE_Error
288
-     */
289
-    protected function _migrate_old_row($old_row)
290
-    {
291
-        // first check if there's already a new attendee with similar characteristics
292
-        $new_att_id = $this->_find_attendee_cpt_matching($old_row);
293
-        if (! $new_att_id) {
294
-            $new_att_id = $this->_insert_new_attendee_cpt($old_row);
295
-            if (! $new_att_id) {
296
-                // if we couldn't even make an attendee, abandon all hope
297
-                return;
298
-            }
299
-            $new_att_meta_id = $this->_insert_attendee_meta_row($old_row, $new_att_id);
300
-            if ($new_att_meta_id) {
301
-                $this->get_migration_script()->set_mapping(
302
-                    $this->_old_table,
303
-                    $old_row['id'],
304
-                    $this->_new_attendee_meta_table,
305
-                    $new_att_meta_id
306
-                );
307
-            }
308
-        }
309
-        $this->get_migration_script()->set_mapping(
310
-            $this->_old_table,
311
-            $old_row['id'],
312
-            $this->_new_attendee_cpt_table,
313
-            $new_att_id
314
-        );
315
-
316
-        $txn_id = $this->_insert_new_transaction($old_row);
317
-        if (! $txn_id) {
318
-            // if we couldn't make the transaction, also abandon all hope
319
-            return;
320
-        }
321
-        $this->get_migration_script()->set_mapping(
322
-            $this->_old_table,
323
-            $old_row['id'],
324
-            $this->_new_transaction_table,
325
-            $txn_id
326
-        );
327
-        $pay_id = $this->_insert_new_payment($old_row, $txn_id);
328
-        if ($pay_id) {
329
-            $this->get_migration_script()->set_mapping(
330
-                $this->_old_table,
331
-                $old_row['id'],
332
-                $this->_new_payment_table,
333
-                $pay_id
334
-            );
335
-        }
336
-
337
-
338
-        // even if there was no payment, we can go ahead with adding the reg
339
-        $new_regs = $this->_insert_new_registrations($old_row, $new_att_id, $txn_id);
340
-        if ($new_regs) {
341
-            $this->get_migration_script()->set_mapping(
342
-                $this->_old_table,
343
-                $old_row['id'],
344
-                $this->_new_reg_table,
345
-                $new_regs
346
-            );
347
-        }
348
-    }
349
-
350
-
351
-    /**
352
-     * Checks if there's already an attendee CPT in the db that has the same
353
-     * first and last name, and email. If so, returns its ID as an int.
354
-     *
355
-     * @param array $old_attendee
356
-     * @return int
357
-     * @global wpdb $wpdb
358
-     */
359
-    private function _find_attendee_cpt_matching(array $old_attendee): int
360
-    {
361
-        global $wpdb;
362
-        $existing_attendee_id =
363
-            $wpdb->get_var(
364
-                $wpdb->prepare(
365
-                    "SELECT id FROM " .
366
-                    $this->_new_attendee_cpt_table .
367
-                    " AS cpt INNER JOIN " .
368
-                    $this->_new_attendee_meta_table .
369
-                    " 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",
370
-                    $old_attendee['fname'],
371
-                    $old_attendee['lname'],
372
-                    $old_attendee['email']
373
-                )
374
-            );
375
-        return intval($existing_attendee_id);
376
-    }
377
-
378
-
379
-    private function _insert_new_attendee_cpt($old_attendee): int
380
-    {
381
-        global $wpdb;
382
-        $cols_n_values = [
383
-            'post_title'    => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
384
-            'post_content'  => '',// ATT_bio
385
-            'post_name'     => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
386
-            'post_date'     => $this->get_migration_script()->convert_date_string_to_utc(
387
-                $this,
388
-                $old_attendee,
389
-                $old_attendee['date']
390
-            ),// ATT_created
391
-            'post_excerpt'  => '',// ATT_short_bio
392
-            'post_modified' => $this->get_migration_script()->convert_date_string_to_utc(
393
-                $this,
394
-                $old_attendee,
395
-                $old_attendee['date']
396
-            ),// ATT_modified
397
-            'post_author'   => 0,// ATT_author
398
-            'post_parent'   => 0,// ATT_parent
399
-            'post_type'     => EspressoPostType::ATTENDEES,
400
-            'post_status'   => 'publish',// status
401
-        ];
402
-        $data_types    = [
403
-            '%s',// ATT_full_name
404
-            '%s',// ATT_bio
405
-            '%s',// ATT_slug
406
-            '%s',// ATT_created
407
-            '%s',// ATT_short_bio
408
-            '%s',// ATT_modified
409
-            '%d',// ATT_author
410
-            '%d',// ATT_parent
411
-            '%s',// post_type
412
-            '%s',// status
413
-        ];
414
-        $success       = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $data_types);
415
-        if (! $success) {
416
-            $this->add_error(
417
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
418
-                    $this->_old_table,
419
-                    $old_attendee,
420
-                    $this->_new_attendee_cpt_table,
421
-                    $cols_n_values,
422
-                    $data_types
423
-                )
424
-            );
425
-            return 0;
426
-        }
427
-        return $wpdb->insert_id;
428
-    }
429
-
430
-
431
-    private function _insert_attendee_meta_row($old_attendee, $new_attendee_cpt_id): int
432
-    {
433
-        global $wpdb;
434
-        // get the state and country ids from the old row
435
-        try {
436
-            $new_country     =
437
-                $this->get_migration_script()->get_or_create_country(stripslashes($old_attendee['country_id']));
438
-            $new_country_iso = $new_country['CNT_ISO'];
439
-        } catch (EE_Error $exception) {
440
-            $new_country_iso = $this->get_migration_script()->get_default_country_iso();
441
-        }
442
-        try {
443
-            $new_state    =
444
-                $this->get_migration_script()->get_or_create_state(
445
-                    stripslashes($old_attendee['state']),
446
-                    $new_country_iso
447
-                );
448
-            $new_state_id = $new_state['STA_ID'];
449
-        } catch (EE_Error $exception) {
450
-            $new_state_id = 0;
451
-        }
452
-        $cols_n_values = [
453
-            'ATT_ID'       => $new_attendee_cpt_id,
454
-            'ATT_fname'    => stripslashes($old_attendee['fname']),
455
-            'ATT_lname'    => stripslashes($old_attendee['lname']),
456
-            'ATT_address'  => stripslashes($old_attendee['address']),
457
-            'ATT_address2' => stripslashes($old_attendee['address2']),
458
-            'ATT_city'     => stripslashes($old_attendee['city']),
459
-            'STA_ID'       => $new_state_id,
460
-            'CNT_ISO'      => $new_country_iso,
461
-            'ATT_zip'      => stripslashes($old_attendee['zip']),
462
-            'ATT_email'    => stripslashes($old_attendee['email']),
463
-            'ATT_phone'    => stripslashes($old_attendee['phone']),
464
-        ];
465
-        $data_types    = [
466
-            '%d',// ATT_ID
467
-            '%s',// ATT_fname
468
-            '%s',// ATT_lname
469
-            '%s',// ATT_address
470
-            '%s',// ATT_address2
471
-            '%s',// ATT_city
472
-            '%d',// STA_ID
473
-            '%s',// CNT_ISO
474
-            '%s',// ATT_zip
475
-            '%s',// ATT_email
476
-            '%s',// ATT_phone
477
-        ];
478
-        $success       = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $data_types);
479
-        if (! $success) {
480
-            $this->add_error(
481
-                $this->get_migration_script()->_create_error_message_for_db_insertion(
482
-                    $this->_old_table,
483
-                    $old_attendee,
484
-                    $this->_new_attendee_meta_table,
485
-                    $cols_n_values,
486
-                    $data_types
487
-                )
488
-            );
489
-            return 0;
490
-        }
491
-        return $wpdb->insert_id;
492
-    }
493
-
494
-
495
-    /**
496
-     * Note: we don't necessarily create a new transaction for each attendee row.
497
-     * Only if the old attendee 'is_primary' is true; otherwise we find the old attendee row that
498
-     * 'is_primary' and has the same 'txn_id', then we return ITS new transaction id
499
-     *
500
-     * @param array $old_attendee
501
-     * @return int new transaction id
502
-     * @throws EE_Error
503
-     * @global wpdb $wpdb
504
-     */
505
-    private function _insert_new_transaction(array $old_attendee): int
506
-    {
507
-        global $wpdb;
508
-
509
-        // first: let's check for an existing transaction for this old attendee
510
-        if (intval($old_attendee['is_primary'])) {// primary attendee, so create txn
511
-            $txn_id =
512
-                $this->get_migration_script()->get_mapping_new_pk(
513
-                    $this->_old_table,
514
-                    intval($old_attendee['id']),
515
-                    $this->_new_transaction_table
516
-                );
517
-        } else { // non-primary attendee, so find its primary attendee's transaction
518
-            $primary_attendee_old_id =
519
-                $wpdb->get_var(
520
-                    $wpdb->prepare(
521
-                        "SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s",
522
-                        $old_attendee['registration_id']
523
-                    )
524
-                );
525
-            if (! $primary_attendee_old_id) {
526
-                $primary_attendee        =
527
-                    $this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
528
-                $primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
529
-            }
530
-            $txn_id =
531
-                $this->get_migration_script()->get_mapping_new_pk(
532
-                    $this->_old_table,
533
-                    intval($primary_attendee_old_id),
534
-                    $this->_new_transaction_table
535
-                );
536
-            if (! $txn_id) {
537
-                $this->add_error(
538
-                    sprintf(
539
-                        esc_html__(
540
-                            "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.",
541
-                            "event_espresso"
542
-                        ),
543
-                        $this->_json_encode($old_attendee),
544
-                        $primary_attendee_old_id
545
-                    )
546
-                );
547
-                $txn_id = 0;
548
-            }
549
-        }
550
-        // if there isn't yet a transaction row for this, create one
551
-        // (so even if it was a non-primary attendee with no EE3 primary attendee,
552
-        // it ought to have SOME transaction, so we'll make one)
553
-        if (! $txn_id) {
554
-            // maps 3.1 payment stati onto 4.1 transaction stati
555
-            $txn_status_mapping = [
556
-                'Completed'        => 'TCM',
557
-                'Pending'          => 'TIN',
558
-                'Payment Declined' => 'TIN',
559
-                'Incomplete'       => 'TIN',
560
-                'Not Completed'    => 'TIN',
561
-                'Cancelled'        => 'TIN',
562
-                'Declined'         => 'TIN',
563
-            ];
564
-            $STS_ID             = $txn_status_mapping[ $old_attendee['payment_status'] ] ?? 'TIN';
565
-            $cols_n_values      = [
566
-                'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc(
567
-                    $this,
568
-                    $old_attendee,
569
-                    $old_attendee['date']
570
-                ),
571
-                'TXN_total'     => floatval($old_attendee['total_cost']),
572
-                'TXN_paid'      => floatval($old_attendee['amount_pd']),
573
-                'STS_ID'        => $STS_ID,
574
-                'TXN_hash_salt' => $old_attendee['hashSalt'],
575
-            ];
576
-            $data_types         = [
577
-                '%s',// TXN_timestamp
578
-                '%f',// TXN_total
579
-                '%f',// TXN_paid
580
-                '%s',// STS_ID
581
-                '%s',// TXN_hash_salt
582
-            ];
583
-            $success            = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $data_types);
584
-            if (! $success) {
585
-                $this->add_error(
586
-                    $this->get_migration_script()->_create_error_message_for_db_insertion(
587
-                        $this->_old_table,
588
-                        $old_attendee,
589
-                        $this->_new_transaction_table,
590
-                        $cols_n_values,
591
-                        $data_types
592
-                    )
593
-                );
594
-                return 0;
595
-            }
596
-            $txn_id = $wpdb->insert_id;
597
-        }
598
-
599
-        return $txn_id;
600
-    }
601
-
602
-
603
-    /**
604
-     * Detects if the MER tables exist
605
-     *
606
-     * @return boolean
607
-     * @global wpdb $wpdb
608
-     */
609
-    private function _mer_tables_exist(): ?bool
610
-    {
611
-        if ($this->_mer_tables_exist === null) {
612
-            global $wpdb;
613
-
614
-            if ($wpdb->get_var("SHOW TABLES LIKE '$this->_old_mer_table'") != $this->_old_mer_table) {
615
-                $this->_mer_tables_exist = false;
616
-            } else {
617
-                $this->_mer_tables_exist = true;
618
-            }
619
-        }
620
-        return $this->_mer_tables_exist;
621
-    }
622
-
623
-
624
-    /**
625
-     * Gets the 4.1 registration's status given the 3.1 attendee row. We consider
626
-     * whether the event required pre-approval or not,a dn the 4.1 payment status.
627
-     *
628
-     * @param array $old_attendee_row
629
-     * @return string
630
-     * @global wpdb $wpdb
631
-     */
632
-    private function _get_reg_status_for_old_payment_status(array $old_attendee_row): string
633
-    {
634
-        // need event default reg status and if pre_approval was required
635
-        global $wpdb;
636
-        $event_required_pre_approval =
637
-            $wpdb->get_var(
638
-                $wpdb->prepare(
639
-                    "SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d",
640
-                    $old_attendee_row['event_id']
641
-                )
642
-            );
643
-        return $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
644
-            $old_attendee_row['payment_status'],
645
-            intval($event_required_pre_approval) && intval($old_attendee_row['pre_approve'])
646
-        );
647
-    }
648
-
649
-
650
-    /**
651
-     * Adds however many registrations are indicated by the old attendee's QUANTITY field,
652
-     * and returns an array of their IDs
653
-     *
654
-     * @param array $old_attendee
655
-     * @param int   $new_attendee_id
656
-     * @param int   $new_txn_id
657
-     * @return array of new registration ids
658
-     * @throws EE_Error
659
-     * @global wpdb $wpdb
660
-     */
661
-    private function _insert_new_registrations(array $old_attendee, int $new_attendee_id, int $new_txn_id): array
662
-    {
663
-        global $wpdb;
664
-
665
-        $STS_ID       = $this->_get_reg_status_for_old_payment_status($old_attendee);
666
-        $new_event_id =
667
-            $this->get_migration_script()->get_mapping_new_pk(
668
-                $wpdb->prefix . 'events_detail',
669
-                $old_attendee['event_id'],
670
-                $wpdb->posts
671
-            );
672
-        if (! $new_event_id) {
673
-            $this->add_error(
674
-                sprintf(
675
-                    esc_html__(
676
-                        "Could not find NEW event CPT ID for old event '%d' on old attendee %s",
677
-                        "event_espresso"
678
-                    ),
679
-                    $old_attendee['event_id'],
680
-                    $this->_json_encode($old_attendee)
681
-                )
682
-            );
683
-        }
684
-
685
-        $ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
686
-        if (! $ticket_id) {
687
-            $ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
688
-            $this->add_error(
689
-                sprintf(
690
-                    esc_html__(
691
-                        'Could not find a ticket for old attendee with id %d for new event %d, so created a new ticket with id %d',
692
-                        'event_espresso'
693
-                    ),
694
-                    $old_attendee['id'],
695
-                    $new_event_id,
696
-                    $ticket_id
697
-                )
698
-            );
699
-        }
700
-        $regs_on_this_row = intval($old_attendee['quantity']);
701
-        $new_regs         = [];
702
-        // 4 cases we need to account for:
703
-        // 1 old attendee_details row with a quantity of X (no mer)
704
-        // Y old attendee_details rows with a quantity of 1 (no mer) joined by their common registration_id
705
-        // Y old attendee_details rows with a quantity of x (because of mer)
706
-        // Y old attendee_details rows with a quantity of 1 (because of mer) joined by wp_events_multi_event_registration_id_group
707
-        for ($count = 1; $count <= $regs_on_this_row; $count++) {
708
-            // sum regs on older rows
709
-            $regs_on_this_event_and_txn = $this->_sum_old_attendees_on_old_txn($old_attendee, true);
710
-            $cols_n_values              = [
711
-                'EVT_ID'           => $new_event_id,
712
-                'ATT_ID'           => $new_attendee_id,
713
-                'TXN_ID'           => $new_txn_id,
714
-                'TKT_ID'           => $ticket_id,
715
-                'STS_ID'           => $STS_ID,
716
-                'REG_date'         => $this->get_migration_script()->convert_date_string_to_utc(
717
-                    $this,
718
-                    $old_attendee,
719
-                    $old_attendee['date']
720
-                ),
721
-                'REG_final_price'  => $old_attendee['final_price'],
722
-                'REG_session'      => substr($old_attendee['attendee_session'], 0, 44),
723
-                'REG_code'         => sanitize_key($old_attendee['registration_id']),
724
-                'REG_url_link'     => sanitize_key($old_attendee['registration_id'] . '-' . $count),
725
-                'REG_count'        => $regs_on_this_event_and_txn + $count,
726
-                'REG_group_size'   => $this->_sum_old_attendees_on_old_txn($old_attendee),
727
-                'REG_att_is_going' => true,
728
-                'REG_deleted'      => false,
729
-            ];
730
-            $data_types                 = [
731
-                '%d',// EVT_ID
732
-                '%d',// ATT_ID
733
-                '%d',// TXN_ID
734
-                '%d',// TKT_ID
735
-                '%s',// STS_ID
736
-                '%s',// REG_date
737
-                '%f',// REG_final_price
738
-                '%s',// REG_session
739
-                '%s',// REG_code
740
-                '%s',// REG_url_link
741
-                '%d',// REG_count
742
-                '%d',// REG_group_size
743
-                '%d',// REG_att_is_going
744
-                '%d',// REG_deleted
745
-            ];
746
-            $success                    = $wpdb->insert($this->_new_reg_table, $cols_n_values, $data_types);
747
-            if (! $success) {
748
-                $this->add_error(
749
-                    $this->get_migration_script()->_create_error_message_for_db_insertion(
750
-                        $this->_old_table,
751
-                        $old_attendee,
752
-                        $this->_new_reg_table,
753
-                        $cols_n_values,
754
-                        $data_types
755
-                    )
756
-                );
757
-                return [];
758
-            }
759
-            $cols_n_values['REG_ID'] = $wpdb->insert_id;
760
-            $new_regs[]              = $wpdb->insert_id;
761
-        }
762
-        $this->_add_regs_to_ticket_and_datetimes($ticket_id, count($new_regs), $STS_ID);
763
-        return $new_regs;
764
-    }
765
-
766
-
767
-    /**
768
-     * Increments the sold values on the ticket and its related datetimes by the amount sold,
769
-     * which should be done directly after adding the rows. Yes this means we're constantly incrementing
770
-     * the sold amounts as we go, and is less efficient than a single big query,
771
-     * but it's safer because we KNOW these regs have been added, rather than inferring
772
-     * that they WILL be added (because the attendees stage runs nearly last during
773
-     * the migration script)
774
-     *
775
-     * @param int    $new_ticket_id
776
-     * @param int    $quantity_sold
777
-     * @param string $STS_ID
778
-     * @return boolean whether they were successfully updated or not
779
-     */
780
-    protected function _add_regs_to_ticket_and_datetimes(int $new_ticket_id, int $quantity_sold, string $STS_ID): bool
781
-    {
782
-        if ($STS_ID != 'RAP') {
783
-            return true;
784
-        }
785
-        global $wpdb;
786
-        $success =
787
-            $wpdb->query(
788
-                $wpdb->prepare(
789
-                    "UPDATE $this->_new_ticket_table SET TKT_sold=TKT_sold+%d WHERE TKT_ID=%d",
790
-                    $quantity_sold,
791
-                    $new_ticket_id
792
-                )
793
-            );
794
-        if ($success) {
795
-            // get the ticket's datetimes, and increment them too
796
-            $success_update_datetimes = $wpdb->query(
797
-                $wpdb->prepare(
798
-                    "UPDATE $this->_new_ticket_table TKT
270
+		$this->_old_mer_table             = $wpdb->prefix . "events_multi_event_registration_id_group";
271
+		$this->_new_attendee_cpt_table    = $wpdb->posts;
272
+		$this->_new_attendee_meta_table   = $wpdb->prefix . "esp_attendee_meta";
273
+		$this->_new_reg_table             = $wpdb->prefix . "esp_registration";
274
+		$this->_new_transaction_table     = $wpdb->prefix . "esp_transaction";
275
+		$this->_new_payment_table         = $wpdb->prefix . "esp_payment";
276
+		$this->_new_ticket_table          = $wpdb->prefix . "esp_ticket";
277
+		$this->_new_ticket_datetime_table = $wpdb->prefix . "esp_datetime_ticket";
278
+		$this->_new_datetime_table        = $wpdb->prefix . "esp_datetime";
279
+		$this->_new_datetime_ticket_table = $wpdb->prefix . "esp_datetime_ticket";
280
+		$this->_new_price_table           = $wpdb->prefix . "esp_price";
281
+		$this->_new_ticket_price_table    = $wpdb->prefix . "esp_ticket_price";
282
+		parent::__construct();
283
+	}
284
+
285
+
286
+	/**
287
+	 * @throws EE_Error
288
+	 */
289
+	protected function _migrate_old_row($old_row)
290
+	{
291
+		// first check if there's already a new attendee with similar characteristics
292
+		$new_att_id = $this->_find_attendee_cpt_matching($old_row);
293
+		if (! $new_att_id) {
294
+			$new_att_id = $this->_insert_new_attendee_cpt($old_row);
295
+			if (! $new_att_id) {
296
+				// if we couldn't even make an attendee, abandon all hope
297
+				return;
298
+			}
299
+			$new_att_meta_id = $this->_insert_attendee_meta_row($old_row, $new_att_id);
300
+			if ($new_att_meta_id) {
301
+				$this->get_migration_script()->set_mapping(
302
+					$this->_old_table,
303
+					$old_row['id'],
304
+					$this->_new_attendee_meta_table,
305
+					$new_att_meta_id
306
+				);
307
+			}
308
+		}
309
+		$this->get_migration_script()->set_mapping(
310
+			$this->_old_table,
311
+			$old_row['id'],
312
+			$this->_new_attendee_cpt_table,
313
+			$new_att_id
314
+		);
315
+
316
+		$txn_id = $this->_insert_new_transaction($old_row);
317
+		if (! $txn_id) {
318
+			// if we couldn't make the transaction, also abandon all hope
319
+			return;
320
+		}
321
+		$this->get_migration_script()->set_mapping(
322
+			$this->_old_table,
323
+			$old_row['id'],
324
+			$this->_new_transaction_table,
325
+			$txn_id
326
+		);
327
+		$pay_id = $this->_insert_new_payment($old_row, $txn_id);
328
+		if ($pay_id) {
329
+			$this->get_migration_script()->set_mapping(
330
+				$this->_old_table,
331
+				$old_row['id'],
332
+				$this->_new_payment_table,
333
+				$pay_id
334
+			);
335
+		}
336
+
337
+
338
+		// even if there was no payment, we can go ahead with adding the reg
339
+		$new_regs = $this->_insert_new_registrations($old_row, $new_att_id, $txn_id);
340
+		if ($new_regs) {
341
+			$this->get_migration_script()->set_mapping(
342
+				$this->_old_table,
343
+				$old_row['id'],
344
+				$this->_new_reg_table,
345
+				$new_regs
346
+			);
347
+		}
348
+	}
349
+
350
+
351
+	/**
352
+	 * Checks if there's already an attendee CPT in the db that has the same
353
+	 * first and last name, and email. If so, returns its ID as an int.
354
+	 *
355
+	 * @param array $old_attendee
356
+	 * @return int
357
+	 * @global wpdb $wpdb
358
+	 */
359
+	private function _find_attendee_cpt_matching(array $old_attendee): int
360
+	{
361
+		global $wpdb;
362
+		$existing_attendee_id =
363
+			$wpdb->get_var(
364
+				$wpdb->prepare(
365
+					"SELECT id FROM " .
366
+					$this->_new_attendee_cpt_table .
367
+					" AS cpt INNER JOIN " .
368
+					$this->_new_attendee_meta_table .
369
+					" 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",
370
+					$old_attendee['fname'],
371
+					$old_attendee['lname'],
372
+					$old_attendee['email']
373
+				)
374
+			);
375
+		return intval($existing_attendee_id);
376
+	}
377
+
378
+
379
+	private function _insert_new_attendee_cpt($old_attendee): int
380
+	{
381
+		global $wpdb;
382
+		$cols_n_values = [
383
+			'post_title'    => stripslashes($old_attendee['fname'] . " " . $old_attendee['lname']),// ATT_full_name
384
+			'post_content'  => '',// ATT_bio
385
+			'post_name'     => sanitize_title($old_attendee['fname'] . "-" . $old_attendee['lname']),// ATT_slug
386
+			'post_date'     => $this->get_migration_script()->convert_date_string_to_utc(
387
+				$this,
388
+				$old_attendee,
389
+				$old_attendee['date']
390
+			),// ATT_created
391
+			'post_excerpt'  => '',// ATT_short_bio
392
+			'post_modified' => $this->get_migration_script()->convert_date_string_to_utc(
393
+				$this,
394
+				$old_attendee,
395
+				$old_attendee['date']
396
+			),// ATT_modified
397
+			'post_author'   => 0,// ATT_author
398
+			'post_parent'   => 0,// ATT_parent
399
+			'post_type'     => EspressoPostType::ATTENDEES,
400
+			'post_status'   => 'publish',// status
401
+		];
402
+		$data_types    = [
403
+			'%s',// ATT_full_name
404
+			'%s',// ATT_bio
405
+			'%s',// ATT_slug
406
+			'%s',// ATT_created
407
+			'%s',// ATT_short_bio
408
+			'%s',// ATT_modified
409
+			'%d',// ATT_author
410
+			'%d',// ATT_parent
411
+			'%s',// post_type
412
+			'%s',// status
413
+		];
414
+		$success       = $wpdb->insert($this->_new_attendee_cpt_table, $cols_n_values, $data_types);
415
+		if (! $success) {
416
+			$this->add_error(
417
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
418
+					$this->_old_table,
419
+					$old_attendee,
420
+					$this->_new_attendee_cpt_table,
421
+					$cols_n_values,
422
+					$data_types
423
+				)
424
+			);
425
+			return 0;
426
+		}
427
+		return $wpdb->insert_id;
428
+	}
429
+
430
+
431
+	private function _insert_attendee_meta_row($old_attendee, $new_attendee_cpt_id): int
432
+	{
433
+		global $wpdb;
434
+		// get the state and country ids from the old row
435
+		try {
436
+			$new_country     =
437
+				$this->get_migration_script()->get_or_create_country(stripslashes($old_attendee['country_id']));
438
+			$new_country_iso = $new_country['CNT_ISO'];
439
+		} catch (EE_Error $exception) {
440
+			$new_country_iso = $this->get_migration_script()->get_default_country_iso();
441
+		}
442
+		try {
443
+			$new_state    =
444
+				$this->get_migration_script()->get_or_create_state(
445
+					stripslashes($old_attendee['state']),
446
+					$new_country_iso
447
+				);
448
+			$new_state_id = $new_state['STA_ID'];
449
+		} catch (EE_Error $exception) {
450
+			$new_state_id = 0;
451
+		}
452
+		$cols_n_values = [
453
+			'ATT_ID'       => $new_attendee_cpt_id,
454
+			'ATT_fname'    => stripslashes($old_attendee['fname']),
455
+			'ATT_lname'    => stripslashes($old_attendee['lname']),
456
+			'ATT_address'  => stripslashes($old_attendee['address']),
457
+			'ATT_address2' => stripslashes($old_attendee['address2']),
458
+			'ATT_city'     => stripslashes($old_attendee['city']),
459
+			'STA_ID'       => $new_state_id,
460
+			'CNT_ISO'      => $new_country_iso,
461
+			'ATT_zip'      => stripslashes($old_attendee['zip']),
462
+			'ATT_email'    => stripslashes($old_attendee['email']),
463
+			'ATT_phone'    => stripslashes($old_attendee['phone']),
464
+		];
465
+		$data_types    = [
466
+			'%d',// ATT_ID
467
+			'%s',// ATT_fname
468
+			'%s',// ATT_lname
469
+			'%s',// ATT_address
470
+			'%s',// ATT_address2
471
+			'%s',// ATT_city
472
+			'%d',// STA_ID
473
+			'%s',// CNT_ISO
474
+			'%s',// ATT_zip
475
+			'%s',// ATT_email
476
+			'%s',// ATT_phone
477
+		];
478
+		$success       = $wpdb->insert($this->_new_attendee_meta_table, $cols_n_values, $data_types);
479
+		if (! $success) {
480
+			$this->add_error(
481
+				$this->get_migration_script()->_create_error_message_for_db_insertion(
482
+					$this->_old_table,
483
+					$old_attendee,
484
+					$this->_new_attendee_meta_table,
485
+					$cols_n_values,
486
+					$data_types
487
+				)
488
+			);
489
+			return 0;
490
+		}
491
+		return $wpdb->insert_id;
492
+	}
493
+
494
+
495
+	/**
496
+	 * Note: we don't necessarily create a new transaction for each attendee row.
497
+	 * Only if the old attendee 'is_primary' is true; otherwise we find the old attendee row that
498
+	 * 'is_primary' and has the same 'txn_id', then we return ITS new transaction id
499
+	 *
500
+	 * @param array $old_attendee
501
+	 * @return int new transaction id
502
+	 * @throws EE_Error
503
+	 * @global wpdb $wpdb
504
+	 */
505
+	private function _insert_new_transaction(array $old_attendee): int
506
+	{
507
+		global $wpdb;
508
+
509
+		// first: let's check for an existing transaction for this old attendee
510
+		if (intval($old_attendee['is_primary'])) {// primary attendee, so create txn
511
+			$txn_id =
512
+				$this->get_migration_script()->get_mapping_new_pk(
513
+					$this->_old_table,
514
+					intval($old_attendee['id']),
515
+					$this->_new_transaction_table
516
+				);
517
+		} else { // non-primary attendee, so find its primary attendee's transaction
518
+			$primary_attendee_old_id =
519
+				$wpdb->get_var(
520
+					$wpdb->prepare(
521
+						"SELECT id FROM " . $this->_old_table . " WHERE is_primary=1 and registration_id=%s",
522
+						$old_attendee['registration_id']
523
+					)
524
+				);
525
+			if (! $primary_attendee_old_id) {
526
+				$primary_attendee        =
527
+					$this->_find_mer_primary_attendee_using_mer_tables($old_attendee['registration_id']);
528
+				$primary_attendee_old_id = is_array($primary_attendee) ? $primary_attendee['id'] : null;
529
+			}
530
+			$txn_id =
531
+				$this->get_migration_script()->get_mapping_new_pk(
532
+					$this->_old_table,
533
+					intval($primary_attendee_old_id),
534
+					$this->_new_transaction_table
535
+				);
536
+			if (! $txn_id) {
537
+				$this->add_error(
538
+					sprintf(
539
+						esc_html__(
540
+							"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.",
541
+							"event_espresso"
542
+						),
543
+						$this->_json_encode($old_attendee),
544
+						$primary_attendee_old_id
545
+					)
546
+				);
547
+				$txn_id = 0;
548
+			}
549
+		}
550
+		// if there isn't yet a transaction row for this, create one
551
+		// (so even if it was a non-primary attendee with no EE3 primary attendee,
552
+		// it ought to have SOME transaction, so we'll make one)
553
+		if (! $txn_id) {
554
+			// maps 3.1 payment stati onto 4.1 transaction stati
555
+			$txn_status_mapping = [
556
+				'Completed'        => 'TCM',
557
+				'Pending'          => 'TIN',
558
+				'Payment Declined' => 'TIN',
559
+				'Incomplete'       => 'TIN',
560
+				'Not Completed'    => 'TIN',
561
+				'Cancelled'        => 'TIN',
562
+				'Declined'         => 'TIN',
563
+			];
564
+			$STS_ID             = $txn_status_mapping[ $old_attendee['payment_status'] ] ?? 'TIN';
565
+			$cols_n_values      = [
566
+				'TXN_timestamp' => $this->get_migration_script()->convert_date_string_to_utc(
567
+					$this,
568
+					$old_attendee,
569
+					$old_attendee['date']
570
+				),
571
+				'TXN_total'     => floatval($old_attendee['total_cost']),
572
+				'TXN_paid'      => floatval($old_attendee['amount_pd']),
573
+				'STS_ID'        => $STS_ID,
574
+				'TXN_hash_salt' => $old_attendee['hashSalt'],
575
+			];
576
+			$data_types         = [
577
+				'%s',// TXN_timestamp
578
+				'%f',// TXN_total
579
+				'%f',// TXN_paid
580
+				'%s',// STS_ID
581
+				'%s',// TXN_hash_salt
582
+			];
583
+			$success            = $wpdb->insert($this->_new_transaction_table, $cols_n_values, $data_types);
584
+			if (! $success) {
585
+				$this->add_error(
586
+					$this->get_migration_script()->_create_error_message_for_db_insertion(
587
+						$this->_old_table,
588
+						$old_attendee,
589
+						$this->_new_transaction_table,
590
+						$cols_n_values,
591
+						$data_types
592
+					)
593
+				);
594
+				return 0;
595
+			}
596
+			$txn_id = $wpdb->insert_id;
597
+		}
598
+
599
+		return $txn_id;
600
+	}
601
+
602
+
603
+	/**
604
+	 * Detects if the MER tables exist
605
+	 *
606
+	 * @return boolean
607
+	 * @global wpdb $wpdb
608
+	 */
609
+	private function _mer_tables_exist(): ?bool
610
+	{
611
+		if ($this->_mer_tables_exist === null) {
612
+			global $wpdb;
613
+
614
+			if ($wpdb->get_var("SHOW TABLES LIKE '$this->_old_mer_table'") != $this->_old_mer_table) {
615
+				$this->_mer_tables_exist = false;
616
+			} else {
617
+				$this->_mer_tables_exist = true;
618
+			}
619
+		}
620
+		return $this->_mer_tables_exist;
621
+	}
622
+
623
+
624
+	/**
625
+	 * Gets the 4.1 registration's status given the 3.1 attendee row. We consider
626
+	 * whether the event required pre-approval or not,a dn the 4.1 payment status.
627
+	 *
628
+	 * @param array $old_attendee_row
629
+	 * @return string
630
+	 * @global wpdb $wpdb
631
+	 */
632
+	private function _get_reg_status_for_old_payment_status(array $old_attendee_row): string
633
+	{
634
+		// need event default reg status and if pre_approval was required
635
+		global $wpdb;
636
+		$event_required_pre_approval =
637
+			$wpdb->get_var(
638
+				$wpdb->prepare(
639
+					"SELECT require_pre_approval FROM " . $wpdb->prefix . "events_detail WHERE id = %d",
640
+					$old_attendee_row['event_id']
641
+				)
642
+			);
643
+		return $this->get_migration_script()->convert_3_1_payment_status_to_4_1_STS_ID(
644
+			$old_attendee_row['payment_status'],
645
+			intval($event_required_pre_approval) && intval($old_attendee_row['pre_approve'])
646
+		);
647
+	}
648
+
649
+
650
+	/**
651
+	 * Adds however many registrations are indicated by the old attendee's QUANTITY field,
652
+	 * and returns an array of their IDs
653
+	 *
654
+	 * @param array $old_attendee
655
+	 * @param int   $new_attendee_id
656
+	 * @param int   $new_txn_id
657
+	 * @return array of new registration ids
658
+	 * @throws EE_Error
659
+	 * @global wpdb $wpdb
660
+	 */
661
+	private function _insert_new_registrations(array $old_attendee, int $new_attendee_id, int $new_txn_id): array
662
+	{
663
+		global $wpdb;
664
+
665
+		$STS_ID       = $this->_get_reg_status_for_old_payment_status($old_attendee);
666
+		$new_event_id =
667
+			$this->get_migration_script()->get_mapping_new_pk(
668
+				$wpdb->prefix . 'events_detail',
669
+				$old_attendee['event_id'],
670
+				$wpdb->posts
671
+			);
672
+		if (! $new_event_id) {
673
+			$this->add_error(
674
+				sprintf(
675
+					esc_html__(
676
+						"Could not find NEW event CPT ID for old event '%d' on old attendee %s",
677
+						"event_espresso"
678
+					),
679
+					$old_attendee['event_id'],
680
+					$this->_json_encode($old_attendee)
681
+				)
682
+			);
683
+		}
684
+
685
+		$ticket_id = $this->_try_to_find_new_ticket_id($old_attendee, $new_event_id);
686
+		if (! $ticket_id) {
687
+			$ticket_id = $this->_insert_new_ticket_because_none_found($old_attendee, $new_event_id);
688
+			$this->add_error(
689
+				sprintf(
690
+					esc_html__(
691
+						'Could not find a ticket for old attendee with id %d for new event %d, so created a new ticket with id %d',
692
+						'event_espresso'
693
+					),
694
+					$old_attendee['id'],
695
+					$new_event_id,
696
+					$ticket_id
697
+				)
698
+			);
699
+		}
700
+		$regs_on_this_row = intval($old_attendee['quantity']);
701
+		$new_regs         = [];
702
+		// 4 cases we need to account for:
703
+		// 1 old attendee_details row with a quantity of X (no mer)
704
+		// Y old attendee_details rows with a quantity of 1 (no mer) joined by their common registration_id
705
+		// Y old attendee_details rows with a quantity of x (because of mer)
706
+		// Y old attendee_details rows with a quantity of 1 (because of mer) joined by wp_events_multi_event_registration_id_group
707
+		for ($count = 1; $count <= $regs_on_this_row; $count++) {
708
+			// sum regs on older rows
709
+			$regs_on_this_event_and_txn = $this->_sum_old_attendees_on_old_txn($old_attendee, true);
710
+			$cols_n_values              = [
711
+				'EVT_ID'           => $new_event_id,
712
+				'ATT_ID'           => $new_attendee_id,
713
+				'TXN_ID'           => $new_txn_id,
714
+				'TKT_ID'           => $ticket_id,
715
+				'STS_ID'           => $STS_ID,
716
+				'REG_date'         => $this->get_migration_script()->convert_date_string_to_utc(
717
+					$this,
718
+					$old_attendee,
719
+					$old_attendee['date']
720
+				),
721
+				'REG_final_price'  => $old_attendee['final_price'],
722
+				'REG_session'      => substr($old_attendee['attendee_session'], 0, 44),
723
+				'REG_code'         => sanitize_key($old_attendee['registration_id']),
724
+				'REG_url_link'     => sanitize_key($old_attendee['registration_id'] . '-' . $count),
725
+				'REG_count'        => $regs_on_this_event_and_txn + $count,
726
+				'REG_group_size'   => $this->_sum_old_attendees_on_old_txn($old_attendee),
727
+				'REG_att_is_going' => true,
728
+				'REG_deleted'      => false,
729
+			];
730
+			$data_types                 = [
731
+				'%d',// EVT_ID
732
+				'%d',// ATT_ID
733
+				'%d',// TXN_ID
734
+				'%d',// TKT_ID
735
+				'%s',// STS_ID
736
+				'%s',// REG_date
737
+				'%f',// REG_final_price
738
+				'%s',// REG_session
739
+				'%s',// REG_code
740
+				'%s',// REG_url_link
741
+				'%d',// REG_count
742
+				'%d',// REG_group_size
743
+				'%d',// REG_att_is_going
744
+				'%d',// REG_deleted
745
+			];
746
+			$success                    = $wpdb->insert($this->_new_reg_table, $cols_n_values, $data_types);
747
+			if (! $success) {
748
+				$this->add_error(
749
+					$this->get_migration_script()->_create_error_message_for_db_insertion(
750
+						$this->_old_table,
751
+						$old_attendee,
752
+						$this->_new_reg_table,
753
+						$cols_n_values,
754
+						$data_types
755
+					)
756
+				);
757
+				return [];
758
+			}
759
+			$cols_n_values['REG_ID'] = $wpdb->insert_id;
760
+			$new_regs[]              = $wpdb->insert_id;
761
+		}
762
+		$this->_add_regs_to_ticket_and_datetimes($ticket_id, count($new_regs), $STS_ID);
763
+		return $new_regs;
764
+	}
765
+
766
+
767
+	/**
768
+	 * Increments the sold values on the ticket and its related datetimes by the amount sold,
769
+	 * which should be done directly after adding the rows. Yes this means we're constantly incrementing
770
+	 * the sold amounts as we go, and is less efficient than a single big query,
771
+	 * but it's safer because we KNOW these regs have been added, rather than inferring
772
+	 * that they WILL be added (because the attendees stage runs nearly last during
773
+	 * the migration script)
774
+	 *
775
+	 * @param int    $new_ticket_id
776
+	 * @param int    $quantity_sold
777
+	 * @param string $STS_ID
778
+	 * @return boolean whether they were successfully updated or not
779
+	 */
780
+	protected function _add_regs_to_ticket_and_datetimes(int $new_ticket_id, int $quantity_sold, string $STS_ID): bool
781
+	{
782
+		if ($STS_ID != 'RAP') {
783
+			return true;
784
+		}
785
+		global $wpdb;
786
+		$success =
787
+			$wpdb->query(
788
+				$wpdb->prepare(
789
+					"UPDATE $this->_new_ticket_table SET TKT_sold=TKT_sold+%d WHERE TKT_ID=%d",
790
+					$quantity_sold,
791
+					$new_ticket_id
792
+				)
793
+			);
794
+		if ($success) {
795
+			// get the ticket's datetimes, and increment them too
796
+			$success_update_datetimes = $wpdb->query(
797
+				$wpdb->prepare(
798
+					"UPDATE $this->_new_ticket_table TKT
799 799
 				INNER JOIN $this->_new_ticket_datetime_table as DTK ON TKT.TKT_ID = DTK.TKT_ID
800 800
 				INNER JOIN $this->_new_datetime_table as DTT ON DTK.DTT_ID = DTT.DTT_ID
801 801
 				SET DTT.DTT_sold = DTT.DTT_sold + %d WHERE TKT.TKT_ID = %d",
802
-                    $quantity_sold,
803
-                    $new_ticket_id
804
-                )
805
-            );
806
-            if (! $success_update_datetimes) {
807
-                $this->add_error(
808
-                    sprintf(
809
-                        esc_html__(
810
-                            "Could not update datetimes related to ticket with ID %d's TKT_sold by %d because %s",
811
-                            "event_espresso"
812
-                        ),
813
-                        $new_ticket_id,
814
-                        $quantity_sold,
815
-                        $wpdb->last_error
816
-                    )
817
-                );
818
-            }
819
-        } else {
820
-            $this->add_error(
821
-                sprintf(
822
-                    esc_html__("Could not update ticket with ID %d's TKT_sold by %d because %s", "event_espresso"),
823
-                    $new_ticket_id,
824
-                    $quantity_sold,
825
-                    $wpdb->last_error
826
-                )
827
-            );
828
-        }
829
-        return true;
830
-    }
831
-
832
-
833
-    /**
834
-     * Makes a guess at which ticket is the one the attendee purchased.
835
-     * Obviously, the old attendee's event_id narrows it down quite a bit;
836
-     * then the old attendee's orig_price and event_time, and price_option can uniquely identify the ticket
837
-     * however, if we don't find an exact match, see if any of those conditions match;
838
-     * and lastly if none of that works, just use the first ticket for the event we find
839
-     *
840
-     * @param array $old_attendee
841
-     * @param       $new_event_id
842
-     * @return string|null
843
-     */
844
-    private function _try_to_find_new_ticket_id(array $old_attendee, $new_event_id): ?string
845
-    {
846
-        global $wpdb;
847
-        $tickets_table          = $this->_new_ticket_table;
848
-        $datetime_tickets_table = $this->_new_ticket_datetime_table;
849
-        $datetime_table         = $this->_new_datetime_table;
850
-
851
-        $old_att_price_option = $old_attendee['price_option'];
852
-        $old_att_price        = floatval($old_attendee['orig_price']);
853
-
854
-        $old_att_start_date = $old_attendee['start_date'];
855
-        $old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
856
-        $old_att_datetime   =
857
-            $this->get_migration_script()->convert_date_string_to_utc(
858
-                $this,
859
-                $old_attendee,
860
-                "$old_att_start_date $old_att_start_time:00"
861
-            );
862
-        // add all conditions to an array from which we can SHIFT conditions off in order to widen our search
863
-        // the most important condition should be last, as it will be array_shift'ed off last
864
-        $conditions           = [
865
-            $wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
866
-            $wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
867
-            $wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
868
-            $wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
869
-        ];
870
-        $select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
802
+					$quantity_sold,
803
+					$new_ticket_id
804
+				)
805
+			);
806
+			if (! $success_update_datetimes) {
807
+				$this->add_error(
808
+					sprintf(
809
+						esc_html__(
810
+							"Could not update datetimes related to ticket with ID %d's TKT_sold by %d because %s",
811
+							"event_espresso"
812
+						),
813
+						$new_ticket_id,
814
+						$quantity_sold,
815
+						$wpdb->last_error
816
+					)
817
+				);
818
+			}
819
+		} else {
820
+			$this->add_error(
821
+				sprintf(
822
+					esc_html__("Could not update ticket with ID %d's TKT_sold by %d because %s", "event_espresso"),
823
+					$new_ticket_id,
824
+					$quantity_sold,
825
+					$wpdb->last_error
826
+				)
827
+			);
828
+		}
829
+		return true;
830
+	}
831
+
832
+
833
+	/**
834
+	 * Makes a guess at which ticket is the one the attendee purchased.
835
+	 * Obviously, the old attendee's event_id narrows it down quite a bit;
836
+	 * then the old attendee's orig_price and event_time, and price_option can uniquely identify the ticket
837
+	 * however, if we don't find an exact match, see if any of those conditions match;
838
+	 * and lastly if none of that works, just use the first ticket for the event we find
839
+	 *
840
+	 * @param array $old_attendee
841
+	 * @param       $new_event_id
842
+	 * @return string|null
843
+	 */
844
+	private function _try_to_find_new_ticket_id(array $old_attendee, $new_event_id): ?string
845
+	{
846
+		global $wpdb;
847
+		$tickets_table          = $this->_new_ticket_table;
848
+		$datetime_tickets_table = $this->_new_ticket_datetime_table;
849
+		$datetime_table         = $this->_new_datetime_table;
850
+
851
+		$old_att_price_option = $old_attendee['price_option'];
852
+		$old_att_price        = floatval($old_attendee['orig_price']);
853
+
854
+		$old_att_start_date = $old_attendee['start_date'];
855
+		$old_att_start_time = $this->get_migration_script()->convertTimeFromAMPM($old_attendee['event_time']);
856
+		$old_att_datetime   =
857
+			$this->get_migration_script()->convert_date_string_to_utc(
858
+				$this,
859
+				$old_attendee,
860
+				"$old_att_start_date $old_att_start_time:00"
861
+			);
862
+		// add all conditions to an array from which we can SHIFT conditions off in order to widen our search
863
+		// the most important condition should be last, as it will be array_shift'ed off last
864
+		$conditions           = [
865
+			$wpdb->prepare("$datetime_table.DTT_EVT_start = %s", $old_att_datetime),// times match?
866
+			$wpdb->prepare("$tickets_table.TKT_price = %f", $old_att_price),// prices match?
867
+			$wpdb->prepare("$tickets_table.TKT_name = %s", $old_att_price_option),// names match?
868
+			$wpdb->prepare("$datetime_table.EVT_ID = %d", $new_event_id),// events match?
869
+		];
870
+		$select_and_join_part = "SELECT $tickets_table.TKT_ID FROM $tickets_table INNER JOIN
871 871
 			$datetime_tickets_table ON $tickets_table.TKT_ID = $datetime_tickets_table.TKT_ID INNER JOIN
872 872
 			$datetime_table ON $datetime_tickets_table.DTT_ID = $datetime_table.DTT_ID";
873
-        // start running queries, widening search each time by removing a condition
874
-        do {
875
-            $full_query      = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
876
-            $ticket_id_found = $wpdb->get_var($full_query);
877
-            array_shift($conditions);
878
-        } while (! $ticket_id_found && $conditions);
879
-        return $ticket_id_found;
880
-    }
881
-
882
-
883
-    /**
884
-     * If we couldn't find a 4.1 ticket for a 3.1 attendee row, this function creates one;
885
-     * and it also tries to find a datetime that works, and inserts a price, and associates
886
-     * the new ticket to that datetime and price.
887
-     *
888
-     * @return int ticket id
889
-     */
890
-    private function _insert_new_ticket_because_none_found($old_attendee, $new_event_id): int
891
-    {
892
-        global $wpdb;
893
-        $old_att_price_option = $old_attendee['price_option'];
894
-        $old_att_price        = floatval($old_attendee['orig_price']);
895
-
896
-        $old_att_start_date     = $old_attendee['start_date'];
897
-        $old_att_start_time     = $this->get_migration_script()->convertTimeFromAMPM(
898
-            $old_attendee['event_time']
899
-        );
900
-        $old_att_start_datetime =
901
-            $this->get_migration_script()->convert_date_string_to_utc(
902
-                $this,
903
-                $old_attendee,
904
-                "$old_att_start_date $old_att_start_time:00"
905
-            );
906
-        $old_att_end_datetime   = null;
907
-
908
-
909
-        // insert new datetime unless we find one
910
-        $datetime_id =
911
-            $wpdb->get_var(
912
-                $wpdb->prepare(
913
-                    "SELECT DTT_ID FROM " .
914
-                    $this->_new_datetime_table .
915
-                    " WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1",
916
-                    $old_att_start_datetime,
917
-                    $new_event_id
918
-                ),
919
-                ARRAY_A
920
-            );
921
-        if (! $datetime_id) {
922
-            $old_att_end_date     = $old_attendee['start_date'];
923
-            $old_att_end_time     = $this->get_migration_script()->convertTimeFromAMPM(
924
-                $old_attendee['event_time']
925
-            );
926
-            $old_att_end_datetime = $this->get_migration_script()->convert_date_string_to_utc(
927
-                $this,
928
-                $old_attendee,
929
-                "$old_att_end_date $old_att_end_time:00"
930
-            );
931
-            $wpdb->insert(
932
-                $this->_new_datetime_table,
933
-                [
934
-                    'EVT_ID'        => $new_event_id,
935
-                    'DTT_EVT_start' => $old_att_start_datetime,
936
-                    'DTT_EVT_end'   => $old_att_end_datetime,
937
-                    'DTT_deleted'   => true,
938
-                ],
939
-                [
940
-                    '%d',// EVT_ID
941
-                    '%s',// DTT_EVT_start
942
-                    '%s',// DTT_EVT_end
943
-                    '%d',// DTT_deleted
944
-                ]
945
-            );
946
-            $datetime_id = $wpdb->insert_id;
947
-        }
948
-
949
-        // insert new ticket
950
-        $wpdb->insert(
951
-            $wpdb->prefix . 'esp_ticket',
952
-            [
953
-                'TKT_name'       => $old_att_price_option,
954
-                'TKT_qty'        => -1,
955
-                'TKT_price'      => $old_att_price,
956
-                'TKT_start_date' => $old_att_start_datetime,
957
-                // we really have no clue what the time should be, but at least it was available when they attended
958
-                'TKT_end_date'   => $old_att_end_datetime,
959
-
960
-            ],
961
-            [
962
-                '%s',// name
963
-                '%d',// qty
964
-                '%d',// price
965
-                '%s',// start_date
966
-                '%s',// end_date
967
-            ]
968
-        );
969
-        $ticket_id = $wpdb->insert_id;
970
-        // associate the ticket with the datetime we found earlier
971
-        $wpdb->insert(
972
-            $this->_new_datetime_ticket_table,
973
-            [
974
-                'DTT_ID' => $datetime_id,
975
-                'TKT_ID' => $ticket_id,
976
-            ],
977
-            [
978
-                '%d',// DTT_ID
979
-                '%d',// TKT_ID
980
-            ]
981
-        );
982
-        // insert new price
983
-        $wpdb->insert(
984
-            $this->_new_price_table,
985
-            [
986
-                'PRC_amount'  => $old_att_price,
987
-                'PRT_ID'      => EE_DMS_4_1_0_prices::price_type_base,
988
-                'PRC_name'    => $old_att_price_option,
989
-                'PRC_deleted' => true,
990
-            ],
991
-            [
992
-                '%d',// PRC_amount
993
-                '%d',// PRT_ID
994
-                '%s',// PRC_name
995
-                '%d',// PRC_deleted
996
-            ]
997
-        );
998
-        $price_id = $wpdb->insert_id;
999
-        // associate the price to the ticket
1000
-        $wpdb->insert(
1001
-            $this->_new_ticket_price_table,
1002
-            [
1003
-                'TKT_ID' => $ticket_id,
1004
-                'PRC_ID' => $price_id,
1005
-            ],
1006
-            [
1007
-                '%d',// TKT_ID
1008
-                '%d',// PRC_ID
1009
-            ]
1010
-        );
1011
-        return $ticket_id;
1012
-    }
1013
-
1014
-
1015
-    /**
1016
-     * Counts all the registrations on this transaction. If $count_only_older is TRUE then returns the number added SO
1017
-     * FAR (ie, only considers attendee rows with an ID less than this one's), but if $count_only_older is FALSe
1018
-     * returns ALL
1019
-     *
1020
-     * @param array   $old_attendee_row
1021
-     * @param boolean $count_only_older true if you want the running count (ie, the total up to this row), and false if
1022
-     *                                  you want ALL
1023
-     * @return int
1024
-     * @global wpdb   $wpdb
1025
-     */
1026
-    private function _sum_old_attendees_on_old_txn(array $old_attendee_row, bool $count_only_older = false): int
1027
-    {
1028
-        global $wpdb;
1029
-        $count_only_older_sql = $count_only_older ? $wpdb->prepare(" AND id<%d", $old_attendee_row['id']) : '';
1030
-        $count                =
1031
-            intval(
1032
-                $wpdb->get_var(
1033
-                    $wpdb->prepare(
1034
-                        "SELECT SUM(quantity) FROM " .
1035
-                        $this->_old_table .
1036
-                        " WHERE registration_id=%s $count_only_older_sql",
1037
-                        $old_attendee_row['registration_id']
1038
-                    )
1039
-                )
1040
-            );
1041
-
1042
-        if ($this->_mer_tables_exist()) {
1043
-            // if MER exists, then it's a little tricky.
1044
-            // when users registered by adding items to the cart, and it was a
1045
-            // group registration requiring additional attendee INFO, then the attendee rows
1046
-            // DO NOT have the same registration_id (although they probably should have)
1047
-            // they are related just like MER attendee rows are related, through the MER group table
1048
-            // BUT we want to count all the MER attendee rows for the same registration
1049
-            $primary_attendee =
1050
-                $this->_find_mer_primary_attendee_using_mer_tables($old_attendee_row['registration_id']);
1051
-
1052
-            // No primary attendee to work with? Return the count.
1053
-            if (is_null($primary_attendee)) {
1054
-                return (int) $count;
1055
-            }
1056
-
1057
-            $count_using_mer_table =
1058
-                $wpdb->get_var(
1059
-                    $wpdb->prepare(
1060
-                        "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",
1061
-                        $old_attendee_row['event_id'],
1062
-                        $primary_attendee['registration_id']
1063
-                    )
1064
-                );
1065
-            $count                 = max($count_using_mer_table, $count);
1066
-        }
1067
-        return (int) $count;
1068
-    }
1069
-
1070
-
1071
-    private function _insert_new_payment($old_attendee, $new_txn_id): int
1072
-    {
1073
-        global $wpdb;
1074
-        // Only add a payment for primary attendees. Important note: partial payments were marked as "Pending".
1075
-        // Also, an offline payment had blank status (ie, "").
1076
-        $old_pay_stati_indicating_no_payment = ['', 'Incomplete', 'Not Completed'];
1077
-        // if this is for a primary 3.1 attendee which WASN'T free and has a completed, cancelled, or declined payment...
1078
-        if (
1079
-            intval($old_attendee['is_primary']) &&
1080
-            floatval($old_attendee['total_cost']) &&
1081
-            ! in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)
1082
-        ) {
1083
-            $pay_status_mapping = [
1084
-                'Completed'        => 'PAP',
1085
-                'Payment Declined' => 'PDC',
1086
-                'Cancelled'        => 'PCN',
1087
-                'Declined'         => 'PDC',
1088
-            ];
1089
-
1090
-            $by_admin = $old_attendee['payment'] == 'Admin';
1091
-
1092
-            // IE, if we don't recognize the status, assume payment failed
1093
-            $STS_ID = $pay_status_mapping[ $old_attendee['payment_status'] ] ?? 'PFL';
1094
-
1095
-            $cols_n_values = [
1096
-                'TXN_ID'               => $new_txn_id,
1097
-                'STS_ID'               => $STS_ID,
1098
-                'PAY_timestamp'        => $this->get_migration_script()->convert_date_string_to_utc(
1099
-                    $this,
1100
-                    $old_attendee,
1101
-                    $old_attendee['date']
1102
-                ),
1103
-                'PAY_method'           => 'CART',
1104
-                'PAY_amount'           => $old_attendee['amount_pd'],
1105
-                // 'txn_type' was used both for the gateway's name, and for other payment notes.
1106
-                'PAY_gateway'          => $old_attendee['txn_type'],
1107
-                'PAY_extra_accntng'    => $old_attendee['txn_type'],
1108
-                'PAY_gateway_response' => '',
1109
-                'PAY_txn_id_chq_nmbr'  => substr($old_attendee['txn_id'], 0, 32),
1110
-                'PAY_via_admin'        => $by_admin,
1111
-                'PAY_details'          => $old_attendee['transaction_details'],
1112
-            ];
1113
-            $data_types    = [
1114
-                '%d',// TXN_Id
1115
-                '%s',// STS_ID
1116
-                '%s',// PAY_timestamp
1117
-                '%s',// PAY_method
1118
-                '%f',// PAY_amount
1119
-                '%s',// PAY_gateway
1120
-                '%s',// PAY_gateway_response
1121
-                '%s',// PAY_txn_id_chq_nmbr
1122
-                '%d',// PAY_via_admin
1123
-                '%s',// PAY_details
1124
-            ];
1125
-            $success       = $wpdb->insert($this->_new_payment_table, $cols_n_values, $data_types);
1126
-            if (! $success) {
1127
-                $this->add_error(
1128
-                    $this->get_migration_script()->_create_error_message_for_db_insertion(
1129
-                        $this->_old_table,
1130
-                        $old_attendee,
1131
-                        $this->_new_attendee_cpt_table,
1132
-                        $cols_n_values,
1133
-                        $data_types
1134
-                    )
1135
-                );
1136
-                return 0;
1137
-            }
1138
-            return $wpdb->insert_id;
1139
-        }
1140
-        return 0;
1141
-    }
1142
-
1143
-
1144
-    /**
1145
-     * If MER is active, if you want ot find the other registrations on that attendee row
1146
-     *
1147
-     * @param string   $old_registration_id
1148
-     * @return array|null
1149
-     * @global wpdb $wpdb
1150
-     */
1151
-    private function _find_mer_primary_attendee_using_mer_tables(string $old_registration_id): ?array
1152
-    {
1153
-        if (! $this->_mer_tables_exist()) {
1154
-            return null;
1155
-        }
1156
-        global $wpdb;
1157
-        return $wpdb->get_row(
1158
-            $wpdb->prepare(
1159
-                "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",
1160
-                $old_registration_id
1161
-            ),
1162
-            ARRAY_A
1163
-        );
1164
-    }
873
+		// start running queries, widening search each time by removing a condition
874
+		do {
875
+			$full_query      = $select_and_join_part . " WHERE " . implode(" AND ", $conditions) . " LIMIT 1";
876
+			$ticket_id_found = $wpdb->get_var($full_query);
877
+			array_shift($conditions);
878
+		} while (! $ticket_id_found && $conditions);
879
+		return $ticket_id_found;
880
+	}
881
+
882
+
883
+	/**
884
+	 * If we couldn't find a 4.1 ticket for a 3.1 attendee row, this function creates one;
885
+	 * and it also tries to find a datetime that works, and inserts a price, and associates
886
+	 * the new ticket to that datetime and price.
887
+	 *
888
+	 * @return int ticket id
889
+	 */
890
+	private function _insert_new_ticket_because_none_found($old_attendee, $new_event_id): int
891
+	{
892
+		global $wpdb;
893
+		$old_att_price_option = $old_attendee['price_option'];
894
+		$old_att_price        = floatval($old_attendee['orig_price']);
895
+
896
+		$old_att_start_date     = $old_attendee['start_date'];
897
+		$old_att_start_time     = $this->get_migration_script()->convertTimeFromAMPM(
898
+			$old_attendee['event_time']
899
+		);
900
+		$old_att_start_datetime =
901
+			$this->get_migration_script()->convert_date_string_to_utc(
902
+				$this,
903
+				$old_attendee,
904
+				"$old_att_start_date $old_att_start_time:00"
905
+			);
906
+		$old_att_end_datetime   = null;
907
+
908
+
909
+		// insert new datetime unless we find one
910
+		$datetime_id =
911
+			$wpdb->get_var(
912
+				$wpdb->prepare(
913
+					"SELECT DTT_ID FROM " .
914
+					$this->_new_datetime_table .
915
+					" WHERE DTT_EVT_start=%s AND EVT_ID=%d LIMIT 1",
916
+					$old_att_start_datetime,
917
+					$new_event_id
918
+				),
919
+				ARRAY_A
920
+			);
921
+		if (! $datetime_id) {
922
+			$old_att_end_date     = $old_attendee['start_date'];
923
+			$old_att_end_time     = $this->get_migration_script()->convertTimeFromAMPM(
924
+				$old_attendee['event_time']
925
+			);
926
+			$old_att_end_datetime = $this->get_migration_script()->convert_date_string_to_utc(
927
+				$this,
928
+				$old_attendee,
929
+				"$old_att_end_date $old_att_end_time:00"
930
+			);
931
+			$wpdb->insert(
932
+				$this->_new_datetime_table,
933
+				[
934
+					'EVT_ID'        => $new_event_id,
935
+					'DTT_EVT_start' => $old_att_start_datetime,
936
+					'DTT_EVT_end'   => $old_att_end_datetime,
937
+					'DTT_deleted'   => true,
938
+				],
939
+				[
940
+					'%d',// EVT_ID
941
+					'%s',// DTT_EVT_start
942
+					'%s',// DTT_EVT_end
943
+					'%d',// DTT_deleted
944
+				]
945
+			);
946
+			$datetime_id = $wpdb->insert_id;
947
+		}
948
+
949
+		// insert new ticket
950
+		$wpdb->insert(
951
+			$wpdb->prefix . 'esp_ticket',
952
+			[
953
+				'TKT_name'       => $old_att_price_option,
954
+				'TKT_qty'        => -1,
955
+				'TKT_price'      => $old_att_price,
956
+				'TKT_start_date' => $old_att_start_datetime,
957
+				// we really have no clue what the time should be, but at least it was available when they attended
958
+				'TKT_end_date'   => $old_att_end_datetime,
959
+
960
+			],
961
+			[
962
+				'%s',// name
963
+				'%d',// qty
964
+				'%d',// price
965
+				'%s',// start_date
966
+				'%s',// end_date
967
+			]
968
+		);
969
+		$ticket_id = $wpdb->insert_id;
970
+		// associate the ticket with the datetime we found earlier
971
+		$wpdb->insert(
972
+			$this->_new_datetime_ticket_table,
973
+			[
974
+				'DTT_ID' => $datetime_id,
975
+				'TKT_ID' => $ticket_id,
976
+			],
977
+			[
978
+				'%d',// DTT_ID
979
+				'%d',// TKT_ID
980
+			]
981
+		);
982
+		// insert new price
983
+		$wpdb->insert(
984
+			$this->_new_price_table,
985
+			[
986
+				'PRC_amount'  => $old_att_price,
987
+				'PRT_ID'      => EE_DMS_4_1_0_prices::price_type_base,
988
+				'PRC_name'    => $old_att_price_option,
989
+				'PRC_deleted' => true,
990
+			],
991
+			[
992
+				'%d',// PRC_amount
993
+				'%d',// PRT_ID
994
+				'%s',// PRC_name
995
+				'%d',// PRC_deleted
996
+			]
997
+		);
998
+		$price_id = $wpdb->insert_id;
999
+		// associate the price to the ticket
1000
+		$wpdb->insert(
1001
+			$this->_new_ticket_price_table,
1002
+			[
1003
+				'TKT_ID' => $ticket_id,
1004
+				'PRC_ID' => $price_id,
1005
+			],
1006
+			[
1007
+				'%d',// TKT_ID
1008
+				'%d',// PRC_ID
1009
+			]
1010
+		);
1011
+		return $ticket_id;
1012
+	}
1013
+
1014
+
1015
+	/**
1016
+	 * Counts all the registrations on this transaction. If $count_only_older is TRUE then returns the number added SO
1017
+	 * FAR (ie, only considers attendee rows with an ID less than this one's), but if $count_only_older is FALSe
1018
+	 * returns ALL
1019
+	 *
1020
+	 * @param array   $old_attendee_row
1021
+	 * @param boolean $count_only_older true if you want the running count (ie, the total up to this row), and false if
1022
+	 *                                  you want ALL
1023
+	 * @return int
1024
+	 * @global wpdb   $wpdb
1025
+	 */
1026
+	private function _sum_old_attendees_on_old_txn(array $old_attendee_row, bool $count_only_older = false): int
1027
+	{
1028
+		global $wpdb;
1029
+		$count_only_older_sql = $count_only_older ? $wpdb->prepare(" AND id<%d", $old_attendee_row['id']) : '';
1030
+		$count                =
1031
+			intval(
1032
+				$wpdb->get_var(
1033
+					$wpdb->prepare(
1034
+						"SELECT SUM(quantity) FROM " .
1035
+						$this->_old_table .
1036
+						" WHERE registration_id=%s $count_only_older_sql",
1037
+						$old_attendee_row['registration_id']
1038
+					)
1039
+				)
1040
+			);
1041
+
1042
+		if ($this->_mer_tables_exist()) {
1043
+			// if MER exists, then it's a little tricky.
1044
+			// when users registered by adding items to the cart, and it was a
1045
+			// group registration requiring additional attendee INFO, then the attendee rows
1046
+			// DO NOT have the same registration_id (although they probably should have)
1047
+			// they are related just like MER attendee rows are related, through the MER group table
1048
+			// BUT we want to count all the MER attendee rows for the same registration
1049
+			$primary_attendee =
1050
+				$this->_find_mer_primary_attendee_using_mer_tables($old_attendee_row['registration_id']);
1051
+
1052
+			// No primary attendee to work with? Return the count.
1053
+			if (is_null($primary_attendee)) {
1054
+				return (int) $count;
1055
+			}
1056
+
1057
+			$count_using_mer_table =
1058
+				$wpdb->get_var(
1059
+					$wpdb->prepare(
1060
+						"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",
1061
+						$old_attendee_row['event_id'],
1062
+						$primary_attendee['registration_id']
1063
+					)
1064
+				);
1065
+			$count                 = max($count_using_mer_table, $count);
1066
+		}
1067
+		return (int) $count;
1068
+	}
1069
+
1070
+
1071
+	private function _insert_new_payment($old_attendee, $new_txn_id): int
1072
+	{
1073
+		global $wpdb;
1074
+		// Only add a payment for primary attendees. Important note: partial payments were marked as "Pending".
1075
+		// Also, an offline payment had blank status (ie, "").
1076
+		$old_pay_stati_indicating_no_payment = ['', 'Incomplete', 'Not Completed'];
1077
+		// if this is for a primary 3.1 attendee which WASN'T free and has a completed, cancelled, or declined payment...
1078
+		if (
1079
+			intval($old_attendee['is_primary']) &&
1080
+			floatval($old_attendee['total_cost']) &&
1081
+			! in_array($old_attendee['payment_status'], $old_pay_stati_indicating_no_payment)
1082
+		) {
1083
+			$pay_status_mapping = [
1084
+				'Completed'        => 'PAP',
1085
+				'Payment Declined' => 'PDC',
1086
+				'Cancelled'        => 'PCN',
1087
+				'Declined'         => 'PDC',
1088
+			];
1089
+
1090
+			$by_admin = $old_attendee['payment'] == 'Admin';
1091
+
1092
+			// IE, if we don't recognize the status, assume payment failed
1093
+			$STS_ID = $pay_status_mapping[ $old_attendee['payment_status'] ] ?? 'PFL';
1094
+
1095
+			$cols_n_values = [
1096
+				'TXN_ID'               => $new_txn_id,
1097
+				'STS_ID'               => $STS_ID,
1098
+				'PAY_timestamp'        => $this->get_migration_script()->convert_date_string_to_utc(
1099
+					$this,
1100
+					$old_attendee,
1101
+					$old_attendee['date']
1102
+				),
1103
+				'PAY_method'           => 'CART',
1104
+				'PAY_amount'           => $old_attendee['amount_pd'],
1105
+				// 'txn_type' was used both for the gateway's name, and for other payment notes.
1106
+				'PAY_gateway'          => $old_attendee['txn_type'],
1107
+				'PAY_extra_accntng'    => $old_attendee['txn_type'],
1108
+				'PAY_gateway_response' => '',
1109
+				'PAY_txn_id_chq_nmbr'  => substr($old_attendee['txn_id'], 0, 32),
1110
+				'PAY_via_admin'        => $by_admin,
1111
+				'PAY_details'          => $old_attendee['transaction_details'],
1112
+			];
1113
+			$data_types    = [
1114
+				'%d',// TXN_Id
1115
+				'%s',// STS_ID
1116
+				'%s',// PAY_timestamp
1117
+				'%s',// PAY_method
1118
+				'%f',// PAY_amount
1119
+				'%s',// PAY_gateway
1120
+				'%s',// PAY_gateway_response
1121
+				'%s',// PAY_txn_id_chq_nmbr
1122
+				'%d',// PAY_via_admin
1123
+				'%s',// PAY_details
1124
+			];
1125
+			$success       = $wpdb->insert($this->_new_payment_table, $cols_n_values, $data_types);
1126
+			if (! $success) {
1127
+				$this->add_error(
1128
+					$this->get_migration_script()->_create_error_message_for_db_insertion(
1129
+						$this->_old_table,
1130
+						$old_attendee,
1131
+						$this->_new_attendee_cpt_table,
1132
+						$cols_n_values,
1133
+						$data_types
1134
+					)
1135
+				);
1136
+				return 0;
1137
+			}
1138
+			return $wpdb->insert_id;
1139
+		}
1140
+		return 0;
1141
+	}
1142
+
1143
+
1144
+	/**
1145
+	 * If MER is active, if you want ot find the other registrations on that attendee row
1146
+	 *
1147
+	 * @param string   $old_registration_id
1148
+	 * @return array|null
1149
+	 * @global wpdb $wpdb
1150
+	 */
1151
+	private function _find_mer_primary_attendee_using_mer_tables(string $old_registration_id): ?array
1152
+	{
1153
+		if (! $this->_mer_tables_exist()) {
1154
+			return null;
1155
+		}
1156
+		global $wpdb;
1157
+		return $wpdb->get_row(
1158
+			$wpdb->prepare(
1159
+				"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",
1160
+				$old_registration_id
1161
+			),
1162
+			ARRAY_A
1163
+		);
1164
+	}
1165 1165
 }
Please login to merge, or discard this patch.
4_1_0_stages/EE_DMS_4_1_0_category_details.dmsstage.php 1 patch
Indentation   +76 added lines, -76 removed lines patch added patch discarded remove patch
@@ -7,86 +7,86 @@
 block discarded – undo
7 7
  */
8 8
 class EE_DMS_4_1_0_category_details extends EE_Data_Migration_Script_Stage
9 9
 {
10
-    private string $_new_term_table;
10
+	private string $_new_term_table;
11 11
 
12 12
 
13
-    /**
14
-     * @throws EE_Error
15
-     */
16
-    public function _migration_step($num_items_to_migrate = 50)
17
-    {
18
-        global $wpdb;
19
-        $start_at_record         = $this->count_records_migrated();
20
-        $rows                    =
21
-            $wpdb->get_results(
22
-                $wpdb->prepare(
23
-                    "SELECT * FROM $this->_old_table LIMIT %d,%d",
24
-                    $start_at_record,
25
-                    $num_items_to_migrate
26
-                ),
27
-                ARRAY_A
28
-            );
29
-        $items_actually_migrated = 0;
30
-        foreach ($rows as $category_detail_row) {
31
-            $term_and_taxonomy_ids = wp_insert_term(
32
-                stripslashes($category_detail_row['category_name']),
33
-                'espresso_event_categories',
34
-                [
35
-                    'description' => stripslashes($category_detail_row['category_desc']),
36
-                    'slug'        => $category_detail_row['category_identifier'],
37
-                ]
38
-            );
39
-            if ($term_and_taxonomy_ids instanceof WP_Error) {
40
-                $this->add_error(
41
-                    sprintf(
42
-                        esc_html__(
43
-                            "Could not create WP Term_Taxonomy from old category: %s. The Error was: %s",
44
-                            "event_espresso"
45
-                        ),
46
-                        $this->_json_encode($category_detail_row),
47
-                        $term_and_taxonomy_ids->get_error_message()
48
-                    )
49
-                );
50
-                $items_actually_migrated++;
51
-                continue;
52
-            }
53
-            $term_id          = $term_and_taxonomy_ids['term_id'];
54
-            $term_taxonomy_id = $term_and_taxonomy_ids['term_taxonomy_id'];
55
-            $this->get_migration_script()->set_mapping(
56
-                $this->_old_table,
57
-                $category_detail_row['id'],
58
-                $this->_new_term_table,
59
-                $term_id
60
-            );
61
-            $this->get_migration_script()->set_mapping(
62
-                $this->_old_table,
63
-                $category_detail_row['id'],
64
-                $this->_new_table,
65
-                $term_taxonomy_id
66
-            );
67
-            $items_actually_migrated++;
68
-        }
69
-        if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
70
-            $this->set_completed();
71
-        }
72
-        return $items_actually_migrated;
73
-    }
13
+	/**
14
+	 * @throws EE_Error
15
+	 */
16
+	public function _migration_step($num_items_to_migrate = 50)
17
+	{
18
+		global $wpdb;
19
+		$start_at_record         = $this->count_records_migrated();
20
+		$rows                    =
21
+			$wpdb->get_results(
22
+				$wpdb->prepare(
23
+					"SELECT * FROM $this->_old_table LIMIT %d,%d",
24
+					$start_at_record,
25
+					$num_items_to_migrate
26
+				),
27
+				ARRAY_A
28
+			);
29
+		$items_actually_migrated = 0;
30
+		foreach ($rows as $category_detail_row) {
31
+			$term_and_taxonomy_ids = wp_insert_term(
32
+				stripslashes($category_detail_row['category_name']),
33
+				'espresso_event_categories',
34
+				[
35
+					'description' => stripslashes($category_detail_row['category_desc']),
36
+					'slug'        => $category_detail_row['category_identifier'],
37
+				]
38
+			);
39
+			if ($term_and_taxonomy_ids instanceof WP_Error) {
40
+				$this->add_error(
41
+					sprintf(
42
+						esc_html__(
43
+							"Could not create WP Term_Taxonomy from old category: %s. The Error was: %s",
44
+							"event_espresso"
45
+						),
46
+						$this->_json_encode($category_detail_row),
47
+						$term_and_taxonomy_ids->get_error_message()
48
+					)
49
+				);
50
+				$items_actually_migrated++;
51
+				continue;
52
+			}
53
+			$term_id          = $term_and_taxonomy_ids['term_id'];
54
+			$term_taxonomy_id = $term_and_taxonomy_ids['term_taxonomy_id'];
55
+			$this->get_migration_script()->set_mapping(
56
+				$this->_old_table,
57
+				$category_detail_row['id'],
58
+				$this->_new_term_table,
59
+				$term_id
60
+			);
61
+			$this->get_migration_script()->set_mapping(
62
+				$this->_old_table,
63
+				$category_detail_row['id'],
64
+				$this->_new_table,
65
+				$term_taxonomy_id
66
+			);
67
+			$items_actually_migrated++;
68
+		}
69
+		if ($this->count_records_migrated() + $items_actually_migrated >= $this->count_records_to_migrate()) {
70
+			$this->set_completed();
71
+		}
72
+		return $items_actually_migrated;
73
+	}
74 74
 
75 75
 
76
-    public function _count_records_to_migrate()
77
-    {
78
-        global $wpdb;
79
-        return $wpdb->get_var("SELECT COUNT(id) FROM $this->_old_table");
80
-    }
76
+	public function _count_records_to_migrate()
77
+	{
78
+		global $wpdb;
79
+		return $wpdb->get_var("SELECT COUNT(id) FROM $this->_old_table");
80
+	}
81 81
 
82 82
 
83
-    public function __construct()
84
-    {
85
-        $this->_pretty_name = esc_html__("Category Details", "event_espresso");
86
-        global $wpdb;
87
-        $this->_old_table      = $wpdb->prefix . "events_category_detail";
88
-        $this->_new_table      = $wpdb->prefix . "term_taxonomy";
89
-        $this->_new_term_table = $wpdb->prefix . "terms";
90
-        parent::__construct();
91
-    }
83
+	public function __construct()
84
+	{
85
+		$this->_pretty_name = esc_html__("Category Details", "event_espresso");
86
+		global $wpdb;
87
+		$this->_old_table      = $wpdb->prefix . "events_category_detail";
88
+		$this->_new_table      = $wpdb->prefix . "term_taxonomy";
89
+		$this->_new_term_table = $wpdb->prefix . "terms";
90
+		parent::__construct();
91
+	}
92 92
 }
Please login to merge, or discard this patch.
4_5_0_stages/EE_DMS_4_5_0_update_wp_user_for_question_groups.dmsstage.php 1 patch
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -10,48 +10,48 @@
 block discarded – undo
10 10
  */
11 11
 class EE_DMS_4_5_0_update_wp_user_for_question_groups extends EE_Data_Migration_Script_Stage_Table
12 12
 {
13
-    public function __construct()
14
-    {
15
-        global $wpdb;
16
-        $this->_pretty_name = esc_html__("Question Groups", "event_espresso");
17
-        $this->_old_table   = $wpdb->prefix . "esp_question_group";
18
-        parent::__construct();
19
-    }
13
+	public function __construct()
14
+	{
15
+		global $wpdb;
16
+		$this->_pretty_name = esc_html__("Question Groups", "event_espresso");
17
+		$this->_old_table   = $wpdb->prefix . "esp_question_group";
18
+		parent::__construct();
19
+	}
20 20
 
21 21
 
22
-    /**
23
-     * @throws ReflectionException
24
-     * @throws EE_Error
25
-     */
26
-    protected function _migrate_old_row($old_row)
27
-    {
28
-        // foreach ticket row we add the id for the current logged in user.
29
-        global $wpdb;
30
-        $user_id = EEH_Activation::get_default_creator_id();
31
-        $user_id = $user_id ?: 0;
32
-        $updated = $wpdb->update(
33
-            $this->_old_table,
34
-            ['QSG_wp_user' => $user_id],
35
-            ['QSG_ID' => $old_row['QSG_ID']],
36
-            [
37
-                '%d',// QSG_wp_user
38
-            ],
39
-            [
40
-                '%d',// QSG_ID
41
-            ]
42
-        );
43
-        if (false === $updated) {
44
-            $this->add_error(
45
-                sprintf(
46
-                    esc_html__(
47
-                        "Error in updating table %s setting QSG_wp_user = %d where QSG_ID = %d",
48
-                        'event_espresso'
49
-                    ),
50
-                    $this->_old_table,
51
-                    $user_id,
52
-                    $old_row['QSG_ID']
53
-                )
54
-            );
55
-        }
56
-    }
22
+	/**
23
+	 * @throws ReflectionException
24
+	 * @throws EE_Error
25
+	 */
26
+	protected function _migrate_old_row($old_row)
27
+	{
28
+		// foreach ticket row we add the id for the current logged in user.
29
+		global $wpdb;
30
+		$user_id = EEH_Activation::get_default_creator_id();
31
+		$user_id = $user_id ?: 0;
32
+		$updated = $wpdb->update(
33
+			$this->_old_table,
34
+			['QSG_wp_user' => $user_id],
35
+			['QSG_ID' => $old_row['QSG_ID']],
36
+			[
37
+				'%d',// QSG_wp_user
38
+			],
39
+			[
40
+				'%d',// QSG_ID
41
+			]
42
+		);
43
+		if (false === $updated) {
44
+			$this->add_error(
45
+				sprintf(
46
+					esc_html__(
47
+						"Error in updating table %s setting QSG_wp_user = %d where QSG_ID = %d",
48
+						'event_espresso'
49
+					),
50
+					$this->_old_table,
51
+					$user_id,
52
+					$old_row['QSG_ID']
53
+				)
54
+			);
55
+		}
56
+	}
57 57
 }
Please login to merge, or discard this patch.