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_Product_Variable 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_Product_Variable, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class WC_Product_Variable extends WC_Product { |
||
19 | |||
20 | /** @public array Array of child products/posts/variations. */ |
||
21 | public $children = null; |
||
22 | |||
23 | /** @private array Array of variation prices. */ |
||
24 | private $prices_array = array(); |
||
25 | |||
26 | /** |
||
27 | * Constructor. |
||
28 | * |
||
29 | * @param mixed $product |
||
30 | */ |
||
31 | public function __construct( $product ) { |
||
35 | |||
36 | /** |
||
37 | * Get the add to cart button text. |
||
38 | * |
||
39 | * @access public |
||
40 | * @return string |
||
41 | */ |
||
42 | public function add_to_cart_text() { |
||
43 | $text = $this->is_purchasable() && $this->is_in_stock() ? __( 'Select options', 'woocommerce' ) : __( 'Read more', 'woocommerce' ); |
||
44 | |||
45 | return apply_filters( 'woocommerce_product_add_to_cart_text', $text, $this ); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Set stock level of the product. |
||
50 | * |
||
51 | * @param mixed $amount (default: null) |
||
52 | * @param string $mode can be set, add, or subtract |
||
53 | * @return int Stock |
||
54 | */ |
||
55 | public function set_stock( $amount = null, $mode = 'set' ) { |
||
60 | |||
61 | /** |
||
62 | * Performed after a stock level change at product level. |
||
63 | */ |
||
64 | public function check_stock_status() { |
||
84 | |||
85 | /** |
||
86 | * Set stock status. |
||
87 | */ |
||
88 | public function set_stock_status( $status ) { |
||
95 | |||
96 | /** |
||
97 | * Return a products child ids. |
||
98 | * |
||
99 | * @param boolean $visible_only Only return variations which are not hidden |
||
100 | * @return array of children ids |
||
101 | */ |
||
102 | public function get_children( $visible_only = false ) { |
||
141 | |||
142 | /** |
||
143 | * Get child product. |
||
144 | * |
||
145 | * @access public |
||
146 | * @param mixed $child_id |
||
147 | * @return WC_Product_Variation |
||
148 | */ |
||
149 | public function get_child( $child_id ) { |
||
155 | |||
156 | /** |
||
157 | * Returns whether or not the product has any child product. |
||
158 | * |
||
159 | * @access public |
||
160 | * @return bool |
||
161 | */ |
||
162 | public function has_child() { |
||
165 | |||
166 | /** |
||
167 | * Returns whether or not the product is on sale. |
||
168 | * @return bool |
||
169 | */ |
||
170 | public function is_on_sale() { |
||
179 | |||
180 | /** |
||
181 | * Get the min or max variation regular price. |
||
182 | * @param string $min_or_max - min or max |
||
183 | * @param boolean $display Whether the value is going to be displayed |
||
184 | * @return string |
||
185 | */ |
||
186 | public function get_variation_regular_price( $min_or_max = 'min', $display = false ) { |
||
191 | |||
192 | /** |
||
193 | * Get the min or max variation sale price. |
||
194 | * @param string $min_or_max - min or max |
||
195 | * @param boolean $display Whether the value is going to be displayed |
||
196 | * @return string |
||
197 | */ |
||
198 | public function get_variation_sale_price( $min_or_max = 'min', $display = false ) { |
||
203 | |||
204 | /** |
||
205 | * Get the min or max variation (active) price. |
||
206 | * @param string $min_or_max - min or max |
||
207 | * @param boolean $display Whether the value is going to be displayed |
||
208 | * @return string |
||
209 | */ |
||
210 | public function get_variation_price( $min_or_max = 'min', $display = false ) { |
||
215 | |||
216 | /** |
||
217 | * Get an array of all sale and regular prices from all variations. This is used for example when displaying the price range at variable product level or seeing if the variable product is on sale. |
||
218 | * |
||
219 | * Can be filtered by plugins which modify costs, but otherwise will include the raw meta costs unlike get_price() which runs costs through the woocommerce_get_price filter. |
||
220 | * This is to ensure modified prices are not cached, unless intended. |
||
221 | * |
||
222 | * @param bool $display Are prices for display? If so, taxes will be calculated. |
||
223 | * @return array() Array of RAW prices, regular prices, and sale prices with keys set to variation ID. |
||
|
|||
224 | */ |
||
225 | public function get_variation_prices( $display = false ) { |
||
226 | global $wp_filter; |
||
227 | |||
228 | /** |
||
229 | * Transient name for storing prices for this product (note: Max transient length is 45) |
||
230 | * @since 2.5.0 a single transient is used per product for all prices, rather than many transients per product. |
||
231 | */ |
||
232 | $transient_name = 'wc_var_prices_' . $this->id; |
||
233 | |||
234 | /** |
||
235 | * Create unique cache key based on the tax location (affects displayed/cached prices), product version and active price filters. |
||
236 | * DEVELOPERS should filter this hash if offering conditonal pricing to keep it unique. |
||
237 | * @var string |
||
238 | */ |
||
239 | if ( $display ) { |
||
240 | $price_hash = array( get_option( 'woocommerce_tax_display_shop', 'excl' ), WC_Tax::get_rates() ); |
||
241 | } else { |
||
242 | $price_hash = array( false ); |
||
243 | } |
||
244 | |||
245 | $filter_names = array( 'woocommerce_variation_prices_price', 'woocommerce_variation_prices_regular_price', 'woocommerce_variation_prices_sale_price' ); |
||
246 | |||
247 | foreach ( $filter_names as $filter_name ) { |
||
248 | if ( ! empty( $wp_filter[ $filter_name ] ) ) { |
||
249 | $price_hash[ $filter_name ] = array(); |
||
250 | |||
251 | foreach ( $wp_filter[ $filter_name ] as $priority => $callbacks ) { |
||
252 | $price_hash[ $filter_name ][] = array_values( wp_list_pluck( $callbacks, 'function' ) ); |
||
253 | } |
||
254 | } |
||
255 | } |
||
256 | |||
257 | $price_hash[] = WC_Cache_Helper::get_transient_version( 'product' ); |
||
258 | $price_hash = md5( json_encode( apply_filters( 'woocommerce_get_variation_prices_hash', $price_hash, $this, $display ) ) ); |
||
259 | |||
260 | if ( ! empty( $this->prices_array[ $price_hash ] ) ) { |
||
261 | /** |
||
262 | * $this->prices_array is an array of values which may have been modified from what is stored in transients - this may not match $transient_cached_prices_array. |
||
263 | * If the value has already been generated, we don't need to grab the values again so just return them. They are already filtered. |
||
264 | */ |
||
265 | return $this->prices_array[ $price_hash ]; |
||
266 | |||
267 | } else { |
||
268 | /** |
||
269 | * No locally cached value? Get the data from the transient or generate it. |
||
270 | */ |
||
271 | |||
272 | // Get value of transient |
||
273 | $transient_cached_prices_array = array_filter( (array) json_decode( strval( get_transient( $transient_name ) ), true ) ); |
||
274 | |||
275 | // If the product version has changed since the transient was last saved, reset the transient cache. |
||
276 | if ( empty( $transient_cached_prices_array['version'] ) || WC_Cache_Helper::get_transient_version( 'product' ) !== $transient_cached_prices_array['version'] ) { |
||
277 | $transient_cached_prices_array = array( 'version' => WC_Cache_Helper::get_transient_version( 'product' ) ); |
||
278 | } |
||
279 | |||
280 | // If the prices are not stored for this hash, generate them and add to the transient. |
||
281 | if ( empty( $transient_cached_prices_array[ $price_hash ] ) ) { |
||
282 | $prices = array(); |
||
283 | $regular_prices = array(); |
||
284 | $sale_prices = array(); |
||
285 | $variation_ids = $this->get_children( true ); |
||
286 | |||
287 | foreach ( $variation_ids as $variation_id ) { |
||
288 | if ( $variation = $this->get_child( $variation_id ) ) { |
||
289 | $price = apply_filters( 'woocommerce_variation_prices_price', $variation->price, $variation, $this ); |
||
290 | $regular_price = apply_filters( 'woocommerce_variation_prices_regular_price', $variation->regular_price, $variation, $this ); |
||
291 | $sale_price = apply_filters( 'woocommerce_variation_prices_sale_price', $variation->sale_price, $variation, $this ); |
||
292 | |||
293 | // Skip empty prices |
||
294 | if ( '' === $price ) { |
||
295 | continue; |
||
296 | } |
||
297 | |||
298 | // If sale price does not equal price, the product is not yet on sale |
||
299 | if ( $sale_price === $regular_price || $sale_price !== $price ) { |
||
300 | $sale_price = $regular_price; |
||
301 | } |
||
302 | |||
303 | // If we are getting prices for display, we need to account for taxes |
||
304 | if ( $display ) { |
||
305 | if ( 'incl' === get_option( 'woocommerce_tax_display_shop' ) ) { |
||
306 | $price = '' === $price ? '' : $variation->get_price_including_tax( 1, $price ); |
||
307 | $regular_price = '' === $regular_price ? '' : $variation->get_price_including_tax( 1, $regular_price ); |
||
308 | $sale_price = '' === $sale_price ? '' : $variation->get_price_including_tax( 1, $sale_price ); |
||
309 | } else { |
||
310 | $price = '' === $price ? '' : $variation->get_price_excluding_tax( 1, $price ); |
||
311 | $regular_price = '' === $regular_price ? '' : $variation->get_price_excluding_tax( 1, $regular_price ); |
||
312 | $sale_price = '' === $sale_price ? '' : $variation->get_price_excluding_tax( 1, $sale_price ); |
||
313 | } |
||
314 | } |
||
315 | |||
316 | $prices[ $variation_id ] = wc_format_decimal( $price, wc_get_price_decimals() ); |
||
317 | $regular_prices[ $variation_id ] = wc_format_decimal( $regular_price, wc_get_price_decimals() ); |
||
318 | $sale_prices[ $variation_id ] = wc_format_decimal( $sale_price . '.00', wc_get_price_decimals() ); |
||
319 | } |
||
320 | } |
||
321 | |||
322 | asort( $prices ); |
||
323 | asort( $regular_prices ); |
||
324 | asort( $sale_prices ); |
||
325 | |||
326 | $transient_cached_prices_array[ $price_hash ] = array( |
||
327 | 'price' => $prices, |
||
328 | 'regular_price' => $regular_prices, |
||
329 | 'sale_price' => $sale_prices, |
||
330 | ); |
||
331 | |||
332 | set_transient( $transient_name, json_encode( $transient_cached_prices_array ), DAY_IN_SECONDS * 30 ); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * Give plugins one last chance to filter the variation prices array which has been generated and store locally to the class. |
||
337 | * This value may differ from the transient cache. It is filtered once before storing locally. |
||
338 | */ |
||
339 | return $this->prices_array[ $price_hash ] = apply_filters( 'woocommerce_variation_prices', $transient_cached_prices_array[ $price_hash ], $this, $display ); |
||
340 | } |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Returns the price in html format. |
||
345 | * |
||
346 | * @access public |
||
347 | * @param string $price (default: '') |
||
348 | * @return string |
||
349 | */ |
||
350 | public function get_price_html( $price = '' ) { |
||
351 | $prices = $this->get_variation_prices( true ); |
||
352 | |||
353 | // No variations, or no active variation prices |
||
354 | if ( $this->get_price() === '' || empty( $prices['price'] ) ) { |
||
355 | $price = apply_filters( 'woocommerce_variable_empty_price_html', '', $this ); |
||
356 | } else { |
||
357 | $min_price = current( $prices['price'] ); |
||
358 | $max_price = end( $prices['price'] ); |
||
359 | $price = $min_price !== $max_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce' ), wc_price( $min_price ), wc_price( $max_price ) ) : wc_price( $min_price ); |
||
360 | $is_free = 0 == $min_price && 0 == $max_price; |
||
361 | |||
362 | if ( $this->is_on_sale() ) { |
||
363 | $min_regular_price = current( $prices['regular_price'] ); |
||
364 | $max_regular_price = end( $prices['regular_price'] ); |
||
365 | $regular_price = $min_regular_price !== $max_regular_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce' ), wc_price( $min_regular_price ), wc_price( $max_regular_price ) ) : wc_price( $min_regular_price ); |
||
366 | $price = apply_filters( 'woocommerce_variable_sale_price_html', $this->get_price_html_from_to( $regular_price, $price ) . $this->get_price_suffix(), $this ); |
||
367 | } elseif ( $is_free ) { |
||
368 | $price = apply_filters( 'woocommerce_variable_free_price_html', __( 'Free!', 'woocommerce' ), $this ); |
||
369 | } else { |
||
370 | $price = apply_filters( 'woocommerce_variable_price_html', $price . $this->get_price_suffix(), $this ); |
||
371 | } |
||
372 | } |
||
373 | return apply_filters( 'woocommerce_get_price_html', $price, $this ); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Return an array of attributes used for variations, as well as their possible values. |
||
378 | * |
||
379 | * @return array of attributes and their available values |
||
380 | */ |
||
381 | public function get_variation_attributes() { |
||
382 | global $wpdb; |
||
383 | |||
384 | $variation_attributes = array(); |
||
385 | $attributes = $this->get_attributes(); |
||
386 | $child_ids = $this->get_children( true ); |
||
387 | |||
388 | if ( ! empty( $child_ids ) ) { |
||
389 | foreach ( $attributes as $attribute ) { |
||
390 | if ( empty( $attribute['is_variation'] ) ) { |
||
391 | continue; |
||
392 | } |
||
393 | |||
394 | // Get possible values for this attribute, for only visible variations. |
||
395 | $values = array_unique( $wpdb->get_col( $wpdb->prepare( |
||
396 | "SELECT meta_value FROM {$wpdb->postmeta} WHERE meta_key = %s AND post_id IN (" . implode( ',', array_map( 'esc_sql', $child_ids ) ) . ")", |
||
397 | wc_variation_attribute_name( $attribute['name'] ) |
||
398 | ) ) ); |
||
399 | |||
400 | // empty value indicates that all options for given attribute are available |
||
401 | if ( in_array( '', $values ) ) { |
||
402 | $values = $attribute['is_taxonomy'] ? wp_get_post_terms( $this->id, $attribute['name'], array( 'fields' => 'slugs' ) ) : wc_get_text_attributes( $attribute['value'] ); |
||
403 | } elseif ( ! $attribute['is_taxonomy'] ) { |
||
404 | // Get custom attributes (non taxonomy) as defined. |
||
405 | $text_attributes = wc_get_text_attributes( $attribute['value'] ); |
||
406 | $assigned_text_attributes = $values; |
||
407 | $values = array(); |
||
408 | |||
409 | // Pre 2.4 handling where 'slugs' were saved instead of the full text attribute |
||
410 | if ( version_compare( get_post_meta( $this->id, '_product_version', true ), '2.4.0', '<' ) ) { |
||
411 | $assigned_text_attributes = array_map( 'sanitize_title', $assigned_text_attributes ); |
||
412 | |||
413 | foreach ( $text_attributes as $text_attribute ) { |
||
414 | if ( in_array( sanitize_title( $text_attribute ), $assigned_text_attributes ) ) { |
||
415 | $values[] = $text_attribute; |
||
416 | } |
||
417 | } |
||
418 | } else { |
||
419 | foreach ( $text_attributes as $text_attribute ) { |
||
420 | if ( in_array( $text_attribute, $assigned_text_attributes ) ) { |
||
421 | $values[] = $text_attribute; |
||
422 | } |
||
423 | } |
||
424 | } |
||
425 | } |
||
426 | |||
427 | $variation_attributes[ $attribute['name'] ] = array_unique( $values ); |
||
428 | } |
||
429 | } |
||
430 | |||
431 | return $variation_attributes; |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * If set, get the default attributes for a variable product. |
||
436 | * |
||
437 | * @access public |
||
438 | * @return array |
||
439 | */ |
||
440 | public function get_variation_default_attributes() { |
||
444 | |||
445 | /** |
||
446 | * Check if variable product has default attributes set. |
||
447 | * |
||
448 | * @access public |
||
449 | * @return bool |
||
450 | */ |
||
451 | public function has_default_attributes() { |
||
457 | |||
458 | /** |
||
459 | * If set, get the default attributes for a variable product. |
||
460 | * |
||
461 | * @param string $attribute_name |
||
462 | * @return string |
||
463 | */ |
||
464 | public function get_variation_default_attribute( $attribute_name ) { |
||
469 | |||
470 | /** |
||
471 | * Match a variation to a given set of attributes using a WP_Query. |
||
472 | * @since 2.4.0 |
||
473 | * @param $match_attributes |
||
474 | * @return int Variation ID which matched, 0 is no match was found |
||
475 | */ |
||
476 | public function get_matching_variation( $match_attributes = array() ) { |
||
541 | |||
542 | /** |
||
543 | * Get an array of available variations for the current product. |
||
544 | * @return array |
||
545 | */ |
||
546 | public function get_available_variations() { |
||
567 | |||
568 | /** |
||
569 | * Returns an array of data for a variation. Used in the add to cart form. |
||
570 | * @since 2.4.0 |
||
571 | * @param WC_Product $variation Variation product object or ID |
||
572 | * @return array |
||
573 | */ |
||
574 | public function get_available_variation( $variation ) { |
||
633 | |||
634 | /** |
||
635 | * Sync variable product prices with the children lowest/highest prices. |
||
636 | */ |
||
637 | public function variable_product_sync( $product_id = '' ) { |
||
659 | |||
660 | /** |
||
661 | * Sync variable product stock status with children. |
||
662 | * @param int $product_id |
||
663 | */ |
||
664 | public static function sync_stock_status( $product_id ) { |
||
686 | |||
687 | /** |
||
688 | * Sync the variable product's attributes with the variations. |
||
689 | */ |
||
690 | public static function sync_attributes( $product_id, $children = false ) { |
||
733 | |||
734 | /** |
||
735 | * Does a child have a weight set? |
||
736 | * @since 2.7.0 |
||
737 | * @return boolean |
||
738 | */ |
||
739 | public function child_has_weight() { |
||
742 | |||
743 | /** |
||
744 | * Does a child have dimensions set? |
||
745 | * @since 2.7.0 |
||
746 | * @return boolean |
||
747 | */ |
||
748 | public function child_has_dimensions() { |
||
751 | |||
752 | /** |
||
753 | * Returns whether or not we are showing dimensions on the product page. |
||
754 | * |
||
755 | * @return bool |
||
756 | */ |
||
757 | public function enable_dimensions_display() { |
||
760 | |||
761 | /** |
||
762 | * Sync the variable product with it's children. |
||
763 | */ |
||
764 | public static function sync( $product_id ) { |
||
886 | } |
||
887 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.