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 |