Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Admin/API/Reports
File: OrderAwareControllerTrait.php
<?php
[0] Fix | Delete
declare( strict_types = 1);
[1] Fix | Delete
[2] Fix | Delete
namespace Automattic\WooCommerce\Admin\API\Reports;
[3] Fix | Delete
[4] Fix | Delete
// Exit if accessed directly.
[5] Fix | Delete
if ( ! defined( 'ABSPATH' ) ) {
[6] Fix | Delete
exit;
[7] Fix | Delete
}
[8] Fix | Delete
[9] Fix | Delete
/**
[10] Fix | Delete
* Trait to contain shared methods for reports Controllers that use order and orders statuses.
[11] Fix | Delete
*
[12] Fix | Delete
* If your analytics controller needs to work with orders,
[13] Fix | Delete
* you will most probably need to use at least {@see get_order_statuses() get_order_statuses()}
[14] Fix | Delete
* to filter only "actionable" statuses to produce consistent results among other analytics.
[15] Fix | Delete
*
[16] Fix | Delete
* @see GenericController
[17] Fix | Delete
*/
[18] Fix | Delete
trait OrderAwareControllerTrait {
[19] Fix | Delete
[20] Fix | Delete
/**
[21] Fix | Delete
* Get the order number for an order. If no filter is present for `woocommerce_order_number`, we can just return the ID.
[22] Fix | Delete
* Returns the parent order number if the order is actually a refund.
[23] Fix | Delete
*
[24] Fix | Delete
* @param int $order_id Order ID.
[25] Fix | Delete
* @return string|null The Order Number or null if the order doesn't exist.
[26] Fix | Delete
*/
[27] Fix | Delete
protected function get_order_number( $order_id ) {
[28] Fix | Delete
$order = wc_get_order( $order_id );
[29] Fix | Delete
[30] Fix | Delete
if ( ! $this->is_valid_order( $order ) ) {
[31] Fix | Delete
return null;
[32] Fix | Delete
}
[33] Fix | Delete
[34] Fix | Delete
if ( 'shop_order_refund' === $order->get_type() ) {
[35] Fix | Delete
$order = wc_get_order( $order->get_parent_id() );
[36] Fix | Delete
[37] Fix | Delete
// If the parent order doesn't exist, return null.
[38] Fix | Delete
if ( ! $this->is_valid_order( $order ) ) {
[39] Fix | Delete
return null;
[40] Fix | Delete
}
[41] Fix | Delete
}
[42] Fix | Delete
[43] Fix | Delete
if ( ! has_filter( 'woocommerce_order_number' ) ) {
[44] Fix | Delete
return $order->get_id();
[45] Fix | Delete
}
[46] Fix | Delete
[47] Fix | Delete
return $order->get_order_number();
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
/**
[51] Fix | Delete
* Whether the order is valid.
[52] Fix | Delete
*
[53] Fix | Delete
* @param bool|WC_Order|WC_Order_Refund $order Order object.
[54] Fix | Delete
* @return bool True if the order is valid, false otherwise.
[55] Fix | Delete
*/
[56] Fix | Delete
protected function is_valid_order( $order ) {
[57] Fix | Delete
return $order instanceof \WC_Order || $order instanceof \WC_Order_Refund;
[58] Fix | Delete
}
[59] Fix | Delete
[60] Fix | Delete
/**
[61] Fix | Delete
* Get the order total with the related currency formatting.
[62] Fix | Delete
* Returns the parent order total if the order is actually a refund.
[63] Fix | Delete
*
[64] Fix | Delete
* @param int $order_id Order ID.
[65] Fix | Delete
* @return string|null The Order Number or null if the order doesn't exist.
[66] Fix | Delete
*/
[67] Fix | Delete
protected function get_total_formatted( $order_id ) {
[68] Fix | Delete
$order = wc_get_order( $order_id );
[69] Fix | Delete
[70] Fix | Delete
if ( ! $this->is_valid_order( $order ) ) {
[71] Fix | Delete
return null;
[72] Fix | Delete
}
[73] Fix | Delete
[74] Fix | Delete
if ( 'shop_order_refund' === $order->get_type() ) {
[75] Fix | Delete
$order = wc_get_order( $order->get_parent_id() );
[76] Fix | Delete
[77] Fix | Delete
if ( ! $this->is_valid_order( $order ) ) {
[78] Fix | Delete
return null;
[79] Fix | Delete
}
[80] Fix | Delete
}
[81] Fix | Delete
[82] Fix | Delete
return wp_strip_all_tags( html_entity_decode( $order->get_formatted_order_total() ), true );
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Get order statuses without prefixes.
[87] Fix | Delete
* Includes unregistered statuses that have been marked "actionable".
[88] Fix | Delete
*
[89] Fix | Delete
* @return array
[90] Fix | Delete
*/
[91] Fix | Delete
public static function get_order_statuses() {
[92] Fix | Delete
// Allow all statuses selected as "actionable" - this may include unregistered statuses.
[93] Fix | Delete
// See: https://github.com/woocommerce/woocommerce-admin/issues/5592.
[94] Fix | Delete
$actionable_statuses = get_option( 'woocommerce_actionable_order_statuses', array() );
[95] Fix | Delete
[96] Fix | Delete
// Prevent errors if the database entry is not the expected type (array).
[97] Fix | Delete
if ( ! is_array( $actionable_statuses ) ) {
[98] Fix | Delete
$actionable_statuses = array();
[99] Fix | Delete
}
[100] Fix | Delete
[101] Fix | Delete
// See WC_REST_Orders_V2_Controller::get_collection_params() re: any/trash statuses.
[102] Fix | Delete
$registered_statuses = array_merge( array( 'any', 'trash' ), array_keys( self::get_order_status_labels() ) );
[103] Fix | Delete
[104] Fix | Delete
// Merge the status arrays (using flip to avoid array_unique()).
[105] Fix | Delete
$allowed_statuses = array_keys( array_merge( array_flip( $registered_statuses ), array_flip( $actionable_statuses ) ) );
[106] Fix | Delete
[107] Fix | Delete
return $allowed_statuses;
[108] Fix | Delete
}
[109] Fix | Delete
[110] Fix | Delete
/**
[111] Fix | Delete
* Get order statuses (and labels) without prefixes.
[112] Fix | Delete
*
[113] Fix | Delete
* @internal
[114] Fix | Delete
* @return array
[115] Fix | Delete
*/
[116] Fix | Delete
public static function get_order_status_labels() {
[117] Fix | Delete
$order_statuses = array();
[118] Fix | Delete
[119] Fix | Delete
foreach ( wc_get_order_statuses() as $key => $label ) {
[120] Fix | Delete
$new_key = str_replace( 'wc-', '', $key );
[121] Fix | Delete
$order_statuses[ $new_key ] = $label;
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
return $order_statuses;
[125] Fix | Delete
}
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function