1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Product Factory Class |
9
|
|
|
* |
10
|
|
|
* The WooCommerce product factory creating the right product object. |
11
|
|
|
* |
12
|
|
|
* @class WC_Product_Factory |
13
|
|
|
* @version 2.3.0 |
14
|
|
|
* @package WooCommerce/Classes |
15
|
|
|
* @category Class |
16
|
|
|
* @author WooThemes |
17
|
|
|
*/ |
18
|
|
|
class WC_Product_Factory { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get a product. |
22
|
|
|
* |
23
|
|
|
* @param bool $the_product (default: false) |
24
|
|
|
* @param array $args (default: array()) |
25
|
|
|
* @return WC_Product|bool false if the product cannot be loaded |
26
|
|
|
*/ |
27
|
|
|
public function get_product( $the_product = false, $args = array() ) { |
28
|
|
|
try { |
29
|
|
|
$the_product = $this->get_product_object( $the_product ); |
30
|
|
|
|
31
|
|
|
if ( ! $the_product ) { |
32
|
|
|
throw new Exception( 'Product object does not exist', 422 ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$classname = $this->get_product_class( $the_product, $args ); |
|
|
|
|
36
|
|
|
|
37
|
|
|
if ( ! $classname ) { |
38
|
|
|
throw new Exception( 'Missing classname', 422 ); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if ( ! class_exists( $classname ) ) { |
42
|
|
|
$classname = 'WC_Product_Simple'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return new $classname( $the_product, $args ); |
46
|
|
|
|
47
|
|
|
} catch ( Exception $e ) { |
48
|
|
|
return false; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a WC coding standards compliant class name e.g. WC_Product_Type_Class instead of WC_Product_type-class. |
54
|
|
|
* @param string $product_type |
55
|
|
|
* @return string|false |
56
|
|
|
*/ |
57
|
|
|
private function get_classname_from_product_type( $product_type ) { |
58
|
|
|
return $product_type ? 'WC_Product_' . implode( '_', array_map( 'ucfirst', explode( '-', $product_type ) ) ) : false; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the product class name. |
63
|
|
|
* @param WP_Post $the_product |
64
|
|
|
* @param array $args (default: array()) |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
|
|
private function get_product_class( $the_product, $args = array() ) { |
68
|
|
|
$product_id = absint( $the_product->ID ); |
69
|
|
|
$post_type = $the_product->post_type; |
70
|
|
|
|
71
|
|
|
if ( 'product' === $post_type ) { |
72
|
|
|
if ( isset( $args['product_type'] ) ) { |
73
|
|
|
$product_type = $args['product_type']; |
74
|
|
|
} else { |
75
|
|
|
$terms = get_the_terms( $the_product, 'product_type' ); |
76
|
|
|
$product_type = ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; |
77
|
|
|
} |
78
|
|
|
} elseif( 'product_variation' === $post_type ) { |
79
|
|
|
$product_type = 'variation'; |
80
|
|
|
} else { |
81
|
|
|
$product_type = false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$classname = $this->get_classname_from_product_type( $product_type ); |
85
|
|
|
|
86
|
|
|
// Filter classname so that the class can be overridden if extended. |
87
|
|
|
return apply_filters( 'woocommerce_product_class', $classname, $product_type, $post_type, $product_id ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the product object. |
92
|
|
|
* @param mixed $the_product |
93
|
|
|
* @uses WP_Post |
94
|
|
|
* @return WP_Post|bool false on failure |
95
|
|
|
*/ |
96
|
|
|
private function get_product_object( $the_product ) { |
97
|
|
|
if ( false === $the_product ) { |
98
|
|
|
$the_product = $GLOBALS['post']; |
99
|
|
|
} elseif ( is_numeric( $the_product ) ) { |
100
|
|
|
$the_product = get_post( $the_product ); |
101
|
|
|
} elseif ( $the_product instanceof WC_Product ) { |
102
|
|
|
$the_product = get_post( $the_product->id ); |
103
|
|
|
} elseif ( ! ( $the_product instanceof WP_Post ) ) { |
|
|
|
|
104
|
|
|
$the_product = false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return apply_filters( 'woocommerce_product_object', $the_product ); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.