Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Give_API often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_API, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 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 string |
||
| 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() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Registers a new rewrite endpoint for accessing the API |
||
| 173 | * |
||
| 174 | * @access public |
||
| 175 | * |
||
| 176 | * @since 1.1 |
||
| 177 | */ |
||
| 178 | public function add_endpoint() { |
||
| 179 | add_rewrite_endpoint( 'give-api', EP_ALL ); |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Registers query vars for API access |
||
| 184 | * |
||
| 185 | * @access public |
||
| 186 | * @since 1.1 |
||
| 187 | * |
||
| 188 | * @param array $vars Query vars |
||
| 189 | * |
||
| 190 | * @return string[] $vars New query vars |
||
| 191 | */ |
||
| 192 | public function query_vars( $vars ) { |
||
| 193 | |||
| 194 | $vars[] = 'token'; |
||
| 195 | $vars[] = 'key'; |
||
| 196 | $vars[] = 'query'; |
||
| 197 | $vars[] = 'type'; |
||
| 198 | $vars[] = 'form'; |
||
| 199 | $vars[] = 'number'; |
||
| 200 | $vars[] = 'date'; |
||
| 201 | $vars[] = 'startdate'; |
||
| 202 | $vars[] = 'enddate'; |
||
| 203 | $vars[] = 'donor'; |
||
| 204 | $vars[] = 'format'; |
||
| 205 | $vars[] = 'id'; |
||
| 206 | $vars[] = 'purchasekey'; |
||
| 207 | $vars[] = 'email'; |
||
| 208 | |||
| 209 | return $vars; |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Retrieve the API versions |
||
| 214 | * |
||
| 215 | * @access public |
||
| 216 | * @since 1.1 |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public function get_versions() { |
||
| 220 | return $this->versions; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Retrieve the API version that was queried |
||
| 225 | * |
||
| 226 | * @access public |
||
| 227 | * @since 1.1 |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function get_queried_version() { |
||
| 231 | return $this->queried_version; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Retrieves the default version of the API to use |
||
| 236 | * |
||
| 237 | * @access public |
||
| 238 | * @since 1.1 |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function get_default_version() { |
||
| 242 | |||
| 243 | $version = get_option( 'give_default_api_version' ); |
||
| 244 | |||
| 245 | if ( defined( 'GIVE_API_VERSION' ) ) { |
||
| 246 | $version = GIVE_API_VERSION; |
||
| 247 | } elseif ( ! $version ) { |
||
| 248 | $version = 'v1'; |
||
| 249 | } |
||
| 250 | |||
| 251 | return $version; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Sets the version of the API that was queried. |
||
| 256 | * |
||
| 257 | * Falls back to the default version if no version is specified |
||
| 258 | * |
||
| 259 | * @access private |
||
| 260 | * @since 1.1 |
||
| 261 | */ |
||
| 262 | private function set_queried_version() { |
||
| 263 | |||
| 264 | global $wp_query; |
||
| 265 | |||
| 266 | $version = $wp_query->query_vars['give-api']; |
||
| 267 | |||
| 268 | if ( strpos( $version, '/' ) ) { |
||
| 269 | |||
| 270 | $version = explode( '/', $version ); |
||
| 271 | $version = strtolower( $version[0] ); |
||
| 272 | |||
| 273 | $wp_query->query_vars['give-api'] = str_replace( $version . '/', '', $wp_query->query_vars['give-api'] ); |
||
| 274 | |||
| 275 | if ( array_key_exists( $version, $this->versions ) ) { |
||
| 276 | |||
| 277 | $this->queried_version = $version; |
||
| 278 | |||
| 279 | } else { |
||
| 280 | |||
| 281 | $this->is_valid_request = false; |
||
| 282 | $this->invalid_version(); |
||
| 283 | } |
||
| 284 | } else { |
||
| 285 | |||
| 286 | $this->queried_version = $this->get_default_version(); |
||
| 287 | |||
| 288 | } |
||
| 289 | |||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Validate the API request |
||
| 294 | * |
||
| 295 | * Checks for the user's public key and token against the secret key. |
||
| 296 | * |
||
| 297 | * @access private |
||
| 298 | * @global object $wp_query WordPress Query |
||
| 299 | * @uses Give_API::get_user() |
||
| 300 | * @uses Give_API::invalid_key() |
||
| 301 | * @uses Give_API::invalid_auth() |
||
| 302 | * @since 1.1 |
||
| 303 | * @return bool |
||
| 304 | */ |
||
| 305 | private function validate_request() { |
||
| 306 | global $wp_query; |
||
| 307 | |||
| 308 | $this->override = false; |
||
| 309 | |||
| 310 | // Make sure we have both user and api key |
||
| 311 | if ( ! empty( $wp_query->query_vars['give-api'] ) && ( $wp_query->query_vars['give-api'] !== 'forms' || ! empty( $wp_query->query_vars['token'] ) ) ) { |
||
| 312 | |||
| 313 | if ( empty( $wp_query->query_vars['token'] ) || empty( $wp_query->query_vars['key'] ) ) { |
||
| 314 | $this->missing_auth(); |
||
| 315 | |||
| 316 | return false; |
||
| 317 | } |
||
| 318 | |||
| 319 | // Retrieve the user by public API key and ensure they exist |
||
| 320 | if ( ! ( $user = $this->get_user( $wp_query->query_vars['key'] ) ) ) { |
||
| 321 | |||
| 322 | $this->invalid_key(); |
||
| 323 | |||
| 324 | return false; |
||
| 325 | |||
| 326 | } else { |
||
| 327 | |||
| 328 | $token = urldecode( $wp_query->query_vars['token'] ); |
||
| 329 | $secret = $this->get_user_secret_key( $user ); |
||
| 330 | $public = urldecode( $wp_query->query_vars['key'] ); |
||
| 331 | |||
| 332 | if ( hash_equals( md5( $secret . $public ), $token ) ) { |
||
| 333 | $this->is_valid_request = true; |
||
| 334 | } else { |
||
| 335 | $this->invalid_auth(); |
||
| 336 | |||
| 337 | return false; |
||
| 338 | } |
||
| 339 | |||
| 340 | } |
||
| 341 | } elseif ( ! empty( $wp_query->query_vars['give-api'] ) && $wp_query->query_vars['give-api'] === 'forms' ) { |
||
| 342 | $this->is_valid_request = true; |
||
| 343 | $wp_query->set( 'key', 'public' ); |
||
| 344 | } |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Retrieve the user ID based on the public key provided |
||
| 349 | * |
||
| 350 | * @access public |
||
| 351 | * @since 1.1 |
||
| 352 | * @global WPDB $wpdb Used to query the database using the WordPress |
||
| 353 | * Database API |
||
| 354 | * |
||
| 355 | * @param string $key Public Key |
||
| 356 | * |
||
| 357 | * @return bool if user ID is found, false otherwise |
||
| 358 | */ |
||
| 359 | public function get_user( $key = '' ) { |
||
| 360 | global $wpdb, $wp_query; |
||
| 361 | |||
| 362 | if ( empty( $key ) ) { |
||
| 363 | $key = urldecode( $wp_query->query_vars['key'] ); |
||
| 364 | } |
||
| 365 | |||
| 366 | if ( empty( $key ) ) { |
||
| 367 | return false; |
||
| 368 | } |
||
| 369 | |||
| 370 | $user = Give_Cache::get( md5( 'give_api_user_' . $key ), true ); |
||
| 371 | |||
| 372 | if ( false === $user ) { |
||
| 373 | $user = $wpdb->get_var( $wpdb->prepare( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = %s LIMIT 1", $key ) ); |
||
| 374 | Give_Cache::set( md5( 'give_api_user_' . $key ), $user, DAY_IN_SECONDS, true ); |
||
| 375 | } |
||
| 376 | |||
| 377 | if ( $user != null ) { |
||
| 378 | $this->user_id = $user; |
||
| 379 | |||
| 380 | return $user; |
||
| 381 | } |
||
| 382 | |||
| 383 | return false; |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Get user public key. |
||
| 388 | * |
||
| 389 | * @param int $user_id |
||
| 390 | * |
||
| 391 | * @return mixed|null|string |
||
| 392 | */ |
||
| 393 | View Code Duplication | public function get_user_public_key( $user_id = 0 ) { |
|
| 394 | global $wpdb; |
||
| 395 | |||
| 396 | if ( empty( $user_id ) ) { |
||
| 397 | return ''; |
||
| 398 | } |
||
| 399 | |||
| 400 | $cache_key = md5( 'give_api_user_public_key' . $user_id ); |
||
| 401 | $user_public_key = Give_Cache::get( $cache_key, true ); |
||
| 402 | |||
| 403 | if ( empty( $user_public_key ) ) { |
||
| 404 | $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 ) ); |
||
| 405 | Give_Cache::set( $cache_key, $user_public_key, HOUR_IN_SECONDS, true ); |
||
| 406 | } |
||
| 407 | |||
| 408 | return $user_public_key; |
||
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get user secret key. |
||
| 413 | * |
||
| 414 | * @param int $user_id |
||
| 415 | * |
||
| 416 | * @return mixed|null|string |
||
| 417 | */ |
||
| 418 | View Code Duplication | public function get_user_secret_key( $user_id = 0 ) { |
|
| 419 | global $wpdb; |
||
| 420 | |||
| 421 | if ( empty( $user_id ) ) { |
||
| 422 | return ''; |
||
| 423 | } |
||
| 424 | |||
| 425 | $cache_key = md5( 'give_api_user_secret_key' . $user_id ); |
||
| 426 | $user_secret_key = Give_Cache::get( $cache_key, true ); |
||
| 427 | |||
| 428 | if ( empty( $user_secret_key ) ) { |
||
| 429 | $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 ) ); |
||
| 430 | Give_Cache::set( $cache_key, $user_secret_key, HOUR_IN_SECONDS, true ); |
||
| 431 | } |
||
| 432 | |||
| 433 | return $user_secret_key; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Displays a missing authentication error if all the parameters are not met. |
||
| 438 | * provided |
||
| 439 | * |
||
| 440 | * @access private |
||
| 441 | * @uses Give_API::output() |
||
| 442 | * @since 1.1 |
||
| 443 | */ |
||
| 444 | View Code Duplication | private function missing_auth() { |
|
| 445 | $error = array(); |
||
| 446 | $error['error'] = __( 'You must specify both a token and API key.', 'give' ); |
||
| 447 | |||
| 448 | $this->data = $error; |
||
| 449 | $this->output( 401 ); |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Displays an authentication failed error if the user failed to provide valid |
||
| 454 | * credentials |
||
| 455 | * |
||
| 456 | * @access private |
||
| 457 | * @since 1.1 |
||
| 458 | * @uses Give_API::output() |
||
| 459 | * @return void |
||
| 460 | */ |
||
| 461 | View Code Duplication | private function invalid_auth() { |
|
| 462 | $error = array(); |
||
| 463 | $error['error'] = __( 'Your request could not be authenticated.', 'give' ); |
||
| 464 | |||
| 465 | $this->data = $error; |
||
| 466 | $this->output( 403 ); |
||
| 467 | } |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Displays an invalid API key error if the API key provided couldn't be |
||
| 471 | * validated |
||
| 472 | * |
||
| 473 | * @access private |
||
| 474 | * @since 1.1 |
||
| 475 | * @uses Give_API::output() |
||
| 476 | * @return void |
||
| 477 | */ |
||
| 478 | View Code Duplication | private function invalid_key() { |
|
| 479 | $error = array(); |
||
| 480 | $error['error'] = __( 'Invalid API key.', 'give' ); |
||
| 481 | |||
| 482 | $this->data = $error; |
||
| 483 | $this->output( 403 ); |
||
| 484 | } |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Displays an invalid version error if the version number passed isn't valid |
||
| 488 | * |
||
| 489 | * @access private |
||
| 490 | * @since 1.1 |
||
| 491 | * @uses Give_API::output() |
||
| 492 | * @return void |
||
| 493 | */ |
||
| 494 | View Code Duplication | private function invalid_version() { |
|
| 495 | $error = array(); |
||
| 496 | $error['error'] = __( 'Invalid API version.', 'give' ); |
||
| 497 | |||
| 498 | $this->data = $error; |
||
| 499 | $this->output( 404 ); |
||
| 500 | } |
||
| 501 | |||
| 502 | /** |
||
| 503 | * Listens for the API and then processes the API requests |
||
| 504 | * |
||
| 505 | * @access public |
||
| 506 | * @global $wp_query |
||
| 507 | * @since 1.1 |
||
| 508 | * @return void |
||
| 509 | */ |
||
| 510 | public function process_query() { |
||
| 511 | |||
| 512 | global $wp_query; |
||
| 513 | |||
| 514 | // Start logging how long the request takes for logging |
||
| 515 | $before = microtime( true ); |
||
| 516 | |||
| 517 | // Check for give-api var. Get out if not present |
||
| 518 | if ( empty( $wp_query->query_vars['give-api'] ) ) { |
||
| 519 | return; |
||
| 520 | } |
||
| 521 | |||
| 522 | // Determine which version was queried |
||
| 523 | $this->set_queried_version(); |
||
| 524 | |||
| 525 | // Determine the kind of query |
||
| 526 | $this->set_query_mode(); |
||
| 527 | |||
| 528 | // Check for a valid user and set errors if necessary |
||
| 529 | $this->validate_request(); |
||
| 530 | |||
| 531 | // Only proceed if no errors have been noted |
||
| 532 | if ( ! $this->is_valid_request ) { |
||
| 533 | return; |
||
| 534 | } |
||
| 535 | |||
| 536 | if ( ! defined( 'GIVE_DOING_API' ) ) { |
||
| 537 | define( 'GIVE_DOING_API', true ); |
||
| 538 | } |
||
| 539 | |||
| 540 | $data = array(); |
||
| 541 | $this->routes = new $this->versions[$this->get_queried_version()]; |
||
| 542 | $this->routes->validate_request(); |
||
| 543 | |||
| 544 | switch ( $this->endpoint ) : |
||
| 545 | |||
| 546 | case 'stats' : |
||
| 547 | |||
| 548 | $data = $this->routes->get_stats( array( |
||
| 549 | 'type' => isset( $wp_query->query_vars['type'] ) ? $wp_query->query_vars['type'] : null, |
||
| 550 | 'form' => isset( $wp_query->query_vars['form'] ) ? $wp_query->query_vars['form'] : null, |
||
| 551 | 'date' => isset( $wp_query->query_vars['date'] ) ? $wp_query->query_vars['date'] : null, |
||
| 552 | 'startdate' => isset( $wp_query->query_vars['startdate'] ) ? $wp_query->query_vars['startdate'] : null, |
||
| 553 | 'enddate' => isset( $wp_query->query_vars['enddate'] ) ? $wp_query->query_vars['enddate'] : null, |
||
| 554 | ) ); |
||
| 555 | |||
| 556 | break; |
||
| 557 | |||
| 558 | case 'forms' : |
||
| 559 | |||
| 560 | $form = isset( $wp_query->query_vars['form'] ) ? $wp_query->query_vars['form'] : null; |
||
| 561 | |||
| 562 | $data = $this->routes->get_forms( $form ); |
||
| 563 | |||
| 564 | break; |
||
| 565 | |||
| 566 | case 'donors' : |
||
| 567 | |||
| 568 | $donor = isset( $wp_query->query_vars['donor'] ) ? $wp_query->query_vars['donor'] : null; |
||
| 569 | |||
| 570 | $data = $this->routes->get_donors( $donor ); |
||
| 571 | |||
| 572 | break; |
||
| 573 | |||
| 574 | case 'donations' : |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Call to get recent donations |
||
| 578 | * |
||
| 579 | * @params text date | today, yesterday or range |
||
| 580 | * @params date startdate | required when date = range and format to be YYYYMMDD (i.e. 20170524) |
||
| 581 | * @params date enddate | required when date = range and format to be YYYYMMDD (i.e. 20170524) |
||
| 582 | */ |
||
| 583 | $data = $this->routes->get_recent_donations( array( |
||
| 584 | 'id' => isset( $wp_query->query_vars['id'] ) ? $wp_query->query_vars['id'] : null, |
||
| 585 | 'date' => isset( $wp_query->query_vars['date'] ) ? $wp_query->query_vars['date'] : null, |
||
| 586 | 'startdate' => isset( $wp_query->query_vars['startdate'] ) ? $wp_query->query_vars['startdate'] : null, |
||
| 587 | 'enddate' => isset( $wp_query->query_vars['enddate'] ) ? $wp_query->query_vars['enddate'] : null, |
||
| 588 | ) ); |
||
| 589 | |||
| 590 | break; |
||
| 591 | |||
| 592 | endswitch; |
||
| 593 | |||
| 594 | // Allow extensions to setup their own return data |
||
| 595 | $this->data = apply_filters( 'give_api_output_data', $data, $this->endpoint, $this ); |
||
| 596 | |||
| 597 | $after = microtime( true ); |
||
| 598 | $request_time = ( $after - $before ); |
||
| 599 | $this->data['request_speed'] = $request_time; |
||
| 600 | |||
| 601 | // Log this API request, if enabled. We log it here because we have access to errors. |
||
| 602 | $this->log_request( $this->data ); |
||
| 603 | |||
| 604 | // Send out data to the output function |
||
| 605 | $this->output(); |
||
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Returns the API endpoint requested |
||
| 610 | * |
||
| 611 | * @access public |
||
| 612 | * @since 1.1 |
||
| 613 | * @return string $query Query mode |
||
| 614 | */ |
||
| 615 | public function get_query_mode() { |
||
| 616 | |||
| 617 | return $this->endpoint; |
||
| 618 | } |
||
| 619 | |||
| 620 | /** |
||
| 621 | * Determines the kind of query requested and also ensure it is a valid query |
||
| 622 | * |
||
| 623 | * @access public |
||
| 624 | * @since 1.1 |
||
| 625 | * @global $wp_query |
||
| 626 | */ |
||
| 627 | public function set_query_mode() { |
||
| 628 | |||
| 629 | global $wp_query; |
||
| 630 | |||
| 631 | // Whitelist our query options |
||
| 632 | $accepted = apply_filters( 'give_api_valid_query_modes', array( |
||
| 633 | 'stats', |
||
| 634 | 'forms', |
||
| 635 | 'donors', |
||
| 636 | 'donations', |
||
| 637 | ) ); |
||
| 638 | |||
| 639 | $query = isset( $wp_query->query_vars['give-api'] ) ? $wp_query->query_vars['give-api'] : null; |
||
| 640 | $query = str_replace( $this->queried_version . '/', '', $query ); |
||
| 641 | |||
| 642 | $error = array(); |
||
| 643 | |||
| 644 | // Make sure our query is valid |
||
| 645 | if ( ! in_array( $query, $accepted ) ) { |
||
| 646 | $error['error'] = __( 'Invalid query.', 'give' ); |
||
| 647 | |||
| 648 | $this->data = $error; |
||
| 649 | // 400 is Bad Request |
||
| 650 | $this->output( 400 ); |
||
| 651 | } |
||
| 652 | |||
| 653 | $this->endpoint = $query; |
||
| 654 | } |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Get page number |
||
| 658 | * |
||
| 659 | * @access public |
||
| 660 | * @since 1.1 |
||
| 661 | * @global $wp_query |
||
| 662 | * @return int $wp_query->query_vars['page'] if page number returned (default: 1) |
||
| 663 | */ |
||
| 664 | public function get_paged() { |
||
| 665 | global $wp_query; |
||
| 666 | |||
| 667 | return isset( $wp_query->query_vars['page'] ) ? $wp_query->query_vars['page'] : 1; |
||
| 668 | } |
||
| 669 | |||
| 670 | |||
| 671 | /** |
||
| 672 | * Number of results to display per page |
||
| 673 | * |
||
| 674 | * @access public |
||
| 675 | * @since 1.1 |
||
| 676 | * @global $wp_query |
||
| 677 | * @return int $per_page Results to display per page (default: 10) |
||
| 678 | */ |
||
| 679 | public function per_page() { |
||
| 680 | global $wp_query; |
||
| 681 | |||
| 682 | $per_page = isset( $wp_query->query_vars['number'] ) ? $wp_query->query_vars['number'] : 10; |
||
| 683 | |||
| 684 | if ( $per_page < 0 && $this->get_query_mode() == 'donors' ) { |
||
| 685 | $per_page = 99999999; |
||
| 686 | } // End if(). |
||
| 687 | |||
| 688 | return apply_filters( 'give_api_results_per_page', $per_page ); |
||
| 689 | } |
||
| 690 | |||
| 691 | /** |
||
| 692 | * Sets up the dates used to retrieve earnings/donations |
||
| 693 | * |
||
| 694 | * @access public |
||
| 695 | * @since 1.2 |
||
| 696 | * |
||
| 697 | * @param array $args Arguments to override defaults |
||
| 698 | * |
||
| 699 | * @return array $dates |
||
| 700 | */ |
||
| 701 | public function get_dates( $args = array() ) { |
||
| 702 | $dates = array(); |
||
| 703 | |||
| 704 | $defaults = array( |
||
| 705 | 'type' => '', |
||
| 706 | 'form' => null, |
||
| 707 | 'date' => null, |
||
| 708 | 'startdate' => null, |
||
| 709 | 'enddate' => null, |
||
| 710 | ); |
||
| 711 | |||
| 712 | $args = wp_parse_args( $args, $defaults ); |
||
| 713 | |||
| 714 | $current_time = current_time( 'timestamp' ); |
||
| 715 | |||
| 716 | if ( 'range' === $args['date'] ) { |
||
| 717 | $startdate = strtotime( $args['startdate'] ); |
||
| 718 | $enddate = strtotime( $args['enddate'] ); |
||
| 719 | $dates['day_start'] = date( 'd', $startdate ); |
||
| 720 | $dates['day_end'] = date( 'd', $enddate ); |
||
| 721 | $dates['m_start'] = date( 'n', $startdate ); |
||
| 722 | $dates['m_end'] = date( 'n', $enddate ); |
||
| 723 | $dates['year'] = date( 'Y', $startdate ); |
||
| 724 | $dates['year_end'] = date( 'Y', $enddate ); |
||
| 725 | } else { |
||
| 726 | // Modify dates based on predefined ranges |
||
| 727 | switch ( $args['date'] ) : |
||
| 728 | |||
| 729 | View Code Duplication | case 'this_month' : |
|
| 730 | $dates['day'] = null; |
||
| 731 | $dates['m_start'] = date( 'n', $current_time ); |
||
| 732 | $dates['m_end'] = date( 'n', $current_time ); |
||
| 733 | $dates['year'] = date( 'Y', $current_time ); |
||
| 734 | break; |
||
| 735 | |||
| 736 | case 'last_month' : |
||
| 737 | $dates['day'] = null; |
||
| 738 | $dates['m_start'] = date( 'n', $current_time ) == 1 ? 12 : date( 'n', $current_time ) - 1; |
||
| 739 | $dates['m_end'] = $dates['m_start']; |
||
| 740 | $dates['year'] = date( 'n', $current_time ) == 1 ? date( 'Y', $current_time ) - 1 : date( 'Y', $current_time ); |
||
| 741 | break; |
||
| 742 | |||
| 743 | View Code Duplication | case 'today' : |
|
| 744 | $dates['day'] = date( 'd', $current_time ); |
||
| 745 | $dates['m_start'] = date( 'n', $current_time ); |
||
| 746 | $dates['m_end'] = date( 'n', $current_time ); |
||
| 747 | $dates['year'] = date( 'Y', $current_time ); |
||
| 748 | break; |
||
| 749 | |||
| 750 | View Code Duplication | case 'yesterday' : |
|
| 751 | |||
| 752 | $year = date( 'Y', $current_time ); |
||
| 753 | $month = date( 'n', $current_time ); |
||
| 754 | $day = date( 'd', $current_time ); |
||
| 755 | |||
| 756 | if ( $month == 1 && $day == 1 ) { |
||
| 757 | |||
| 758 | $year -= 1; |
||
| 759 | $month = 12; |
||
| 760 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
||
| 761 | |||
| 762 | } elseif ( $month > 1 && $day == 1 ) { |
||
| 763 | |||
| 764 | $month -= 1; |
||
| 765 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
||
| 766 | |||
| 767 | } else { |
||
| 768 | |||
| 769 | $day -= 1; |
||
| 770 | |||
| 771 | } |
||
| 772 | |||
| 773 | $dates['day'] = $day; |
||
| 774 | $dates['m_start'] = $month; |
||
| 775 | $dates['m_end'] = $month; |
||
| 776 | $dates['year'] = $year; |
||
| 777 | |||
| 778 | break; |
||
| 779 | |||
| 780 | View Code Duplication | case 'this_quarter' : |
|
| 781 | $month_now = date( 'n', $current_time ); |
||
| 782 | |||
| 783 | $dates['day'] = null; |
||
| 784 | |||
| 785 | if ( $month_now <= 3 ) { |
||
| 786 | |||
| 787 | $dates['m_start'] = 1; |
||
| 788 | $dates['m_end'] = 3; |
||
| 789 | $dates['year'] = date( 'Y', $current_time ); |
||
| 790 | |||
| 791 | } elseif ( $month_now <= 6 ) { |
||
| 792 | |||
| 793 | $dates['m_start'] = 4; |
||
| 794 | $dates['m_end'] = 6; |
||
| 795 | $dates['year'] = date( 'Y', $current_time ); |
||
| 796 | |||
| 797 | } elseif ( $month_now <= 9 ) { |
||
| 798 | |||
| 799 | $dates['m_start'] = 7; |
||
| 800 | $dates['m_end'] = 9; |
||
| 801 | $dates['year'] = date( 'Y', $current_time ); |
||
| 802 | |||
| 803 | } else { |
||
| 804 | |||
| 805 | $dates['m_start'] = 10; |
||
| 806 | $dates['m_end'] = 12; |
||
| 807 | $dates['year'] = date( 'Y', $current_time ); |
||
| 808 | |||
| 809 | } |
||
| 810 | break; |
||
| 811 | |||
| 812 | View Code Duplication | case 'last_quarter' : |
|
| 813 | $month_now = date( 'n', $current_time ); |
||
| 814 | |||
| 815 | $dates['day'] = null; |
||
| 816 | |||
| 817 | if ( $month_now <= 3 ) { |
||
| 818 | |||
| 819 | $dates['m_start'] = 10; |
||
| 820 | $dates['m_end'] = 12; |
||
| 821 | $dates['year'] = date( 'Y', $current_time ) - 1; // Previous year |
||
| 822 | |||
| 823 | } elseif ( $month_now <= 6 ) { |
||
| 824 | |||
| 825 | $dates['m_start'] = 1; |
||
| 826 | $dates['m_end'] = 3; |
||
| 827 | $dates['year'] = date( 'Y', $current_time ); |
||
| 828 | |||
| 829 | } elseif ( $month_now <= 9 ) { |
||
| 830 | |||
| 831 | $dates['m_start'] = 4; |
||
| 832 | $dates['m_end'] = 6; |
||
| 833 | $dates['year'] = date( 'Y', $current_time ); |
||
| 834 | |||
| 835 | } else { |
||
| 836 | |||
| 837 | $dates['m_start'] = 7; |
||
| 838 | $dates['m_end'] = 9; |
||
| 839 | $dates['year'] = date( 'Y', $current_time ); |
||
| 840 | |||
| 841 | } |
||
| 842 | break; |
||
| 843 | |||
| 844 | View Code Duplication | case 'this_year' : |
|
| 845 | $dates['day'] = null; |
||
| 846 | $dates['m_start'] = null; |
||
| 847 | $dates['m_end'] = null; |
||
| 848 | $dates['year'] = date( 'Y', $current_time ); |
||
| 849 | break; |
||
| 850 | |||
| 851 | View Code Duplication | case 'last_year' : |
|
| 852 | $dates['day'] = null; |
||
| 853 | $dates['m_start'] = null; |
||
| 854 | $dates['m_end'] = null; |
||
| 855 | $dates['year'] = date( 'Y', $current_time ) - 1; |
||
| 856 | break; |
||
| 857 | |||
| 858 | endswitch; |
||
| 859 | }// End if(). |
||
| 860 | |||
| 861 | /** |
||
| 862 | * Returns the filters for the dates used to retrieve earnings. |
||
| 863 | * |
||
| 864 | * @since 1.2 |
||
| 865 | * |
||
| 866 | * @param array $dates The dates used for retrieving earnings. |
||
| 867 | */ |
||
| 868 | return apply_filters( 'give_api_stat_dates', $dates ); |
||
| 869 | } |
||
| 870 | |||
| 871 | /** |
||
| 872 | * Process Get Donors API Request. |
||
| 873 | * |
||
| 874 | * @access public |
||
| 875 | * @since 1.1 |
||
| 876 | * @global WPDB $wpdb Used to query the database using the WordPress Database API. |
||
| 877 | * |
||
| 878 | * @param int $donor Donor ID |
||
| 879 | * |
||
| 880 | * @return array $donors Multidimensional array of the donors. |
||
| 881 | */ |
||
| 882 | public function get_donors( $donor = null ) { |
||
| 883 | |||
| 884 | $donors = array(); |
||
| 885 | $error = array(); |
||
| 886 | if ( ! user_can( $this->user_id, 'view_give_sensitive_data' ) && ! $this->override ) { |
||
| 887 | return $donors; |
||
| 888 | } |
||
| 889 | |||
| 890 | $paged = $this->get_paged(); |
||
| 891 | $per_page = $this->per_page(); |
||
| 892 | $offset = $per_page * ( $paged - 1 ); |
||
| 893 | |||
| 894 | if ( is_numeric( $donor ) ) { |
||
| 895 | $field = 'id'; |
||
| 896 | } else { |
||
| 897 | $field = 'email'; |
||
| 898 | } |
||
| 899 | |||
| 900 | $donor_query = Give()->donors->get_donors( array( |
||
| 901 | 'number' => $per_page, |
||
| 902 | 'offset' => $offset, |
||
| 903 | $field => $donor, |
||
| 904 | ) ); |
||
| 905 | $donor_count = 0; |
||
| 906 | |||
| 907 | if ( $donor_query ) { |
||
| 908 | |||
| 909 | foreach ( $donor_query as $donor_obj ) { |
||
| 910 | |||
| 911 | $names = explode( ' ', $donor_obj->name ); |
||
| 912 | $first_name = ! empty( $names[0] ) ? $names[0] : ''; |
||
| 913 | $last_name = ''; |
||
| 914 | View Code Duplication | if ( ! empty( $names[1] ) ) { |
|
| 915 | unset( $names[0] ); |
||
| 916 | $last_name = implode( ' ', $names ); |
||
| 917 | } |
||
| 918 | |||
| 919 | $donors['donors'][ $donor_count ]['info']['user_id'] = ''; |
||
| 920 | $donors['donors'][ $donor_count ]['info']['username'] = ''; |
||
| 921 | $donors['donors'][ $donor_count ]['info']['display_name'] = ''; |
||
| 922 | $donors['donors'][ $donor_count ]['info']['donor_id'] = $donor_obj->id; |
||
| 923 | $donors['donors'][ $donor_count ]['info']['first_name'] = $first_name; |
||
| 924 | $donors['donors'][ $donor_count ]['info']['last_name'] = $last_name; |
||
| 925 | $donors['donors'][ $donor_count ]['info']['email'] = $donor_obj->email; |
||
| 926 | |||
| 927 | if ( ! empty( $donor_obj->user_id ) ) { |
||
| 928 | |||
| 929 | $user_data = get_userdata( $donor_obj->user_id ); |
||
| 930 | |||
| 931 | // Donor with registered account. |
||
| 932 | $donors['donors'][ $donor_count ]['info']['user_id'] = $donor_obj->user_id; |
||
| 933 | $donors['donors'][ $donor_count ]['info']['username'] = $user_data->user_login; |
||
| 934 | $donors['donors'][ $donor_count ]['info']['display_name'] = $user_data->display_name; |
||
| 935 | |||
| 936 | } |
||
| 937 | |||
| 938 | $donors['donors'][ $donor_count ]['stats']['total_donations'] = $donor_obj->purchase_count; |
||
| 939 | $donors['donors'][ $donor_count ]['stats']['total_spent'] = $donor_obj->purchase_value; |
||
| 940 | |||
| 941 | $donor_count ++; |
||
| 942 | |||
| 943 | } |
||
| 944 | } elseif ( $donor ) { |
||
| 945 | |||
| 946 | $error['error'] = sprintf( /* translators: %s: donor */ |
||
| 947 | __( 'Donor %s not found.', 'give' ), $donor ); |
||
| 948 | |||
| 949 | return $error; |
||
| 950 | |||
| 951 | } else { |
||
| 952 | |||
| 953 | $error['error'] = __( 'No donors found.', 'give' ); |
||
| 954 | |||
| 955 | return $error; |
||
| 956 | |||
| 957 | }// End if(). |
||
| 958 | |||
| 959 | return $donors; |
||
| 960 | } |
||
| 961 | |||
| 962 | /** |
||
| 963 | * Process Get Donation Forms API Request |
||
| 964 | * |
||
| 965 | * @access public |
||
| 966 | * @since 1.1 |
||
| 967 | * |
||
| 968 | * @param int $form Give Form ID. |
||
| 969 | * |
||
| 970 | * @return array $donors Multidimensional array of the forms. |
||
| 971 | */ |
||
| 972 | public function get_forms( $form = null ) { |
||
| 973 | |||
| 974 | $forms = array(); |
||
| 975 | $error = array(); |
||
| 976 | |||
| 977 | if ( $form == null ) { |
||
| 978 | $forms['forms'] = array(); |
||
| 979 | |||
| 980 | $form_list = get_posts( array( |
||
| 981 | 'post_type' => 'give_forms', |
||
| 982 | 'posts_per_page' => $this->per_page(), |
||
| 983 | 'suppress_filters' => true, |
||
| 984 | 'paged' => $this->get_paged(), |
||
| 985 | ) ); |
||
| 986 | |||
| 987 | if ( $form_list ) { |
||
| 988 | $i = 0; |
||
| 989 | foreach ( $form_list as $form_info ) { |
||
| 990 | $forms['forms'][ $i ] = $this->get_form_data( $form_info ); |
||
| 991 | $i ++; |
||
| 992 | } |
||
| 993 | } |
||
| 994 | } else { |
||
| 995 | if ( get_post_type( $form ) == 'give_forms' ) { |
||
| 996 | $form_info = get_post( $form ); |
||
| 997 | |||
| 998 | $forms['forms'][0] = $this->get_form_data( $form_info ); |
||
| 999 | |||
| 1000 | } else { |
||
| 1001 | $error['error'] = sprintf( /* translators: %s: form */ |
||
| 1002 | __( 'Form %s not found.', 'give' ), $form ); |
||
| 1003 | |||
| 1004 | return $error; |
||
| 1005 | } |
||
| 1006 | } |
||
| 1007 | |||
| 1008 | return $forms; |
||
| 1009 | } |
||
| 1010 | |||
| 1011 | /** |
||
| 1012 | * Given a give_forms post object, generate the data for the API output |
||
| 1013 | * |
||
| 1014 | * @since 1.1 |
||
| 1015 | * |
||
| 1016 | * @param object $form_info The Give Form's Post Object. |
||
| 1017 | * |
||
| 1018 | * @return array Array of post data to return back in the API. |
||
| 1019 | */ |
||
| 1020 | private function get_form_data( $form_info ) { |
||
| 1021 | |||
| 1022 | $form = array(); |
||
| 1023 | |||
| 1024 | $form['info']['id'] = $form_info->ID; |
||
| 1025 | $form['info']['slug'] = $form_info->post_name; |
||
| 1026 | $form['info']['title'] = $form_info->post_title; |
||
| 1027 | $form['info']['create_date'] = $form_info->post_date; |
||
| 1028 | $form['info']['modified_date'] = $form_info->post_modified; |
||
| 1029 | $form['info']['status'] = $form_info->post_status; |
||
| 1030 | $form['info']['link'] = html_entity_decode( $form_info->guid ); |
||
| 1031 | $form['info']['content'] = give_get_meta( $form_info->ID, '_give_form_content', true ); |
||
| 1032 | $form['info']['thumbnail'] = wp_get_attachment_url( get_post_thumbnail_id( $form_info->ID ) ); |
||
| 1033 | |||
| 1034 | if ( give_is_setting_enabled( give_get_option( 'categories', 'disabled' ) ) ) { |
||
| 1035 | $form['info']['category'] = get_the_terms( $form_info, 'give_forms_category' ); |
||
| 1036 | $form['info']['tags'] = get_the_terms( $form_info, 'give_forms_tag' ); |
||
| 1037 | } |
||
| 1038 | if ( give_is_setting_enabled( give_get_option( 'tags', 'disabled' ) ) ) { |
||
| 1039 | $form['info']['tags'] = get_the_terms( $form_info, 'give_forms_tag' ); |
||
| 1040 | } |
||
| 1041 | |||
| 1042 | // Check whether any goal is to be achieved for the donation form. |
||
| 1043 | $goal_option = give_get_meta( $form_info->ID, '_give_goal_option', true ); |
||
| 1044 | $goal_amount = give_get_meta( $form_info->ID, '_give_set_goal', true ); |
||
| 1045 | if ( give_is_setting_enabled( $goal_option ) && $goal_amount ) { |
||
| 1046 | $total_income = give_get_form_earnings_stats( $form_info->ID ); |
||
| 1047 | $goal_percentage_completed = ( $total_income < $goal_amount ) ? round( ( $total_income / $goal_amount ) * 100, 2 ) : 100; |
||
| 1048 | $form['goal']['amount'] = isset( $goal_amount ) ? $goal_amount : ''; |
||
| 1049 | $form['goal']['percentage_completed'] = isset( $goal_percentage_completed ) ? $goal_percentage_completed : ''; |
||
| 1050 | } |
||
| 1051 | |||
| 1052 | if ( user_can( $this->user_id, 'view_give_reports' ) || $this->override ) { |
||
| 1053 | $form['stats']['total']['donations'] = give_get_form_sales_stats( $form_info->ID ); |
||
| 1054 | $form['stats']['total']['earnings'] = give_get_form_earnings_stats( $form_info->ID ); |
||
| 1055 | $form['stats']['monthly_average']['donations'] = give_get_average_monthly_form_sales( $form_info->ID ); |
||
| 1056 | $form['stats']['monthly_average']['earnings'] = give_get_average_monthly_form_earnings( $form_info->ID ); |
||
| 1057 | } |
||
| 1058 | |||
| 1059 | $counter = 0; |
||
| 1060 | if ( give_has_variable_prices( $form_info->ID ) ) { |
||
| 1061 | foreach ( give_get_variable_prices( $form_info->ID ) as $price ) { |
||
| 1062 | $counter ++; |
||
| 1063 | // multi-level item |
||
| 1064 | $level = isset( $price['_give_text'] ) ? $price['_give_text'] : 'level-' . $counter; |
||
| 1065 | $form['pricing'][ sanitize_key( $level ) ] = $price['_give_amount']; |
||
| 1066 | |||
| 1067 | } |
||
| 1068 | } else { |
||
| 1069 | $form['pricing']['amount'] = give_get_form_price( $form_info->ID ); |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | if ( user_can( $this->user_id, 'view_give_sensitive_data' ) || $this->override ) { |
||
| 1073 | |||
| 1074 | /** |
||
| 1075 | * Fires when generating API sensitive data. |
||
| 1076 | * |
||
| 1077 | * @since 1.1 |
||
| 1078 | */ |
||
| 1079 | do_action( 'give_api_sensitive_data' ); |
||
| 1080 | |||
| 1081 | } |
||
| 1082 | |||
| 1083 | return apply_filters( 'give_api_forms_form', $form ); |
||
| 1084 | |||
| 1085 | } |
||
| 1086 | |||
| 1087 | /** |
||
| 1088 | * Process Get Stats API Request |
||
| 1089 | * |
||
| 1090 | * @since 1.1 |
||
| 1091 | * |
||
| 1092 | * @global WPDB $wpdb Used to query the database using the WordPress. |
||
| 1093 | * |
||
| 1094 | * @param array $args Arguments provided by API Request. |
||
| 1095 | * |
||
| 1096 | * @return array |
||
| 1097 | */ |
||
| 1098 | public function get_stats( $args = array() ) { |
||
| 1099 | $defaults = array( |
||
| 1100 | 'type' => null, |
||
| 1101 | 'form' => null, |
||
| 1102 | 'date' => null, |
||
| 1103 | 'startdate' => null, |
||
| 1104 | 'enddate' => null, |
||
| 1105 | ); |
||
| 1106 | |||
| 1107 | $args = wp_parse_args( $args, $defaults ); |
||
| 1108 | |||
| 1109 | $dates = $this->get_dates( $args ); |
||
| 1110 | |||
| 1111 | $stats = array(); |
||
| 1112 | $earnings = array( |
||
| 1113 | 'earnings' => array(), |
||
| 1114 | ); |
||
| 1115 | $donations = array( |
||
| 1116 | 'donations' => array(), |
||
| 1117 | ); |
||
| 1118 | $error = array(); |
||
| 1119 | |||
| 1120 | if ( ! user_can( $this->user_id, 'view_give_reports' ) && ! $this->override ) { |
||
| 1121 | return $stats; |
||
| 1122 | } |
||
| 1123 | |||
| 1124 | if ( $args['type'] == 'donations' ) { |
||
| 1125 | |||
| 1126 | if ( $args['form'] == null ) { |
||
| 1127 | if ( $args['date'] == null ) { |
||
| 1128 | $donations = $this->get_default_sales_stats(); |
||
| 1129 | } elseif ( $args['date'] === 'range' ) { |
||
| 1130 | // Return donations for a date range. |
||
| 1131 | // Ensure the end date is later than the start date. |
||
| 1132 | View Code Duplication | if ( $args['enddate'] < $args['startdate'] ) { |
|
| 1133 | $error['error'] = __( 'The end date must be later than the start date.', 'give' ); |
||
| 1134 | } |
||
| 1135 | |||
| 1136 | // Ensure both the start and end date are specified |
||
| 1137 | View Code Duplication | if ( empty( $args['startdate'] ) || empty( $args['enddate'] ) ) { |
|
| 1138 | $error['error'] = __( 'Invalid or no date range specified.', 'give' ); |
||
| 1139 | } |
||
| 1140 | |||
| 1141 | $total = 0; |
||
| 1142 | |||
| 1143 | // Loop through the years |
||
| 1144 | $y = $dates['year']; |
||
| 1145 | View Code Duplication | while ( $y <= $dates['year_end'] ) : |
|
| 1146 | |||
| 1147 | if ( $dates['year'] == $dates['year_end'] ) { |
||
| 1148 | $month_start = $dates['m_start']; |
||
| 1149 | $month_end = $dates['m_end']; |
||
| 1150 | } elseif ( $y == $dates['year'] && $dates['year_end'] > $dates['year'] ) { |
||
| 1151 | $month_start = $dates['m_start']; |
||
| 1152 | $month_end = 12; |
||
| 1153 | } elseif ( $y == $dates['year_end'] ) { |
||
| 1154 | $month_start = 1; |
||
| 1155 | $month_end = $dates['m_end']; |
||
| 1156 | } else { |
||
| 1157 | $month_start = 1; |
||
| 1158 | $month_end = 12; |
||
| 1159 | } |
||
| 1160 | |||
| 1161 | $i = $month_start; |
||
| 1162 | while ( $i <= $month_end ) : |
||
| 1163 | |||
| 1164 | if ( $i == $dates['m_start'] ) { |
||
| 1165 | $d = $dates['day_start']; |
||
| 1166 | } else { |
||
| 1167 | $d = 1; |
||
| 1168 | } |
||
| 1169 | |||
| 1170 | if ( $i == $dates['m_end'] ) { |
||
| 1171 | $num_of_days = $dates['day_end']; |
||
| 1172 | } else { |
||
| 1173 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
||
| 1174 | } |
||
| 1175 | |||
| 1176 | while ( $d <= $num_of_days ) : |
||
| 1177 | $sale_count = give_get_sales_by_date( $d, $i, $y ); |
||
| 1178 | $date_key = date( 'Ymd', strtotime( $y . '/' . $i . '/' . $d ) ); |
||
| 1179 | if ( ! isset( $donations['sales'][ $date_key ] ) ) { |
||
| 1180 | $donations['sales'][ $date_key ] = 0; |
||
| 1181 | } |
||
| 1182 | $donations['sales'][ $date_key ] += $sale_count; |
||
| 1183 | $total += $sale_count; |
||
| 1184 | $d ++; |
||
| 1185 | endwhile; |
||
| 1186 | $i ++; |
||
| 1187 | endwhile; |
||
| 1188 | |||
| 1189 | $y ++; |
||
| 1190 | endwhile; |
||
| 1191 | |||
| 1192 | $donations['totals'] = $total; |
||
| 1193 | View Code Duplication | } else { |
|
| 1194 | if ( $args['date'] == 'this_quarter' || $args['date'] == 'last_quarter' ) { |
||
| 1195 | $donations_count = 0; |
||
| 1196 | |||
| 1197 | // Loop through the months |
||
| 1198 | $month = $dates['m_start']; |
||
| 1199 | |||
| 1200 | while ( $month <= $dates['m_end'] ) : |
||
| 1201 | $donations_count += give_get_sales_by_date( null, $month, $dates['year'] ); |
||
| 1202 | $month ++; |
||
| 1203 | endwhile; |
||
| 1204 | |||
| 1205 | $donations['donations'][ $args['date'] ] = $donations_count; |
||
| 1206 | } else { |
||
| 1207 | $donations['donations'][ $args['date'] ] = give_get_sales_by_date( $dates['day'], $dates['m_start'], $dates['year'] ); |
||
| 1208 | } |
||
| 1209 | }// End if(). |
||
| 1210 | } elseif ( $args['form'] == 'all' ) { |
||
| 1211 | $forms = get_posts( array( |
||
| 1212 | 'post_type' => 'give_forms', |
||
| 1213 | 'nopaging' => true, |
||
| 1214 | ) ); |
||
| 1215 | $i = 0; |
||
| 1216 | foreach ( $forms as $form_info ) { |
||
| 1217 | $donations['donations'][ $i ] = array( |
||
| 1218 | $form_info->post_name => give_get_form_sales_stats( $form_info->ID ), |
||
| 1219 | ); |
||
| 1220 | $i ++; |
||
| 1221 | } |
||
| 1222 | View Code Duplication | } else { |
|
| 1223 | if ( get_post_type( $args['form'] ) == 'give_forms' ) { |
||
| 1224 | $form_info = get_post( $args['form'] ); |
||
| 1225 | $donations['donations'][0] = array( |
||
| 1226 | $form_info->post_name => give_get_form_sales_stats( $args['form'] ), |
||
| 1227 | ); |
||
| 1228 | } else { |
||
| 1229 | $error['error'] = sprintf( /* translators: %s: form */ |
||
| 1230 | __( 'Form %s not found.', 'give' ), $args['form'] ); |
||
| 1231 | } |
||
| 1232 | }// End if(). |
||
| 1233 | |||
| 1234 | if ( ! empty( $error ) ) { |
||
| 1235 | return $error; |
||
| 1236 | } |
||
| 1237 | |||
| 1238 | return $donations; |
||
| 1239 | |||
| 1240 | } elseif ( $args['type'] == 'earnings' ) { |
||
| 1241 | if ( $args['form'] == null ) { |
||
| 1242 | if ( $args['date'] == null ) { |
||
| 1243 | $earnings = $this->get_default_earnings_stats(); |
||
| 1244 | } elseif ( $args['date'] === 'range' ) { |
||
| 1245 | // Return sales for a date range |
||
| 1246 | // Ensure the end date is later than the start date |
||
| 1247 | View Code Duplication | if ( $args['enddate'] < $args['startdate'] ) { |
|
| 1248 | $error['error'] = __( 'The end date must be later than the start date.', 'give' ); |
||
| 1249 | } |
||
| 1250 | |||
| 1251 | // Ensure both the start and end date are specified |
||
| 1252 | View Code Duplication | if ( empty( $args['startdate'] ) || empty( $args['enddate'] ) ) { |
|
| 1253 | $error['error'] = __( 'Invalid or no date range specified.', 'give' ); |
||
| 1254 | } |
||
| 1255 | |||
| 1256 | $total = (float) 0.00; |
||
| 1257 | |||
| 1258 | // Loop through the years |
||
| 1259 | $y = $dates['year']; |
||
| 1260 | if ( ! isset( $earnings['earnings'] ) ) { |
||
| 1261 | $earnings['earnings'] = array(); |
||
| 1262 | } |
||
| 1263 | View Code Duplication | while ( $y <= $dates['year_end'] ) : |
|
| 1264 | |||
| 1265 | if ( $dates['year'] == $dates['year_end'] ) { |
||
| 1266 | $month_start = $dates['m_start']; |
||
| 1267 | $month_end = $dates['m_end']; |
||
| 1268 | } elseif ( $y == $dates['year'] && $dates['year_end'] > $dates['year'] ) { |
||
| 1269 | $month_start = $dates['m_start']; |
||
| 1270 | $month_end = 12; |
||
| 1271 | } elseif ( $y == $dates['year_end'] ) { |
||
| 1272 | $month_start = 1; |
||
| 1273 | $month_end = $dates['m_end']; |
||
| 1274 | } else { |
||
| 1275 | $month_start = 1; |
||
| 1276 | $month_end = 12; |
||
| 1277 | } |
||
| 1278 | |||
| 1279 | $i = $month_start; |
||
| 1280 | while ( $i <= $month_end ) : |
||
| 1281 | |||
| 1282 | if ( $i == $dates['m_start'] ) { |
||
| 1283 | $d = $dates['day_start']; |
||
| 1284 | } else { |
||
| 1285 | $d = 1; |
||
| 1286 | } |
||
| 1287 | |||
| 1288 | if ( $i == $dates['m_end'] ) { |
||
| 1289 | $num_of_days = $dates['day_end']; |
||
| 1290 | } else { |
||
| 1291 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
||
| 1292 | } |
||
| 1293 | |||
| 1294 | while ( $d <= $num_of_days ) : |
||
| 1295 | $earnings_stat = give_get_earnings_by_date( $d, $i, $y ); |
||
| 1296 | $date_key = date( 'Ymd', strtotime( $y . '/' . $i . '/' . $d ) ); |
||
| 1297 | if ( ! isset( $earnings['earnings'][ $date_key ] ) ) { |
||
| 1298 | $earnings['earnings'][ $date_key ] = 0; |
||
| 1299 | } |
||
| 1300 | $earnings['earnings'][ $date_key ] += $earnings_stat; |
||
| 1301 | $total += $earnings_stat; |
||
| 1302 | $d ++; |
||
| 1303 | endwhile; |
||
| 1304 | |||
| 1305 | $i ++; |
||
| 1306 | endwhile; |
||
| 1307 | |||
| 1308 | $y ++; |
||
| 1309 | endwhile; |
||
| 1310 | |||
| 1311 | $earnings['totals'] = $total; |
||
| 1312 | View Code Duplication | } else { |
|
| 1313 | if ( $args['date'] == 'this_quarter' || $args['date'] == 'last_quarter' ) { |
||
| 1314 | $earnings_count = (float) 0.00; |
||
| 1315 | |||
| 1316 | // Loop through the months |
||
| 1317 | $month = $dates['m_start']; |
||
| 1318 | |||
| 1319 | while ( $month <= $dates['m_end'] ) : |
||
| 1320 | $earnings_count += give_get_earnings_by_date( null, $month, $dates['year'] ); |
||
| 1321 | $month ++; |
||
| 1322 | endwhile; |
||
| 1323 | |||
| 1324 | $earnings['earnings'][ $args['date'] ] = $earnings_count; |
||
| 1325 | } else { |
||
| 1326 | $earnings['earnings'][ $args['date'] ] = give_get_earnings_by_date( $dates['day'], $dates['m_start'], $dates['year'] ); |
||
| 1327 | } |
||
| 1328 | }// End if(). |
||
| 1329 | } elseif ( $args['form'] == 'all' ) { |
||
| 1330 | $forms = get_posts( array( |
||
| 1331 | 'post_type' => 'give_forms', |
||
| 1332 | 'nopaging' => true, |
||
| 1333 | ) ); |
||
| 1334 | |||
| 1335 | $i = 0; |
||
| 1336 | foreach ( $forms as $form_info ) { |
||
| 1337 | $earnings['earnings'][ $i ] = array( |
||
| 1338 | $form_info->post_name => give_get_form_earnings_stats( $form_info->ID ), |
||
| 1339 | ); |
||
| 1340 | $i ++; |
||
| 1341 | } |
||
| 1342 | View Code Duplication | } else { |
|
| 1343 | if ( get_post_type( $args['form'] ) == 'give_forms' ) { |
||
| 1344 | $form_info = get_post( $args['form'] ); |
||
| 1345 | $earnings['earnings'][0] = array( |
||
| 1346 | $form_info->post_name => give_get_form_earnings_stats( $args['form'] ), |
||
| 1347 | ); |
||
| 1348 | } else { |
||
| 1349 | $error['error'] = sprintf( /* translators: %s: form */ |
||
| 1350 | __( 'Form %s not found.', 'give' ), $args['form'] ); |
||
| 1351 | } |
||
| 1352 | }// End if(). |
||
| 1353 | |||
| 1354 | if ( ! empty( $error ) ) { |
||
| 1355 | return $error; |
||
| 1356 | } |
||
| 1357 | |||
| 1358 | return $earnings; |
||
| 1359 | } elseif ( $args['type'] == 'donors' ) { |
||
| 1360 | $donors = new Give_DB_Donors(); |
||
| 1361 | $stats['donations']['total_donors'] = $donors->count(); |
||
| 1362 | |||
| 1363 | return $stats; |
||
| 1364 | |||
| 1365 | } elseif ( empty( $args['type'] ) ) { |
||
| 1366 | $stats = array_merge( $stats, $this->get_default_sales_stats() ); |
||
| 1367 | $stats = array_merge( $stats, $this->get_default_earnings_stats() ); |
||
| 1368 | |||
| 1369 | return array( |
||
| 1370 | 'stats' => $stats, |
||
| 1371 | ); |
||
| 1372 | }// End if(). |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | /** |
||
| 1376 | * Retrieves Recent Donations |
||
| 1377 | * |
||
| 1378 | * @access public |
||
| 1379 | * @since 1.1 |
||
| 1380 | * |
||
| 1381 | * @param $args array |
||
| 1382 | * |
||
| 1383 | * @return array |
||
| 1384 | */ |
||
| 1385 | public function get_recent_donations( $args = array() ) { |
||
| 1386 | global $wp_query; |
||
| 1387 | |||
| 1388 | $defaults = array( |
||
| 1389 | 'id' => null, |
||
| 1390 | 'date' => null, |
||
| 1391 | 'startdate' => null, |
||
| 1392 | 'enddate' => null, |
||
| 1393 | ); |
||
| 1394 | |||
| 1395 | $args = wp_parse_args( $args, $defaults ); |
||
| 1396 | |||
| 1397 | $donations = array(); |
||
| 1398 | |||
| 1399 | if ( ! user_can( $this->user_id, 'view_give_reports' ) && ! $this->override ) { |
||
| 1400 | return $donations; |
||
| 1401 | } |
||
| 1402 | |||
| 1403 | if ( isset( $wp_query->query_vars['id'] ) ) { |
||
| 1404 | $query = array(); |
||
| 1405 | $query[] = new Give_Payment( $wp_query->query_vars['id'] ); |
||
| 1406 | } elseif ( isset( $wp_query->query_vars['purchasekey'] ) ) { |
||
| 1407 | $query = array(); |
||
| 1408 | $query[] = give_get_payment_by( 'key', $wp_query->query_vars['purchasekey'] ); |
||
| 1409 | } elseif ( isset( $wp_query->query_vars['email'] ) ) { |
||
| 1410 | $args = array( |
||
| 1411 | 'fields' => 'ids', |
||
| 1412 | 'meta_key' => '_give_payment_user_email', |
||
| 1413 | 'meta_value' => $wp_query->query_vars['email'], |
||
| 1414 | 'number' => $this->per_page(), |
||
| 1415 | 'page' => $this->get_paged(), |
||
| 1416 | ); |
||
| 1417 | $query = give_get_payments( $args ); |
||
| 1418 | } elseif ( isset( $wp_query->query_vars['date'] ) ) { |
||
| 1419 | |||
| 1420 | $current_time = current_time( 'timestamp' ); |
||
| 1421 | $dates = $this->get_dates( $args ); |
||
| 1422 | $start_date = ''; |
||
| 1423 | $end_date = ''; |
||
| 1424 | |||
| 1425 | /** |
||
| 1426 | * Switch case for date query argument |
||
| 1427 | * |
||
| 1428 | * @since 1.8.8 |
||
| 1429 | * |
||
| 1430 | * @params text date | today, yesterday or range |
||
| 1431 | * @params date startdate | required when date = range and format to be YYYYMMDD (i.e. 20170524) |
||
| 1432 | * @params date enddate | required when date = range and format to be YYYYMMDD (i.e. 20170524) |
||
| 1433 | */ |
||
| 1434 | switch ( $wp_query->query_vars['date'] ) { |
||
| 1435 | |||
| 1436 | case 'today': |
||
| 1437 | |||
| 1438 | // Set and Format Start and End Date to be date of today. |
||
| 1439 | $start_date = $end_date = date( 'Y/m/d', $current_time ); |
||
| 1440 | |||
| 1441 | break; |
||
| 1442 | |||
| 1443 | case 'yesterday': |
||
| 1444 | |||
| 1445 | // Set and Format Start and End Date to be date of yesterday. |
||
| 1446 | $start_date = $end_date = date( 'Y/m', $current_time ) . '/' . ( date( 'd', $current_time ) - 1 ); |
||
| 1447 | |||
| 1448 | break; |
||
| 1449 | |||
| 1450 | case 'range': |
||
| 1451 | |||
| 1452 | // Format Start Date and End Date for filtering payment based on date range. |
||
| 1453 | $start_date = $dates['year'] . '/' . $dates['m_start'] . '/' . $dates['day_start']; |
||
| 1454 | $end_date = $dates['year_end'] . '/' . $dates['m_end'] . '/' . $dates['day_end']; |
||
| 1455 | |||
| 1456 | break; |
||
| 1457 | |||
| 1458 | } |
||
| 1459 | |||
| 1460 | $args = array( |
||
| 1461 | 'fields' => 'ids', |
||
| 1462 | 'start_date' => $start_date, |
||
| 1463 | 'end_date' => $end_date, |
||
| 1464 | 'number' => $this->per_page(), |
||
| 1465 | 'page' => $this->get_paged(), |
||
| 1466 | ); |
||
| 1467 | |||
| 1468 | $query = give_get_payments( $args ); |
||
| 1469 | } else { |
||
| 1470 | $args = array( |
||
| 1471 | 'fields' => 'ids', |
||
| 1472 | 'number' => $this->per_page(), |
||
| 1473 | 'page' => $this->get_paged(), |
||
| 1474 | ); |
||
| 1475 | $query = give_get_payments( $args ); |
||
| 1476 | }// End if(). |
||
| 1477 | |||
| 1478 | if ( $query ) { |
||
| 1479 | $i = 0; |
||
| 1480 | foreach ( $query as $payment ) { |
||
| 1481 | |||
| 1482 | if ( is_numeric( $payment ) ) { |
||
| 1483 | $payment = new Give_Payment( $payment ); |
||
| 1484 | $payment_meta = $payment->get_meta(); |
||
| 1485 | $user_info = $payment->user_info; |
||
| 1486 | } |
||
| 1487 | |||
| 1488 | $payment_meta = $payment->get_meta(); |
||
| 1489 | $user_info = $payment->user_info; |
||
| 1490 | |||
| 1491 | $first_name = isset( $user_info['first_name'] ) ? $user_info['first_name'] : ''; |
||
| 1492 | $last_name = isset( $user_info['last_name'] ) ? $user_info['last_name'] : ''; |
||
| 1493 | |||
| 1494 | $donations['donations'][ $i ]['ID'] = $payment->number; |
||
| 1495 | $donations['donations'][ $i ]['transaction_id'] = $payment->transaction_id; |
||
| 1496 | $donations['donations'][ $i ]['key'] = $payment->key; |
||
| 1497 | $donations['donations'][ $i ]['total'] = $payment->total; |
||
| 1498 | $donations['donations'][ $i ]['status'] = give_get_payment_status( $payment, true ); |
||
| 1499 | $donations['donations'][ $i ]['gateway'] = $payment->gateway; |
||
| 1500 | $donations['donations'][ $i ]['name'] = $first_name . ' ' . $last_name; |
||
| 1501 | $donations['donations'][ $i ]['fname'] = $first_name; |
||
| 1502 | $donations['donations'][ $i ]['lname'] = $last_name; |
||
| 1503 | $donations['donations'][ $i ]['email'] = $payment->email; |
||
| 1504 | $donations['donations'][ $i ]['date'] = $payment->date; |
||
| 1505 | |||
| 1506 | $form_id = isset( $payment_meta['form_id'] ) ? $payment_meta['form_id'] : $payment_meta; |
||
| 1507 | $price = isset( $payment_meta['form_id'] ) ? give_get_form_price( $payment_meta['form_id'] ) : false; |
||
| 1508 | $price_id = isset( $payment_meta['price_id'] ) ? $payment_meta['price_id'] : null; |
||
| 1509 | |||
| 1510 | $donations['donations'][ $i ]['form']['id'] = $form_id; |
||
| 1511 | $donations['donations'][ $i ]['form']['name'] = get_the_title( $payment_meta['form_id'] ); |
||
| 1512 | $donations['donations'][ $i ]['form']['price'] = $price; |
||
| 1513 | |||
| 1514 | if ( give_has_variable_prices( $form_id ) ) { |
||
| 1515 | if ( isset( $payment_meta['price_id'] ) ) { |
||
| 1516 | $price_name = give_get_price_option_name( $form_id, $payment_meta['price_id'], $payment->ID ); |
||
| 1517 | $donations['donations'][ $i ]['form']['price_name'] = $price_name; |
||
| 1518 | $donations['donations'][ $i ]['form']['price_id'] = $price_id; |
||
| 1519 | $donations['donations'][ $i ]['form']['price'] = give_get_price_option_amount( $form_id, $price_id ); |
||
| 1520 | |||
| 1521 | } |
||
| 1522 | } |
||
| 1523 | |||
| 1524 | // Add custom meta to API |
||
| 1525 | foreach ( $payment_meta as $meta_key => $meta_value ) { |
||
| 1526 | |||
| 1527 | $exceptions = array( |
||
| 1528 | 'form_title', |
||
| 1529 | 'form_id', |
||
| 1530 | 'price_id', |
||
| 1531 | 'user_info', |
||
| 1532 | 'key', |
||
| 1533 | 'email', |
||
| 1534 | 'date', |
||
| 1535 | ); |
||
| 1536 | |||
| 1537 | // Don't clutter up results with dupes |
||
| 1538 | if ( in_array( $meta_key, $exceptions ) ) { |
||
| 1539 | continue; |
||
| 1540 | } |
||
| 1541 | |||
| 1542 | $donations['donations'][ $i ]['payment_meta'][ $meta_key ] = $meta_value; |
||
| 1543 | |||
| 1544 | } |
||
| 1545 | |||
| 1546 | $i ++; |
||
| 1547 | }// End foreach(). |
||
| 1548 | }// End if(). |
||
| 1549 | |||
| 1550 | return apply_filters( 'give_api_donations_endpoint', $donations ); |
||
| 1551 | } |
||
| 1552 | |||
| 1553 | /** |
||
| 1554 | * Retrieve the output format. |
||
| 1555 | * |
||
| 1556 | * Determines whether results should be displayed in XML or JSON. |
||
| 1557 | * |
||
| 1558 | * @since 1.1 |
||
| 1559 | * @access public |
||
| 1560 | * |
||
| 1561 | * @return mixed |
||
| 1562 | */ |
||
| 1563 | public function get_output_format() { |
||
| 1570 | |||
| 1571 | |||
| 1572 | /** |
||
| 1573 | * Log each API request, if enabled. |
||
| 1574 | * |
||
| 1575 | * @access private |
||
| 1576 | * @since 1.1 |
||
| 1577 | * |
||
| 1578 | * @global Give_Logging $give_logs |
||
| 1579 | * @global WP_Query $wp_query |
||
| 1580 | * |
||
| 1581 | * @param array $data |
||
| 1582 | * |
||
| 1583 | * @return void |
||
| 1584 | */ |
||
| 1585 | private function log_request( $data = array() ) { |
||
| 1633 | |||
| 1634 | |||
| 1635 | /** |
||
| 1636 | * Retrieve the output data. |
||
| 1637 | * |
||
| 1638 | * @access public |
||
| 1639 | * @since 1.1 |
||
| 1640 | * @return array |
||
| 1641 | */ |
||
| 1642 | public function get_output() { |
||
| 1645 | |||
| 1646 | /** |
||
| 1647 | * Output Query in either JSON/XML. |
||
| 1648 | * The query data is outputted as JSON by default. |
||
| 1649 | * |
||
| 1650 | * @since 1.1 |
||
| 1651 | * @global WP_Query $wp_query |
||
| 1652 | * |
||
| 1653 | * @param int $status_code |
||
| 1654 | */ |
||
| 1655 | public function output( $status_code = 200 ) { |
||
| 1722 | |||
| 1723 | /** |
||
| 1724 | * Modify User Profile |
||
| 1725 | * |
||
| 1726 | * Modifies the output of profile.php to add key generation/revocation. |
||
| 1727 | * |
||
| 1728 | * @access public |
||
| 1729 | * @since 1.1 |
||
| 1730 | * |
||
| 1731 | * @param object $user Current user info |
||
| 1732 | * |
||
| 1733 | * @return void |
||
| 1734 | */ |
||
| 1735 | function user_key_field( $user ) { |
||
| 1736 | |||
| 1737 | if ( ( give_get_option( 'api_allow_user_keys', false ) || current_user_can( 'manage_give_settings' ) ) && current_user_can( 'edit_user', $user->ID ) ) { |
||
| 1738 | |||
| 1739 | $user = get_userdata( $user->ID ); |
||
| 1740 | ?> |
||
| 1741 | <table class="form-table"> |
||
| 1742 | <tbody> |
||
| 1743 | <tr> |
||
| 1744 | <th> |
||
| 1745 | <?php _e( 'Give API Keys', 'give' ); ?> |
||
| 1746 | </th> |
||
| 1747 | <td> |
||
| 1777 | |||
| 1778 | /** |
||
| 1779 | * Process an API key generation/revocation |
||
| 1780 | * |
||
| 1781 | * @access public |
||
| 1782 | * @since 1.1 |
||
| 1783 | * |
||
| 1784 | * @param array $args |
||
| 1785 | * |
||
| 1786 | * @return void |
||
| 1787 | */ |
||
| 1788 | public function process_api_key( $args ) { |
||
| 1849 | |||
| 1850 | /** |
||
| 1851 | * Generate new API keys for a user |
||
| 1852 | * |
||
| 1853 | * @param int $user_id User ID the key is being generated for. |
||
| 1854 | * @param boolean $regenerate Regenerate the key for the user. |
||
| 1855 | * |
||
| 1856 | * @access public |
||
| 1857 | * @since 1.1 |
||
| 1858 | * |
||
| 1859 | * @return boolean True if (re)generated successfully, false otherwise. |
||
| 1860 | */ |
||
| 1861 | public function generate_api_key( $user_id = 0, $regenerate = false ) { |
||
| 1917 | |||
| 1918 | /** |
||
| 1919 | * Revoke a users API keys |
||
| 1920 | * |
||
| 1921 | * @access public |
||
| 1922 | * @since 1.1 |
||
| 1923 | * |
||
| 1924 | * @param int $user_id User ID of user to revoke key for |
||
| 1925 | * |
||
| 1926 | * @return bool |
||
| 1927 | */ |
||
| 1928 | public function revoke_api_key( $user_id = 0 ) { |
||
| 1954 | |||
| 1955 | public function get_version() { |
||
| 1958 | |||
| 1959 | /** |
||
| 1960 | * Generate the public key for a user |
||
| 1961 | * |
||
| 1962 | * @access private |
||
| 1963 | * @since 1.1 |
||
| 1964 | * |
||
| 1965 | * @param string $user_email |
||
| 1966 | * |
||
| 1967 | * @return string |
||
| 1968 | */ |
||
| 1969 | private function generate_public_key( $user_email = '' ) { |
||
| 1975 | |||
| 1976 | /** |
||
| 1977 | * Generate the secret key for a user |
||
| 1978 | * |
||
| 1979 | * @access private |
||
| 1980 | * @since 1.1 |
||
| 1981 | * |
||
| 1982 | * @param int $user_id |
||
| 1983 | * |
||
| 1984 | * @return string |
||
| 1985 | */ |
||
| 1986 | private function generate_private_key( $user_id = 0 ) { |
||
| 1992 | |||
| 1993 | /** |
||
| 1994 | * Retrieve the user's token |
||
| 1995 | * |
||
| 1996 | * @access private |
||
| 1997 | * @since 1.1 |
||
| 1998 | * |
||
| 1999 | * @param int $user_id |
||
| 2000 | * |
||
| 2001 | * @return string |
||
| 2002 | */ |
||
| 2003 | public function get_token( $user_id = 0 ) { |
||
| 2006 | |||
| 2007 | /** |
||
| 2008 | * Generate the default donation stats returned by the 'stats' endpoint |
||
| 2009 | * |
||
| 2010 | * @access private |
||
| 2011 | * @since 1.1 |
||
| 2012 | * @return array default sales statistics |
||
| 2013 | */ |
||
| 2014 | View Code Duplication | private function get_default_sales_stats() { |
|
| 2025 | |||
| 2026 | /** |
||
| 2027 | * Generate the default earnings stats returned by the 'stats' endpoint |
||
| 2028 | * |
||
| 2029 | * @access private |
||
| 2030 | * @since 1.1 |
||
| 2031 | * @return array default earnings statistics |
||
| 2032 | */ |
||
| 2033 | View Code Duplication | private function get_default_earnings_stats() { |
|
| 2044 | |||
| 2045 | /** |
||
| 2046 | * API Key Backwards Compatibility |
||
| 2047 | * |
||
| 2048 | * A Backwards Compatibility call for the change of meta_key/value for users API Keys. |
||
| 2049 | * |
||
| 2050 | * @since 1.3.6 |
||
| 2051 | * |
||
| 2052 | * @param string $check Whether to check the cache or not |
||
| 2053 | * @param int $object_id The User ID being passed |
||
| 2054 | * @param string $meta_key The user meta key |
||
| 2055 | * @param bool $single If it should return a single value or array |
||
| 2056 | * |
||
| 2057 | * @return string The API key/secret for the user supplied |
||
| 2058 | */ |
||
| 2059 | public function api_key_backwards_compat( $check, $object_id, $meta_key, $single ) { |
||
| 2083 | |||
| 2084 | } |
||
| 2085 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..