HEX
Server: Apache/2.4.65 (Ubuntu)
System: Linux ielts-store-v2 6.8.0-1036-gcp #38~22.04.1-Ubuntu SMP Thu Aug 14 01:19:18 UTC 2025 x86_64
User: root (0)
PHP: 7.2.34-54+ubuntu20.04.1+deb.sury.org+1
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,
Upload Files
File: /var/www/html/ielts-store/wp-content/plugins/automatewoo/includes/Rest_Api/Utilities/Pagination.php
<?php

namespace AutomateWoo\Rest_Api\Utilities;

use WP_REST_Request;
use WP_REST_Response;

defined( 'ABSPATH' ) || exit;

/**
 * REST API pagination utility class.
 *
 * Based off of
 *
 * @since 4.9.0
 */
class Pagination {

	/**
	 * The REST request object.
	 *
	 * @var WP_REST_Request
	 */
	protected $request;

	/**
	 * The REST response object.
	 *
	 * @var WP_REST_Request
	 */
	protected $response;

	/**
	 * The total items found.
	 *
	 * @var int
	 */
	protected $total_items;

	/**
	 * Pagination constructor.
	 *
	 * @param WP_REST_Request  $request
	 * @param WP_REST_Response $response
	 * @param int              $total_items
	 */
	public function __construct( WP_REST_Request $request, WP_REST_Response $response, $total_items ) {
		$this->request     = $request;
		$this->response    = $response;
		$this->total_items = absint( $total_items );
	}

	/**
	 * Add pagination headers to a response object and then return it.
	 *
	 * @return WP_REST_Response
	 */
	public function add_headers() {
		$current_page   = absint( $this->request->get_param( 'page' ) );
		$items_per_page = absint( $this->request->get_param( 'per_page' ) );
		$total_pages    = ceil( $this->total_items / $items_per_page );
		$link_base      = $this->get_link_base();

		$this->response->header( 'X-WP-Total', $this->total_items );
		$this->response->header( 'X-WP-TotalPages', $total_pages );

		if ( $current_page > 1 ) {
			$previous_page = $current_page - 1;
			if ( $previous_page > $total_pages ) {
				$previous_page = $total_pages;
			}
			$this->add_page_link( 'prev', $previous_page, $link_base );
		}

		if ( $total_pages > $current_page ) {
			$this->add_page_link( 'next', ( $current_page + 1 ), $link_base );
		}

		return $this->response;
	}

	/**
	 * Get base for links from the request object.
	 *
	 * @return string
	 */
	protected function get_link_base() {
		return add_query_arg( $this->request->get_query_params(), rest_url( $this->request->get_route() ) );
	}

	/**
	 * Add a page link.
	 *
	 * @param string $name      Page link name. e.g. prev.
	 * @param int    $page      Page number.
	 * @param string $link_base Base URL.
	 */
	protected function add_page_link( $name, $page, $link_base ) {
		$this->response->link_header( $name, add_query_arg( 'page', $page, $link_base ) );
	}

}