PluginProbe
Groups – Memberships and Access Control / 1.10.3
Groups – Memberships and Access Control v1.10.3
4.7.1 4.7.0 4.6.0 4.5.0 4.4.0 4.3.0 trunk 1.0.0-beta-1 1.0.0-beta-2 1.0.0-beta-3 1.0.0-beta-3b 1.0.0-beta-3c 1.0.0-beta-3d 1.1.4 1.1.5 1.10.0 1.10.1 1.10.2 1.10.3 1.11.0 1.11.1 1.11.2 1.11.3 1.12.0 1.13.0 All 131 releases
groups / lib / core / class-groups-pagination.php

class-groups-pagination.php in Groups – Memberships and Access Control 1.10.3, at lib/core/class-groups-pagination.php

169 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( !defined( 'ABSPATH' ) ) {
23 exit;
24 }
25
26 /**
27 * Pagination based on WP_List_Table.
28 */
29 class Groups_Pagination {
30
31 /**
32 *
33 * @param int $total_items how many items there are to display
34 * @param int $total_pages how many pages there are, normally leave set to null
35 * @param int $per_page how many results to show on each page
36 */
37 function __construct( $total_items, $total_pages, $per_page ) {
38 $this->set_pagination_args(
39 array(
40 'total_items' => $total_items,
41 'total_pages' => $total_pages,
42 'per_page' => $per_page
43 )
44 );
45 }
46
47 /**
48 * Get the current page number
49 * @return int the current page number
50 */
51 function get_pagenum() {
52 $pagenum = isset( $_REQUEST['paged'] ) ? absint( $_REQUEST['paged'] ) : 0;
53 if ( !isset( $_REQUEST['paged'] ) ) { // needed with rewritten page added
54 if ( preg_match( "/(\/page\/)(\d+)/", $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $matches ) ) {
55 $pagenum = absint( $matches[2] );
56 }
57 }
58
59 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) {
60 $pagenum = $this->_pagination_args['total_pages'];
61 }
62 return max( 1, $pagenum );
63 }
64
65 /**
66 * An internal method that sets all the necessary pagination arguments
67 *
68 * @param array $args An associative array with information about the pagination
69 * @access protected
70 */
71 function set_pagination_args( $args ) {
72 $args = wp_parse_args( $args, array(
73 'total_items' => 0,
74 'total_pages' => 0,
75 'per_page' => 0,
76 ) );
77
78 if ( !$args['total_pages'] && $args['per_page'] > 0 )
79 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
80
81 $this->_pagination_args = $args;
82 }
83
84 /**
85 * Returns or displays the pagination.
86 * @param string $which where it's displayed
87 * @param boolean $echo displays if true, otherwise returns
88 */
89 function pagination( $which, $echo = false ) {
90
91 if ( empty( $this->_pagination_args ) )
92 return;
93
94 extract( $this->_pagination_args );
95
96 $output = '<span class="displaying-num">' . sprintf( _n( '1 item', '%s items', $total_items ), number_format_i18n( $total_items ) ) . '</span>';
97
98 $current = $this->get_pagenum();
99
100 $current_url = ( is_ssl() ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
101
102 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
103
104 // needs to remove rewritten added page
105 $current_url = preg_replace( "/\/page\/\d+/", "", $current_url );
106
107 $page_links = array();
108
109 $disable_first = $disable_last = '';
110 if ( $current == 1 )
111 $disable_first = ' disabled';
112 if ( $current == $total_pages )
113 $disable_last = ' disabled';
114
115 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
116 'first-page' . $disable_first,
117 esc_attr__( 'Go to the first page' ),
118 esc_url( remove_query_arg( 'paged', $current_url ) ),
119 '&laquo;'
120 );
121
122 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
123 'prev-page' . $disable_first,
124 esc_attr__( 'Go to the previous page' ),
125 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
126 '&lsaquo;'
127 );
128
129 if ( 'bottom' == $which )
130 $html_current_page = $current;
131 else
132 $html_current_page = sprintf( "<input class='current-page' title='%s' type='text' name='%s' value='%s' size='%d' />",
133 esc_attr__( 'Current page' ),
134 esc_attr( 'paged' ),
135 $current,
136 strlen( $total_pages )
137 );
138
139 $html_total_pages = sprintf( "<span class='total-pages'>%s</span>", number_format_i18n( $total_pages ) );
140 $page_links[] = '<span class="paging-input">' . sprintf( _x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>';
141
142 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
143 'next-page' . $disable_last,
144 esc_attr__( 'Go to the next page' ),
145 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
146 '&rsaquo;'
147 );
148
149 $page_links[] = sprintf( "<a class='%s' title='%s' href='%s'>%s</a>",
150 'last-page' . $disable_last,
151 esc_attr__( 'Go to the last page' ),
152 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
153 '&raquo;'
154 );
155
156 $output .= "\n" . join( "\n", $page_links );
157
158 $page_class = $total_pages < 2 ? ' one-page' : '';
159
160 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
161
162 if ( $echo ) {
163 echo $this->_pagination;
164 } else {
165 return $this->_pagination;
166 }
167 }
168 }
169