Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
35.48% |
11 / 31 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
Order_Status | |
35.48% |
11 / 31 |
|
20.00% |
1 / 5 |
50.67 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
register_status | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 | |||
add_order_status_to_woocommerce | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
add_to_paid_status_list | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
add_to_reports_status_list | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
6.05 |
1 | <?php |
2 | |
3 | namespace BrianHenryIE\WC_Address_Validation\WooCommerce; |
4 | |
5 | use BrianHenryIE\WC_Address_Validation\API_Interface; |
6 | use BrianHenryIE\WC_Address_Validation\Settings_Interface; |
7 | use Psr\Log\LoggerAwareTrait; |
8 | use Psr\Log\LoggerInterface; |
9 | |
10 | class Order_Status { |
11 | |
12 | use LoggerAwareTrait; |
13 | |
14 | const BAD_ADDRESS_STATUS = 'bad-address'; |
15 | |
16 | /** |
17 | * @var Settings_Interface |
18 | */ |
19 | protected Settings_Interface $settings; |
20 | |
21 | /** |
22 | * @var API_Interface |
23 | */ |
24 | protected API_Interface $api; |
25 | |
26 | /** |
27 | * Order_Status constructor. |
28 | * |
29 | * @param API_Interface $api |
30 | * @param Settings_Interface $settings |
31 | * @param LoggerInterface $logger |
32 | */ |
33 | public function __construct( API_Interface $api, Settings_Interface $settings, LoggerInterface $logger ) { |
34 | |
35 | $this->logger = $logger; |
36 | $this->settings = $settings; |
37 | $this->api = $api; |
38 | } |
39 | |
40 | /** |
41 | * Register the order/post status with WordPress. |
42 | * |
43 | * @hooked woocommerce_init |
44 | * @see WooCommerce::init() |
45 | */ |
46 | public function register_status(): void { |
47 | |
48 | register_post_status( |
49 | 'wc-' . self::BAD_ADDRESS_STATUS, |
50 | array( |
51 | 'label' => 'Bad Address', |
52 | 'public' => true, |
53 | 'exclude_from_search' => false, |
54 | 'show_in_admin_all_list' => true, |
55 | 'show_in_admin_status_list' => true, |
56 | 'label_count' => _n_noop( 'Bad Address <span class="count">(%s)</span>', 'Bad Addresses <span class="count">(%s)</span>' ), |
57 | ) |
58 | ); |
59 | } |
60 | |
61 | /** |
62 | * Add "wc-bad-address" to WooCommerce's list of statuses. |
63 | * |
64 | * Adds the new order status after "on-hold". |
65 | * |
66 | * @hooked wc_order_statuses |
67 | * @see wc_get_order_statuses() |
68 | * |
69 | * @param string[] $order_statuses WooCommerce order statuses. |
70 | * @return string[] |
71 | */ |
72 | public function add_order_status_to_woocommerce( $order_statuses ): array { |
73 | |
74 | $new_order_statuses = array(); |
75 | |
76 | foreach ( $order_statuses as $key => $status ) { |
77 | $new_order_statuses[ $key ] = $status; |
78 | if ( 'wc-on-hold' === $key ) { |
79 | $new_order_statuses[ 'wc-' . self::BAD_ADDRESS_STATUS ] = 'Bad Address'; |
80 | } |
81 | } |
82 | return $new_order_statuses; |
83 | } |
84 | |
85 | /** |
86 | * Add the status to the list considered "paid" when considered by WooCommerce and other plugins. |
87 | * |
88 | * @hooked woocommerce_order_is_paid_statuses |
89 | * @see wc_get_is_paid_statuses() |
90 | * |
91 | * @param string[] $statuses ['processing', completed'] and other custom statuses that apply to paid orders. |
92 | * @return string[] |
93 | */ |
94 | public function add_to_paid_status_list( $statuses ): array { |
95 | $statuses[] = self::BAD_ADDRESS_STATUS; |
96 | return $statuses; |
97 | } |
98 | |
99 | /** |
100 | * WooCommerce's reports do not respect wc_get_is_paid_statuses() so we need to add the status here too. |
101 | * |
102 | * @hooked woocommerce_reports_order_statuses |
103 | * @see \WC_Admin_Report::get_order_report_data() |
104 | * @see wp-admin/admin.php?page=wc-reports |
105 | * |
106 | * @param false|string[] $order_status |
107 | * |
108 | * @return false|string[] |
109 | */ |
110 | public function add_to_reports_status_list( $order_status ) { |
111 | |
112 | // In the refund report it is false. |
113 | if ( false === $order_status || ! is_array( $order_status ) ) { |
114 | return $order_status; |
115 | } |
116 | |
117 | // In all paid scenarios, there are at least 'completed', 'processing', 'on-hold' already in the list. |
118 | if ( ! ( in_array( 'completed', $order_status, true ) |
119 | && in_array( 'processing', $order_status, true ) |
120 | && in_array( 'on-hold', $order_status, true ) |
121 | ) ) { |
122 | return $order_status; |
123 | } |
124 | |
125 | $this->logger->debug( 'Adding order status to reports status list', array( 'hooked' => 'woocommerce_reports_order_statuses' ) ); |
126 | |
127 | $order_status[] = self::BAD_ADDRESS_STATUS; |
128 | |
129 | return $order_status; |
130 | } |
131 | } |