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 private |
||
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 private |
||
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 | * |
||
89 | * @var bool |
||
90 | * @access private |
||
91 | * @since 1.1 |
||
92 | */ |
||
93 | public $override = true; |
||
94 | |||
95 | /** |
||
96 | * Version of the API queried |
||
97 | * |
||
98 | * @var string |
||
99 | * @access public |
||
100 | * @since 1.1 |
||
101 | */ |
||
102 | private $queried_version; |
||
103 | |||
104 | /** |
||
105 | * All versions of the API |
||
106 | * |
||
107 | * @var string |
||
108 | * @access public |
||
109 | * @since 1.1 |
||
110 | */ |
||
111 | protected $versions = array(); |
||
112 | |||
113 | /** |
||
114 | * Queried endpoint |
||
115 | * |
||
116 | * @var string |
||
117 | * @access public |
||
118 | * @since 1.1 |
||
119 | */ |
||
120 | private $endpoint; |
||
121 | |||
122 | /** |
||
123 | * Endpoints routes |
||
124 | * |
||
125 | * @var object |
||
126 | * @access public |
||
127 | * @since 1.1 |
||
128 | */ |
||
129 | private $routes; |
||
130 | |||
131 | /** |
||
132 | * Setup the Give API |
||
133 | * |
||
134 | * @since 1.1 |
||
135 | */ |
||
136 | 12 | public function __construct() { |
|
137 | |||
138 | 12 | $this->versions = array( |
|
139 | 12 | 'v1' => 'GIVE_API_V1', |
|
140 | ); |
||
141 | |||
142 | 12 | foreach ( $this->get_versions() as $version => $class ) { |
|
143 | 12 | require_once GIVE_PLUGIN_DIR . 'includes/api/class-give-api-' . $version . '.php'; |
|
144 | 12 | } |
|
145 | |||
146 | 12 | add_action( 'init', array( $this, 'add_endpoint' ) ); |
|
147 | 12 | add_action( 'wp', array( $this, 'process_query' ), - 1 ); |
|
148 | 12 | add_filter( 'query_vars', array( $this, 'query_vars' ) ); |
|
149 | 12 | add_action( 'show_user_profile', array( $this, 'user_key_field' ) ); |
|
150 | 12 | add_action( 'edit_user_profile', array( $this, 'user_key_field' ) ); |
|
151 | 12 | add_action( 'personal_options_update', array( $this, 'update_key' ) ); |
|
152 | 12 | add_action( 'edit_user_profile_update', array( $this, 'update_key' ) ); |
|
153 | 12 | add_action( 'give_process_api_key', array( $this, 'process_api_key' ) ); |
|
154 | |||
155 | // Setup a backwards compatibility check for user API Keys |
||
156 | 12 | add_filter( 'get_user_metadata', array( $this, 'api_key_backwards_compat' ), 10, 4 ); |
|
157 | |||
158 | // Determine if JSON_PRETTY_PRINT is available |
||
159 | 12 | $this->pretty_print = defined( 'JSON_PRETTY_PRINT' ) ? JSON_PRETTY_PRINT : null; |
|
160 | |||
161 | // Allow API request logging to be turned off |
||
162 | 12 | $this->log_requests = apply_filters( 'give_api_log_requests', $this->log_requests ); |
|
163 | |||
164 | // Setup Give_Payment_Stats instance |
||
165 | 12 | $this->stats = new Give_Payment_Stats; |
|
166 | |||
167 | 12 | } |
|
168 | |||
169 | /** |
||
170 | * Registers a new rewrite endpoint for accessing the API |
||
171 | * |
||
172 | * @access public |
||
173 | * |
||
174 | * @param array $rewrite_rules WordPress Rewrite Rules |
||
175 | * |
||
176 | * @since 1.1 |
||
177 | */ |
||
178 | 10 | public function add_endpoint( $rewrite_rules ) { |
|
179 | 10 | add_rewrite_endpoint( 'give-api', EP_ALL ); |
|
180 | 10 | } |
|
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 | 4 | public function query_vars( $vars ) { |
|
193 | |||
194 | 4 | $vars[] = 'token'; |
|
195 | 4 | $vars[] = 'key'; |
|
196 | 4 | $vars[] = 'query'; |
|
197 | 4 | $vars[] = 'type'; |
|
198 | 4 | $vars[] = 'form'; |
|
199 | 4 | $vars[] = 'number'; |
|
200 | 4 | $vars[] = 'date'; |
|
201 | 4 | $vars[] = 'startdate'; |
|
202 | 4 | $vars[] = 'enddate'; |
|
203 | 4 | $vars[] = 'donor'; |
|
204 | 4 | $vars[] = 'format'; |
|
205 | 4 | $vars[] = 'id'; |
|
206 | 4 | $vars[] = 'purchasekey'; |
|
207 | 4 | $vars[] = 'email'; |
|
208 | |||
209 | 4 | return $vars; |
|
210 | } |
||
211 | |||
212 | /** |
||
213 | * Retrieve the API versions |
||
214 | * |
||
215 | * @access public |
||
216 | * @since 1.1 |
||
217 | * @return array |
||
218 | */ |
||
219 | 12 | public function get_versions() { |
|
220 | 12 | 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 private |
||
238 | * @since 1.1 |
||
239 | * @return string |
||
240 | */ |
||
241 | 1 | public function get_default_version() { |
|
242 | |||
243 | 1 | $version = get_option( 'give_default_api_version' ); |
|
244 | |||
245 | 1 | if ( defined( 'GIVE_API_VERSION' ) ) { |
|
246 | 1 | $version = GIVE_API_VERSION; |
|
247 | 1 | } elseif ( ! $version ) { |
|
248 | $version = 'v1'; |
||
249 | } |
||
250 | |||
251 | 1 | 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 | |||
285 | } else { |
||
286 | |||
287 | $this->queried_version = $this->get_default_version(); |
||
288 | |||
289 | } |
||
290 | |||
291 | } |
||
292 | |||
293 | /** |
||
294 | * Validate the API request |
||
295 | * |
||
296 | * Checks for the user's public key and token against the secret key |
||
297 | * |
||
298 | * @access private |
||
299 | * @global object $wp_query WordPress Query |
||
300 | * @uses Give_API::get_user() |
||
301 | * @uses Give_API::invalid_key() |
||
302 | * @uses Give_API::invalid_auth() |
||
303 | * @since 1.1 |
||
304 | * @return void |
||
305 | */ |
||
306 | private function validate_request() { |
||
307 | global $wp_query; |
||
308 | |||
309 | $this->override = false; |
||
310 | |||
311 | // Make sure we have both user and api key |
||
312 | if ( ! empty( $wp_query->query_vars['give-api'] ) && ( $wp_query->query_vars['give-api'] != 'forms' || ! empty( $wp_query->query_vars['token'] ) ) ) { |
||
313 | |||
314 | if ( empty( $wp_query->query_vars['token'] ) || empty( $wp_query->query_vars['key'] ) ) { |
||
315 | $this->missing_auth(); |
||
316 | } |
||
317 | |||
318 | // Retrieve the user by public API key and ensure they exist |
||
319 | if ( ! ( $user = $this->get_user( $wp_query->query_vars['key'] ) ) ) { |
||
320 | |||
321 | $this->invalid_key(); |
||
322 | |||
323 | } else { |
||
324 | |||
325 | $token = urldecode( $wp_query->query_vars['token'] ); |
||
326 | $secret = $this->get_user_secret_key( $user ); |
||
327 | $public = urldecode( $wp_query->query_vars['key'] ); |
||
328 | |||
329 | if ( hash_equals( md5( $secret . $public ), $token ) ) { |
||
330 | $this->is_valid_request = true; |
||
331 | } else { |
||
332 | $this->invalid_auth(); |
||
333 | } |
||
334 | } |
||
335 | } elseif ( ! empty( $wp_query->query_vars['give-api'] ) && $wp_query->query_vars['give-api'] == 'forms' ) { |
||
336 | $this->is_valid_request = true; |
||
337 | $wp_query->set( 'key', 'public' ); |
||
338 | } |
||
339 | } |
||
340 | |||
341 | /** |
||
342 | * Retrieve the user ID based on the public key provided |
||
343 | * |
||
344 | * @access public |
||
345 | * @since 1.1 |
||
346 | * @global object $wpdb Used to query the database using the WordPress |
||
347 | * Database API |
||
348 | * |
||
349 | * @param string $key Public Key |
||
350 | * |
||
351 | * @return bool if user ID is found, false otherwise |
||
352 | */ |
||
353 | 1 | public function get_user( $key = '' ) { |
|
379 | |||
380 | 2 | public function get_user_public_key( $user_id = 0 ) { |
|
397 | |||
398 | 2 | public function get_user_secret_key( $user_id = 0 ) { |
|
415 | |||
416 | /** |
||
417 | * Displays a missing authentication error if all the parameters aren't |
||
418 | * provided |
||
419 | * |
||
420 | * @access private |
||
421 | * @uses Give_API::output() |
||
422 | * @since 1.1 |
||
423 | */ |
||
424 | private function missing_auth() { |
||
431 | |||
432 | /** |
||
433 | * Displays an authentication failed error if the user failed to provide valid |
||
434 | * credentials |
||
435 | * |
||
436 | * @access private |
||
437 | * @since 1.1 |
||
438 | * @uses Give_API::output() |
||
439 | * @return void |
||
440 | */ |
||
441 | private function invalid_auth() { |
||
448 | |||
449 | /** |
||
450 | * Displays an invalid API key error if the API key provided couldn't be |
||
451 | * validated |
||
452 | * |
||
453 | * @access private |
||
454 | * @since 1.1 |
||
455 | * @uses Give_API::output() |
||
456 | * @return void |
||
457 | */ |
||
458 | private function invalid_key() { |
||
465 | |||
466 | /** |
||
467 | * Displays an invalid version error if the version number passed isn't valid |
||
468 | * |
||
469 | * @access private |
||
470 | * @since 1.1 |
||
471 | * @uses Give_API::output() |
||
472 | * @return void |
||
473 | */ |
||
474 | private function invalid_version() { |
||
481 | |||
482 | /** |
||
483 | * Listens for the API and then processes the API requests |
||
484 | * |
||
485 | * @access public |
||
486 | * @global $wp_query |
||
487 | * @since 1.1 |
||
488 | * @return void |
||
489 | */ |
||
490 | 3 | public function process_query() { |
|
491 | |||
492 | 3 | global $wp_query; |
|
493 | |||
494 | // Start logging how long the request takes for logging |
||
495 | 3 | $before = microtime( true ); |
|
496 | |||
497 | // Check for give-api var. Get out if not present |
||
498 | 3 | if ( empty( $wp_query->query_vars['give-api'] ) ) { |
|
499 | 3 | return; |
|
500 | } |
||
501 | |||
502 | // Determine which version was queried |
||
503 | $this->set_queried_version(); |
||
504 | |||
505 | // Determine the kind of query |
||
506 | $this->set_query_mode(); |
||
507 | |||
508 | // Check for a valid user and set errors if necessary |
||
509 | $this->validate_request(); |
||
510 | |||
511 | // Only proceed if no errors have been noted |
||
512 | if ( ! $this->is_valid_request ) { |
||
513 | return; |
||
514 | } |
||
515 | |||
516 | if ( ! defined( 'GIVE_DOING_API' ) ) { |
||
517 | define( 'GIVE_DOING_API', true ); |
||
518 | } |
||
519 | |||
520 | $data = array(); |
||
521 | $this->routes = new $this->versions[$this->get_queried_version()]; |
||
522 | $this->routes->validate_request(); |
||
523 | |||
524 | switch ( $this->endpoint ) : |
||
525 | |||
526 | case 'stats' : |
||
527 | |||
528 | $data = $this->routes->get_stats( array( |
||
529 | 'type' => isset( $wp_query->query_vars['type'] ) ? $wp_query->query_vars['type'] : null, |
||
530 | 'form' => isset( $wp_query->query_vars['form'] ) ? $wp_query->query_vars['form'] : null, |
||
531 | 'date' => isset( $wp_query->query_vars['date'] ) ? $wp_query->query_vars['date'] : null, |
||
532 | 'startdate' => isset( $wp_query->query_vars['startdate'] ) ? $wp_query->query_vars['startdate'] : null, |
||
533 | 'enddate' => isset( $wp_query->query_vars['enddate'] ) ? $wp_query->query_vars['enddate'] : null |
||
534 | ) ); |
||
535 | |||
536 | break; |
||
537 | |||
538 | case 'forms' : |
||
539 | |||
540 | $form = isset( $wp_query->query_vars['form'] ) ? $wp_query->query_vars['form'] : null; |
||
541 | |||
542 | $data = $this->routes->get_forms( $form ); |
||
543 | |||
544 | break; |
||
545 | |||
546 | case 'donors' : |
||
547 | |||
548 | $customer = isset( $wp_query->query_vars['donor'] ) ? $wp_query->query_vars['donor'] : null; |
||
549 | |||
550 | $data = $this->routes->get_customers( $customer ); |
||
551 | |||
552 | break; |
||
553 | |||
554 | case 'donations' : |
||
555 | |||
556 | $data = $this->routes->get_recent_donations(); |
||
557 | |||
558 | break; |
||
559 | |||
560 | endswitch; |
||
561 | |||
562 | // Allow extensions to setup their own return data |
||
563 | $this->data = apply_filters( 'give_api_output_data', $data, $this->endpoint, $this ); |
||
564 | |||
565 | $after = microtime( true ); |
||
566 | $request_time = ( $after - $before ); |
||
567 | $this->data['request_speed'] = $request_time; |
||
568 | |||
569 | // Log this API request, if enabled. We log it here because we have access to errors. |
||
570 | $this->log_request( $this->data ); |
||
571 | |||
572 | // Send out data to the output function |
||
573 | $this->output(); |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * Returns the API endpoint requested |
||
578 | * |
||
579 | * @access private |
||
580 | * @since 1.1 |
||
581 | * @return string $query Query mode |
||
582 | */ |
||
583 | public function get_query_mode() { |
||
587 | |||
588 | /** |
||
589 | * Determines the kind of query requested and also ensure it is a valid query |
||
590 | * |
||
591 | * @access private |
||
592 | * @since 1.1 |
||
593 | * @global $wp_query |
||
594 | */ |
||
595 | public function set_query_mode() { |
||
622 | |||
623 | /** |
||
624 | * Get page number |
||
625 | * |
||
626 | * @access private |
||
627 | * @since 1.1 |
||
628 | * @global $wp_query |
||
629 | * @return int $wp_query->query_vars['page'] if page number returned (default: 1) |
||
630 | */ |
||
631 | 10 | public function get_paged() { |
|
636 | |||
637 | |||
638 | /** |
||
639 | * Number of results to display per page |
||
640 | * |
||
641 | * @access private |
||
642 | * @since 1.1 |
||
643 | * @global $wp_query |
||
644 | * @return int $per_page Results to display per page (default: 10) |
||
645 | */ |
||
646 | 10 | public function per_page() { |
|
657 | |||
658 | /** |
||
659 | * Sets up the dates used to retrieve earnings/donations |
||
660 | * |
||
661 | * @access public |
||
662 | * @since 1.2 |
||
663 | * |
||
664 | * @param array $args Arguments to override defaults |
||
665 | * |
||
666 | * @return array $dates |
||
667 | */ |
||
668 | public function get_dates( $args = array() ) { |
||
669 | $dates = array(); |
||
670 | |||
671 | $defaults = array( |
||
672 | 'type' => '', |
||
673 | 'form' => null, |
||
674 | 'date' => null, |
||
675 | 'startdate' => null, |
||
676 | 'enddate' => null |
||
677 | ); |
||
678 | |||
679 | $args = wp_parse_args( $args, $defaults ); |
||
680 | |||
681 | $current_time = current_time( 'timestamp' ); |
||
682 | |||
683 | if ( 'range' === $args['date'] ) { |
||
684 | $startdate = strtotime( $args['startdate'] ); |
||
685 | $enddate = strtotime( $args['enddate'] ); |
||
686 | $dates['day_start'] = date( 'd', $startdate ); |
||
687 | $dates['day_end'] = date( 'd', $enddate ); |
||
688 | $dates['m_start'] = date( 'n', $startdate ); |
||
689 | $dates['m_end'] = date( 'n', $enddate ); |
||
690 | $dates['year'] = date( 'Y', $startdate ); |
||
691 | $dates['year_end'] = date( 'Y', $enddate ); |
||
692 | } else { |
||
693 | // Modify dates based on predefined ranges |
||
694 | switch ( $args['date'] ) : |
||
695 | |||
696 | case 'this_month' : |
||
697 | $dates['day'] = null; |
||
698 | $dates['m_start'] = date( 'n', $current_time ); |
||
699 | $dates['m_end'] = date( 'n', $current_time ); |
||
700 | $dates['year'] = date( 'Y', $current_time ); |
||
701 | break; |
||
702 | |||
703 | case 'last_month' : |
||
704 | $dates['day'] = null; |
||
705 | $dates['m_start'] = date( 'n', $current_time ) == 1 ? 12 : date( 'n', $current_time ) - 1; |
||
706 | $dates['m_end'] = $dates['m_start']; |
||
707 | $dates['year'] = date( 'n', $current_time ) == 1 ? date( 'Y', $current_time ) - 1 : date( 'Y', $current_time ); |
||
708 | break; |
||
709 | |||
710 | case 'today' : |
||
711 | $dates['day'] = date( 'd', $current_time ); |
||
712 | $dates['m_start'] = date( 'n', $current_time ); |
||
713 | $dates['m_end'] = date( 'n', $current_time ); |
||
714 | $dates['year'] = date( 'Y', $current_time ); |
||
715 | break; |
||
716 | |||
717 | case 'yesterday' : |
||
718 | |||
719 | $year = date( 'Y', $current_time ); |
||
720 | $month = date( 'n', $current_time ); |
||
721 | $day = date( 'd', $current_time ); |
||
722 | |||
723 | if ( $month == 1 && $day == 1 ) { |
||
724 | |||
725 | $year -= 1; |
||
726 | $month = 12; |
||
727 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
||
728 | |||
729 | } elseif ( $month > 1 && $day == 1 ) { |
||
730 | |||
731 | $month -= 1; |
||
732 | $day = cal_days_in_month( CAL_GREGORIAN, $month, $year ); |
||
733 | |||
734 | } else { |
||
735 | |||
736 | $day -= 1; |
||
737 | |||
738 | } |
||
739 | |||
740 | $dates['day'] = $day; |
||
741 | $dates['m_start'] = $month; |
||
742 | $dates['m_end'] = $month; |
||
743 | $dates['year'] = $year; |
||
744 | |||
745 | break; |
||
746 | |||
747 | case 'this_quarter' : |
||
748 | $month_now = date( 'n', $current_time ); |
||
749 | |||
750 | $dates['day'] = null; |
||
751 | |||
752 | if ( $month_now <= 3 ) { |
||
753 | |||
754 | $dates['m_start'] = 1; |
||
755 | $dates['m_end'] = 3; |
||
756 | $dates['year'] = date( 'Y', $current_time ); |
||
757 | |||
758 | } else if ( $month_now <= 6 ) { |
||
759 | |||
760 | $dates['m_start'] = 4; |
||
761 | $dates['m_end'] = 6; |
||
762 | $dates['year'] = date( 'Y', $current_time ); |
||
763 | |||
764 | } else if ( $month_now <= 9 ) { |
||
765 | |||
766 | $dates['m_start'] = 7; |
||
767 | $dates['m_end'] = 9; |
||
768 | $dates['year'] = date( 'Y', $current_time ); |
||
769 | |||
770 | } else { |
||
771 | |||
772 | $dates['m_start'] = 10; |
||
773 | $dates['m_end'] = 12; |
||
774 | $dates['year'] = date( 'Y', $current_time ); |
||
775 | |||
776 | } |
||
777 | break; |
||
778 | |||
779 | case 'last_quarter' : |
||
780 | $month_now = date( 'n', $current_time ); |
||
781 | |||
782 | $dates['day'] = null; |
||
783 | |||
784 | if ( $month_now <= 3 ) { |
||
785 | |||
786 | $dates['m_start'] = 10; |
||
787 | $dates['m_end'] = 12; |
||
788 | $dates['year'] = date( 'Y', $current_time ) - 1; // Previous year |
||
789 | |||
790 | } else if ( $month_now <= 6 ) { |
||
791 | |||
792 | $dates['m_start'] = 1; |
||
793 | $dates['m_end'] = 3; |
||
794 | $dates['year'] = date( 'Y', $current_time ); |
||
795 | |||
796 | } else if ( $month_now <= 9 ) { |
||
797 | |||
798 | $dates['m_start'] = 4; |
||
799 | $dates['m_end'] = 6; |
||
800 | $dates['year'] = date( 'Y', $current_time ); |
||
801 | |||
802 | } else { |
||
803 | |||
804 | $dates['m_start'] = 7; |
||
805 | $dates['m_end'] = 9; |
||
806 | $dates['year'] = date( 'Y', $current_time ); |
||
807 | |||
808 | } |
||
809 | break; |
||
810 | |||
811 | case 'this_year' : |
||
812 | $dates['day'] = null; |
||
813 | $dates['m_start'] = null; |
||
814 | $dates['m_end'] = null; |
||
815 | $dates['year'] = date( 'Y', $current_time ); |
||
816 | break; |
||
817 | |||
818 | case 'last_year' : |
||
819 | $dates['day'] = null; |
||
820 | $dates['m_start'] = null; |
||
821 | $dates['m_end'] = null; |
||
822 | $dates['year'] = date( 'Y', $current_time ) - 1; |
||
823 | break; |
||
824 | |||
825 | endswitch; |
||
826 | } |
||
827 | |||
828 | /** |
||
829 | * Returns the filters for the dates used to retreive earnings/donations |
||
830 | * |
||
831 | * @since 1.2 |
||
832 | * |
||
833 | * @param object $dates The dates used for retreiving earnings/donations |
||
834 | */ |
||
835 | |||
836 | return apply_filters( 'give_api_stat_dates', $dates ); |
||
837 | } |
||
838 | |||
839 | /** |
||
840 | * Process Get Customers API Request |
||
841 | * |
||
842 | * @access public |
||
843 | * @since 1.1 |
||
844 | * @global object $wpdb Used to query the database using the WordPress |
||
845 | * Database API |
||
846 | * |
||
847 | * @param int $customer Customer ID |
||
848 | * |
||
849 | * @return array $customers Multidimensional array of the customers |
||
850 | */ |
||
851 | 1 | public function get_customers( $customer = null ) { |
|
932 | |||
933 | /** |
||
934 | * Process Get Products API Request |
||
935 | * |
||
936 | * @access public |
||
937 | * @since 1.1 |
||
938 | * |
||
939 | * @param int $form Give Form ID |
||
940 | * |
||
941 | * @return array $customers Multidimensional array of the forms |
||
942 | */ |
||
943 | 10 | public function get_forms( $form = null ) { |
|
980 | |||
981 | /** |
||
982 | * Given a give_forms post object, generate the data for the API output |
||
983 | * |
||
984 | * @since 1.1 |
||
985 | * |
||
986 | * @param object $form_info The Download Post Object |
||
987 | * |
||
988 | * @return array Array of post data to return back in the API |
||
989 | */ |
||
990 | 10 | private function get_form_data( $form_info ) { |
|
1042 | |||
1043 | /** |
||
1044 | * Process Get Stats API Request |
||
1045 | * |
||
1046 | * @since 1.1 |
||
1047 | * |
||
1048 | * @global object $wpdb Used to query the database using the WordPress |
||
1049 | * |
||
1050 | * @param array $args Arguments provided by API Request |
||
1051 | * |
||
1052 | * @return array |
||
1053 | */ |
||
1054 | public function get_stats( $args = array() ) { |
||
1314 | |||
1315 | /** |
||
1316 | * Retrieves Recent Sales |
||
1317 | * |
||
1318 | * @access public |
||
1319 | * @since 1.1 |
||
1320 | * @return array |
||
1321 | */ |
||
1322 | 10 | public function get_recent_donations() { |
|
1418 | |||
1419 | /** |
||
1420 | * Retrieve the output format |
||
1421 | * |
||
1422 | * Determines whether results should be displayed in XML or JSON |
||
1423 | * |
||
1424 | * @since 1.1 |
||
1425 | * |
||
1426 | * @return mixed|void |
||
1427 | */ |
||
1428 | public function get_output_format() { |
||
1435 | |||
1436 | |||
1437 | /** |
||
1438 | * Log each API request, if enabled |
||
1439 | * |
||
1440 | * @access private |
||
1441 | * @since 1.1 |
||
1442 | * @global $give_logs |
||
1443 | * @global $wp_query |
||
1444 | * |
||
1445 | * @param array $data |
||
1446 | * |
||
1447 | * @return void |
||
1448 | */ |
||
1449 | private function log_request( $data = array() ) { |
||
1489 | |||
1490 | |||
1491 | /** |
||
1492 | * Retrieve the output data |
||
1493 | * |
||
1494 | * @access public |
||
1495 | * @since 1.1 |
||
1496 | * @return array |
||
1497 | */ |
||
1498 | public function get_output() { |
||
1501 | |||
1502 | /** |
||
1503 | * Output Query in either JSON/XML. The query data is outputted as JSON |
||
1504 | * by default |
||
1505 | * |
||
1506 | * @since 1.1 |
||
1507 | * @global $wp_query |
||
1508 | * |
||
1509 | * @param int $status_code |
||
1510 | */ |
||
1511 | public function output( $status_code = 200 ) { |
||
1555 | |||
1556 | /** |
||
1557 | * Modify User Profile |
||
1558 | * |
||
1559 | * Modifies the output of profile.php to add key generation/revocation |
||
1560 | * |
||
1561 | * @access public |
||
1562 | * @since 1.1 |
||
1563 | * |
||
1564 | * @param object $user Current user info |
||
1565 | * |
||
1566 | * @return void |
||
1567 | */ |
||
1568 | function user_key_field( $user ) { |
||
1606 | |||
1607 | /** |
||
1608 | * Process an API key generation/revocation |
||
1609 | * |
||
1610 | * @access public |
||
1611 | * @since 1.1 |
||
1612 | * |
||
1613 | * @param array $args |
||
1614 | * |
||
1615 | * @return void |
||
1616 | */ |
||
1617 | public function process_api_key( $args ) { |
||
1670 | |||
1671 | /** |
||
1672 | * Generate new API keys for a user |
||
1673 | * |
||
1674 | * @access public |
||
1675 | * @since 1.1 |
||
1676 | * |
||
1677 | * @param int $user_id User ID the key is being generated for |
||
1678 | * @param boolean $regenerate Regenerate the key for the user |
||
1679 | * |
||
1680 | * @return boolean True if (re)generated succesfully, false otherwise. |
||
1681 | */ |
||
1682 | public function generate_api_key( $user_id = 0, $regenerate = false ) { |
||
1713 | |||
1714 | /** |
||
1715 | * Revoke a users API keys |
||
1716 | * |
||
1717 | * @access public |
||
1718 | * @since 1.1 |
||
1719 | * |
||
1720 | * @param int $user_id User ID of user to revoke key for |
||
1721 | * |
||
1722 | * @return string |
||
1723 | */ |
||
1724 | public function revoke_api_key( $user_id = 0 ) { |
||
1750 | |||
1751 | 2 | public function get_version() { |
|
1754 | |||
1755 | |||
1756 | /** |
||
1757 | * Generate and Save API key |
||
1758 | * |
||
1759 | * Generates the key requested by user_key_field and stores it in the database |
||
1760 | * |
||
1761 | * @access public |
||
1762 | * @since 1.1 |
||
1763 | * |
||
1764 | * @param int $user_id |
||
1765 | * |
||
1766 | * @return void |
||
1767 | */ |
||
1768 | 2 | public function update_key( $user_id ) { |
|
1787 | |||
1788 | /** |
||
1789 | * Generate the public key for a user |
||
1790 | * |
||
1791 | * @access private |
||
1792 | * @since 1.1 |
||
1793 | * |
||
1794 | * @param string $user_email |
||
1795 | * |
||
1796 | * @return string |
||
1797 | */ |
||
1798 | 2 | private function generate_public_key( $user_email = '' ) { |
|
1803 | |||
1804 | /** |
||
1805 | * Generate the secret key for a user |
||
1806 | * |
||
1807 | * @access private |
||
1808 | * @since 1.1 |
||
1809 | * |
||
1810 | * @param int $user_id |
||
1811 | * |
||
1812 | * @return string |
||
1813 | */ |
||
1814 | 2 | private function generate_private_key( $user_id = 0 ) { |
|
1820 | |||
1821 | /** |
||
1822 | * Retrieve the user's token |
||
1823 | * |
||
1824 | * @access private |
||
1825 | * @since 1.1 |
||
1826 | * |
||
1827 | * @param int $user_id |
||
1828 | * |
||
1829 | * @return string |
||
1830 | */ |
||
1831 | public function get_token( $user_id = 0 ) { |
||
1834 | |||
1835 | /** |
||
1836 | * Generate the default sales stats returned by the 'stats' endpoint |
||
1837 | * |
||
1838 | * @access private |
||
1839 | * @since 1.1 |
||
1840 | * @return array default sales statistics |
||
1841 | */ |
||
1842 | private function get_default_sales_stats() { |
||
1853 | |||
1854 | /** |
||
1855 | * Generate the default earnings stats returned by the 'stats' endpoint |
||
1856 | * |
||
1857 | * @access private |
||
1858 | * @since 1.1 |
||
1859 | * @return array default earnings statistics |
||
1860 | */ |
||
1861 | private function get_default_earnings_stats() { |
||
1872 | |||
1873 | /** |
||
1874 | * API Key Backwards Compatibility |
||
1875 | * |
||
1876 | * @description A Backwards Compatibility call for the change of meta_key/value for users API Keys |
||
1877 | * |
||
1878 | * @since 1.3.6 |
||
1879 | * |
||
1880 | * @param string $check Whether to check the cache or not |
||
1881 | * @param int $object_id The User ID being passed |
||
1882 | * @param string $meta_key The user meta key |
||
1883 | * @param bool $single If it should return a single value or array |
||
1884 | * |
||
1885 | * @return string The API key/secret for the user supplied |
||
1886 | */ |
||
1887 | 38 | public function api_key_backwards_compat( $check, $object_id, $meta_key, $single ) { |
|
1911 | |||
1912 | } |
||
1913 |
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.