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 | * @param array $rewrite_rules WordPress Rewrite Rules |
||
| 177 | * |
||
| 178 | * @since 1.1 |
||
| 179 | */ |
||
| 180 | public function add_endpoint( $rewrite_rules ) { |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Registers query vars for API access |
||
| 186 | * |
||
| 187 | * @access public |
||
| 188 | * @since 1.1 |
||
| 189 | * |
||
| 190 | * @param array $vars Query vars |
||
| 191 | * |
||
| 192 | * @return string[] $vars New query vars |
||
| 193 | */ |
||
| 194 | public function query_vars( $vars ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Retrieve the API versions |
||
| 216 | * |
||
| 217 | * @access public |
||
| 218 | * @since 1.1 |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | public function get_versions() { |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Retrieve the API version that was queried |
||
| 227 | * |
||
| 228 | * @access public |
||
| 229 | * @since 1.1 |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | public function get_queried_version() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Retrieves the default version of the API to use |
||
| 238 | * |
||
| 239 | * @access public |
||
| 240 | * @since 1.1 |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | public function get_default_version() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Sets the version of the API that was queried. |
||
| 258 | * |
||
| 259 | * Falls back to the default version if no version is specified |
||
| 260 | * |
||
| 261 | * @access private |
||
| 262 | * @since 1.1 |
||
| 263 | */ |
||
| 264 | private function set_queried_version() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Validate the API request |
||
| 296 | * |
||
| 297 | * Checks for the user's public key and token against the secret key. |
||
| 298 | * |
||
| 299 | * @access private |
||
| 300 | * @global object $wp_query WordPress Query |
||
| 301 | * @uses Give_API::get_user() |
||
| 302 | * @uses Give_API::invalid_key() |
||
| 303 | * @uses Give_API::invalid_auth() |
||
| 304 | * @since 1.1 |
||
| 305 | * @return bool |
||
| 306 | */ |
||
| 307 | private function validate_request() { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Retrieve the user ID based on the public key provided |
||
| 348 | * |
||
| 349 | * @access public |
||
| 350 | * @since 1.1 |
||
| 351 | * @global WPDB $wpdb Used to query the database using the WordPress |
||
| 352 | * Database API |
||
| 353 | * |
||
| 354 | * @param string $key Public Key |
||
| 355 | * |
||
| 356 | * @return bool if user ID is found, false otherwise |
||
| 357 | */ |
||
| 358 | public function get_user( $key = '' ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get user public key. |
||
| 387 | * |
||
| 388 | * @param int $user_id |
||
| 389 | * |
||
| 390 | * @return mixed|null|string |
||
| 391 | */ |
||
| 392 | public function get_user_public_key( $user_id = 0 ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Get user secret key. |
||
| 412 | * |
||
| 413 | * @param int $user_id |
||
| 414 | * |
||
| 415 | * @return mixed|null|string |
||
| 416 | */ |
||
| 417 | public function get_user_secret_key( $user_id = 0 ) { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Displays a missing authentication error if all the parameters are not met. |
||
| 437 | * provided |
||
| 438 | * |
||
| 439 | * @access private |
||
| 440 | * @uses Give_API::output() |
||
| 441 | * @since 1.1 |
||
| 442 | */ |
||
| 443 | private function missing_auth() { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Displays an authentication failed error if the user failed to provide valid |
||
| 453 | * credentials |
||
| 454 | * |
||
| 455 | * @access private |
||
| 456 | * @since 1.1 |
||
| 457 | * @uses Give_API::output() |
||
| 458 | * @return void |
||
| 459 | */ |
||
| 460 | private function invalid_auth() { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Displays an invalid API key error if the API key provided couldn't be |
||
| 470 | * validated |
||
| 471 | * |
||
| 472 | * @access private |
||
| 473 | * @since 1.1 |
||
| 474 | * @uses Give_API::output() |
||
| 475 | * @return void |
||
| 476 | */ |
||
| 477 | private function invalid_key() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Displays an invalid version error if the version number passed isn't valid |
||
| 487 | * |
||
| 488 | * @access private |
||
| 489 | * @since 1.1 |
||
| 490 | * @uses Give_API::output() |
||
| 491 | * @return void |
||
| 492 | */ |
||
| 493 | private function invalid_version() { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Listens for the API and then processes the API requests |
||
| 503 | * |
||
| 504 | * @access public |
||
| 505 | * @global $wp_query |
||
| 506 | * @since 1.1 |
||
| 507 | * @return void |
||
| 508 | */ |
||
| 509 | public function process_query() { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Returns the API endpoint requested |
||
| 608 | * |
||
| 609 | * @access public |
||
| 610 | * @since 1.1 |
||
| 611 | * @return string $query Query mode |
||
| 612 | */ |
||
| 613 | public function get_query_mode() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Determines the kind of query requested and also ensure it is a valid query |
||
| 620 | * |
||
| 621 | * @access public |
||
| 622 | * @since 1.1 |
||
| 623 | * @global $wp_query |
||
| 624 | */ |
||
| 625 | public function set_query_mode() { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Get page number |
||
| 656 | * |
||
| 657 | * @access public |
||
| 658 | * @since 1.1 |
||
| 659 | * @global $wp_query |
||
| 660 | * @return int $wp_query->query_vars['page'] if page number returned (default: 1) |
||
| 661 | */ |
||
| 662 | public function get_paged() { |
||
| 667 | |||
| 668 | |||
| 669 | /** |
||
| 670 | * Number of results to display per page |
||
| 671 | * |
||
| 672 | * @access public |
||
| 673 | * @since 1.1 |
||
| 674 | * @global $wp_query |
||
| 675 | * @return int $per_page Results to display per page (default: 10) |
||
| 676 | */ |
||
| 677 | public function per_page() { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Sets up the dates used to retrieve earnings/donations |
||
| 691 | * |
||
| 692 | * @access public |
||
| 693 | * @since 1.2 |
||
| 694 | * |
||
| 695 | * @param array $args Arguments to override defaults |
||
| 696 | * |
||
| 697 | * @return array $dates |
||
| 698 | */ |
||
| 699 | public function get_dates( $args = array() ) { |
||
| 868 | |||
| 869 | /** |
||
| 870 | * Process Get Donors API Request. |
||
| 871 | * |
||
| 872 | * @access public |
||
| 873 | * @since 1.1 |
||
| 874 | * @global WPDB $wpdb Used to query the database using the WordPress Database API. |
||
| 875 | * |
||
| 876 | * @param int $donor Donor ID |
||
| 877 | * |
||
| 878 | * @return array $donors Multidimensional array of the donors. |
||
| 879 | */ |
||
| 880 | public function get_donors( $donor = null ) { |
||
| 959 | |||
| 960 | /** |
||
| 961 | * Process Get Donation Forms API Request |
||
| 962 | * |
||
| 963 | * @access public |
||
| 964 | * @since 1.1 |
||
| 965 | * |
||
| 966 | * @param int $form Give Form ID. |
||
| 967 | * |
||
| 968 | * @return array $donors Multidimensional array of the forms. |
||
| 969 | */ |
||
| 970 | public function get_forms( $form = null ) { |
||
| 1008 | |||
| 1009 | /** |
||
| 1010 | * Given a give_forms post object, generate the data for the API output |
||
| 1011 | * |
||
| 1012 | * @since 1.1 |
||
| 1013 | * |
||
| 1014 | * @param object $form_info The Give Form's Post Object. |
||
| 1015 | * |
||
| 1016 | * @return array Array of post data to return back in the API. |
||
| 1017 | */ |
||
| 1018 | private function get_form_data( $form_info ) { |
||
| 1084 | |||
| 1085 | /** |
||
| 1086 | * Process Get Stats API Request |
||
| 1087 | * |
||
| 1088 | * @since 1.1 |
||
| 1089 | * |
||
| 1090 | * @global WPDB $wpdb Used to query the database using the WordPress. |
||
| 1091 | * |
||
| 1092 | * @param array $args Arguments provided by API Request. |
||
| 1093 | * |
||
| 1094 | * @return array |
||
| 1095 | */ |
||
| 1096 | public function get_stats( $args = array() ) { |
||
| 1097 | $defaults = array( |
||
| 1098 | 'type' => null, |
||
| 1099 | 'form' => null, |
||
| 1100 | 'date' => null, |
||
| 1101 | 'startdate' => null, |
||
| 1102 | 'enddate' => null, |
||
| 1103 | ); |
||
| 1104 | |||
| 1105 | $args = wp_parse_args( $args, $defaults ); |
||
| 1106 | |||
| 1107 | $dates = $this->get_dates( $args ); |
||
| 1108 | |||
| 1109 | $stats = array(); |
||
| 1110 | $earnings = array( |
||
| 1111 | 'earnings' => array(), |
||
| 1112 | ); |
||
| 1113 | $sales = array( |
||
| 1114 | 'donations' => array(), |
||
| 1115 | ); |
||
| 1116 | $error = array(); |
||
| 1117 | |||
| 1118 | if ( ! user_can( $this->user_id, 'view_give_reports' ) && ! $this->override ) { |
||
| 1119 | return $stats; |
||
| 1120 | } |
||
| 1121 | |||
| 1122 | if ( $args['type'] == 'donations' ) { |
||
| 1123 | |||
| 1124 | if ( $args['form'] == null ) { |
||
| 1125 | if ( $args['date'] == null ) { |
||
| 1126 | $sales = $this->get_default_sales_stats(); |
||
| 1127 | } elseif ( $args['date'] === 'range' ) { |
||
| 1128 | // Return donations for a date range. |
||
| 1129 | // Ensure the end date is later than the start date. |
||
| 1130 | if ( $args['enddate'] < $args['startdate'] ) { |
||
| 1131 | $error['error'] = esc_html__( 'The end date must be later than the start date.', 'give' ); |
||
| 1132 | } |
||
| 1133 | |||
| 1134 | // Ensure both the start and end date are specified |
||
| 1135 | if ( empty( $args['startdate'] ) || empty( $args['enddate'] ) ) { |
||
| 1136 | $error['error'] = esc_html__( 'Invalid or no date range specified.', 'give' ); |
||
| 1137 | } |
||
| 1138 | |||
| 1139 | $total = 0; |
||
| 1140 | |||
| 1141 | // Loop through the years |
||
| 1142 | $y = $dates['year']; |
||
| 1143 | while ( $y <= $dates['year_end'] ) : |
||
| 1144 | |||
| 1145 | if ( $dates['year'] == $dates['year_end'] ) { |
||
| 1146 | $month_start = $dates['m_start']; |
||
| 1147 | $month_end = $dates['m_end']; |
||
| 1148 | } elseif ( $y == $dates['year'] && $dates['year_end'] > $dates['year'] ) { |
||
| 1149 | $month_start = $dates['m_start']; |
||
| 1150 | $month_end = 12; |
||
| 1151 | } elseif ( $y == $dates['year_end'] ) { |
||
| 1152 | $month_start = 1; |
||
| 1153 | $month_end = $dates['m_end']; |
||
| 1154 | } else { |
||
| 1155 | $month_start = 1; |
||
| 1156 | $month_end = 12; |
||
| 1157 | } |
||
| 1158 | |||
| 1159 | $i = $month_start; |
||
| 1160 | while ( $i <= $month_end ) : |
||
| 1161 | |||
| 1162 | if ( $i == $dates['m_start'] ) { |
||
| 1163 | $d = $dates['day_start']; |
||
| 1164 | } else { |
||
| 1165 | $d = 1; |
||
| 1166 | } |
||
| 1167 | |||
| 1168 | if ( $i == $dates['m_end'] ) { |
||
| 1169 | $num_of_days = $dates['day_end']; |
||
| 1170 | } else { |
||
| 1171 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
||
| 1172 | } |
||
| 1173 | |||
| 1174 | while ( $d <= $num_of_days ) : |
||
| 1175 | $sale_count = give_get_sales_by_date( $d, $i, $y ); |
||
| 1176 | $date_key = date( 'Ymd', strtotime( $y . '/' . $i . '/' . $d ) ); |
||
| 1177 | if ( ! isset( $sales['sales'][ $date_key ] ) ) { |
||
| 1178 | $sales['sales'][ $date_key ] = 0; |
||
| 1179 | } |
||
| 1180 | $sales['sales'][ $date_key ] += $sale_count; |
||
| 1181 | $total += $sale_count; |
||
| 1182 | $d ++; |
||
| 1183 | endwhile; |
||
| 1184 | $i ++; |
||
| 1185 | endwhile; |
||
| 1186 | |||
| 1187 | $y ++; |
||
| 1188 | endwhile; |
||
| 1189 | |||
| 1190 | $sales['totals'] = $total; |
||
| 1191 | } else { |
||
| 1192 | if ( $args['date'] == 'this_quarter' || $args['date'] == 'last_quarter' ) { |
||
| 1193 | $sales_count = 0; |
||
| 1194 | |||
| 1195 | // Loop through the months |
||
| 1196 | $month = $dates['m_start']; |
||
| 1197 | |||
| 1198 | while ( $month <= $dates['m_end'] ) : |
||
| 1199 | $sales_count += give_get_sales_by_date( null, $month, $dates['year'] ); |
||
| 1200 | $month ++; |
||
| 1201 | endwhile; |
||
| 1202 | |||
| 1203 | $sales['donations'][ $args['date'] ] = $sales_count; |
||
| 1204 | } else { |
||
| 1205 | $sales['donations'][ $args['date'] ] = give_get_sales_by_date( $dates['day'], $dates['m_start'], $dates['year'] ); |
||
| 1206 | } |
||
| 1207 | }// End if(). |
||
| 1208 | } elseif ( $args['form'] == 'all' ) { |
||
| 1209 | $forms = get_posts( array( |
||
| 1210 | 'post_type' => 'give_forms', |
||
| 1211 | 'nopaging' => true, |
||
| 1212 | ) ); |
||
| 1213 | $i = 0; |
||
| 1214 | foreach ( $forms as $form_info ) { |
||
| 1215 | $sales['donations'][ $i ] = array( |
||
| 1216 | $form_info->post_name => give_get_form_sales_stats( $form_info->ID ), |
||
| 1217 | ); |
||
| 1218 | $i ++; |
||
| 1219 | } |
||
| 1220 | } else { |
||
| 1221 | if ( get_post_type( $args['form'] ) == 'give_forms' ) { |
||
| 1222 | $form_info = get_post( $args['form'] ); |
||
| 1223 | $sales['donations'][0] = array( |
||
| 1224 | $form_info->post_name => give_get_form_sales_stats( $args['form'] ), |
||
| 1225 | ); |
||
| 1226 | } else { |
||
| 1227 | $error['error'] = sprintf( /* translators: %s: form */ |
||
| 1228 | esc_html__( 'Form %s not found.', 'give' ), $args['form'] ); |
||
| 1229 | } |
||
| 1230 | }// End if(). |
||
| 1231 | |||
| 1232 | if ( ! empty( $error ) ) { |
||
| 1233 | return $error; |
||
| 1234 | } |
||
| 1235 | |||
| 1236 | return $sales; |
||
| 1237 | |||
| 1238 | } elseif ( $args['type'] == 'earnings' ) { |
||
| 1239 | if ( $args['form'] == null ) { |
||
| 1240 | if ( $args['date'] == null ) { |
||
| 1241 | $earnings = $this->get_default_earnings_stats(); |
||
| 1242 | } elseif ( $args['date'] === 'range' ) { |
||
| 1243 | // Return sales for a date range |
||
| 1244 | // Ensure the end date is later than the start date |
||
| 1245 | if ( $args['enddate'] < $args['startdate'] ) { |
||
| 1246 | $error['error'] = esc_html__( 'The end date must be later than the start date.', 'give' ); |
||
| 1247 | } |
||
| 1248 | |||
| 1249 | // Ensure both the start and end date are specified |
||
| 1250 | if ( empty( $args['startdate'] ) || empty( $args['enddate'] ) ) { |
||
| 1251 | $error['error'] = esc_html__( 'Invalid or no date range specified.', 'give' ); |
||
| 1252 | } |
||
| 1253 | |||
| 1254 | $total = (float) 0.00; |
||
| 1255 | |||
| 1256 | // Loop through the years |
||
| 1257 | $y = $dates['year']; |
||
| 1258 | if ( ! isset( $earnings['earnings'] ) ) { |
||
| 1259 | $earnings['earnings'] = array(); |
||
| 1260 | } |
||
| 1261 | while ( $y <= $dates['year_end'] ) : |
||
| 1262 | |||
| 1263 | if ( $dates['year'] == $dates['year_end'] ) { |
||
| 1264 | $month_start = $dates['m_start']; |
||
| 1265 | $month_end = $dates['m_end']; |
||
| 1266 | } elseif ( $y == $dates['year'] && $dates['year_end'] > $dates['year'] ) { |
||
| 1267 | $month_start = $dates['m_start']; |
||
| 1268 | $month_end = 12; |
||
| 1269 | } elseif ( $y == $dates['year_end'] ) { |
||
| 1270 | $month_start = 1; |
||
| 1271 | $month_end = $dates['m_end']; |
||
| 1272 | } else { |
||
| 1273 | $month_start = 1; |
||
| 1274 | $month_end = 12; |
||
| 1275 | } |
||
| 1276 | |||
| 1277 | $i = $month_start; |
||
| 1278 | while ( $i <= $month_end ) : |
||
| 1279 | |||
| 1280 | if ( $i == $dates['m_start'] ) { |
||
| 1281 | $d = $dates['day_start']; |
||
| 1282 | } else { |
||
| 1283 | $d = 1; |
||
| 1284 | } |
||
| 1285 | |||
| 1286 | if ( $i == $dates['m_end'] ) { |
||
| 1287 | $num_of_days = $dates['day_end']; |
||
| 1288 | } else { |
||
| 1289 | $num_of_days = cal_days_in_month( CAL_GREGORIAN, $i, $y ); |
||
| 1290 | } |
||
| 1291 | |||
| 1292 | while ( $d <= $num_of_days ) : |
||
| 1293 | $earnings_stat = give_get_earnings_by_date( $d, $i, $y ); |
||
| 1294 | $date_key = date( 'Ymd', strtotime( $y . '/' . $i . '/' . $d ) ); |
||
| 1295 | if ( ! isset( $earnings['earnings'][ $date_key ] ) ) { |
||
| 1296 | $earnings['earnings'][ $date_key ] = 0; |
||
| 1297 | } |
||
| 1298 | $earnings['earnings'][ $date_key ] += $earnings_stat; |
||
| 1299 | $total += $earnings_stat; |
||
| 1300 | $d ++; |
||
| 1301 | endwhile; |
||
| 1302 | |||
| 1303 | $i ++; |
||
| 1304 | endwhile; |
||
| 1305 | |||
| 1306 | $y ++; |
||
| 1307 | endwhile; |
||
| 1308 | |||
| 1309 | $earnings['totals'] = $total; |
||
| 1310 | } else { |
||
| 1311 | if ( $args['date'] == 'this_quarter' || $args['date'] == 'last_quarter' ) { |
||
| 1312 | $earnings_count = (float) 0.00; |
||
| 1313 | |||
| 1314 | // Loop through the months |
||
| 1315 | $month = $dates['m_start']; |
||
| 1316 | |||
| 1317 | while ( $month <= $dates['m_end'] ) : |
||
| 1318 | $earnings_count += give_get_earnings_by_date( null, $month, $dates['year'] ); |
||
| 1319 | $month ++; |
||
| 1320 | endwhile; |
||
| 1321 | |||
| 1322 | $earnings['earnings'][ $args['date'] ] = $earnings_count; |
||
| 1323 | } else { |
||
| 1324 | $earnings['earnings'][ $args['date'] ] = give_get_earnings_by_date( $dates['day'], $dates['m_start'], $dates['year'] ); |
||
| 1325 | } |
||
| 1326 | }// End if(). |
||
| 1327 | } elseif ( $args['form'] == 'all' ) { |
||
| 1328 | $forms = get_posts( array( |
||
| 1329 | 'post_type' => 'give_forms', |
||
| 1330 | 'nopaging' => true, |
||
| 1331 | ) ); |
||
| 1332 | |||
| 1333 | $i = 0; |
||
| 1334 | foreach ( $forms as $form_info ) { |
||
| 1335 | $earnings['earnings'][ $i ] = array( |
||
| 1336 | $form_info->post_name => give_get_form_earnings_stats( $form_info->ID ), |
||
| 1337 | ); |
||
| 1338 | $i ++; |
||
| 1339 | } |
||
| 1340 | } else { |
||
| 1341 | if ( get_post_type( $args['form'] ) == 'give_forms' ) { |
||
| 1342 | $form_info = get_post( $args['form'] ); |
||
| 1343 | $earnings['earnings'][0] = array( |
||
| 1344 | $form_info->post_name => give_get_form_earnings_stats( $args['form'] ), |
||
| 1345 | ); |
||
| 1346 | } else { |
||
| 1347 | $error['error'] = sprintf( /* translators: %s: form */ |
||
| 1348 | esc_html__( 'Form %s not found.', 'give' ), $args['form'] ); |
||
| 1349 | } |
||
| 1350 | }// End if(). |
||
| 1351 | |||
| 1352 | if ( ! empty( $error ) ) { |
||
| 1353 | return $error; |
||
| 1354 | } |
||
| 1355 | |||
| 1356 | return $earnings; |
||
| 1357 | } elseif ( $args['type'] == 'donors' ) { |
||
| 1358 | $donors = new Give_DB_Donors(); |
||
| 1359 | $stats['donations']['total_donors'] = $donors->count(); |
||
| 1360 | |||
| 1361 | return $stats; |
||
| 1362 | |||
| 1363 | } elseif ( empty( $args['type'] ) ) { |
||
| 1364 | $stats = array_merge( $stats, $this->get_default_sales_stats() ); |
||
| 1365 | $stats = array_merge( $stats, $this->get_default_earnings_stats() ); |
||
| 1366 | |||
| 1367 | return array( |
||
| 1368 | 'stats' => $stats, |
||
| 1369 | ); |
||
| 1370 | }// End if(). |
||
| 1371 | } |
||
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Retrieves Recent Donations |
||
| 1375 | * |
||
| 1376 | * @access public |
||
| 1377 | * @since 1.1 |
||
| 1378 | * |
||
| 1379 | * @param $args array |
||
| 1380 | * |
||
| 1381 | * @return array |
||
| 1382 | */ |
||
| 1383 | public function get_recent_donations( $args = array() ) { |
||
| 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 ) { |
||
| 1774 | |||
| 1775 | /** |
||
| 1776 | * Process an API key generation/revocation |
||
| 1777 | * |
||
| 1778 | * @access public |
||
| 1779 | * @since 1.1 |
||
| 1780 | * |
||
| 1781 | * @param array $args |
||
| 1782 | * |
||
| 1783 | * @return void |
||
| 1784 | */ |
||
| 1785 | public function process_api_key( $args ) { |
||
| 1786 | |||
| 1787 | if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'give-api-nonce' ) ) { |
||
| 1788 | wp_die( __( 'Nonce verification failed.', 'give' ), __( 'Error', 'give' ), array( |
||
| 1789 | 'response' => 403, |
||
| 1790 | ) ); |
||
| 1791 | } |
||
| 1792 | |||
| 1793 | if ( empty( $args['user_id'] ) ) { |
||
| 1794 | wp_die( __( 'User ID Required.', 'give' ), __( 'Error', 'give' ), array( |
||
| 1795 | 'response' => 401, |
||
| 1796 | ) ); |
||
| 1797 | } |
||
| 1798 | |||
| 1799 | if ( is_numeric( $args['user_id'] ) ) { |
||
| 1800 | $user_id = isset( $args['user_id'] ) ? absint( $args['user_id'] ) : get_current_user_id(); |
||
| 1801 | } else { |
||
| 1802 | $userdata = get_user_by( 'login', $args['user_id'] ); |
||
| 1803 | $user_id = $userdata->ID; |
||
| 1804 | } |
||
| 1805 | $process = isset( $args['give_api_process'] ) ? strtolower( $args['give_api_process'] ) : false; |
||
| 1806 | |||
| 1807 | if ( $user_id == get_current_user_id() && ! give_get_option( 'allow_user_api_keys' ) && ! current_user_can( 'manage_give_settings' ) ) { |
||
| 1808 | wp_die( sprintf( /* translators: %s: process */ |
||
| 1809 | esc_html__( 'You do not have permission to %s API keys for this user.', 'give' ), $process ), esc_html__( 'Error', 'give' ), array( |
||
| 1810 | 'response' => 403, |
||
| 1811 | ) ); |
||
| 1812 | } elseif ( ! current_user_can( 'manage_give_settings' ) ) { |
||
| 1813 | wp_die( sprintf( /* translators: %s: process */ |
||
| 1814 | esc_html__( 'You do not have permission to %s API keys for this user.', 'give' ), $process ), esc_html__( 'Error', 'give' ), array( |
||
| 1815 | 'response' => 403, |
||
| 1816 | ) ); |
||
| 1817 | } |
||
| 1818 | |||
| 1819 | switch ( $process ) { |
||
| 1820 | case 'generate': |
||
| 1821 | if ( $this->generate_api_key( $user_id ) ) { |
||
| 1822 | Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) ); |
||
| 1823 | wp_redirect( add_query_arg( 'give-message', 'api-key-generated', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1824 | exit(); |
||
| 1825 | } else { |
||
| 1826 | wp_redirect( add_query_arg( 'give-message', 'api-key-failed', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1827 | exit(); |
||
| 1828 | } |
||
| 1829 | break; |
||
| 1830 | case 'regenerate': |
||
| 1831 | $this->generate_api_key( $user_id, true ); |
||
| 1832 | Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) ); |
||
| 1833 | wp_redirect( add_query_arg( 'give-message', 'api-key-regenerated', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1834 | exit(); |
||
| 1835 | break; |
||
| 1836 | case 'revoke': |
||
| 1837 | $this->revoke_api_key( $user_id ); |
||
| 1838 | Give_Cache::delete( Give_Cache::get_key( 'give_total_api_keys' ) ); |
||
| 1839 | wp_redirect( add_query_arg( 'give-message', 'api-key-revoked', 'edit.php?post_type=give_forms&page=give-tools&tab=api' ) ); |
||
| 1840 | exit(); |
||
| 1841 | break; |
||
| 1842 | default; |
||
| 1843 | break; |
||
| 1844 | } |
||
| 1845 | } |
||
| 1846 | |||
| 1847 | /** |
||
| 1848 | * Generate new API keys for a user |
||
| 1849 | * |
||
| 1850 | * @access public |
||
| 1851 | * @since 1.1 |
||
| 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 | * @return boolean True if (re)generated succesfully, false otherwise. |
||
| 1857 | */ |
||
| 1858 | public function generate_api_key( $user_id = 0, $regenerate = false ) { |
||
| 1889 | |||
| 1890 | /** |
||
| 1891 | * Revoke a users API keys |
||
| 1892 | * |
||
| 1893 | * @access public |
||
| 1894 | * @since 1.1 |
||
| 1895 | * |
||
| 1896 | * @param int $user_id User ID of user to revoke key for |
||
| 1897 | * |
||
| 1898 | * @return bool |
||
| 1899 | */ |
||
| 1900 | public function revoke_api_key( $user_id = 0 ) { |
||
| 1926 | |||
| 1927 | public function get_version() { |
||
| 1930 | |||
| 1931 | |||
| 1932 | /** |
||
| 1933 | * Generate and Save API key |
||
| 1934 | * |
||
| 1935 | * Generates the key requested by user_key_field and stores it in the database |
||
| 1936 | * |
||
| 1937 | * @access public |
||
| 1938 | * @since 1.1 |
||
| 1939 | * |
||
| 1940 | * @param int $user_id |
||
| 1941 | * |
||
| 1942 | * @return void |
||
| 1943 | */ |
||
| 1944 | public function update_key( $user_id ) { |
||
| 1963 | |||
| 1964 | /** |
||
| 1965 | * Generate the public key for a user |
||
| 1966 | * |
||
| 1967 | * @access private |
||
| 1968 | * @since 1.1 |
||
| 1969 | * |
||
| 1970 | * @param string $user_email |
||
| 1971 | * |
||
| 1972 | * @return string |
||
| 1973 | */ |
||
| 1974 | private function generate_public_key( $user_email = '' ) { |
||
| 1980 | |||
| 1981 | /** |
||
| 1982 | * Generate the secret key for a user |
||
| 1983 | * |
||
| 1984 | * @access private |
||
| 1985 | * @since 1.1 |
||
| 1986 | * |
||
| 1987 | * @param int $user_id |
||
| 1988 | * |
||
| 1989 | * @return string |
||
| 1990 | */ |
||
| 1991 | private function generate_private_key( $user_id = 0 ) { |
||
| 1997 | |||
| 1998 | /** |
||
| 1999 | * Retrieve the user's token |
||
| 2000 | * |
||
| 2001 | * @access private |
||
| 2002 | * @since 1.1 |
||
| 2003 | * |
||
| 2004 | * @param int $user_id |
||
| 2005 | * |
||
| 2006 | * @return string |
||
| 2007 | */ |
||
| 2008 | public function get_token( $user_id = 0 ) { |
||
| 2011 | |||
| 2012 | /** |
||
| 2013 | * Generate the default donation stats returned by the 'stats' endpoint |
||
| 2014 | * |
||
| 2015 | * @access private |
||
| 2016 | * @since 1.1 |
||
| 2017 | * @return array default sales statistics |
||
| 2018 | */ |
||
| 2019 | private function get_default_sales_stats() { |
||
| 2020 | |||
| 2021 | // Default sales return |
||
| 2022 | $sales = array(); |
||
| 2023 | $sales['donations']['today'] = $this->stats->get_sales( 0, 'today' ); |
||
| 2024 | $sales['donations']['current_month'] = $this->stats->get_sales( 0, 'this_month' ); |
||
| 2025 | $sales['donations']['last_month'] = $this->stats->get_sales( 0, 'last_month' ); |
||
| 2026 | $sales['donations']['totals'] = give_get_total_donations(); |
||
| 2027 | |||
| 2028 | return $sales; |
||
| 2029 | } |
||
| 2030 | |||
| 2031 | /** |
||
| 2032 | * Generate the default earnings stats returned by the 'stats' endpoint |
||
| 2033 | * |
||
| 2034 | * @access private |
||
| 2035 | * @since 1.1 |
||
| 2036 | * @return array default earnings statistics |
||
| 2037 | */ |
||
| 2038 | private function get_default_earnings_stats() { |
||
| 2039 | |||
| 2040 | // Default earnings return |
||
| 2041 | $earnings = array(); |
||
| 2042 | $earnings['earnings']['today'] = $this->stats->get_earnings( 0, 'today' ); |
||
| 2043 | $earnings['earnings']['current_month'] = $this->stats->get_earnings( 0, 'this_month' ); |
||
| 2044 | $earnings['earnings']['last_month'] = $this->stats->get_earnings( 0, 'last_month' ); |
||
| 2045 | $earnings['earnings']['totals'] = give_get_total_earnings(); |
||
| 2046 | |||
| 2047 | return $earnings; |
||
| 2048 | } |
||
| 2049 | |||
| 2050 | /** |
||
| 2051 | * API Key Backwards Compatibility |
||
| 2052 | * |
||
| 2053 | * A Backwards Compatibility call for the change of meta_key/value for users API Keys. |
||
| 2054 | * |
||
| 2055 | * @since 1.3.6 |
||
| 2056 | * |
||
| 2057 | * @param string $check Whether to check the cache or not |
||
| 2058 | * @param int $object_id The User ID being passed |
||
| 2059 | * @param string $meta_key The user meta key |
||
| 2060 | * @param bool $single If it should return a single value or array |
||
| 2061 | * |
||
| 2062 | * @return string The API key/secret for the user supplied |
||
| 2063 | */ |
||
| 2064 | public function api_key_backwards_compat( $check, $object_id, $meta_key, $single ) { |
||
| 2088 | |||
| 2089 | } |
||
| 2090 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.