/var/www/html_us/wp-content/plugins/woocommerce/src/Admin/API/Reports/OrderAwareControllerTrait.php


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
<?php
declare( strict_types 1);

namespace 
Automattic\WooCommerce\Admin\API\Reports;

// Exit if accessed directly.
if ( ! defined'ABSPATH' ) ) {
    exit;
}

/**
 * Trait to contain shared methods for reports Controllers that use order and orders statuses.
 *
 * If your analytics controller needs to work with orders,
 * you will most probably need to use at least {@see get_order_statuses() get_order_statuses()}
 * to filter only "actionable" statuses to produce consistent results among other analytics.
 *
 * @see GenericController
 */
trait OrderAwareControllerTrait {

    
/**
     * Get the order number for an order. If no filter is present for `woocommerce_order_number`, we can just return the ID.
     * Returns the parent order number if the order is actually a refund.
     *
     * @param  int $order_id Order ID.
     * @return string|null The Order Number or null if the order doesn't exist.
     */
    
protected function get_order_number$order_id ) {
        
$order wc_get_order$order_id );

        if ( ! 
$this->is_valid_order$order ) ) {
            return 
null;
        }

        if ( 
'shop_order_refund' === $order->get_type() ) {
            
$order wc_get_order$order->get_parent_id() );

            
// If the parent order doesn't exist, return null.
            
if ( ! $this->is_valid_order$order ) ) {
                return 
null;
            }
        }

        if ( ! 
has_filter'woocommerce_order_number' ) ) {
            return 
$order->get_id();
        }

        return 
$order->get_order_number();
    }

    
/**
     * Whether the order is valid.
     *
     * @param bool|WC_Order|WC_Order_Refund $order Order object.
     * @return bool True if the order is valid, false otherwise.
     */
    
protected function is_valid_order$order ) {
        return 
$order instanceof \WC_Order || $order instanceof \WC_Order_Refund;
    }

    
/**
     * Get the order total with the related currency formatting.
     * Returns the parent order total if the order is actually a refund.
     *
     * @param  int $order_id Order ID.
     * @return string|null The Order Number or null if the order doesn't exist.
     */
    
protected function get_total_formatted$order_id ) {
        
$order wc_get_order$order_id );

        if ( ! 
$this->is_valid_order$order ) ) {
            return 
null;
        }

        if ( 
'shop_order_refund' === $order->get_type() ) {
            
$order wc_get_order$order->get_parent_id() );

            if ( ! 
$this->is_valid_order$order ) ) {
                return 
null;
            }
        }

        return 
wp_strip_all_tagshtml_entity_decode$order->get_formatted_order_total() ), true );
    }

    
/**
     * Get order statuses without prefixes.
     * Includes unregistered statuses that have been marked "actionable".
     *
     * @return array
     */
    
public static function get_order_statuses() {
        
// Allow all statuses selected as "actionable" - this may include unregistered statuses.
        // See: https://github.com/woocommerce/woocommerce-admin/issues/5592.
        
$actionable_statuses get_option'woocommerce_actionable_order_statuses', array() );

        
// See WC_REST_Orders_V2_Controller::get_collection_params() re: any/trash statuses.
        
$registered_statuses array_merge( array( 'any''trash' ), array_keysself::get_order_status_labels() ) );

        
// Merge the status arrays (using flip to avoid array_unique()).
        
$allowed_statuses array_keysarray_mergearray_flip$registered_statuses ), array_flip$actionable_statuses ) ) );

        return 
$allowed_statuses;
    }

    
/**
     * Get order statuses (and labels) without prefixes.
     *
     * @internal
     * @return array
     */
    
public static function get_order_status_labels() {
        
$order_statuses = array();

        foreach ( 
wc_get_order_statuses() as $key => $label ) {
            
$new_key                    str_replace'wc-'''$key );
            
$order_statuses$new_key ] = $label;
        }

        return 
$order_statuses;
    }
}