| 1 |
<?php |
| 2 |
/** |
| 3 |
* class-groups-pagination.php |
| 4 |
* |
| 5 |
* Copyright (c) "kento" Karim Rahimpur www.itthinx.com |
| 6 |
* |
| 7 |
* This code is released under the GNU General Public License. |
| 8 |
* See COPYRIGHT.txt and LICENSE.txt. |
| 9 |
* |
| 10 |
* This code is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* This header and all notices must be kept intact. |
| 16 |
* |
| 17 |
* @author Karim Rahimpur |
| 18 |
* @package groups |
| 19 |
* @since groups 1.0.0 |
| 20 |
*/ |
| 21 |
|
| 22 |
/** |
| 23 |
* Pagination based on WP_List_Table. |
| 24 |
*/ |
| 25 |
class Groups_Pagination { |
| 26 |
|
| 27 |
/** |
| 28 |
* |
| 29 |
* @param int $total_items how many items there are to display |
| 30 |
* @param int $total_pages how many pages there are, normally leave set to null |
| 31 |
* @param int $per_page how many results to show on each page |
| 32 |
*/ |
| 33 |
function Groups_Pagination($total_items, $total_pages, $per_page) { |
| 34 |
$this->set_pagination_args( |
| 35 |
array( |
| 36 |
'total_items' => $total_items, |
| 37 |
'total_pages' => $total_pages, |
| 38 |
'per_page' => $per_page |
| 39 |
) |
| 40 |
); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Get the current page number |
| 45 |
* @return int the current page number |
| 46 |
*/ |
| 47 |
function get_pagenum() { |
| 48 |
$pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0; |
| 49 |
if ( !isset( $_REQUEST['paged'] ) ) { // needed with rewritten page added |
| 50 |
if ( preg_match( "/(\/page\/)(\d+)/", $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $matches ) ) { |
| 51 |
$pagenum = absint( $matches[2] ); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) |
| 56 |
$pagenum = $this->_pagination_args['total_pages']; |
| 57 |
|
| 58 |
return max( 1, $pagenum ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* An internal method that sets all the necessary pagination arguments |
| 63 |
* |
| 64 |
* @param array $args An associative array with information about the pagination |
| 65 |
* @access protected |
| 66 |
*/ |
| 67 |
function set_pagination_args( $args ) { |
| 68 |
$args = wp_parse_args( $args, array( |
| 69 |
'total_items' => 0, |
| 70 |
'total_pages' => 0, |
| 71 |
'per_page' => 0, |
| 72 |
) ); |
| 73 |
|
| 74 |
if ( !$args['total_pages'] && $args['per_page'] > 0 ) |
| 75 |
$args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] ); |
| 76 |
|
| 77 |
$this->_pagination_args = $args; |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Returns or displays the pagination. |
| 82 |
* @param string $which where it's displayed |
| 83 |
* @param boolean $echo displays if true, otherwise returns |
| 84 |
*/ |
| 85 |
function pagination( $which, $echo = false ) { |
| 86 |
|
| 87 |
if ( empty( $this->_pagination_args ) ) |
| 88 |
return; |
| 89 |
|
| 90 |
extract( $this->_pagination_args ); |
| 91 |
|
| 92 |
$output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>'; |
| 93 |
|
| 94 |
$current = $this->get_pagenum(); |
| 95 |
|
| 96 |
$current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; |
| 97 |
|
| 98 |
$current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url ); |
| 99 |
|
| 100 |
// needs to remove rewritten added page |
| 101 |
$current_url = preg_replace( "/\/page\/\d+/", "", $current_url ); |
| 102 |
|
| 103 |
$page_links = array(); |
| 104 |
|
| 105 |
$disable_first = $disable_last = ''; |
| 106 |
if ( $current == 1 ) |
| 107 |
$disable_first = ' disabled'; |
| 108 |
if ( $current == $total_pages ) |
| 109 |
$disable_last = ' disabled'; |
| 110 |
|
| 111 |
$page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>", |
| 112 |
'first-page' . $disable_first, |
| 113 |
esc_attr__( 'Go to the first page' ), |
| 114 |
esc_url( remove_query_arg( 'paged', $current_url ) ), |
| 115 |
'«' |
| 116 |
); |
| 117 |
|
| 118 |
$page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>", |
| 119 |
'prev-page' . $disable_first, |
| 120 |
esc_attr__( 'Go to the previous page' ), |
| 121 |
esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ), |
| 122 |
'‹' |
| 123 |
); |
| 124 |
|
| 125 |
if ( 'bottom' == $which ) |
| 126 |
$html_current_page = $current; |
| 127 |
else |
| 128 |
$html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='%s' value='%s' size='%d' />", |
| 129 |
esc_attr__( 'Current page' ), |
| 130 |
esc_attr( 'paged' ), |
| 131 |
$current, |
| 132 |
strlen( $total_pages ) |
| 133 |
); |
| 134 |
|
| 135 |
$html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) ); |
| 136 |
$page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>'; |
| 137 |
|
| 138 |
$page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>", |
| 139 |
'next-page' . $disable_last, |
| 140 |
esc_attr__( 'Go to the next page' ), |
| 141 |
esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ), |
| 142 |
'›' |
| 143 |
); |
| 144 |
|
| 145 |
$page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>", |
| 146 |
'last-page' . $disable_last, |
| 147 |
esc_attr__( 'Go to the last page' ), |
| 148 |
esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ), |
| 149 |
'»' |
| 150 |
); |
| 151 |
|
| 152 |
$output .= "\n" . join( "\n", $page_links ); |
| 153 |
|
| 154 |
$page_class = $total_pages < 2 ? ' one-page' : ''; |
| 155 |
|
| 156 |
$this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>"; |
| 157 |
|
| 158 |
if ( $echo ) { |
| 159 |
echo $this->_pagination; |
| 160 |
} else { |
| 161 |
return $this->_pagination; |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
?> |