|
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
|
|
|
if ( version_compare( $wpi_version, '1.0.3', '<' ) ) { |
|
25
|
|
|
wpinv_v110_upgrades(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
update_option( 'wpinv_version', WPINV_VERSION ); |
|
29
|
|
|
} |
|
30
|
|
|
add_action( 'admin_init', 'wpinv_automatic_upgrade' ); |
|
31
|
|
|
|
|
32
|
|
|
function wpinv_v005_upgrades() { |
|
33
|
|
|
global $wpdb; |
|
34
|
|
|
|
|
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' )" ); |
|
39
|
|
|
|
|
40
|
|
|
// Clean post cache |
|
41
|
|
|
foreach ( $results as $row ) { |
|
42
|
|
|
clean_post_cache( $row->ID ); |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
// Item meta key changes |
|
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 ); |
|
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'" ); |
|
54
|
|
|
|
|
55
|
|
|
foreach ( $results as $row ) { |
|
56
|
|
|
clean_post_cache( $row->post_id ); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
wpinv_add_admin_caps(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
function wpinv_v110_upgrades() { |
|
64
|
|
|
// Upgrade email settings |
|
65
|
|
|
wpinv_update_new_email_settings(); |
|
66
|
|
|
|
|
67
|
|
|
// Add Subscription tables |
|
68
|
|
|
$db = new WPInv_Subscriptions_DB; |
|
69
|
|
|
@$db->create_table(); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
wpinv_convert_old_subscriptions(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
function wpinv_convert_old_subscriptions() { |
|
75
|
|
|
global $wpdb; |
|
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"; |
|
78
|
|
|
|
|
79
|
|
|
$results = $wpdb->get_results( $query ); |
|
80
|
|
|
|
|
81
|
|
|
if ( empty( $results ) ) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
foreach ( $results as $row ) { |
|
86
|
|
|
$invoice = new WPInv_Invoice( $row->ID ); |
|
87
|
|
|
|
|
88
|
|
|
if ( empty( $invoice->ID ) ) { |
|
89
|
|
|
continue; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if ( $invoice->has_status( 'wpi-renewal' ) ) { |
|
93
|
|
|
continue; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
$item = $invoice->get_recurring( true ); |
|
97
|
|
|
|
|
98
|
|
|
if ( empty( $item ) ) { |
|
99
|
|
|
continue; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
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 ); |
|
105
|
|
|
$transaction_id = $invoice->get_transaction_id(); |
|
106
|
|
|
|
|
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 ); |
|
110
|
|
|
|
|
111
|
|
|
if ( !empty( $last_payment ) ) { |
|
112
|
|
|
$invoice_date = $last_payment->post_date; |
|
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 ); |
|
116
|
|
|
|
|
117
|
|
|
if ( !empty( $meta_profile_id ) ) { |
|
118
|
|
|
$profile_id = $meta_profile_id; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
if ( !empty( $meta_transaction_id ) ) { |
|
122
|
|
|
$transaction_id = $meta_transaction_id; |
|
123
|
|
|
} |
|
124
|
|
|
} else { |
|
125
|
|
|
$invoice_date = $invoice->get_invoice_date( false ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
$profile_id = empty( $profile_id ) ? $invoice->ID : $profile_id; |
|
129
|
|
|
$status = empty( $subscription_status ) ? 'pending' : $subscription_status; |
|
130
|
|
|
|
|
131
|
|
|
$period = $item->get_recurring_period( true ); |
|
132
|
|
|
$interval = $item->get_recurring_interval(); |
|
133
|
|
|
$bill_times = (int)$item->get_recurring_limit(); |
|
134
|
|
|
$add_period = $interval . ' ' . $period; |
|
135
|
|
|
$trial_period = ''; |
|
136
|
|
|
|
|
137
|
|
View Code Duplication |
if ( $invoice->is_free_trial() ) { |
|
138
|
|
|
$trial_period = $item->get_trial_period( true ); |
|
139
|
|
|
$free_interval = $item->get_trial_interval(); |
|
140
|
|
|
$trial_period = $free_interval . ' ' . $trial_period; |
|
141
|
|
|
|
|
142
|
|
|
if ( empty( $last_payment ) ) { |
|
143
|
|
|
$add_period = $trial_period; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
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' ) { |
|
150
|
|
|
$status = 'expired'; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
$args = array( |
|
155
|
|
|
'product_id' => $item->ID, |
|
156
|
|
|
'customer_id' => $invoice->user_id, |
|
157
|
|
|
'parent_payment_id' => $invoice->ID, |
|
158
|
|
|
'status' => $status, |
|
159
|
|
|
'frequency' => $interval, |
|
160
|
|
|
'period' => $period, |
|
161
|
|
|
'initial_amount' => $invoice->get_total(), |
|
162
|
|
|
'recurring_amount' => $invoice->get_recurring_details( 'total' ), |
|
163
|
|
|
'bill_times' => $bill_times, |
|
164
|
|
|
'created' => $invoice_date, |
|
165
|
|
|
'expiration' => $expiration, |
|
166
|
|
|
'trial_period' => $trial_period, |
|
167
|
|
|
'profile_id' => $profile_id, |
|
168
|
|
|
'transaction_id' => $transaction_id, |
|
169
|
|
|
); |
|
170
|
|
|
|
|
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 ); |
|
174
|
|
|
|
|
175
|
|
|
if ( empty( $subscription ) || $subscription->id <= 0 ) { |
|
176
|
|
|
$subscription = new WPInv_Subscription(); |
|
177
|
|
|
$new_sub = $subscription->create( $args ); |
|
178
|
|
|
|
|
179
|
|
|
if ( !empty( $bill_times ) && $new_sub->get_times_billed() >= $bill_times && ( 'active' == $new_sub->status || 'trialling' == $new_sub->status ) ) { |
|
180
|
|
|
$new_sub->complete(); // Mark completed if all times billed |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
function wpinv_update_new_email_settings() { |
|
187
|
|
|
global $wpinv_options; |
|
188
|
|
|
|
|
189
|
|
|
$current_options = get_option( 'wpinv_settings', array() ); |
|
190
|
|
|
$options = array( |
|
191
|
|
|
'email_new_invoice_body' => __( '<p>Hi Admin,</p><p>You have received payment invoice from {name}.</p>', 'invoicing' ), |
|
192
|
|
|
'email_cancelled_invoice_body' => __( '<p>Hi Admin,</p><p>The invoice #{invoice_number} from {site_title} has been cancelled.</p>', 'invoicing' ), |
|
193
|
|
|
'email_failed_invoice_body' => __( '<p>Hi Admin,</p><p>Payment for invoice #{invoice_number} from {site_title} has been failed.</p>', 'invoicing' ), |
|
194
|
|
|
'email_onhold_invoice_body' => __( '<p>Hi {name},</p><p>Your invoice is on-hold until we confirm your payment has been received.</p>', 'invoicing' ), |
|
195
|
|
|
'email_processing_invoice_body' => __( '<p>Hi {name},</p><p>Your invoice has been received at {site_title} and is now being processed.</p>', 'invoicing' ), |
|
196
|
|
|
'email_refunded_invoice_body' => __( '<p>Hi {name},</p><p>Your invoice on {site_title} has been refunded.</p>', 'invoicing' ), |
|
197
|
|
|
'email_user_invoice_body' => __( '<p>Hi {name},</p><p>An invoice has been created for you on {site_title}. To view / pay for this invoice please use the following link: <a class="btn btn-success" href="{invoice_link}">View / Pay</a></p>', 'invoicing' ), |
|
198
|
|
|
'email_user_note_body' => __( '<p>Hi {name},</p><p>Following note has been added to your {invoice_label}:</p><blockquote class="wpinv-note">{customer_note}</blockquote>', 'invoicing' ), |
|
199
|
|
|
'email_overdue_body' => __( '<p>Hi {full_name},</p><p>This is just a friendly reminder that your invoice <a href="{invoice_link}">#{invoice_number}</a> {is_was} due on {invoice_due_date}.</p><p>The total of this invoice is {invoice_total}</p><p>To view / pay now for this invoice please use the following link: <a class="btn btn-success" href="{invoice_link}">View / Pay</a></p>', 'invoicing' ), |
|
200
|
|
|
); |
|
201
|
|
|
|
|
202
|
|
|
foreach ($options as $option => $value){ |
|
203
|
|
|
if (!isset($current_options[$option])) { |
|
204
|
|
|
$current_options[$option] = $value; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
$wpinv_options = $current_options; |
|
209
|
|
|
|
|
210
|
|
|
update_option( 'wpinv_settings', $current_options ); |
|
211
|
|
|
} |
If you suppress an error, we recommend checking for the error condition explicitly: