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