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:
| 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 array |
||
| 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 array |
||
| 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 array |
||
| 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 the query is run to convert them to the proper syntax. |
||
| 82 | * |
||
| 83 | * @since 1.8.14 |
||
| 84 | * @access public |
||
| 85 | * |
||
| 86 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 87 | */ |
||
| 88 | public function __construct( $args = array() ) { |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Modify the query/query arguments before we retrieve donors. |
||
| 115 | * |
||
| 116 | * @since 1.8.14 |
||
| 117 | * @access public |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function init() { |
||
| 123 | |||
| 124 | |||
| 125 | /** |
||
| 126 | * Retrieve donors. |
||
| 127 | * |
||
| 128 | * The query can be modified in two ways; either the action before the |
||
| 129 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 130 | * compatibility). |
||
| 131 | * |
||
| 132 | * @since 1.8.14 |
||
| 133 | * @access public |
||
| 134 | * |
||
| 135 | * @global wpdb $wpdb |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function get_donors() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Get sql query from queried array. |
||
| 171 | * |
||
| 172 | * @since 2.0 |
||
| 173 | * @access public |
||
| 174 | * |
||
| 175 | * @global wpdb $wpdb |
||
| 176 | * @return string |
||
| 177 | */ |
||
| 178 | public function get_sql() { |
||
| 179 | global $wpdb; |
||
| 180 | |||
| 181 | if ( $this->args['number'] < 1 ) { |
||
| 182 | $this->args['number'] = 999999999999; |
||
| 183 | } |
||
| 184 | |||
| 185 | $where = $this->get_where_query(); |
||
| 186 | |||
| 187 | |||
| 188 | // Set offset. |
||
| 189 | if ( empty( $this->args['offset'] ) && ( 0 < $this->args['paged'] ) ) { |
||
| 190 | $this->args['offset'] = $this->args['number'] * ( $this->args['paged'] - 1 ); |
||
| 191 | } |
||
| 192 | |||
| 193 | // Set fields. |
||
| 194 | $fields = "{$this->table_name}.*"; |
||
| 195 | if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) { |
||
| 196 | if ( is_string( $this->args['fields'] ) ) { |
||
| 197 | $fields = "{$this->table_name}.{$this->args['fields']}"; |
||
| 198 | } else if ( is_array( $this->args['fields'] ) ) { |
||
| 199 | $fields = "{$this->table_name}." . implode( " , {$this->table_name}.", $this->args['fields'] ); |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | // Set count. |
||
| 204 | if ( ! empty( $this->args['count'] ) ) { |
||
| 205 | $fields = "COUNT({$this->table_name}.id)"; |
||
| 206 | } |
||
| 207 | |||
| 208 | $orderby = $this->get_order_query(); |
||
| 209 | |||
| 210 | $sql = $wpdb->prepare( |
||
| 211 | "SELECT {$fields} FROM {$this->table_name} LIMIT %d,%d;", |
||
| 212 | absint( $this->args['offset'] ), |
||
| 213 | absint( $this->args['number'] ) |
||
| 214 | ); |
||
| 215 | |||
| 216 | // $where, $orderby and order already prepared query they can generate notice if you re prepare them in above. |
||
| 217 | // WordPress consider LIKE condition as placeholder if start with s,f, or d. |
||
| 218 | $sql = str_replace( 'LIMIT', "{$where} {$orderby} {$this->args['order']} LIMIT", $sql ); |
||
| 219 | |||
| 220 | return $sql; |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Set query where clause. |
||
| 225 | * |
||
| 226 | * @since 1.8.14 |
||
| 227 | * @access private |
||
| 228 | * |
||
| 229 | * @global wpdb $wpdb |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | private function get_where_query() { |
||
| 233 | $where = ''; |
||
| 234 | |||
| 235 | // Get sql query for meta. |
||
| 236 | if ( ! empty( $this->args['meta_query'] ) ) { |
||
| 237 | $meta_query_object = new WP_Meta_Query( $this->args['meta_query'] ); |
||
| 238 | $meta_query = $meta_query_object->get_sql( |
||
| 239 | $this->meta_type, |
||
| 240 | $this->table_name, |
||
| 241 | 'id' |
||
| 242 | ); |
||
| 243 | |||
| 244 | $where = implode( '', $meta_query ); |
||
| 245 | } |
||
| 246 | |||
| 247 | $where .= 'WHERE 1=1 '; |
||
| 248 | $where .= $this->get_where_search(); |
||
| 249 | $where .= $this->get_where_email(); |
||
| 250 | $where .= $this->get_where_donor(); |
||
| 251 | $where .= $this->get_where_user(); |
||
| 252 | $where .= $this->get_where_date(); |
||
| 253 | |||
| 254 | return trim( $where ); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Set email where clause. |
||
| 259 | * |
||
| 260 | * @since 1.8.14 |
||
| 261 | * @access private |
||
| 262 | * |
||
| 263 | * @global wpdb $wpdb |
||
| 264 | * @return string |
||
| 265 | */ |
||
| 266 | private function get_where_email() { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set donor where clause. |
||
| 290 | * |
||
| 291 | * @since 1.8.14 |
||
| 292 | * @access private |
||
| 293 | * |
||
| 294 | * @global wpdb $wpdb |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | View Code Duplication | private function get_where_donor() { |
|
| 312 | |||
| 313 | /** |
||
| 314 | * Set date where clause. |
||
| 315 | * |
||
| 316 | * @since 1.8.14 |
||
| 317 | * @access private |
||
| 318 | * |
||
| 319 | * @global wpdb $wpdb |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | private function get_where_date() { |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Set search where clause. |
||
| 352 | * |
||
| 353 | * @since 1.8.14 |
||
| 354 | * @access private |
||
| 355 | * |
||
| 356 | * @global wpdb $wpdb |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | private function get_where_search() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set user where clause. |
||
| 384 | * |
||
| 385 | * @since 1.8.14 |
||
| 386 | * @access private |
||
| 387 | * |
||
| 388 | * @global wpdb $wpdb |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | View Code Duplication | private function get_where_user() { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Set orderby query |
||
| 409 | * |
||
| 410 | * @since 1.8.14 |
||
| 411 | * @access private |
||
| 412 | * |
||
| 413 | * @return string |
||
| 414 | */ |
||
| 415 | private function get_order_query() { |
||
| 437 | } |
||
| 438 |