getInputs(); $order_id = $inputs['order_id'] ?? '?'; $status = $inputs['status'] ?? '?'; return "Change order #{$order_id} status to \"{$status}\""; } /** * Human-readable version of tool inputs for the approval card. * * @return array */ public function get_approval_inputs(): array { return $this->getInputs(); } /** * Execute the tool * * @param int $order_id WooCommerce order ID. * @param string $status New order status. * @return array */ public function __invoke( int $order_id, string $status ): array { $check = $this->guard( 'manage_woocommerce', 'manage_options', 'wpcafe_manage_orders' ); if ( $check ) { return $check; } if ( ! function_exists( 'wc_get_order_statuses' ) ) { return [ 'error' => 'WooCommerce is not active.' ]; } if ( $order_id <= 0 ) { return [ 'error' => 'order_id must be a positive integer.' ]; } if ( empty( trim( $status ) ) ) { return [ 'error' => 'status is required.' ]; } $valid_statuses = array_keys( wc_get_order_statuses() ); $prefixed = 'wc-' === substr( $status, 0, 3 ) ? $status : 'wc-' . $status; if ( ! in_array( $prefixed, $valid_statuses, true ) ) { $readable = implode( ', ', array_map( fn( $s ) => 0 === strpos( $s, 'wc-' ) ? substr( $s, 3 ) : $s, $valid_statuses ) ); return [ 'error' => "Invalid status '{$status}'. Valid values: {$readable}." ]; } try { $request = new \WP_REST_Request( 'PATCH', '/wpcafe/v2/food-orders/' . $order_id ); $request->set_url_params( [ 'id' => $order_id ] ); $request->set_param( 'status', $status ); $controller = new \WpCafe\FoodOrder\Controllers\Food_Order_Controller(); $response = $controller->update_item_status( $request ); $data = $this->extract_response( $response, 'Failed to update order status.' ); if ( isset( $data['error'] ) ) { return $data; } return [ 'success' => true, 'order_id' => $order_id, 'status' => $status, 'message' => "Order #{$order_id} status updated to \"{$status}\".", ]; } catch ( \Throwable $e ) { Log::error( 'Wpcafe_Update_Order_Status_Tool error', [ 'error' => $e->getMessage() ] ); return [ 'error' => 'Failed to update order status: ' . $e->getMessage() ]; } } }