service = $service; } /** * {@inheritDoc} */ public function register_routes(): void { register_rest_route( $this->namespace, '/reports/(?P[a-z_]+)', array( 'args' => array( 'type' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), ), array( 'methods' => 'GET', 'callback' => array( $this, 'show' ), 'permission_callback' => array( $this, 'check_permission' ), 'args' => array( 'range' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_key', ), 'from' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), 'to' => array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', ), ), ), ) ); } /** * GET /reports/{type} * * @param WP_REST_Request $request Request. * @return WP_REST_Response|WP_Error */ public function show( WP_REST_Request $request ) { $type = (string) $request['type']; if ( ! in_array( $type, ReportService::TYPES, true ) ) { return new WP_Error( 'profit_engine_report_unknown', __( 'Unknown report type.', 'profit-engine' ), array( 'status' => 404 ) ); } return new WP_REST_Response( $this->service->build( $type, array( 'range' => (string) $request->get_param( 'range' ), 'from' => (string) $request->get_param( 'from' ), 'to' => (string) $request->get_param( 'to' ), ) ), 200 ); } }