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