PluginProbe
Groups – Memberships and Access Control / 4.7.0
Groups – Memberships and Access Control v4.7.0
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 4.7.0, at lib/core/class-groups-pagination.php

194 lines 5.6 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 * @var string
33 */
34 private $_pagination = null;
35
36 /**
37 * @var array
38 */
39 private $_pagination_args = null;
40
41 /**
42 * Create an instance.
43 *
44 * @param int $total_items how many items there are to display
45 * @param int $total_pages how many pages there are, normally leave set to null
46 * @param int $per_page how many results to show on each page
47 */
48 public function __construct( $total_items, $total_pages, $per_page ) {
49 $this->set_pagination_args(
50 array(
51 'total_items' => $total_items,
52 'total_pages' => $total_pages,
53 'per_page' => $per_page
54 )
55 );
56 }
57
58 /**
59 * Get the current page number
60 *
61 * @return int the current page number
62 */
63 public function get_pagenum() {
64 $paged = groups_sanitize_request( 'paged' );
65 $pagenum = absint( $paged ?? 0 );
66 if ( !$paged ) { // needed with rewritten page added
67 $current_url = groups_get_current_url();
68 $matches = array();
69 if ( preg_match( "/(\/page\/)(\d+)/", $current_url, $matches ) ) {
70 $pagenum = absint( $matches[2] );
71 }
72 }
73
74 if( isset( $this->_pagination_args['total_pages'] ) && $pagenum > $this->_pagination_args['total_pages'] ) {
75 $pagenum = $this->_pagination_args['total_pages'];
76 }
77 return max( 1, $pagenum );
78 }
79
80 /**
81 * An internal method that sets all the necessary pagination arguments
82 *
83 * @param array $args An associative array with information about the pagination
84 *
85 * @access protected
86 */
87 public function set_pagination_args( $args ) {
88 $args = wp_parse_args( $args, array(
89 'total_items' => 0,
90 'total_pages' => 0,
91 'per_page' => 0,
92 ) );
93
94 if ( !$args['total_pages'] && $args['per_page'] > 0 )
95 $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
96
97 $this->_pagination_args = $args;
98 }
99
100 /**
101 * Returns or displays the pagination.
102 *
103 * @param string $which where it's displayed
104 * @param boolean $echo displays if true, otherwise returns
105 *
106 * @return string|null
107 */
108 public function pagination( $which, $echo = false ) {
109
110 if ( empty( $this->_pagination_args ) ) {
111 return;
112 }
113
114 $total_items = isset( $this->_pagination_args['total_items'] ) ? $this->_pagination_args['total_items'] : 0;
115 $total_pages = isset( $this->_pagination_args['total_pages'] ) ? $this->_pagination_args['total_pages'] : 0;
116
117 $output = '<span class="displaying-num">';
118 /* translators: number of items */
119 $output .= sprintf( esc_html( _n( '%s item', '%s items', $total_items, 'groups' ) ), esc_html( number_format_i18n( $total_items ) ) );
120 $output .= '</span>';
121
122 $current = $this->get_pagenum();
123
124 $current_url = groups_get_current_url();
125
126 $current_url = remove_query_arg( array( 'hotkeys_highlight_last', 'hotkeys_highlight_first' ), $current_url );
127
128 // needs to remove rewritten added page
129 $current_url = preg_replace( '/\/page\/\d+/', '', $current_url );
130
131 $page_links = array();
132
133 $disable_first = $disable_last = '';
134 if ( $current == 1 )
135 $disable_first = ' disabled';
136 if ( $current == $total_pages )
137 $disable_last = ' disabled';
138
139 $page_links[] = sprintf( '<a class="%s" title="%s" href="%s">%s</a>',
140 'button first-page' . $disable_first,
141 esc_attr__( 'Go to the first page', 'groups' ),
142 esc_url( remove_query_arg( 'paged', $current_url ) ),
143 '&laquo;'
144 );
145
146 $page_links[] = sprintf( '<a class="%s" title="%s" href="%s">%s</a>',
147 'button prev-page' . $disable_first,
148 esc_attr__( 'Go to the previous page', 'groups' ),
149 esc_url( add_query_arg( 'paged', max( 1, $current-1 ), $current_url ) ),
150 '&lsaquo;'
151 );
152
153 if ( 'bottom' == $which )
154 $html_current_page = $current;
155 else
156 $html_current_page = sprintf( '<input class="current-page" title="%s" type="text" name="%s" value="%s" size="%d" />',
157 esc_attr__( 'Current page', 'groups' ),
158 esc_attr( 'paged' ),
159 $current,
160 strlen( $total_pages )
161 );
162
163 $html_total_pages = sprintf( '<span class="total-pages">%s</span>', number_format_i18n( $total_pages ) );
164 $page_links[] = '<span class="paging-input">' . sprintf( esc_html_x( '%1$s of %2$s', 'paging' ), $html_current_page, $html_total_pages ) . '</span>'; // phpcs:ignore WordPress.WP.I18n.MissingArgDomain, WordPress.WP.I18n.MissingTranslatorsComment
165
166 $page_links[] = sprintf( '<a class="%s" title="%s" href="%s">%s</a>',
167 'button next-page' . $disable_last,
168 esc_attr__( 'Go to the next page', 'groups' ),
169 esc_url( add_query_arg( 'paged', min( $total_pages, $current+1 ), $current_url ) ),
170 '&rsaquo;'
171 );
172
173 $page_links[] = sprintf( '<a class="%s" title="%s" href="%s">%s</a>',
174 'button last-page' . $disable_last,
175 esc_attr__( 'Go to the last page', 'groups' ),
176 esc_url( add_query_arg( 'paged', $total_pages, $current_url ) ),
177 '&raquo;'
178 );
179
180 $output .= "\n" . join( "\n", $page_links );
181
182 $page_class = $total_pages < 2 ? ' one-page' : '';
183
184 $this->_pagination = "<div class='tablenav-pages{$page_class}'>$output</div>";
185
186 if ( $echo ) {
187 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
188 echo $this->_pagination; // @phpstan-ignore return.missing
189 } else {
190 return $this->_pagination;
191 }
192 }
193 }
194