| 852 | function paginate_links( $arg = '' ) { |
| 853 | if ( is_array($arg) ) |
| 854 | $a = &$arg; |
| 855 | else |
| 856 | parse_str($arg, $a); |
| 857 | |
| 858 | // Defaults |
| 859 | $base = '%_%'; // http://example.com/all_posts.php%_% : %_% is replaced by format (below) |
| 860 | $format = '?page=%#%'; // ?page=%#% : %#% is replaced by the page number |
| 861 | $total = 1; |
| 862 | $current = 0; |
| 863 | $show_all = false; |
| 864 | $prev_next = true; |
| 865 | $prev_text = __('« Previous'); |
| 866 | $next_text = __('Next »'); |
| 867 | $end_size = 1; // How many numbers on either end including the end |
| 868 | $mid_size = 2; // How many numbers to either side of current not including current |
| 869 | $type = 'plain'; |
| 870 | $add_args = false; // array of query args to aadd |
| 871 | |
| 872 | extract($a); |
| 873 | |
| 874 | // Who knows what else people pass in $args |
| 875 | $total = (int) $total; |
| 876 | $current = (int) $current; |
| 877 | $end_size = 0 < (int) $end_size ? (int) $end_size : 1; // Out of bounds? Make it the default. |
| 878 | $mid_size = 0 <= (int) $mid_size ? (int) $mid_size : 2; |
| 879 | $add_args = is_array($add_args) ? $add_args : false; |
| 880 | $r = ''; |
| 881 | $page_links = array(); |
| 882 | $n = 0; |
| 883 | $dots = false; |
| 884 | |
| 885 | if ( $prev_next && $current && 1 < $current ) : |
| 886 | $link = str_replace('%_%', 2 == $current ? '' : str_replace('%#%', $current - 1, $format), $base); |
| 887 | if ( $add_args ) |
| 888 | $link = add_query_arg( $add_args, $link ); |
| 889 | $page_links[] = "<a class='prev page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$prev_text</a>"; |
| 890 | endif; |
| 891 | for ( $n = 1; $n <= $total; $n++ ) : |
| 892 | if ( $n == $current ) : |
| 893 | $page_links[] = "<span class='page-numbers current'>$n</span>"; |
| 894 | $dots = true; |
| 895 | else : |
| 896 | if ( $show_all || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : |
| 897 | $link = str_replace('%_%', 1 == $n ? '' : str_replace('%#%', $n, $format), $base); |
| 898 | if ( $add_args ) |
| 899 | $link = add_query_arg( $add_args, $link ); |
| 900 | $page_links[] = "<a class='page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$n</a>"; |
| 901 | $dots = true; |
| 902 | elseif ( $dots && !$show_all ) : |
| 903 | $page_links[] = "<span class='page-numbers dots'>...</span>"; |
| 904 | $dots = false; |
| 905 | endif; |
| 906 | endif; |
| 907 | endfor; |
| 908 | if ( $prev_next && $current && ( $current < $total || -1 == $total ) ) : |
| 909 | $link = str_replace('%_%', str_replace('%#%', $current + 1, $format), $base); |
| 910 | if ( $add_args ) |
| 911 | $link = add_query_arg( $add_args, $link ); |
| 912 | $page_links[] = "<a class='next page-numbers' href='" . wp_specialchars( $link, 1 ) . "'>$next_text</a>"; |
| 913 | endif; |
| 914 | switch ( $type ) : |
| 915 | case 'array' : |
| 916 | return $page_links; |
| 917 | break; |
| 918 | case 'list' : |
| 919 | $r .= "<ul class='page-numbers'>\n\t<li>"; |
| 920 | $r .= join("</li>\n\t<li>", $page_links); |
| 921 | $r .= "</li>\n</ul>\n"; |
| 922 | break; |
| 923 | default : |
| 924 | $r = join("\n", $page_links); |
| 925 | break; |
| 926 | endswitch; |
| 927 | return $r; |
| 928 | } |
| 929 | |