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 |
||
21 | class WC_Query { |
||
22 | |||
23 | /** @public array Query vars to add to wp */ |
||
24 | public $query_vars = array(); |
||
25 | |||
26 | /** @public array Unfiltered product ids (before layered nav etc) */ |
||
27 | public $unfiltered_product_ids = array(); |
||
28 | |||
29 | /** @public array Filtered product ids (after layered nav) */ |
||
30 | public $filtered_product_ids = array(); |
||
31 | |||
32 | /** @public array Filtered product ids (after layered nav, per taxonomy) */ |
||
33 | public $filtered_product_ids_for_taxonomy = array(); |
||
34 | |||
35 | /** @public array Product IDs that match the layered nav + price filter */ |
||
36 | public $post__in = array(); |
||
37 | |||
38 | /** @public array|string The meta query for the page */ |
||
39 | public $meta_query = ''; |
||
40 | |||
41 | /** @public array Post IDs matching layered nav only */ |
||
42 | public $layered_nav_post__in = array(); |
||
43 | |||
44 | /** @public array Stores post IDs matching layered nav, so price filter can find max price in view */ |
||
45 | public $layered_nav_product_ids = array(); |
||
46 | |||
47 | /** |
||
48 | * Constructor for the query class. Hooks in methods. |
||
49 | * |
||
50 | * @access public |
||
51 | */ |
||
52 | public function __construct() { |
||
53 | add_action( 'init', array( $this, 'add_endpoints' ) ); |
||
54 | add_action( 'init', array( $this, 'layered_nav_init' ) ); |
||
55 | add_action( 'init', array( $this, 'price_filter_init' ) ); |
||
56 | |||
57 | if ( ! is_admin() ) { |
||
58 | add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 ); |
||
59 | add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 ); |
||
60 | add_action( 'parse_request', array( $this, 'parse_request'), 0 ); |
||
61 | add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) ); |
||
62 | add_filter( 'the_posts', array( $this, 'the_posts' ), 11, 2 ); |
||
63 | add_action( 'wp', array( $this, 'remove_product_query' ) ); |
||
64 | add_action( 'wp', array( $this, 'remove_ordering_args' ) ); |
||
65 | } |
||
66 | |||
67 | $this->init_query_vars(); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Get any errors from querystring. |
||
72 | */ |
||
73 | public function get_errors() { |
||
78 | |||
79 | /** |
||
80 | * Init query vars by loading options. |
||
81 | */ |
||
82 | public function init_query_vars() { |
||
83 | // Query vars to add to WP |
||
84 | $this->query_vars = array( |
||
85 | // Checkout actions |
||
86 | 'order-pay' => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ), |
||
87 | 'order-received' => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ), |
||
88 | |||
89 | // My account actions |
||
90 | 'view-order' => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ), |
||
91 | 'edit-account' => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ), |
||
92 | 'edit-address' => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ), |
||
93 | 'lost-password' => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ), |
||
94 | 'customer-logout' => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ), |
||
95 | 'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ), |
||
96 | ); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Get page title for an endpoint. |
||
101 | * @param string |
||
102 | * @return string |
||
103 | */ |
||
104 | public function get_endpoint_title( $endpoint ) { |
||
105 | global $wp; |
||
106 | |||
107 | switch ( $endpoint ) { |
||
108 | case 'order-pay' : |
||
109 | $title = __( 'Pay for Order', 'woocommerce' ); |
||
110 | break; |
||
111 | case 'order-received' : |
||
112 | $title = __( 'Order Received', 'woocommerce' ); |
||
113 | break; |
||
114 | case 'view-order' : |
||
115 | $order = wc_get_order( $wp->query_vars['view-order'] ); |
||
116 | $title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : ''; |
||
117 | break; |
||
118 | case 'edit-account' : |
||
119 | $title = __( 'Edit Account Details', 'woocommerce' ); |
||
120 | break; |
||
121 | case 'edit-address' : |
||
122 | $title = __( 'Edit Address', 'woocommerce' ); |
||
123 | break; |
||
124 | case 'add-payment-method' : |
||
125 | $title = __( 'Add Payment Method', 'woocommerce' ); |
||
126 | break; |
||
127 | case 'lost-password' : |
||
128 | $title = __( 'Lost Password', 'woocommerce' ); |
||
129 | break; |
||
130 | default : |
||
131 | $title = ''; |
||
132 | break; |
||
133 | } |
||
134 | return $title; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Add endpoints for query vars. |
||
139 | */ |
||
140 | public function add_endpoints() { |
||
141 | foreach ( $this->query_vars as $key => $var ) { |
||
142 | add_rewrite_endpoint( $var, EP_ROOT | EP_PAGES ); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Add query vars. |
||
148 | * |
||
149 | * @access public |
||
150 | * @param array $vars |
||
151 | * @return array |
||
152 | */ |
||
153 | public function add_query_vars( $vars ) { |
||
154 | foreach ( $this->query_vars as $key => $var ) { |
||
155 | $vars[] = $key; |
||
156 | } |
||
157 | |||
158 | return $vars; |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get query vars. |
||
163 | * |
||
164 | * @return array |
||
165 | */ |
||
166 | public function get_query_vars() { |
||
169 | |||
170 | /** |
||
171 | * Get query current active query var. |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | public function get_current_endpoint() { |
||
176 | global $wp; |
||
177 | foreach ( $this->get_query_vars() as $key => $value ) { |
||
178 | if ( isset( $wp->query_vars[ $key ] ) ) { |
||
179 | return $key; |
||
180 | } |
||
181 | } |
||
182 | return ''; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * Parse the request and look for query vars - endpoints may not be supported. |
||
187 | */ |
||
188 | public function parse_request() { |
||
202 | |||
203 | /** |
||
204 | * Hook into pre_get_posts to do the main product query. |
||
205 | * |
||
206 | * @param mixed $q query object |
||
207 | */ |
||
208 | public function pre_get_posts( $q ) { |
||
209 | // We only want to affect the main query |
||
210 | if ( ! $q->is_main_query() ) { |
||
211 | return; |
||
212 | } |
||
213 | |||
214 | // Fix for verbose page rules |
||
215 | if ( $GLOBALS['wp_rewrite']->use_verbose_page_rules && isset( $q->queried_object->ID ) && $q->queried_object->ID === wc_get_page_id( 'shop' ) ) { |
||
216 | $q->set( 'post_type', 'product' ); |
||
217 | $q->set( 'page', '' ); |
||
218 | $q->set( 'pagename', '' ); |
||
219 | |||
220 | // Fix conditional Functions |
||
221 | $q->is_archive = true; |
||
222 | $q->is_post_type_archive = true; |
||
223 | $q->is_singular = false; |
||
224 | $q->is_page = false; |
||
225 | } |
||
226 | |||
227 | // Fix for endpoints on the homepage |
||
228 | if ( $q->is_home() && 'page' === get_option( 'show_on_front' ) && absint( get_option( 'page_on_front' ) ) !== absint( $q->get( 'page_id' ) ) ) { |
||
229 | $_query = wp_parse_args( $q->query ); |
||
230 | if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->query_vars ) ) ) { |
||
231 | $q->is_page = true; |
||
232 | $q->is_home = false; |
||
233 | $q->is_singular = true; |
||
234 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
||
235 | add_filter( 'redirect_canonical', '__return_false' ); |
||
236 | } |
||
237 | } |
||
238 | |||
239 | // When orderby is set, WordPress shows posts. Get around that here. |
||
240 | if ( $q->is_home() && 'page' === get_option( 'show_on_front' ) && absint( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) { |
||
241 | $_query = wp_parse_args( $q->query ); |
||
242 | if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) { |
||
243 | $q->is_page = true; |
||
244 | $q->is_home = false; |
||
245 | $q->set( 'page_id', (int) get_option( 'page_on_front' ) ); |
||
246 | $q->set( 'post_type', 'product' ); |
||
247 | } |
||
248 | } |
||
249 | |||
250 | // Special check for shops with the product archive on front |
||
251 | if ( $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) { |
||
252 | |||
253 | // This is a front-page shop |
||
254 | $q->set( 'post_type', 'product' ); |
||
255 | $q->set( 'page_id', '' ); |
||
256 | |||
257 | if ( isset( $q->query['paged'] ) ) { |
||
258 | $q->set( 'paged', $q->query['paged'] ); |
||
259 | } |
||
260 | |||
261 | // Define a variable so we know this is the front page shop later on |
||
262 | define( 'SHOP_IS_ON_FRONT', true ); |
||
263 | |||
264 | // Get the actual WP page to avoid errors and let us use is_front_page() |
||
265 | // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096 |
||
266 | global $wp_post_types; |
||
267 | |||
268 | $shop_page = get_post( wc_get_page_id( 'shop' ) ); |
||
269 | |||
270 | $wp_post_types['product']->ID = $shop_page->ID; |
||
271 | $wp_post_types['product']->post_title = $shop_page->post_title; |
||
272 | $wp_post_types['product']->post_name = $shop_page->post_name; |
||
273 | $wp_post_types['product']->post_type = $shop_page->post_type; |
||
274 | $wp_post_types['product']->ancestors = get_ancestors( $shop_page->ID, $shop_page->post_type ); |
||
275 | |||
276 | // Fix conditional Functions like is_front_page |
||
277 | $q->is_singular = false; |
||
278 | $q->is_post_type_archive = true; |
||
279 | $q->is_archive = true; |
||
280 | $q->is_page = true; |
||
281 | |||
282 | // Remove post type archive name from front page title tag |
||
283 | add_filter( 'post_type_archive_title', '__return_empty_string', 5 ); |
||
284 | |||
285 | // Fix WP SEO |
||
286 | if ( class_exists( 'WPSEO_Meta' ) ) { |
||
287 | add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) ); |
||
288 | add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) ); |
||
289 | } |
||
290 | |||
291 | // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies |
||
292 | } elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) { |
||
293 | return; |
||
294 | } |
||
295 | |||
296 | $this->product_query( $q ); |
||
297 | |||
298 | if ( is_search() ) { |
||
299 | add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) ); |
||
300 | add_filter( 'wp', array( $this, 'remove_posts_where' ) ); |
||
301 | } |
||
302 | |||
303 | // We're on a shop page so queue the woocommerce_get_products_in_view function |
||
304 | add_action( 'wp', array( $this, 'get_products_in_view' ), 2); |
||
305 | |||
306 | // And remove the pre_get_posts hook |
||
307 | $this->remove_product_query(); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Search post excerpt. |
||
312 | * |
||
313 | * @access public |
||
314 | * @param string $where (default: '') |
||
315 | * @return string (modified where clause) |
||
316 | */ |
||
317 | public function search_post_excerpt( $where = '' ) { |
||
318 | global $wp_the_query; |
||
319 | |||
320 | // If this is not a WC Query, do not modify the query |
||
321 | if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) ) |
||
322 | return $where; |
||
323 | |||
324 | $where = preg_replace( |
||
325 | "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/", |
||
326 | "post_title LIKE $1) OR (post_excerpt LIKE $1", $where ); |
||
327 | |||
328 | return $where; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * WP SEO meta description. |
||
333 | * |
||
334 | * Hooked into wpseo_ hook already, so no need for function_exist. |
||
335 | * |
||
336 | * @access public |
||
337 | * @return string |
||
338 | */ |
||
339 | public function wpseo_metadesc() { |
||
342 | |||
343 | /** |
||
344 | * WP SEO meta key. |
||
345 | * |
||
346 | * Hooked into wpseo_ hook already, so no need for function_exist. |
||
347 | * |
||
348 | * @access public |
||
349 | * @return string |
||
350 | */ |
||
351 | public function wpseo_metakey() { |
||
354 | |||
355 | /** |
||
356 | * Hook into the_posts to do the main product query if needed - relevanssi compatibility. |
||
357 | * |
||
358 | * @access public |
||
359 | * @param array $posts |
||
360 | * @param WP_Query|bool $query (default: false) |
||
361 | * @return array |
||
362 | */ |
||
363 | public function the_posts( $posts, $query = false ) { |
||
364 | // Abort if there's no query |
||
365 | if ( ! $query ) |
||
366 | return $posts; |
||
367 | |||
368 | // Abort if we're not filtering posts |
||
369 | if ( empty( $this->post__in ) ) |
||
370 | return $posts; |
||
371 | |||
372 | // Abort if this query has already been done |
||
373 | if ( ! empty( $query->wc_query ) ) |
||
374 | return $posts; |
||
375 | |||
376 | // Abort if this isn't a search query |
||
377 | if ( empty( $query->query_vars["s"] ) ) |
||
378 | return $posts; |
||
379 | |||
380 | // Abort if we're not on a post type archive/product taxonomy |
||
381 | if ( ! $query->is_post_type_archive( 'product' ) && ! $query->is_tax( get_object_taxonomies( 'product' ) ) ) |
||
382 | return $posts; |
||
383 | |||
384 | $filtered_posts = array(); |
||
385 | $queried_post_ids = array(); |
||
386 | |||
387 | foreach ( $posts as $post ) { |
||
388 | if ( in_array( $post->ID, $this->post__in ) ) { |
||
389 | $filtered_posts[] = $post; |
||
390 | $queried_post_ids[] = $post->ID; |
||
391 | } |
||
392 | } |
||
393 | |||
394 | $query->posts = $filtered_posts; |
||
395 | $query->post_count = count( $filtered_posts ); |
||
396 | |||
397 | // Ensure filters are set |
||
398 | $this->unfiltered_product_ids = $queried_post_ids; |
||
399 | $this->filtered_product_ids = $queried_post_ids; |
||
400 | |||
401 | View Code Duplication | if ( sizeof( $this->layered_nav_post__in ) > 0 ) { |
|
402 | $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in ); |
||
403 | } else { |
||
404 | $this->layered_nav_product_ids = $this->unfiltered_product_ids; |
||
405 | } |
||
406 | |||
407 | return $filtered_posts; |
||
408 | } |
||
409 | |||
410 | |||
411 | /** |
||
412 | * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop. |
||
413 | * |
||
414 | * @param mixed $q |
||
415 | */ |
||
416 | public function product_query( $q ) { |
||
417 | |||
418 | // Meta query |
||
419 | $meta_query = $this->get_meta_query( $q->get( 'meta_query' ) ); |
||
420 | |||
421 | // Ordering |
||
422 | $ordering = $this->get_catalog_ordering_args(); |
||
423 | |||
424 | // Get a list of post id's which match the current filters set (in the layered nav and price filter) |
||
425 | $post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) ); |
||
426 | |||
427 | // Ordering query vars |
||
428 | $q->set( 'orderby', $ordering['orderby'] ); |
||
429 | $q->set( 'order', $ordering['order'] ); |
||
430 | if ( isset( $ordering['meta_key'] ) ) { |
||
431 | $q->set( 'meta_key', $ordering['meta_key'] ); |
||
432 | } |
||
433 | |||
434 | // Query vars that affect posts shown |
||
435 | $q->set( 'meta_query', $meta_query ); |
||
436 | $q->set( 'post__in', $post__in ); |
||
437 | $q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', get_option( 'posts_per_page' ) ) ); |
||
438 | |||
439 | // Set a special variable |
||
440 | $q->set( 'wc_query', 'product_query' ); |
||
441 | |||
442 | // Store variables |
||
443 | $this->post__in = $post__in; |
||
444 | $this->meta_query = $meta_query; |
||
445 | |||
446 | do_action( 'woocommerce_product_query', $q, $this ); |
||
447 | } |
||
448 | |||
449 | |||
450 | /** |
||
451 | * Remove the query. |
||
452 | */ |
||
453 | public function remove_product_query() { |
||
456 | |||
457 | /** |
||
458 | * Remove ordering queries. |
||
459 | */ |
||
460 | public function remove_ordering_args() { |
||
464 | |||
465 | /** |
||
466 | * Remove the posts_where filter. |
||
467 | */ |
||
468 | public function remove_posts_where() { |
||
471 | |||
472 | |||
473 | /** |
||
474 | * Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients. |
||
475 | */ |
||
476 | public function get_products_in_view() { |
||
477 | global $wp_the_query; |
||
478 | |||
479 | // Get main query |
||
480 | $current_wp_query = $wp_the_query->query; |
||
481 | |||
530 | |||
531 | |||
532 | /** |
||
533 | * Returns an array of arguments for ordering products based on the selected values. |
||
534 | * |
||
535 | * @access public |
||
536 | * @return array |
||
537 | */ |
||
538 | public function get_catalog_ordering_args( $orderby = '', $order = '' ) { |
||
591 | |||
592 | /** |
||
593 | * WP Core doens't let us change the sort direction for invidual orderby params - http://core.trac.wordpress.org/ticket/17065. |
||
594 | * |
||
595 | * This lets us sort by meta value desc, and have a second orderby param. |
||
596 | * |
||
597 | * @access public |
||
598 | * @param array $args |
||
599 | * @return array |
||
600 | */ |
||
601 | public function order_by_popularity_post_clauses( $args ) { |
||
608 | |||
609 | /** |
||
610 | * Order by rating post clauses. |
||
611 | * |
||
612 | * @access public |
||
613 | * @param array $args |
||
614 | * @return array |
||
615 | */ |
||
616 | public function order_by_rating_post_clauses( $args ) { |
||
634 | |||
635 | /** |
||
636 | * Appends meta queries to an array. |
||
637 | * @access public |
||
638 | * @param array $meta_query |
||
639 | * @return array |
||
640 | */ |
||
641 | public function get_meta_query( $meta_query = array() ) { |
||
650 | |||
651 | /** |
||
652 | * Returns a meta query to handle product visibility. |
||
653 | * |
||
654 | * @access public |
||
655 | * @param string $compare (default: 'IN') |
||
656 | * @return array |
||
657 | */ |
||
658 | public function visibility_meta_query( $compare = 'IN' ) { |
||
672 | |||
673 | /** |
||
674 | * Returns a meta query to handle product stock status. |
||
675 | * |
||
676 | * @access public |
||
677 | * @param string $status (default: 'instock') |
||
678 | * @return array |
||
679 | */ |
||
680 | public function stock_status_meta_query( $status = 'instock' ) { |
||
691 | |||
692 | /** |
||
693 | * Layered Nav Init. |
||
694 | */ |
||
695 | public function layered_nav_init( ) { |
||
724 | |||
725 | /** |
||
726 | * Layered Nav post filter. |
||
727 | * |
||
728 | * @param array $filtered_posts |
||
729 | * @return array |
||
730 | */ |
||
731 | public function layered_nav_query( $filtered_posts ) { |
||
816 | |||
817 | /** |
||
818 | * Price filter Init. |
||
819 | */ |
||
820 | public function price_filter_init() { |
||
838 | |||
839 | /** |
||
840 | * Price Filter post filter. |
||
841 | * |
||
842 | * @param array $filtered_posts |
||
843 | * @return array |
||
844 | */ |
||
845 | public function price_filter( $filtered_posts = array() ) { |
||
921 | |||
922 | } |
||
923 | |||
927 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.