|
@@ -11,49 +11,49 @@ discard block |
|
|
block discarded – undo |
|
11
|
11
|
* @since 1.0.0 |
|
12
|
12
|
*/ |
|
13
|
13
|
function wpinv_automatic_upgrade() { |
|
14
|
|
- $wpi_version = get_option( 'wpinv_version' ); |
|
|
14
|
+ $wpi_version = get_option('wpinv_version'); |
|
15
|
15
|
|
|
16
|
|
- if ( $wpi_version == WPINV_VERSION ) { |
|
|
16
|
+ if ($wpi_version == WPINV_VERSION) { |
|
17
|
17
|
return; |
|
18
|
18
|
} |
|
19
|
19
|
|
|
20
|
|
- if ( version_compare( $wpi_version, '0.0.5', '<' ) ) { |
|
|
20
|
+ if (version_compare($wpi_version, '0.0.5', '<')) { |
|
21
|
21
|
wpinv_v005_upgrades(); |
|
22
|
22
|
} |
|
23
|
23
|
|
|
24
|
|
- if ( version_compare( $wpi_version, '1.0.3', '<' ) ) { |
|
|
24
|
+ if (version_compare($wpi_version, '1.0.3', '<')) { |
|
25
|
25
|
wpinv_v110_upgrades(); |
|
26
|
26
|
} |
|
27
|
27
|
|
|
28
|
|
- update_option( 'wpinv_version', WPINV_VERSION ); |
|
|
28
|
+ update_option('wpinv_version', WPINV_VERSION); |
|
29
|
29
|
} |
|
30
|
|
-add_action( 'admin_init', 'wpinv_automatic_upgrade' ); |
|
|
30
|
+add_action('admin_init', 'wpinv_automatic_upgrade'); |
|
31
|
31
|
|
|
32
|
32
|
function wpinv_v005_upgrades() { |
|
33
|
33
|
global $wpdb; |
|
34
|
34
|
|
|
35
|
35
|
// Invoices status |
|
36
|
|
- $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' )" ); |
|
37
|
|
- if ( !empty( $results ) ) { |
|
38
|
|
- $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' )" ); |
|
|
36
|
+ $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' )"); |
|
|
37
|
+ if (!empty($results)) { |
|
|
38
|
+ $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' )"); |
|
39
|
39
|
|
|
40
|
40
|
// Clean post cache |
|
41
|
|
- foreach ( $results as $row ) { |
|
42
|
|
- clean_post_cache( $row->ID ); |
|
|
41
|
+ foreach ($results as $row) { |
|
|
42
|
+ clean_post_cache($row->ID); |
|
43
|
43
|
} |
|
44
|
44
|
} |
|
45
|
45
|
|
|
46
|
46
|
// Item meta key changes |
|
47
|
47
|
$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' )"; |
|
48
|
|
- $results = $wpdb->get_results( $query ); |
|
|
48
|
+ $results = $wpdb->get_results($query); |
|
49
|
49
|
|
|
50
|
|
- if ( !empty( $results ) ) { |
|
51
|
|
- $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )" ); |
|
52
|
|
- $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'" ); |
|
53
|
|
- $wpdb->query( "UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'" ); |
|
|
50
|
+ if (!empty($results)) { |
|
|
51
|
+ $wpdb->query("UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_id' WHERE meta_key IN( '_wpinv_item_id', '_wpinv_package_id', '_wpinv_post_id' )"); |
|
|
52
|
+ $wpdb->query("UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_name' WHERE meta_key = '_wpinv_cpt_name'"); |
|
|
53
|
+ $wpdb->query("UPDATE " . $wpdb->postmeta . " SET meta_key = '_wpinv_custom_singular_name' WHERE meta_key = '_wpinv_cpt_singular_name'"); |
|
54
|
54
|
|
|
55
|
|
- foreach ( $results as $row ) { |
|
56
|
|
- clean_post_cache( $row->post_id ); |
|
|
55
|
+ foreach ($results as $row) { |
|
|
56
|
+ clean_post_cache($row->post_id); |
|
57
|
57
|
} |
|
58
|
58
|
} |
|
59
|
59
|
|
|
@@ -74,79 +74,79 @@ discard block |
|
|
block discarded – undo |
|
74
|
74
|
function wpinv_convert_old_subscriptions() { |
|
75
|
75
|
global $wpdb; |
|
76
|
76
|
|
|
77
|
|
- $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 = 'trialling' OR ". $wpdb->postmeta .".meta_value = 'failing') AND ". $wpdb->posts .".post_type = 'wpi_invoice' GROUP BY ". $wpdb->posts .".ID ORDER BY ". $wpdb->posts .".ID ASC"; |
|
|
77
|
+ $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 = 'trialling' OR " . $wpdb->postmeta . ".meta_value = 'failing') AND " . $wpdb->posts . ".post_type = 'wpi_invoice' GROUP BY " . $wpdb->posts . ".ID ORDER BY " . $wpdb->posts . ".ID ASC"; |
|
78
|
78
|
|
|
79
|
|
- $results = $wpdb->get_results( $query ); |
|
|
79
|
+ $results = $wpdb->get_results($query); |
|
80
|
80
|
|
|
81
|
|
- if ( empty( $results ) ) { |
|
|
81
|
+ if (empty($results)) { |
|
82
|
82
|
return; |
|
83
|
83
|
} |
|
84
|
84
|
|
|
85
|
|
- foreach ( $results as $row ) { |
|
86
|
|
- $invoice = new WPInv_Invoice( $row->ID ); |
|
|
85
|
+ foreach ($results as $row) { |
|
|
86
|
+ $invoice = new WPInv_Invoice($row->ID); |
|
87
|
87
|
|
|
88
|
|
- if ( empty( $invoice->ID ) ) { |
|
|
88
|
+ if (empty($invoice->ID)) { |
|
89
|
89
|
continue; |
|
90
|
90
|
} |
|
91
|
91
|
|
|
92
|
|
- if ( $invoice->has_status( 'wpi-renewal' ) ) { |
|
|
92
|
+ if ($invoice->has_status('wpi-renewal')) { |
|
93
|
93
|
continue; |
|
94
|
94
|
} |
|
95
|
95
|
|
|
96
|
|
- $item = $invoice->get_recurring( true ); |
|
|
96
|
+ $item = $invoice->get_recurring(true); |
|
97
|
97
|
|
|
98
|
|
- if ( empty( $item ) ) { |
|
|
98
|
+ if (empty($item)) { |
|
99
|
99
|
continue; |
|
100
|
100
|
} |
|
101
|
101
|
|
|
102
|
102
|
$is_free_trial = $invoice->is_free_trial(); |
|
103
|
|
- $profile_id = get_post_meta( $invoice->ID, '_wpinv_subscr_profile_id', true ); |
|
104
|
|
- $subscription_status = get_post_meta( $invoice->ID, '_wpinv_subscr_status', true ); |
|
|
103
|
+ $profile_id = get_post_meta($invoice->ID, '_wpinv_subscr_profile_id', true); |
|
|
104
|
+ $subscription_status = get_post_meta($invoice->ID, '_wpinv_subscr_status', true); |
|
105
|
105
|
$transaction_id = $invoice->get_transaction_id(); |
|
106
|
106
|
|
|
107
|
107
|
// Last invoice |
|
108
|
|
- $query = "SELECT ID, post_date FROM ". $wpdb->posts ." WHERE post_type = 'wpi_invoice' AND post_parent = '" . $invoice->ID . "' ORDER BY ID DESC LIMIT 1"; |
|
109
|
|
- $last_payment = $wpdb->get_row( $query ); |
|
|
108
|
+ $query = "SELECT ID, post_date FROM " . $wpdb->posts . " WHERE post_type = 'wpi_invoice' AND post_parent = '" . $invoice->ID . "' ORDER BY ID DESC LIMIT 1"; |
|
|
109
|
+ $last_payment = $wpdb->get_row($query); |
|
110
|
110
|
|
|
111
|
|
- if ( !empty( $last_payment ) ) { |
|
112
|
|
- $invoice_date = $last_payment->post_date; |
|
|
111
|
+ if (!empty($last_payment)) { |
|
|
112
|
+ $invoice_date = $last_payment->post_date; |
|
113
|
113
|
|
|
114
|
|
- $meta_profile_id = get_post_meta( $last_payment->ID, '_wpinv_subscr_profile_id', true ); |
|
115
|
|
- $meta_transaction_id = get_post_meta( $last_payment->ID, '_wpinv_transaction_id', true ); |
|
|
114
|
+ $meta_profile_id = get_post_meta($last_payment->ID, '_wpinv_subscr_profile_id', true); |
|
|
115
|
+ $meta_transaction_id = get_post_meta($last_payment->ID, '_wpinv_transaction_id', true); |
|
116
|
116
|
|
|
117
|
|
- if ( !empty( $meta_profile_id ) ) { |
|
118
|
|
- $profile_id = $meta_profile_id; |
|
|
117
|
+ if (!empty($meta_profile_id)) { |
|
|
118
|
+ $profile_id = $meta_profile_id; |
|
119
|
119
|
} |
|
120
|
120
|
|
|
121
|
|
- if ( !empty( $meta_transaction_id ) ) { |
|
122
|
|
- $transaction_id = $meta_transaction_id; |
|
|
121
|
+ if (!empty($meta_transaction_id)) { |
|
|
122
|
+ $transaction_id = $meta_transaction_id; |
|
123
|
123
|
} |
|
124
|
124
|
} else { |
|
125
|
|
- $invoice_date = $invoice->get_invoice_date( false ); |
|
|
125
|
+ $invoice_date = $invoice->get_invoice_date(false); |
|
126
|
126
|
} |
|
127
|
127
|
|
|
128
|
|
- $profile_id = empty( $profile_id ) ? $invoice->ID : $profile_id; |
|
129
|
|
- $status = empty( $subscription_status ) ? 'pending' : $subscription_status; |
|
|
128
|
+ $profile_id = empty($profile_id) ? $invoice->ID : $profile_id; |
|
|
129
|
+ $status = empty($subscription_status) ? 'pending' : $subscription_status; |
|
130
|
130
|
|
|
131
|
|
- $period = $item->get_recurring_period( true ); |
|
|
131
|
+ $period = $item->get_recurring_period(true); |
|
132
|
132
|
$interval = $item->get_recurring_interval(); |
|
133
|
133
|
$bill_times = (int)$item->get_recurring_limit(); |
|
134
|
134
|
$add_period = $interval . ' ' . $period; |
|
135
|
135
|
$trial_period = ''; |
|
136
|
136
|
|
|
137
|
|
- if ( $invoice->is_free_trial() ) { |
|
138
|
|
- $trial_period = $item->get_trial_period( true ); |
|
|
137
|
+ if ($invoice->is_free_trial()) { |
|
|
138
|
+ $trial_period = $item->get_trial_period(true); |
|
139
|
139
|
$free_interval = $item->get_trial_interval(); |
|
140
|
140
|
$trial_period = $free_interval . ' ' . $trial_period; |
|
141
|
141
|
|
|
142
|
|
- if ( empty( $last_payment ) ) { |
|
|
142
|
+ if (empty($last_payment)) { |
|
143
|
143
|
$add_period = $trial_period; |
|
144
|
144
|
} |
|
145
|
145
|
} |
|
146
|
146
|
|
|
147
|
|
- $expiration = date_i18n( 'Y-m-d H:i:s', strtotime( '+' . $add_period . ' 23:59:59', strtotime( $invoice_date ) ) ); |
|
148
|
|
- if ( strtotime( $expiration ) < strtotime( date_i18n( 'Y-m-d' ) ) ) { |
|
149
|
|
- if ( $status == 'active' || $status == 'trialling' || $status == 'pending' ) { |
|
|
147
|
+ $expiration = date_i18n('Y-m-d H:i:s', strtotime('+' . $add_period . ' 23:59:59', strtotime($invoice_date))); |
|
|
148
|
+ if (strtotime($expiration) < strtotime(date_i18n('Y-m-d'))) { |
|
|
149
|
+ if ($status == 'active' || $status == 'trialling' || $status == 'pending') { |
|
150
|
150
|
$status = 'expired'; |
|
151
|
151
|
} |
|
152
|
152
|
} |
|
@@ -159,7 +159,7 @@ discard block |
|
|
block discarded – undo |
|
159
|
159
|
'frequency' => $interval, |
|
160
|
160
|
'period' => $period, |
|
161
|
161
|
'initial_amount' => $invoice->get_total(), |
|
162
|
|
- 'recurring_amount' => $invoice->get_recurring_details( 'total' ), |
|
|
162
|
+ 'recurring_amount' => $invoice->get_recurring_details('total'), |
|
163
|
163
|
'bill_times' => $bill_times, |
|
164
|
164
|
'created' => $invoice_date, |
|
165
|
165
|
'expiration' => $expiration, |
|
@@ -169,14 +169,14 @@ discard block |
|
|
block discarded – undo |
|
169
|
169
|
); |
|
170
|
170
|
|
|
171
|
171
|
$subs_db = new WPInv_Subscriptions_DB; |
|
172
|
|
- $subs = $subs_db->get_subscriptions( array( 'parent_payment_id' => $invoice->ID, 'number' => 1 ) ); |
|
173
|
|
- $subscription = reset( $subs ); |
|
|
172
|
+ $subs = $subs_db->get_subscriptions(array('parent_payment_id' => $invoice->ID, 'number' => 1)); |
|
|
173
|
+ $subscription = reset($subs); |
|
174
|
174
|
|
|
175
|
|
- if ( empty( $subscription ) || $subscription->id <= 0 ) { |
|
|
175
|
+ if (empty($subscription) || $subscription->id <= 0) { |
|
176
|
176
|
$subscription = new WPInv_Subscription(); |
|
177
|
|
- $new_sub = $subscription->create( $args ); |
|
|
177
|
+ $new_sub = $subscription->create($args); |
|
178
|
178
|
|
|
179
|
|
- if ( !empty( $bill_times ) && $new_sub->get_times_billed() >= $bill_times && ( 'active' == $new_sub->status || 'trialling' == $new_sub->status ) ) { |
|
|
179
|
+ if (!empty($bill_times) && $new_sub->get_times_billed() >= $bill_times && ('active' == $new_sub->status || 'trialling' == $new_sub->status)) { |
|
180
|
180
|
$new_sub->complete(); // Mark completed if all times billed |
|
181
|
181
|
} |
|
182
|
182
|
} |
|
@@ -186,36 +186,36 @@ discard block |
|
|
block discarded – undo |
|
186
|
186
|
function wpinv_update_new_email_settings() { |
|
187
|
187
|
global $wpinv_options; |
|
188
|
188
|
|
|
189
|
|
- $current_options = get_option( 'wpinv_settings', array() ); |
|
|
189
|
+ $current_options = get_option('wpinv_settings', array()); |
|
190
|
190
|
$options = array(); |
|
191
|
191
|
$registered_settings = wpinv_get_registered_settings(); |
|
192
|
192
|
$email_settings = $registered_settings['emails']; |
|
193
|
193
|
|
|
194
|
194
|
// Populate some default values |
|
195
|
|
- foreach( $email_settings as $tab => $sections ) { |
|
196
|
|
- foreach( $sections as $section => $settings) { |
|
|
195
|
+ foreach ($email_settings as $tab => $sections) { |
|
|
196
|
+ foreach ($sections as $section => $settings) { |
|
197
|
197
|
// Check for backwards compatibility |
|
198
|
|
- $tab_sections = wpinv_get_settings_tab_sections( $tab ); |
|
199
|
|
- if( ! is_array( $tab_sections ) || ! array_key_exists( $section, $tab_sections ) ) { |
|
|
198
|
+ $tab_sections = wpinv_get_settings_tab_sections($tab); |
|
|
199
|
+ if (!is_array($tab_sections) || !array_key_exists($section, $tab_sections)) { |
|
200
|
200
|
$section = 'main'; |
|
201
|
201
|
$settings = $sections; |
|
202
|
202
|
} |
|
203
|
203
|
|
|
204
|
|
- foreach ( $settings as $option ) { |
|
205
|
|
- if ( !empty( $option['id'] ) && !isset( $wpinv_options[ $option['id'] ] ) ) { |
|
206
|
|
- if ( 'checkbox' == $option['type'] && !empty( $option['std'] ) ) { |
|
207
|
|
- $options[ $option['id'] ] = '1'; |
|
208
|
|
- } else if ( !empty( $option['std'] ) ) { |
|
209
|
|
- $options[ $option['id'] ] = $option['std']; |
|
|
204
|
+ foreach ($settings as $option) { |
|
|
205
|
+ if (!empty($option['id']) && !isset($wpinv_options[$option['id']])) { |
|
|
206
|
+ if ('checkbox' == $option['type'] && !empty($option['std'])) { |
|
|
207
|
+ $options[$option['id']] = '1'; |
|
|
208
|
+ } else if (!empty($option['std'])) { |
|
|
209
|
+ $options[$option['id']] = $option['std']; |
|
210
|
210
|
} |
|
211
|
211
|
} |
|
212
|
212
|
} |
|
213
|
213
|
} |
|
214
|
214
|
} |
|
215
|
215
|
|
|
216
|
|
- $merged_options_current = array_merge( $wpinv_options, $options ); |
|
217
|
|
- $merged_options = array_merge( $merged_options_current, $current_options ); |
|
|
216
|
+ $merged_options_current = array_merge($wpinv_options, $options); |
|
|
217
|
+ $merged_options = array_merge($merged_options_current, $current_options); |
|
218
|
218
|
$wpinv_options = $merged_options; |
|
219
|
219
|
|
|
220
|
|
- update_option( 'wpinv_settings', $merged_options ); |
|
|
220
|
+ update_option('wpinv_settings', $merged_options); |
|
221
|
221
|
} |
|
222
|
222
|
\ No newline at end of file |