Constants
21 hours ago
Accordion.php
21 hours ago
Alert.php
21 hours ago
AttachmentCard.php
21 hours ago
Avatar.php
21 hours ago
Badge.php
21 hours ago
BaseComponent.php
21 hours ago
Button.php
21 hours ago
ConfirmationModal.php
21 hours ago
CourseFilter.php
21 hours ago
DateFilter.php
21 hours ago
DropdownFilter.php
21 hours ago
EmptyState.php
21 hours ago
FileUploader.php
21 hours ago
InputField.php
21 hours ago
Modal.php
21 hours ago
Nav.php
21 hours ago
Pagination.php
21 hours ago
Popover.php
21 hours ago
PreviewTrigger.php
21 hours ago
Progress.php
21 hours ago
SearchFilter.php
21 hours ago
Sorting.php
21 hours ago
StarRating.php
21 hours ago
StarRatingInput.php
21 hours ago
StatusSelect.php
21 hours ago
SvgIcon.php
21 hours ago
Table.php
21 hours ago
Tabs.php
21 hours ago
Tooltip.php
21 hours ago
WPEditor.php
21 hours 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 |