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.0 3.4.1 3.4.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 3 weeks ago Accordion.php 6 days ago Alert.php 6 days ago AttachmentCard.php 6 days ago Avatar.php 6 days ago Badge.php 6 days ago BaseComponent.php 3 weeks ago Button.php 6 days ago ConfirmationModal.php 6 days ago CourseFilter.php 6 days ago DateFilter.php 6 days ago DropdownFilter.php 6 days ago EmptyState.php 6 days ago FileUploader.php 6 days ago InputField.php 6 days ago Modal.php 6 days ago Nav.php 6 days ago Pagination.php 6 days ago Popover.php 6 days ago PreviewTrigger.php 6 days ago Progress.php 6 days ago SearchFilter.php 6 days ago Sorting.php 6 days ago StarRating.php 6 days ago StarRatingInput.php 6 days ago StatusSelect.php 6 days ago SvgIcon.php 6 days ago Table.php 6 days ago Tabs.php 6 days ago Tooltip.php 6 days ago WPEditor.php 6 days ago
Pagination.php
336 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 * ```php
25 * // Minimal — current page from URL, default prev/next icons
26 * Pagination::make()
27 * ->current( (int) Input::get( 'current_page', 1 ) )
28 * ->total( $total_items )
29 * ->limit( 10 )
30 * ->render();
31 *
32 * // With custom prev/next icon markup
33 * Pagination::make()
34 * ->current( 2 )
35 * ->total( 200 )
36 * ->limit( tutor_utils()->get_option( 'pagination_per_page' ) )
37 * ->prev( SvgIcon::make()->name( Icon::CHEVRON_LEFT_2 )->flip_rtl()->get() )
38 * ->next( SvgIcon::make()->name( Icon::CHEVRON_RIGHT_2 )->flip_rtl()->get() )
39 * ->render();
40 *
41 * // Custom URL format (e.g., pretty-permalink pagination)
42 * Pagination::make()
43 * ->current( get_query_var( 'paged', 1 ) )
44 * ->total( $total_items )
45 * ->limit( 20 )
46 * ->format( '/page/%#%/' )
47 * ->render();
48 *
49 * // Show all page links (no ellipsis collapsing)
50 * Pagination::make()
51 * ->current( 1 )
52 * ->total( 50 )
53 * ->limit( 5 )
54 * ->show_all( true )
55 * ->render();
56 *
57 * // Extra CSS class on the nav wrapper
58 * Pagination::make()
59 * ->current( 1 )
60 * ->total( 100 )
61 * ->limit( 10 )
62 * ->attr( 'class', 'tutor-mt-6' )
63 * ->render();
64 *
65 * // Retrieve HTML without echoing
66 * $html = Pagination::make()->current( 1 )->total( 50 )->limit( 10 )->get();
67 * ```
68 *
69 * @since 4.0.0
70 */
71 class Pagination extends BaseComponent {
72
73 /**
74 * Current paginated value.
75 *
76 * @var int
77 */
78 protected $pagination_current = 1;
79
80 /**
81 * Total paginated value.
82 *
83 * @var int
84 */
85 protected $pagination_total = 1;
86
87 /**
88 * Pagination previous text.
89 *
90 * @var string
91 */
92 protected $prev;
93
94 /**
95 * Pagination next text.
96 *
97 * @var string
98 */
99 protected $next;
100
101 /**
102 * Pagination limit.
103 *
104 * @var int
105 */
106 protected $pagination_limit = 10;
107
108 /**
109 * Whether to show all links or not.
110 *
111 * @var bool
112 */
113 protected $show_all = false;
114
115 /**
116 * Pagination url format.
117 *
118 * @var string
119 */
120 protected $format;
121
122 /**
123 * Set the current number of pagination's.
124 *
125 * @since 4.0.0
126 *
127 * @param int $current the current number of pagination's.
128 *
129 * @return self
130 */
131 public function current( int $current = 1 ): self {
132 $this->pagination_current = $current;
133 return $this;
134 }
135
136 /**
137 * Set the total number of pagination's.
138 *
139 * @since 4.0.0
140 *
141 * @param int $total the current number of pagination's.
142 *
143 * @return self
144 */
145 public function total( int $total = 1 ): self {
146 $this->pagination_total = $total;
147 return $this;
148 }
149
150 /**
151 * Set the pagination limit for each page.
152 *
153 * @since 4.0.0
154 *
155 * @param integer $limit the pagination limit.
156 *
157 * @return self
158 */
159 public function limit( int $limit = 10 ): self {
160 $this->pagination_limit = $limit;
161 return $this;
162 }
163
164 /**
165 * Set pagination url format.
166 *
167 * @since 4.0.0
168 *
169 * @param string $format the url format.
170 *
171 * @return self
172 */
173 public function format( string $format ): self {
174 $this->format = $format;
175 return $this;
176 }
177
178 /**
179 * Whether to show all pagination links.
180 *
181 * @since 4.0.0
182 *
183 * @param boolean $show_all Show all pagination links or not.
184 *
185 * @return self
186 */
187 public function show_all( bool $show_all = false ): self {
188 $this->show_all = $show_all;
189 return $this;
190 }
191
192 /**
193 * Set previous text for pagination.
194 *
195 * @since 4.0.0
196 *
197 * @param string $prev the previous text.
198 *
199 * @return self
200 */
201 public function prev( string $prev ): self {
202 $this->prev = $prev;
203 return $this;
204 }
205
206 /**
207 * Set next text for pagination.
208 *
209 * @since 4.0.0
210 *
211 * @param string $next the next text.
212 *
213 * @return self
214 */
215 public function next( string $next ): self {
216 $this->next = $next;
217 return $this;
218 }
219
220 /**
221 * Render pagination info content.
222 *
223 * @since 4.0.0
224 *
225 * @return string the HTML content.
226 */
227 protected function render_pagination_info(): string {
228 $per_page = max( ceil( $this->pagination_total / $this->pagination_limit ), 1 );
229 $current = max( intval( $this->pagination_current ), 1 );
230
231 return sprintf(
232 '<span class="tutor-pagination-info" aria-live="polite">
233 %s
234 <span class="tutor-pagination-current">%d</span>
235 %s
236 <span class="tutor-pagination-total">%d</span>
237 </span>',
238 __( 'Page', 'tutor' ),
239 $current,
240 __( 'of', 'tutor' ),
241 $per_page
242 );
243 }
244
245 /**
246 * Get Paginated links.
247 *
248 * @since 4.0.0
249 *
250 * @return array|null
251 */
252 protected function get_paginated_links_list() {
253 $per_page = max( ceil( $this->pagination_total / $this->pagination_limit ), 1 );
254 $current = max( intval( $this->pagination_current ), 1 );
255 $format = ! empty( $this->format ) ? $this->format : '?current_page=%#%';
256 $prev_text = ! empty( $this->prev ) ? $this->prev : SvgIcon::make()->name( Icon::CHEVRON_LEFT_2 )->flip_rtl()->get();
257 $next_text = ! empty( $this->next ) ? $this->next : SvgIcon::make()->name( Icon::CHEVRON_RIGHT_2 )->flip_rtl()->get();
258 return paginate_links(
259 array(
260 'format' => $format,
261 'current' => $current,
262 'total' => $per_page,
263 'prev_text' => $prev_text,
264 'next_text' => $next_text,
265 'type' => 'array',
266 )
267 );
268 }
269
270 /**
271 * Get the final Pagination HTML Component.
272 *
273 * @since 4.0.0
274 *
275 * @return string
276 */
277 public function get(): string {
278 if ( $this->pagination_total <= $this->pagination_limit ) {
279 return '';
280 }
281
282 $pagination_links = $this->get_paginated_links_list() ?? array();
283 $pagination_info = $this->render_pagination_info() ?? '';
284 $links = '';
285
286 $classes = array( 'tutor-pagination' );
287 if ( isset( $this->attributes['class'] ) ) {
288 $classes[] = $this->attributes['class'];
289 }
290
291 if ( count( $pagination_links ) ) {
292 foreach ( $pagination_links as $link ) {
293 $link = str_replace( 'page-numbers dots', 'tutor-pagination-ellipsis', $link );
294 $link = str_replace( 'page-numbers', 'tutor-pagination-item', $link );
295 $link = preg_replace( '/\bcurrent\b(?=[^"]*"[^"]*$)/', 'tutor-pagination-item-active', $link );
296 $link = str_replace( 'prev', 'tutor-pagination-item-prev', $link );
297 $link = str_replace( 'next', 'tutor-pagination-item-next', $link );
298 $link = str_replace( 'tutor-pagination-item-activeColor', 'currentColor', $link );
299
300 if ( str_contains( $link, 'tutor-pagination-item-prev' ) ) {
301 $link = preg_replace(
302 '/<a\b/',
303 '<a aria-label="' . esc_attr__( 'Previous page', 'tutor' ) . '"',
304 $link,
305 1
306 );
307 }
308
309 if ( str_contains( $link, 'tutor-pagination-item-next' ) ) {
310 $link = preg_replace(
311 '/<a\b/',
312 '<a aria-label="' . esc_attr__( 'Next page', 'tutor' ) . '"',
313 $link,
314 1
315 );
316 }
317
318 $links .= sprintf( '<li>%s</li>', $link );
319 }
320 }
321
322 return sprintf(
323 '<nav class="%s" role="navigation" aria-label="%s">
324 %s
325 <ul class="tutor-pagination-list">
326 %s
327 </ul>
328 </nav>',
329 esc_attr( implode( ' ', $classes ) ),
330 esc_attr__( 'Pagination', 'tutor' ),
331 $pagination_info,
332 $links
333 );
334 }
335 }
336