Passed
Push — master ( 37ef0b...5302c6 )
by Brian
04:00
created
includes/admin/class-getpaid-installer.php 1 patch
Indentation   +333 added lines, -333 removed lines patch added patch discarded remove patch
@@ -20,205 +20,205 @@  discard block
 block discarded – undo
20 20
  */
21 21
 class GetPaid_Installer {
22 22
 
23
-	/**
24
-	 * Upgrades the install.
25
-	 *
26
-	 * @param string $upgrade_from The current invoicing version.
27
-	 */
28
-	public function upgrade_db( $upgrade_from ) {
29
-
30
-		// Save the current invoicing version.
31
-		update_option( 'wpinv_version', WPINV_VERSION );
32
-
33
-		// Setup the invoice Custom Post Type.
34
-		GetPaid_Post_Types::register_post_types();
35
-
36
-		// Clear the permalinks
37
-		flush_rewrite_rules();
38
-
39
-		// Maybe create new/missing pages.
40
-		$this->create_pages();
41
-
42
-		// Maybe re(add) admin capabilities.
43
-		$this->add_capabilities();
44
-
45
-		// Maybe create the default payment form.
46
-		wpinv_get_default_payment_form();
47
-
48
-		// Create any missing database tables.
49
-		$method = "upgrade_from_$upgrade_from";
50
-
51
-		if ( method_exists( $this, $method ) ) {
52
-			$this->$method();
53
-		}
54
-
55
-	}
56
-
57
-	/**
58
-	 * Do a fresh install.
59
-	 *
60
-	 */
61
-	public function upgrade_from_0() {
62
-		$this->create_subscriptions_table();
63
-		$this->create_invoices_table();
64
-		$this->create_invoice_items_table();
65
-
66
-		// Save default tax rates.
67
-		update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) );
68
-	}
69
-
70
-	/**
71
-	 * Upgrade to 0.0.5
72
-	 *
73
-	 */
74
-	public function upgrade_from_004() {
75
-		global $wpdb;
76
-
77
-		// Invoices.
78
-		$results = $wpdb->get_results( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" );
79
-		if ( ! empty( $results ) ) {
80
-			$wpdb->query( "UPDATE {$wpdb->posts} SET post_status = CONCAT( 'wpi-', post_status ) WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" );
81
-
82
-			// Clean post cache
83
-			foreach ( $results as $row ) {
84
-				clean_post_cache( $row->ID );
85
-			}
86
-
87
-		}
88
-
89
-		// Item meta key changes
90
-		$query = "SELECT DISTINCT post_id FROM " . $wpdb->postmeta . " WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id', '_wpinv_cpt_name', '_wpinv_cpt_singular_name' )";
91
-		$results = $wpdb->get_results( $query );
92
-
93
-		if ( ! empty( $results ) ) {
94
-			$wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" );
95
-			$wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" );
96
-			$wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" );
97
-
98
-			foreach ( $results as $row ) {
99
-				clean_post_cache( $row->post_id );
100
-			}
101
-
102
-		}
103
-
104
-		$this->upgrade_from_102();
105
-	}
106
-
107
-	/**
108
-	 * Upgrade to 1.0.3
109
-	 *
110
-	 */
111
-	public function upgrade_from_102() {
112
-		$this->create_subscriptions_table();
113
-		$this->upgrade_from_118();
114
-	}
115
-
116
-	/**
117
-	 * Upgrade to version 2.0.0.
118
-	 *
119
-	 */
120
-	public function upgrade_from_118() {
121
-		$this->create_invoices_table();
122
-		$this->create_invoice_items_table();
123
-		$this->migrate_old_invoices();
124
-	}
125
-
126
-	/**
127
-	 * Give administrators the capability to manage GetPaid.
128
-	 *
129
-	 */
130
-	public function add_capabilities() {
131
-		$GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' );
132
-	}
133
-
134
-	/**
135
-	 * Re-create GetPaid pages.
136
-	 *
137
-	 */
138
-	public function create_pages() {
139
-
140
-		$pages = apply_filters(
141
-			'wpinv_create_pages',
142
-			array(
143
-
144
-				// Checkout page.
145
-				'checkout_page' => array(
146
-					'name'      => _x( 'gp-checkout', 'Page slug', 'invoicing' ),
147
-					'title'     => _x( 'Checkout', 'Page title', 'invoicing' ),
148
-					'content'   => '
23
+    /**
24
+     * Upgrades the install.
25
+     *
26
+     * @param string $upgrade_from The current invoicing version.
27
+     */
28
+    public function upgrade_db( $upgrade_from ) {
29
+
30
+        // Save the current invoicing version.
31
+        update_option( 'wpinv_version', WPINV_VERSION );
32
+
33
+        // Setup the invoice Custom Post Type.
34
+        GetPaid_Post_Types::register_post_types();
35
+
36
+        // Clear the permalinks
37
+        flush_rewrite_rules();
38
+
39
+        // Maybe create new/missing pages.
40
+        $this->create_pages();
41
+
42
+        // Maybe re(add) admin capabilities.
43
+        $this->add_capabilities();
44
+
45
+        // Maybe create the default payment form.
46
+        wpinv_get_default_payment_form();
47
+
48
+        // Create any missing database tables.
49
+        $method = "upgrade_from_$upgrade_from";
50
+
51
+        if ( method_exists( $this, $method ) ) {
52
+            $this->$method();
53
+        }
54
+
55
+    }
56
+
57
+    /**
58
+     * Do a fresh install.
59
+     *
60
+     */
61
+    public function upgrade_from_0() {
62
+        $this->create_subscriptions_table();
63
+        $this->create_invoices_table();
64
+        $this->create_invoice_items_table();
65
+
66
+        // Save default tax rates.
67
+        update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) );
68
+    }
69
+
70
+    /**
71
+     * Upgrade to 0.0.5
72
+     *
73
+     */
74
+    public function upgrade_from_004() {
75
+        global $wpdb;
76
+
77
+        // Invoices.
78
+        $results = $wpdb->get_results( "SELECT ID FROM {$wpdb->posts} WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" );
79
+        if ( ! empty( $results ) ) {
80
+            $wpdb->query( "UPDATE {$wpdb->posts} SET post_status = CONCAT( 'wpi-', post_status ) WHERE post_type = 'wpi_invoice' AND post_status IN( 'pending', 'processing', 'onhold', 'refunded', 'cancelled', 'failed', 'renewal' )" );
81
+
82
+            // Clean post cache
83
+            foreach ( $results as $row ) {
84
+                clean_post_cache( $row->ID );
85
+            }
86
+
87
+        }
88
+
89
+        // Item meta key changes
90
+        $query = "SELECT DISTINCT post_id FROM " . $wpdb->postmeta . " WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id', '_wpinv_cpt_name', '_wpinv_cpt_singular_name' )";
91
+        $results = $wpdb->get_results( $query );
92
+
93
+        if ( ! empty( $results ) ) {
94
+            $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" );
95
+            $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" );
96
+            $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" );
97
+
98
+            foreach ( $results as $row ) {
99
+                clean_post_cache( $row->post_id );
100
+            }
101
+
102
+        }
103
+
104
+        $this->upgrade_from_102();
105
+    }
106
+
107
+    /**
108
+     * Upgrade to 1.0.3
109
+     *
110
+     */
111
+    public function upgrade_from_102() {
112
+        $this->create_subscriptions_table();
113
+        $this->upgrade_from_118();
114
+    }
115
+
116
+    /**
117
+     * Upgrade to version 2.0.0.
118
+     *
119
+     */
120
+    public function upgrade_from_118() {
121
+        $this->create_invoices_table();
122
+        $this->create_invoice_items_table();
123
+        $this->migrate_old_invoices();
124
+    }
125
+
126
+    /**
127
+     * Give administrators the capability to manage GetPaid.
128
+     *
129
+     */
130
+    public function add_capabilities() {
131
+        $GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' );
132
+    }
133
+
134
+    /**
135
+     * Re-create GetPaid pages.
136
+     *
137
+     */
138
+    public function create_pages() {
139
+
140
+        $pages = apply_filters(
141
+            'wpinv_create_pages',
142
+            array(
143
+
144
+                // Checkout page.
145
+                'checkout_page' => array(
146
+                    'name'      => _x( 'gp-checkout', 'Page slug', 'invoicing' ),
147
+                    'title'     => _x( 'Checkout', 'Page title', 'invoicing' ),
148
+                    'content'   => '
149 149
 						<!-- wp:shortcode -->
150 150
 						[wpinv_checkout]
151 151
 						<!-- /wp:shortcode -->
152 152
 					',
153
-					'parent'    => '',
154
-				),
155
-
156
-				// Invoice history page.
157
-				'invoice_history_page' => array(
158
-					'name'    => _x( 'gp-invoices', 'Page slug', 'invoicing' ),
159
-					'title'   => _x( 'My Invoices', 'Page title', 'invoicing' ),
160
-					'content' => '
153
+                    'parent'    => '',
154
+                ),
155
+
156
+                // Invoice history page.
157
+                'invoice_history_page' => array(
158
+                    'name'    => _x( 'gp-invoices', 'Page slug', 'invoicing' ),
159
+                    'title'   => _x( 'My Invoices', 'Page title', 'invoicing' ),
160
+                    'content' => '
161 161
 					<!-- wp:shortcode -->
162 162
 					[wpinv_history]
163 163
 					<!-- /wp:shortcode -->
164 164
 				',
165
-					'parent'  => '',
166
-				),
167
-
168
-				// Success page content.
169
-				'success_page' => array(
170
-					'name'     => _x( 'gp-receipt', 'Page slug', 'invoicing' ),
171
-					'title'    => _x( 'Payment Confirmation', 'Page title', 'invoicing' ),
172
-					'content'  => '
165
+                    'parent'  => '',
166
+                ),
167
+
168
+                // Success page content.
169
+                'success_page' => array(
170
+                    'name'     => _x( 'gp-receipt', 'Page slug', 'invoicing' ),
171
+                    'title'    => _x( 'Payment Confirmation', 'Page title', 'invoicing' ),
172
+                    'content'  => '
173 173
 					<!-- wp:shortcode -->
174 174
 					[wpinv_receipt]
175 175
 					<!-- /wp:shortcode -->
176 176
 				',
177
-					'parent'   => 'gp-checkout',
178
-				),
179
-
180
-				// Failure page content.
181
-				'failure_page' => array(
182
-					'name'    => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ),
183
-					'title'   => _x( 'Transaction Failed', 'Page title', 'invoicing' ),
184
-					'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ),
185
-					'parent'  => 'gp-checkout',
186
-				),
187
-
188
-				// Subscriptions history page.
189
-				'invoice_subscription_page' => array(
190
-					'name'    => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ),
191
-					'title'   => _x( 'My Subscriptions', 'Page title', 'invoicing' ),
192
-					'content' => '
177
+                    'parent'   => 'gp-checkout',
178
+                ),
179
+
180
+                // Failure page content.
181
+                'failure_page' => array(
182
+                    'name'    => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ),
183
+                    'title'   => _x( 'Transaction Failed', 'Page title', 'invoicing' ),
184
+                    'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ),
185
+                    'parent'  => 'gp-checkout',
186
+                ),
187
+
188
+                // Subscriptions history page.
189
+                'invoice_subscription_page' => array(
190
+                    'name'    => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ),
191
+                    'title'   => _x( 'My Subscriptions', 'Page title', 'invoicing' ),
192
+                    'content' => '
193 193
 					<!-- wp:shortcode -->
194 194
 					[wpinv_subscriptions]
195 195
 					<!-- /wp:shortcode -->
196 196
 				',
197
-					'parent' => '',
198
-				),
197
+                    'parent' => '',
198
+                ),
199 199
 
200
-			)
201
-		);
200
+            )
201
+        );
202 202
 
203
-		foreach ( $pages as $key => $page ) {
204
-			wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] );
205
-		}
203
+        foreach ( $pages as $key => $page ) {
204
+            wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] );
205
+        }
206 206
 
207
-	}
207
+    }
208 208
 
209
-	/**
210
-	 * Create subscriptions table.
211
-	 *
212
-	 */
213
-	public function create_subscriptions_table() {
209
+    /**
210
+     * Create subscriptions table.
211
+     *
212
+     */
213
+    public function create_subscriptions_table() {
214 214
 
215
-		global $wpdb;
215
+        global $wpdb;
216 216
 
217
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
217
+        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
218 218
 
219
-		// Create tables.
220
-		$charset_collate = $wpdb->get_charset_collate();
221
-		$sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpinv_subscriptions (
219
+        // Create tables.
220
+        $charset_collate = $wpdb->get_charset_collate();
221
+        $sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpinv_subscriptions (
222 222
 			id bigint(20) unsigned NOT NULL auto_increment,
223 223
 			customer_id bigint(20) NOT NULL,
224 224
 			frequency int(11) NOT NULL DEFAULT '1',
@@ -241,22 +241,22 @@  discard block
 block discarded – undo
241 241
 			KEY customer_and_status (customer_id, status)
242 242
 		  ) $charset_collate;";
243 243
 
244
-		dbDelta( $sql );
244
+        dbDelta( $sql );
245 245
 
246
-	}
246
+    }
247 247
 
248
-	/**
249
-	 * Create invoices table.
250
-	 *
251
-	 */
252
-	public function create_invoices_table() {
253
-		global $wpdb;
248
+    /**
249
+     * Create invoices table.
250
+     *
251
+     */
252
+    public function create_invoices_table() {
253
+        global $wpdb;
254 254
 
255
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
255
+        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
256 256
 
257
-		// Create tables.
258
-		$charset_collate = $wpdb->get_charset_collate();
259
-		$sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoices (
257
+        // Create tables.
258
+        $charset_collate = $wpdb->get_charset_collate();
259
+        $sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoices (
260 260
 			post_id BIGINT(20) NOT NULL,
261 261
             `number` VARCHAR(100),
262 262
             `key` VARCHAR(100),
@@ -292,22 +292,22 @@  discard block
 block discarded – undo
292 292
 			KEY `key` (`key`)
293 293
 		  ) $charset_collate;";
294 294
 
295
-		dbDelta( $sql );
295
+        dbDelta( $sql );
296 296
 
297
-	}
297
+    }
298 298
 
299
-	/**
300
-	 * Create invoice items table.
301
-	 *
302
-	 */
303
-	public function create_invoice_items_table() {
304
-		global $wpdb;
299
+    /**
300
+     * Create invoice items table.
301
+     *
302
+     */
303
+    public function create_invoice_items_table() {
304
+        global $wpdb;
305 305
 
306
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
306
+        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
307 307
 
308
-		// Create tables.
309
-		$charset_collate = $wpdb->get_charset_collate();
310
-		$sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoice_items (
308
+        // Create tables.
309
+        $charset_collate = $wpdb->get_charset_collate();
310
+        $sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoice_items (
311 311
 			ID BIGINT(20) NOT NULL AUTO_INCREMENT,
312 312
             post_id BIGINT(20) NOT NULL,
313 313
             item_id BIGINT(20) NOT NULL,
@@ -329,138 +329,138 @@  discard block
 block discarded – undo
329 329
 			KEY post_id (post_id)
330 330
 		  ) $charset_collate;";
331 331
 
332
-		dbDelta( $sql );
333
-
334
-	}
335
-
336
-	/**
337
-	 * Migrates old invoices to new invoices.
338
-	 *
339
-	 */
340
-	public function migrate_old_invoices() {
341
-		global $wpdb;
342
-
343
-		$invoices = array_unique(
344
-			get_posts(
345
-				array(
346
-					'post_type'      => array( 'wpi_invoice', 'wpi_quote' ),
347
-					'posts_per_page' => -1,
348
-					'fields'         => 'ids',
349
-					'post_status'    => array_keys( get_post_stati() ),
350
-				)
351
-			)
352
-		);
353
-
354
-		// Abort if we do not have any invoices.
355
-		if ( empty( $invoices ) ) {
356
-			return;
357
-		}
358
-
359
-		$invoices_table      = $wpdb->prefix . 'getpaid_invoices';
360
-		$invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items';
361
-
362
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php' );
363
-
364
-		$invoice_rows = array();
365
-		foreach ( $invoices as $invoice ) {
366
-
367
-			$invoice = new WPInv_Legacy_Invoice( $invoice );
368
-
369
-			if ( empty( $invoice->ID ) ) {
370
-				return;
371
-			}
372
-
373
-			$fields = array (
374
-				'post_id'        => $invoice->ID,
375
-				'number'         => $invoice->get_number(),
376
-				'key'            => $invoice->get_key(),
377
-				'type'           => str_replace( 'wpi_', '', $invoice->post_type ),
378
-				'mode'           => $invoice->mode,
379
-				'user_ip'        => $invoice->get_ip(),
380
-				'first_name'     => $invoice->get_first_name(),
381
-				'last_name'      => $invoice->get_last_name(),
382
-				'address'        => $invoice->get_address(),
383
-				'city'           => $invoice->city,
384
-				'state'          => $invoice->state,
385
-				'country'        => $invoice->country,
386
-				'zip'            => $invoice->zip,
387
-				'adddress_confirmed' => (int) $invoice->adddress_confirmed,
388
-				'gateway'        => $invoice->get_gateway(),
389
-				'transaction_id' => $invoice->get_transaction_id(),
390
-				'currency'       => $invoice->get_currency(),
391
-				'subtotal'       => $invoice->get_subtotal(),
392
-				'tax'            => $invoice->get_tax(),
393
-				'fees_total'     => $invoice->get_fees_total(),
394
-				'total'          => $invoice->get_total(),
395
-				'discount'       => $invoice->get_discount(),
396
-				'discount_code'  => $invoice->get_discount_code(),
397
-				'disable_taxes'  => $invoice->disable_taxes,
398
-				'due_date'       => $invoice->get_due_date(),
399
-				'completed_date' => $invoice->get_completed_date(),
400
-				'company'        => $invoice->company,
401
-				'vat_number'     => $invoice->vat_number,
402
-				'vat_rate'       => $invoice->vat_rate,
403
-				'custom_meta'    => $invoice->payment_meta
404
-			);
405
-
406
-			foreach ( $fields as $key => $val ) {
407
-				if ( is_null( $val ) ) {
408
-					$val = '';
409
-				}
410
-				$val = maybe_serialize( $val );
411
-				$fields[ $key ] = $wpdb->prepare( '%s', $val );
412
-			}
413
-
414
-			$fields = implode( ', ', $fields );
415
-			$invoice_rows[] = "($fields)";
416
-
417
-			$item_rows    = array();
418
-			$item_columns = array();
419
-			foreach ( $invoice->get_cart_details() as $details ) {
420
-				$fields = array(
421
-					'post_id'          => $invoice->ID,
422
-					'item_id'          => $details['id'],
423
-					'item_name'        => $details['name'],
424
-					'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'],
425
-					'vat_rate'         => $details['vat_rate'],
426
-					'vat_class'        => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'],
427
-					'tax'              => $details['tax'],
428
-					'item_price'       => $details['item_price'],
429
-					'custom_price'     => $details['custom_price'],
430
-					'quantity'         => $details['quantity'],
431
-					'discount'         => $details['discount'],
432
-					'subtotal'         => $details['subtotal'],
433
-					'price'            => $details['price'],
434
-					'meta'             => $details['meta'],
435
-					'fees'             => $details['fees'],
436
-				);
437
-
438
-				$item_columns = array_keys ( $fields );
439
-
440
-				foreach ( $fields as $key => $val ) {
441
-					if ( is_null( $val ) ) {
442
-						$val = '';
443
-					}
444
-					$val = maybe_serialize( $val );
445
-					$fields[ $key ] = $wpdb->prepare( '%s', $val );
446
-				}
447
-
448
-				$fields = implode( ', ', $fields );
449
-				$item_rows[] = "($fields)";
450
-			}
451
-
452
-			$item_rows    = implode( ', ', $item_rows );
453
-			$item_columns = implode( ', ', $item_columns );
454
-			$wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" );
455
-		}
456
-
457
-		if ( empty( $invoice_rows ) ) {
458
-			return;
459
-		}
460
-
461
-		$invoice_rows = implode( ', ', $invoice_rows );
462
-		$wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" );
463
-
464
-	}
332
+        dbDelta( $sql );
333
+
334
+    }
335
+
336
+    /**
337
+     * Migrates old invoices to new invoices.
338
+     *
339
+     */
340
+    public function migrate_old_invoices() {
341
+        global $wpdb;
342
+
343
+        $invoices = array_unique(
344
+            get_posts(
345
+                array(
346
+                    'post_type'      => array( 'wpi_invoice', 'wpi_quote' ),
347
+                    'posts_per_page' => -1,
348
+                    'fields'         => 'ids',
349
+                    'post_status'    => array_keys( get_post_stati() ),
350
+                )
351
+            )
352
+        );
353
+
354
+        // Abort if we do not have any invoices.
355
+        if ( empty( $invoices ) ) {
356
+            return;
357
+        }
358
+
359
+        $invoices_table      = $wpdb->prefix . 'getpaid_invoices';
360
+        $invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items';
361
+
362
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php' );
363
+
364
+        $invoice_rows = array();
365
+        foreach ( $invoices as $invoice ) {
366
+
367
+            $invoice = new WPInv_Legacy_Invoice( $invoice );
368
+
369
+            if ( empty( $invoice->ID ) ) {
370
+                return;
371
+            }
372
+
373
+            $fields = array (
374
+                'post_id'        => $invoice->ID,
375
+                'number'         => $invoice->get_number(),
376
+                'key'            => $invoice->get_key(),
377
+                'type'           => str_replace( 'wpi_', '', $invoice->post_type ),
378
+                'mode'           => $invoice->mode,
379
+                'user_ip'        => $invoice->get_ip(),
380
+                'first_name'     => $invoice->get_first_name(),
381
+                'last_name'      => $invoice->get_last_name(),
382
+                'address'        => $invoice->get_address(),
383
+                'city'           => $invoice->city,
384
+                'state'          => $invoice->state,
385
+                'country'        => $invoice->country,
386
+                'zip'            => $invoice->zip,
387
+                'adddress_confirmed' => (int) $invoice->adddress_confirmed,
388
+                'gateway'        => $invoice->get_gateway(),
389
+                'transaction_id' => $invoice->get_transaction_id(),
390
+                'currency'       => $invoice->get_currency(),
391
+                'subtotal'       => $invoice->get_subtotal(),
392
+                'tax'            => $invoice->get_tax(),
393
+                'fees_total'     => $invoice->get_fees_total(),
394
+                'total'          => $invoice->get_total(),
395
+                'discount'       => $invoice->get_discount(),
396
+                'discount_code'  => $invoice->get_discount_code(),
397
+                'disable_taxes'  => $invoice->disable_taxes,
398
+                'due_date'       => $invoice->get_due_date(),
399
+                'completed_date' => $invoice->get_completed_date(),
400
+                'company'        => $invoice->company,
401
+                'vat_number'     => $invoice->vat_number,
402
+                'vat_rate'       => $invoice->vat_rate,
403
+                'custom_meta'    => $invoice->payment_meta
404
+            );
405
+
406
+            foreach ( $fields as $key => $val ) {
407
+                if ( is_null( $val ) ) {
408
+                    $val = '';
409
+                }
410
+                $val = maybe_serialize( $val );
411
+                $fields[ $key ] = $wpdb->prepare( '%s', $val );
412
+            }
413
+
414
+            $fields = implode( ', ', $fields );
415
+            $invoice_rows[] = "($fields)";
416
+
417
+            $item_rows    = array();
418
+            $item_columns = array();
419
+            foreach ( $invoice->get_cart_details() as $details ) {
420
+                $fields = array(
421
+                    'post_id'          => $invoice->ID,
422
+                    'item_id'          => $details['id'],
423
+                    'item_name'        => $details['name'],
424
+                    'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'],
425
+                    'vat_rate'         => $details['vat_rate'],
426
+                    'vat_class'        => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'],
427
+                    'tax'              => $details['tax'],
428
+                    'item_price'       => $details['item_price'],
429
+                    'custom_price'     => $details['custom_price'],
430
+                    'quantity'         => $details['quantity'],
431
+                    'discount'         => $details['discount'],
432
+                    'subtotal'         => $details['subtotal'],
433
+                    'price'            => $details['price'],
434
+                    'meta'             => $details['meta'],
435
+                    'fees'             => $details['fees'],
436
+                );
437
+
438
+                $item_columns = array_keys ( $fields );
439
+
440
+                foreach ( $fields as $key => $val ) {
441
+                    if ( is_null( $val ) ) {
442
+                        $val = '';
443
+                    }
444
+                    $val = maybe_serialize( $val );
445
+                    $fields[ $key ] = $wpdb->prepare( '%s', $val );
446
+                }
447
+
448
+                $fields = implode( ', ', $fields );
449
+                $item_rows[] = "($fields)";
450
+            }
451
+
452
+            $item_rows    = implode( ', ', $item_rows );
453
+            $item_columns = implode( ', ', $item_columns );
454
+            $wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" );
455
+        }
456
+
457
+        if ( empty( $invoice_rows ) ) {
458
+            return;
459
+        }
460
+
461
+        $invoice_rows = implode( ', ', $invoice_rows );
462
+        $wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" );
463
+
464
+    }
465 465
 
466 466
 }
Please login to merge, or discard this patch.