Passed
Pull Request — master (#116)
by
unknown
03:57
created

wpinv-upgrade-functions.php ➔ convert_old_subscriptions()   C

Complexity

Conditions 14
Paths 32

Size

Total Lines 70
Code Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 48
nc 32
nop 0
dl 0
loc 70
rs 5.6188
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
    if ( $wpi_version == WPINV_VERSION ) {
17
        return;
18
    }
19
    
20
    if ( version_compare( $wpi_version, '0.0.5', '<' ) ) {
21
        wpinv_v005_upgrades();
22
    }
23
    
24
    update_option( 'wpinv_version', WPINV_VERSION );
25
}
26
add_action( 'admin_init', 'wpinv_automatic_upgrade' );
27
28
function wpinv_v005_upgrades() {
29
    global $wpdb;
30
    
31
    // Invoices status
32
    $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' )" );
33
    if ( !empty( $results ) ) {
34
        $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' )" );
35
        
36
        // Clean post cache
37
        foreach ( $results as $row ) {
38
            clean_post_cache( $row->ID );
39
        }
40
    }
41
    
42
    // Item meta key changes
43
    $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' )";
44
    $results = $wpdb->get_results( $query );
45
    
46
    if ( !empty( $results ) ) {
47
        $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" );
48
        $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" );
49
        $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" );
50
        
51
        foreach ( $results as $row ) {
52
            clean_post_cache( $row->post_id );
53
        }
54
    }
55
56
    wpinv_add_admin_caps();
57
}
58
59
if ( get_option('wpinv_db_version') != WPINV_VERSION ) {
60
    add_action('plugins_loaded', 'wpinv_upgrade_all', 10);
61
}
62
63
function wpinv_upgrade_all(){
64
65
        // Add Subscription tables
66
        $db = new WPInv_Subscriptions_DB;
67
        @$db->create_table();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
68
69
        convert_old_subscriptions();
70
}
71
72
function convert_old_subscriptions(){
73
74
    global $wpdb;
75
76
    $query = "SELECT ". $wpdb->posts .".ID FROM ". $wpdb->posts ." INNER JOIN ". $wpdb->postmeta ." ON ( ". $wpdb->posts .".ID = ". $wpdb->postmeta .".post_id ) WHERE 1=1  AND ". $wpdb->postmeta .".meta_key = '_wpinv_subscr_status' AND (". $wpdb->postmeta .".meta_value = 'pending' OR ". $wpdb->postmeta .".meta_value = 'active' OR ". $wpdb->postmeta .".meta_value = 'cancelled' OR ". $wpdb->postmeta .".meta_value = 'completed' OR ". $wpdb->postmeta .".meta_value = 'expired' OR ". $wpdb->postmeta .".meta_value = 'trialing' OR ". $wpdb->postmeta .".meta_value = 'failing') AND ". $wpdb->posts .".post_type = 'wpi_invoice' GROUP BY ". $wpdb->posts .".ID ORDER BY ". $wpdb->posts .".post_date ASC";
77
78
    $results = $wpdb->get_results($query);
79
80
    foreach ( $results as $row ) {
81
82
        $invoice = new WPInv_Invoice($row->ID);
83
84
        $item_id = $invoice->get_meta( '_wpinv_item_ids', true );
85
        $item = new WPInv_Item( $item_id );
86
87
        if($invoice->has_status('wpi-renewal') || !$item->is_recurring()){
88
            continue;
89
        }
90
91
        $period             = $item->get_recurring_period(true);
92
        $interval           = $item->get_recurring_interval();
93
        $bill_times         = (int)$item->get_recurring_limit();
94
        $initial_amount     = wpinv_sanitize_amount( $invoice->get_total(), 2 );
95
        $recurring_amount   = wpinv_sanitize_amount( $invoice->get_recurring_details( 'total' ), 2 );
96
        $subscription_status = $invoice->get_meta( '_wpinv_subscr_status', true );
97
        $status             = empty($subscription_status) ? 'pending' : $subscription_status;
98
        $expiration         = date( 'Y-m-d H:i:s', strtotime( '+' . $interval . ' ' . $period  . ' 23:59:59', strtotime($invoice->date) ) );
99
100
        $trial_period = '';
101
        if ( $invoice->is_free_trial() && $item->has_free_trial() ) {
102
            $trial_period       = $item->get_trial_period(true);
103
            $free_trial         = $item->get_free_trial();
104
            $trial_period       = ! empty( $invoice->is_free_trial() ) ? $free_trial . ' ' . $trial_period : '';
105
            $expiration         = date( 'Y-m-d H:i:s', strtotime( '+' . $trial_period . ' 23:59:59', strtotime($invoice->date) ) );
106
            $status             = "trialing" === $status ? 'trialling' : $status;
107
        }
108
109
        $args = array(
110
            'product_id'        => $item_id,
111
            'customer_id'       => $invoice->user_id,
112
            'parent_payment_id' => $invoice->ID,
113
            'status'            => $status,
114
            'frequency'         => $interval,
115
            'period'            => $period,
116
            'initial_amount'    => $initial_amount,
117
            'recurring_amount'  => $recurring_amount,
118
            'bill_times'        => $bill_times,
119
            'created'           => date( 'Y-m-d H:i:s', strtotime($invoice->date) ),
120
            'expiration'        => $expiration,
121
            'trial_period'      => $trial_period,
122
            'profile_id'        => $invoice->ID,
123
            'transaction_id'    => $invoice->get_transaction_id(),
124
        );
125
126
        $subs_db      = new WPInv_Subscriptions_DB;
127
        $subs         = $subs_db->get_subscriptions( array( 'parent_payment_id' => $invoice->ID, 'number' => 1 ) );
128
        $subscription = reset( $subs );
129
130
        if( !$subscription || $subscription->id <= 0 ) {
131
132
            $subscription = new WPInv_Subscription();
133
            $new_sub = $subscription->create( $args );
134
135
            if($new_sub->get_times_billed() >= $bill_times && 'active' == $new_sub->status || 'trialling' == $new_sub->status){
136
                $new_sub->complete(); // Mark completed if all times billed
137
            }
138
        }
139
140
    }
141
}