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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
<?php /** * A class of utilities for dealing with orders. */
namespace Automattic\WooCommerce\Utilities;
use Automattic\WooCommerce\Caches\OrderCacheController; use Automattic\WooCommerce\Internal\Admin\Orders\PageController; use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; use Automattic\WooCommerce\Internal\Utilities\COTMigrationUtil; use WC_Order; use WP_Post;
/** * A class of utilities for dealing with orders. */ final class OrderUtil {
/** * Helper function to get screen name of orders page in wp-admin. * * @return string */ public static function get_order_admin_screen() : string { return wc_get_container()->get( COTMigrationUtil::class )->get_order_admin_screen(); }
/** * Helper function to get whether custom order tables are enabled or not. * * @return bool */ public static function custom_orders_table_usage_is_enabled() : bool { return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled(); }
/** * Helper function to get whether custom order tables are enabled or not. * * @return bool */ public static function custom_orders_table_datastore_cache_enabled(): bool { return wc_get_container()->get( CustomOrdersTableController::class )->hpos_data_caching_is_enabled(); }
/** * Helper function to get whether the orders cache should be used or not. * * @return bool True if the orders cache should be used, false otherwise. */ public static function orders_cache_usage_is_enabled() : bool { return wc_get_container()->get( OrderCacheController::class )->orders_cache_usage_is_enabled(); }
/** * Checks if posts and order custom table sync is enabled and there are no pending orders. * * @return bool */ public static function is_custom_order_tables_in_sync() : bool { return wc_get_container()->get( COTMigrationUtil::class )->is_custom_order_tables_in_sync(); }
/** * 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 static function get_post_or_object_meta( ?WP_Post $post, ?\WC_Data $data, string $key, bool $single ) { return wc_get_container()->get( COTMigrationUtil::class )->get_post_or_object_meta( $post, $data, $key, $single ); }
/** * 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 static function init_theorder_object( $post_or_order_object ) { return wc_get_container()->get( COTMigrationUtil::class )->init_theorder_object( $post_or_order_object ); }
/** * Helper function to id from an 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 static function get_post_or_order_id( $post_or_order_object ) : int { return wc_get_container()->get( COTMigrationUtil::class )->get_post_or_order_id( $post_or_order_object ); }
/** * 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 static function is_order( $order_id, $types = array( 'shop_order' ) ) { return wc_get_container()->get( COTMigrationUtil::class )->is_order( $order_id, $types ); }
/** * 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 static function get_order_type( $order_id ) { return wc_get_container()->get( COTMigrationUtil::class )->get_order_type( $order_id ); }
/** * Helper method to generate admin url for an order. * * @param int $order_id Order ID. * * @return string Admin url for an order. */ public static function get_order_admin_edit_url( int $order_id ) : string { return wc_get_container()->get( PageController::class )->get_edit_url( $order_id ); }
/** * Helper method to generate admin URL for new order. * * @return string Link for new order. */ public static function get_order_admin_new_url() : string { return wc_get_container()->get( PageController::class )->get_new_page_url(); }
/** * Check if the current admin screen is an order list table. * * @param string $order_type Optional. The order type to check for. Default shop_order. * * @return bool */ public static function is_order_list_table_screen( $order_type = 'shop_order' ) : bool { return wc_get_container()->get( PageController::class )->is_order_screen( $order_type, 'list' ); }
/** * Check if the current admin screen is for editing an order. * * @param string $order_type Optional. The order type to check for. Default shop_order. * * @return bool */ public static function is_order_edit_screen( $order_type = 'shop_order' ) : bool { return wc_get_container()->get( PageController::class )->is_order_screen( $order_type, 'edit' ); }
/** * Check if the current admin screen is adding a new order. * * @param string $order_type Optional. The order type to check for. Default shop_order. * * @return bool */ public static function is_new_order_screen( $order_type = 'shop_order' ) : bool { return wc_get_container()->get( PageController::class )->is_order_screen( $order_type, 'new' ); }
/** * Get the name of the database table that's currently in use for orders. * * @return string */ public static function get_table_for_orders() { return wc_get_container()->get( COTMigrationUtil::class )->get_table_for_orders(); }
/** * Get the name of the database table that's currently in use for orders. * * @return string */ public static function get_table_for_order_meta() { return wc_get_container()->get( COTMigrationUtil::class )->get_table_for_order_meta(); }
/** * Counts number of orders of a given type. * * @since 8.7.0 * * @param string $order_type Order type. * @return array<string,int> Array of order counts indexed by order type. */ public static function get_count_for_type( $order_type ) { global $wpdb;
$cache_key = \WC_Cache_Helper::get_cache_prefix( 'orders' ) . 'order-count-' . $order_type; $count_per_status = wp_cache_get( $cache_key, 'counts' );
if ( false === $count_per_status ) { if ( self::custom_orders_table_usage_is_enabled() ) { // phpcs:disable WordPress.DB.PreparedSQL.NotPrepared $results = $wpdb->get_results( $wpdb->prepare( 'SELECT `status`, COUNT(*) AS `count` FROM ' . self::get_table_for_orders() . ' WHERE `type` = %s GROUP BY `status`', $order_type ), ARRAY_A ); // phpcs:enable
$count_per_status = array_map( 'absint', array_column( $results, 'count', 'status' ) ); } else { $count_per_status = (array) wp_count_posts( $order_type ); }
// Make sure all order statuses are included just in case. $count_per_status = array_merge( array_fill_keys( array_keys( wc_get_order_statuses() ), 0 ), $count_per_status );
wp_cache_set( $cache_key, $count_per_status, 'counts' ); }
return $count_per_status; }
/** * Removes the 'wc-' prefix from status. * * @param string $status The status to remove the prefix from. * * @return string The status without the prefix. * @since 9.2.0 */ public static function remove_status_prefix( string $status ): string { if ( strpos( $status, 'wc-' ) === 0 ) { $status = substr( $status, 3 ); }
return $status; } }
|