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

110 lines
3.0 KiB
PHP
Raw Normal View History

<?php
2015-11-06 09:22:19 +00:00
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
2015-11-03 13:53:50 +00:00
* Product Factory Class
*
2015-11-03 13:31:20 +00:00
* 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 a product.
*
* @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() ) {
try {
$the_product = $this->get_product_object( $the_product );
2012-11-27 16:22:47 +00:00
if ( ! $the_product ) {
throw new Exception( 'Product object does not exist', 422 );
}
2012-11-30 15:11:39 +00:00
$classname = $this->get_product_class( $the_product, $args );
2014-11-25 22:50:53 +00:00
if ( ! $classname ) {
throw new Exception( 'Missing classname', 422 );
}
if ( ! class_exists( $classname ) ) {
$classname = 'WC_Product_Simple';
}
return new $classname( $the_product, $args );
} 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
* @return string|false
2014-11-25 22:50:53 +00:00
*/
private function get_classname_from_product_type( $product_type ) {
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
* @param array $args (default: array())
2014-11-25 22:50:53 +00:00
* @return string
*/
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 ) {
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';
}
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
/**
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;
}
2015-02-26 11:59:45 +00:00
return apply_filters( 'woocommerce_product_object', $the_product );
}
}