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_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 | * Preserve args |
||
| 30 | * |
||
| 31 | * @since 1.8.17 |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | public $_args = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The args to pass to the give_get_payments() query |
||
| 40 | * |
||
| 41 | * @since 1.0 |
||
| 42 | * @access public |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public $args = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The payments found based on the criteria set |
||
| 50 | * |
||
| 51 | * @since 1.0 |
||
| 52 | * @access public |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | public $payments = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Default query arguments. |
||
| 60 | * |
||
| 61 | * 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. |
||
| 62 | * |
||
| 63 | * @since 1.0 |
||
| 64 | * @access public |
||
| 65 | * |
||
| 66 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 67 | */ |
||
| 68 | public function __construct( $args = array() ) { |
||
| 69 | $defaults = array( |
||
| 70 | 'output' => 'payments', |
||
| 71 | 'post_type' => array( 'give_payment' ), |
||
| 72 | 'start_date' => false, |
||
| 73 | 'end_date' => false, |
||
| 74 | 'number' => 20, |
||
| 75 | 'page' => null, |
||
| 76 | 'orderby' => 'ID', |
||
| 77 | 'order' => 'DESC', |
||
| 78 | 'user' => null, |
||
| 79 | 'donor' => null, |
||
| 80 | 'status' => give_get_payment_status_keys(), |
||
| 81 | 'meta_key' => null, |
||
|
|
|||
| 82 | 'year' => null, |
||
| 83 | 'month' => null, |
||
| 84 | 'day' => null, |
||
| 85 | 's' => null, |
||
| 86 | 'search_in_notes' => false, |
||
| 87 | 'children' => false, |
||
| 88 | 'fields' => null, |
||
| 89 | 'gateway' => null, |
||
| 90 | 'give_forms' => null, |
||
| 91 | 'offset' => 0, |
||
| 92 | 'paged' => 0, |
||
| 93 | 'post_parent' => 0, |
||
| 94 | |||
| 95 | // Currently these params only works with get_payment_by_group |
||
| 96 | 'group_by' => '', |
||
| 97 | 'count' => false, |
||
| 98 | ); |
||
| 99 | |||
| 100 | $this->args = $this->_args = wp_parse_args( $args, $defaults ); |
||
| 101 | |||
| 102 | $this->init(); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Set a query variable. |
||
| 107 | * |
||
| 108 | * @since 1.0 |
||
| 109 | * @access public |
||
| 110 | * |
||
| 111 | * @param $query_var |
||
| 112 | * @param $value |
||
| 113 | */ |
||
| 114 | public function __set( $query_var, $value ) { |
||
| 115 | if ( in_array( $query_var, array( 'meta_query', 'tax_query' ) ) ) { |
||
| 116 | $this->args[ $query_var ][] = $value; |
||
| 117 | } else { |
||
| 118 | $this->args[ $query_var ] = $value; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Unset a query variable. |
||
| 124 | * |
||
| 125 | * @since 1.0 |
||
| 126 | * @access public |
||
| 127 | * |
||
| 128 | * @param $query_var |
||
| 129 | */ |
||
| 130 | public function __unset( $query_var ) { |
||
| 131 | unset( $this->args[ $query_var ] ); |
||
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Modify the query/query arguments before we retrieve payments. |
||
| 136 | * |
||
| 137 | * @since 1.0 |
||
| 138 | * @access public |
||
| 139 | * |
||
| 140 | * @return void |
||
| 141 | */ |
||
| 142 | public function init() { |
||
| 143 | } |
||
| 144 | |||
| 145 | |||
| 146 | /** |
||
| 147 | * Set query filter. |
||
| 148 | * |
||
| 149 | * @since 1.8.9 |
||
| 150 | * @access private |
||
| 151 | */ |
||
| 152 | private function set_filters() { |
||
| 153 | // Reset param to apply filters. |
||
| 154 | // While set filters $args will get override and multiple get_payments call will not work. |
||
| 155 | $this->args = $this->_args; |
||
| 156 | |||
| 157 | $this->date_filter_pre(); |
||
| 158 | $this->orderby(); |
||
| 159 | $this->status(); |
||
| 160 | $this->month(); |
||
| 161 | $this->per_page(); |
||
| 162 | $this->page(); |
||
| 163 | $this->user(); |
||
| 164 | $this->donor(); |
||
| 165 | $this->search(); |
||
| 166 | $this->mode(); |
||
| 167 | $this->children(); |
||
| 168 | $this->give_forms(); |
||
| 169 | $this->gateway_filter(); |
||
| 170 | |||
| 171 | add_filter( 'posts_orderby', array( $this, 'custom_orderby' ), 10, 2 ); |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Unset query filter. |
||
| 176 | * |
||
| 177 | * @since 1.8.9 |
||
| 178 | * @access private |
||
| 179 | */ |
||
| 180 | private function unset_filters() { |
||
| 181 | remove_filter( 'posts_orderby', array( $this, 'custom_orderby' ) ); |
||
| 182 | } |
||
| 183 | |||
| 184 | |||
| 185 | /** |
||
| 186 | * Retrieve payments. |
||
| 187 | * |
||
| 188 | * The query can be modified in two ways; either the action before the |
||
| 189 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 190 | * compatibility). |
||
| 191 | * |
||
| 192 | * @since 1.0 |
||
| 193 | * @access public |
||
| 194 | * |
||
| 195 | * @return array |
||
| 196 | */ |
||
| 197 | public function get_payments() { |
||
| 198 | // Modify the query/query arguments before we retrieve payments. |
||
| 199 | $this->set_filters(); |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Fires before retrieving payments. |
||
| 203 | * |
||
| 204 | * @since 1.0 |
||
| 205 | * |
||
| 206 | * @param Give_Payments_Query $this Payments query object. |
||
| 207 | */ |
||
| 208 | do_action( 'give_pre_get_payments', $this ); |
||
| 209 | |||
| 210 | $query = new WP_Query( $this->args ); |
||
| 211 | $this->payments = array(); |
||
| 212 | |||
| 213 | $custom_output = array( |
||
| 214 | 'payments', |
||
| 215 | 'give_payments', |
||
| 216 | ); |
||
| 217 | |||
| 218 | if ( ! in_array( $this->args['output'], $custom_output ) ) { |
||
| 219 | return $query->posts; |
||
| 220 | } |
||
| 221 | |||
| 222 | if ( $query->have_posts() ) { |
||
| 223 | while ( $query->have_posts() ) { |
||
| 224 | $query->the_post(); |
||
| 225 | |||
| 226 | $payment_id = get_post()->ID; |
||
| 227 | $payment = new Give_Payment( $payment_id ); |
||
| 228 | |||
| 229 | $this->payments[] = apply_filters( 'give_payment', $payment, $payment_id, $this ); |
||
| 230 | } |
||
| 231 | |||
| 232 | wp_reset_postdata(); |
||
| 233 | } |
||
| 234 | |||
| 235 | // Remove query filters after we retrieve payments. |
||
| 236 | $this->unset_filters(); |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Fires after retrieving payments. |
||
| 240 | * |
||
| 241 | * @since 1.0 |
||
| 242 | * |
||
| 243 | * @param Give_Payments_Query $this Payments query object. |
||
| 244 | */ |
||
| 245 | do_action( 'give_post_get_payments', $this ); |
||
| 246 | |||
| 247 | return $this->payments; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Get payments by group |
||
| 252 | * |
||
| 253 | * @since 1.8.17 |
||
| 254 | * @access public |
||
| 255 | * |
||
| 256 | * @return array |
||
| 257 | */ |
||
| 258 | public function get_payment_by_group() { |
||
| 259 | global $wpdb; |
||
| 260 | |||
| 261 | $allowed_groups = array( 'post_status' ); |
||
| 262 | $result = array(); |
||
| 263 | |||
| 264 | |||
| 265 | if ( in_array( $this->args['group_by'], $allowed_groups ) ) { |
||
| 266 | // Set only count in result. |
||
| 267 | if ( $this->args['count'] ) { |
||
| 268 | $statuses = get_post_stati(); |
||
| 269 | $status_query = ''; |
||
| 270 | |||
| 271 | $counter = 0; |
||
| 272 | foreach ( $statuses as $status ) { |
||
| 273 | $prefix = $counter ? " OR " : ''; |
||
| 274 | $status_query .= "{$prefix}{$wpdb->posts}.post_status=\"{$status}\""; |
||
| 275 | $counter ++; |
||
| 276 | } |
||
| 277 | |||
| 278 | $new_results = $wpdb->get_results( $this->get_sql(), ARRAY_N ); |
||
| 279 | |||
| 280 | foreach ( $new_results as $results ) { |
||
| 281 | $result[ $results[0] ] = $results[1]; |
||
| 282 | } |
||
| 283 | |||
| 284 | switch ( $this->args['group_by'] ) { |
||
| 285 | case 'post_status': |
||
| 286 | |||
| 287 | if ( isset( $statuses['private'] ) && empty( $args['s'] ) ) { |
||
| 288 | unset( $statuses['private'] ); |
||
| 289 | } |
||
| 290 | |||
| 291 | /* @var Give_Payment $donation */ |
||
| 292 | foreach ( $statuses as $status => $status_label ) { |
||
| 293 | if ( ! isset( $result[ $status ] ) ) { |
||
| 294 | $result[ $status ] = 0; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | break; |
||
| 299 | } |
||
| 300 | } else { |
||
| 301 | $donations = $this->get_payments(); |
||
| 302 | |||
| 303 | /* @var $donation Give_Payment */ |
||
| 304 | foreach ( $donations as $donation ) { |
||
| 305 | $result[ $donation->{$this->args['group_by']} ][] = $donation; |
||
| 306 | } |
||
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | |||
| 311 | /** |
||
| 312 | * Filter the result |
||
| 313 | * |
||
| 314 | * @since 1.8.17 |
||
| 315 | */ |
||
| 316 | return apply_filters( 'give_get_payment_by_group', $result, $this ); |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * If querying a specific date, add the proper filters. |
||
| 321 | * |
||
| 322 | * @since 1.0 |
||
| 323 | * @access public |
||
| 324 | * |
||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | public function date_filter_pre() { |
||
| 328 | if ( ! ( $this->args['start_date'] || $this->args['end_date'] ) ) { |
||
| 329 | return; |
||
| 330 | } |
||
| 331 | |||
| 332 | $this->setup_dates( $this->args['start_date'], $this->args['end_date'] ); |
||
| 333 | $is_start_date = property_exists( __CLASS__, 'start_date' ); |
||
| 334 | $is_end_date = property_exists( __CLASS__, 'end_date' ); |
||
| 335 | |||
| 336 | if( $is_start_date || $is_end_date ) { |
||
| 337 | $date_query = array(); |
||
| 338 | |||
| 339 | if( $is_start_date && ! is_wp_error( $this->start_date )) { |
||
| 340 | $date_query['after'] = date( 'Y-m-d H:i:s', $this->start_date ); |
||
| 341 | } |
||
| 342 | |||
| 343 | if( $is_end_date && ! is_wp_error( $this->end_date ) ) { |
||
| 344 | $date_query['before'] = date( 'Y-m-d H:i:s', $this->end_date ); |
||
| 345 | } |
||
| 346 | |||
| 347 | $this->__set( 'date_query', $date_query ); |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | } |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Post Status |
||
| 355 | * |
||
| 356 | * @since 1.0 |
||
| 357 | * @access public |
||
| 358 | * |
||
| 359 | * @return void |
||
| 360 | */ |
||
| 361 | public function status() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Current Page |
||
| 372 | * |
||
| 373 | * @since 1.0 |
||
| 374 | * @access public |
||
| 375 | * |
||
| 376 | * @return void |
||
| 377 | */ |
||
| 378 | public function page() { |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Posts Per Page |
||
| 389 | * |
||
| 390 | * @since 1.0 |
||
| 391 | * @access public |
||
| 392 | * |
||
| 393 | * @return void |
||
| 394 | */ |
||
| 395 | public function per_page() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Current Month |
||
| 412 | * |
||
| 413 | * @since 1.0 |
||
| 414 | * @access public |
||
| 415 | * |
||
| 416 | * @return void |
||
| 417 | */ |
||
| 418 | public function month() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Order by |
||
| 429 | * |
||
| 430 | * @since 1.0 |
||
| 431 | * @access public |
||
| 432 | * |
||
| 433 | * @return void |
||
| 434 | */ |
||
| 435 | public function orderby() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Custom orderby. |
||
| 459 | * Note: currently custom sorting is only used for donation listing page. |
||
| 460 | * |
||
| 461 | * @since 1.8 |
||
| 462 | * @access public |
||
| 463 | * |
||
| 464 | * @param string $order |
||
| 465 | * @param WP_Query $query |
||
| 466 | * |
||
| 467 | * @return mixed |
||
| 468 | */ |
||
| 469 | public function custom_orderby( $order, $query ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Specific User |
||
| 491 | * |
||
| 492 | * @since 1.0 |
||
| 493 | * @access public |
||
| 494 | * |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public function user() { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Specific donor id |
||
| 518 | * |
||
| 519 | * @access public |
||
| 520 | * @since 1.8.9 |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | public function donor() { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Search |
||
| 538 | * |
||
| 539 | * @since 1.0 |
||
| 540 | * @access public |
||
| 541 | * |
||
| 542 | * @return void |
||
| 543 | */ |
||
| 544 | public function search() { |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Payment Mode |
||
| 653 | * |
||
| 654 | * @since 1.0 |
||
| 655 | * @access public |
||
| 656 | * |
||
| 657 | * @return void |
||
| 658 | */ |
||
| 659 | public function mode() { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Children |
||
| 676 | * |
||
| 677 | * @since 1.0 |
||
| 678 | * @access public |
||
| 679 | * |
||
| 680 | * @return void |
||
| 681 | */ |
||
| 682 | public function children() { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Specific Give Form |
||
| 691 | * |
||
| 692 | * @since 1.0 |
||
| 693 | * @access public |
||
| 694 | * |
||
| 695 | * @return void |
||
| 696 | */ |
||
| 697 | View Code Duplication | public function give_forms() { |
|
| 722 | |||
| 723 | /** |
||
| 724 | * Specific Gateway |
||
| 725 | * |
||
| 726 | * @since 1.8.17 |
||
| 727 | * @access public |
||
| 728 | * |
||
| 729 | * @return void |
||
| 730 | */ |
||
| 731 | View Code Duplication | public function gateway_filter() { |
|
| 756 | |||
| 757 | |||
| 758 | /** |
||
| 759 | * Get sql query |
||
| 760 | * |
||
| 761 | * Note: Internal purpose only. We are developing on this fn. |
||
| 762 | * |
||
| 763 | * @since 1.8.18 |
||
| 764 | * @access public |
||
| 765 | * @global $wpdb |
||
| 766 | * |
||
| 767 | * @return string |
||
| 768 | */ |
||
| 769 | public function get_sql() { |
||
| 857 | |||
| 858 | } |
||
| 859 |