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.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / components / Pagination.php
tutor / components Last commit date
Constants 4 days ago Accordion.php 4 days ago Alert.php 4 days ago AttachmentCard.php 4 days ago Avatar.php 4 days ago Badge.php 4 days ago BaseComponent.php 4 days ago Button.php 4 days ago ConfirmationModal.php 4 days ago CourseFilter.php 4 days ago DateFilter.php 4 days ago DropdownFilter.php 4 days ago EmptyState.php 4 days ago FileUploader.php 4 days ago InputField.php 4 days ago Modal.php 4 days ago Nav.php 4 days ago Pagination.php 4 days ago Popover.php 4 days ago PreviewTrigger.php 4 days ago Progress.php 4 days ago SearchFilter.php 4 days ago Sorting.php 4 days ago StarRating.php 4 days ago StarRatingInput.php 4 days ago StatusSelect.php 4 days ago SvgIcon.php 4 days ago Table.php 4 days ago Tabs.php 4 days ago Tooltip.php 4 days ago WPEditor.php 4 days ago
Pagination.php
301 lines
1 <?php
2 /**
3 * Pagination Component Class.
4 *
5 * @package Tutor\Components
6 * @author Themeum
7 * @link https://themeum.com
8 * @since 4.0.0
9 */
10
11 namespace Tutor\Components;
12
13 use TUTOR\Icon;
14
15 defined( 'ABSPATH' ) || exit;
16
17 /**
18 * Class Pagination
19 *
20 * Responsible for rendering pagination component.
21 *
22 * // Example Usage :
23 *
24 * ```
25 * Pagination::make()
26 * ->current( 2 )
27 * ->total( 200 )
28 * ->limit( tutor_utils()->get_option( 'pagination_per_page' ) )
29 * ->prev( SvgIcon::make()->name( Icon::CHEVRON_LEFT_2 )->get() )
30 * ->next( SvgIcon::make()->name( Icon::CHEVRON_RIGHT_2 )->get() )
31 * ->render();
32 * ```
33 *
34 * @since 4.0.0
35 */
36 class Pagination extends BaseComponent {
37
38 /**
39 * Current paginated value.
40 *
41 * @var int
42 */
43 protected $pagination_current = 1;
44
45 /**
46 * Total paginated value.
47 *
48 * @var int
49 */
50 protected $pagination_total = 1;
51
52 /**
53 * Pagination previous text.
54 *
55 * @var string
56 */
57 protected $prev;
58
59 /**
60 * Pagination next text.
61 *
62 * @var string
63 */
64 protected $next;
65
66 /**
67 * Pagination limit.
68 *
69 * @var int
70 */
71 protected $pagination_limit = 10;
72
73 /**
74 * Whether to show all links or not.
75 *
76 * @var bool
77 */
78 protected $show_all = false;
79
80 /**
81 * Pagination url format.
82 *
83 * @var string
84 */
85 protected $format;
86
87 /**
88 * Set the current number of pagination's.
89 *
90 * @since 4.0.0
91 *
92 * @param int $current the current number of pagination's.
93 *
94 * @return self
95 */
96 public function current( int $current = 1 ): self {
97 $this->pagination_current = $current;
98 return $this;
99 }
100
101 /**
102 * Set the total number of pagination's.
103 *
104 * @since 4.0.0
105 *
106 * @param int $total the current number of pagination's.
107 *
108 * @return self
109 */
110 public function total( int $total = 1 ): self {
111 $this->pagination_total = $total;
112 return $this;
113 }
114
115 /**
116 * Set the pagination limit for each page.
117 *
118 * @since 4.0.0
119 *
120 * @param integer $limit the pagination limit.
121 *
122 * @return self
123 */
124 public function limit( int $limit = 10 ): self {
125 $this->pagination_limit = $limit;
126 return $this;
127 }
128
129 /**
130 * Set pagination url format.
131 *
132 * @since 4.0.0
133 *
134 * @param string $format the url format.
135 *
136 * @return self
137 */
138 public function format( string $format ): self {
139 $this->format = $format;
140 return $this;
141 }
142
143 /**
144 * Whether to show all pagination links.
145 *
146 * @since 4.0.0
147 *
148 * @param boolean $show_all Show all pagination links or not.
149 *
150 * @return self
151 */
152 public function show_all( bool $show_all = false ): self {
153 $this->show_all = $show_all;
154 return $this;
155 }
156
157 /**
158 * Set previous text for pagination.
159 *
160 * @since 4.0.0
161 *
162 * @param string $prev the previous text.
163 *
164 * @return self
165 */
166 public function prev( string $prev ): self {
167 $this->prev = $prev;
168 return $this;
169 }
170
171 /**
172 * Set next text for pagination.
173 *
174 * @since 4.0.0
175 *
176 * @param string $next the next text.
177 *
178 * @return self
179 */
180 public function next( string $next ): self {
181 $this->next = $next;
182 return $this;
183 }
184
185 /**
186 * Render pagination info content.
187 *
188 * @since 4.0.0
189 *
190 * @return string the HTML content.
191 */
192 protected function render_pagination_info(): string {
193 $per_page = max( ceil( $this->pagination_total / $this->pagination_limit ), 1 );
194 $current = max( intval( $this->pagination_current ), 1 );
195
196 return sprintf(
197 '<span class="tutor-pagination-info" aria-live="polite">
198 %s
199 <span class="tutor-pagination-current">%d</span>
200 %s
201 <span class="tutor-pagination-total">%d</span>
202 </span>',
203 __( 'Page', 'tutor' ),
204 $current,
205 __( 'of', 'tutor' ),
206 $per_page
207 );
208 }
209
210 /**
211 * Get Paginated links.
212 *
213 * @since 4.0.0
214 *
215 * @return array|null
216 */
217 protected function get_paginated_links_list() {
218 $per_page = max( ceil( $this->pagination_total / $this->pagination_limit ), 1 );
219 $current = max( intval( $this->pagination_current ), 1 );
220 $format = ! empty( $this->format ) ? $this->format : '?current_page=%#%';
221 $prev_text = ! empty( $this->prev ) ? $this->prev : SvgIcon::make()->name( Icon::CHEVRON_LEFT_2 )->flip_rtl()->get();
222 $next_text = ! empty( $this->next ) ? $this->next : SvgIcon::make()->name( Icon::CHEVRON_RIGHT_2 )->flip_rtl()->get();
223 return paginate_links(
224 array(
225 'format' => $format,
226 'current' => $current,
227 'total' => $per_page,
228 'prev_text' => $prev_text,
229 'next_text' => $next_text,
230 'type' => 'array',
231 )
232 );
233 }
234
235 /**
236 * Get the final Pagination HTML Component.
237 *
238 * @since 4.0.0
239 *
240 * @return string
241 */
242 public function get(): string {
243 if ( $this->pagination_total <= $this->pagination_limit ) {
244 return '';
245 }
246
247 $pagination_links = $this->get_paginated_links_list() ?? array();
248 $pagination_info = $this->render_pagination_info() ?? '';
249 $links = '';
250
251 $classes = array( 'tutor-pagination' );
252 if ( isset( $this->attributes['class'] ) ) {
253 $classes[] = $this->attributes['class'];
254 }
255
256 if ( count( $pagination_links ) ) {
257 foreach ( $pagination_links as $link ) {
258 $link = str_replace( 'page-numbers dots', 'tutor-pagination-ellipsis', $link );
259 $link = str_replace( 'page-numbers', 'tutor-pagination-item', $link );
260 $link = preg_replace( '/\bcurrent\b(?=[^"]*"[^"]*$)/', 'tutor-pagination-item-active', $link );
261 $link = str_replace( 'prev', 'tutor-pagination-item-prev', $link );
262 $link = str_replace( 'next', 'tutor-pagination-item-next', $link );
263 $link = str_replace( 'tutor-pagination-item-activeColor', 'currentColor', $link );
264
265 if ( str_contains( $link, 'tutor-pagination-item-prev' ) ) {
266 $link = preg_replace(
267 '/<a\b/',
268 '<a aria-label="' . esc_attr__( 'Previous page', 'tutor' ) . '"',
269 $link,
270 1
271 );
272 }
273
274 if ( str_contains( $link, 'tutor-pagination-item-next' ) ) {
275 $link = preg_replace(
276 '/<a\b/',
277 '<a aria-label="' . esc_attr__( 'Next page', 'tutor' ) . '"',
278 $link,
279 1
280 );
281 }
282
283 $links .= sprintf( '<li>%s</li>', $link );
284 }
285 }
286
287 return sprintf(
288 '<nav class="%s" role="navigation" aria-label="%s">
289 %s
290 <ul class="tutor-pagination-list">
291 %s
292 </ul>
293 </nav>',
294 esc_attr( implode( ' ', $classes ) ),
295 esc_attr__( 'Pagination', 'tutor' ),
296 $pagination_info,
297 $links
298 );
299 }
300 }
301