401 ) ); } return true; } /** * Check if user has admin permission. * * @param WP_REST_Request $request Request object. * @return bool|WP_Error */ public function check_admin_permission( $request ) { if ( ! current_user_can( 'manage_woocommerce' ) ) { return new WP_Error( 'rest_forbidden', esc_html__( 'You do not have permission to access this resource.', 'advanced-customer-account' ), array( 'status' => 403 ) ); } return true; } /** * Get current customer ID. * * @return int */ protected function get_customer_id() { return get_current_user_id(); } /** * Format price for display. * * @param float $price Price to format. * @return string */ protected function format_price( $price ) { return html_entity_decode( wp_strip_all_tags( wc_price( $price ) ) ); } /** * Format date for display. * * @param string $date Date string. * @param string $format Date format. * @return string */ protected function format_date( $date, $format = '' ) { if ( empty( $format ) ) { $format = get_option( 'date_format' ); } return date_i18n( $format, strtotime( $date ) ); } /** * Get pagination parameters. * * @return array */ protected function get_pagination_params() { return array( 'page' => array( 'description' => __( 'Current page of the collection.', 'advanced-customer-account' ), 'type' => 'integer', 'default' => 1, 'sanitize_callback' => 'absint', 'minimum' => 1, ), 'per_page' => array( 'description' => __( 'Maximum number of items to be returned in result set.', 'advanced-customer-account' ), 'type' => 'integer', 'default' => 10, 'sanitize_callback' => 'absint', 'minimum' => 1, 'maximum' => 100, ), ); } /** * Prepare pagination response. * * @param array $items Items array. * @param int $total Total items count. * @param int $page Current page. * @param int $per_page Items per page. * @return WP_REST_Response */ protected function prepare_paginated_response( $items, $total, $page, $per_page ) { $total_pages = (int) ceil( $total / $per_page ); $data = array( 'items' => $items, 'total' => (int) $total, 'total_pages' => $total_pages, 'page' => (int) $page, 'per_page' => (int) $per_page, ); return rest_ensure_response( $data ); } }