Test Failed
Push — master ( f5256c...25a383 )
by Devin
07:02
created

Give_Donor_Stats::get_instance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Donor stats
4
 *
5
 * Note: Currently wr are working on this API. This is internal use only
6
 *
7
 * @package     Give
8
 * @subpackage  Classes/Donor/Stats
9
 * @copyright   Copyright (c) 2018, WordImpress
10
 * @license     https://opensource.org/licenses/gpl-license GNU Public License
11
 * @since       2.2.0
12
 */
13
class Give_Donor_Stats {
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
14
	/**
15
	 * Instance.
16
	 *
17
	 * @since  2.2.0
18
	 * @access private
19
	 * @var
20
	 */
21
	static private $instance;
22
23
	/**
24
	 * Singleton pattern.
25
	 *
26
	 * @since  2.2.0
27
	 * @access private
28
	 */
29
	private function __construct() {
30
	}
31
32
33
	/**
34
	 * Get instance.
35
	 *
36
	 * @since  2.2.0
37
	 * @access public
38
	 * @return Give_Donor_Stats
39
	 */
40
	public static function get_instance() {
41
		if ( null === static::$instance ) {
42
			self::$instance = new static();
43
		}
44
45
		return self::$instance;
46
	}
47
48
	/**
49
	 *  Get total donated amount
50
	 *
51
	 *
52
	 * @since  2.2.0
53
	 * @access public
54
	 *
55
	 * @param array $args
56
	 *
57
	 * @return string
58
	 *
59
	 */
60
	public static function donated( $args = array() ) {
61
		global $wpdb;
62
		$donation_id_col = Give()->payment_meta->get_meta_type() . '_id';
63
64
		$donated_amount = '';
65
66
		if ( empty( $args['donor'] ) ) {
67
			return $donated_amount;
68
		}
69
70
		$args['output'] = 'posts';
71
		$args['status'] = 'publish';
72
		$args['fields'] = 'ids';
73
		$args['number'] = - 1;
74
75
		$donation_query  = new Give_Payments_Query( $args );
76
		$donations       = $donation_query->get_payments();
77
		$donation_id_str = implode( '\',\'', $donations );
78
79
		$query = "SELECT {$donation_id_col} as id, meta_value as total
80
					FROM {$wpdb->donationmeta}
81
					WHERE meta_key='_give_payment_total'
82
					AND {$donation_id_col} IN ('{$donation_id_str}')";
83
84
		$donated_amounts = $wpdb->get_results( $query, ARRAY_A );
0 ignored issues
show
introduced by
Usage of a direct database call is discouraged.
Loading history...
introduced by
Usage of a direct database call without caching is prohibited. Use wp_cache_get / wp_cache_set.
Loading history...
85
86
		if ( ! empty( $donated_amounts ) ) {
87
			foreach ( $donated_amounts as $donation ) {
88
				$currency_code = give_get_payment_currency_code( $donation['id'] );
89
				/**
90
				 * Filter the donation amount
91
				 * Note: this filter documented in payments/functions.php:give_donation_amount()
92
				 *
93
				 * @since 2.1
94
				 */
95
				$formatted_amount = apply_filters(
96
					'give_donation_amount',
97
					give_format_amount( $donation['total'], array( 'currency' => $currency_code ) ),
98
					$donation['total'],
99
					$donation['id'],
100
					array( 'type' => 'stats', 'currency' => false, 'amount' => false )
101
				);
102
103
				$donated_amount = (float) give_maybe_sanitize_amount( $formatted_amount, array( 'currency' => $currency_code  ) );
104
				$donated_amount += $donated_amount;
105
			}
106
		}
107
108
		return $donated_amount;
109
	}
110
}
111