Make WordPress Core

Changeset 4275


Ignore:
Timestamp:
10/03/2006 07:16:49 AM (18 years ago)
Author:
ryan
Message:

paginate_links() from mdawaffe. fixes #3159

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/users.php

    r4258 r4275  
    8585    function do_paging() {
    8686        if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
    87             $prev_page = ( $this->page > 1) ? true : false;
    88             $next_page = ( ($this->page * $this->users_per_page) < $this->total_users_for_query ) ? true : false;
    89             $this->paging_text = '';
    90             if ( $prev_page )
    91                 $this->paging_text .= '<p class="alignleft"><a href="' . add_query_arg(array('usersearch' => $this->search_term, 'userspage' => $this->page - 1), 'users.php?') . '">&laquo; Previous Page</a></p>';
    92             if ( $next_page )
    93                 $this->paging_text .= '<p class="alignright"><a href="' . add_query_arg(array('usersearch' => $this->search_term, 'userspage' => $this->page + 1), 'users.php?') . '">Next Page &raquo;</a></p>';
    94             if ( $prev_page || $next_page )
    95                 $this->paging_text .= '<br style="clear:both" />';
     87            $this->paging_text = paginate_links( array(
     88                'total' => ceil($this->total_users_for_query / $this->users_per_page),
     89                'current' => $this->page,
     90                'prev_text' => '&laquo; Previous Page',
     91                'next_text' => 'Next Page &raquo;',
     92                'base' => 'users.php?%_%',
     93                'format' => 'userspage=%#%',
     94                'add_args' => array( 'usersearch' => urlencode($this->search_term) )
     95            ) );
    9696        }
    9797    }
  • trunk/wp-includes/general-template.php

    r4218 r4275  
    878878    echo $output;
    879879}
     880
     881function paginate_links( $arg = '' ) {
     882    if ( is_array($arg) )
     883        $a = &$arg;
     884    else
     885        parse_str($arg, $a);
     886
     887    // Defaults
     888    $base = '%_%'; // http://example.com/all_posts.php%_% : %_% is replaced by format (below)
     889    $format = '?page=%#%'; // ?page=%#% : %#% is replaced by the page number
     890    $total = 1;
     891    $current = 0;
     892    $show_all = false;
     893    $prev_next = true;
     894    $prev_text = __('&laquo; Previous');
     895    $next_text = __('Next &raquo;');
     896    $end_size = 1; // How many numbers on either end including the end
     897    $mid_size = 2; // How many numbers to either side of current not including current
     898    $type = 'plain';
     899    $add_args = false; // array of query args to aadd
     900
     901    extract($a);
     902
     903    // Who knows what else people pass in $args
     904    $total    = (int) $total;
     905    $current  = (int) $current;
     906    $end_size = 0  < (int) $end_size ? (int) $end_size : 1; // Out of bounds?  Make it the default.
     907    $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2;
     908    $add_args = is_array($add_args) ? $add_args : false;
     909    $r = '';
     910    $page_links = array();
     911    $n = 0;
     912    $dots = false;
     913
     914    if ( $prev_next && $current && 1 < $current ) :
     915        $link = str_replace('%_%', 2 == $current ? '' : str_replace('%#%', $current - 1, $format), $base);
     916        if ( $add_args )
     917            $link = add_query_arg( $add_args, $link );
     918        $page_links[] = "<a class='prev page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$prev_text</a>";
     919    endif;
     920    for ( $n = 1; $n <= $total; $n++ ) :
     921        if ( $n == $current ) :
     922            $page_links[] = "<span class='page-numbers current'>$n</span>";
     923            $dots = true;
     924        else :
     925            if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) :
     926                $link = str_replace('%_%', 1 == $n ? '' : str_replace('%#%', $n, $format), $base);
     927                if ( $add_args )
     928                    $link = add_query_arg( $add_args, $link );
     929                $page_links[] = "<a class='page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$n</a>";
     930                $dots = true;
     931            elseif ( $dots && !$show_all ) :
     932                $page_links[] = "<span class='page-numbers dots'>...</span>";
     933                $dots = false;
     934            endif;
     935        endif;
     936    endfor;
     937    if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) :
     938        $link = str_replace('%_%', str_replace('%#%', $current + 1, $format), $base);
     939        if ( $add_args )
     940            $link = add_query_arg( $add_args, $link );
     941        $page_links[] = "<a class='next page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$next_text</a>";
     942    endif;
     943    switch ( $type ) :
     944        case 'array' :
     945            return $page_links;
     946            break;
     947        case 'list' :
     948            $r .= "<ul class='page-numbers'>\n\t<li>";
     949            $r .= join("</li>\n\t<li>", $page_links);
     950            $r .= "</li>\n</ul>\n";
     951            break;
     952        default :
     953            $r = join("\n", $page_links);
     954            break;
     955    endswitch;
     956    return $r;
     957}
    880958?>
Note: See TracChangeset for help on using the changeset viewer.