max_num_pages; if ( $max_pages <= 1 ) { return ''; } $current_page = max( 1, $query->get( 'paged' ) ? (int) $query->get( 'paged' ) : 1 ); switch ( $type ) { case 'numbered': return self::render_numbered( $current_page, $max_pages, $grid_id ); case 'load_more': return self::render_load_more( $current_page, $max_pages, $config, $grid_id ); case 'infinite': return self::render_infinite( $current_page, $max_pages, $config, $grid_id ); case 'prev_next': return self::render_prev_next( $current_page, $max_pages, $grid_id ); default: return apply_filters( 'gridmaster_render_custom_pagination', '', $type, $query, $config, $grid_id ); } } /** * Builds the ordered sequence of page numbers and ellipsis markers for numbered pagination. * * @param int $current Current page number (1-indexed). * @param int $max Total pages. * @return array Sequence containing integers and '...' strings. */ public static function build_page_sequence( $current, $max ) { $current = max( 1, (int) $current ); $max = max( 1, (int) $max ); if ( $max <= 7 ) { return range( 1, $max ); } $candidates = array( 1, $max ); $win_start = max( 1, $current - 1 ); $win_end = min( $max, $current + 1 ); for ( $i = $win_start; $i <= $win_end; $i++ ) { $candidates[] = $i; } if ( $current <= 3 ) { $candidates[] = 2; $candidates[] = 3; $candidates[] = 4; } if ( $current >= $max - 2 ) { $candidates[] = $max - 1; $candidates[] = $max - 2; $candidates[] = $max - 3; } $candidates = array_values( array_unique( array_filter( $candidates, function ( $p ) use ( $max ) { return $p >= 1 && $p <= $max; } ) ) ); sort( $candidates ); $sequence = array(); $prev = 0; foreach ( $candidates as $p ) { if ( $prev > 0 ) { $gap = $p - $prev; if ( 2 === $gap ) { $sequence[] = $prev + 1; } elseif ( $gap > 2 ) { $sequence[] = '...'; } } $sequence[] = $p; $prev = $p; } return $sequence; } /** * Renders numbered pagination links with ellipsis truncation for large page counts. */ private static function render_numbered( $current, $max, $grid_id ) { $sequence = self::build_page_sequence( $current, $max ); ob_start(); ?>
= $max ) { return ''; } $loading = ! empty( $config['loading_text'] ) ? sanitize_text_field( $config['loading_text'] ) : __( 'Loading more posts...', 'ajax-filter-posts' ); ob_start(); ?>