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