Passed
Push — master ( 9807f2...f9d3bb )
by Brian
05:13
created
includes/admin/class-getpaid-installer.php 2 patches
Indentation   +343 added lines, -343 removed lines patch added patch discarded remove patch
@@ -20,214 +20,214 @@  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
-	 * Upgrade to version 2.0.8.
128
-	 *
129
-	 */
130
-	public function upgrade_from_207() {
131
-		global $wpdb;
132
-		$wpdb->query( "ALTER TABLE {$wpdb->prefix}getpaid_invoice_items MODIFY COLUMN quantity FLOAT(20);" );
133
-	}
134
-
135
-	/**
136
-	 * Give administrators the capability to manage GetPaid.
137
-	 *
138
-	 */
139
-	public function add_capabilities() {
140
-		$GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' );
141
-	}
142
-
143
-	/**
144
-	 * Re-create GetPaid pages.
145
-	 *
146
-	 */
147
-	public function create_pages() {
148
-
149
-		$pages = apply_filters(
150
-			'wpinv_create_pages',
151
-			array(
152
-
153
-				// Checkout page.
154
-				'checkout_page' => array(
155
-					'name'      => _x( 'gp-checkout', 'Page slug', 'invoicing' ),
156
-					'title'     => _x( 'Checkout', 'Page title', 'invoicing' ),
157
-					'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
+     * Upgrade to version 2.0.8.
128
+     *
129
+     */
130
+    public function upgrade_from_207() {
131
+        global $wpdb;
132
+        $wpdb->query( "ALTER TABLE {$wpdb->prefix}getpaid_invoice_items MODIFY COLUMN quantity FLOAT(20);" );
133
+    }
134
+
135
+    /**
136
+     * Give administrators the capability to manage GetPaid.
137
+     *
138
+     */
139
+    public function add_capabilities() {
140
+        $GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' );
141
+    }
142
+
143
+    /**
144
+     * Re-create GetPaid pages.
145
+     *
146
+     */
147
+    public function create_pages() {
148
+
149
+        $pages = apply_filters(
150
+            'wpinv_create_pages',
151
+            array(
152
+
153
+                // Checkout page.
154
+                'checkout_page' => array(
155
+                    'name'      => _x( 'gp-checkout', 'Page slug', 'invoicing' ),
156
+                    'title'     => _x( 'Checkout', 'Page title', 'invoicing' ),
157
+                    'content'   => '
158 158
 						<!-- wp:shortcode -->
159 159
 						[wpinv_checkout]
160 160
 						<!-- /wp:shortcode -->
161 161
 					',
162
-					'parent'    => '',
163
-				),
164
-
165
-				// Invoice history page.
166
-				'invoice_history_page' => array(
167
-					'name'    => _x( 'gp-invoices', 'Page slug', 'invoicing' ),
168
-					'title'   => _x( 'My Invoices', 'Page title', 'invoicing' ),
169
-					'content' => '
162
+                    'parent'    => '',
163
+                ),
164
+
165
+                // Invoice history page.
166
+                'invoice_history_page' => array(
167
+                    'name'    => _x( 'gp-invoices', 'Page slug', 'invoicing' ),
168
+                    'title'   => _x( 'My Invoices', 'Page title', 'invoicing' ),
169
+                    'content' => '
170 170
 					<!-- wp:shortcode -->
171 171
 					[wpinv_history]
172 172
 					<!-- /wp:shortcode -->
173 173
 				',
174
-					'parent'  => '',
175
-				),
176
-
177
-				// Success page content.
178
-				'success_page' => array(
179
-					'name'     => _x( 'gp-receipt', 'Page slug', 'invoicing' ),
180
-					'title'    => _x( 'Payment Confirmation', 'Page title', 'invoicing' ),
181
-					'content'  => '
174
+                    'parent'  => '',
175
+                ),
176
+
177
+                // Success page content.
178
+                'success_page' => array(
179
+                    'name'     => _x( 'gp-receipt', 'Page slug', 'invoicing' ),
180
+                    'title'    => _x( 'Payment Confirmation', 'Page title', 'invoicing' ),
181
+                    'content'  => '
182 182
 					<!-- wp:shortcode -->
183 183
 					[wpinv_receipt]
184 184
 					<!-- /wp:shortcode -->
185 185
 				',
186
-					'parent'   => 'gp-checkout',
187
-				),
188
-
189
-				// Failure page content.
190
-				'failure_page' => array(
191
-					'name'    => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ),
192
-					'title'   => _x( 'Transaction Failed', 'Page title', 'invoicing' ),
193
-					'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ),
194
-					'parent'  => 'gp-checkout',
195
-				),
196
-
197
-				// Subscriptions history page.
198
-				'invoice_subscription_page' => array(
199
-					'name'    => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ),
200
-					'title'   => _x( 'My Subscriptions', 'Page title', 'invoicing' ),
201
-					'content' => '
186
+                    'parent'   => 'gp-checkout',
187
+                ),
188
+
189
+                // Failure page content.
190
+                'failure_page' => array(
191
+                    'name'    => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ),
192
+                    'title'   => _x( 'Transaction Failed', 'Page title', 'invoicing' ),
193
+                    'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ),
194
+                    'parent'  => 'gp-checkout',
195
+                ),
196
+
197
+                // Subscriptions history page.
198
+                'invoice_subscription_page' => array(
199
+                    'name'    => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ),
200
+                    'title'   => _x( 'My Subscriptions', 'Page title', 'invoicing' ),
201
+                    'content' => '
202 202
 					<!-- wp:shortcode -->
203 203
 					[wpinv_subscriptions]
204 204
 					<!-- /wp:shortcode -->
205 205
 				',
206
-					'parent' => '',
207
-				),
206
+                    'parent' => '',
207
+                ),
208 208
 
209
-			)
210
-		);
209
+            )
210
+        );
211 211
 
212
-		foreach ( $pages as $key => $page ) {
213
-			wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] );
214
-		}
212
+        foreach ( $pages as $key => $page ) {
213
+            wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] );
214
+        }
215 215
 
216
-	}
216
+    }
217 217
 
218
-	/**
219
-	 * Create subscriptions table.
220
-	 *
221
-	 */
222
-	public function create_subscriptions_table() {
218
+    /**
219
+     * Create subscriptions table.
220
+     *
221
+     */
222
+    public function create_subscriptions_table() {
223 223
 
224
-		global $wpdb;
224
+        global $wpdb;
225 225
 
226
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
226
+        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
227 227
 
228
-		// Create tables.
229
-		$charset_collate = $wpdb->get_charset_collate();
230
-		$sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpinv_subscriptions (
228
+        // Create tables.
229
+        $charset_collate = $wpdb->get_charset_collate();
230
+        $sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}wpinv_subscriptions (
231 231
 			id bigint(20) unsigned NOT NULL auto_increment,
232 232
 			customer_id bigint(20) NOT NULL,
233 233
 			frequency int(11) NOT NULL DEFAULT '1',
@@ -250,22 +250,22 @@  discard block
 block discarded – undo
250 250
 			KEY customer_and_status (customer_id, status)
251 251
 		  ) $charset_collate;";
252 252
 
253
-		dbDelta( $sql );
253
+        dbDelta( $sql );
254 254
 
255
-	}
255
+    }
256 256
 
257
-	/**
258
-	 * Create invoices table.
259
-	 *
260
-	 */
261
-	public function create_invoices_table() {
262
-		global $wpdb;
257
+    /**
258
+     * Create invoices table.
259
+     *
260
+     */
261
+    public function create_invoices_table() {
262
+        global $wpdb;
263 263
 
264
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
264
+        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
265 265
 
266
-		// Create tables.
267
-		$charset_collate = $wpdb->get_charset_collate();
268
-		$sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoices (
266
+        // Create tables.
267
+        $charset_collate = $wpdb->get_charset_collate();
268
+        $sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoices (
269 269
 			post_id BIGINT(20) NOT NULL,
270 270
             `number` VARCHAR(100),
271 271
             `key` VARCHAR(100),
@@ -301,22 +301,22 @@  discard block
 block discarded – undo
301 301
 			KEY `key` (`key`)
302 302
 		  ) $charset_collate;";
303 303
 
304
-		dbDelta( $sql );
304
+        dbDelta( $sql );
305 305
 
306
-	}
306
+    }
307 307
 
308
-	/**
309
-	 * Create invoice items table.
310
-	 *
311
-	 */
312
-	public function create_invoice_items_table() {
313
-		global $wpdb;
308
+    /**
309
+     * Create invoice items table.
310
+     *
311
+     */
312
+    public function create_invoice_items_table() {
313
+        global $wpdb;
314 314
 
315
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
315
+        require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
316 316
 
317
-		// Create tables.
318
-		$charset_collate = $wpdb->get_charset_collate();
319
-		$sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoice_items (
317
+        // Create tables.
318
+        $charset_collate = $wpdb->get_charset_collate();
319
+        $sql             = "CREATE TABLE IF NOT EXISTS {$wpdb->prefix}getpaid_invoice_items (
320 320
 			ID BIGINT(20) NOT NULL AUTO_INCREMENT,
321 321
             post_id BIGINT(20) NOT NULL,
322 322
             item_id BIGINT(20) NOT NULL,
@@ -338,139 +338,139 @@  discard block
 block discarded – undo
338 338
 			KEY post_id (post_id)
339 339
 		  ) $charset_collate;";
340 340
 
341
-		dbDelta( $sql );
342
-
343
-	}
344
-
345
-	/**
346
-	 * Migrates old invoices to new invoices.
347
-	 *
348
-	 */
349
-	public function migrate_old_invoices() {
350
-		global $wpdb;
351
-
352
-		$invoices_table      = $wpdb->prefix . 'getpaid_invoices';
353
-		$invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items';
354
-		$migrated            = $wpdb->get_col( "SELECT post_id FROM $invoices_table" );
355
-		$invoices            = array_unique(
356
-			get_posts(
357
-				array(
358
-					'post_type'      => array( 'wpi_invoice', 'wpi_quote' ),
359
-					'posts_per_page' => -1,
360
-					'fields'         => 'ids',
361
-					'post_status'    => array_keys( get_post_stati() ),
362
-					'exclude'        => (array) $migrated,
363
-				)
364
-			)
365
-		);
366
-
367
-		// Abort if we do not have any invoices.
368
-		if ( empty( $invoices ) ) {
369
-			return;
370
-		}
371
-
372
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php' );
373
-
374
-		$invoice_rows = array();
375
-		foreach ( $invoices as $invoice ) {
376
-
377
-			$invoice = new WPInv_Legacy_Invoice( $invoice );
378
-
379
-			if ( empty( $invoice->ID ) ) {
380
-				return;
381
-			}
382
-
383
-			$fields = array (
384
-				'post_id'        => $invoice->ID,
385
-				'number'         => $invoice->get_number(),
386
-				'key'            => $invoice->get_key(),
387
-				'type'           => str_replace( 'wpi_', '', $invoice->post_type ),
388
-				'mode'           => $invoice->mode,
389
-				'user_ip'        => $invoice->get_ip(),
390
-				'first_name'     => $invoice->get_first_name(),
391
-				'last_name'      => $invoice->get_last_name(),
392
-				'address'        => $invoice->get_address(),
393
-				'city'           => $invoice->city,
394
-				'state'          => $invoice->state,
395
-				'country'        => $invoice->country,
396
-				'zip'            => $invoice->zip,
397
-				'adddress_confirmed' => (int) $invoice->adddress_confirmed,
398
-				'gateway'        => $invoice->get_gateway(),
399
-				'transaction_id' => $invoice->get_transaction_id(),
400
-				'currency'       => $invoice->get_currency(),
401
-				'subtotal'       => $invoice->get_subtotal(),
402
-				'tax'            => $invoice->get_tax(),
403
-				'fees_total'     => $invoice->get_fees_total(),
404
-				'total'          => $invoice->get_total(),
405
-				'discount'       => $invoice->get_discount(),
406
-				'discount_code'  => $invoice->get_discount_code(),
407
-				'disable_taxes'  => $invoice->disable_taxes,
408
-				'due_date'       => $invoice->get_due_date(),
409
-				'completed_date' => $invoice->get_completed_date(),
410
-				'company'        => $invoice->company,
411
-				'vat_number'     => $invoice->vat_number,
412
-				'vat_rate'       => $invoice->vat_rate,
413
-				'custom_meta'    => $invoice->payment_meta
414
-			);
415
-
416
-			foreach ( $fields as $key => $val ) {
417
-				if ( is_null( $val ) ) {
418
-					$val = '';
419
-				}
420
-				$val = maybe_serialize( $val );
421
-				$fields[ $key ] = $wpdb->prepare( '%s', $val );
422
-			}
423
-
424
-			$fields = implode( ', ', $fields );
425
-			$invoice_rows[] = "($fields)";
426
-
427
-			$item_rows    = array();
428
-			$item_columns = array();
429
-			foreach ( $invoice->get_cart_details() as $details ) {
430
-				$fields = array(
431
-					'post_id'          => $invoice->ID,
432
-					'item_id'          => $details['id'],
433
-					'item_name'        => $details['name'],
434
-					'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'],
435
-					'vat_rate'         => $details['vat_rate'],
436
-					'vat_class'        => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'],
437
-					'tax'              => $details['tax'],
438
-					'item_price'       => $details['item_price'],
439
-					'custom_price'     => $details['custom_price'],
440
-					'quantity'         => $details['quantity'],
441
-					'discount'         => $details['discount'],
442
-					'subtotal'         => $details['subtotal'],
443
-					'price'            => $details['price'],
444
-					'meta'             => $details['meta'],
445
-					'fees'             => $details['fees'],
446
-				);
447
-
448
-				$item_columns = array_keys ( $fields );
449
-
450
-				foreach ( $fields as $key => $val ) {
451
-					if ( is_null( $val ) ) {
452
-						$val = '';
453
-					}
454
-					$val = maybe_serialize( $val );
455
-					$fields[ $key ] = $wpdb->prepare( '%s', $val );
456
-				}
457
-
458
-				$fields = implode( ', ', $fields );
459
-				$item_rows[] = "($fields)";
460
-			}
461
-
462
-			$item_rows    = implode( ', ', $item_rows );
463
-			$item_columns = implode( ', ', $item_columns );
464
-			$wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" );
465
-		}
466
-
467
-		if ( empty( $invoice_rows ) ) {
468
-			return;
469
-		}
470
-
471
-		$invoice_rows = implode( ', ', $invoice_rows );
472
-		$wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" );
473
-
474
-	}
341
+        dbDelta( $sql );
342
+
343
+    }
344
+
345
+    /**
346
+     * Migrates old invoices to new invoices.
347
+     *
348
+     */
349
+    public function migrate_old_invoices() {
350
+        global $wpdb;
351
+
352
+        $invoices_table      = $wpdb->prefix . 'getpaid_invoices';
353
+        $invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items';
354
+        $migrated            = $wpdb->get_col( "SELECT post_id FROM $invoices_table" );
355
+        $invoices            = array_unique(
356
+            get_posts(
357
+                array(
358
+                    'post_type'      => array( 'wpi_invoice', 'wpi_quote' ),
359
+                    'posts_per_page' => -1,
360
+                    'fields'         => 'ids',
361
+                    'post_status'    => array_keys( get_post_stati() ),
362
+                    'exclude'        => (array) $migrated,
363
+                )
364
+            )
365
+        );
366
+
367
+        // Abort if we do not have any invoices.
368
+        if ( empty( $invoices ) ) {
369
+            return;
370
+        }
371
+
372
+        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php' );
373
+
374
+        $invoice_rows = array();
375
+        foreach ( $invoices as $invoice ) {
376
+
377
+            $invoice = new WPInv_Legacy_Invoice( $invoice );
378
+
379
+            if ( empty( $invoice->ID ) ) {
380
+                return;
381
+            }
382
+
383
+            $fields = array (
384
+                'post_id'        => $invoice->ID,
385
+                'number'         => $invoice->get_number(),
386
+                'key'            => $invoice->get_key(),
387
+                'type'           => str_replace( 'wpi_', '', $invoice->post_type ),
388
+                'mode'           => $invoice->mode,
389
+                'user_ip'        => $invoice->get_ip(),
390
+                'first_name'     => $invoice->get_first_name(),
391
+                'last_name'      => $invoice->get_last_name(),
392
+                'address'        => $invoice->get_address(),
393
+                'city'           => $invoice->city,
394
+                'state'          => $invoice->state,
395
+                'country'        => $invoice->country,
396
+                'zip'            => $invoice->zip,
397
+                'adddress_confirmed' => (int) $invoice->adddress_confirmed,
398
+                'gateway'        => $invoice->get_gateway(),
399
+                'transaction_id' => $invoice->get_transaction_id(),
400
+                'currency'       => $invoice->get_currency(),
401
+                'subtotal'       => $invoice->get_subtotal(),
402
+                'tax'            => $invoice->get_tax(),
403
+                'fees_total'     => $invoice->get_fees_total(),
404
+                'total'          => $invoice->get_total(),
405
+                'discount'       => $invoice->get_discount(),
406
+                'discount_code'  => $invoice->get_discount_code(),
407
+                'disable_taxes'  => $invoice->disable_taxes,
408
+                'due_date'       => $invoice->get_due_date(),
409
+                'completed_date' => $invoice->get_completed_date(),
410
+                'company'        => $invoice->company,
411
+                'vat_number'     => $invoice->vat_number,
412
+                'vat_rate'       => $invoice->vat_rate,
413
+                'custom_meta'    => $invoice->payment_meta
414
+            );
415
+
416
+            foreach ( $fields as $key => $val ) {
417
+                if ( is_null( $val ) ) {
418
+                    $val = '';
419
+                }
420
+                $val = maybe_serialize( $val );
421
+                $fields[ $key ] = $wpdb->prepare( '%s', $val );
422
+            }
423
+
424
+            $fields = implode( ', ', $fields );
425
+            $invoice_rows[] = "($fields)";
426
+
427
+            $item_rows    = array();
428
+            $item_columns = array();
429
+            foreach ( $invoice->get_cart_details() as $details ) {
430
+                $fields = array(
431
+                    'post_id'          => $invoice->ID,
432
+                    'item_id'          => $details['id'],
433
+                    'item_name'        => $details['name'],
434
+                    'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'],
435
+                    'vat_rate'         => $details['vat_rate'],
436
+                    'vat_class'        => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'],
437
+                    'tax'              => $details['tax'],
438
+                    'item_price'       => $details['item_price'],
439
+                    'custom_price'     => $details['custom_price'],
440
+                    'quantity'         => $details['quantity'],
441
+                    'discount'         => $details['discount'],
442
+                    'subtotal'         => $details['subtotal'],
443
+                    'price'            => $details['price'],
444
+                    'meta'             => $details['meta'],
445
+                    'fees'             => $details['fees'],
446
+                );
447
+
448
+                $item_columns = array_keys ( $fields );
449
+
450
+                foreach ( $fields as $key => $val ) {
451
+                    if ( is_null( $val ) ) {
452
+                        $val = '';
453
+                    }
454
+                    $val = maybe_serialize( $val );
455
+                    $fields[ $key ] = $wpdb->prepare( '%s', $val );
456
+                }
457
+
458
+                $fields = implode( ', ', $fields );
459
+                $item_rows[] = "($fields)";
460
+            }
461
+
462
+            $item_rows    = implode( ', ', $item_rows );
463
+            $item_columns = implode( ', ', $item_columns );
464
+            $wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" );
465
+        }
466
+
467
+        if ( empty( $invoice_rows ) ) {
468
+            return;
469
+        }
470
+
471
+        $invoice_rows = implode( ', ', $invoice_rows );
472
+        $wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" );
473
+
474
+    }
475 475
 
476 476
 }
Please login to merge, or discard this patch.
Spacing   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -8,7 +8,7 @@  discard block
 block discarded – undo
8 8
  * @since   2.0.2
9 9
  */
10 10
 
11
-defined( 'ABSPATH' ) || exit;
11
+defined('ABSPATH') || exit;
12 12
 
13 13
 /**
14 14
  * The main installer/updater class.
@@ -25,10 +25,10 @@  discard block
 block discarded – undo
25 25
 	 *
26 26
 	 * @param string $upgrade_from The current invoicing version.
27 27
 	 */
28
-	public function upgrade_db( $upgrade_from ) {
28
+	public function upgrade_db($upgrade_from) {
29 29
 
30 30
 		// Save the current invoicing version.
31
-		update_option( 'wpinv_version', WPINV_VERSION );
31
+		update_option('wpinv_version', WPINV_VERSION);
32 32
 
33 33
 		// Setup the invoice Custom Post Type.
34 34
 		GetPaid_Post_Types::register_post_types();
@@ -48,7 +48,7 @@  discard block
 block discarded – undo
48 48
 		// Create any missing database tables.
49 49
 		$method = "upgrade_from_$upgrade_from";
50 50
 
51
-		if ( method_exists( $this, $method ) ) {
51
+		if (method_exists($this, $method)) {
52 52
 			$this->$method();
53 53
 		}
54 54
 
@@ -64,7 +64,7 @@  discard block
 block discarded – undo
64 64
 		$this->create_invoice_items_table();
65 65
 
66 66
 		// Save default tax rates.
67
-		update_option( 'wpinv_tax_rates', wpinv_get_data( 'tax-rates' ) );
67
+		update_option('wpinv_tax_rates', wpinv_get_data('tax-rates'));
68 68
 	}
69 69
 
70 70
 	/**
@@ -75,28 +75,28 @@  discard block
 block discarded – undo
75 75
 		global $wpdb;
76 76
 
77 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' )" );
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 81
 
82 82
 			// Clean post cache
83
-			foreach ( $results as $row ) {
84
-				clean_post_cache( $row->ID );
83
+			foreach ($results as $row) {
84
+				clean_post_cache($row->ID);
85 85
 			}
86 86
 
87 87
 		}
88 88
 
89 89
 		// Item meta key changes
90 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 );
91
+		$results = $wpdb->get_results($query);
92 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'" );
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 97
 
98
-			foreach ( $results as $row ) {
99
-				clean_post_cache( $row->post_id );
98
+			foreach ($results as $row) {
99
+				clean_post_cache($row->post_id);
100 100
 			}
101 101
 
102 102
 		}
@@ -129,7 +129,7 @@  discard block
 block discarded – undo
129 129
 	 */
130 130
 	public function upgrade_from_207() {
131 131
 		global $wpdb;
132
-		$wpdb->query( "ALTER TABLE {$wpdb->prefix}getpaid_invoice_items MODIFY COLUMN quantity FLOAT(20);" );
132
+		$wpdb->query("ALTER TABLE {$wpdb->prefix}getpaid_invoice_items MODIFY COLUMN quantity FLOAT(20);");
133 133
 	}
134 134
 
135 135
 	/**
@@ -137,7 +137,7 @@  discard block
 block discarded – undo
137 137
 	 *
138 138
 	 */
139 139
 	public function add_capabilities() {
140
-		$GLOBALS['wp_roles']->add_cap( 'administrator', 'manage_invoicing' );
140
+		$GLOBALS['wp_roles']->add_cap('administrator', 'manage_invoicing');
141 141
 	}
142 142
 
143 143
 	/**
@@ -152,8 +152,8 @@  discard block
 block discarded – undo
152 152
 
153 153
 				// Checkout page.
154 154
 				'checkout_page' => array(
155
-					'name'      => _x( 'gp-checkout', 'Page slug', 'invoicing' ),
156
-					'title'     => _x( 'Checkout', 'Page title', 'invoicing' ),
155
+					'name'      => _x('gp-checkout', 'Page slug', 'invoicing'),
156
+					'title'     => _x('Checkout', 'Page title', 'invoicing'),
157 157
 					'content'   => '
158 158
 						<!-- wp:shortcode -->
159 159
 						[wpinv_checkout]
@@ -164,8 +164,8 @@  discard block
 block discarded – undo
164 164
 
165 165
 				// Invoice history page.
166 166
 				'invoice_history_page' => array(
167
-					'name'    => _x( 'gp-invoices', 'Page slug', 'invoicing' ),
168
-					'title'   => _x( 'My Invoices', 'Page title', 'invoicing' ),
167
+					'name'    => _x('gp-invoices', 'Page slug', 'invoicing'),
168
+					'title'   => _x('My Invoices', 'Page title', 'invoicing'),
169 169
 					'content' => '
170 170
 					<!-- wp:shortcode -->
171 171
 					[wpinv_history]
@@ -176,8 +176,8 @@  discard block
 block discarded – undo
176 176
 
177 177
 				// Success page content.
178 178
 				'success_page' => array(
179
-					'name'     => _x( 'gp-receipt', 'Page slug', 'invoicing' ),
180
-					'title'    => _x( 'Payment Confirmation', 'Page title', 'invoicing' ),
179
+					'name'     => _x('gp-receipt', 'Page slug', 'invoicing'),
180
+					'title'    => _x('Payment Confirmation', 'Page title', 'invoicing'),
181 181
 					'content'  => '
182 182
 					<!-- wp:shortcode -->
183 183
 					[wpinv_receipt]
@@ -188,16 +188,16 @@  discard block
 block discarded – undo
188 188
 
189 189
 				// Failure page content.
190 190
 				'failure_page' => array(
191
-					'name'    => _x( 'gp-transaction-failed', 'Page slug', 'invoicing' ),
192
-					'title'   => _x( 'Transaction Failed', 'Page title', 'invoicing' ),
193
-					'content' => __( 'Your transaction failed, please try again or contact site support.', 'invoicing' ),
191
+					'name'    => _x('gp-transaction-failed', 'Page slug', 'invoicing'),
192
+					'title'   => _x('Transaction Failed', 'Page title', 'invoicing'),
193
+					'content' => __('Your transaction failed, please try again or contact site support.', 'invoicing'),
194 194
 					'parent'  => 'gp-checkout',
195 195
 				),
196 196
 
197 197
 				// Subscriptions history page.
198 198
 				'invoice_subscription_page' => array(
199
-					'name'    => _x( 'gp-subscriptions', 'Page slug', 'invoicing' ),
200
-					'title'   => _x( 'My Subscriptions', 'Page title', 'invoicing' ),
199
+					'name'    => _x('gp-subscriptions', 'Page slug', 'invoicing'),
200
+					'title'   => _x('My Subscriptions', 'Page title', 'invoicing'),
201 201
 					'content' => '
202 202
 					<!-- wp:shortcode -->
203 203
 					[wpinv_subscriptions]
@@ -209,8 +209,8 @@  discard block
 block discarded – undo
209 209
 			)
210 210
 		);
211 211
 
212
-		foreach ( $pages as $key => $page ) {
213
-			wpinv_create_page( esc_sql( $page['name'] ), $key, $page['title'], $page['content'], $page['parent'] );
212
+		foreach ($pages as $key => $page) {
213
+			wpinv_create_page(esc_sql($page['name']), $key, $page['title'], $page['content'], $page['parent']);
214 214
 		}
215 215
 
216 216
 	}
@@ -223,7 +223,7 @@  discard block
 block discarded – undo
223 223
 
224 224
 		global $wpdb;
225 225
 
226
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
226
+		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
227 227
 
228 228
 		// Create tables.
229 229
 		$charset_collate = $wpdb->get_charset_collate();
@@ -250,7 +250,7 @@  discard block
 block discarded – undo
250 250
 			KEY customer_and_status (customer_id, status)
251 251
 		  ) $charset_collate;";
252 252
 
253
-		dbDelta( $sql );
253
+		dbDelta($sql);
254 254
 
255 255
 	}
256 256
 
@@ -261,7 +261,7 @@  discard block
 block discarded – undo
261 261
 	public function create_invoices_table() {
262 262
 		global $wpdb;
263 263
 
264
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
264
+		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
265 265
 
266 266
 		// Create tables.
267 267
 		$charset_collate = $wpdb->get_charset_collate();
@@ -301,7 +301,7 @@  discard block
 block discarded – undo
301 301
 			KEY `key` (`key`)
302 302
 		  ) $charset_collate;";
303 303
 
304
-		dbDelta( $sql );
304
+		dbDelta($sql);
305 305
 
306 306
 	}
307 307
 
@@ -312,7 +312,7 @@  discard block
 block discarded – undo
312 312
 	public function create_invoice_items_table() {
313 313
 		global $wpdb;
314 314
 
315
-		require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
315
+		require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
316 316
 
317 317
 		// Create tables.
318 318
 		$charset_collate = $wpdb->get_charset_collate();
@@ -338,7 +338,7 @@  discard block
 block discarded – undo
338 338
 			KEY post_id (post_id)
339 339
 		  ) $charset_collate;";
340 340
 
341
-		dbDelta( $sql );
341
+		dbDelta($sql);
342 342
 
343 343
 	}
344 344
 
@@ -351,40 +351,40 @@  discard block
 block discarded – undo
351 351
 
352 352
 		$invoices_table      = $wpdb->prefix . 'getpaid_invoices';
353 353
 		$invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items';
354
-		$migrated            = $wpdb->get_col( "SELECT post_id FROM $invoices_table" );
354
+		$migrated            = $wpdb->get_col("SELECT post_id FROM $invoices_table");
355 355
 		$invoices            = array_unique(
356 356
 			get_posts(
357 357
 				array(
358
-					'post_type'      => array( 'wpi_invoice', 'wpi_quote' ),
358
+					'post_type'      => array('wpi_invoice', 'wpi_quote'),
359 359
 					'posts_per_page' => -1,
360 360
 					'fields'         => 'ids',
361
-					'post_status'    => array_keys( get_post_stati() ),
361
+					'post_status'    => array_keys(get_post_stati()),
362 362
 					'exclude'        => (array) $migrated,
363 363
 				)
364 364
 			)
365 365
 		);
366 366
 
367 367
 		// Abort if we do not have any invoices.
368
-		if ( empty( $invoices ) ) {
368
+		if (empty($invoices)) {
369 369
 			return;
370 370
 		}
371 371
 
372
-		require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php' );
372
+		require_once(WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php');
373 373
 
374 374
 		$invoice_rows = array();
375
-		foreach ( $invoices as $invoice ) {
375
+		foreach ($invoices as $invoice) {
376 376
 
377
-			$invoice = new WPInv_Legacy_Invoice( $invoice );
377
+			$invoice = new WPInv_Legacy_Invoice($invoice);
378 378
 
379
-			if ( empty( $invoice->ID ) ) {
379
+			if (empty($invoice->ID)) {
380 380
 				return;
381 381
 			}
382 382
 
383
-			$fields = array (
383
+			$fields = array(
384 384
 				'post_id'        => $invoice->ID,
385 385
 				'number'         => $invoice->get_number(),
386 386
 				'key'            => $invoice->get_key(),
387
-				'type'           => str_replace( 'wpi_', '', $invoice->post_type ),
387
+				'type'           => str_replace('wpi_', '', $invoice->post_type),
388 388
 				'mode'           => $invoice->mode,
389 389
 				'user_ip'        => $invoice->get_ip(),
390 390
 				'first_name'     => $invoice->get_first_name(),
@@ -413,27 +413,27 @@  discard block
 block discarded – undo
413 413
 				'custom_meta'    => $invoice->payment_meta
414 414
 			);
415 415
 
416
-			foreach ( $fields as $key => $val ) {
417
-				if ( is_null( $val ) ) {
416
+			foreach ($fields as $key => $val) {
417
+				if (is_null($val)) {
418 418
 					$val = '';
419 419
 				}
420
-				$val = maybe_serialize( $val );
421
-				$fields[ $key ] = $wpdb->prepare( '%s', $val );
420
+				$val = maybe_serialize($val);
421
+				$fields[$key] = $wpdb->prepare('%s', $val);
422 422
 			}
423 423
 
424
-			$fields = implode( ', ', $fields );
424
+			$fields = implode(', ', $fields);
425 425
 			$invoice_rows[] = "($fields)";
426 426
 
427 427
 			$item_rows    = array();
428 428
 			$item_columns = array();
429
-			foreach ( $invoice->get_cart_details() as $details ) {
429
+			foreach ($invoice->get_cart_details() as $details) {
430 430
 				$fields = array(
431 431
 					'post_id'          => $invoice->ID,
432 432
 					'item_id'          => $details['id'],
433 433
 					'item_name'        => $details['name'],
434
-					'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'],
434
+					'item_description' => empty($details['meta']['description']) ? '' : $details['meta']['description'],
435 435
 					'vat_rate'         => $details['vat_rate'],
436
-					'vat_class'        => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'],
436
+					'vat_class'        => empty($details['vat_class']) ? '_standard' : $details['vat_class'],
437 437
 					'tax'              => $details['tax'],
438 438
 					'item_price'       => $details['item_price'],
439 439
 					'custom_price'     => $details['custom_price'],
@@ -445,31 +445,31 @@  discard block
 block discarded – undo
445 445
 					'fees'             => $details['fees'],
446 446
 				);
447 447
 
448
-				$item_columns = array_keys ( $fields );
448
+				$item_columns = array_keys($fields);
449 449
 
450
-				foreach ( $fields as $key => $val ) {
451
-					if ( is_null( $val ) ) {
450
+				foreach ($fields as $key => $val) {
451
+					if (is_null($val)) {
452 452
 						$val = '';
453 453
 					}
454
-					$val = maybe_serialize( $val );
455
-					$fields[ $key ] = $wpdb->prepare( '%s', $val );
454
+					$val = maybe_serialize($val);
455
+					$fields[$key] = $wpdb->prepare('%s', $val);
456 456
 				}
457 457
 
458
-				$fields = implode( ', ', $fields );
458
+				$fields = implode(', ', $fields);
459 459
 				$item_rows[] = "($fields)";
460 460
 			}
461 461
 
462
-			$item_rows    = implode( ', ', $item_rows );
463
-			$item_columns = implode( ', ', $item_columns );
464
-			$wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" );
462
+			$item_rows    = implode(', ', $item_rows);
463
+			$item_columns = implode(', ', $item_columns);
464
+			$wpdb->query("INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows");
465 465
 		}
466 466
 
467
-		if ( empty( $invoice_rows ) ) {
467
+		if (empty($invoice_rows)) {
468 468
 			return;
469 469
 		}
470 470
 
471
-		$invoice_rows = implode( ', ', $invoice_rows );
472
-		$wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" );
471
+		$invoice_rows = implode(', ', $invoice_rows);
472
+		$wpdb->query("INSERT INTO $invoices_table VALUES $invoice_rows");
473 473
 
474 474
 	}
475 475
 
Please login to merge, or discard this patch.