1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
4
|
|
|
exit; // Exit if accessed directly |
5
|
|
|
} |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Order Factory Class |
9
|
|
|
* |
10
|
|
|
* The WooCommerce order factory creating the right order objects. |
11
|
|
|
* |
12
|
|
|
* @class WC_Order_Factory |
13
|
|
|
* @version 2.6.0 |
14
|
|
|
* @package WooCommerce/Classes |
15
|
|
|
* @category Class |
16
|
|
|
* @author WooThemes |
17
|
|
|
*/ |
18
|
|
|
class WC_Order_Factory { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Get order. |
22
|
|
|
* |
23
|
|
|
* @param bool $the_order (default: false) |
24
|
|
|
* @return WC_Order|bool |
25
|
|
|
*/ |
26
|
|
|
public static function get_order( $the_order = false ) { |
27
|
|
|
global $post; |
28
|
|
|
|
29
|
|
|
if ( false === $the_order ) { |
30
|
|
|
$the_order = $post; |
31
|
|
|
} elseif ( is_numeric( $the_order ) ) { |
32
|
|
|
$the_order = get_post( $the_order ); |
33
|
|
|
} elseif ( $the_order instanceof WC_Order ) { |
34
|
|
|
$the_order = get_post( $the_order->get_id() ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if ( ! $the_order || ! is_object( $the_order ) ) { |
38
|
|
|
return false; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$order_id = absint( $the_order->ID ); |
42
|
|
|
$post_type = $the_order->post_type; |
43
|
|
|
|
44
|
|
|
if ( $order_type = wc_get_order_type( $post_type ) ) { |
45
|
|
|
$classname = $order_type['class_name']; |
46
|
|
|
} else { |
47
|
|
|
$classname = false; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
// Filter classname so that the class can be overridden if extended. |
51
|
|
|
$classname = apply_filters( 'woocommerce_order_class', $classname, $post_type, $order_id, $the_order ); |
52
|
|
|
|
53
|
|
|
if ( ! class_exists( $classname ) ) { |
54
|
|
|
return false; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new $classname( $the_order ); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get order item. |
62
|
|
|
* @param int |
63
|
|
|
* @return WC_Order_Item |
64
|
|
|
*/ |
65
|
|
|
public static function get_order_item( $item_id = 0 ) { |
66
|
|
|
global $wpdb; |
67
|
|
|
|
68
|
|
|
if ( is_numeric( $item_id ) ) { |
69
|
|
|
$item_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_order_items WHERE order_item_id = %d LIMIT 1;", $item_id ) ); |
70
|
|
|
$item_type = $item_data->order_item_type; |
71
|
|
|
} elseif ( $item_id instanceof WC_Order_Item ) { |
72
|
|
|
$item_data = $item_id->get_data(); |
73
|
|
|
$item_type = $item_data->get_type(); |
74
|
|
|
} elseif( is_object( $item_id ) && ! empty( $item_id->order_item_type ) ) { |
75
|
|
|
$item_data = $item_id; |
76
|
|
|
$item_type = $item_id->order_item_type; |
77
|
|
|
} else { |
78
|
|
|
$item_data = false; |
79
|
|
|
$item_type = false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ( $item_data && $item_type ) { |
83
|
|
|
switch ( $item_type ) { |
84
|
|
|
case 'line_item' : |
85
|
|
|
case 'product' : |
86
|
|
|
return new WC_Order_Item_Product( $item_data ); |
87
|
|
|
break; |
|
|
|
|
88
|
|
|
case 'coupon' : |
89
|
|
|
return new WC_Order_Item_Coupon( $item_data ); |
90
|
|
|
break; |
|
|
|
|
91
|
|
|
case 'fee' : |
92
|
|
|
return new WC_Order_Item_Fee( $item_data ); |
93
|
|
|
break; |
|
|
|
|
94
|
|
|
case 'shipping' : |
95
|
|
|
return new WC_Order_Item_Shipping( $item_data ); |
96
|
|
|
break; |
|
|
|
|
97
|
|
|
case 'tax' : |
98
|
|
|
return new WC_Order_Item_Tax( $item_data ); |
99
|
|
|
break; |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return new WC_Order_Item(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.