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