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() { |
||
| 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 ) { |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Retrieve the API versions |
||
| 214 | * |
||
| 215 | * @access public |
||
| 216 | * @since 1.1 |
||
| 217 | * @return array |
||
| 218 | */ |
||
| 219 | public function get_versions() { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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 = '' ) { |
||
| 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 ) { |
|
| 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 ) { |
|
| 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() { |
|
| 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() { |
|
| 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() { |
|
| 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() { |
|
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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() { |
||
| 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() ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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 ) { |
||
| 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() ) { |
||
| 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 | |||
| 1423 | /** |
||
| 1424 | * Switch case for date query argument |
||
| 1425 | * |
||
| 1426 | * @since 1.8.8 |
||
| 1427 | * |
||
| 1428 | * @params text date | today, yesterday or range |
||
| 1429 | * @params date startdate | required when date = range and format to be YYYYMMDD (i.e. 20170524) |
||
| 1430 | * @params date enddate | required when date = range and format to be YYYYMMDD (i.e. 20170524) |
||
| 1431 | */ |
||
| 1432 | switch ( $wp_query->query_vars['date'] ) { |
||
| 1433 | |||
| 1434 | case 'today': |
||
| 1435 | |||
| 1436 | // Set and Format Start and End Date to be date of today. |
||
| 1437 | $start_date = $end_date = date( 'Y/m/d', $current_time ); |
||
| 1438 | |||
| 1439 | break; |
||
| 1440 | |||
| 1441 | case 'yesterday': |
||
| 1442 | |||
| 1443 | // Set and Format Start and End Date to be date of yesterday. |
||
| 1444 | $start_date = $end_date = date( 'Y/m', $current_time ) . '/' . ( date( 'd', $current_time ) - 1 ); |
||
| 1445 | |||
| 1446 | break; |
||
| 1447 | |||
| 1448 | case 'range': |
||
| 1449 | |||
| 1450 | // Format Start Date and End Date for filtering payment based on date range. |
||
| 1451 | $start_date = $dates['year'] . '/' . $dates['m_start'] . '/' . $dates['day_start']; |
||
| 1452 | $end_date = $dates['year_end'] . '/' . $dates['m_end'] . '/' . $dates['day_end']; |
||
| 1453 | |||
| 1454 | break; |
||
| 1455 | |||
| 1456 | } |
||
| 1457 | |||
| 1458 | $args = array( |
||
| 1459 | 'fields' => 'ids', |
||
| 1460 | 'start_date' => $start_date, |
||
| 1461 | 'end_date' => $end_date, |
||
| 1462 | 'number' => $this->per_page(), |
||
| 1463 | 'page' => $this->get_paged(), |
||
| 1464 | ); |
||
| 1465 | |||
| 1466 | $query = give_get_payments( $args ); |
||
| 1467 | } else { |
||
| 1468 | $args = array( |
||
| 1469 | 'fields' => 'ids', |
||
| 1470 | 'number' => $this->per_page(), |
||
| 1471 | 'page' => $this->get_paged(), |
||
| 1472 | ); |
||
| 1473 | $query = give_get_payments( $args ); |
||
| 1474 | }// End if(). |
||
| 1475 | |||
| 1476 | if ( $query ) { |
||
| 1477 | $i = 0; |
||
| 1478 | foreach ( $query as $payment ) { |
||
| 1479 | |||
| 1480 | if ( is_numeric( $payment ) ) { |
||
| 1481 | $payment = new Give_Payment( $payment ); |
||
| 1482 | $payment_meta = $payment->get_meta(); |
||
| 1483 | $user_info = $payment->user_info; |
||
| 1484 | } |
||
| 1485 | |||
| 1486 | $payment_meta = $payment->get_meta(); |
||
| 1487 | $user_info = $payment->user_info; |
||
| 1488 | |||
| 1489 | $first_name = isset( $user_info['first_name'] ) ? $user_info['first_name'] : ''; |
||
| 1490 | $last_name = isset( $user_info['last_name'] ) ? $user_info['last_name'] : ''; |
||
| 1491 | |||
| 1492 | $donations['donations'][ $i ]['ID'] = $payment->number; |
||
| 1493 | $donations['donations'][ $i ]['transaction_id'] = $payment->transaction_id; |
||
| 1494 | $donations['donations'][ $i ]['key'] = $payment->key; |
||
| 1495 | $donations['donations'][ $i ]['total'] = $payment->total; |
||
| 1496 | $donations['donations'][ $i ]['status'] = give_get_payment_status( $payment, true ); |
||
| 1497 | $donations['donations'][ $i ]['gateway'] = $payment->gateway; |
||
| 1498 | $donations['donations'][ $i ]['name'] = $first_name . ' ' . $last_name; |
||
| 1499 | $donations['donations'][ $i ]['fname'] = $first_name; |
||
| 1500 | $donations['donations'][ $i ]['lname'] = $last_name; |
||
| 1501 | $donations['donations'][ $i ]['email'] = $payment->email; |
||
| 1502 | $donations['donations'][ $i ]['date'] = $payment->date; |
||
| 1503 | |||
| 1504 | $form_id = isset( $payment_meta['form_id'] ) ? $payment_meta['form_id'] : $payment_meta; |
||
| 1505 | $price = isset( $payment_meta['form_id'] ) ? give_get_form_price( $payment_meta['form_id'] ) : false; |
||
| 1506 | $price_id = isset( $payment_meta['price_id'] ) ? $payment_meta['price_id'] : null; |
||
| 1507 | |||
| 1508 | $donations['donations'][ $i ]['form']['id'] = $form_id; |
||
| 1509 | $donations['donations'][ $i ]['form']['name'] = get_the_title( $payment_meta['form_id'] ); |
||
| 1510 | $donations['donations'][ $i ]['form']['price'] = $price; |
||
| 1511 | |||
| 1512 | if ( give_has_variable_prices( $form_id ) ) { |
||
| 1513 | if ( isset( $payment_meta['price_id'] ) ) { |
||
| 1514 | $price_name = give_get_price_option_name( $form_id, $payment_meta['price_id'], $payment->ID ); |
||
| 1515 | $donations['donations'][ $i ]['form']['price_name'] = $price_name; |
||
| 1516 | $donations['donations'][ $i ]['form']['price_id'] = $price_id; |
||
| 1517 | $donations['donations'][ $i ]['form']['price'] = give_get_price_option_amount( $form_id, $price_id ); |
||
| 1518 | |||
| 1519 | } |
||
| 1520 | } |
||
| 1521 | |||
| 1522 | // Add custom meta to API |
||
| 1523 | foreach ( $payment_meta as $meta_key => $meta_value ) { |
||
| 1524 | |||
| 1525 | $exceptions = array( |
||
| 1526 | 'form_title', |
||
| 1527 | 'form_id', |
||
| 1528 | 'price_id', |
||
| 1529 | 'user_info', |
||
| 1530 | 'key', |
||
| 1531 | 'email', |
||
| 1532 | 'date', |
||
| 1533 | ); |
||
| 1534 | |||
| 1535 | // Don't clutter up results with dupes |
||
| 1536 | if ( in_array( $meta_key, $exceptions ) ) { |
||
| 1537 | continue; |
||
| 1538 | } |
||
| 1539 | |||
| 1540 | $donations['donations'][ $i ]['payment_meta'][ $meta_key ] = $meta_value; |
||
| 1541 | |||
| 1542 | } |
||
| 1543 | |||
| 1544 | $i ++; |
||
| 1545 | }// End foreach(). |
||
| 1546 | }// End if(). |
||
| 1547 | |||
| 1548 | return apply_filters( 'give_api_donations_endpoint', $donations ); |
||
| 1549 | } |
||
| 1550 | |||
| 1551 | /** |
||
| 1552 | * Retrieve the output format. |
||
| 1553 | * |
||
| 1554 | * Determines whether results should be displayed in XML or JSON. |
||
| 1555 | * |
||
| 1556 | * @since 1.1 |
||
| 1557 | * @access public |
||
| 1558 | * |
||
| 1559 | * @return mixed |
||
| 1560 | */ |
||
| 1561 | public function get_output_format() { |
||
| 1568 | |||
| 1569 | |||
| 1570 | /** |
||
| 1571 | * Log each API request, if enabled. |
||
| 1572 | * |
||
| 1573 | * @access private |
||
| 1574 | * @since 1.1 |
||
| 1575 | * |
||
| 1576 | * @global Give_Logging $give_logs |
||
| 1577 | * @global WP_Query $wp_query |
||
| 1578 | * |
||
| 1579 | * @param array $data |
||
| 1580 | * |
||
| 1581 | * @return void |
||
| 1582 | */ |
||
| 1583 | private function log_request( $data = array() ) { |
||
| 1631 | |||
| 1632 | |||
| 1633 | /** |
||
| 1634 | * Retrieve the output data. |
||
| 1635 | * |
||
| 1636 | * @access public |
||
| 1637 | * @since 1.1 |
||
| 1638 | * @return array |
||
| 1639 | */ |
||
| 1640 | public function get_output() { |
||
| 1643 | |||
| 1644 | /** |
||
| 1645 | * Output Query in either JSON/XML. |
||
| 1646 | * The query data is outputted as JSON by default. |
||
| 1647 | * |
||
| 1648 | * @since 1.1 |
||
| 1649 | * @global WP_Query $wp_query |
||
| 1650 | * |
||
| 1651 | * @param int $status_code |
||
| 1652 | */ |
||
| 1653 | public function output( $status_code = 200 ) { |
||
| 1720 | |||
| 1721 | /** |
||
| 1722 | * Modify User Profile |
||
| 1723 | * |
||
| 1724 | * Modifies the output of profile.php to add key generation/revocation. |
||
| 1725 | * |
||
| 1726 | * @access public |
||
| 1727 | * @since 1.1 |
||
| 1728 | * |
||
| 1729 | * @param object $user Current user info |
||
| 1730 | * |
||
| 1731 | * @return void |
||
| 1732 | */ |
||
| 1733 | function user_key_field( $user ) { |
||
| 1775 | |||
| 1776 | /** |
||
| 1777 | * Process an API key generation/revocation |
||
| 1778 | * |
||
| 1779 | * @access public |
||
| 1780 | * @since 1.1 |
||
| 1781 | * |
||
| 1782 | * @param array $args |
||
| 1783 | * |
||
| 1784 | * @return void |
||
| 1785 | */ |
||
| 1786 | public function process_api_key( $args ) { |
||
| 1787 | |||
| 1788 | View Code Duplication | if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'give-api-nonce' ) ) { |
|
| 1789 | wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( |
||
| 1790 | 'response' => 403, |
||
| 1791 | ) ); |
||
| 1792 | } |
||
| 1793 | |||
| 1794 | if ( empty( $args['user_id'] ) ) { |
||
| 1795 | wp_die( __( 'User ID Required.', 'give' ), __( 'Error', 'give' ), array( |
||
| 1796 | 'response' => 401, |
||
| 1797 | ) ); |
||
| 1798 | } |
||
| 1799 | |||
| 1800 | if ( is_numeric( $args['user_id'] ) ) { |
||
| 1801 | $user_id = isset( $args['user_id'] ) ? absint( $args['user_id'] ) : get_current_user_id(); |
||
| 1802 | } else { |
||
| 1803 | $userdata = get_user_by( 'login', $args['user_id'] ); |
||
| 1804 | $user_id = $userdata->ID; |
||
| 1805 | } |
||
| 1806 | $process = isset( $args['give_api_process'] ) ? strtolower( $args['give_api_process'] ) : false; |
||
| 1807 | |||
| 1808 | if ( $user_id == get_current_user_id() && ! give_get_option( 'allow_user_api_keys' ) && ! current_user_can( 'manage_give_settings' ) ) { |
||
| 1809 | wp_die( sprintf( /* translators: %s: process */ |
||
| 1810 | esc_html__( 'You do not have permission to %s API keys for this user.', 'give' ), $process ), esc_html__( 'Error', 'give' ), array( |
||
| 1811 | 'response' => 403, |
||
| 1812 | ) ); |
||
| 1813 | } elseif ( ! current_user_can( 'manage_give_settings' ) ) { |
||
| 1814 | wp_die( sprintf( /* translators: %s: process */ |
||
| 1815 | esc_html__( 'You do not have permission to %s API keys for this user.', 'give' ), $process ), esc_html__( 'Error', 'give' ), array( |
||
| 1816 | 'response' => 403, |
||
| 1817 | ) ); |
||
| 1818 | } |
||
| 1819 | |||
| 1820 | switch ( $process ) { |
||
| 1821 | case 'generate': |
||
| 1822 | if ( $this->generate_api_key( $user_id ) ) { |
||
| 1823 | Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) ); |
||
| 1824 | wp_redirect( add_query_arg( 'give-message', 'api-key-generated', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1825 | exit(); |
||
| 1826 | } else { |
||
| 1827 | wp_redirect( add_query_arg( 'give-message', 'api-key-failed', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1828 | exit(); |
||
| 1829 | } |
||
| 1830 | break; |
||
| 1831 | View Code Duplication | case 'regenerate': |
|
| 1832 | $this->generate_api_key( $user_id, true ); |
||
| 1833 | Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) ); |
||
| 1834 | wp_redirect( add_query_arg( 'give-message', 'api-key-regenerated', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1835 | exit(); |
||
| 1836 | break; |
||
| 1837 | View Code Duplication | case 'revoke': |
|
| 1838 | $this->revoke_api_key( $user_id ); |
||
| 1839 | Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) ); |
||
| 1840 | wp_redirect( add_query_arg( 'give-message', 'api-key-revoked', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1841 | exit(); |
||
| 1842 | break; |
||
| 1843 | default; |
||
| 1844 | break; |
||
| 1845 | } |
||
| 1846 | } |
||
| 1847 | |||
| 1848 | /** |
||
| 1849 | * Generate new API keys for a user |
||
| 1850 | * |
||
| 1851 | * @param int $user_id User ID the key is being generated for. |
||
| 1852 | * @param boolean $regenerate Regenerate the key for the user. |
||
| 1853 | * |
||
| 1854 | * @access public |
||
| 1855 | * @since 1.1 |
||
| 1856 | * |
||
| 1857 | * @return boolean True if (re)generated succesfully, false otherwise. |
||
| 1858 | */ |
||
| 1859 | public function generate_api_key( $user_id = 0, $regenerate = false ) { |
||
| 1898 | |||
| 1899 | /** |
||
| 1900 | * Revoke a users API keys |
||
| 1901 | * |
||
| 1902 | * @access public |
||
| 1903 | * @since 1.1 |
||
| 1904 | * |
||
| 1905 | * @param int $user_id User ID of user to revoke key for |
||
| 1906 | * |
||
| 1907 | * @return bool |
||
| 1908 | */ |
||
| 1909 | public function revoke_api_key( $user_id = 0 ) { |
||
| 1935 | |||
| 1936 | public function get_version() { |
||
| 1939 | |||
| 1940 | /** |
||
| 1941 | * Generate the public key for a user |
||
| 1942 | * |
||
| 1943 | * @access private |
||
| 1944 | * @since 1.1 |
||
| 1945 | * |
||
| 1946 | * @param string $user_email |
||
| 1947 | * |
||
| 1948 | * @return string |
||
| 1949 | */ |
||
| 1950 | private function generate_public_key( $user_email = '' ) { |
||
| 1956 | |||
| 1957 | /** |
||
| 1958 | * Generate the secret key for a user |
||
| 1959 | * |
||
| 1960 | * @access private |
||
| 1961 | * @since 1.1 |
||
| 1962 | * |
||
| 1963 | * @param int $user_id |
||
| 1964 | * |
||
| 1965 | * @return string |
||
| 1966 | */ |
||
| 1967 | private function generate_private_key( $user_id = 0 ) { |
||
| 1973 | |||
| 1974 | /** |
||
| 1975 | * Retrieve the user's token |
||
| 1976 | * |
||
| 1977 | * @access private |
||
| 1978 | * @since 1.1 |
||
| 1979 | * |
||
| 1980 | * @param int $user_id |
||
| 1981 | * |
||
| 1982 | * @return string |
||
| 1983 | */ |
||
| 1984 | public function get_token( $user_id = 0 ) { |
||
| 1987 | |||
| 1988 | /** |
||
| 1989 | * Generate the default donation stats returned by the 'stats' endpoint |
||
| 1990 | * |
||
| 1991 | * @access private |
||
| 1992 | * @since 1.1 |
||
| 1993 | * @return array default sales statistics |
||
| 1994 | */ |
||
| 1995 | View Code Duplication | private function get_default_sales_stats() { |
|
| 2006 | |||
| 2007 | /** |
||
| 2008 | * Generate the default earnings stats returned by the 'stats' endpoint |
||
| 2009 | * |
||
| 2010 | * @access private |
||
| 2011 | * @since 1.1 |
||
| 2012 | * @return array default earnings statistics |
||
| 2013 | */ |
||
| 2014 | View Code Duplication | private function get_default_earnings_stats() { |
|
| 2025 | |||
| 2026 | /** |
||
| 2027 | * API Key Backwards Compatibility |
||
| 2028 | * |
||
| 2029 | * A Backwards Compatibility call for the change of meta_key/value for users API Keys. |
||
| 2030 | * |
||
| 2031 | * @since 1.3.6 |
||
| 2032 | * |
||
| 2033 | * @param string $check Whether to check the cache or not |
||
| 2034 | * @param int $object_id The User ID being passed |
||
| 2035 | * @param string $meta_key The user meta key |
||
| 2036 | * @param bool $single If it should return a single value or array |
||
| 2037 | * |
||
| 2038 | * @return string The API key/secret for the user supplied |
||
| 2039 | */ |
||
| 2040 | public function api_key_backwards_compat( $check, $object_id, $meta_key, $single ) { |
||
| 2064 | |||
| 2065 | } |
||
| 2066 |
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..