actions.php
6 years ago
backward-compatibility.php
6 years ago
class-give-payment.php
5 years ago
class-give-sequential-donation-number.php
6 years ago
class-payment-stats.php
5 years ago
class-payments-query.php
5 years ago
functions.php
5 years ago
class-payment-stats.php
298 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Earnings / Sales Stats |
| 4 | * |
| 5 | * @package Give |
| 6 | * @subpackage Classes/Stats |
| 7 | * @copyright Copyright (c) 2016, GiveWP |
| 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 = [ |
| 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 = [ |
| 104 | 'status' => 'publish', |
| 105 | 'start_date' => $this->start_date, |
| 106 | 'end_date' => $this->end_date, |
| 107 | 'fields' => 'ids', |
| 108 | 'number' => - 1, |
| 109 | 'output' => '', |
| 110 | ]; |
| 111 | |
| 112 | // Filter by Gateway ID meta_key |
| 113 | if ( $gateway_id ) { |
| 114 | $args['meta_query'][] = [ |
| 115 | 'key' => '_give_payment_gateway', |
| 116 | 'value' => $gateway_id, |
| 117 | ]; |
| 118 | } |
| 119 | |
| 120 | // Filter by Gateway ID meta_key |
| 121 | if ( $form_id ) { |
| 122 | $args['meta_query'][] = [ |
| 123 | 'key' => '_give_payment_form_id', |
| 124 | 'value' => $form_id, |
| 125 | ]; |
| 126 | } |
| 127 | |
| 128 | if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) { |
| 129 | $args['meta_query']['relation'] = 'AND'; |
| 130 | } |
| 131 | |
| 132 | $args = apply_filters( 'give_stats_earnings_args', $args ); |
| 133 | $key = Give_Cache::get_key( 'give_stats', $args ); |
| 134 | |
| 135 | // Set transient for faster stats. |
| 136 | $earnings = Give_Cache::get( $key ); |
| 137 | |
| 138 | if ( false === $earnings ) { |
| 139 | |
| 140 | $this->timestamp = false; |
| 141 | $payments = new Give_Payments_Query( $args ); |
| 142 | $payments = $payments->get_payments(); |
| 143 | $earnings = 0; |
| 144 | |
| 145 | if ( ! empty( $payments ) ) { |
| 146 | $donation_id_col = Give()->payment_meta->get_meta_type() . '_id'; |
| 147 | $query = "SELECT {$donation_id_col} as id, meta_value as total |
| 148 | FROM {$wpdb->donationmeta} |
| 149 | WHERE meta_key='_give_payment_total' |
| 150 | AND {$donation_id_col} IN ('" . implode( '\',\'', $payments ) . "')"; |
| 151 | |
| 152 | $payments = $wpdb->get_results( $query, ARRAY_A ); |
| 153 | |
| 154 | if ( ! empty( $payments ) ) { |
| 155 | foreach ( $payments as $payment ) { |
| 156 | $currency_code = give_get_payment_currency_code( $payment['id'] ); |
| 157 | |
| 158 | /** |
| 159 | * Filter the donation amount |
| 160 | * Note: this filter documented in payments/functions.php:give_donation_amount() |
| 161 | * |
| 162 | * @since 2.1 |
| 163 | */ |
| 164 | $formatted_amount = apply_filters( |
| 165 | 'give_donation_amount', |
| 166 | give_format_amount( $payment['total'], [ 'donation_id' => $payment['id'] ] ), |
| 167 | $payment['total'], |
| 168 | $payment['id'], |
| 169 | [ |
| 170 | 'type' => 'stats', |
| 171 | 'currency' => false, |
| 172 | 'amount' => false, |
| 173 | ] |
| 174 | ); |
| 175 | |
| 176 | $earnings += (float) give_maybe_sanitize_amount( $formatted_amount, [ 'currency' => $currency_code ] ); |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // Cache the results for one hour. |
| 182 | Give_Cache::set( $key, give_sanitize_amount_for_db( $earnings ), 60 * 60 ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Filter the earnings. |
| 187 | * |
| 188 | * @since 1.8.17 |
| 189 | * |
| 190 | * @param float $earnings Earning amount. |
| 191 | * @param int $form_id Donation Form ID. |
| 192 | * @param string|bool $start_date Earning start date. |
| 193 | * @param string|bool $end_date Earning end date. |
| 194 | * @param string|bool $gateway_id Payment gateway id. |
| 195 | */ |
| 196 | $earnings = apply_filters( 'give_get_earnings', $earnings, $form_id, $start_date, $end_date, $gateway_id ); |
| 197 | |
| 198 | // return earnings |
| 199 | return round( $earnings, give_get_price_decimals( $form_id ) ); |
| 200 | |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Retrieve earning stat transient key |
| 205 | * |
| 206 | * @since 1.0 |
| 207 | * @access public |
| 208 | * |
| 209 | * @param $form_id int The donation form to retrieve stats for. If false, gets stats for all forms |
| 210 | * @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` |
| 211 | * @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` |
| 212 | * @param $gateway_id string|bool The gateway to get earnings for such as 'paypal' or 'stripe' |
| 213 | * |
| 214 | * @return float|int Total amount of donations based on the passed arguments. |
| 215 | */ |
| 216 | public function get_earnings_cache_key( $form_id = 0, $start_date = false, $end_date = false, $gateway_id = false ) { |
| 217 | |
| 218 | $this->setup_dates( $start_date, $end_date ); |
| 219 | |
| 220 | // Make sure start date is valid |
| 221 | if ( is_wp_error( $this->start_date ) ) { |
| 222 | return $this->start_date; |
| 223 | } |
| 224 | |
| 225 | // Make sure end date is valid |
| 226 | if ( is_wp_error( $this->end_date ) ) { |
| 227 | return $this->end_date; |
| 228 | } |
| 229 | |
| 230 | $args = [ |
| 231 | 'status' => 'publish', |
| 232 | 'start_date' => $this->start_date, |
| 233 | 'end_date' => $this->end_date, |
| 234 | 'fields' => 'ids', |
| 235 | 'number' => - 1, |
| 236 | ]; |
| 237 | |
| 238 | // Filter by Gateway ID meta_key |
| 239 | if ( $gateway_id ) { |
| 240 | $args['meta_query'][] = [ |
| 241 | 'key' => '_give_payment_gateway', |
| 242 | 'value' => $gateway_id, |
| 243 | ]; |
| 244 | } |
| 245 | |
| 246 | // Filter by Gateway ID meta_key |
| 247 | if ( $form_id ) { |
| 248 | $args['meta_query'][] = [ |
| 249 | 'key' => '_give_payment_form_id', |
| 250 | 'value' => $form_id, |
| 251 | ]; |
| 252 | } |
| 253 | |
| 254 | if ( ! empty( $args['meta_query'] ) && 1 < count( $args['meta_query'] ) ) { |
| 255 | $args['meta_query']['relation'] = 'AND'; |
| 256 | } |
| 257 | |
| 258 | $args = apply_filters( 'give_stats_earnings_args', $args ); |
| 259 | $key = Give_Cache::get_key( 'give_stats', $args ); |
| 260 | |
| 261 | // return earnings |
| 262 | return $key; |
| 263 | |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Get the best selling forms |
| 268 | * |
| 269 | * @since 1.0 |
| 270 | * @since 2.9.6 Added an explicit ORDER BY on which to apply a DESC order. |
| 271 | * @access public |
| 272 | * @global wpdb $wpdb |
| 273 | * |
| 274 | * @param $number int The number of results to retrieve with the default set to 10. |
| 275 | * |
| 276 | * @return array Best selling forms |
| 277 | */ |
| 278 | public function get_best_selling( $number = 10 ) { |
| 279 | global $wpdb; |
| 280 | |
| 281 | $meta_table = __give_v20_bc_table_details( 'form' ); |
| 282 | |
| 283 | $give_forms = $wpdb->get_results( |
| 284 | $wpdb->prepare( |
| 285 | "SELECT {$meta_table['column']['id']} as form_id, max(meta_value) as sales |
| 286 | FROM {$meta_table['name']} WHERE meta_key='_give_form_sales' AND meta_value > 0 |
| 287 | GROUP BY meta_value+0 |
| 288 | ORDER BY sales DESC |
| 289 | LIMIT %d;", |
| 290 | $number |
| 291 | ) |
| 292 | ); |
| 293 | |
| 294 | return $give_forms; |
| 295 | } |
| 296 | |
| 297 | } |
| 298 |