Complex classes like Give_Payments_Query 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_Payments_Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Give_Payments_Query extends Give_Stats { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The args to pass to the give_get_payments() query |
||
| 30 | * |
||
| 31 | * @since 1.0 |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | public $args = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The payments found based on the criteria set |
||
| 40 | * |
||
| 41 | * @since 1.0 |
||
| 42 | * @access public |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public $payments = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Default query arguments. |
||
| 50 | * |
||
| 51 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before the query is run to convert them to the proper syntax. |
||
| 52 | * |
||
| 53 | * @since 1.0 |
||
| 54 | * @access public |
||
| 55 | * |
||
| 56 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 57 | 52 | */ |
|
| 58 | public function __construct( $args = array() ) { |
||
| 59 | 52 | $defaults = array( |
|
| 60 | 52 | 'output' => 'payments', |
|
| 61 | 52 | 'post_type' => array( 'give_payment' ), |
|
| 62 | 52 | 'start_date' => false, |
|
| 63 | 52 | 'end_date' => false, |
|
| 64 | 52 | 'number' => 20, |
|
| 65 | 52 | 'page' => null, |
|
| 66 | 52 | 'orderby' => 'ID', |
|
| 67 | 52 | 'order' => 'DESC', |
|
| 68 | 52 | 'user' => null, |
|
| 69 | 52 | 'donor' => null, |
|
| 70 | 52 | 'status' => give_get_payment_status_keys(), |
|
| 71 | 52 | 'meta_key' => null, |
|
|
|
|||
| 72 | 52 | 'year' => null, |
|
| 73 | 52 | 'month' => null, |
|
| 74 | 52 | 'day' => null, |
|
| 75 | 52 | 's' => null, |
|
| 76 | 52 | 'search_in_notes' => false, |
|
| 77 | 'children' => false, |
||
| 78 | 52 | 'fields' => null, |
|
| 79 | 'give_forms' => null, |
||
| 80 | 52 | ); |
|
| 81 | |||
| 82 | 52 | $this->args = wp_parse_args( $args, $defaults ); |
|
| 83 | 52 | ||
| 84 | $this->init(); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Set a query variable. |
||
| 89 | * |
||
| 90 | * @since 1.0 |
||
| 91 | 52 | * @access public |
|
| 92 | 52 | * |
|
| 93 | 32 | * @param $query_var |
|
| 94 | 32 | * @param $value |
|
| 95 | 52 | */ |
|
| 96 | public function __set( $query_var, $value ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | 52 | * Unset a query variable. |
|
| 106 | 52 | * |
|
| 107 | 52 | * @since 1.0 |
|
| 108 | * @access public |
||
| 109 | * |
||
| 110 | * @param $query_var |
||
| 111 | */ |
||
| 112 | public function __unset( $query_var ) { |
||
| 115 | |||
| 116 | 52 | /** |
|
| 117 | * Modify the query/query arguments before we retrieve payments. |
||
| 118 | 52 | * |
|
| 119 | 52 | * @since 1.0 |
|
| 120 | * @access public |
||
| 121 | 52 | * |
|
| 122 | 52 | * @return void |
|
| 123 | 52 | */ |
|
| 124 | 52 | public function init() { |
|
| 126 | 52 | ||
| 127 | 52 | ||
| 128 | 52 | /** |
|
| 129 | 52 | * Set query filter. |
|
| 130 | 52 | * |
|
| 131 | 52 | * @since 1.8.9 |
|
| 132 | * @access private |
||
| 133 | */ |
||
| 134 | private function set_filters() { |
||
| 150 | |||
| 151 | 52 | /** |
|
| 152 | 52 | * Unset query filter. |
|
| 153 | 52 | * |
|
| 154 | * @since 1.8.9 |
||
| 155 | 52 | * @access private |
|
| 156 | 52 | */ |
|
| 157 | private function unset_filters() { |
||
| 161 | |||
| 162 | |||
| 163 | /** |
||
| 164 | * Retrieve payments. |
||
| 165 | * |
||
| 166 | * The query can be modified in two ways; either the action before the |
||
| 167 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 168 | * compatibility). |
||
| 169 | * |
||
| 170 | * @since 1.0 |
||
| 171 | * @access public |
||
| 172 | * |
||
| 173 | * @return array |
||
| 174 | */ |
||
| 175 | public function get_payments() { |
||
| 227 | |||
| 228 | /** |
||
| 229 | * If querying a specific date, add the proper filters. |
||
| 230 | * |
||
| 231 | * @since 1.0 |
||
| 232 | * @access public |
||
| 233 | 52 | * |
|
| 234 | 52 | * @return void |
|
| 235 | 52 | */ |
|
| 236 | public function date_filter_pre() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * If querying a specific date, remove filters after the query has been run |
||
| 248 | * to avoid affecting future queries. |
||
| 249 | 52 | * |
|
| 250 | * @since 1.0 |
||
| 251 | 52 | * @access public |
|
| 252 | 42 | * |
|
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | 52 | public function date_filter_post() { |
|
| 262 | 52 | ||
| 263 | /** |
||
| 264 | * Post Status |
||
| 265 | * |
||
| 266 | * @since 1.0 |
||
| 267 | * @access public |
||
| 268 | * |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | 52 | public function status() { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Current Page |
||
| 282 | * |
||
| 283 | * @since 1.0 |
||
| 284 | * @access public |
||
| 285 | * |
||
| 286 | * @return void |
||
| 287 | 52 | */ |
|
| 288 | 52 | public function page() { |
|
| 296 | 52 | ||
| 297 | 52 | /** |
|
| 298 | * Posts Per Page |
||
| 299 | * |
||
| 300 | * @since 1.0 |
||
| 301 | * @access public |
||
| 302 | * |
||
| 303 | * @return void |
||
| 304 | */ |
||
| 305 | public function per_page() { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Current Month |
||
| 322 | * |
||
| 323 | * @since 1.0 |
||
| 324 | * @access public |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public function month() { |
||
| 336 | 32 | ||
| 337 | /** |
||
| 338 | 32 | * Order by |
|
| 339 | * |
||
| 340 | * @since 1.0 |
||
| 341 | * @access public |
||
| 342 | 32 | * |
|
| 343 | 32 | * @return void |
|
| 344 | */ |
||
| 345 | 32 | public function orderby() { |
|
| 366 | |||
| 367 | 32 | /** |
|
| 368 | 32 | * Custom orderby. |
|
| 369 | * Note: currently custom sorting is only used for donation listing page. |
||
| 370 | 32 | * |
|
| 371 | * @since 1.8 |
||
| 372 | * @access public |
||
| 373 | * |
||
| 374 | * @param string $order |
||
| 375 | * @param WP_Query $query |
||
| 376 | * |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | public function custom_orderby( $order, $query ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Specific User |
||
| 398 | * |
||
| 399 | * @since 1.0 |
||
| 400 | * @access public |
||
| 401 | * |
||
| 402 | * @return void |
||
| 403 | */ |
||
| 404 | public function user() { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Specific donor id |
||
| 423 | * |
||
| 424 | * @access public |
||
| 425 | * @since 1.8.9 |
||
| 426 | * @return void |
||
| 427 | */ |
||
| 428 | public function donor() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Search |
||
| 441 | * |
||
| 442 | * @since 1.0 |
||
| 443 | * @access public |
||
| 444 | * |
||
| 445 | 52 | * @return void |
|
| 446 | 52 | */ |
|
| 447 | 52 | public function search() { |
|
| 553 | |||
| 554 | /** |
||
| 555 | * Payment Mode |
||
| 556 | * |
||
| 557 | * @since 1.0 |
||
| 558 | * @access public |
||
| 559 | * |
||
| 560 | * @return void |
||
| 561 | */ |
||
| 562 | public function mode() { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Children |
||
| 577 | * |
||
| 578 | * @since 1.0 |
||
| 579 | * @access public |
||
| 580 | * |
||
| 581 | * @return void |
||
| 582 | */ |
||
| 583 | public function children() { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Specific Give Form |
||
| 592 | * |
||
| 593 | * @since 1.0 |
||
| 594 | * @access public |
||
| 595 | * |
||
| 596 | * @return void |
||
| 597 | */ |
||
| 598 | public function give_forms() { |
||
| 621 | |||
| 622 | } |
||
| 623 |