From 206496b584977271065cb1e199283e725241b25a Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 25 Nov 2014 22:50:53 +0000 Subject: [PATCH] Split up WC_Product_Factory methods --- includes/class-wc-product-factory.php | 101 ++++++++++++++++---------- 1 file changed, 64 insertions(+), 37 deletions(-) diff --git a/includes/class-wc-product-factory.php b/includes/class-wc-product-factory.php index b58b6bfb4b8..b36f86a3009 100644 --- a/includes/class-wc-product-factory.php +++ b/includes/class-wc-product-factory.php @@ -5,7 +5,7 @@ * The WooCommerce product factory creating the right product object * * @class WC_Product_Factory - * @version 2.0.0 + * @version 2.3.0 * @package WooCommerce/Classes * @category Class * @author WooThemes @@ -15,54 +15,81 @@ class WC_Product_Factory { /** * get_product function. * - * @access public * @param bool $the_product (default: false) * @param array $args (default: array()) - * @return WC_Product + * @return WC_Product|bool false if the product cannot be loaded */ public function get_product( $the_product = false, $args = array() ) { - global $post; - - if ( false === $the_product ) { - $the_product = $post; - } elseif ( is_numeric( $the_product ) ) { - $the_product = get_post( $the_product ); - } elseif ( $the_product instanceof WC_Product ) { - return $the_product; - } + $the_product = $this->get_product_object( $the_product ); if ( ! $the_product ) { return false; } - if ( is_object( $the_product ) ) { - $product_id = absint( $the_product->ID ); - $post_type = $the_product->post_type; - } + $classname = $this->get_product_class( $the_product ); - if ( in_array( $post_type, array( 'product', 'product_variation' ) ) ) { - if ( isset( $args['product_type'] ) ) { - $product_type = $args['product_type']; - } elseif ( 'product_variation' == $post_type ) { - $product_type = 'variation'; - } else { - $terms = get_the_terms( $product_id, 'product_type' ); - $product_type = ! empty( $terms ) && isset( current( $terms )->name ) ? sanitize_title( current( $terms )->name ) : 'simple'; - } - - // Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class - $classname = 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) ); - } else { - $classname = false; - $product_type = false; - } - - // Filter classname so that the class can be overridden if extended. - $classname = apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id ); - - if ( ! class_exists( $classname ) ) + if ( ! class_exists( $classname ) ) { $classname = 'WC_Product_Simple'; + } return new $classname( $the_product, $args ); } + + /** + * Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class + * @param string $product_type + * @return string|bool + */ + private function get_classname_from_product_type( $product_type ) { + return $product_type ? 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) ) : false; + } + + /** + * Get the product class name + * @param WP_Post $the_product + * @return string + */ + private function get_product_class( $the_product ) { + $product_id = absint( $the_product->ID ); + $post_type = $the_product->post_type; + + if ( 'product' === $post_type ) { + if ( isset( $args['product_type'] ) ) { + $product_type = $args['product_type']; + } else { + $terms = get_the_terms( $product_id, 'product_type' ); + $product_type = ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; + } + } elseif( 'product_variation' === $post_type ) { + $product_type = 'variation'; + } else { + $product_type = false; + } + + $classname = $this->get_classname_from_product_type( $product_type ); + + // Filter classname so that the class can be overridden if extended. + return apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id ); + } + + /** + * Get the product object + * @param mixed $the_product + * @return WP_Post|bool false on failure + */ + private function get_product_object( $the_product ) { + if ( false === $the_product ) { + $the_product = $GLOBALS['post']; + } elseif ( is_numeric( $the_product ) ) { + $the_product = get_post( $the_product ); + } elseif ( $the_product instanceof WC_Product ) { + $the_product = get_post( $the_product->id ); + } elseif ( $the_product instanceof WP_Post ) { + $the_product = $the_product; + } else { + $the_product = false; + } + + return $the_product; + } }