$extra Extra fields to merge into `data`. * @return WP_Error The error in the standard envelope. */ protected function error( string $code, string $message, int $status, array $extra = array() ): WP_Error { return new WP_Error( $code, $message, array_merge( array( 'status' => $status ), $extra ) ); } /** * The standard 404 envelope for a resource that does not exist. * * @since 1.0.0 * * @param string $message An optional override message. * @return WP_Error The `releasecart_not_found` error at HTTP 404. */ protected function notFound( string $message = '' ): WP_Error { return $this->error( 'releasecart_not_found', '' !== $message ? $message : __( 'The requested resource was not found.', 'releasecart' ), 404 ); } /** * Build a validation error with field messages. * * @since 1.0.0 * * @param array $errors The individual field-level validation messages. * @param string $message An optional override summary message. * @return WP_Error The `releasecart_invalid_params` error at HTTP 400. */ protected function invalidParams( array $errors, string $message = '' ): WP_Error { return $this->error( 'releasecart_invalid_params', '' !== $message ? $message : __( 'One or more parameters were invalid.', 'releasecart' ), 400, array( 'errors' => array_values( $errors ) ) ); } /** * Build a safe server error response. * * @since 1.0.0 * * @param string $message An optional safe message. * @return WP_Error The `releasecart_server_error` error at HTTP 500. */ protected function serverError( string $message = '' ): WP_Error { return $this->error( 'releasecart_server_error', '' !== $message ? $message : __( 'The request could not be completed. Please try again.', 'releasecart' ), 500 ); } /** * Add collection pagination headers. * * @since 1.0.0 * * @param WP_REST_Response $response The collection response to decorate. * @param int $total The total number of matching records. * @param int $perPage The page size used for the query. * @return WP_REST_Response The same response, with pagination headers set. */ protected function withPagination( WP_REST_Response $response, int $total, int $perPage ): WP_REST_Response { $totalPages = $perPage > 0 ? (int) ceil( $total / $perPage ) : 0; $response->header( 'X-WP-Total', (string) $total ); $response->header( 'X-WP-TotalPages', (string) $totalPages ); return $response; } }