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 WC_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 WC_Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class WC_Query { |
||
20 | |||
21 | /** @public array Query vars to add to wp */ |
||
22 | public $query_vars = array(); |
||
23 | |||
24 | /** |
||
25 | * Stores chosen attributes |
||
26 | * @var array |
||
27 | */ |
||
28 | private static $_chosen_attributes; |
||
29 | |||
30 | /** |
||
31 | * Constructor for the query class. Hooks in methods. |
||
32 | * |
||
33 | * @access public |
||
34 | */ |
||
35 | public function __construct() { |
||
47 | |||
48 | /** |
||
49 | * Get any errors from querystring. |
||
50 | */ |
||
51 | public function get_errors() { |
||
56 | |||
57 | /** |
||
58 | * Init query vars by loading options. |
||
59 | */ |
||
60 | public function init_query_vars() { |
||
80 | |||
81 | /** |
||
82 | * Get page title for an endpoint. |
||
83 | * @param string |
||
84 | * @return string |
||
85 | */ |
||
86 | public function get_endpoint_title( $endpoint ) { |
||
132 | |||
133 | /** |
||
134 | * Endpoint mask describing the places the endpoint should be added. |
||
135 | * |
||
136 | * @since 2.6.2 |
||
137 | * @return int |
||
138 | */ |
||
139 | protected function get_endpoints_mask() { |
||
140 | if ( 'page' === get_option( 'show_on_front' ) ) { |
||
141 | $page_on_front = get_option( 'page_on_front' ); |
||
142 | $myaccount_page_id = get_option( 'woocommerce_myaccount_page_id' ); |
||
143 | $checkout_page_id = get_option( 'woocommerce_checkout_page_id' ); |
||
144 | |||
145 | if ( in_array( $page_on_front, array( $myaccount_page_id, $checkout_page_id ) ) ) { |
||
146 | return EP_ROOT | EP_PAGES; |
||
147 | } |
||
148 | } |
||
149 | |||
150 | return EP_PAGES; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Add endpoints for query vars. |
||
155 | */ |
||
156 | public function add_endpoints() { |
||
157 | $mask = $this->get_endpoints_mask(); |
||
158 | |||
159 | foreach ( $this->query_vars as $key => $var ) { |
||
160 | if ( ! empty( $var ) ) { |
||
161 | add_rewrite_endpoint( $var, $mask ); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Add query vars. |
||
168 | * |
||
169 | * @access public |
||
170 | * @param array $vars |
||
171 | * @return array |
||
172 | */ |
||
173 | public function add_query_vars( $vars ) { |
||
179 | |||
180 | /** |
||
181 | * Get query vars. |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | public function get_query_vars() { |
||
188 | |||
189 | /** |
||
190 | * Get query current active query var. |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | public function get_current_endpoint() { |
||
203 | |||
204 | /** |
||
205 | * Parse the request and look for query vars - endpoints may not be supported. |
||
206 | */ |
||
207 | public function parse_request() { |
||
221 | |||
222 | /** |
||
223 | * Are we currently on the front page? |
||
224 | * @return boolean |
||
225 | */ |
||
226 | private function is_showing_page_on_front( $q ) { |
||
229 | |||
230 | /** |
||
231 | * Is the front page a page we define? |
||
232 | * @return boolean |
||
233 | */ |
||
234 | private function page_on_front_is( $page_id ) { |
||
237 | |||
238 | /** |
||
239 | * Hook into pre_get_posts to do the main product query. |
||
240 | * |
||
241 | * @param mixed $q query object |
||
242 | */ |
||
243 | public function pre_get_posts( $q ) { |
||
244 | // We only want to affect the main query |
||
245 | if ( ! $q->is_main_query() ) { |
||
246 | return; |
||
247 | } |
||
248 | |||
249 | // Fix for endpoints on the homepage |
||
250 | if ( $this->is_showing_page_on_front( $q ) && ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) { |
||
251 | $_query = wp_parse_args( $q->query ); |
||
252 | if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->query_vars ) ) ) { |
||
253 | $q->is_page = true; |
||
254 | $q->is_home = false; |
||
255 | $q->is_singular = true; |
||
256 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
||
257 | add_filter( 'redirect_canonical', '__return_false' ); |
||
258 | } |
||
259 | } |
||
260 | |||
261 | // When orderby is set, WordPress shows posts. Get around that here. |
||
262 | if ( $this->is_showing_page_on_front( $q ) && $this->page_on_front_is( wc_get_page_id( 'shop' ) ) ) { |
||
263 | $_query = wp_parse_args( $q->query ); |
||
264 | if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) { |
||
265 | $q->is_page = true; |
||
266 | $q->is_home = false; |
||
267 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
||
268 | $q->set( 'post_type', 'product' ); |
||
269 | } |
||
270 | } |
||
271 | |||
272 | // Fix product feeds |
||
273 | if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) { |
||
274 | $q->is_comment_feed = false; |
||
275 | } |
||
276 | |||
277 | // Special check for shops with the product archive on front |
||
278 | if ( $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) { |
||
279 | // This is a front-page shop |
||
280 | $q->set( 'post_type', 'product' ); |
||
281 | $q->set( 'page_id', '' ); |
||
282 | |||
283 | if ( isset( $q->query['paged'] ) ) { |
||
284 | $q->set( 'paged', $q->query['paged'] ); |
||
285 | } |
||
286 | |||
287 | // Define a variable so we know this is the front page shop later on |
||
288 | define( 'SHOP_IS_ON_FRONT', true ); |
||
289 | |||
290 | // Get the actual WP page to avoid errors and let us use is_front_page() |
||
291 | // This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096 |
||
292 | global $wp_post_types; |
||
293 | |||
294 | $shop_page = get_post( wc_get_page_id( 'shop' ) ); |
||
295 | |||
296 | $wp_post_types['product']->ID = $shop_page->ID; |
||
297 | $wp_post_types['product']->post_title = $shop_page->post_title; |
||
298 | $wp_post_types['product']->post_name = $shop_page->post_name; |
||
299 | $wp_post_types['product']->post_type = $shop_page->post_type; |
||
300 | $wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type ); |
||
301 | |||
302 | // Fix conditional Functions like is_front_page |
||
303 | $q->is_singular = false; |
||
304 | $q->is_post_type_archive = true; |
||
305 | $q->is_archive = true; |
||
306 | $q->is_page = true; |
||
307 | |||
308 | // Remove post type archive name from front page title tag |
||
309 | add_filter( 'post_type_archive_title', '__return_empty_string', 5 ); |
||
310 | |||
311 | // Fix WP SEO |
||
312 | if ( class_exists( 'WPSEO_Meta' ) ) { |
||
313 | add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) ); |
||
314 | add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) ); |
||
315 | } |
||
316 | |||
317 | // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies |
||
318 | } elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) { |
||
319 | return; |
||
320 | } |
||
321 | |||
322 | $this->product_query( $q ); |
||
323 | |||
324 | if ( is_search() ) { |
||
325 | add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) ); |
||
326 | add_filter( 'wp', array( $this, 'remove_posts_where' ) ); |
||
327 | } |
||
328 | |||
329 | // And remove the pre_get_posts hook |
||
330 | $this->remove_product_query(); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * Search post excerpt. |
||
335 | * |
||
336 | * @access public |
||
337 | * @param string $where (default: '') |
||
338 | * @return string (modified where clause) |
||
339 | */ |
||
340 | public function search_post_excerpt( $where = '' ) { |
||
353 | |||
354 | /** |
||
355 | * WP SEO meta description. |
||
356 | * |
||
357 | * Hooked into wpseo_ hook already, so no need for function_exist. |
||
358 | * |
||
359 | * @access public |
||
360 | * @return string |
||
361 | */ |
||
362 | public function wpseo_metadesc() { |
||
365 | |||
366 | /** |
||
367 | * WP SEO meta key. |
||
368 | * |
||
369 | * Hooked into wpseo_ hook already, so no need for function_exist. |
||
370 | * |
||
371 | * @access public |
||
372 | * @return string |
||
373 | */ |
||
374 | public function wpseo_metakey() { |
||
377 | |||
378 | /** |
||
379 | * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop. |
||
380 | * |
||
381 | * @param mixed $q |
||
382 | */ |
||
383 | public function product_query( $q ) { |
||
401 | |||
402 | |||
403 | /** |
||
404 | * Remove the query. |
||
405 | */ |
||
406 | public function remove_product_query() { |
||
409 | |||
410 | /** |
||
411 | * Remove ordering queries. |
||
412 | */ |
||
413 | public function remove_ordering_args() { |
||
417 | |||
418 | /** |
||
419 | * Remove the posts_where filter. |
||
420 | */ |
||
421 | public function remove_posts_where() { |
||
424 | |||
425 | /** |
||
426 | * Returns an array of arguments for ordering products based on the selected values. |
||
427 | * |
||
428 | * @access public |
||
429 | * @return array |
||
430 | */ |
||
431 | public function get_catalog_ordering_args( $orderby = '', $order = '' ) { |
||
432 | // Get ordering from query string unless defined |
||
433 | if ( ! $orderby ) { |
||
434 | $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) ); |
||
435 | |||
436 | // Get order + orderby args from string |
||
437 | $orderby_value = explode( '-', $orderby_value ); |
||
438 | $orderby = esc_attr( $orderby_value[0] ); |
||
439 | $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order; |
||
440 | } |
||
441 | |||
442 | $orderby = strtolower( $orderby ); |
||
443 | $order = strtoupper( $order ); |
||
444 | $args = array(); |
||
445 | |||
446 | // default - menu_order |
||
447 | $args['orderby'] = 'menu_order title'; |
||
448 | $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC'; |
||
449 | $args['meta_key'] = ''; |
||
450 | |||
451 | switch ( $orderby ) { |
||
452 | case 'rand' : |
||
453 | $args['orderby'] = 'rand'; |
||
454 | break; |
||
455 | case 'date' : |
||
456 | $args['orderby'] = 'date ID'; |
||
457 | $args['order'] = $order == 'ASC' ? 'ASC' : 'DESC'; |
||
458 | break; |
||
459 | case 'price' : |
||
460 | $args['orderby'] = "meta_value_num ID"; |
||
461 | $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC'; |
||
462 | $args['meta_key'] = '_price'; |
||
463 | break; |
||
464 | case 'popularity' : |
||
465 | $args['meta_key'] = 'total_sales'; |
||
466 | |||
467 | // Sorting handled later though a hook |
||
468 | add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) ); |
||
469 | break; |
||
470 | case 'rating' : |
||
471 | // Sorting handled later though a hook |
||
472 | add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) ); |
||
473 | break; |
||
474 | case 'title' : |
||
475 | $args['orderby'] = 'title'; |
||
476 | $args['order'] = $order == 'DESC' ? 'DESC' : 'ASC'; |
||
477 | break; |
||
478 | } |
||
479 | |||
480 | return apply_filters( 'woocommerce_get_catalog_ordering_args', $args ); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * WP Core doens't let us change the sort direction for invidual orderby params - https://core.trac.wordpress.org/ticket/17065. |
||
485 | * |
||
486 | * This lets us sort by meta value desc, and have a second orderby param. |
||
487 | * |
||
488 | * @access public |
||
489 | * @param array $args |
||
490 | * @return array |
||
491 | */ |
||
492 | public function order_by_popularity_post_clauses( $args ) { |
||
497 | |||
498 | /** |
||
499 | * Order by rating post clauses. |
||
500 | * |
||
501 | * @access public |
||
502 | * @param array $args |
||
503 | * @return array |
||
504 | */ |
||
505 | public function order_by_rating_post_clauses( $args ) { |
||
519 | |||
520 | /** |
||
521 | * Appends meta queries to an array. |
||
522 | * |
||
523 | * @param array $meta_query |
||
524 | * @return array |
||
525 | */ |
||
526 | public function get_meta_query( $meta_query = array() ) { |
||
527 | if ( ! is_array( $meta_query ) ) { |
||
528 | $meta_query = array(); |
||
529 | } |
||
530 | |||
531 | $meta_query['visibility'] = $this->visibility_meta_query(); |
||
532 | $meta_query['stock_status'] = $this->stock_status_meta_query(); |
||
533 | $meta_query['price_filter'] = $this->price_filter_meta_query(); |
||
534 | $meta_query['rating_filter'] = $this->rating_filter_meta_query(); |
||
535 | |||
536 | return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) ); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Return a meta query for filtering by price. |
||
541 | * @return array |
||
542 | */ |
||
543 | private function price_filter_meta_query() { |
||
576 | |||
577 | /** |
||
578 | * Return a meta query for filtering by rating. |
||
579 | * @return array |
||
580 | */ |
||
581 | public function rating_filter_meta_query() { |
||
590 | |||
591 | /** |
||
592 | * Returns a meta query to handle product visibility. |
||
593 | * @param string $compare (default: 'IN') |
||
594 | * @return array |
||
595 | */ |
||
596 | public function visibility_meta_query( $compare = 'IN' ) { |
||
603 | |||
604 | /** |
||
605 | * Returns a meta query to handle product stock status. |
||
606 | * |
||
607 | * @access public |
||
608 | * @param string $status (default: 'instock') |
||
609 | * @return array |
||
610 | */ |
||
611 | public function stock_status_meta_query( $status = 'instock' ) { |
||
618 | |||
619 | /** |
||
620 | * Appends tax queries to an array. |
||
621 | * @param array $tax_query |
||
622 | * @return array |
||
623 | */ |
||
624 | public function get_tax_query( $tax_query = array() ) { |
||
644 | |||
645 | /** |
||
646 | * Get the tax query which was used by the main query. |
||
647 | * @return array |
||
648 | */ |
||
649 | public static function get_main_tax_query() { |
||
681 | |||
682 | /** |
||
683 | * Get the meta query which was used by the main query. |
||
684 | * @return array |
||
685 | */ |
||
686 | public static function get_main_meta_query() { |
||
694 | |||
695 | /** |
||
696 | * Based on WP_Query::parse_search |
||
697 | */ |
||
698 | public static function get_main_search_query_sql() { |
||
699 | global $wp_the_query, $wpdb; |
||
700 | |||
701 | $args = $wp_the_query->query_vars; |
||
702 | $search_terms = isset( $args['search_terms'] ) ? $args['search_terms'] : array(); |
||
703 | $sql = array(); |
||
704 | |||
705 | foreach ( $search_terms as $term ) { |
||
706 | // Terms prefixed with '-' should be excluded. |
||
707 | $include = '-' !== substr( $term, 0, 1 ); |
||
708 | |||
709 | if ( $include ) { |
||
710 | $like_op = 'LIKE'; |
||
711 | $andor_op = 'OR'; |
||
712 | } else { |
||
713 | $like_op = 'NOT LIKE'; |
||
714 | $andor_op = 'AND'; |
||
715 | $term = substr( $term, 1 ); |
||
716 | } |
||
717 | |||
718 | $like = '%' . $wpdb->esc_like( $term ) . '%'; |
||
719 | $sql[] = $wpdb->prepare( "(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like ); |
||
720 | } |
||
721 | |||
722 | if ( ! empty( $sql ) && ! is_user_logged_in() ) { |
||
723 | $sql[] = "($wpdb->posts.post_password = '')"; |
||
724 | } |
||
725 | |||
726 | return implode( ' AND ', $sql ); |
||
727 | } |
||
728 | |||
729 | /** |
||
730 | * Layered Nav Init. |
||
731 | */ |
||
732 | public static function get_layered_nav_chosen_attributes() { |
||
754 | |||
755 | /** |
||
756 | * @deprecated 2.6.0 |
||
757 | */ |
||
758 | public function layered_nav_init() { |
||
761 | |||
762 | /** |
||
763 | * Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients. |
||
764 | * @deprecated 2.6.0 due to performance concerns |
||
765 | */ |
||
766 | public function get_products_in_view() { |
||
769 | |||
770 | /** |
||
771 | * Layered Nav post filter. |
||
772 | * @deprecated 2.6.0 due to performance concerns |
||
773 | */ |
||
774 | public function layered_nav_query( $filtered_posts ) { |
||
777 | } |
||
778 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.