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_Donors_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_Donors_Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Give_Donors_Query { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The args to pass to the give_get_donors() query |
||
| 30 | * |
||
| 31 | * @since 1.8.14 |
||
| 32 | * @access public |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | public $args = array(); |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The donors found based on the criteria set |
||
| 40 | * |
||
| 41 | * @since 1.8.14 |
||
| 42 | * @access public |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | public $donors = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The donors found based on the criteria set |
||
| 50 | * |
||
| 51 | * @since 1.8.14 |
||
| 52 | * @access public |
||
| 53 | * |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | public $table_name = ''; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The donors found based on the criteria set |
||
| 60 | * |
||
| 61 | * @since 1.8.14 |
||
| 62 | * @access public |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | public $meta_table_name = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The donors found based on the criteria set |
||
| 70 | * |
||
| 71 | * @since 1.8.14 |
||
| 72 | * @access public |
||
| 73 | * |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | public $meta_type = ''; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Default query arguments. |
||
| 80 | * |
||
| 81 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
||
| 82 | * the query is run to convert them to the proper syntax. |
||
| 83 | * |
||
| 84 | * @since 1.8.14 |
||
| 85 | * @access public |
||
| 86 | * |
||
| 87 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 88 | */ |
||
| 89 | public function __construct( $args = array() ) { |
||
| 90 | $defaults = array( |
||
| 91 | 'number' => 20, |
||
| 92 | 'offset' => 0, |
||
| 93 | 'paged' => 1, |
||
| 94 | 'orderby' => 'id', |
||
| 95 | 'order' => 'DESC', |
||
| 96 | 'user' => null, |
||
| 97 | 'email' => null, |
||
| 98 | 'donor' => null, |
||
| 99 | 'meta_query' => array(), |
||
|
|
|||
| 100 | 'date_query' => array(), |
||
| 101 | 's' => null, |
||
| 102 | 'fields' => 'all', // Supports donors (all fields) or valid column as string or array list. |
||
| 103 | 'count' => false, |
||
| 104 | 'give_forms' => array(), |
||
| 105 | |||
| 106 | /* |
||
| 107 | * donation_amount will contain value like: |
||
| 108 | * array( |
||
| 109 | * 'compare' => *compare symbol* (by default set to > ) |
||
| 110 | * 'amount' => *numeric_value* |
||
| 111 | * ) |
||
| 112 | * |
||
| 113 | * You can also pass number value to this param then compare symbol will auto set to > |
||
| 114 | */ |
||
| 115 | 'donation_amount' => array() |
||
| 116 | // 'form' => array(), |
||
| 117 | ); |
||
| 118 | |||
| 119 | $this->args = wp_parse_args( $args, $defaults ); |
||
| 120 | $this->table_name = Give()->donors->table_name; |
||
| 121 | $this->meta_table_name = Give()->donor_meta->table_name; |
||
| 122 | $this->meta_type = Give()->donor_meta->meta_type; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Modify the query/query arguments before we retrieve donors. |
||
| 127 | * |
||
| 128 | * @since 1.8.14 |
||
| 129 | * @access public |
||
| 130 | * |
||
| 131 | * @return void |
||
| 132 | */ |
||
| 133 | public function init() { |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | /** |
||
| 138 | * Retrieve donors. |
||
| 139 | * |
||
| 140 | * The query can be modified in two ways; either the action before the |
||
| 141 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 142 | * compatibility). |
||
| 143 | * |
||
| 144 | * @since 1.8.14 |
||
| 145 | * @access public |
||
| 146 | * |
||
| 147 | * @global wpdb $wpdb |
||
| 148 | * |
||
| 149 | * @return array |
||
| 150 | */ |
||
| 151 | public function get_donors() { |
||
| 152 | global $wpdb; |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Fires before retrieving donors. |
||
| 156 | * |
||
| 157 | * @since 1.8.14 |
||
| 158 | * |
||
| 159 | * @param Give_Donors_Query $this Donors query object. |
||
| 160 | */ |
||
| 161 | do_action( 'give_pre_get_donors', $this ); |
||
| 162 | |||
| 163 | $cache_key = Give_Cache::get_key( 'give_donor', $this->get_sql(), false ); |
||
| 164 | |||
| 165 | // Get donors from cache. |
||
| 166 | $this->donors = Give_Cache::get_db_query( $cache_key ); |
||
| 167 | |||
| 168 | if ( is_null( $this->donors ) ) { |
||
| 169 | if ( empty( $this->args['count'] ) ) { |
||
| 170 | $this->donors = $wpdb->get_results( $this->get_sql() ); |
||
| 171 | } else { |
||
| 172 | $this->donors = $wpdb->get_var( $this->get_sql() ); |
||
| 173 | } |
||
| 174 | |||
| 175 | Give_Cache::set_db_query( $cache_key, $this->donors ); |
||
| 176 | } |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Fires after retrieving donors. |
||
| 181 | * |
||
| 182 | * @since 1.8.14 |
||
| 183 | * |
||
| 184 | * @param Give_Donors_Query $this Donors query object. |
||
| 185 | */ |
||
| 186 | do_action( 'give_post_get_donors', $this ); |
||
| 187 | |||
| 188 | return $this->donors; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Get sql query from queried array. |
||
| 193 | * |
||
| 194 | * @since 2.0 |
||
| 195 | * @access public |
||
| 196 | * |
||
| 197 | * @global wpdb $wpdb |
||
| 198 | * @return string |
||
| 199 | */ |
||
| 200 | public function get_sql() { |
||
| 201 | global $wpdb; |
||
| 202 | |||
| 203 | if ( $this->args['number'] < 1 ) { |
||
| 204 | $this->args['number'] = 99999999999; |
||
| 205 | } |
||
| 206 | |||
| 207 | $where = $this->get_where_query(); |
||
| 208 | |||
| 209 | |||
| 210 | // Set offset. |
||
| 211 | if ( empty( $this->args['offset'] ) && ( 0 < $this->args['paged'] ) ) { |
||
| 212 | $this->args['offset'] = $this->args['number'] * ( $this->args['paged'] - 1 ); |
||
| 213 | } |
||
| 214 | |||
| 215 | // Set fields. |
||
| 216 | $fields = "{$this->table_name}.*"; |
||
| 217 | if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) { |
||
| 218 | if ( is_string( $this->args['fields'] ) ) { |
||
| 219 | $fields = "{$this->table_name}.{$this->args['fields']}"; |
||
| 220 | View Code Duplication | } elseif ( is_array( $this->args['fields'] ) ) { |
|
| 221 | $fields = "{$this->table_name}." . implode( " , {$this->table_name}.", $this->args['fields'] ); |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | // Set count. |
||
| 226 | if ( ! empty( $this->args['count'] ) ) { |
||
| 227 | $fields = "COUNT({$this->table_name}.id)"; |
||
| 228 | } |
||
| 229 | |||
| 230 | $orderby = $this->get_order_query(); |
||
| 231 | |||
| 232 | $sql = $wpdb->prepare( "SELECT {$fields} FROM {$this->table_name} LIMIT %d,%d;", absint( $this->args['offset'] ), absint( $this->args['number'] ) ); |
||
| 233 | |||
| 234 | // $where, $orderby and order already prepared query they can generate notice if you re prepare them in above. |
||
| 235 | // WordPress consider LIKE condition as placeholder if start with s,f, or d. |
||
| 236 | $sql = str_replace( 'LIMIT', "{$where} {$orderby} {$this->args['order']} LIMIT", $sql ); |
||
| 237 | |||
| 238 | return $sql; |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Set query where clause. |
||
| 243 | * |
||
| 244 | * @since 1.8.14 |
||
| 245 | * @access private |
||
| 246 | * |
||
| 247 | * @global wpdb $wpdb |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | private function get_where_query() { |
||
| 251 | |||
| 252 | // Get sql query for meta. |
||
| 253 | if ( ! empty( $this->args['meta_query'] ) ) { |
||
| 254 | $meta_query_object = new WP_Meta_Query( $this->args['meta_query'] ); |
||
| 255 | $meta_query = $meta_query_object->get_sql( $this->meta_type, $this->table_name, 'id' ); |
||
| 256 | |||
| 257 | $where[] = implode( '', $meta_query ); |
||
| 258 | } |
||
| 259 | |||
| 260 | $where[] = 'WHERE 1=1'; |
||
| 261 | $where[] = $this->get_where_search(); |
||
| 262 | $where[] = $this->get_where_email(); |
||
| 263 | $where[] = $this->get_where_donor(); |
||
| 264 | $where[] = $this->get_where_user(); |
||
| 265 | $where[] = $this->get_where_date(); |
||
| 266 | $where[] = $this->get_where_purchase_count(); |
||
| 267 | $where[] = $this->get_where_give_forms(); |
||
| 268 | |||
| 269 | $where = array_filter( $where ); |
||
| 270 | |||
| 271 | return trim( implode( ' ', array_map( 'trim', $where ) ) ); |
||
| 272 | |||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Set email where clause. |
||
| 277 | * |
||
| 278 | * @since 1.8.14 |
||
| 279 | * @access private |
||
| 280 | * |
||
| 281 | * @global wpdb $wpdb |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | private function get_where_email() { |
||
| 285 | global $wpdb; |
||
| 286 | |||
| 287 | $where = ''; |
||
| 288 | |||
| 289 | if ( ! empty( $this->args['email'] ) ) { |
||
| 290 | |||
| 291 | if ( is_array( $this->args['email'] ) ) { |
||
| 292 | |||
| 293 | $emails_count = count( $this->args['email'] ); |
||
| 294 | $emails_placeholder = array_fill( 0, $emails_count, '%s' ); |
||
| 295 | $emails = implode( ', ', $emails_placeholder ); |
||
| 296 | |||
| 297 | $where .= $wpdb->prepare( "AND {$this->table_name}.email IN( $emails )", $this->args['email'] ); |
||
| 298 | } else { |
||
| 299 | $where .= $wpdb->prepare( "AND {$this->table_name}.email = %s", $this->args['email'] ); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | |||
| 303 | return $where; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Set donor where clause. |
||
| 308 | * |
||
| 309 | * @since 1.8.14 |
||
| 310 | * @access private |
||
| 311 | * |
||
| 312 | * @global wpdb $wpdb |
||
| 313 | * @return string |
||
| 314 | */ |
||
| 315 | View Code Duplication | private function get_where_donor() { |
|
| 316 | $where = ''; |
||
| 317 | |||
| 318 | // Specific donors. |
||
| 319 | if ( ! empty( $this->args['donor'] ) ) { |
||
| 320 | if ( ! is_array( $this->args['donor'] ) ) { |
||
| 321 | $this->args['donor'] = explode( ',', $this->args['donor'] ); |
||
| 322 | } |
||
| 323 | $donor_ids = implode( ',', array_map( 'intval', $this->args['donor'] ) ); |
||
| 324 | |||
| 325 | $where .= "AND {$this->table_name}.id IN( {$donor_ids} )"; |
||
| 326 | } |
||
| 327 | |||
| 328 | return $where; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Set date where clause. |
||
| 333 | * |
||
| 334 | * @since 1.8.14 |
||
| 335 | * @access private |
||
| 336 | * |
||
| 337 | * @global wpdb $wpdb |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | private function get_where_date() { |
||
| 341 | $where = ''; |
||
| 342 | |||
| 343 | // Donors created for a specific date or in a date range |
||
| 344 | if ( ! empty( $this->args['date_query'] ) ) { |
||
| 345 | $date_query_object = new WP_Date_Query( is_array( $this->args['date_query'] ) ? $this->args['date_query'] : wp_parse_args( $this->args['date_query'] ), "{$this->table_name}.date_created" ); |
||
| 346 | |||
| 347 | $where .= str_replace( array( |
||
| 348 | "\n", |
||
| 349 | '( (', |
||
| 350 | '))', |
||
| 351 | ), array( |
||
| 352 | '', |
||
| 353 | '( (', |
||
| 354 | ') )', |
||
| 355 | ), $date_query_object->get_sql() ); |
||
| 356 | } |
||
| 357 | |||
| 358 | return $where; |
||
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set search where clause. |
||
| 363 | * |
||
| 364 | * @since 1.8.14 |
||
| 365 | * @access private |
||
| 366 | * |
||
| 367 | * @global wpdb $wpdb |
||
| 368 | * @return string |
||
| 369 | */ |
||
| 370 | private function get_where_search() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set user where clause. |
||
| 395 | * |
||
| 396 | * @since 1.8.14 |
||
| 397 | * @access private |
||
| 398 | * |
||
| 399 | * @global wpdb $wpdb |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | View Code Duplication | private function get_where_user() { |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Set orderby query |
||
| 420 | * |
||
| 421 | * @since 1.8.14 |
||
| 422 | * @access private |
||
| 423 | * |
||
| 424 | * @return string |
||
| 425 | */ |
||
| 426 | private function get_order_query() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Set purchase value where clause. |
||
| 449 | * @todo: add phpunit test |
||
| 450 | * |
||
| 451 | * @since 2.1.0 |
||
| 452 | * @access private |
||
| 453 | * |
||
| 454 | * @global wpdb $wpdb |
||
| 455 | * @return string |
||
| 456 | */ |
||
| 457 | private function get_where_purchase_count() { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Set give_forms where clause. |
||
| 476 | * |
||
| 477 | * @todo : add phpunit test |
||
| 478 | * |
||
| 479 | * @since 2.1.0 |
||
| 480 | * @access private |
||
| 481 | * |
||
| 482 | * @global wpdb $wpdb |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | private function get_where_give_forms() { |
||
| 524 | } |
||
| 525 |