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