Passed
Push — master ( 6621fa...568391 )
by Brian
06:12
created

wpinv_v119_upgrades()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Upgrade related functions.
4
 *
5
 * @since 1.0.0
6
 */
7
8
/**
9
 * Perform automatic upgrades when necessary.
10
 *
11
 * @since 1.0.0
12
*/
13
function wpinv_automatic_upgrade() {
14
    $wpi_version = get_option( 'wpinv_version' );
15
16
    // Update tables.
17
    if ( ! get_option( 'getpaid_created_invoice_tables' ) ) {
18
        wpinv_v119_upgrades();
19
        update_option( 'getpaid_created_invoice_tables', true );
20
    }
21
22
    if ( $wpi_version == WPINV_VERSION ) {
23
        return;
24
    }
25
26
    if ( version_compare( $wpi_version, '0.0.5', '<' ) ) {
0 ignored issues
show
Bug introduced by
It seems like $wpi_version can also be of type false; however, parameter $version1 of version_compare() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

26
    if ( version_compare( /** @scrutinizer ignore-type */ $wpi_version, '0.0.5', '<' ) ) {
Loading history...
27
        wpinv_v005_upgrades();
28
    }
29
30
    if ( version_compare( $wpi_version, '1.0.3', '<' ) ) {
31
        wpinv_v110_upgrades();
32
    }
33
34
    update_option( 'wpinv_version', WPINV_VERSION );
35
}
36
add_action( 'admin_init', 'wpinv_automatic_upgrade' );
37
38
function wpinv_v005_upgrades() {
39
    global $wpdb;
40
41
    // Invoices status
42
    $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' )" );
43
    if ( !empty( $results ) ) {
44
        $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' )" );
45
46
        // Clean post cache
47
        foreach ( $results as $row ) {
48
            clean_post_cache( $row->ID );
49
        }
50
    }
51
52
    // Item meta key changes
53
    $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' )";
54
    $results = $wpdb->get_results( $query );
55
56
    if ( !empty( $results ) ) {
57
        $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" );
58
        $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" );
59
        $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" );
60
        
61
        foreach ( $results as $row ) {
62
            clean_post_cache( $row->post_id );
63
        }
64
    }
65
66
    wpinv_add_admin_caps();
67
}
68
69
function wpinv_v110_upgrades() {
70
71
    // Add Subscription tables
72
    $db = new WPInv_Subscriptions_DB;
73
    /** @scrutinizer ignore-unhandled */ @$db->create_table();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $db->create_table() targeting WPInv_Subscriptions_DB::create_table() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
74
75
}
76
77
/**
78
 * Version 119 upgrades.
79
 */
80
function wpinv_v119_upgrades() {
81
    wpinv_create_invoices_table();
82
    wpinv_convert_old_invoices();
83
}
84
85
/**
86
 * Creates the invoices table.
87
 */
88
function wpinv_create_invoices_table() {
89
    global $wpdb;
90
91
    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
92
93
    // Create invoices table.
94
    $table = $wpdb->prefix . 'getpaid_invoices';
95
    $sql   = "CREATE TABLE $table (
96
97
            post_id BIGINT(20) NOT NULL,
98
            `number` VARCHAR(100),
99
            `key` VARCHAR(100),
100
            `type` VARCHAR(100) NOT NULL DEFAULT 'invoice',
101
            mode VARCHAR(100) NOT NULL DEFAULT 'live',
102
103
            user_ip VARCHAR(100),
104
            first_name VARCHAR(100),
105
            last_name VARCHAR(100),
106
            `address` VARCHAR(100),
107
            city VARCHAR(100),
108
            `state` VARCHAR(100),
109
            country VARCHAR(100),
110
            zip VARCHAR(100),
111
            adddress_confirmed INT(10),
112
113
            gateway VARCHAR(100),
114
            transaction_id VARCHAR(100),
115
            currency VARCHAR(10),
116
            subtotal FLOAT NOT NULL DEFAULT 0,
117
            tax FLOAT NOT NULL DEFAULT 0,
118
            fees_total FLOAT NOT NULL DEFAULT 0,
119
            total FLOAT NOT NULL DEFAULT 0,
120
            discount FLOAT NOT NULL DEFAULT 0,
121
            discount_code VARCHAR(100),
122
            disable_taxes INT(2) NOT NULL DEFAULT 0,
123
            due_date DATETIME,
124
            completed_date DATETIME,
125
            company VARCHAR(100),
126
            vat_number VARCHAR(100),
127
            vat_rate VARCHAR(100),
128
129
            custom_meta TEXT,
130
            PRIMARY KEY  (post_id),
131
            KEY number (number),
132
            KEY `key` ( `key` )
133
            ) CHARACTER SET utf8 COLLATE utf8_general_ci;";
134
135
    dbDelta( $sql );
136
137
    // Create invoice items table.
138
    $table = $wpdb->prefix . 'getpaid_invoice_items';
139
    $sql   = "CREATE TABLE $table (
140
            ID BIGINT(20) NOT NULL AUTO_INCREMENT,
141
            post_id BIGINT(20) NOT NULL,
142
143
            item_id BIGINT(20) NOT NULL,
144
            item_name TEXT NOT NULL,
145
            item_description TEXT NOT NULL,
146
147
            vat_rate FLOAT NOT NULL DEFAULT 0,
148
            vat_class VARCHAR(100),
149
            tax FLOAT NOT NULL DEFAULT 0,
150
            item_price FLOAT NOT NULL DEFAULT 0,
151
            custom_price FLOAT NOT NULL DEFAULT 0,
152
            quantity INT(20) NOT NULL DEFAULT 1,
153
            discount FLOAT NOT NULL DEFAULT 0,
154
            subtotal FLOAT NOT NULL DEFAULT 0,
155
            price FLOAT NOT NULL DEFAULT 0,
156
            meta TEXT,
157
            fees TEXT,
158
            PRIMARY KEY  (ID),
159
            KEY item_id (item_id),
160
            KEY post_id ( post_id )
161
            ) CHARACTER SET utf8 COLLATE utf8_general_ci;";
162
163
    dbDelta( $sql );
164
}
165
166
/**
167
 * Copies data from meta tables to our custom tables.
168
 */
169
function wpinv_convert_old_invoices() {
170
    global $wpdb;
171
172
    $invoices = array_unique(
173
        get_posts(
174
            array(
175
                'post_type'      => array( 'wpi_invoice', 'wpi_quote' ),
176
                'posts_per_page' => -1,
177
                'fields'         => 'ids',
178
                'post_status'    => array_keys( wpinv_get_invoice_statuses( true ) ),
179
            )
180
        )
181
    );
182
    $invoices_table = $wpdb->prefix . 'getpaid_invoices';
183
    $invoice_items_table = $wpdb->prefix . 'getpaid_invoice_items';
184
185
    if ( ! class_exists( 'WPInv_Legacy_Invoice' ) ) {
186
        require_once( WPINV_PLUGIN_DIR . 'includes/class-wpinv-legacy-invoice.php' );
187
    }
188
189
    $invoice_rows = array();
190
    foreach ( $invoices as $invoice ) {
191
192
        $invoice = new WPInv_Legacy_Invoice( $invoice );
193
        $fields = array (
194
            'post_id'        => $invoice->ID,
195
            'number'         => $invoice->get_number(),
196
            'key'            => $invoice->get_key(),
197
            'type'           => str_replace( 'wpi_', '', $invoice->post_type ),
198
            'mode'           => $invoice->mode,
199
            'user_ip'        => $invoice->get_ip(),
200
            'first_name'     => $invoice->get_first_name(),
201
            'last_name'      => $invoice->get_last_name(),
202
            'address'        => $invoice->get_address(),
203
            'city'           => $invoice->city,
204
            'state'          => $invoice->state,
205
            'country'        => $invoice->country,
206
            'zip'            => $invoice->zip,
207
            'adddress_confirmed' => (int) $invoice->adddress_confirmed,
208
            'gateway'        => $invoice->get_gateway(),
209
            'transaction_id' => $invoice->get_transaction_id(),
210
            'currency'       => $invoice->get_currency(),
211
            'subtotal'       => $invoice->get_subtotal(),
212
            'tax'            => $invoice->get_tax(),
213
            'fees_total'     => $invoice->get_fees_total(),
214
            'total'          => $invoice->get_total(),
215
            'discount'       => $invoice->get_discount(),
216
            'discount_code'  => $invoice->get_discount_code(),
217
            'disable_taxes'  => $invoice->disable_taxes,
218
            'due_date'       => $invoice->get_due_date(),
219
            'completed_date' => $invoice->get_completed_date(),
220
            'company'        => $invoice->company,
221
            'vat_number'     => $invoice->vat_number,
222
            'vat_rate'       => $invoice->vat_rate,
223
            'custom_meta'    => $invoice->payment_meta
224
        );
225
226
        foreach ( $fields as $key => $val ) {
227
            if ( is_null( $val ) ) {
228
                $val = '';
229
            }
230
            $val = maybe_serialize( $val );
231
            $fields[ $key ] = $wpdb->prepare( '%s', $val );
232
        }
233
234
        $fields = implode( ', ', $fields );
235
        $invoice_rows[] = "($fields)";
236
237
        $item_rows    = array();
238
        $item_columns = array();
239
        foreach ( $invoice->get_cart_details() as $details ) {
240
            $fields = array(
241
                'post_id'          => $invoice->ID,
242
                'item_id'          => $details['id'],
243
                'item_name'        => $details['name'],
244
                'item_description' => empty( $details['meta']['description'] ) ? '' : $details['meta']['description'],
245
                'vat_rate'         => $details['vat_rate'],
246
                'vat_class'        => empty( $details['vat_class'] ) ? '_standard' : $details['vat_class'],
247
                'tax'              => $details['tax'],
248
                'item_price'       => $details['item_price'],
249
                'custom_price'     => $details['custom_price'],
250
                'quantity'         => $details['quantity'],
251
                'discount'         => $details['discount'],
252
                'subtotal'         => $details['subtotal'],
253
                'price'            => $details['price'],
254
                'meta'             => $details['meta'],
255
                'fees'             => $details['fees'],
256
            );
257
258
            $item_columns = array_keys ( $fields );
259
260
            foreach ( $fields as $key => $val ) {
261
                if ( is_null( $val ) ) {
262
                    $val = '';
263
                }
264
                $val = maybe_serialize( $val );
265
                $fields[ $key ] = $wpdb->prepare( '%s', $val );
266
            }
267
268
            $fields = implode( ', ', $fields );
269
            $item_rows[] = "($fields)";
270
        }
271
272
        $item_rows    = implode( ', ', $item_rows );
273
        $item_columns = implode( ', ', $item_columns );
274
        $wpdb->query( "INSERT INTO $invoice_items_table ($item_columns) VALUES $item_rows" );
275
    }
276
277
    $invoice_rows = implode( ', ', $invoice_rows );
278
    $wpdb->query( "INSERT INTO $invoices_table VALUES $invoice_rows" );
279
280
}
281