2012-11-22 11:37:41 +00:00
|
|
|
<?php
|
2015-11-06 09:22:19 +00:00
|
|
|
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
|
|
|
|
2012-11-22 11:37:41 +00:00
|
|
|
/**
|
2015-11-03 13:53:50 +00:00
|
|
|
* Product Factory Class
|
2012-11-22 11:37:41 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* The WooCommerce product factory creating the right product object.
|
2012-11-22 11:37:41 +00:00
|
|
|
*
|
|
|
|
* @class WC_Product_Factory
|
2014-11-25 22:50:53 +00:00
|
|
|
* @version 2.3.0
|
2012-11-22 11:37:41 +00:00
|
|
|
* @package WooCommerce/Classes
|
2013-02-20 17:14:46 +00:00
|
|
|
* @category Class
|
2012-11-22 11:37:41 +00:00
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class WC_Product_Factory {
|
|
|
|
|
2012-11-29 16:48:40 +00:00
|
|
|
/**
|
2016-05-13 18:06:16 +00:00
|
|
|
* Get a product.
|
2012-11-29 16:48:40 +00:00
|
|
|
*
|
|
|
|
* @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-29 16:48:40 +00:00
|
|
|
*/
|
2012-11-26 13:37:24 +00:00
|
|
|
public function get_product( $the_product = false, $args = array() ) {
|
2016-05-13 18:06:16 +00:00
|
|
|
try {
|
|
|
|
$the_product = $this->get_product_object( $the_product );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2016-05-13 18:06:16 +00:00
|
|
|
if ( ! $the_product ) {
|
|
|
|
throw new Exception( 'Product object does not exist', 422 );
|
|
|
|
}
|
2012-11-30 15:11:39 +00:00
|
|
|
|
2016-05-13 18:06:16 +00:00
|
|
|
$classname = $this->get_product_class( $the_product, $args );
|
2014-11-25 22:50:53 +00:00
|
|
|
|
2016-05-13 18:06:16 +00:00
|
|
|
if ( ! $classname ) {
|
|
|
|
throw new Exception( 'Missing classname', 422 );
|
|
|
|
}
|
2016-04-01 00:51:01 +00:00
|
|
|
|
2016-05-13 18:06:16 +00:00
|
|
|
if ( ! class_exists( $classname ) ) {
|
|
|
|
$classname = 'WC_Product_Simple';
|
|
|
|
}
|
|
|
|
|
|
|
|
return new $classname( $the_product, $args );
|
2012-11-22 12:56:52 +00:00
|
|
|
|
2016-05-13 18:06:16 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-25 22:50:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class.
|
2014-11-25 22:50:53 +00:00
|
|
|
* @param string $product_type
|
2015-01-23 14:37:20 +00:00
|
|
|
* @return string|false
|
2014-11-25 22:50:53 +00:00
|
|
|
*/
|
2016-10-26 17:02:50 +00:00
|
|
|
public static function get_classname_from_product_type( $product_type ) {
|
2014-11-25 22:50:53 +00:00
|
|
|
return $product_type ? 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) ) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get the product class name.
|
2014-11-25 22:50:53 +00:00
|
|
|
* @param WP_Post $the_product
|
2015-05-18 05:14:35 +00:00
|
|
|
* @param array $args (default: array())
|
2014-11-25 22:50:53 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-05-18 05:14:35 +00:00
|
|
|
private function get_product_class( $the_product, $args = array() ) {
|
2014-11-25 22:50:53 +00:00
|
|
|
$product_id = absint( $the_product->ID );
|
|
|
|
$post_type = $the_product->post_type;
|
|
|
|
|
|
|
|
if ( 'product' === $post_type ) {
|
2013-01-10 11:46:39 +00:00
|
|
|
if ( isset( $args['product_type'] ) ) {
|
|
|
|
$product_type = $args['product_type'];
|
|
|
|
} else {
|
2016-02-09 23:26:22 +00:00
|
|
|
$terms = get_the_terms( $the_product, 'product_type' );
|
2014-11-25 22:50:53 +00:00
|
|
|
$product_type = ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple';
|
2013-01-10 11:46:39 +00:00
|
|
|
}
|
2016-08-27 04:23:02 +00:00
|
|
|
} elseif ( 'product_variation' === $post_type ) {
|
2014-11-25 22:50:53 +00:00
|
|
|
$product_type = 'variation';
|
2012-11-22 12:56:52 +00:00
|
|
|
} else {
|
2013-01-10 11:46:39 +00:00
|
|
|
$product_type = false;
|
2012-11-22 12:56:52 +00:00
|
|
|
}
|
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 );
|
|
|
|
|
2012-11-22 11:37:41 +00:00
|
|
|
// 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
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get the product object.
|
2014-11-25 22:50:53 +00:00
|
|
|
* @param mixed $the_product
|
2015-03-27 15:15:40 +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 );
|
2015-02-03 16:14:56 +00:00
|
|
|
} elseif ( ! ( $the_product instanceof WP_Post ) ) {
|
2014-11-25 22:50:53 +00:00
|
|
|
$the_product = false;
|
|
|
|
}
|
2013-01-10 11:46:39 +00:00
|
|
|
|
2015-02-26 11:59:45 +00:00
|
|
|
return apply_filters( 'woocommerce_product_object', $the_product );
|
2012-11-22 11:37:41 +00:00
|
|
|
}
|
2013-07-01 21:48:07 +00:00
|
|
|
}
|