PluginProbe
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) / 1.7.0.2
Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) v1.7.0.2
1.8.13.1 1.8.13 1.8.12.3 1.8.12.2 1.8.12.1 1.8.12 1.8.11.3 1.8.11.2 1.8.11.1 1.8.11 1.6.6 1.6.60 1.6.7 1.6.8 1.6.9 1.7.0 1.7.0.1 1.7.0.11 1.7.0.12 1.7.0.14 1.7.0.2 1.7.0.3 1.7.0.5 1.7.0.6 1.7.0.7 All 212 releases
charitable / includes / donations / class-charitable-donation-report.php

class-charitable-donation-report.php in Charitable – Donation & Fundraising Platform (Donation Forms, Recurring Donations & Fundraising Campaigns) 1.7.0.2, at includes/donations/class-charitable-donation-report.php

255 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Model for generating a donation report.
4 *
5 * @package Charitable/Classes/Charitable_Donation_Report
6 * @author David Bisset
7 * @copyright Copyright (c) 2022, WP Charitable LLC
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.6.0
10 * @version 1.6.0
11 */
12
13 // Exit if accessed directly.
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit;
16 }
17
18 if ( ! class_exists( 'Charitable_Donation_Report' ) ) :
19
20 /**
21 * Charitable_Donation_Report
22 *
23 * @since 1.6.0
24 */
25 class Charitable_Donation_Report {
26
27 /**
28 * Mixed set of arguments for the query.
29 *
30 * @since 1.6.0
31 *
32 * @var array
33 */
34 private $args;
35
36 /**
37 * Types of reports.
38 *
39 * @since 1.6.0
40 *
41 * @var array
42 */
43 private $types = array(
44 'amount',
45 'donations',
46 'donors',
47 );
48
49 /**
50 * Reports.
51 *
52 * @since 1.6.0
53 *
54 * @var array|false
55 */
56 private $reports = false;
57
58 /**
59 * Create class object.
60 *
61 * @since 1.6.0
62 *
63 * @param array $args Mixed set of arguments for the query.
64 */
65 public function __construct( $args = array() ) {
66 $this->args = $this->parse_args( $args );
67 }
68
69 /**
70 * Get the reports for each report type.
71 *
72 * @since 1.6.0
73 *
74 * @return array
75 */
76 public function get_reports() {
77 if ( false === $this->reports ) {
78 $this->reports = array();
79
80 if ( false === $this->args['report_type'] ) {
81 return $this->reports;
82 }
83
84 foreach ( $this->types as $type ) {
85 if ( in_array( $type, $this->args['report_type'] ) ) {
86 $this->reports[ $type ] = $this->run_report_query( $type );
87 }
88 }
89 }
90
91 return $this->reports;
92 }
93
94 /**
95 * Get a single report result.
96 *
97 * @since 1.6.0
98 *
99 * @param string $report_type The report type to get.
100 * @return mixed
101 */
102 public function get_report( $report_type ) {
103 if ( is_array( $this->reports ) ) {
104 return $this->reports[ $report_type ];
105 }
106
107 return $this->run_report_query( $report_type );
108 }
109
110 /**
111 * Run a particular report query.
112 *
113 * @since 1.6.0
114 *
115 * @param string $type The type of report.
116 * @return mixed
117 */
118 private function run_report_query( $type ) {
119 switch ( $type ) {
120 case 'amount':
121 return $this->run_amount_query();
122
123 case 'donors':
124 return $this->run_donor_query();
125
126 case 'donations':
127 return $this->run_donation_query();
128 }
129 }
130
131 /**
132 * Generate a total query type.
133 *
134 * @since 1.6.0
135 *
136 * @return string
137 */
138 private function run_amount_query() {
139 if ( empty( $this->args['campaigns'] ) ) {
140 return charitable_get_table( 'campaign_donations' )->get_total();
141 }
142
143 return charitable_get_table( 'campaign_donations' )->get_campaign_donated_amount( $this->args['campaigns'] );
144 }
145
146 /**
147 * Generate a donations query type.
148 *
149 * @since 1.6.0
150 *
151 * @return int
152 */
153 private function run_donation_query() {
154 $query = new Charitable_Donations_Query(
155 array(
156 'output' => 'count',
157 'campaign' => $this->args['campaigns'],
158 'status' => $this->args['status'],
159 )
160 );
161
162 return $query->count();
163 }
164
165 /**
166 * Generate a donors query type.
167 *
168 * @since 1.6.0
169 *
170 * @return int
171 */
172 private function run_donor_query() {
173 $query = new Charitable_Donor_Query(
174 array(
175 'output' => 'count',
176 'campaign' => $this->args['campaigns'],
177 'status' => $this->args['status'],
178 )
179 );
180
181 return $query->count();
182 }
183
184 /**
185 * Parse arguments.
186 *
187 * @since 1.6.0
188 *
189 * @param array $args User defined arguments.
190 * @return array
191 */
192 private function parse_args( $args ) {
193 $defaults = array(
194 'report_type' => 'all',
195 'campaigns' => array(),
196 'status' => array( 'charitable-completed', 'charitable-preapproved' ),
197 );
198
199 $args = array_merge( $defaults, $args );
200 $args['campaigns'] = $this->parse_campaigns( $args['campaigns'] );
201 $args['report_type'] = $this->parse_report_type( $args['report_type'] );
202
203 return $args;
204 }
205
206 /**
207 * Parse campaigns argument.
208 *
209 * @since 1.6.0
210 *
211 * @param mixed $campaigns An array of campaigns.
212 * @return array
213 */
214 private function parse_campaigns( $campaigns ) {
215 if ( empty( $campaigns ) ) {
216 return array();
217 }
218
219 return array_filter( $campaigns, 'intval' );
220 }
221
222 /**
223 * Parse the passed report type.
224 *
225 * @since 1.6.0
226 *
227 * @param string|array $report_type The type of report to get. May be a string or an array of strings.
228 * @return array|false
229 */
230 private function parse_report_type( $report_type ) {
231 if ( 'all' == $report_type ) {
232 return $this->types;
233 }
234
235 if ( is_array( $report_type ) ) {
236 return $report_type;
237 }
238
239 if ( ! in_array( $report_type, $this->types ) ) {
240 charitable_get_deprecated()->doing_it_wrong(
241 __METHOD__,
242 /* translators: %s: report type */
243 sprintf( __( '%s is not a valid donation report type.', 'charitable' ), $report_type ),
244 '1.6.0'
245 );
246
247 return false;
248 }
249
250 return array( $report_type );
251 }
252 }
253
254 endif;
255