OrderUtil.php
| 1 | <?php |
| 2 | /** |
| 3 | * A class of utilities for dealing with orders. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Utilities; |
| 9 | |
| 10 | use Automattic\WooCommerce\Caches\OrderCacheController; |
| 11 | use Automattic\WooCommerce\Caches\OrderCountCache; |
| 12 | use Automattic\WooCommerce\Enums\OrderStatus; |
| 13 | use Automattic\WooCommerce\Internal\Admin\Orders\PageController; |
| 14 | use Automattic\WooCommerce\Internal\DataStores\Orders\CustomOrdersTableController; |
| 15 | use Automattic\WooCommerce\Internal\Utilities\COTMigrationUtil; |
| 16 | use WC_Order; |
| 17 | use WP_Post; |
| 18 | |
| 19 | /** |
| 20 | * A class of utilities for dealing with orders. |
| 21 | */ |
| 22 | final class OrderUtil { |
| 23 | |
| 24 | /** |
| 25 | * Helper function to get screen name of orders page in wp-admin. |
| 26 | * |
| 27 | * @return string |
| 28 | */ |
| 29 | public static function get_order_admin_screen(): string { |
| 30 | return wc_get_container()->get( COTMigrationUtil::class )->get_order_admin_screen(); |
| 31 | } |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Helper function to get whether custom order tables are enabled or not. |
| 36 | * |
| 37 | * @return bool |
| 38 | */ |
| 39 | public static function custom_orders_table_usage_is_enabled(): bool { |
| 40 | return wc_get_container()->get( CustomOrdersTableController::class )->custom_orders_table_usage_is_enabled(); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Helper function to get whether custom order tables are enabled or not. |
| 45 | * |
| 46 | * @return bool |
| 47 | */ |
| 48 | public static function custom_orders_table_datastore_cache_enabled(): bool { |
| 49 | return wc_get_container()->get( CustomOrdersTableController::class )->hpos_data_caching_is_enabled(); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Helper function to get whether the orders cache should be used or not. |
| 54 | * |
| 55 | * @return bool True if the orders cache should be used, false otherwise. |
| 56 | */ |
| 57 | public static function orders_cache_usage_is_enabled(): bool { |
| 58 | return wc_get_container()->get( OrderCacheController::class )->orders_cache_usage_is_enabled(); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Checks if posts and order custom table sync is enabled and there are no pending orders. |
| 63 | * |
| 64 | * @return bool |
| 65 | */ |
| 66 | public static function is_custom_order_tables_in_sync(): bool { |
| 67 | return wc_get_container()->get( COTMigrationUtil::class )->is_custom_order_tables_in_sync(); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Checks whether the real-time data sync between the posts and orders tables is enabled. |
| 72 | * |
| 73 | * Unlike is_custom_order_tables_in_sync(), this only reflects whether the sync setting is enabled (a cheap |
| 74 | * option read) and does not run a query to check whether the tables are currently fully synchronized. Use |
| 75 | * this when you only need to know if sync is turned on, not whether every order is currently in sync. |
| 76 | * |
| 77 | * @since 11.0.0 |
| 78 | * |
| 79 | * @return bool True if data sync is enabled, false otherwise. |
| 80 | */ |
| 81 | public static function custom_orders_table_data_sync_is_enabled(): bool { |
| 82 | return wc_get_container()->get( COTMigrationUtil::class )->custom_orders_table_data_sync_is_enabled(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets value of a meta key from WC_Data object if passed, otherwise from the post object. |
| 87 | * This helper function support backward compatibility for meta box functions, when moving from posts based store to custom tables. |
| 88 | * |
| 89 | * @param WP_Post|null $post Post object, meta will be fetched from this only when `$data` is not passed. |
| 90 | * @param \WC_Data|null $data WC_Data object, will be preferred over post object when passed. |
| 91 | * @param string $key Key to fetch metadata for. |
| 92 | * @param bool $single Whether metadata is single. |
| 93 | * |
| 94 | * @return array|mixed|string Value of the meta key. |
| 95 | */ |
| 96 | public static function get_post_or_object_meta( ?WP_Post $post, ?\WC_Data $data, string $key, bool $single ) { |
| 97 | return wc_get_container()->get( COTMigrationUtil::class )->get_post_or_object_meta( $post, $data, $key, $single ); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Helper function to initialize the global $theorder object, mostly used during order meta boxes rendering. |
| 102 | * |
| 103 | * @param WC_Order|WP_Post $post_or_order_object Post or order object. |
| 104 | * |
| 105 | * @return bool|WC_Order|WC_Order_Refund WC_Order object. |
| 106 | */ |
| 107 | public static function init_theorder_object( $post_or_order_object ) { |
| 108 | return wc_get_container()->get( COTMigrationUtil::class )->init_theorder_object( $post_or_order_object ); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Helper function to id from an post or order object. |
| 113 | * |
| 114 | * @param WP_Post/WC_Order $post_or_order_object WP_Post/WC_Order object to get ID for. |
| 115 | * |
| 116 | * @return int Order or post ID. |
| 117 | */ |
| 118 | public static function get_post_or_order_id( $post_or_order_object ): int { |
| 119 | return wc_get_container()->get( COTMigrationUtil::class )->get_post_or_order_id( $post_or_order_object ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Checks if passed id, post or order object is a WC_Order object. |
| 124 | * |
| 125 | * @param int|WP_Post|WC_Order $order_id Order ID, post object or order object. |
| 126 | * @param string[] $types Types to match against. |
| 127 | * |
| 128 | * @return bool Whether the passed param is an order. |
| 129 | */ |
| 130 | public static function is_order( $order_id, $types = array( 'shop_order' ) ) { |
| 131 | return wc_get_container()->get( COTMigrationUtil::class )->is_order( $order_id, $types ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns type pf passed id, post or order object. |
| 136 | * |
| 137 | * @param int|WP_Post|WC_Order $order_id Order ID, post object or order object. |
| 138 | * |
| 139 | * @return string|null Type of the order. |
| 140 | */ |
| 141 | public static function get_order_type( $order_id ) { |
| 142 | return wc_get_container()->get( COTMigrationUtil::class )->get_order_type( $order_id ); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Helper method to generate admin url for an order. |
| 147 | * |
| 148 | * @param int $order_id Order ID. |
| 149 | * |
| 150 | * @return string Admin url for an order. |
| 151 | */ |
| 152 | public static function get_order_admin_edit_url( int $order_id ): string { |
| 153 | return wc_get_container()->get( PageController::class )->get_edit_url( $order_id ); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Helper method to generate admin URL for new order. |
| 158 | * |
| 159 | * @return string Link for new order. |
| 160 | */ |
| 161 | public static function get_order_admin_new_url(): string { |
| 162 | return wc_get_container()->get( PageController::class )->get_new_page_url(); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * Check if the current admin screen is an order list table. |
| 167 | * |
| 168 | * @param string $order_type Optional. The order type to check for. Default shop_order. |
| 169 | * |
| 170 | * @return bool |
| 171 | */ |
| 172 | public static function is_order_list_table_screen( $order_type = 'shop_order' ): bool { |
| 173 | return wc_get_container()->get( PageController::class )->is_order_screen( $order_type, 'list' ); |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Check if the current admin screen is for editing an order. |
| 178 | * |
| 179 | * @param string $order_type Optional. The order type to check for. Default shop_order. |
| 180 | * |
| 181 | * @return bool |
| 182 | */ |
| 183 | public static function is_order_edit_screen( $order_type = 'shop_order' ): bool { |
| 184 | return wc_get_container()->get( PageController::class )->is_order_screen( $order_type, 'edit' ); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Check if the current admin screen is adding a new order. |
| 189 | * |
| 190 | * @param string $order_type Optional. The order type to check for. Default shop_order. |
| 191 | * |
| 192 | * @return bool |
| 193 | */ |
| 194 | public static function is_new_order_screen( $order_type = 'shop_order' ): bool { |
| 195 | return wc_get_container()->get( PageController::class )->is_order_screen( $order_type, 'new' ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Get the name of the database table that's currently in use for orders. |
| 200 | * |
| 201 | * @return string |
| 202 | */ |
| 203 | public static function get_table_for_orders() { |
| 204 | return wc_get_container()->get( COTMigrationUtil::class )->get_table_for_orders(); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get the name of the database table that's currently in use for orders. |
| 209 | * |
| 210 | * @return string |
| 211 | */ |
| 212 | public static function get_table_for_order_meta() { |
| 213 | return wc_get_container()->get( COTMigrationUtil::class )->get_table_for_order_meta(); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Counts number of orders of a given type. |
| 218 | * |
| 219 | * @since 8.7.0 |
| 220 | * |
| 221 | * @param string $order_type Order type. |
| 222 | * @return array<string,int> Array of order counts indexed by order type. |
| 223 | */ |
| 224 | public static function get_count_for_type( $order_type ) { |
| 225 | global $wpdb; |
| 226 | |
| 227 | $order_type = (string) $order_type; |
| 228 | |
| 229 | $order_count_cache = new OrderCountCache(); |
| 230 | $count_per_status = $order_count_cache->get( $order_type ); |
| 231 | |
| 232 | if ( null === $count_per_status ) { |
| 233 | if ( self::custom_orders_table_usage_is_enabled() ) { |
| 234 | $results = $wpdb->get_results( |
| 235 | $wpdb->prepare( |
| 236 | 'SELECT status, COUNT(*) AS count FROM %i WHERE type = %s GROUP BY status', |
| 237 | self::get_table_for_orders(), |
| 238 | $order_type |
| 239 | ), |
| 240 | ARRAY_A |
| 241 | ); |
| 242 | $count_per_status = array_map( 'absint', array_column( $results, 'count', 'status' ) ); |
| 243 | } else { |
| 244 | $count_per_status = (array) wp_count_posts( $order_type ); |
| 245 | } |
| 246 | |
| 247 | // Make sure all order statuses are included just in case. |
| 248 | $count_per_status = array_merge( |
| 249 | array_fill_keys( array_merge( array_keys( wc_get_order_statuses() ), array( OrderStatus::TRASH ) ), 0 ), |
| 250 | $count_per_status |
| 251 | ); |
| 252 | |
| 253 | $order_count_cache->set_multiple( $order_type, $count_per_status ); |
| 254 | } |
| 255 | |
| 256 | return $count_per_status; |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Removes the 'wc-' prefix from status. |
| 261 | * |
| 262 | * @param string $status The status to remove the prefix from. |
| 263 | * |
| 264 | * @return string The status without the prefix. |
| 265 | * @since 9.2.0 |
| 266 | */ |
| 267 | public static function remove_status_prefix( string $status ): string { |
| 268 | if ( strpos( $status, 'wc-' ) === 0 ) { |
| 269 | $status = substr( $status, 3 ); |
| 270 | } |
| 271 | |
| 272 | return $status; |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Checks if the new full refund data is used. |
| 277 | * |
| 278 | * @return bool |
| 279 | */ |
| 280 | public static function uses_new_full_refund_data() { |
| 281 | $db_version = get_option( 'woocommerce_db_version', null ); |
| 282 | $uses_old_full_refund_data = get_option( 'woocommerce_analytics_uses_old_full_refund_data', 'no' ); |
| 283 | if ( null === $db_version ) { |
| 284 | return 'no' === $uses_old_full_refund_data; |
| 285 | } |
| 286 | return version_compare( $db_version, '10.2.0', '>=' ) && 'no' === $uses_old_full_refund_data; |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * Checks if the data store currently in use for orders is unknown (none of the ones managed by WooCommerce core). |
| 291 | * |
| 292 | * @return bool True if the data store currently in use for orders is neither the HPOS one nor the CPT one. |
| 293 | */ |
| 294 | public static function unknown_orders_data_store_in_use(): bool { |
| 295 | return ! self::custom_orders_table_usage_is_enabled() && |
| 296 | ( \WC_Order_Data_Store_CPT::class !== \WC_Data_Store::load( 'order' )->get_current_class_name() ); |
| 297 | } |
| 298 | } |
| 299 |