'GET', 'permission_callback' => '__return_true', // The order key is the credential, checked in the callback. 'args' => array( 'order' => array( 'required' => true, 'type' => 'integer' ), 'key' => array( 'required' => true, 'type' => 'string' ), ), 'callback' => function ( WP_REST_Request $r ) { $order = sofrah_board_order_for_key( $r['order'], $r['key'] ); if ( ! $order ) { return new WP_Error( 'sofrah_board_not_found', __( 'Order not found.', 'sofrah-order-board' ), array( 'status' => 404 ) ); } $res = rest_ensure_response( sofrah_board_public_status( $order ) ); $res->header( 'Cache-Control', 'no-store' ); return $res; }, ) ); register_rest_route( $ns, '/status/cancel', array( 'methods' => 'POST', 'permission_callback' => '__return_true', // The order key is the credential, checked in the callback. 'args' => array( 'order' => array( 'required' => true, 'type' => 'integer' ), 'key' => array( 'required' => true, 'type' => 'string' ), ), 'callback' => function ( WP_REST_Request $r ) { $order = sofrah_board_order_for_key( $r['order'], $r['key'] ); if ( ! $order ) { return new WP_Error( 'sofrah_board_not_found', __( 'Order not found.', 'sofrah-order-board' ), array( 'status' => 404 ) ); } $done = sofrah_board_transition( $order->get_id(), 'cancelled_diner', 'diner', '' ); if ( is_wp_error( $done ) ) { return $done; } return rest_ensure_response( sofrah_board_public_status( wc_get_order( $order->get_id() ) ) ); }, ) ); register_rest_route( $ns, '/board', array( 'methods' => 'GET', 'permission_callback' => fn() => current_user_can( 'edit_shop_orders' ), 'callback' => function () { update_option( 'sofrah_board_board_seen', time(), false ); $res = rest_ensure_response( sofrah_board_board_data() ); $res->header( 'Cache-Control', 'no-store' ); return $res; }, ) ); register_rest_route( $ns, '/orders/(?P\d+)/transition', array( 'methods' => 'POST', 'permission_callback' => fn() => current_user_can( 'edit_shop_orders' ), 'args' => array( 'to' => array( 'required' => true, 'type' => 'string', 'enum' => array( 'accepted', 'rejected', 'preparing', 'ready', 'collected', 'not_collected', 'cancelled_restaurant' ) ), 'reason' => array( 'type' => 'string', 'default' => '', 'sanitize_callback' => 'sanitize_text_field' ), 'minutes' => array( 'type' => 'integer', 'default' => 0 ), ), 'callback' => function ( WP_REST_Request $r ) { if ( 'cancelled_restaurant' === $r['to'] && ! current_user_can( 'manage_woocommerce' ) ) { return new WP_Error( 'sofrah_board_forbidden', __( 'Only a manager can cancel an accepted order.', 'sofrah-order-board' ), array( 'status' => 403 ) ); } $done = sofrah_board_transition( (int) $r['id'], $r['to'], wp_get_current_user()->user_login, $r['reason'], (int) $r['minutes'] ); return is_wp_error( $done ) ? $done : rest_ensure_response( sofrah_board_board_data() ); }, ) ); register_rest_route( $ns, '/ordering', array( 'methods' => 'POST', 'permission_callback' => fn() => current_user_can( 'edit_shop_orders' ), 'args' => array( 'paused' => array( 'required' => true, 'type' => 'boolean' ) ), 'callback' => function ( WP_REST_Request $r ) { sofrah_board_set_paused( (bool) $r['paused'] ); return rest_ensure_response( sofrah_board_board_data() ); }, ) ); register_rest_route( $ns, '/orders/(?P\d+)/paid', array( 'methods' => 'POST', 'permission_callback' => fn() => current_user_can( 'edit_shop_orders' ), 'callback' => function ( WP_REST_Request $r ) { $done = sofrah_board_mark_paid( (int) $r['id'], wp_get_current_user()->user_login ); return is_wp_error( $done ) ? $done : rest_ensure_response( sofrah_board_board_data() ); }, ) ); } );