1 <?php
2 3 4 5 6 7 8 9 10
11
12
13 if ( !defined( 'ABSPATH' ) ) exit;
14
15 16 17 18 19 20 21 22 23 24 25 26
27 function edd_complete_purchase( $payment_id, $new_status, $old_status ) {
28 if ( $old_status == 'publish' || $old_status == 'complete' )
29 return;
30
31
32 if ( $new_status != 'publish' && $new_status != 'complete' )
33 return;
34
35 if ( edd_is_test_mode() && ! apply_filters( 'edd_log_test_payment_stats', false ) )
36 return;
37
38 $payment_data = edd_get_payment_meta( $payment_id );
39 $downloads = maybe_unserialize( $payment_data['downloads'] );
40 $user_info = maybe_unserialize( $payment_data['user_info'] );
41 $cart_details = maybe_unserialize( $payment_data['cart_details'] );
42
43 if ( is_array( $downloads ) ) {
44
45 foreach ( $downloads as $download ) {
46 edd_record_sale_in_log( $download['id'], $payment_id, $user_info );
47 edd_increase_purchase_count( $download['id'] );
48 $amount = null;
49
50 if ( is_array( $cart_details ) ) {
51 foreach ( $cart_details as $key => $item ) {
52 if ( array_search( $download['id'], $item ) ) {
53 $cart_item_id = $key;
54 }
55 }
56
57 $amount = isset( $cart_details[$cart_item_id]['price'] ) ? $cart_details[$cart_item_id]['price'] : null;
58 }
59
60 $amount = edd_get_download_final_price( $download['id'], $user_info, $amount );
61 edd_increase_earnings( $download['id'], $amount );
62 }
63
64
65 delete_transient( 'edd_earnings_total' );
66 }
67
68 if ( isset( $user_info['discount'] ) && $user_info['discount'] != 'none' ) {
69 edd_increase_discount_usage( $user_info['discount'] );
70 }
71
72
73 edd_empty_cart();
74 }
75 add_action( 'edd_update_payment_status', 'edd_complete_purchase', 100, 3 );
76
77 78 79 80 81 82 83 84 85
86 function edd_record_status_change( $payment_id, $new_status, $old_status ) {
87 if ( $new_status == 'publish' )
88 $new_status = 'complete';
89
90 if ( $old_status == 'publish' )
91 $old_status = 'complete';
92
93 $status_change = sprintf( __( 'Status changed from %s to %s', 'edd' ), $old_status, $new_status );
94
95 edd_insert_payment_note( $payment_id, $status_change );
96 }
97 add_action( 'edd_update_payment_status', 'edd_record_status_change', 100, 3 );
98
99 100 101 102 103 104 105 106 107 108
109 function edd_update_edited_purchase( $data ) {
110 if ( wp_verify_nonce( $data['edd-payment-nonce'], 'edd_payment_nonce' ) ) {
111 $payment_id = $_POST['payment-id'];
112
113 $payment_data = edd_get_payment_meta( $payment_id );
114
115 if ( isset( $_POST['edd-purchased-downloads'] ) ) {
116 $download_list = array();
117
118 foreach ( $_POST['edd-purchased-downloads'] as $key => $download ) {
119 if ( isset ( $download['options']['price_id'] ) ) {
120 $download_list[] = array(
121 'id' => $key,
122 'options' => array(
123 'price_id' => $download['options']['price_id']
124 )
125 );
126 } else {
127 $download_list[] = array( 'id' => $download );
128 }
129 }
130
131 $payment_data['downloads'] = serialize( $download_list );
132 }
133
134 $user_info = maybe_unserialize( $payment_data['user_info'] );
135 $user_info['email'] = strip_tags( $_POST['edd-buyer-email'] );
136 $user_info['user_id'] = strip_tags( intval( $_POST['edd-buyer-user-id'] ) );
137 $payment_data['user_info'] = serialize( $user_info );
138
139 update_post_meta( $payment_id, '_edd_payment_meta', $payment_data );
140 update_post_meta( $payment_id, '_edd_payment_user_email', strip_tags( $_POST['edd-buyer-email'] ) );
141 update_post_meta( $payment_id, '_edd_payment_user_id', strip_tags( intval( $_POST['edd-buyer-user-id'] ) ) );
142
143 if ( ! empty( $_POST['edd-payment-note'] ) ) {
144 $note = wp_kses( $_POST['edd-payment-note'], array() );
145 $note_id = edd_insert_payment_note( $payment_id, $note );
146 }
147
148 if ( $_POST['edd-old-status'] != $_POST['edd-payment-status'] ) {
149 edd_update_payment_status( $payment_id, $_POST['edd-payment-status'] );
150 }
151
152 if ( $_POST['edd-payment-status'] == 'publish' && isset( $_POST['edd-payment-send-email'] ) ) {
153
154 edd_email_purchase_receipt( $payment_id, false );
155 }
156
157 do_action( 'edd_update_edited_purchase', $payment_id );
158 }
159 }
160 add_action( 'edd_edit_payment', 'edd_update_edited_purchase' );
161
162 163 164 165 166 167 168
169 function edd_trigger_purchase_delete( $data ) {
170 if ( wp_verify_nonce( $data['_wpnonce'], 'edd_payment_nonce' ) ) {
171 $payment_id = absint( $data['purchase_id'] );
172 edd_delete_purchase( $payment_id );
173 wp_redirect( admin_url( '/edit.php?post_type=download&page=edd-payment-history&edd-message=payment_deleted' ) );
174 exit;
175 }
176 }
177 add_action( 'edd_delete_payment', 'edd_trigger_purchase_delete' );
178
179 180 181 182 183 184 185 186
187 function edd_clear_earnings_cache( $payment, $payment_data ) {
188 delete_transient( 'edd_total_earnings' );
189 }
190 add_action( 'edd_insert_payment', 'edd_clear_earnings_cache', 10, 2 );
191
192 193 194 195 196 197 198 199 200 201
202 function edd_clear_user_history_cache( $payment_id, $new_status, $old_status ) {
203 $user_info = edd_get_payment_meta_user_info( $payment_id );
204
205 delete_transient( 'edd_user_' . $user_info['id'] . '_purchases' );
206 delete_transient( md5( 'edd_customer_total_' . $user_info['email'] ) );
207 }
208 add_action( 'edd_update_payment_status', 'edd_clear_user_history_cache', 10, 3 );
209
210 211 212 213 214 215 216 217 218 219
220 function edd_update_old_payments_with_totals( $data ) {
221 if ( ! wp_verify_nonce( $data['_wpnonce'], 'edd_upgrade_payments_nonce' ) )
222 return;
223
224 if ( get_option( 'edd_payment_totals_upgraded' ) )
225 return;
226
227 $payments = edd_get_payments( array(
228 'offset' => 0,
229 'number' => -1,
230 'mode' => 'all'
231 ) );
232
233 if ( $payments ) {
234 foreach ( $payments as $payment ) {
235 $meta = edd_get_payment_meta( $payment->ID );
236 update_post_meta( $payment->ID, '_edd_payment_total', $meta['amount'] );
237 }
238 }
239
240 add_option( 'edd_payment_totals_upgraded', 1 );
241 }
242 add_action( 'edd_upgrade_payments', 'edd_update_old_payments_with_totals' );