Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 196
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WooOrders
0.00% covered (danger)
0.00%
0 / 196
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 register_order_abilities
0.00% covered (danger)
0.00%
0 / 40
0.00% covered (danger)
0.00%
0 / 1
6
 register_report_abilities
0.00% covered (danger)
0.00%
0 / 152
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types=1 );
3
4namespace BLU\Abilities;
5
6/**
7 * WooOrders abilities for WooCommerce orders and reports.
8 */
9class WooOrders {
10
11    /**
12     * Constructor - registers WooCommerce order abilities if WooCommerce is active.
13     */
14    public function __construct() {
15        if ( ! class_exists( 'WooCommerce' ) ) {
16            return;
17        }
18
19        $this->register_order_abilities();
20        $this->register_report_abilities();
21    }
22
23    /**
24     * Register order abilities.
25     */
26    private function register_order_abilities(): void {
27        // Search orders
28        blu_register_ability(
29            'blu/wc-orders-search',
30            array(
31                'label'               => 'Search WooCommerce Orders',
32                'description'         => 'Get a list of WooCommerce orders',
33                'category'            => 'blu-mcp',
34                'input_schema'        => array(
35                    'type'       => 'object',
36                    'properties' => array(
37                        'status'   => array(
38                            'type'        => 'string',
39                            'description' => 'Filter by order status',
40                        ),
41                        'page'     => array(
42                            'type'        => 'integer',
43                            'description' => 'Page number',
44                        ),
45                        'per_page' => array(
46                            'type'        => 'integer',
47                            'description' => 'Orders per page',
48                        ),
49                    ),
50                ),
51                'execute_callback'    => function ( $input = null ) {
52                    $request = new \WP_REST_Request( 'GET', '/wc/v3/orders' );
53                    if ( $input ) {
54                        $request->set_query_params( $input );
55                    }
56                    $response = rest_do_request( $request );
57                    return blu_standardize_rest_response( $response );
58                },
59                'permission_callback' => fn() => current_user_can( 'edit_shop_orders' ),
60                'meta'                => array(
61                    'annotations' => array(
62                        'readonly'     => true,
63                        'destructive'  => false,
64                        'idempotent'   => true,
65                    ),
66                ),
67            )
68        );
69    }
70
71    /**
72     * Register report abilities.
73     */
74    private function register_report_abilities(): void {
75        // Coupons totals report
76        blu_register_ability(
77            'blu/wc-reports-coupons-totals',
78            array(
79                'label'               => 'Get WooCommerce Coupons Report',
80                'description'         => 'Get WooCommerce coupons totals report',
81                'category'            => 'blu-mcp',
82                'input_schema'        => array(
83                    'type' => 'object',
84                ),
85                'execute_callback'    => function () {
86                    $request = new \WP_REST_Request( 'GET', '/wc/v3/reports/coupons/totals' );
87                    $response = rest_do_request( $request );
88                    return blu_standardize_rest_response( $response );
89                },
90                'permission_callback' => fn() => current_user_can( 'view_woocommerce_reports' ),
91                'meta'                => array(
92                    'annotations' => array(
93                        'readonly'     => true,
94                        'destructive'  => false,
95                        'idempotent'   => true,
96                    ),
97                ),
98            )
99        );
100
101        // Customers totals report
102        blu_register_ability(
103            'blu/wc-reports-customers-totals',
104            array(
105                'label'               => 'Get WooCommerce Customers Report',
106                'description'         => 'Get WooCommerce customers totals report',
107                'category'            => 'blu-mcp',
108                'input_schema'        => array(
109                    'type' => 'object',
110                ),
111                'execute_callback'    => function () {
112                    $request = new \WP_REST_Request( 'GET', '/wc/v3/reports/customers/totals' );
113                    $response = rest_do_request( $request );
114                    return blu_standardize_rest_response( $response );
115                },
116                'permission_callback' => fn() => current_user_can( 'view_woocommerce_reports' ),
117                'meta'                => array(
118                    'annotations' => array(
119                        'readonly'     => true,
120                        'destructive'  => false,
121                        'idempotent'   => true,
122                    ),
123                ),
124            )
125        );
126
127        // Orders totals report
128        blu_register_ability(
129            'blu/wc-reports-orders-totals',
130            array(
131                'label'               => 'Get WooCommerce Orders Report',
132                'description'         => 'Get WooCommerce orders totals report',
133                'category'            => 'blu-mcp',
134                'input_schema'        => array(
135                    'type' => 'object',
136                ),
137                'execute_callback'    => function () {
138                    $request = new \WP_REST_Request( 'GET', '/wc/v3/reports/orders/totals' );
139                    $response = rest_do_request( $request );
140                    return blu_standardize_rest_response( $response );
141                },
142                'permission_callback' => fn() => current_user_can( 'view_woocommerce_reports' ),
143                'meta'                => array(
144                    'annotations' => array(
145                        'readonly'     => true,
146                        'destructive'  => false,
147                        'idempotent'   => true,
148                    ),
149                ),
150            )
151        );
152
153        // Products totals report
154        blu_register_ability(
155            'blu/wc-reports-products-totals',
156            array(
157                'label'               => 'Get WooCommerce Products Report',
158                'description'         => 'Get WooCommerce products totals report',
159                'category'            => 'blu-mcp',
160                'input_schema'        => array(
161                    'type' => 'object',
162                ),
163                'execute_callback'    => function () {
164                    $request = new \WP_REST_Request( 'GET', '/wc/v3/reports/products/totals' );
165                    $response = rest_do_request( $request );
166                    return blu_standardize_rest_response( $response );
167                },
168                'permission_callback' => fn() => current_user_can( 'view_woocommerce_reports' ),
169                'meta'                => array(
170                    'annotations' => array(
171                        'readonly'     => true,
172                        'destructive'  => false,
173                        'idempotent'   => true,
174                    ),
175                ),
176            )
177        );
178
179        // Reviews totals report
180        blu_register_ability(
181            'blu/wc-reports-reviews-totals',
182            array(
183                'label'               => 'Get WooCommerce Reviews Report',
184                'description'         => 'Get WooCommerce reviews totals report',
185                'category'            => 'blu-mcp',
186                'input_schema'        => array(
187                    'type' => 'object',
188                ),
189                'execute_callback'    => function () {
190                    $request = new \WP_REST_Request( 'GET', '/wc/v3/reports/reviews/totals' );
191                    $response = rest_do_request( $request );
192                    return blu_standardize_rest_response( $response );
193                },
194                'permission_callback' => fn() => current_user_can( 'view_woocommerce_reports' ),
195                'meta'                => array(
196                    'annotations' => array(
197                        'readonly'     => true,
198                        'destructive'  => false,
199                        'idempotent'   => true,
200                    ),
201                ),
202            )
203        );
204
205        // Sales report
206        blu_register_ability(
207            'blu/wc-reports-sales',
208            array(
209                'label'               => 'Get WooCommerce Sales Report',
210                'description'         => 'Get WooCommerce sales report',
211                'category'            => 'blu-mcp',
212                'input_schema'        => array(
213                    'type'       => 'object',
214                    'properties' => array(
215                        'period' => array(
216                            'type'        => 'string',
217                            'description' => 'Report period (week, month, year)',
218                        ),
219                    ),
220                ),
221                'execute_callback'    => function ( $input = null ) {
222                    $request = new \WP_REST_Request( 'GET', '/wc/v3/reports/sales' );
223                    if ( $input ) {
224                        $request->set_query_params( $input );
225                    }
226                    $response = rest_do_request( $request );
227                    return blu_standardize_rest_response( $response );
228                },
229                'permission_callback' => fn() => current_user_can( 'view_woocommerce_reports' ),
230                'meta'                => array(
231                    'annotations' => array(
232                        'readonly'     => true,
233                        'destructive'  => false,
234                        'idempotent'   => true,
235                    ),
236                ),
237            )
238        );
239    }
240}