forked from awesomemotive/EDD-Recount-Earnings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
edd-recount-earnings.php
201 lines (155 loc) · 5.01 KB
/
edd-recount-earnings.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
* Plugin Name: Easy Digital Downloads - Recount Earnings
* Description: Allows you to recalculate the earnings of products in EDD. Useful if product earnings get off somehow
* Author: Pippin Williamson
* Version: 1.0.2
*/
class EDD_Recount_Earnings {
/**
* Class constructor
*/
public function __construct() {
add_action( 'edd_stats_meta_box', array( $this, 'stats_box_link' ) );
add_filter( 'edd_tools_tabs', array( $this, 'add_tab' ) );
add_action( 'edd_tools_tab_recount_earnings', array( $this, 'tools_page' ) );
add_action( 'edd_recount_earnings', array( $this, 'recount' ) );
add_action( 'edd_recount_store_earnings', array( $this, 'recount_store_earnings' ) );
}
/**
* Adds a recount earnings links to the download edit pages stats box
*/
public function stats_box_link() {
global $post;
$args = array(
'post' => $post->ID,
'action' => 'edit',
'edd_action' => 'recount_earnings',
);
$base_url = admin_url( 'post.php' );
echo '<tr>';
echo '<td colspan="2">';
echo '<a href="' . add_query_arg( $args, $base_url ) . '">' . __( 'Recount Earnings', 'edd-recount-earnings' ) . '</a>';
echo '</td>';
echo '</tr>';
}
/**
* Adds the tab heading to the EDD tools page
*/
public function add_tab( $tabs ) {
$tabs['recount_earnings'] = __( 'Recount Earnings', 'edd' );
return $tabs;
}
/**
* Outputs the content of the tab for the EDD tools page
*/
public function tools_page() {
?>
<div class="postbox">
<h3><span><?php _e( 'Recount Store Earnings', 'edd' ); ?></span></h3>
<div class="inside">
<p><?php _e( 'Use this tool to recount your store\'s total earnings in the case they have become incorrect.', 'edd' ); ?></p>
<form method="post" action="<?php echo admin_url( 'edit.php?post_type=download&page=edd-tools&tab=recount_earnings' ); ?>">
<p><input type="hidden" name="edd_action" value="recount_store_earnings" /></p>
<p>
<?php submit_button( __( 'Recount Earnings', 'edd' ), 'secondary', 'submit', false ); ?>
</p>
</form>
</div>
<!-- .inside -->
</div><!-- .postbox -->
<?php
}
/**
* Recounts an individual downloads earnigns and sales
*/
public function recount() {
global $edd_logs, $wpdb;
if ( empty( $_GET['post'] ) ) {
return;
}
if ( ! current_user_can( 'edit_products' ) ) {
wp_die( 'Cheating' );
}
$download_id = absint( $_GET['post'] );
if ( ! get_post( $download_id ) ) {
return;
}
$args = array(
'post_parent' => $download_id,
'log_type' => 'sale',
'nopaging' => true,
'fields' => 'ids',
);
$log_ids = $edd_logs->get_connected_logs( $args, 'sale' );
$earnings = 0;
$total_sales = 0;
if ( $log_ids ) {
$log_ids = implode( ',', $log_ids );
$payment_ids = $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key='_edd_log_payment_id' AND post_id IN ($log_ids)" );
unset( $log_ids );
$payment_ids = implode( ',', $payment_ids );
$payments = $wpdb->get_results( "SELECT ID, post_status FROM $wpdb->posts WHERE ID IN (" . $payment_ids . ")" );
unset( $payment_ids );
foreach ( $payments as $payment ) {
if ( in_array( $payment->post_status, array( 'revoked', 'published', 'edd_subscription' ) ) ) {
continue;
}
$items = edd_get_payment_meta_cart_details( $payment->ID );
foreach ( $items as $item ) {
if ( $item['id'] != $download_id ) {
continue;
}
$total_sales ++;
$earnings += $item['price'];
}
}
}
update_post_meta( $download_id, '_edd_download_sales', $total_sales );
if ( ! empty( $earnings ) ) {
update_post_meta( $download_id, '_edd_download_earnings', $earnings );
}
$args = array(
'action' => 'edit',
'post' => $download_id
);
$base_url = admin_url( 'post.php' );
wp_redirect( add_query_arg( $args, $base_url ) );
exit;
}
/**
* Recount the earnings for the entire store
*
* @todo This should probably do a recount on number of sales for individual downloads too, but that's a lot heavier than what it currently does
*/
public function recount_store_earnings() {
if ( ! current_user_can( 'view_shop_reports' ) ) {
wp_die( 'Cheating' );
}
$total = (float) 0;
$args = apply_filters( 'edd_get_total_earnings_args', array(
'offset' => 0,
'number' => - 1,
'mode' => 'live',
'status' => array( 'publish', 'revoked' ),
'fields' => 'ids'
) );
$payments = edd_get_payments( $args );
if ( $payments ) {
foreach ( $payments as $payment ) {
$total += edd_get_payment_amount( $payment );
}
}
// Cache results for 1 day. This cache is cleared automatically when a payment is made
set_transient( 'edd_earnings_total', $total, 86400 );
if ( $total < 0 ) {
$total = 0; // Don't ever show negative earnings
}
$total = round( $total, 2 );
// Store the total for the first time
update_option( 'edd_earnings_total', $total );
wp_redirect( admin_url( 'edit.php?post_type=download&page=edd-tools&tab=recount_earnings' ) );
exit;
}
}
new EDD_Recount_Earnings;