1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
<?php /** * Utility functions meant for helping in migration from posts tables to custom order tables. */
namespace Automattic\WooCommerce\Internal\Utilities;
use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; use Automattic\WooCommerce\Internal\DataStores\Orders\{ DataSynchronizer, OrdersTableDataStore }; use WC_Order; use WP_Post;
/** * Utility functions meant for helping in migration from posts tables to custom order tables. */ class COTMigrationUtil {
/** * Custom order table controller. * * @var CustomOrdersTableController */ private $table_controller;
/** * Data synchronizer. * * @var DataSynchronizer */ private $data_synchronizer;
/** * Initialize method, invoked by the DI container. * * @internal Automatically called by the container. * @param CustomOrdersTableController $table_controller Custom order table controller. * @param DataSynchronizer $data_synchronizer Data synchronizer. * * @return void */ final public function init( CustomOrdersTableController $table_controller, DataSynchronizer $data_synchronizer ) { $this->table_controller = $table_controller; $this->data_synchronizer = $data_synchronizer; }
/** * Helper function to get screen name of orders page in wp-admin. * * @throws \Exception If called from outside of wp-admin. * * @return string */ public function get_order_admin_screen() : string { if ( ! is_admin() ) { throw new \Exception( 'This function should only be called in admin.' ); } return $this->custom_orders_table_usage_is_enabled() && function_exists( 'wc_get_page_screen_id' ) ? wc_get_page_screen_id( 'shop-order' ) : 'shop_order'; }
/** * Helper function to get whether custom order tables are enabled or not. * * @return bool */ private function custom_orders_table_usage_is_enabled() : bool { return $this->table_controller->custom_orders_table_usage_is_enabled(); }
/** * Checks if posts and order custom table sync is enabled and there are no pending orders. * * @return bool */ public function is_custom_order_tables_in_sync() : bool { if ( ! $this->data_synchronizer->data_sync_is_enabled() ) { return false; }
return 0 === $this->data_synchronizer->get_total_pending_count(); }
/** * Gets value of a meta key from WC_Data object if passed, otherwise from the post object. * This helper function support backward compatibility for meta box functions, when moving from posts based store to custom tables. * * @param WP_Post|null $post Post object, meta will be fetched from this only when `$data` is not passed. * @param \WC_Data|null $data WC_Data object, will be preferred over post object when passed. * @param string $key Key to fetch metadata for. * @param bool $single Whether metadata is single. * * @return array|mixed|string Value of the meta key. */ public function get_post_or_object_meta( ?WP_Post $post, ?\WC_Data $data, string $key, bool $single ) { if ( isset( $data ) ) { if ( method_exists( $data, "get$key" ) ) { return $data->{"get$key"}(); } return $data->get_meta( $key, $single ); } else { return isset( $post->ID ) ? get_post_meta( $post->ID, $key, $single ) : false; } }
/** * Helper function to initialize the global $theorder object, mostly used during order meta boxes rendering. * * @param WC_Order|WP_Post $post_or_order_object Post or order object. * * @return bool|WC_Order|WC_Order_Refund WC_Order object. */ public function init_theorder_object( $post_or_order_object ) { global $theorder; if ( $theorder instanceof WC_Order ) { return $theorder; }
if ( $post_or_order_object instanceof WC_Order ) { $theorder = $post_or_order_object; } else { $theorder = wc_get_order( $post_or_order_object->ID ); } return $theorder; }
/** * Helper function to get ID from a post or order object. * * @param WP_Post/WC_Order $post_or_order_object WP_Post/WC_Order object to get ID for. * * @return int Order or post ID. */ public function get_post_or_order_id( $post_or_order_object ) : int { if ( is_numeric( $post_or_order_object ) ) { return (int) $post_or_order_object; } elseif ( $post_or_order_object instanceof WC_Order ) { return $post_or_order_object->get_id(); } elseif ( $post_or_order_object instanceof WP_Post ) { return $post_or_order_object->ID; } return 0; }
/** * Checks if passed id, post or order object is a WC_Order object. * * @param int|WP_Post|WC_Order $order_id Order ID, post object or order object. * @param string[] $types Types to match against. * * @return bool Whether the passed param is an order. */ public function is_order( $order_id, array $types = array( 'shop_order' ) ) : bool { $order_id = $this->get_post_or_order_id( $order_id ); $order_data_store = \WC_Data_Store::load( 'order' ); return in_array( $order_data_store->get_order_type( $order_id ), $types, true ); }
/** * Returns type pf passed id, post or order object. * * @param int|WP_Post|WC_Order $order_id Order ID, post object or order object. * * @return string|null Type of the order. */ public function get_order_type( $order_id ) { $order_id = $this->get_post_or_order_id( $order_id ); $order_data_store = \WC_Data_Store::load( 'order' ); return $order_data_store->get_order_type( $order_id ); }
/** * Get the name of the database table that's currently in use for orders. * * @return string */ public function get_table_for_orders() { if ( $this->custom_orders_table_usage_is_enabled() ) { $table_name = OrdersTableDataStore::get_orders_table_name(); } else { global $wpdb; $table_name = $wpdb->posts; }
return $table_name; }
/** * Get the name of the database table that's currently in use for orders. * * @return string */ public function get_table_for_order_meta() { if ( $this->custom_orders_table_usage_is_enabled() ) { $table_name = OrdersTableDataStore::get_meta_table_name(); } else { global $wpdb; $table_name = $wpdb->postmeta; }
return $table_name; } }
|