PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.1.2
GiveWP – Donation Plugin and Fundraising Platform v2.1.2
4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 2.30.0 2.31.0 2.31.1 2.32.0 2.33.0 2.33.1 2.33.2 2.33.3 2.33.4 2.33.5 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.4.5 2.4.6 2.4.7 2.5.0 2.5.1 2.5.10 2.5.11 2.5.12 2.5.13 2.5.2 2.5.3 2.5.4 2.5.5 2.5.6 2.5.7 2.5.8 2.5.9 2.6.0 2.6.1 2.6.2 2.6.3 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.9.0 2.9.1 2.9.2 2.9.3 2.9.4 2.9.5 2.9.6 2.9.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.10.0 3.11.0 3.12.0 3.12.1 3.12.2 3.12.3 3.13.0 3.14.0 3.14.1 3.14.2 3.15.0 3.15.1 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.17.0 3.17.1 3.17.2 3.18.0 3.19.0 3.19.1 3.19.2 3.19.3 3.19.4 3.2.0 3.2.1 3.2.2 3.20.0 3.21.0 3.21.1 3.22.0 3.22.1 3.22.2 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.6.0 3.6.1 3.6.2 3.7.0 3.8.0 3.9.0 4.0.0 4.1.0 4.1.1 4.10.0 4.10.1 4.11.0 4.12.0 4.13.0 4.13.1 4.13.2 4.14.0 4.14.1 4.14.2 4.14.3 4.14.4 4.14.5 4.14.6 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.1 4.7.0 4.7.1 4.8.0 4.8.1 4.9.0 trunk 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.10.0 2.10.1 2.10.2 2.10.3 2.10.4 2.11.0 2.11.1 2.11.2 2.11.3 2.12.0 2.12.1 2.12.2 2.12.3 2.13.0 2.13.1 2.13.2 2.13.3 2.13.4 2.14.0 2.15.0 2.16.0 2.16.1 2.17.0 2.17.1 2.17.3 2.18.0 2.18.1 2.19.1 2.19.2 2.19.3 2.19.4 2.19.5 2.19.6 2.19.7 2.19.8 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.20.0 2.20.1 2.20.2 2.21.0 2.21.1 2.21.2 2.21.3 2.21.4 2.22.0 2.22.1 2.22.2 2.22.3 2.23.0 2.23.1 2.23.2 2.24.0 2.24.1 2.24.2 2.25.0 2.25.1 2.25.2 2.25.3 2.26.0 2.27.0 2.27.1 2.27.2 2.27.3 2.28.0 2.29.0 2.29.1 2.29.2
give / includes / payments / class-payment-stats.php
give / includes / payments Last commit date
actions.php 8 years ago backward-compatibility.php 8 years ago class-give-payment.php 8 years ago class-give-sequential-donation-number.php 8 years ago class-payment-stats.php 8 years ago class-payments-query.php 8 years ago functions.php 8 years ago
class-payment-stats.php
290 lines
1 <?php
2 /**
3 * Earnings / Sales Stats
4 *
5 * @package Give
6 * @subpackage Classes/Stats
7 * @copyright Copyright (c) 2016, WordImpress
8 * @license https://opensource.org/licenses/gpl-license GNU Public License
9 * @since 1.0
10 */
11
12 // Exit if accessed directly.
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17 /**
18 * Give_Stats Class
19 *
20 * This class is for retrieving stats for earnings and sales.
21 *
22 * Stats can be retrieved for date ranges and pre-defined periods.
23 *
24 * @since 1.0
25 */
26 class Give_Payment_Stats extends Give_Stats {
27
28 /**
29 * Retrieve sale stats
30 *
31 * @since 1.0
32 * @access public
33 *
34 * @param $form_id int The donation form to retrieve stats for. If false, gets stats for all forms
35 * @param $start_date string|bool The starting date for which we'd like to filter our sale stats. If false, we'll use the default start date of `this_month`
36 * @param $end_date string|bool The end date for which we'd like to filter our sale stats. If false, we'll use the default end date of `this_month`
37 * @param $status string|array The sale status(es) to count. Only valid when retrieving global stats
38 *
39 * @return float|int Total amount of donations based on the passed arguments.
40 */
41 public function get_sales( $form_id = 0, $start_date = false, $end_date = false, $status = 'publish' ) {
42
43 $this->setup_dates( $start_date, $end_date );
44
45 // Make sure start date is valid
46 if ( is_wp_error( $this->start_date ) ) {
47 return $this->start_date;
48 }
49
50 // Make sure end date is valid
51 if ( is_wp_error( $this->end_date ) ) {
52 return $this->end_date;
53 }
54
55 $args = array(
56 'status' => 'publish',
57 'start_date' => $this->start_date,
58 'end_date' => $this->end_date,
59 'fields' => 'ids',
60 'number' => - 1,
61 'output' => ''
62 );
63
64 if ( ! empty( $form_id ) ) {
65 $args['give_forms'] = $form_id;
66 }
67
68 /* @var Give_Payments_Query $payments */
69 $payments = new Give_Payments_Query( $args );
70 $payments = $payments->get_payments();
71
72 return count( $payments );
73 }
74
75
76 /**
77 * Retrieve earning stats
78 *
79 * @since 1.0
80 * @access public
81 *
82 * @param $form_id int The donation form to retrieve stats for. If false, gets stats for all forms.
83 * @param $start_date string|bool The starting date for which we'd like to filter our donation earnings stats. If false, method will use the default start date of `this_month`.
84 * @param $end_date string|bool The end date for which we'd like to filter the donations stats. If false, method will use the default end date of `this_month`.
85 * @param $gateway_id string|bool The gateway to get earnings for such as 'paypal' or 'stripe'.
86 *
87 * @return float|int Total amount of donations based on the passed arguments.
88 */
89 public function get_earnings( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) {
90 global $wpdb;
91 $this->setup_dates( $start_date, $end_date );
92
93 // Make sure start date is valid
94 if ( is_wp_error( $this->start_date ) ) {
95 return $this->start_date;
96 }
97
98 // Make sure end date is valid
99 if ( is_wp_error( $this->end_date ) ) {
100 return $this->end_date;
101 }
102
103 $args = array(
104 'status' => 'publish',
105 'give_forms' => $form_id,
106 'start_date' => $this->start_date,
107 'end_date' => $this->end_date,
108 'fields' => 'ids',
109 'number' => - 1,
110 'output' => '',
111 );
112
113
114 // Filter by Gateway ID meta_key
115 if ( $gateway_id ) {
116 $args['meta_query'][] = array(
117 'key' => '_give_payment_gateway',
118 'value' => $gateway_id,
119 );
120 }
121
122 // Filter by Gateway ID meta_key
123 if ( $form_id ) {
124 $args['meta_query'][] = array(
125 'key' => '_give_payment_form_id',
126 'value' => $form_id,
127 );
128 }
129
130 if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) {
131 $args['meta_query']['relation'] = 'AND';
132 }
133
134 $args = apply_filters( 'give_stats_earnings_args', $args );
135 $key = Give_Cache::get_key( 'give_stats', $args );
136
137 // Set transient for faster stats.
138 $earnings = Give_Cache::get( $key );
139
140 if ( false === $earnings ) {
141
142 $this->timestamp = false;
143 $payments = new Give_Payments_Query( $args );
144 $payments = $payments->get_payments();
145 $earnings = 0;
146
147 if ( ! empty( $payments ) ) {
148 $query = "SELECT payment_id as id, meta_value as total
149 FROM {$wpdb->paymentmeta}
150 WHERE meta_key='_give_payment_total'
151 AND payment_id IN ('". implode( '\',\'', $payments ) ."')";
152
153 $payments = $wpdb->get_results($query, ARRAY_A);
154
155 if( ! empty( $payments ) ) {
156 foreach ( $payments as $payment ) {
157 /**
158 * Filter the donation amount
159 * Note: this filter documented in payments/functions.php:give_donation_amount()
160 *
161 * @since 2.1
162 */
163 $formatted_amount = apply_filters(
164 'give_donation_amount',
165 give_format_amount( $payment['total'], array( 'donation_id' => $payment['id'] ) ),
166 $payment['total'],
167 $payment['id'],
168 array( 'type' => 'stats', 'currency'=> false, 'amount' => false )
169 );
170
171 $earnings += (float) give_maybe_sanitize_amount( $formatted_amount );
172 }
173 }
174
175 }
176
177 // Cache the results for one hour.
178 Give_Cache::set( $key, give_sanitize_amount_for_db( $earnings ), 60 * 60 );
179 }
180
181 /**
182 * Filter the earnings.
183 *
184 * @since 1.8.17
185 *
186 * @param float $earnings Earning amount.
187 * @param int $form_id Donation Form ID.
188 * @param string|bool $start_date Earning start date.
189 * @param string|bool $end_date Earning end date.
190 * @param string|bool $gateway_id Payment gateway id.
191 */
192 $earnings = apply_filters( 'give_get_earnings', $earnings, $form_id, $start_date, $end_date, $gateway_id );
193
194 //return earnings
195 return round( $earnings, give_get_price_decimals( $form_id ) );
196
197 }
198
199 /**
200 * Retrieve earning stat transient key
201 *
202 * @since 1.0
203 * @access public
204 *
205 * @param $form_id int The donation form to retrieve stats for. If false, gets stats for all forms
206 * @param $start_date string|bool The starting date for which we'd like to filter our donation earnings stats. If false, we'll use the default start date of `this_month`
207 * @param $end_date string|bool The end date for which we'd like to filter our sale stats. If false, we'll use the default end date of `this_month`
208 * @param $gateway_id string|bool The gateway to get earnings for such as 'paypal' or 'stripe'
209 *
210 * @return float|int Total amount of donations based on the passed arguments.
211 */
212 public function get_earnings_cache_key( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) {
213
214 $this->setup_dates( $start_date, $end_date );
215
216 // Make sure start date is valid
217 if ( is_wp_error( $this->start_date ) ) {
218 return $this->start_date;
219 }
220
221 // Make sure end date is valid
222 if ( is_wp_error( $this->end_date ) ) {
223 return $this->end_date;
224 }
225
226 $args = array(
227 'status' => 'publish',
228 'give_forms' => $form_id,
229 'start_date' => $this->start_date,
230 'end_date' => $this->end_date,
231 'fields' => 'ids',
232 'number' => - 1,
233 );
234
235
236 // Filter by Gateway ID meta_key
237 if ( $gateway_id ) {
238 $args['meta_query'][] = array(
239 'key' => '_give_payment_gateway',
240 'value' => $gateway_id,
241 );
242 }
243
244 // Filter by Gateway ID meta_key
245 if ( $form_id ) {
246 $args['meta_query'][] = array(
247 'key' => '_give_payment_form_id',
248 'value' => $form_id,
249 );
250 }
251
252 if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) {
253 $args['meta_query']['relation'] = 'AND';
254 }
255
256 $args = apply_filters( 'give_stats_earnings_args', $args );
257 $key = Give_Cache::get_key( 'give_stats', $args );
258
259 //return earnings
260 return $key;
261
262 }
263
264 /**
265 * Get the best selling forms
266 *
267 * @since 1.0
268 * @access public
269 * @global wpdb $wpdb
270 *
271 * @param $number int The number of results to retrieve with the default set to 10.
272 *
273 * @return array Best selling forms
274 */
275 public function get_best_selling( $number = 10 ) {
276 global $wpdb;
277
278 $meta_table = __give_v20_bc_table_details( 'form' );
279
280 $give_forms = $wpdb->get_results( $wpdb->prepare(
281 "SELECT {$meta_table['column']['id']} as form_id, max(meta_value) as sales
282 FROM {$meta_table['name']} WHERE meta_key='_give_form_sales' AND meta_value > 0
283 GROUP BY meta_value+0
284 DESC LIMIT %d;", $number
285 ) );
286
287 return $give_forms;
288 }
289
290 }