Give_Payment_Stats::get_earnings PHP Method

get_earnings() public method

Retrieve earning stats
Since: 1.0
public get_earnings ( $form_id, $start_date = false, $end_date = false, $gateway_id = false ) : float | integer
$form_id int The donation form to retrieve stats for. If false, gets stats for all forms
$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`
$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`
$gateway_id string|bool The gateway to get earnings for such as 'paypal' or 'stripe'
return float | integer Total amount of donations based on the passed arguments.
    public function get_earnings($form_id = 0, $start_date = false, $end_date = false, $gateway_id = false)
    {
        global $wpdb;
        $this->setup_dates($start_date, $end_date);
        // Make sure start date is valid
        if (is_wp_error($this->start_date)) {
            return $this->start_date;
        }
        // Make sure end date is valid
        if (is_wp_error($this->end_date)) {
            return $this->end_date;
        }
        add_filter('posts_where', array($this, 'payments_where'));
        if (empty($form_id)) {
            // Global earning stats
            $args = array('post_type' => 'give_payment', 'nopaging' => true, 'post_status' => array('publish'), 'fields' => 'ids', 'update_post_term_cache' => false, 'suppress_filters' => false, 'start_date' => $this->start_date, 'end_date' => $this->end_date, 'give_transient_type' => 'give_earnings');
            //Filter by Gateway ID meta_key
            if ($gateway_id !== false) {
                $args['meta_key'] = '_give_payment_gateway';
                $args['meta_value'] = $gateway_id;
            }
            $args = apply_filters('give_stats_earnings_args', $args);
            $key = 'give_stats_' . substr(md5(serialize($args)), 0, 15);
            $earnings = get_transient($key);
            if (false === $earnings) {
                $sales = get_posts($args);
                $earnings = 0;
                if ($sales) {
                    $sales = implode(',', array_map('intval', $sales));
                    $earnings += $wpdb->get_var("SELECT SUM(meta_value) FROM {$wpdb->postmeta} WHERE meta_key = '_give_payment_total' AND post_id IN({$sales})");
                }
                // Cache the results for one hour
                set_transient($key, $earnings, HOUR_IN_SECONDS);
            }
        } else {
            // Donation form specific earning stats
            global $give_logs, $wpdb;
            $args = array('post_parent' => $form_id, 'nopaging' => true, 'log_type' => 'sale', 'fields' => 'ids', 'suppress_filters' => false, 'start_date' => $this->start_date, 'end_date' => $this->end_date, 'give_transient_type' => 'give_earnings');
            $args = apply_filters('give_stats_earnings_args', $args);
            $key = 'give_stats_' . substr(md5(serialize($args)), 0, 15);
            //Set transient for faster stats
            $earnings = get_transient($key);
            if (false === $earnings) {
                $this->timestamp = false;
                $log_ids = $give_logs->get_connected_logs($args, 'sale');
                $earnings = 0;
                if ($log_ids) {
                    $log_ids = implode(',', array_map('intval', $log_ids));
                    $payment_ids = $wpdb->get_col("SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key='_give_log_payment_id' AND post_id IN ({$log_ids});");
                    foreach ($payment_ids as $payment_id) {
                        $earnings += give_get_payment_amount($payment_id);
                    }
                }
                // Cache the results for one hour
                set_transient($key, $earnings, 60 * 60);
            }
        }
        //remove our filter
        remove_filter('posts_where', array($this, 'payments_where'));
        //return earnings
        return round($earnings, give_currency_decimal_filter());
    }

Usage Example

示例#1
0
 /**
  * Test Get Earnings by Date of Give Donation Form
  *
  * @covers Give_Payment_Stats::get_earnings
  */
 public function test_get_earnings_by_date_of_give_form()
 {
     $payment = new Give_Payment();
     $stats = new Give_Payment_Stats();
     $earnings = $stats->get_earnings($payment->form_id, 'this_month');
     $this->_new_form_id = $payment->form_id;
     $this->assertEquals(45, $earnings);
 }
All Usage Examples Of Give_Payment_Stats::get_earnings