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() ) { | 
            ||
| 89 | $defaults = array(  | 
            ||
| 90 | 'number' => 20,  | 
            ||
| 91 | 'offset' => 0,  | 
            ||
| 92 | 'paged' => 1,  | 
            ||
| 93 | 'orderby' => 'id',  | 
            ||
| 94 | 'order' => 'DESC',  | 
            ||
| 95 | 'user' => null,  | 
            ||
| 96 | 'email' => null,  | 
            ||
| 97 | 'donor' => null,  | 
            ||
| 98 | 'meta_query' => array(),  | 
            ||
| 
                                                                                                    
                        
                         | 
                |||
| 99 | 'date_query' => array(),  | 
            ||
| 100 | 's' => null,  | 
            ||
| 101 | 'fields' => 'all', // Support donors (all fields) or valid column as string or array list  | 
            ||
| 102 | 'count' => false,  | 
            ||
| 103 | // 'form' => array(),  | 
            ||
| 104 | );  | 
            ||
| 105 | |||
| 106 | $this->args = wp_parse_args( $args, $defaults );  | 
            ||
| 107 | $this->table_name = Give()->donors->table_name;  | 
            ||
| 108 | $this->meta_table_name = Give()->donor_meta->table_name;  | 
            ||
| 109 | $this->meta_type = Give()->donor_meta->meta_type;  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * Modify the query/query arguments before we retrieve donors.  | 
            ||
| 114 | *  | 
            ||
| 115 | * @since 1.8.14  | 
            ||
| 116 | * @access public  | 
            ||
| 117 | *  | 
            ||
| 118 | * @return void  | 
            ||
| 119 | */  | 
            ||
| 120 | 	public function init() { | 
            ||
| 121 | }  | 
            ||
| 122 | |||
| 123 | |||
| 124 | /**  | 
            ||
| 125 | * Retrieve donors.  | 
            ||
| 126 | *  | 
            ||
| 127 | * The query can be modified in two ways; either the action before the  | 
            ||
| 128 | * query is run, or the filter on the arguments (existing mainly for backwards  | 
            ||
| 129 | * compatibility).  | 
            ||
| 130 | *  | 
            ||
| 131 | * @since 1.8.14  | 
            ||
| 132 | * @access public  | 
            ||
| 133 | *  | 
            ||
| 134 | * @global wpdb $wpdb  | 
            ||
| 135 | *  | 
            ||
| 136 | * @return array  | 
            ||
| 137 | */  | 
            ||
| 138 | 	public function get_donors() { | 
            ||
| 139 | global $wpdb;  | 
            ||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * Fires before retrieving donors.  | 
            ||
| 143 | *  | 
            ||
| 144 | * @since 1.8.14  | 
            ||
| 145 | *  | 
            ||
| 146 | * @param Give_Donors_Query $this Donors query object.  | 
            ||
| 147 | */  | 
            ||
| 148 | do_action( 'give_pre_get_donors', $this );  | 
            ||
| 149 | |||
| 150 | 		if ( empty( $this->args['count'] ) ) { | 
            ||
| 151 | $this->donors = $wpdb->get_results( $this->get_sql() );  | 
            ||
| 152 | 		} else { | 
            ||
| 153 | $this->donors = $wpdb->get_var( $this->get_sql() );  | 
            ||
| 154 | }  | 
            ||
| 155 | |||
| 156 | /**  | 
            ||
| 157 | * Fires after retrieving donors.  | 
            ||
| 158 | *  | 
            ||
| 159 | * @since 1.8.14  | 
            ||
| 160 | *  | 
            ||
| 161 | * @param Give_Donors_Query $this Donors query object.  | 
            ||
| 162 | */  | 
            ||
| 163 | do_action( 'give_post_get_donors', $this );  | 
            ||
| 164 | |||
| 165 | return $this->donors;  | 
            ||
| 166 | }  | 
            ||
| 167 | |||
| 168 | /**  | 
            ||
| 169 | * Get sql query from queried array.  | 
            ||
| 170 | *  | 
            ||
| 171 | * @since 2.0  | 
            ||
| 172 | * @access public  | 
            ||
| 173 | *  | 
            ||
| 174 | * @global wpdb $wpdb  | 
            ||
| 175 | * @return string  | 
            ||
| 176 | */  | 
            ||
| 177 | 	public function get_sql() { | 
            ||
| 178 | global $wpdb;  | 
            ||
| 179 | |||
| 180 | 		if ( $this->args['number'] < 1 ) { | 
            ||
| 181 | $this->args['number'] = 999999999999;  | 
            ||
| 182 | }  | 
            ||
| 183 | |||
| 184 | $where = $this->get_where_query();  | 
            ||
| 185 | |||
| 186 | |||
| 187 | // Set offset.  | 
            ||
| 188 | 		if ( empty( $this->args['offset'] ) && ( 0 < $this->args['paged'] ) ) { | 
            ||
| 189 | $this->args['offset'] = $this->args['number'] * ( $this->args['paged'] - 1 );  | 
            ||
| 190 | }  | 
            ||
| 191 | |||
| 192 | // Set fields.  | 
            ||
| 193 | 		$fields = "{$this->table_name}.*"; | 
            ||
| 194 | 		if ( ! empty( $this->args['fields'] ) && 'all' !== $this->args['fields'] ) { | 
            ||
| 195 | 			if ( is_string( $this->args['fields'] ) ) { | 
            ||
| 196 | 				$fields = "{$this->table_name}.{$this->args['fields']}"; | 
            ||
| 197 | 			} else if ( is_array( $this->args['fields'] ) ) { | 
            ||
| 198 | 				$fields = "{$this->table_name}." . implode( " , {$this->table_name}.", $this->args['fields'] ); | 
            ||
| 199 | }  | 
            ||
| 200 | }  | 
            ||
| 201 | |||
| 202 | // Set count.  | 
            ||
| 203 | 		if ( ! empty( $this->args['count'] ) ) { | 
            ||
| 204 | 			$fields = "COUNT({$this->table_name}.id)"; | 
            ||
| 205 | }  | 
            ||
| 206 | |||
| 207 | $orderby = $this->get_order_query();  | 
            ||
| 208 | |||
| 209 | $sql = $wpdb->prepare(  | 
            ||
| 210 | 			"SELECT {$fields} FROM {$this->table_name} LIMIT %d,%d;", | 
            ||
| 211 | absint( $this->args['offset'] ),  | 
            ||
| 212 | absint( $this->args['number'] )  | 
            ||
| 213 | );  | 
            ||
| 214 | |||
| 215 | // $where, $orderby and order already prepared query they can generate notice if you re prepare them in above.  | 
            ||
| 216 | // WordPress consider LIKE condition as placeholder if start with s,f, or d.  | 
            ||
| 217 | 		$sql = str_replace( 'LIMIT', "{$where} {$orderby} {$this->args['order']} LIMIT", $sql ); | 
            ||
| 218 | |||
| 219 | return $sql;  | 
            ||
| 220 | }  | 
            ||
| 221 | |||
| 222 | /**  | 
            ||
| 223 | * Set query where clause.  | 
            ||
| 224 | *  | 
            ||
| 225 | * @since 1.8.14  | 
            ||
| 226 | * @access private  | 
            ||
| 227 | *  | 
            ||
| 228 | * @global wpdb $wpdb  | 
            ||
| 229 | * @return string  | 
            ||
| 230 | */  | 
            ||
| 231 | 	private function get_where_query() { | 
            ||
| 232 | $where = '';  | 
            ||
| 233 | |||
| 234 | // Get sql query for meta.  | 
            ||
| 235 | 		if ( ! empty( $this->args['meta_query'] ) ) { | 
            ||
| 236 | $meta_query_object = new WP_Meta_Query( $this->args['meta_query'] );  | 
            ||
| 237 | $meta_query = $meta_query_object->get_sql(  | 
            ||
| 238 | $this->meta_type,  | 
            ||
| 239 | $this->table_name,  | 
            ||
| 240 | 'id'  | 
            ||
| 241 | );  | 
            ||
| 242 | |||
| 243 | $where = implode( '', $meta_query );  | 
            ||
| 244 | }  | 
            ||
| 245 | |||
| 246 | $where .= 'WHERE 1=1 ';  | 
            ||
| 247 | $where .= $this->get_where_search();  | 
            ||
| 248 | $where .= $this->get_where_email();  | 
            ||
| 249 | $where .= $this->get_where_donor();  | 
            ||
| 250 | $where .= $this->get_where_user();  | 
            ||
| 251 | $where .= $this->get_where_date();  | 
            ||
| 252 | |||
| 253 | return trim( $where );  | 
            ||
| 254 | |||
| 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 |