PluginProbe ʕ •ᴥ•ʔ
GiveWP – Donation Plugin and Fundraising Platform / 2.29.2
GiveWP – Donation Plugin and Fundraising Platform v2.29.2
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 / api / class-give-api.php
give / includes / api Last commit date
class-give-api-v1.php 7 years ago class-give-api-v2.php 4 years ago class-give-api.php 3 years ago
class-give-api.php
2294 lines
1 <?php
2 /**
3 * Give API
4 *
5 * A front-facing JSON/XML API that makes it possible to query donation data.
6 *
7 * @package Give
8 * @subpackage Classes/API
9 * @copyright Copyright (c) 2016, GiveWP
10 * @license https://opensource.org/licenses/gpl-license GNU Public License
11 * @since 1.1
12 */
13
14 // Exit if accessed directly.
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * Give_API Class
21 *
22 * Renders API returns as a JSON/XML array
23 *
24 * @since 1.1
25 */
26 class Give_API {
27
28 /**
29 * Latest API Version
30 */
31 const VERSION = 1;
32
33 /**
34 * Pretty Print?
35 *
36 * @var bool
37 * @access private
38 * @since 1.1
39 */
40 private $pretty_print = false;
41
42 /**
43 * Log API requests?
44 *
45 * @var bool
46 * @access public
47 * @since 1.1
48 */
49 public $log_requests = true;
50
51 /**
52 * Is this a valid request?
53 *
54 * @var bool
55 * @access private
56 * @since 1.1
57 */
58 private $is_valid_request = false;
59
60 /**
61 * User ID Performing the API Request
62 *
63 * @var int
64 * @access public
65 * @since 1.1
66 */
67 public $user_id = 0;
68
69 /**
70 * Instance of Give Stats class
71 *
72 * @var object
73 * @access private
74 * @since 1.1
75 */
76 private $stats;
77
78 /**
79 * Response data to return
80 *
81 * @var array
82 * @access private
83 * @since 1.1
84 */
85 private $data = array();
86
87 /**
88 * Whether or not to override api key validation.
89 *
90 * @var bool
91 * @access public
92 * @since 1.1
93 */
94 public $override = true;
95
96 /**
97 * Version of the API queried
98 *
99 * @var string
100 * @access public
101 * @since 1.1
102 */
103 private $queried_version;
104
105 /**
106 * All versions of the API
107 *
108 * @var array
109 * @access protected
110 * @since 1.1
111 */
112 protected $versions = array();
113
114 /**
115 * Queried endpoint
116 *
117 * @var string
118 * @access private
119 * @since 1.1
120 */
121 private $endpoint;
122
123 /**
124 * Endpoints routes
125 *
126 * @var object
127 * @access private
128 * @since 1.1
129 */
130 private $routes;
131
132 /**
133 * Setup the Give API
134 *
135 * @since 1.1
136 * @access public
137 */
138 public function __construct() {
139
140 $this->versions = array(
141 'v1' => 'GIVE_API_V1',
142 );
143
144 foreach ( $this->get_versions() as $version => $class ) {
145 require_once GIVE_PLUGIN_DIR . 'includes/api/class-give-api-' . $version . '.php';
146 }
147
148 add_action( 'init', array( $this, 'add_endpoint' ) );
149 add_action( 'wp', array( $this, 'process_query' ), - 1 );
150 add_filter( 'query_vars', array( $this, 'query_vars' ) );
151 add_action( 'show_user_profile', array( $this, 'user_key_field' ) );
152 add_action( 'edit_user_profile', array( $this, 'user_key_field' ) );
153 add_action( 'personal_options_update', array( $this, 'generate_api_key' ) );
154 add_action( 'edit_user_profile_update', array( $this, 'generate_api_key' ) );
155 add_action( 'give_process_api_key', array( $this, 'process_api_key' ) );
156
157 // Setup a backwards compatibility check for user API Keys
158 add_filter( 'get_user_metadata', array( $this, 'api_key_backwards_compat' ), 10, 4 );
159
160 // Determine if JSON_PRETTY_PRINT is available
161 $this->pretty_print = defined( 'JSON_PRETTY_PRINT' ) ? JSON_PRETTY_PRINT : null;
162
163 // Allow API request logging to be turned off
164 $this->log_requests = apply_filters( 'give_api_log_requests', $this->log_requests );
165
166 // Setup Give_Payment_Stats instance
167 $this->stats = new Give_Payment_Stats();
168
169 }
170
171 /**
172 * There are certain responsibility of this function:
173 * 1. handle backward compatibility for deprecated functions
174 *
175 * @since 2.0
176 *
177 * @param $name
178 * @param $arguments
179 *
180 * @return mixed
181 */
182 public function __call( $name, $arguments ) {
183 $deprecated_function_arr = array(
184 'get_customers',
185 );
186
187 if ( in_array( $name, $deprecated_function_arr, true ) ) {
188 switch ( $name ) {
189 case 'get_customers':
190 $args = ! empty( $arguments[0] ) ? $arguments[0] : array();
191
192 return $this->get_donors( $args );
193 }
194 }
195 }
196
197 /**
198 * Registers a new rewrite endpoint for accessing the API
199 *
200 * @access public
201 *
202 * @since 1.1
203 */
204 public function add_endpoint() {
205 add_rewrite_endpoint( 'give-api', EP_ALL );
206 }
207
208 /**
209 * Registers query vars for API access
210 *
211 * @access public
212 * @since 1.1
213 *
214 * @param array $vars Query vars
215 *
216 * @return string[] $vars New query vars
217 */
218 public function query_vars( $vars ) {
219
220 $vars[] = 'token';
221 $vars[] = 'key';
222 $vars[] = 'query';
223 $vars[] = 'type';
224 $vars[] = 'form';
225 $vars[] = 'number';
226 $vars[] = 'date';
227 $vars[] = 'startdate';
228 $vars[] = 'enddate';
229 $vars[] = 'donor';
230 $vars[] = 'format';
231 $vars[] = 'id';
232 $vars[] = 'purchasekey';
233 $vars[] = 'email';
234
235 return $vars;
236 }
237
238 /**
239 * Retrieve the API versions
240 *
241 * @access public
242 * @since 1.1
243 * @return array
244 */
245 public function get_versions() {
246 return $this->versions;
247 }
248
249 /**
250 * Retrieve the API version that was queried
251 *
252 * @access public
253 * @since 1.1
254 * @return string
255 */
256 public function get_queried_version() {
257 return $this->queried_version;
258 }
259
260 /**
261 * Retrieves the default version of the API to use
262 *
263 * @access public
264 * @since 1.1
265 * @return string
266 */
267 public function get_default_version() {
268
269 $version = get_option( 'give_default_api_version' );
270
271 if ( defined( 'GIVE_API_VERSION' ) ) {
272 $version = GIVE_API_VERSION;
273 } elseif ( ! $version ) {
274 $version = 'v1';
275 }
276
277 return $version;
278 }
279
280 /**
281 * Sets the version of the API that was queried.
282 *
283 * Falls back to the default version if no version is specified
284 *
285 * @access private
286 * @since 1.1
287 */
288 private function set_queried_version() {
289
290 global $wp_query;
291
292 $version = $wp_query->query_vars['give-api'];
293
294 if ( strpos( $version, '/' ) ) {
295
296 $version = explode( '/', $version );
297 $version = strtolower( $version[0] );
298
299 $wp_query->query_vars['give-api'] = str_replace( $version . '/', '', $wp_query->query_vars['give-api'] );
300
301 if ( array_key_exists( $version, $this->versions ) ) {
302
303 $this->queried_version = $version;
304
305 } else {
306
307 $this->is_valid_request = false;
308 $this->invalid_version();
309 }
310 } else {
311
312 $this->queried_version = $this->get_default_version();
313
314 }
315
316 }
317
318 /**
319 * Validate the API request
320 *
321 * Checks for the user's public key and token against the secret key.
322 *
323 * @access private
324 * @global object $wp_query WordPress Query
325 * @uses Give_API::get_user()
326 * @uses Give_API::invalid_key()
327 * @uses Give_API::invalid_auth()
328 * @since 1.1
329 * @return bool
330 */
331 private function validate_request() {
332 global $wp_query;
333
334 $this->override = false;
335
336 // Make sure we have both user and api key
337 if ( ! empty( $wp_query->query_vars['give-api'] ) && ( $wp_query->query_vars['give-api'] !== 'forms' || ! empty( $wp_query->query_vars['token'] ) ) ) {
338
339 if ( empty( $wp_query->query_vars['token'] ) || empty( $wp_query->query_vars['key'] ) ) {
340 $this->missing_auth();
341
342 return false;
343 }
344
345 // Retrieve the user by public API key and ensure they exist
346 if ( ! preg_match( '/^[a-f0-9]{32}$/i', $wp_query->query_vars['key'] ) || ! ( $user = $this->get_user( $wp_query->query_vars['key'] ) ) ) {
347
348 $this->invalid_key();
349
350 return false;
351
352 } else {
353 $token = urldecode( $wp_query->query_vars['token'] );
354 $secret = $this->get_user_secret_key( $user );
355 $public = urldecode( $wp_query->query_vars['key'] );
356
357 // Verify that if user has secret key or not
358 if ( ! $secret ) {
359 $this->invalid_auth();
360 }
361
362 if ( hash_equals( md5( $secret . $public ), $token ) ) {
363 $this->is_valid_request = true;
364 } else {
365 $this->invalid_auth();
366
367 return false;
368 }
369 }
370 } elseif ( ! empty( $wp_query->query_vars['give-api'] ) && $wp_query->query_vars['give-api'] === 'forms' ) {
371 $this->is_valid_request = true;
372 $wp_query->set( 'key', 'public' );
373 }
374 }
375
376 /**
377 * Retrieve the user ID based on the public key provided
378 *
379 * @access public
380 * @since 1.1
381 * @global WPDB $wpdb Used to query the database using the WordPress
382 * Database API
383 *
384 * @param string $key Public Key
385 *
386 * @return bool if user ID is found, false otherwise
387 */
388 public function get_user( $key = '' ) {
389 global $wpdb, $wp_query;
390
391 if ( empty( $key ) ) {
392 $key = urldecode( $wp_query->query_vars['key'] );
393 }
394
395 if ( empty( $key ) ) {
396 return false;
397 }
398
399 $user = Give_Cache::get( md5( 'give_api_user_' . $key ), true );
400
401 if ( false === $user ) {
402 $user = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s AND meta_value=%s LIMIT 1", $key, 'give_user_public_key' ) );
403 Give_Cache::set( md5( 'give_api_user_' . $key ), $user, DAY_IN_SECONDS, true );
404 }
405
406 if ( $user != null ) {
407 $this->user_id = $user;
408
409 return $user;
410 }
411
412 return false;
413 }
414
415 /**
416 * Get user public key.
417 *
418 * @param int $user_id
419 *
420 * @return mixed|null|string
421 */
422 public function get_user_public_key( $user_id = 0 ) {
423 global $wpdb;
424
425 if ( empty( $user_id ) ) {
426 return '';
427 }
428
429 $cache_key = md5( 'give_api_user_public_key' . $user_id );
430 $user_public_key = Give_Cache::get( $cache_key, true );
431
432 if ( empty( $user_public_key ) ) {
433 $user_public_key = $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->usermeta WHERE meta_value = 'give_user_public_key' AND user_id = %d", $user_id ) );
434 Give_Cache::set( $cache_key, $user_public_key, HOUR_IN_SECONDS, true );
435 }
436
437 return $user_public_key;
438 }
439
440 /**
441 * Get user secret key.
442 *
443 * @param int $user_id
444 *
445 * @return mixed|null|string
446 */
447 public function get_user_secret_key( $user_id = 0 ) {
448 global $wpdb;
449
450 if ( empty( $user_id ) ) {
451 return '';
452 }
453
454 $cache_key = md5( 'give_api_user_secret_key' . $user_id );
455 $user_secret_key = Give_Cache::get( $cache_key, true );
456
457 if ( empty( $user_secret_key ) ) {
458 $user_secret_key = $wpdb->get_var( $wpdb->prepare( "SELECT meta_key FROM $wpdb->usermeta WHERE meta_value = 'give_user_secret_key' AND user_id = %d", $user_id ) );
459 Give_Cache::set( $cache_key, $user_secret_key, HOUR_IN_SECONDS, true );
460 }
461
462 return $user_secret_key;
463 }
464
465 /**
466 * Displays a missing authentication error if all the parameters are not met.
467 * provided
468 *
469 * @access private
470 * @uses Give_API::output()
471 * @since 1.1
472 */
473 private function missing_auth() {
474 $error = array();
475 $error['error'] = __( 'You must specify both a token and API key.', 'give' );
476
477 $this->data = $error;
478 $this->output( 401 );
479 }
480
481 /**
482 * Displays an authentication failed error if the user failed to provide valid
483 * credentials
484 *
485 * @access private
486 * @since 1.1
487 * @uses Give_API::output()
488 * @return void
489 */
490 private function invalid_auth() {
491 $error = array();
492 $error['error'] = __( 'Your request could not be authenticated.', 'give' );
493
494 $this->data = $error;
495 $this->output( 403 );
496 }
497
498 /**
499 * Displays an invalid API key error if the API key provided couldn't be
500 * validated
501 *
502 * @access private
503 * @since 1.1
504 * @uses Give_API::output()
505 * @return void
506 */
507 private function invalid_key() {
508 $error = array();
509 $error['error'] = __( 'Invalid API key.', 'give' );
510
511 $this->data = $error;
512 $this->output( 403 );
513 }
514
515 /**
516 * Displays an invalid version error if the version number passed isn't valid
517 *
518 * @access private
519 * @since 1.1
520 * @uses Give_API::output()
521 * @return void
522 */
523 private function invalid_version() {
524 $error = array();
525 $error['error'] = __( 'Invalid API version.', 'give' );
526
527 $this->data = $error;
528 $this->output( 404 );
529 }
530
531 /**
532 * Listens for the API and then processes the API requests
533 *
534 * @access public
535 * @global $wp_query
536 * @since 1.1
537 * @return void
538 */
539 public function process_query() {
540
541 global $wp_query;
542
543 // Start logging how long the request takes for logging
544 $before = microtime( true );
545
546 // Check for give-api var. Get out if not present
547 if ( empty( $wp_query->query_vars['give-api'] ) ) {
548 return;
549 }
550
551 // Determine which version was queried
552 $this->set_queried_version();
553
554 // Determine the kind of query
555 $this->set_query_mode();
556
557 // Check for a valid user and set errors if necessary
558 $this->validate_request();
559
560 // Only proceed if no errors have been noted
561 if ( ! $this->is_valid_request ) {
562 return;
563 }
564
565 if ( ! defined( 'GIVE_DOING_API' ) ) {
566 define( 'GIVE_DOING_API', true );
567 }
568
569 $data = array();
570 $this->routes = new $this->versions[ $this->get_queried_version() ];
571 $this->routes->validate_request();
572
573 switch ( $this->endpoint ) :
574
575 case 'stats':
576 $data = $this->routes->get_stats(
577 array(
578 'type' => isset( $wp_query->query_vars['type'] ) ? $wp_query->query_vars['type'] : null,
579 'form' => isset( $wp_query->query_vars['form'] ) ? $wp_query->query_vars['form'] : null,
580 'date' => isset( $wp_query->query_vars['date'] ) ? $wp_query->query_vars['date'] : null,
581 'startdate' => isset( $wp_query->query_vars['startdate'] ) ? $wp_query->query_vars['startdate'] : null,
582 'enddate' => isset( $wp_query->query_vars['enddate'] ) ? $wp_query->query_vars['enddate'] : null,
583 )
584 );
585
586 break;
587
588 case 'forms':
589 $form = isset( $wp_query->query_vars['form'] ) ? $wp_query->query_vars['form'] : null;
590
591 $data = $this->routes->get_forms( $form );
592
593 break;
594
595 case 'donors':
596 $donor = isset( $wp_query->query_vars['donor'] ) ? $wp_query->query_vars['donor'] : null;
597
598 $data = $this->routes->get_donors( $donor );
599
600 break;
601
602 case 'donations':
603 /**
604 * Call to get recent donations
605 *
606 * @params text date | today, yesterday or range
607 * @params date startdate | required when date = range and format to be YYYYMMDD (i.e. 20170524)
608 * @params date enddate | required when date = range and format to be YYYYMMDD (i.e. 20170524)
609 */
610 $data = $this->routes->get_recent_donations(
611 array(
612 'id' => isset( $wp_query->query_vars['id'] ) ? $wp_query->query_vars['id'] : null,
613 'date' => isset( $wp_query->query_vars['date'] ) ? $wp_query->query_vars['date'] : null,
614 'startdate' => isset( $wp_query->query_vars['startdate'] ) ? $wp_query->query_vars['startdate'] : null,
615 'enddate' => isset( $wp_query->query_vars['enddate'] ) ? $wp_query->query_vars['enddate'] : null,
616 )
617 );
618
619 break;
620
621 endswitch;
622
623 // Allow extensions to setup their own return data
624 $this->data = apply_filters( 'give_api_output_data', $data, $this->endpoint, $this );
625
626 $after = microtime( true );
627 $request_time = ( $after - $before );
628 $this->data['request_speed'] = $request_time;
629
630 // Log this API request, if enabled. We log it here because we have access to errors.
631 $this->log_request( $this->data );
632
633 // Send out data to the output function
634 $this->output();
635 }
636
637 /**
638 * Returns the API endpoint requested
639 *
640 * @access public
641 * @since 1.1
642 * @return string $query Query mode
643 */
644 public function get_query_mode() {
645
646 return $this->endpoint;
647 }
648
649 /**
650 * Determines the kind of query requested and also ensure it is a valid query
651 *
652 * @access public
653 * @since 1.1
654 * @global $wp_query
655 */
656 public function set_query_mode() {
657
658 global $wp_query;
659
660 // Whitelist our query options
661 $accepted = apply_filters(
662 'give_api_valid_query_modes',
663 array(
664 'stats',
665 'forms',
666 'donors',
667 'donations',
668 )
669 );
670
671 $query = isset( $wp_query->query_vars['give-api'] ) ? $wp_query->query_vars['give-api'] : null;
672 $query = str_replace( $this->queried_version . '/', '', $query );
673
674 $error = array();
675
676 // Make sure our query is valid
677 if ( ! in_array( $query, $accepted ) ) {
678 $error['error'] = __( 'Invalid query.', 'give' );
679
680 $this->data = $error;
681 // 400 is Bad Request
682 $this->output( 400 );
683 }
684
685 $this->endpoint = $query;
686 }
687
688 /**
689 * Get page number
690 *
691 * @access public
692 * @since 1.1
693 * @global $wp_query
694 * @return int $wp_query->query_vars['page'] if page number returned (default: 1)
695 */
696 public function get_paged() {
697 global $wp_query;
698
699 return isset( $wp_query->query_vars['page'] ) ? $wp_query->query_vars['page'] : 1;
700 }
701
702
703 /**
704 * Number of results to display per page
705 *
706 * @access public
707 * @since 1.1
708 * @global $wp_query
709 * @return int $per_page Results to display per page (default: 10)
710 */
711 public function per_page() {
712 global $wp_query;
713
714 $per_page = isset( $wp_query->query_vars['number'] ) ? $wp_query->query_vars['number'] : 10;
715
716 if ( $per_page < 0 && $this->get_query_mode() == 'donors' ) {
717 $per_page = 99999999;
718 } // End if().
719
720 return apply_filters( 'give_api_results_per_page', $per_page );
721 }
722
723 /**
724 * Sets up the dates used to retrieve earnings/donations
725 *
726 * @access public
727 * @since 1.2
728 *
729 * @param array $args Arguments to override defaults
730 *
731 * @return array $dates
732 */
733 public function get_dates( $args = array() ) {
734 $dates = array();
735
736 $defaults = array(
737 'type' => '',
738 'form' => null,
739 'date' => null,
740 'startdate' => null,
741 'enddate' => null,
742 );
743
744 $args = wp_parse_args( $args, $defaults );
745
746 $current_time = current_time( 'timestamp' );
747
748 if ( 'range' === $args['date'] ) {
749 $startdate = strtotime( $args['startdate'] );
750 $enddate = strtotime( $args['enddate'] );
751 $dates['day_start'] = date( 'd', $startdate );
752 $dates['day_end'] = date( 'd', $enddate );
753 $dates['m_start'] = date( 'n', $startdate );
754 $dates['m_end'] = date( 'n', $enddate );
755 $dates['year'] = date( 'Y', $startdate );
756 $dates['year_end'] = date( 'Y', $enddate );
757 } else {
758 // Modify dates based on predefined ranges
759 switch ( $args['date'] ) :
760
761 case 'this_month':
762 $dates['day'] = null;
763 $dates['m_start'] = date( 'n', $current_time );
764 $dates['m_end'] = date( 'n', $current_time );
765 $dates['year'] = date( 'Y', $current_time );
766 break;
767
768 case 'last_month':
769 $dates['day'] = null;
770 $dates['m_start'] = date( 'n', $current_time ) == 1 ? 12 : date( 'n', $current_time ) - 1;
771 $dates['m_end'] = $dates['m_start'];
772 $dates['year'] = date( 'n', $current_time ) == 1 ? date( 'Y', $current_time ) - 1 : date( 'Y', $current_time );
773 break;
774
775 case 'today':
776 $dates['day'] = date( 'd', $current_time );
777 $dates['m_start'] = date( 'n', $current_time );
778 $dates['m_end'] = date( 'n', $current_time );
779 $dates['year'] = date( 'Y', $current_time );
780 break;
781
782 case 'yesterday':
783 $year = date( 'Y', $current_time );
784 $month = date( 'n', $current_time );
785 $day = date( 'd', $current_time );
786
787 if ( $month == 1 && $day == 1 ) {
788
789 $year -= 1;
790 $month = 12;
791 $day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
792
793 } elseif ( $month > 1 && $day == 1 ) {
794
795 $month -= 1;
796 $day = cal_days_in_month( CAL_GREGORIAN, $month, $year );
797
798 } else {
799
800 $day -= 1;
801
802 }
803
804 $dates['day'] = $day;
805 $dates['m_start'] = $month;
806 $dates['m_end'] = $month;
807 $dates['year'] = $year;
808
809 break;
810
811 case 'this_quarter':
812 $month_now = date( 'n', $current_time );
813
814 $dates['day'] = null;
815
816 if ( $month_now <= 3 ) {
817
818 $dates['m_start'] = 1;
819 $dates['m_end'] = 3;
820 $dates['year'] = date( 'Y', $current_time );
821
822 } elseif ( $month_now <= 6 ) {
823
824 $dates['m_start'] = 4;
825 $dates['m_end'] = 6;
826 $dates['year'] = date( 'Y', $current_time );
827
828 } elseif ( $month_now <= 9 ) {
829
830 $dates['m_start'] = 7;
831 $dates['m_end'] = 9;
832 $dates['year'] = date( 'Y', $current_time );
833
834 } else {
835
836 $dates['m_start'] = 10;
837 $dates['m_end'] = 12;
838 $dates['year'] = date( 'Y', $current_time );
839
840 }
841 break;
842
843 case 'last_quarter':
844 $month_now = date( 'n', $current_time );
845
846 $dates['day'] = null;
847
848 if ( $month_now <= 3 ) {
849
850 $dates['m_start'] = 10;
851 $dates['m_end'] = 12;
852 $dates['year'] = date( 'Y', $current_time ) - 1; // Previous year
853
854 } elseif ( $month_now <= 6 ) {
855
856 $dates['m_start'] = 1;
857 $dates['m_end'] = 3;
858 $dates['year'] = date( 'Y', $current_time );
859
860 } elseif ( $month_now <= 9 ) {
861
862 $dates['m_start'] = 4;
863 $dates['m_end'] = 6;
864 $dates['year'] = date( 'Y', $current_time );
865
866 } else {
867
868 $dates['m_start'] = 7;
869 $dates['m_end'] = 9;
870 $dates['year'] = date( 'Y', $current_time );
871
872 }
873 break;
874
875 case 'this_year':
876 $dates['day'] = null;
877 $dates['m_start'] = null;
878 $dates['m_end'] = null;
879 $dates['year'] = date( 'Y', $current_time );
880 break;
881
882 case 'last_year':
883 $dates['day'] = null;
884 $dates['m_start'] = null;
885 $dates['m_end'] = null;
886 $dates['year'] = date( 'Y', $current_time ) - 1;
887 break;
888
889 endswitch;
890 }// End if().
891
892 /**
893 * Returns the filters for the dates used to retrieve earnings.
894 *
895 * @since 1.2
896 *
897 * @param array $dates The dates used for retrieving earnings.
898 */
899 return apply_filters( 'give_api_stat_dates', $dates );
900 }
901
902 /**
903 * Process Get Donors API Request.
904 *
905 * @access public
906 * @since 1.1
907 * @global WPDB $wpdb Used to query the database using the WordPress Database API.
908 *
909 * @param int $donor Donor ID.
910 *
911 * @return array $donors Multidimensional array of the donors.
912 */
913 public function get_donors( $donor = null ) {
914
915 $donors = array();
916 $error = array();
917 if ( ! user_can( $this->user_id, 'view_give_sensitive_data' ) && ! $this->override ) {
918 return $donors;
919 }
920
921 $paged = $this->get_paged();
922 $per_page = $this->per_page();
923 $offset = $per_page * ( $paged - 1 );
924
925 if ( is_numeric( $donor ) ) {
926 $field = 'id';
927 } else {
928 $field = 'email';
929 }
930
931 $donor_query = Give()->donors->get_donors(
932 array(
933 'number' => $per_page,
934 'offset' => $offset,
935 $field => $donor,
936 )
937 );
938 $donor_count = 0;
939
940 if ( $donor_query ) {
941
942 foreach ( $donor_query as $donor_obj ) {
943
944 $names = explode( ' ', $donor_obj->name );
945 $first_name = ! empty( $names[0] ) ? $names[0] : '';
946 $last_name = '';
947 if ( ! empty( $names[1] ) ) {
948 unset( $names[0] );
949 $last_name = implode( ' ', $names );
950 }
951
952 $title_prefix = Give()->donor_meta->get_meta( $donor_obj->id, '_give_donor_title_prefix', true );
953
954 // Set title prefix empty, if not available in db.
955 if ( empty( $title_prefix ) ) {
956 $title_prefix = '';
957 }
958
959 $donors['donors'][ $donor_count ]['info']['user_id'] = '';
960 $donors['donors'][ $donor_count ]['info']['username'] = '';
961 $donors['donors'][ $donor_count ]['info']['display_name'] = '';
962 $donors['donors'][ $donor_count ]['info']['donor_id'] = $donor_obj->id;
963 $donors['donors'][ $donor_count ]['info']['title_prefix'] = $title_prefix;
964 $donors['donors'][ $donor_count ]['info']['first_name'] = $first_name;
965 $donors['donors'][ $donor_count ]['info']['last_name'] = $last_name;
966 $donors['donors'][ $donor_count ]['info']['email'] = $donor_obj->email;
967
968 if ( ! empty( $donor_obj->user_id ) ) {
969
970 $user_data = get_userdata( $donor_obj->user_id );
971
972 // Donor with registered account.
973 $donors['donors'][ $donor_count ]['info']['user_id'] = $donor_obj->user_id;
974 $donors['donors'][ $donor_count ]['info']['username'] = $user_data->user_login;
975 $donors['donors'][ $donor_count ]['info']['display_name'] = $user_data->display_name;
976
977 }
978
979 $donors['donors'][ $donor_count ]['stats']['total_donations'] = $donor_obj->purchase_count;
980 $donors['donors'][ $donor_count ]['stats']['total_spent'] = $donor_obj->purchase_value;
981
982 $donor = new Give_Donor( $donor_obj->id );
983
984 // Get donor's addresses.
985 $donors['donors'][ $donor_count ]['address'] = $donor->address;
986
987 $donor_count ++;
988
989 } // End foreach().
990 } elseif ( $donor ) {
991
992 $error['error'] = sprintf(
993 /* translators: %s: donor */
994 __( 'Donor %s not found.', 'give' ),
995 $donor
996 );
997
998 return $error;
999
1000 } else {
1001
1002 $error['error'] = __( 'No donors found.', 'give' );
1003
1004 return $error;
1005
1006 } // End if().
1007
1008 return $donors;
1009 }
1010
1011 /**
1012 * Process Get Donation Forms API Request
1013 *
1014 * @access public
1015 * @since 1.1
1016 *
1017 * @param int $form Give Form ID.
1018 *
1019 * @return array $donors Multidimensional array of the forms.
1020 */
1021 public function get_forms( $form = null ) {
1022
1023 $forms = array();
1024 $error = array();
1025
1026 if ( $form == null ) {
1027 $forms['forms'] = array();
1028
1029 $form_list = get_posts(
1030 array(
1031 'post_type' => 'give_forms',
1032 'posts_per_page' => $this->per_page(),
1033 'suppress_filters' => true,
1034 'paged' => $this->get_paged(),
1035 )
1036 );
1037
1038 if ( $form_list ) {
1039 $i = 0;
1040 foreach ( $form_list as $form_info ) {
1041 $forms['forms'][ $i ] = $this->get_form_data( $form_info );
1042 $i ++;
1043 }
1044 }
1045 } else {
1046 if ( get_post_type( $form ) == 'give_forms' ) {
1047 $form_info = get_post( $form );
1048
1049 $forms['forms'][0] = $this->get_form_data( $form_info );
1050
1051 } else {
1052 $error['error'] = sprintf( /* translators: %s: form */
1053 __( 'Form %s not found.', 'give' ),
1054 $form
1055 );
1056
1057 return $error;
1058 }
1059 }
1060
1061 return $forms;
1062 }
1063
1064 /**
1065 * Given a give_forms post object, generate the data for the API output
1066 *
1067 * @since 1.1
1068 *
1069 * @param object $form_info The Give Form's Post Object.
1070 *
1071 * @return array Array of post data to return back in the API.
1072 */
1073 private function get_form_data( $form_info ) {
1074
1075 $form = array();
1076 $currency = give_get_option( 'currency' );
1077
1078 $form['info']['id'] = $form_info->ID;
1079 $form['info']['slug'] = $form_info->post_name;
1080 $form['info']['title'] = $form_info->post_title;
1081 $form['info']['create_date'] = $form_info->post_date;
1082 $form['info']['modified_date'] = $form_info->post_modified;
1083 $form['info']['status'] = $form_info->post_status;
1084 $form['info']['link'] = html_entity_decode( $form_info->guid );
1085 $form['info']['content'] = give_get_meta( $form_info->ID, '_give_form_content', true );
1086 $form['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $form_info->ID ) );
1087
1088 if ( give_is_setting_enabled( give_get_option( 'categories', 'disabled' ) ) ) {
1089 $form['info']['category'] = get_the_terms( $form_info, 'give_forms_category' );
1090 $form['info']['tags'] = get_the_terms( $form_info, 'give_forms_tag' );
1091 }
1092 if ( give_is_setting_enabled( give_get_option( 'tags', 'disabled' ) ) ) {
1093 $form['info']['tags'] = get_the_terms( $form_info, 'give_forms_tag' );
1094 }
1095
1096 // Check whether any goal is to be achieved for the donation form.
1097 $goal_option = give_get_meta( $form_info->ID, '_give_goal_option', true );
1098 $goal_amount = give_get_meta( $form_info->ID, '_give_set_goal', true );
1099 if ( give_is_setting_enabled( $goal_option ) && $goal_amount ) {
1100 $total_income = give_get_form_earnings_stats( $form_info->ID );
1101 $goal_percentage_completed = ( $total_income < $goal_amount ) ? round( ( $total_income / $goal_amount ) * 100, 2 ) : 100;
1102 $form['goal']['amount'] = isset( $goal_amount ) ? give_format_decimal(
1103 array(
1104 'amount' => $goal_amount,
1105 'currency' => $currency,
1106 )
1107 ) : '';
1108 $form['goal']['percentage_completed'] = isset( $goal_percentage_completed ) ? $goal_percentage_completed : '';
1109 }
1110
1111 if ( user_can( $this->user_id, 'view_give_reports' ) || $this->override ) {
1112 $form['stats']['total']['donations'] = give_get_form_sales_stats( $form_info->ID );
1113 $form['stats']['total']['earnings'] = give_format_decimal(
1114 array(
1115 'amount' => give_get_form_earnings_stats( $form_info->ID ),
1116 'currency' => $currency,
1117 )
1118 );
1119 $form['stats']['monthly_average']['donations'] = give_get_average_monthly_form_sales( $form_info->ID );
1120 $form['stats']['monthly_average']['earnings'] = give_format_decimal(
1121 array(
1122 'amount' => give_get_average_monthly_form_earnings( $form_info->ID ),
1123 'currency' => $currency,
1124 )
1125 );
1126 }
1127
1128 $counter = 0;
1129 if ( give_has_variable_prices( $form_info->ID ) ) {
1130 foreach ( give_get_variable_prices( $form_info->ID ) as $price ) {
1131 $counter ++;
1132 // multi-level item
1133 $level = isset( $price['_give_text'] ) ? $price['_give_text'] : 'level-' . $counter;
1134 $form['pricing'][ sanitize_key( $level ) ] = give_format_decimal(
1135 array(
1136 'amount' => $price['_give_amount'],
1137 'currency' => $currency,
1138 )
1139 );
1140
1141 }
1142 } else {
1143 $form['pricing']['amount'] = give_format_decimal(
1144 array(
1145 'amount' => give_get_form_price( $form_info->ID ),
1146 'currency' => $currency,
1147 )
1148 );
1149 }
1150
1151 if ( user_can( $this->user_id, 'view_give_sensitive_data' ) || $this->override ) {
1152
1153 /**
1154 * Fires when generating API sensitive data.
1155 *
1156 * @since 1.1
1157 */
1158 do_action( 'give_api_sensitive_data' );
1159
1160 }
1161
1162 return apply_filters( 'give_api_forms_form', $form );
1163
1164 }
1165
1166 /**
1167 * Process Get Stats API Request
1168 *
1169 * @since 1.1
1170 *
1171 * @global WPDB $wpdb Used to query the database using the WordPress.
1172 *
1173 * @param array $args Arguments provided by API Request.
1174 *
1175 * @return array
1176 */
1177 public function get_stats( $args = array() ) {
1178 $defaults = array(
1179 'type' => null,
1180 'form' => null,
1181 'date' => null,
1182 'startdate' => null,
1183 'enddate' => null,
1184 );
1185
1186 $args = wp_parse_args( $args, $defaults );
1187
1188 $dates = $this->get_dates( $args );
1189
1190 $currency = give_get_option( 'currency' );
1191 $stats = array();
1192 $earnings = array(
1193 'earnings' => array(),
1194 );
1195 $donations = array(
1196 'donations' => array(),
1197 );
1198 $error = array();
1199
1200 if ( ! user_can( $this->user_id, 'view_give_reports' ) && ! $this->override ) {
1201 return $stats;
1202 }
1203
1204 if ( $args['type'] == 'donations' ) {
1205
1206 if ( $args['form'] == null ) {
1207 if ( $args['date'] == null ) {
1208 $donations = $this->get_default_sales_stats();
1209 } elseif ( $args['date'] === 'range' ) {
1210 // Return donations for a date range.
1211 // Ensure the end date is later than the start date.
1212 if ( $args['enddate'] < $args['startdate'] ) {
1213 $error['error'] = __( 'The end date must be later than the start date.', 'give' );
1214 }
1215
1216 // Ensure both the start and end date are specified
1217 if ( empty( $args['startdate'] ) || empty( $args['enddate'] ) ) {
1218 $error['error'] = __( 'Invalid or no date range specified.', 'give' );
1219 }
1220
1221 $total = 0;
1222
1223 // Loop through the years
1224 $y = $dates['year'];
1225 while ( $y <= $dates['year_end'] ) :
1226
1227 if ( $dates['year'] == $dates['year_end'] ) {
1228 $month_start = $dates['m_start'];
1229 $month_end = $dates['m_end'];
1230 } elseif ( $y == $dates['year'] && $dates['year_end'] > $dates['year'] ) {
1231 $month_start = $dates['m_start'];
1232 $month_end = 12;
1233 } elseif ( $y == $dates['year_end'] ) {
1234 $month_start = 1;
1235 $month_end = $dates['m_end'];
1236 } else {
1237 $month_start = 1;
1238 $month_end = 12;
1239 }
1240
1241 $i = $month_start;
1242 while ( $i <= $month_end ) :
1243
1244 if ( $i == $dates['m_start'] ) {
1245 $d = $dates['day_start'];
1246 } else {
1247 $d = 1;
1248 }
1249
1250 if ( $i == $dates['m_end'] ) {
1251 $num_of_days = $dates['day_end'];
1252 } else {
1253 $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
1254 }
1255
1256 while ( $d <= $num_of_days ) :
1257 $sale_count = give_get_sales_by_date( $d, $i, $y );
1258 $date_key = date( 'Ymd', strtotime( $y . '/' . $i . '/' . $d ) );
1259 if ( ! isset( $donations['sales'][ $date_key ] ) ) {
1260 $donations['sales'][ $date_key ] = 0;
1261 }
1262 $donations['sales'][ $date_key ] += $sale_count;
1263 $total += $sale_count;
1264 $d ++;
1265 endwhile;
1266 $i ++;
1267 endwhile;
1268
1269 $y ++;
1270 endwhile;
1271
1272 $donations['totals'] = $total;
1273 } else {
1274 if ( $args['date'] == 'this_quarter' || $args['date'] == 'last_quarter' ) {
1275 $donations_count = 0;
1276
1277 // Loop through the months
1278 $month = $dates['m_start'];
1279
1280 while ( $month <= $dates['m_end'] ) :
1281 $donations_count += give_get_sales_by_date( null, $month, $dates['year'] );
1282 $month ++;
1283 endwhile;
1284
1285 $donations['donations'][ $args['date'] ] = $donations_count;
1286 } else {
1287 $donations['donations'][ $args['date'] ] = give_get_sales_by_date( $dates['day'], $dates['m_start'], $dates['year'] );
1288 }
1289 }// End if().
1290 } elseif ( $args['form'] == 'all' ) {
1291 $forms = get_posts(
1292 array(
1293 'post_type' => 'give_forms',
1294 'nopaging' => true,
1295 )
1296 );
1297 $i = 0;
1298 foreach ( $forms as $form_info ) {
1299 $donations['donations'][ $i ] = array(
1300 $form_info->post_name => $this->stats->get_sales(
1301 $form_info->ID,
1302 is_numeric( $args['startdate'] )
1303 ? strtotime( $args['startdate'] )
1304 : $args['startdate'],
1305 is_numeric( $args['enddate'] )
1306 ? strtotime( $args['enddate'] )
1307 : $args['enddate']
1308 ),
1309 );
1310 $i ++;
1311 }
1312 } else {
1313 if ( get_post_type( $args['form'] ) == 'give_forms' ) {
1314 $form_info = get_post( $args['form'] );
1315 $donations['donations'][0] = array(
1316 $form_info->post_name => $this->stats->get_sales(
1317 $args['form'],
1318 is_numeric( $args['startdate'] )
1319 ? strtotime( $args['startdate'] )
1320 : $args['startdate'],
1321 is_numeric( $args['enddate'] )
1322 ? strtotime( $args['enddate'] )
1323 : $args['enddate']
1324 ),
1325 );
1326 } else {
1327 $error['error'] = sprintf( /* translators: %s: form */
1328 __( 'Form %s not found.', 'give' ),
1329 $args['form']
1330 );
1331 }
1332 }// End if().
1333
1334 if ( ! empty( $error ) ) {
1335 return $error;
1336 }
1337
1338 return $donations;
1339
1340 } elseif ( $args['type'] == 'earnings' ) {
1341 if ( $args['form'] == null ) {
1342 if ( $args['date'] == null ) {
1343 $earnings = $this->get_default_earnings_stats();
1344 } elseif ( $args['date'] === 'range' ) {
1345 // Return sales for a date range
1346 // Ensure the end date is later than the start date
1347 if ( $args['enddate'] < $args['startdate'] ) {
1348 $error['error'] = __( 'The end date must be later than the start date.', 'give' );
1349 }
1350
1351 // Ensure both the start and end date are specified
1352 if ( empty( $args['startdate'] ) || empty( $args['enddate'] ) ) {
1353 $error['error'] = __( 'Invalid or no date range specified.', 'give' );
1354 }
1355
1356 $total = (float) 0.00;
1357
1358 // Loop through the years
1359 $y = $dates['year'];
1360 if ( ! isset( $earnings['earnings'] ) ) {
1361 $earnings['earnings'] = array();
1362 }
1363 while ( $y <= $dates['year_end'] ) :
1364
1365 if ( $dates['year'] == $dates['year_end'] ) {
1366 $month_start = $dates['m_start'];
1367 $month_end = $dates['m_end'];
1368 } elseif ( $y == $dates['year'] && $dates['year_end'] > $dates['year'] ) {
1369 $month_start = $dates['m_start'];
1370 $month_end = 12;
1371 } elseif ( $y == $dates['year_end'] ) {
1372 $month_start = 1;
1373 $month_end = $dates['m_end'];
1374 } else {
1375 $month_start = 1;
1376 $month_end = 12;
1377 }
1378
1379 $i = $month_start;
1380 while ( $i <= $month_end ) :
1381
1382 if ( $i == $dates['m_start'] ) {
1383 $d = $dates['day_start'];
1384 } else {
1385 $d = 1;
1386 }
1387
1388 if ( $i == $dates['m_end'] ) {
1389 $num_of_days = $dates['day_end'];
1390 } else {
1391 $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y );
1392 }
1393
1394 while ( $d <= $num_of_days ) :
1395 $earnings_stat = give_get_earnings_by_date( $d, $i, $y );
1396 $date_key = date( 'Ymd', strtotime( $y . '/' . $i . '/' . $d ) );
1397 if ( ! isset( $earnings['earnings'][ $date_key ] ) ) {
1398 $earnings['earnings'][ $date_key ] = 0;
1399 }
1400
1401 $earnings['earnings'][ $date_key ] += give_format_decimal(
1402 array(
1403 'amount' => $earnings_stat,
1404 'currency' => $currency,
1405 )
1406 );
1407 $total += $earnings_stat;
1408 $d ++;
1409 endwhile;
1410
1411 $i ++;
1412 endwhile;
1413
1414 $y ++;
1415 endwhile;
1416
1417 $earnings['totals'] = give_format_decimal(
1418 array(
1419 'amount' => $total,
1420 'currency' => $currency,
1421 )
1422 );
1423 } else {
1424 if ( $args['date'] == 'this_quarter' || $args['date'] == 'last_quarter' ) {
1425 $earnings_count = (float) 0.00;
1426
1427 // Loop through the months
1428 $month = $dates['m_start'];
1429
1430 while ( $month <= $dates['m_end'] ) :
1431 $earnings_count += give_get_earnings_by_date( null, $month, $dates['year'] );
1432 $month ++;
1433 endwhile;
1434
1435 $earnings['earnings'][ $args['date'] ] = give_format_decimal(
1436 array(
1437 'amount' => $earnings_count,
1438 'currency' => $currency,
1439 )
1440 );
1441 } else {
1442 $earnings['earnings'][ $args['date'] ] = give_format_decimal(
1443 array(
1444 'amount' => give_get_earnings_by_date( $dates['day'], $dates['m_start'], $dates['year'] ),
1445 'currency' => $currency,
1446 )
1447 );
1448 }
1449 }// End if().
1450 } elseif ( $args['form'] == 'all' ) {
1451 $forms = get_posts(
1452 array(
1453 'post_type' => 'give_forms',
1454 'nopaging' => true,
1455 )
1456 );
1457
1458 $i = 0;
1459 foreach ( $forms as $form_info ) {
1460 $earnings['earnings'][ $i ] = array(
1461 $form_info->post_name => give_format_decimal(
1462 array(
1463 'amount' => give_get_form_earnings_stats( $form_info->ID ),
1464 'currency' => $currency,
1465 )
1466 ),
1467 );
1468 $i ++;
1469 }
1470 } else {
1471 if ( get_post_type( $args['form'] ) == 'give_forms' ) {
1472 $form_info = get_post( $args['form'] );
1473 $earnings['earnings'][0] = array(
1474 $form_info->post_name => give_format_decimal(
1475 array(
1476 'amount' => $this->stats->get_earnings(
1477 $args['form'],
1478 is_numeric( $args['startdate'] )
1479 ? strtotime( $args['startdate'] )
1480 : $args['startdate'],
1481 is_numeric( $args['enddate'] )
1482 ? strtotime( $args['enddate'] )
1483 : $args['enddate']
1484 ),
1485 'currency' => $currency,
1486 )
1487 ),
1488 );
1489 } else {
1490 $error['error'] = sprintf( /* translators: %s: form */
1491 __( 'Form %s not found.', 'give' ),
1492 $args['form']
1493 );
1494 }
1495 }// End if().
1496
1497 if ( ! empty( $error ) ) {
1498 return $error;
1499 }
1500
1501 return $earnings;
1502 } elseif ( $args['type'] == 'donors' ) {
1503 $donors = new Give_DB_Donors();
1504 $stats['donations']['total_donors'] = $donors->count();
1505
1506 return $stats;
1507
1508 } elseif ( empty( $args['type'] ) ) {
1509 $stats = array_merge( $stats, $this->get_default_sales_stats() );
1510 $stats = array_merge( $stats, $this->get_default_earnings_stats() );
1511
1512 return array(
1513 'stats' => $stats,
1514 );
1515 }// End if().
1516 }
1517
1518 /**
1519 * Retrieves Recent Donations
1520 *
1521 * @access public
1522 * @since 1.1
1523 *
1524 * @param $args array
1525 *
1526 * @return array
1527 */
1528 public function get_recent_donations( $args = array() ) {
1529 global $wp_query;
1530
1531 $defaults = array(
1532 'id' => null,
1533 'date' => null,
1534 'startdate' => null,
1535 'enddate' => null,
1536 );
1537
1538 $args = wp_parse_args( $args, $defaults );
1539
1540 $donations = array();
1541
1542 if ( ! user_can( $this->user_id, 'view_give_reports' ) && ! $this->override ) {
1543 return $donations;
1544 }
1545
1546 if ( isset( $wp_query->query_vars['id'] ) ) {
1547 $query = array();
1548 $query[] = new Give_Payment( $wp_query->query_vars['id'] );
1549 } elseif ( isset( $wp_query->query_vars['purchasekey'] ) ) {
1550 $query = array();
1551 $query[] = give_get_payment_by( 'key', $wp_query->query_vars['purchasekey'] );
1552 } elseif ( isset( $wp_query->query_vars['email'] ) ) {
1553 $args = array(
1554 'fields' => 'ids',
1555 'meta_key' => '_give_payment_donor_email',
1556 'meta_value' => $wp_query->query_vars['email'],
1557 'number' => $this->per_page(),
1558 'page' => $this->get_paged(),
1559 );
1560 $query = give_get_payments( $args );
1561 } elseif ( isset( $wp_query->query_vars['date'] ) ) {
1562
1563 $current_time = current_time( 'timestamp' );
1564 $dates = $this->get_dates( $args );
1565 $start_date = '';
1566 $end_date = '';
1567
1568 /**
1569 * Switch case for date query argument
1570 *
1571 * @since 1.8.8
1572 *
1573 * @params text date | today, yesterday or range
1574 * @params date startdate | required when date = range and format to be YYYYMMDD (i.e. 20170524)
1575 * @params date enddate | required when date = range and format to be YYYYMMDD (i.e. 20170524)
1576 */
1577 switch ( $wp_query->query_vars['date'] ) {
1578
1579 case 'today':
1580 // Set and Format Start and End Date to be date of today.
1581 $start_date = $end_date = date( 'Y/m/d', $current_time );
1582
1583 break;
1584
1585 case 'yesterday':
1586 // Set and Format Start and End Date to be date of yesterday.
1587 $start_date = $end_date = date( 'Y/m', $current_time ) . '/' . ( date( 'd', $current_time ) - 1 );
1588
1589 break;
1590
1591 case 'range':
1592 // Format Start Date and End Date for filtering payment based on date range.
1593 $start_date = $dates['year'] . '/' . $dates['m_start'] . '/' . $dates['day_start'];
1594 $end_date = $dates['year_end'] . '/' . $dates['m_end'] . '/' . $dates['day_end'];
1595
1596 break;
1597
1598 }
1599
1600 $args = array(
1601 'fields' => 'ids',
1602 'start_date' => $start_date,
1603 'end_date' => $end_date,
1604 'number' => $this->per_page(),
1605 'page' => $this->get_paged(),
1606 );
1607
1608 $query = give_get_payments( $args );
1609 } else {
1610 $args = array(
1611 'fields' => 'ids',
1612 'number' => $this->per_page(),
1613 'page' => $this->get_paged(),
1614 );
1615 $query = give_get_payments( $args );
1616 }// End if().
1617
1618 if ( $query ) {
1619 $i = 0;
1620 foreach ( $query as $payment ) {
1621
1622 if ( is_numeric( $payment ) ) {
1623 $payment = new Give_Payment( $payment );
1624 }
1625
1626 $payment_meta = $payment->get_meta();
1627 $user_info = $payment->user_info;
1628
1629 $first_name = isset( $user_info['first_name'] ) ? $user_info['first_name'] : '';
1630 $last_name = isset( $user_info['last_name'] ) ? $user_info['last_name'] : '';
1631
1632 $donations['donations'][ $i ]['ID'] = $payment->ID;
1633 $donations['donations'][ $i ]['number'] = $payment->number;
1634 $donations['donations'][ $i ]['transaction_id'] = $payment->transaction_id;
1635 $donations['donations'][ $i ]['key'] = $payment->key;
1636 $donations['donations'][ $i ]['total'] = give_format_decimal(
1637 array(
1638 'donation_id' => $payment->ID,
1639 'dp' => true,
1640 )
1641 );
1642 $donations['donations'][ $i ]['status'] = give_get_payment_status( $payment, true );
1643 $donations['donations'][ $i ]['gateway'] = $payment->gateway;
1644 $donations['donations'][ $i ]['name'] = trim( "{$first_name} {$last_name}" );
1645 $donations['donations'][ $i ]['fname'] = $first_name;
1646 $donations['donations'][ $i ]['lname'] = $last_name;
1647 $donations['donations'][ $i ]['email'] = $payment->email;
1648 $donations['donations'][ $i ]['date'] = $payment->date;
1649 $donations['donations'][ $i ]['payment_meta'] = array();
1650
1651 $form_id = isset( $payment_meta['form_id'] ) ? $payment_meta['form_id'] : $payment_meta;
1652 $price = isset( $payment_meta['form_id'] ) ? give_get_form_price( $payment_meta['form_id'] ) : false;
1653 $price_id = isset( $payment_meta['price_id'] ) ? $payment_meta['price_id'] : null;
1654
1655 $donations['donations'][ $i ]['form']['id'] = $form_id;
1656 $donations['donations'][ $i ]['form']['name'] = get_the_title( $payment_meta['form_id'] );
1657 $donations['donations'][ $i ]['form']['price'] = give_format_decimal(
1658 array(
1659 'amount' => $price,
1660 'currency' => give_get_option( 'currency' ),
1661 'dp' => true,
1662 )
1663 );
1664
1665 if ( give_has_variable_prices( $form_id ) ) {
1666 if ( isset( $payment_meta['price_id'] ) ) {
1667 $price_name = give_get_price_option_name( $form_id, $payment_meta['price_id'], $payment->ID );
1668 $donations['donations'][ $i ]['form']['price_name'] = $price_name;
1669 $donations['donations'][ $i ]['form']['price_id'] = $price_id;
1670 $donations['donations'][ $i ]['form']['price'] = give_format_decimal(
1671 array(
1672 'amount' => give_get_price_option_amount( $form_id, $price_id ),
1673 'currency' => give_get_option( 'currency' ),
1674 'dp' => true,
1675 )
1676 );
1677 }
1678 }
1679
1680 if ( ! empty( $payment_meta ) ) {
1681 // Add custom meta to API
1682 foreach ( $payment_meta as $meta_key => $meta_value ) {
1683
1684 $exceptions = array(
1685 '_give_payment_form_title',
1686 'form_title',
1687 '_give_payment_form_id',
1688 'form_id',
1689 '_give_payment_price_id',
1690 'price_id',
1691 'user_info',
1692 '_give_payment_purchase_key',
1693 'key',
1694 'email',
1695 'date',
1696 'currency',
1697 '_give_payment_total',
1698 '_give_payment_date',
1699 );
1700
1701 // Don't clutter up results with dupes
1702 if ( ! is_string( $meta_value ) || in_array( $meta_key, $exceptions ) ) {
1703 continue;
1704 }
1705
1706 // Meta key can contain price value like _give_fee_amount, so convert them to standard format.
1707 if ( give_is_amount_sanitized( $meta_value ) ) {
1708 $meta_value = give_format_decimal(
1709 array(
1710 'amount' => $meta_value,
1711 'currency' => give_get_option( 'currency' ),
1712 'dp' => true,
1713 )
1714 );
1715 }
1716
1717 $donations['donations'][ $i ]['payment_meta'][ $meta_key ] = $meta_value;
1718
1719 }
1720 }
1721
1722 $i ++;
1723 }// End foreach().
1724 }// End if().
1725
1726 return apply_filters( 'give_api_donations_endpoint', $donations );
1727 }
1728
1729 /**
1730 * Retrieve the output format.
1731 *
1732 * Determines whether results should be displayed in XML or JSON.
1733 *
1734 * @since 1.1
1735 * @access public
1736 *
1737 * @return mixed
1738 */
1739 public function get_output_format() {
1740 global $wp_query;
1741
1742 $format = isset( $wp_query->query_vars['format'] ) ? $wp_query->query_vars['format'] : 'json';
1743
1744 return apply_filters( 'give_api_output_format', $format );
1745 }
1746
1747
1748 /**
1749 * Log each API request, if enabled.
1750 *
1751 * @access private
1752 * @since 1.1
1753 *
1754 * @global WP_Query $wp_query
1755 *
1756 * @param array $data
1757 *
1758 * @return void
1759 */
1760 private function log_request( $data = array() ) {
1761 if ( ! $this->log_requests ) {
1762 return;
1763 }
1764
1765 /**
1766 * @var WP_Query $wp_query
1767 */
1768 global $wp_query;
1769
1770 $query = array(
1771 'give-api' => $wp_query->query_vars['give-api'],
1772 'key' => isset( $wp_query->query_vars['key'] ) ? $wp_query->query_vars['key'] : null,
1773 'token' => isset( $wp_query->query_vars['token'] ) ? $wp_query->query_vars['token'] : null,
1774 'query' => isset( $wp_query->query_vars['query'] ) ? $wp_query->query_vars['query'] : null,
1775 'type' => isset( $wp_query->query_vars['type'] ) ? $wp_query->query_vars['type'] : null,
1776 'form' => isset( $wp_query->query_vars['form'] ) ? $wp_query->query_vars['form'] : null,
1777 'donor' => isset( $wp_query->query_vars['donor'] ) ? $wp_query->query_vars['donor'] : null,
1778 'date' => isset( $wp_query->query_vars['date'] ) ? $wp_query->query_vars['date'] : null,
1779 'startdate' => isset( $wp_query->query_vars['startdate'] ) ? $wp_query->query_vars['startdate'] : null,
1780 'enddate' => isset( $wp_query->query_vars['enddate'] ) ? $wp_query->query_vars['enddate'] : null,
1781 'id' => isset( $wp_query->query_vars['id'] ) ? $wp_query->query_vars['id'] : null,
1782 'purchasekey' => isset( $wp_query->query_vars['purchasekey'] ) ? $wp_query->query_vars['purchasekey'] : null,
1783 'email' => isset( $wp_query->query_vars['email'] ) ? $wp_query->query_vars['email'] : null,
1784 );
1785
1786 $log_data = array(
1787 'log_type' => 'api_request',
1788 'post_excerpt' => http_build_query( $query ),
1789 'post_content' => ! empty( $data['error'] ) ? $data['error'] : '',
1790 );
1791
1792 $log_meta = array(
1793 'api_query' => http_build_query( $query ),
1794 'request_ip' => give_get_ip(),
1795 'user' => $this->user_id,
1796 'key' => isset( $wp_query->query_vars['key'] ) ? $wp_query->query_vars['key'] : null,
1797 'token' => isset( $wp_query->query_vars['token'] ) ? $wp_query->query_vars['token'] : null,
1798 'time' => $data['request_speed'],
1799 'version' => $this->get_queried_version(),
1800 );
1801
1802 Give()->logs->insert_log( $log_data, $log_meta );
1803 }
1804
1805
1806 /**
1807 * Retrieve the output data.
1808 *
1809 * @access public
1810 * @since 1.1
1811 * @return array
1812 */
1813 public function get_output() {
1814 return $this->data;
1815 }
1816
1817 /**
1818 * Output Query in either JSON/XML.
1819 * The query data is outputted as JSON by default.
1820 *
1821 * @since 1.1
1822 * @global WP_Query $wp_query
1823 *
1824 * @param int $status_code
1825 */
1826 public function output( $status_code = 200 ) {
1827
1828 $format = $this->get_output_format();
1829
1830 status_header( $status_code );
1831
1832 /**
1833 * Fires before outputting the API.
1834 *
1835 * @since 1.1
1836 *
1837 * @param array $data Response data to return.
1838 * @param Give_API $this The Give_API object.
1839 * @param string $format Output format, XML or JSON. Default is JSON.
1840 */
1841 do_action( 'give_api_output_before', $this->data, $this, $format );
1842
1843 switch ( $format ) :
1844
1845 case 'xml':
1846 require_once GIVE_PLUGIN_DIR . 'includes/libraries/array2xml.php';
1847 $xml = Array2XML::createXML( 'give', $this->data );
1848 echo $xml->saveXML();
1849
1850 break;
1851
1852 case 'json':
1853 header( 'Content-Type: application/json' );
1854 if ( ! empty( $this->pretty_print ) ) {
1855 echo json_encode( $this->data, $this->pretty_print );
1856 } else {
1857 echo json_encode( $this->data );
1858 }
1859
1860 break;
1861
1862 default:
1863 /**
1864 * Fires by the API while outputting other formats.
1865 *
1866 * @since 1.1
1867 *
1868 * @param array $data Response data to return.
1869 * @param Give_API $this The Give_API object.
1870 */
1871 do_action( "give_api_output_{$format}", $this->data, $this );
1872
1873 break;
1874
1875 endswitch;
1876
1877 /**
1878 * Fires after outputting the API.
1879 *
1880 * @since 1.1
1881 *
1882 * @param array $data Response data to return.
1883 * @param Give_API $this The Give_API object.
1884 * @param string $format Output format, XML or JSON. Default is JSON.
1885 */
1886 do_action( 'give_api_output_after', $this->data, $this, $format );
1887
1888 give_die();
1889 }
1890
1891 /**
1892 * Modify User Profile
1893 *
1894 * Modifies the output of profile.php to add key generation/revocation.
1895 *
1896 * @access public
1897 * @since 1.1
1898 *
1899 * @param object $user Current user info
1900 *
1901 * @return void
1902 */
1903 function user_key_field( $user ) {
1904
1905 if ( ( give_get_option( 'api_allow_user_keys', false ) || current_user_can( 'manage_give_settings' ) ) && current_user_can( 'edit_user', $user->ID ) ) {
1906
1907 $user = get_userdata( $user->ID );
1908 ?>
1909 <table class="form-table">
1910 <tbody>
1911 <tr>
1912 <th>
1913 <?php _e( 'GiveWP API Keys', 'give' ); ?>
1914 </th>
1915 <td>
1916 <?php
1917 $public_key = $this->get_user_public_key( $user->ID );
1918 $secret_key = $this->get_user_secret_key( $user->ID );
1919 ?>
1920 <?php if ( empty( $user->give_user_public_key ) ) { ?>
1921 <input name="give_set_api_key" type="checkbox" id="give_set_api_key" />
1922 <span class="description"><label for="give_set_api_key"><?php _e( 'Generate API Key', 'give' ); ?></label></span>
1923 <?php } else { ?>
1924 <strong style="display:inline-block; width: 125px;"><?php _e( 'Public key:', 'give' ); ?>
1925 &nbsp;</strong>
1926 <input type="text" disabled="disabled" class="regular-text" id="publickey" value="<?php echo esc_attr( $public_key ); ?>" />
1927 <br />
1928 <strong style="display:inline-block; width: 125px;"><?php _e( 'Secret key:', 'give' ); ?>
1929 &nbsp;</strong>
1930 <input type="text" disabled="disabled" class="regular-text" id="privatekey" value="<?php echo esc_attr( $secret_key ); ?>" />
1931 <br />
1932 <strong style="display:inline-block; width: 125px;"><?php _e( 'Token:', 'give' ); ?>
1933 &nbsp;</strong>
1934 <input type="text" disabled="disabled" class="regular-text" id="token" value="<?php echo esc_attr( $this->get_token( $user->ID ) ); ?>" />
1935 <br />
1936 <input name="give_revoke_api_key" type="checkbox" id="give_revoke_api_key" />
1937 <span class="description"><label for="give_revoke_api_key"><?php _e( 'Revoke API Keys', 'give' ); ?></label></span>
1938 <?php } ?>
1939 </td>
1940 </tr>
1941 </tbody>
1942 </table>
1943 <?php
1944 }// End if().
1945 }
1946
1947 /**
1948 * Process an API key generation/revocation
1949 *
1950 * @access public
1951 * @since 1.1
1952 *
1953 * @param array $args
1954 *
1955 * @return void
1956 */
1957 public function process_api_key( $args ) {
1958
1959 if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'give-api-nonce' ) ) {
1960 wp_die(
1961 __( 'We\'re unable to recognize your session. Please refresh the screen to try again; otherwise contact your website administrator for assistance.', 'give' ),
1962 __( 'Error', 'give' ),
1963 array(
1964 'response' => 403,
1965 )
1966 );
1967 }
1968
1969 if ( empty( $args['user_id'] ) ) {
1970 wp_die(
1971 __( 'User ID Required.', 'give' ),
1972 __( 'Error', 'give' ),
1973 array(
1974 'response' => 401,
1975 )
1976 );
1977 }
1978
1979 if ( is_numeric( $args['user_id'] ) ) {
1980 $user_id = isset( $args['user_id'] ) ? absint( $args['user_id'] ) : get_current_user_id();
1981 } else {
1982 $userdata = get_user_by( 'login', $args['user_id'] );
1983 $user_id = $userdata->ID;
1984 }
1985 $process = isset( $args['give_api_process'] ) ? strtolower( $args['give_api_process'] ) : false;
1986
1987 if ( $user_id == get_current_user_id() && ! give_get_option( 'allow_user_api_keys' ) && ! current_user_can( 'manage_give_settings' ) ) {
1988 wp_die(
1989 sprintf( /* translators: %s: process */
1990 __( 'You do not have permission to %s API keys for this user.', 'give' ),
1991 $process
1992 ),
1993 __( 'Error', 'give' ),
1994 array(
1995 'response' => 403,
1996 )
1997 );
1998 } elseif ( ! current_user_can( 'manage_give_settings' ) ) {
1999 wp_die(
2000 sprintf( /* translators: %s: process */
2001 __( 'You do not have permission to %s API keys for this user.', 'give' ),
2002 $process
2003 ),
2004 __( 'Error', 'give' ),
2005 array(
2006 'response' => 403,
2007 )
2008 );
2009 }
2010
2011 switch ( $process ) {
2012 case 'generate':
2013 if ( $this->generate_api_key( $user_id ) ) {
2014 Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) );
2015 wp_redirect( esc_url_raw( add_query_arg( 'give-messages[]', 'api-key-generated', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ) );
2016 exit();
2017 } else {
2018 wp_redirect( esc_url_raw( add_query_arg( 'give-messages[]', 'api-key-failed', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ) );
2019 exit();
2020 }
2021 break;
2022 case 'regenerate':
2023 $this->generate_api_key( $user_id, true );
2024 Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) );
2025 wp_redirect( esc_url_raw( add_query_arg( 'give-messages[]', 'api-key-regenerated', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ) );
2026 exit();
2027 break;
2028 case 'revoke':
2029 $this->revoke_api_key( $user_id );
2030 Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) );
2031 wp_redirect( esc_url_raw( add_query_arg( 'give-messages[]', 'api-key-revoked', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ) );
2032 exit();
2033 break;
2034 default;
2035 break;
2036 }
2037 }
2038
2039 /**
2040 * Generate new API keys for a user
2041 *
2042 * @param int $user_id User ID the key is being generated for.
2043 * @param boolean $regenerate Regenerate the key for the user.
2044 *
2045 * @access public
2046 * @since 1.1
2047 *
2048 * @return boolean True if (re)generated successfully, false otherwise.
2049 */
2050 public function generate_api_key( $user_id = 0, $regenerate = false ) {
2051
2052 // Bail out, if user doesn't exists.
2053 if ( empty( $user_id ) ) {
2054 return false;
2055 }
2056
2057 $user = get_userdata( $user_id );
2058
2059 // Bail Out, if user object doesn't exists.
2060 if ( ! $user ) {
2061 return false;
2062 }
2063
2064 $new_public_key = '';
2065 $new_secret_key = '';
2066
2067 if ( ! empty( $_POST['from'] ) && 'profile' === $_POST['from'] ) {
2068 // For User Profile Page.
2069 if ( ! empty( $_POST['give_set_api_key'] ) ) {
2070 // Generate API Key from User Profile page.
2071 $new_public_key = $this->generate_public_key( $user->user_email );
2072 $new_secret_key = $this->generate_private_key( $user->ID );
2073 } elseif ( ! empty( $_POST['give_revoke_api_key'] ) ) {
2074 // Revoke API Key from User Profile page.
2075 $this->revoke_api_key( $user->ID );
2076 } else {
2077 return false;
2078 }
2079 } else {
2080 // For Tools > API page.
2081 $public_key = $this->get_user_public_key( $user_id );
2082
2083 if ( empty( $public_key ) && ! $regenerate ) {
2084 // Generating API for first time.
2085 $new_public_key = $this->generate_public_key( $user->user_email );
2086 $new_secret_key = $this->generate_private_key( $user->ID );
2087 } elseif ( $public_key && $regenerate ) {
2088 // API Key already exists and Regenerating API Key.
2089 $this->revoke_api_key( $user->ID );
2090 $new_public_key = $this->generate_public_key( $user->user_email );
2091 $new_secret_key = $this->generate_private_key( $user->ID );
2092 } elseif ( ! empty( $public_key ) && ! $regenerate ) {
2093 // Doing nothing, when API Key exists but still try to generate again instead of regenerating.
2094 return false;
2095 } else {
2096 // Revoke API Key.
2097 $this->revoke_api_key( $user->ID );
2098 }
2099 }
2100
2101 update_user_meta( $user_id, $new_public_key, 'give_user_public_key' );
2102 update_user_meta( $user_id, $new_secret_key, 'give_user_secret_key' );
2103
2104 return true;
2105 }
2106
2107 /**
2108 * Revoke a users API keys
2109 *
2110 * @access public
2111 * @since 1.1
2112 *
2113 * @param int $user_id User ID of user to revoke key for
2114 *
2115 * @return bool
2116 */
2117 public function revoke_api_key( $user_id = 0 ) {
2118
2119 if ( empty( $user_id ) ) {
2120 return false;
2121 }
2122
2123 $user = get_userdata( $user_id );
2124
2125 if ( ! $user ) {
2126 return false;
2127 }
2128
2129 $public_key = $this->get_user_public_key( $user_id );
2130 $secret_key = $this->get_user_secret_key( $user_id );
2131 if ( ! empty( $public_key ) ) {
2132 Give_Cache::delete( Give_Cache::get_key( md5( 'give_api_user_' . $public_key ) ) );
2133 Give_Cache::delete( Give_Cache::get_key( md5( 'give_api_user_public_key' . $user_id ) ) );
2134 Give_Cache::delete( Give_Cache::get_key( md5( 'give_api_user_secret_key' . $user_id ) ) );
2135 delete_user_meta( $user_id, $public_key );
2136 delete_user_meta( $user_id, $secret_key );
2137 } else {
2138 return false;
2139 }
2140
2141 return true;
2142 }
2143
2144 public function get_version() {
2145 return self::VERSION;
2146 }
2147
2148 /**
2149 * Generate the public key for a user
2150 *
2151 * @access private
2152 * @since 1.1
2153 *
2154 * @param string $user_email
2155 *
2156 * @return string
2157 */
2158 private function generate_public_key( $user_email = '' ) {
2159 $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
2160 $public = hash( 'md5', $user_email . $auth_key . date( 'U' ) );
2161
2162 return $public;
2163 }
2164
2165 /**
2166 * Generate the secret key for a user
2167 *
2168 * @access private
2169 * @since 1.1
2170 *
2171 * @param int $user_id
2172 *
2173 * @return string
2174 */
2175 private function generate_private_key( $user_id = 0 ) {
2176 $auth_key = defined( 'AUTH_KEY' ) ? AUTH_KEY : '';
2177 $secret = hash( 'md5', $user_id . $auth_key . date( 'U' ) );
2178
2179 return $secret;
2180 }
2181
2182 /**
2183 * Retrieve the user's token
2184 *
2185 * @access private
2186 * @since 1.1
2187 *
2188 * @param int $user_id
2189 *
2190 * @return string
2191 */
2192 public function get_token( $user_id = 0 ) {
2193 return hash( 'md5', $this->get_user_secret_key( $user_id ) . $this->get_user_public_key( $user_id ) );
2194 }
2195
2196 /**
2197 * Generate the default donation stats returned by the 'stats' endpoint
2198 *
2199 * @access private
2200 * @since 1.1
2201 * @return array default sales statistics
2202 */
2203 private function get_default_sales_stats() {
2204
2205 // Default sales return
2206 $donations = array();
2207 $donations['donations']['today'] = $this->stats->get_sales( 0, 'today' );
2208 $donations['donations']['current_month'] = $this->stats->get_sales( 0, 'this_month' );
2209 $donations['donations']['last_month'] = $this->stats->get_sales( 0, 'last_month' );
2210 $donations['donations']['totals'] = give_get_total_donations();
2211
2212 return $donations;
2213 }
2214
2215 /**
2216 * Generate the default earnings stats returned by the 'stats' endpoint
2217 *
2218 * @access private
2219 * @return array default earnings statistics
2220 * @since 1.1
2221 */
2222 private function get_default_earnings_stats() {
2223 $currency = give_get_option( 'currency' );
2224
2225 // Default earnings return
2226 $earnings = array();
2227 $earnings['earnings']['today'] = give_format_decimal(
2228 array(
2229 'amount' => $this->stats->get_earnings( 0, 'today' ),
2230 'currency' => $currency,
2231 )
2232 );
2233 $earnings['earnings']['current_month'] = give_format_decimal(
2234 array(
2235 'amount' => $this->stats->get_earnings( 0, 'this_month' ),
2236 'currency' => $currency,
2237 )
2238 );
2239 $earnings['earnings']['last_month'] = give_format_decimal(
2240 array(
2241 'amount' => $this->stats->get_earnings( 0, 'last_month' ),
2242 'currency' => $currency,
2243 )
2244 );
2245 $earnings['earnings']['totals'] = give_format_decimal(
2246 array(
2247 'amount' => give_get_total_earnings(),
2248 'currency' => $currency,
2249 )
2250 );
2251
2252 return $earnings;
2253 }
2254
2255 /**
2256 * API Key Backwards Compatibility
2257 *
2258 * A Backwards Compatibility call for the change of meta_key/value for users API Keys.
2259 *
2260 * @since 1.3.6
2261 *
2262 * @param string $check Whether to check the cache or not
2263 * @param int $object_id The User ID being passed
2264 * @param string $meta_key The user meta key
2265 * @param bool $single If it should return a single value or array
2266 *
2267 * @return string The API key/secret for the user supplied
2268 */
2269 public function api_key_backwards_compat( $check, $object_id, $meta_key, $single ) {
2270
2271 if ( $meta_key !== 'give_user_public_key' && $meta_key !== 'give_user_secret_key' ) {
2272 return $check;
2273 }
2274
2275 $return = $check;
2276
2277 switch ( $meta_key ) {
2278 case 'give_user_public_key':
2279 $return = Give()->api->get_user_public_key( $object_id );
2280 break;
2281 case 'give_user_secret_key':
2282 $return = Give()->api->get_user_secret_key( $object_id );
2283 break;
2284 }
2285
2286 if ( ! $single ) {
2287 $return = array( $return );
2288 }
2289
2290 return $return;
2291
2292 }
2293 }
2294