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 |
||
| 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', // Support donors (all fields) or valid column as string or array list |
||
| 103 | 'count' => false, |
||
| 104 | // 'form' => array(), |
||
| 105 | ); |
||
| 106 | |||
| 107 | $this->args = wp_parse_args( $args, $defaults ); |
||
| 108 | $this->table_name = Give()->donors->table_name; |
||
| 109 | $this->meta_table_name = Give()->donor_meta->table_name; |
||
| 110 | $this->meta_type = Give()->donor_meta->meta_type; |
||
| 111 | } |
||
| 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() { |
||
| 140 | global $wpdb; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Fires before retrieving donors. |
||
| 144 | * |
||
| 145 | * @since 1.8.14 |
||
| 146 | * |
||
| 147 | * @param Give_Donors_Query $this Donors query object. |
||
| 148 | */ |
||
| 149 | do_action( 'give_pre_get_donors', $this ); |
||
| 150 | |||
| 151 | $cache_key = Give_Cache::get_key( 'give_donor', $this->get_sql(), false ); |
||
| 152 | |||
| 153 | // Get donors from cache. |
||
| 154 | $this->donors = Give_Cache::get_db_query( $cache_key ); |
||
| 155 | |||
| 156 | if ( is_null( $this->donors ) ) { |
||
| 157 | if ( empty( $this->args['count'] ) ) { |
||
| 158 | $this->donors = $wpdb->get_results( $this->get_sql() ); |
||
| 159 | } else { |
||
| 160 | $this->donors = $wpdb->get_var( $this->get_sql() ); |
||
| 161 | } |
||
| 162 | |||
| 163 | Give_Cache::set_db_query( $cache_key, $this->donors ); |
||
| 164 | } |
||
| 165 | |||
| 166 | |||
| 167 | /** |
||
| 168 | * Fires after retrieving donors. |
||
| 169 | * |
||
| 170 | * @since 1.8.14 |
||
| 171 | * |
||
| 172 | * @param Give_Donors_Query $this Donors query object. |
||
| 173 | */ |
||
| 174 | do_action( 'give_post_get_donors', $this ); |
||
| 175 | |||
| 176 | return $this->donors; |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get sql query from queried array. |
||
| 181 | * |
||
| 182 | * @since 2.0 |
||
| 183 | * @access public |
||
| 184 | * |
||
| 185 | * @global wpdb $wpdb |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | public function get_sql() { |
||
| 189 | global $wpdb; |
||
| 190 | |||
| 191 | if ( $this->args['number'] < 1 ) { |
||
| 192 | $this->args['number'] = 999999999999; |
||
| 193 | } |
||
| 194 | |||
| 195 | $where = $this->get_where_query(); |
||
| 196 | |||
| 197 | |||
| 198 | // Set offset. |
||
| 199 | if ( empty( $this->args['offset'] ) && ( 0 < $this->args['paged'] ) ) { |
||
| 200 | $this->args['offset'] = $this->args['number'] * ( $this->args['paged'] - 1 ); |
||
| 201 | } |
||
| 202 | |||
| 203 | // Set fields. |
||
| 204 | $fields = "{$this->table_name}.*"; |
||
| 205 | if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) { |
||
| 206 | if ( is_string( $this->args['fields'] ) ) { |
||
| 207 | $fields = "{$this->table_name}.{$this->args['fields']}"; |
||
| 208 | } elseif ( is_array( $this->args['fields'] ) ) { |
||
| 209 | $fields = "{$this->table_name}." . implode( " , {$this->table_name}.", $this->args['fields'] ); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | // Set count. |
||
| 214 | if ( ! empty( $this->args['count'] ) ) { |
||
| 215 | $fields = "COUNT({$this->table_name}.id)"; |
||
| 216 | } |
||
| 217 | |||
| 218 | $orderby = $this->get_order_query(); |
||
| 219 | |||
| 220 | $sql = $wpdb->prepare( |
||
| 221 | "SELECT {$fields} FROM {$this->table_name} LIMIT %d,%d;", |
||
| 222 | absint( $this->args['offset'] ), |
||
| 223 | absint( $this->args['number'] ) |
||
| 224 | ); |
||
| 225 | |||
| 226 | // $where, $orderby and order already prepared query they can generate notice if you re prepare them in above. |
||
| 227 | // WordPress consider LIKE condition as placeholder if start with s,f, or d. |
||
| 228 | $sql = str_replace( 'LIMIT', "{$where} {$orderby} {$this->args['order']} LIMIT", $sql ); |
||
| 229 | |||
| 230 | return $sql; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Set query where clause. |
||
| 235 | * |
||
| 236 | * @since 1.8.14 |
||
| 237 | * @access private |
||
| 238 | * |
||
| 239 | * @global wpdb $wpdb |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | private function get_where_query() { |
||
| 243 | $where = ''; |
||
| 244 | |||
| 245 | // Get sql query for meta. |
||
| 246 | if ( ! empty( $this->args['meta_query'] ) ) { |
||
| 247 | $meta_query_object = new WP_Meta_Query( $this->args['meta_query'] ); |
||
| 248 | $meta_query = $meta_query_object->get_sql( |
||
| 249 | $this->meta_type, |
||
| 250 | $this->table_name, |
||
| 251 | 'id' |
||
| 252 | ); |
||
| 253 | |||
| 254 | $where = implode( '', $meta_query ); |
||
| 255 | } |
||
| 256 | |||
| 257 | $where .= 'WHERE 1=1 '; |
||
| 258 | $where .= $this->get_where_search(); |
||
| 259 | $where .= $this->get_where_email(); |
||
| 260 | $where .= $this->get_where_donor(); |
||
| 261 | $where .= $this->get_where_user(); |
||
| 262 | $where .= $this->get_where_date(); |
||
| 263 | |||
| 264 | return trim( $where ); |
||
| 265 | |||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Set email where clause. |
||
| 270 | * |
||
| 271 | * @since 1.8.14 |
||
| 272 | * @access private |
||
| 273 | * |
||
| 274 | * @global wpdb $wpdb |
||
| 275 | * @return string |
||
| 276 | */ |
||
| 277 | private function get_where_email() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set donor where clause. |
||
| 301 | * |
||
| 302 | * @since 1.8.14 |
||
| 303 | * @access private |
||
| 304 | * |
||
| 305 | * @global wpdb $wpdb |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | View Code Duplication | private function get_where_donor() { |
|
| 323 | |||
| 324 | /** |
||
| 325 | * Set date where clause. |
||
| 326 | * |
||
| 327 | * @since 1.8.14 |
||
| 328 | * @access private |
||
| 329 | * |
||
| 330 | * @global wpdb $wpdb |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | private function get_where_date() { |
||
| 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() { |
||
| 448 | } |
||
| 449 |