woocommerce/includes/class-wc-product-factory.php

97 lines
2.6 KiB
PHP
Raw Normal View History

<?php
/**
* Product Factory Class
*
* The WooCommerce product factory creating the right product object
*
* @class WC_Product_Factory
2014-11-25 22:50:53 +00:00
* @version 2.3.0
* @package WooCommerce/Classes
2013-02-20 17:14:46 +00:00
* @category Class
* @author WooThemes
*/
class WC_Product_Factory {
/**
* get_product function.
*
* @param bool $the_product (default: false)
* @param array $args (default: array())
2014-11-25 22:50:53 +00:00
* @return WC_Product|bool false if the product cannot be loaded
*/
2012-11-26 13:37:24 +00:00
public function get_product( $the_product = false, $args = array() ) {
2014-11-25 22:50:53 +00:00
$the_product = $this->get_product_object( $the_product );
2012-11-27 16:22:47 +00:00
if ( ! $the_product ) {
2012-11-30 15:11:39 +00:00
return false;
}
2012-11-30 15:11:39 +00:00
2014-11-25 22:50:53 +00:00
$classname = $this->get_product_class( $the_product );
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
2014-11-25 22:50:53 +00:00
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' );
2014-11-25 22:50:53 +00:00
$product_type = ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
}
2014-11-25 22:50:53 +00:00
} elseif( 'product_variation' === $post_type ) {
$product_type = 'variation';
} else {
$product_type = false;
}
2012-11-27 16:22:47 +00:00
2014-11-25 22:50:53 +00:00
$classname = $this->get_classname_from_product_type( $product_type );
// Filter classname so that the class can be overridden if extended.
2014-11-25 22:50:53 +00:00
return apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id );
}
2012-11-27 16:22:47 +00:00
2014-11-25 22:50:53 +00:00
/**
* Get the product object
* @param mixed $the_product
2015-01-15 14:50:37 +00:00
* @uses WP_POST
2014-11-25 22:50:53 +00:00
* @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;
}
2014-11-25 22:50:53 +00:00
return $the_product;
}
}